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;
282 if (!isa<ConstantSDNode>(Op))
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;
295 if (!isa<ConstantFPSDNode>(Op))
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;
325 if (!isa<ConstantSDNode>(Op))
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.
380template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
381 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
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");
443 case ISD::VP_REDUCE_FADD:
444 case ISD::VP_REDUCE_SEQ_FADD:
445 return ISD::FADD;
448 case ISD::VP_REDUCE_FMUL:
449 case ISD::VP_REDUCE_SEQ_FMUL:
450 return ISD::FMUL;
452 case ISD::VP_REDUCE_ADD:
453 return ISD::ADD;
455 case ISD::VP_REDUCE_MUL:
456 return ISD::MUL;
458 case ISD::VP_REDUCE_AND:
459 return ISD::AND;
461 case ISD::VP_REDUCE_OR:
462 return ISD::OR;
464 case ISD::VP_REDUCE_XOR:
465 return ISD::XOR;
467 case ISD::VP_REDUCE_SMAX:
468 return ISD::SMAX;
470 case ISD::VP_REDUCE_SMIN:
471 return ISD::SMIN;
473 case ISD::VP_REDUCE_UMAX:
474 return ISD::UMAX;
476 case ISD::VP_REDUCE_UMIN:
477 return ISD::UMIN;
479 case ISD::VP_REDUCE_FMAX:
480 return ISD::FMAXNUM;
482 case ISD::VP_REDUCE_FMIN:
483 return ISD::FMINNUM;
485 case ISD::VP_REDUCE_FMAXIMUM:
486 return ISD::FMAXIMUM;
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
629 return getSetCCInverseImpl(Op, Type.isInteger());
630}
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.
719 ArrayRef<SDValue> Ops) {
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.
728 ArrayRef<SDUse> Ops) {
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: {
752 const ConstantSDNode *C = cast<ConstantSDNode>(N);
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;
765 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
766 ID.AddPointer(GA->getGlobal());
767 ID.AddInteger(GA->getOffset());
768 ID.AddInteger(GA->getTargetFlags());
769 break;
770 }
771 case ISD::BasicBlock:
772 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
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;
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;
799 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
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: {
810 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
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: {
857 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
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: {
864 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
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: {
871 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
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: {
879 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
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: {
887 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
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: {
895 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
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: {
903 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
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: {
911 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
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 }
920 case ISD::ATOMIC_SWAP:
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: {
948 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(N);
949 ID.AddInteger(ASC->getSrcAddressSpace());
950 ID.AddInteger(ASC->getDestAddressSpace());
951 break;
952 }
954 case ISD::BlockAddress: {
955 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
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;
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
1119void SDDbgInfo::erase(const SDNode *Node) {
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.
1130 removeOperands(N);
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 };
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 };
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
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
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() ||
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.
2138 std::swap(N1, N2);
2140}
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};
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
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();
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
2819 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2820 const APInt &C2 = N2C->getAPIntValue();
2821 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
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<uint64_t>
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<uint64_t>
3317 EVT VT = V.getValueType();
3318 APInt DemandedElts = VT.isFixedLengthVector()
3320 : APInt(1, 1);
3321 return getValidShiftAmount(V, DemandedElts, Depth);
3322}
3323
3324std::optional<uint64_t>
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<uint64_t>
3338 EVT VT = V.getValueType();
3339 APInt DemandedElts = VT.isFixedLengthVector()
3341 : APInt(1, 1);
3342 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3343}
3344
3345std::optional<uint64_t>
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<uint64_t>
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.Zero.setAllBits(); Known.One.setAllBits();
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;
3488 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
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.Zero.setAllBits(); Known.One.setAllBits();
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.Zero.setAllBits(); Known.One.setAllBits();
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.One.setAllBits();
3553 Known.Zero.setAllBits();
3554 if (!!DemandedSubElts) {
3555 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3556 if (Known.isUnknown())
3557 break; // early-out.
3558 }
3559 if (!!DemandedSrcElts) {
3560 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3561 Known = Known.intersectWith(Known2);
3562 }
3563 break;
3564 }
3566 // Offset the demanded elts by the subvector index.
3567 SDValue Src = Op.getOperand(0);
3568 // Bail until we can represent demanded elements for scalable vectors.
3569 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3570 break;
3571 uint64_t Idx = Op.getConstantOperandVal(1);
3572 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3573 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3574 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3575 break;
3576 }
3577 case ISD::SCALAR_TO_VECTOR: {
3578 if (Op.getValueType().isScalableVector())
3579 break;
3580 // We know about scalar_to_vector as much as we know about it source,
3581 // which becomes the first element of otherwise unknown vector.
3582 if (DemandedElts != 1)
3583 break;
3584
3585 SDValue N0 = Op.getOperand(0);
3586 Known = computeKnownBits(N0, Depth + 1);
3587 if (N0.getValueSizeInBits() != BitWidth)
3588 Known = Known.trunc(BitWidth);
3589
3590 break;
3591 }
3592 case ISD::BITCAST: {
3593 if (Op.getValueType().isScalableVector())
3594 break;
3595
3596 SDValue N0 = Op.getOperand(0);
3597 EVT SubVT = N0.getValueType();
3598 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3599
3600 // Ignore bitcasts from unsupported types.
3601 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3602 break;
3603
3604 // Fast handling of 'identity' bitcasts.
3605 if (BitWidth == SubBitWidth) {
3606 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3607 break;
3608 }
3609
3610 bool IsLE = getDataLayout().isLittleEndian();
3611
3612 // Bitcast 'small element' vector to 'large element' scalar/vector.
3613 if ((BitWidth % SubBitWidth) == 0) {
3614 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3615
3616 // Collect known bits for the (larger) output by collecting the known
3617 // bits from each set of sub elements and shift these into place.
3618 // We need to separately call computeKnownBits for each set of
3619 // sub elements as the knownbits for each is likely to be different.
3620 unsigned SubScale = BitWidth / SubBitWidth;
3621 APInt SubDemandedElts(NumElts * SubScale, 0);
3622 for (unsigned i = 0; i != NumElts; ++i)
3623 if (DemandedElts[i])
3624 SubDemandedElts.setBit(i * SubScale);
3625
3626 for (unsigned i = 0; i != SubScale; ++i) {
3627 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3628 Depth + 1);
3629 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3630 Known.insertBits(Known2, SubBitWidth * Shifts);
3631 }
3632 }
3633
3634 // Bitcast 'large element' scalar/vector to 'small element' vector.
3635 if ((SubBitWidth % BitWidth) == 0) {
3636 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3637
3638 // Collect known bits for the (smaller) output by collecting the known
3639 // bits from the overlapping larger input elements and extracting the
3640 // sub sections we actually care about.
3641 unsigned SubScale = SubBitWidth / BitWidth;
3642 APInt SubDemandedElts =
3643 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3644 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3645
3646 Known.Zero.setAllBits(); Known.One.setAllBits();
3647 for (unsigned i = 0; i != NumElts; ++i)
3648 if (DemandedElts[i]) {
3649 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3650 unsigned Offset = (Shifts % SubScale) * BitWidth;
3651 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3652 // If we don't know any bits, early out.
3653 if (Known.isUnknown())
3654 break;
3655 }
3656 }
3657 break;
3658 }
3659 case ISD::AND:
3660 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3661 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3662
3663 Known &= Known2;
3664 break;
3665 case ISD::OR:
3666 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3667 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3668
3669 Known |= Known2;
3670 break;
3671 case ISD::XOR:
3672 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3673 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3674
3675 Known ^= Known2;
3676 break;
3677 case ISD::MUL: {
3678 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3679 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3680 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3681 // TODO: SelfMultiply can be poison, but not undef.
3682 if (SelfMultiply)
3683 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3684 Op.getOperand(0), DemandedElts, false, Depth + 1);
3685 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3686
3687 // If the multiplication is known not to overflow, the product of a number
3688 // with itself is non-negative. Only do this if we didn't already computed
3689 // the opposite value for the sign bit.
3690 if (Op->getFlags().hasNoSignedWrap() &&
3691 Op.getOperand(0) == Op.getOperand(1) &&
3692 !Known.isNegative())
3693 Known.makeNonNegative();
3694 break;
3695 }
3696 case ISD::MULHU: {
3697 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3698 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3699 Known = KnownBits::mulhu(Known, Known2);
3700 break;
3701 }
3702 case ISD::MULHS: {
3703 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3704 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3705 Known = KnownBits::mulhs(Known, Known2);
3706 break;
3707 }
3708 case ISD::ABDU: {
3709 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3710 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3711 Known = KnownBits::abdu(Known, Known2);
3712 break;
3713 }
3714 case ISD::ABDS: {
3715 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3717 Known = KnownBits::abds(Known, Known2);
3718 unsigned SignBits1 =
3719 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3720 if (SignBits1 == 1)
3721 break;
3722 unsigned SignBits0 =
3723 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3724 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3725 break;
3726 }
3727 case ISD::UMUL_LOHI: {
3728 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3729 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3730 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3731 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3732 if (Op.getResNo() == 0)
3733 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3734 else
3735 Known = KnownBits::mulhu(Known, Known2);
3736 break;
3737 }
3738 case ISD::SMUL_LOHI: {
3739 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3740 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3741 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3742 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3743 if (Op.getResNo() == 0)
3744 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3745 else
3746 Known = KnownBits::mulhs(Known, Known2);
3747 break;
3748 }
3749 case ISD::AVGFLOORU: {
3750 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3751 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3752 Known = KnownBits::avgFloorU(Known, Known2);
3753 break;
3754 }
3755 case ISD::AVGCEILU: {
3756 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3757 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3758 Known = KnownBits::avgCeilU(Known, Known2);
3759 break;
3760 }
3761 case ISD::AVGFLOORS: {
3762 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3763 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3764 Known = KnownBits::avgFloorS(Known, Known2);
3765 break;
3766 }
3767 case ISD::AVGCEILS: {
3768 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3769 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3770 Known = KnownBits::avgCeilS(Known, Known2);
3771 break;
3772 }
3773 case ISD::SELECT:
3774 case ISD::VSELECT:
3775 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3776 // If we don't know any bits, early out.
3777 if (Known.isUnknown())
3778 break;
3779 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3780
3781 // Only known if known in both the LHS and RHS.
3782 Known = Known.intersectWith(Known2);
3783 break;
3784 case ISD::SELECT_CC:
3785 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3786 // If we don't know any bits, early out.
3787 if (Known.isUnknown())
3788 break;
3789 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3790
3791 // Only known if known in both the LHS and RHS.
3792 Known = Known.intersectWith(Known2);
3793 break;
3794 case ISD::SMULO:
3795 case ISD::UMULO:
3796 if (Op.getResNo() != 1)
3797 break;
3798 // The boolean result conforms to getBooleanContents.
3799 // If we know the result of a setcc has the top bits zero, use this info.
3800 // We know that we have an integer-based boolean since these operations
3801 // are only available for integer.
3802 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3804 BitWidth > 1)
3805 Known.Zero.setBitsFrom(1);
3806 break;
3807 case ISD::SETCC:
3808 case ISD::SETCCCARRY:
3809 case ISD::STRICT_FSETCC:
3810 case ISD::STRICT_FSETCCS: {
3811 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3812 // If we know the result of a setcc has the top bits zero, use this info.
3813 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3815 BitWidth > 1)
3816 Known.Zero.setBitsFrom(1);
3817 break;
3818 }
3819 case ISD::SHL: {
3820 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3821 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3822
3823 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3824 bool NSW = Op->getFlags().hasNoSignedWrap();
3825
3826 bool ShAmtNonZero = Known2.isNonZero();
3827
3828 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3829
3830 // Minimum shift low bits are known zero.
3831 if (std::optional<uint64_t> ShMinAmt =
3832 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3833 Known.Zero.setLowBits(*ShMinAmt);
3834 break;
3835 }
3836 case ISD::SRL:
3837 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3838 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3839 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3840 Op->getFlags().hasExact());
3841
3842 // Minimum shift high bits are known zero.
3843 if (std::optional<uint64_t> ShMinAmt =
3844 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3845 Known.Zero.setHighBits(*ShMinAmt);
3846 break;
3847 case ISD::SRA:
3848 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3849 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3850 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3851 Op->getFlags().hasExact());
3852 break;
3853 case ISD::FSHL:
3854 case ISD::FSHR:
3855 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3856 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3857
3858 // For fshl, 0-shift returns the 1st arg.
3859 // For fshr, 0-shift returns the 2nd arg.
3860 if (Amt == 0) {
3861 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3862 DemandedElts, Depth + 1);
3863 break;
3864 }
3865
3866 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3867 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3868 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3869 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3870 if (Opcode == ISD::FSHL) {
3871 Known <<= Amt;
3872 Known2 >>= BitWidth - Amt;
3873 } else {
3874 Known <<= BitWidth - Amt;
3875 Known2 >>= Amt;
3876 }
3877 Known = Known.unionWith(Known2);
3878 }
3879 break;
3880 case ISD::SHL_PARTS:
3881 case ISD::SRA_PARTS:
3882 case ISD::SRL_PARTS: {
3883 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3884
3885 // Collect lo/hi source values and concatenate.
3886 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3887 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3888 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3889 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3890 Known = Known2.concat(Known);
3891
3892 // Collect shift amount.
3893 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3894
3895 if (Opcode == ISD::SHL_PARTS)
3896 Known = KnownBits::shl(Known, Known2);
3897 else if (Opcode == ISD::SRA_PARTS)
3898 Known = KnownBits::ashr(Known, Known2);
3899 else // if (Opcode == ISD::SRL_PARTS)
3900 Known = KnownBits::lshr(Known, Known2);
3901
3902 // TODO: Minimum shift low/high bits are known zero.
3903
3904 if (Op.getResNo() == 0)
3905 Known = Known.extractBits(LoBits, 0);
3906 else
3907 Known = Known.extractBits(HiBits, LoBits);
3908 break;
3909 }
3911 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3912 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3913 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3914 break;
3915 }
3916 case ISD::CTTZ:
3917 case ISD::CTTZ_ZERO_UNDEF: {
3918 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3919 // If we have a known 1, its position is our upper bound.
3920 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3921 unsigned LowBits = llvm::bit_width(PossibleTZ);
3922 Known.Zero.setBitsFrom(LowBits);
3923 break;
3924 }
3925 case ISD::CTLZ:
3926 case ISD::CTLZ_ZERO_UNDEF: {
3927 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3928 // If we have a known 1, its position is our upper bound.
3929 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3930 unsigned LowBits = llvm::bit_width(PossibleLZ);
3931 Known.Zero.setBitsFrom(LowBits);
3932 break;
3933 }
3934 case ISD::CTPOP: {
3935 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3936 // If we know some of the bits are zero, they can't be one.
3937 unsigned PossibleOnes = Known2.countMaxPopulation();
3938 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3939 break;
3940 }
3941 case ISD::PARITY: {
3942 // Parity returns 0 everywhere but the LSB.
3943 Known.Zero.setBitsFrom(1);
3944 break;
3945 }
3946 case ISD::MGATHER:
3947 case ISD::MLOAD: {
3948 ISD::LoadExtType ETy =
3949 (Opcode == ISD::MGATHER)
3950 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3951 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3952 if (ETy == ISD::ZEXTLOAD) {
3953 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3954 KnownBits Known0(MemVT.getScalarSizeInBits());
3955 return Known0.zext(BitWidth);
3956 }
3957 break;
3958 }
3959 case ISD::LOAD: {
3960 LoadSDNode *LD = cast<LoadSDNode>(Op);
3961 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3962 if (ISD::isNON_EXTLoad(LD) && Cst) {
3963 // Determine any common known bits from the loaded constant pool value.
3964 Type *CstTy = Cst->getType();
3965 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3966 !Op.getValueType().isScalableVector()) {
3967 // If its a vector splat, then we can (quickly) reuse the scalar path.
3968 // NOTE: We assume all elements match and none are UNDEF.
3969 if (CstTy->isVectorTy()) {
3970 if (const Constant *Splat = Cst->getSplatValue()) {
3971 Cst = Splat;
3972 CstTy = Cst->getType();
3973 }
3974 }
3975 // TODO - do we need to handle different bitwidths?
3976 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3977 // Iterate across all vector elements finding common known bits.
3978 Known.One.setAllBits();
3979 Known.Zero.setAllBits();
3980 for (unsigned i = 0; i != NumElts; ++i) {
3981 if (!DemandedElts[i])
3982 continue;
3983 if (Constant *Elt = Cst->getAggregateElement(i)) {
3984 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3985 const APInt &Value = CInt->getValue();
3986 Known.One &= Value;
3987 Known.Zero &= ~Value;
3988 continue;
3989 }
3990 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3991 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3992 Known.One &= Value;
3993 Known.Zero &= ~Value;
3994 continue;
3995 }
3996 }
3997 Known.One.clearAllBits();
3998 Known.Zero.clearAllBits();
3999 break;
4000 }
4001 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4002 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4003 Known = KnownBits::makeConstant(CInt->getValue());
4004 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4005 Known =
4006 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4007 }
4008 }
4009 }
4010 } else if (Op.getResNo() == 0) {
4011 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4012 KnownBits KnownScalarMemory(ScalarMemorySize);
4013 if (const MDNode *MD = LD->getRanges())
4014 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4015
4016 // Extend the Known bits from memory to the size of the scalar result.
4017 if (ISD::isZEXTLoad(Op.getNode()))
4018 Known = KnownScalarMemory.zext(BitWidth);
4019 else if (ISD::isSEXTLoad(Op.getNode()))
4020 Known = KnownScalarMemory.sext(BitWidth);
4021 else if (ISD::isEXTLoad(Op.getNode()))
4022 Known = KnownScalarMemory.anyext(BitWidth);
4023 else
4024 Known = KnownScalarMemory;
4025 assert(Known.getBitWidth() == BitWidth);
4026 return Known;
4027 }
4028 break;
4029 }
4031 if (Op.getValueType().isScalableVector())
4032 break;
4033 EVT InVT = Op.getOperand(0).getValueType();
4034 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4035 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4036 Known = Known.zext(BitWidth);
4037 break;
4038 }
4039 case ISD::ZERO_EXTEND: {
4040 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4041 Known = Known.zext(BitWidth);
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 // If the sign bit is known to be zero or one, then sext will extend
4051 // it to the top bits, else it will just zext.
4052 Known = Known.sext(BitWidth);
4053 break;
4054 }
4055 case ISD::SIGN_EXTEND: {
4056 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4057 // If the sign bit is known to be zero or one, then sext will extend
4058 // it to the top bits, else it will just zext.
4059 Known = Known.sext(BitWidth);
4060 break;
4061 }
4063 if (Op.getValueType().isScalableVector())
4064 break;
4065 EVT InVT = Op.getOperand(0).getValueType();
4066 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4067 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4068 Known = Known.anyext(BitWidth);
4069 break;
4070 }
4071 case ISD::ANY_EXTEND: {
4072 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4073 Known = Known.anyext(BitWidth);
4074 break;
4075 }
4076 case ISD::TRUNCATE: {
4077 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4078 Known = Known.trunc(BitWidth);
4079 break;
4080 }
4081 case ISD::AssertZext: {
4082 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4084 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4085 Known.Zero |= (~InMask);
4086 Known.One &= (~Known.Zero);
4087 break;
4088 }
4089 case ISD::AssertAlign: {
4090 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4091 assert(LogOfAlign != 0);
4092
4093 // TODO: Should use maximum with source
4094 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4095 // well as clearing one bits.
4096 Known.Zero.setLowBits(LogOfAlign);
4097 Known.One.clearLowBits(LogOfAlign);
4098 break;
4099 }
4100 case ISD::FGETSIGN:
4101 // All bits are zero except the low bit.
4102 Known.Zero.setBitsFrom(1);
4103 break;
4104 case ISD::ADD:
4105 case ISD::SUB: {
4106 SDNodeFlags Flags = Op.getNode()->getFlags();
4107 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4108 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4110 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4111 Flags.hasNoUnsignedWrap(), Known, Known2);
4112 break;
4113 }
4114 case ISD::USUBO:
4115 case ISD::SSUBO:
4116 case ISD::USUBO_CARRY:
4117 case ISD::SSUBO_CARRY:
4118 if (Op.getResNo() == 1) {
4119 // If we know the result of a setcc has the top bits zero, use this info.
4120 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4122 BitWidth > 1)
4123 Known.Zero.setBitsFrom(1);
4124 break;
4125 }
4126 [[fallthrough]];
4127 case ISD::SUBC: {
4128 assert(Op.getResNo() == 0 &&
4129 "We only compute knownbits for the difference here.");
4130
4131 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4132 KnownBits Borrow(1);
4133 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4134 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4135 // Borrow has bit width 1
4136 Borrow = Borrow.trunc(1);
4137 } else {
4138 Borrow.setAllZero();
4139 }
4140
4141 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4142 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4143 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4144 break;
4145 }
4146 case ISD::UADDO:
4147 case ISD::SADDO:
4148 case ISD::UADDO_CARRY:
4149 case ISD::SADDO_CARRY:
4150 if (Op.getResNo() == 1) {
4151 // If we know the result of a setcc has the top bits zero, use this info.
4152 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4154 BitWidth > 1)
4155 Known.Zero.setBitsFrom(1);
4156 break;
4157 }
4158 [[fallthrough]];
4159 case ISD::ADDC:
4160 case ISD::ADDE: {
4161 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4162
4163 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4164 KnownBits Carry(1);
4165 if (Opcode == ISD::ADDE)
4166 // Can't track carry from glue, set carry to unknown.
4167 Carry.resetAll();
4168 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4169 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4170 // Carry has bit width 1
4171 Carry = Carry.trunc(1);
4172 } else {
4173 Carry.setAllZero();
4174 }
4175
4176 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4177 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4178 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4179 break;
4180 }
4181 case ISD::UDIV: {
4182 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4183 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4184 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4185 break;
4186 }
4187 case ISD::SDIV: {
4188 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4189 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4190 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4191 break;
4192 }
4193 case ISD::SREM: {
4194 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4195 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4196 Known = KnownBits::srem(Known, Known2);
4197 break;
4198 }
4199 case ISD::UREM: {
4200 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4201 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4202 Known = KnownBits::urem(Known, Known2);
4203 break;
4204 }
4205 case ISD::EXTRACT_ELEMENT: {
4206 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4207 const unsigned Index = Op.getConstantOperandVal(1);
4208 const unsigned EltBitWidth = Op.getValueSizeInBits();
4209
4210 // Remove low part of known bits mask
4211 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4212 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4213
4214 // Remove high part of known bit mask
4215 Known = Known.trunc(EltBitWidth);
4216 break;
4217 }
4219 SDValue InVec = Op.getOperand(0);
4220 SDValue EltNo = Op.getOperand(1);
4221 EVT VecVT = InVec.getValueType();
4222 // computeKnownBits not yet implemented for scalable vectors.
4223 if (VecVT.isScalableVector())
4224 break;
4225 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4226 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4227
4228 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4229 // anything about the extended bits.
4230 if (BitWidth > EltBitWidth)
4231 Known = Known.trunc(EltBitWidth);
4232
4233 // If we know the element index, just demand that vector element, else for
4234 // an unknown element index, ignore DemandedElts and demand them all.
4235 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4236 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4237 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4238 DemandedSrcElts =
4239 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4240
4241 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4242 if (BitWidth > EltBitWidth)
4243 Known = Known.anyext(BitWidth);
4244 break;
4245 }
4247 if (Op.getValueType().isScalableVector())
4248 break;
4249
4250 // If we know the element index, split the demand between the
4251 // source vector and the inserted element, otherwise assume we need
4252 // the original demanded vector elements and the value.
4253 SDValue InVec = Op.getOperand(0);
4254 SDValue InVal = Op.getOperand(1);
4255 SDValue EltNo = Op.getOperand(2);
4256 bool DemandedVal = true;
4257 APInt DemandedVecElts = DemandedElts;
4258 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4259 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4260 unsigned EltIdx = CEltNo->getZExtValue();
4261 DemandedVal = !!DemandedElts[EltIdx];
4262 DemandedVecElts.clearBit(EltIdx);
4263 }
4264 Known.One.setAllBits();
4265 Known.Zero.setAllBits();
4266 if (DemandedVal) {
4267 Known2 = computeKnownBits(InVal, Depth + 1);
4268 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4269 }
4270 if (!!DemandedVecElts) {
4271 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4272 Known = Known.intersectWith(Known2);
4273 }
4274 break;
4275 }
4276 case ISD::BITREVERSE: {
4277 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4278 Known = Known2.reverseBits();
4279 break;
4280 }
4281 case ISD::BSWAP: {
4282 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4283 Known = Known2.byteSwap();
4284 break;
4285 }
4286 case ISD::ABS: {
4287 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4288 Known = Known2.abs();
4289 Known.Zero.setHighBits(
4290 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4291 break;
4292 }
4293 case ISD::USUBSAT: {
4294 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4295 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4296 Known = KnownBits::usub_sat(Known, Known2);
4297 break;
4298 }
4299 case ISD::UMIN: {
4300 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4301 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4302 Known = KnownBits::umin(Known, Known2);
4303 break;
4304 }
4305 case ISD::UMAX: {
4306 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4307 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4308 Known = KnownBits::umax(Known, Known2);
4309 break;
4310 }
4311 case ISD::SMIN:
4312 case ISD::SMAX: {
4313 // If we have a clamp pattern, we know that the number of sign bits will be
4314 // the minimum of the clamp min/max range.
4315 bool IsMax = (Opcode == ISD::SMAX);
4316 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4317 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4318 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4319 CstHigh =
4320 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4321 if (CstLow && CstHigh) {
4322 if (!IsMax)
4323 std::swap(CstLow, CstHigh);
4324
4325 const APInt &ValueLow = CstLow->getAPIntValue();
4326 const APInt &ValueHigh = CstHigh->getAPIntValue();
4327 if (ValueLow.sle(ValueHigh)) {
4328 unsigned LowSignBits = ValueLow.getNumSignBits();
4329 unsigned HighSignBits = ValueHigh.getNumSignBits();
4330 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4331 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4332 Known.One.setHighBits(MinSignBits);
4333 break;
4334 }
4335 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4336 Known.Zero.setHighBits(MinSignBits);
4337 break;
4338 }
4339 }
4340 }
4341
4342 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4343 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4344 if (IsMax)
4345 Known = KnownBits::smax(Known, Known2);
4346 else
4347 Known = KnownBits::smin(Known, Known2);
4348
4349 // For SMAX, if CstLow is non-negative we know the result will be
4350 // non-negative and thus all sign bits are 0.
4351 // TODO: There's an equivalent of this for smin with negative constant for
4352 // known ones.
4353 if (IsMax && CstLow) {
4354 const APInt &ValueLow = CstLow->getAPIntValue();
4355 if (ValueLow.isNonNegative()) {
4356 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4357 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4358 }
4359 }
4360
4361 break;
4362 }
4363 case ISD::UINT_TO_FP: {
4364 Known.makeNonNegative();
4365 break;
4366 }
4367 case ISD::SINT_TO_FP: {
4368 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4369 if (Known2.isNonNegative())
4370 Known.makeNonNegative();
4371 else if (Known2.isNegative())
4372 Known.makeNegative();
4373 break;
4374 }
4375 case ISD::FP_TO_UINT_SAT: {
4376 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4377 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4379 break;
4380 }
4381 case ISD::ATOMIC_LOAD: {
4382 // If we are looking at the loaded value.
4383 if (Op.getResNo() == 0) {
4384 auto *AT = cast<AtomicSDNode>(Op);
4385 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4386 KnownBits KnownScalarMemory(ScalarMemorySize);
4387 if (const MDNode *MD = AT->getRanges())
4388 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4389
4390 switch (AT->getExtensionType()) {
4391 case ISD::ZEXTLOAD:
4392 Known = KnownScalarMemory.zext(BitWidth);
4393 break;
4394 case ISD::SEXTLOAD:
4395 Known = KnownScalarMemory.sext(BitWidth);
4396 break;
4397 case ISD::EXTLOAD:
4398 switch (TLI->getExtendForAtomicOps()) {
4399 case ISD::ZERO_EXTEND:
4400 Known = KnownScalarMemory.zext(BitWidth);
4401 break;
4402 case ISD::SIGN_EXTEND:
4403 Known = KnownScalarMemory.sext(BitWidth);
4404 break;
4405 default:
4406 Known = KnownScalarMemory.anyext(BitWidth);
4407 break;
4408 }
4409 break;
4410 case ISD::NON_EXTLOAD:
4411 Known = KnownScalarMemory;
4412 break;
4413 }
4414 assert(Known.getBitWidth() == BitWidth);
4415 }
4416 break;
4417 }
4419 if (Op.getResNo() == 1) {
4420 // The boolean result conforms to getBooleanContents.
4421 // If we know the result of a setcc has the top bits zero, use this info.
4422 // We know that we have an integer-based boolean since these operations
4423 // are only available for integer.
4424 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4426 BitWidth > 1)
4427 Known.Zero.setBitsFrom(1);
4428 break;
4429 }
4430 [[fallthrough]];
4432 case ISD::ATOMIC_SWAP:
4443 case ISD::ATOMIC_LOAD_UMAX: {
4444 // If we are looking at the loaded value.
4445 if (Op.getResNo() == 0) {
4446 auto *AT = cast<AtomicSDNode>(Op);
4447 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4448
4450 Known.Zero.setBitsFrom(MemBits);
4451 }
4452 break;
4453 }
4454 case ISD::FrameIndex:
4456 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4457 Known, getMachineFunction());
4458 break;
4459
4460 default:
4461 if (Opcode < ISD::BUILTIN_OP_END)
4462 break;
4463 [[fallthrough]];
4467 // TODO: Probably okay to remove after audit; here to reduce change size
4468 // in initial enablement patch for scalable vectors
4469 if (Op.getValueType().isScalableVector())
4470 break;
4471
4472 // Allow the target to implement this method for its nodes.
4473 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4474 break;
4475 }
4476
4477 return Known;
4478}
4479
4480/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4482 switch (OR) {
4490 }
4491 llvm_unreachable("Unknown OverflowResult");
4492}
4493
4496 // X + 0 never overflow
4497 if (isNullConstant(N1))
4498 return OFK_Never;
4499
4500 // If both operands each have at least two sign bits, the addition
4501 // cannot overflow.
4502 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4503 return OFK_Never;
4504
4505 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4506 return OFK_Sometime;
4507}
4508
4511 // X + 0 never overflow
4512 if (isNullConstant(N1))
4513 return OFK_Never;
4514
4515 // mulhi + 1 never overflow
4516 KnownBits N1Known = computeKnownBits(N1);
4517 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4518 N1Known.getMaxValue().ult(2))
4519 return OFK_Never;
4520
4521 KnownBits N0Known = computeKnownBits(N0);
4522 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4523 N0Known.getMaxValue().ult(2))
4524 return OFK_Never;
4525
4526 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4527 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4528 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4529 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4530}
4531
4534 // X - 0 never overflow
4535 if (isNullConstant(N1))
4536 return OFK_Never;
4537
4538 // If both operands each have at least two sign bits, the subtraction
4539 // cannot overflow.
4540 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4541 return OFK_Never;
4542
4543 KnownBits N0Known = computeKnownBits(N0);
4544 KnownBits N1Known = computeKnownBits(N1);
4545 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4546 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4547 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4548}
4549
4552 // X - 0 never overflow
4553 if (isNullConstant(N1))
4554 return OFK_Never;
4555
4556 KnownBits N0Known = computeKnownBits(N0);
4557 KnownBits N1Known = computeKnownBits(N1);
4558 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4559 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4560 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4561}
4562
4565 // X * 0 and X * 1 never overflow.
4566 if (isNullConstant(N1) || isOneConstant(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.unsignedMulMayOverflow(N1Range));
4574}
4575
4578 // X * 0 and X * 1 never overflow.
4579 if (isNullConstant(N1) || isOneConstant(N1))
4580 return OFK_Never;
4581
4582 // Get the size of the result.
4583 unsigned BitWidth = N0.getScalarValueSizeInBits();
4584
4585 // Sum of the sign bits.
4586 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4587
4588 // If we have enough sign bits, then there's no overflow.
4589 if (SignBits > BitWidth + 1)
4590 return OFK_Never;
4591
4592 if (SignBits == BitWidth + 1) {
4593 // The overflow occurs when the true multiplication of the
4594 // the operands is the minimum negative number.
4595 KnownBits N0Known = computeKnownBits(N0);
4596 KnownBits N1Known = computeKnownBits(N1);
4597 // If one of the operands is non-negative, then there's no
4598 // overflow.
4599 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4600 return OFK_Never;
4601 }
4602
4603 return OFK_Sometime;
4604}
4605
4607 if (Depth >= MaxRecursionDepth)
4608 return false; // Limit search depth.
4609
4610 EVT OpVT = Val.getValueType();
4611 unsigned BitWidth = OpVT.getScalarSizeInBits();
4612
4613 // Is the constant a known power of 2?
4615 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4616 }))
4617 return true;
4618
4619 // A left-shift of a constant one will have exactly one bit set because
4620 // shifting the bit off the end is undefined.
4621 if (Val.getOpcode() == ISD::SHL) {
4622 auto *C = isConstOrConstSplat(Val.getOperand(0));
4623 if (C && C->getAPIntValue() == 1)
4624 return true;
4625 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4626 isKnownNeverZero(Val, Depth);
4627 }
4628
4629 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4630 // one bit set.
4631 if (Val.getOpcode() == ISD::SRL) {
4632 auto *C = isConstOrConstSplat(Val.getOperand(0));
4633 if (C && C->getAPIntValue().isSignMask())
4634 return true;
4635 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4636 isKnownNeverZero(Val, Depth);
4637 }
4638
4639 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4640 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4641
4642 // Are all operands of a build vector constant powers of two?
4643 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4644 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4645 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4646 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4647 return false;
4648 }))
4649 return true;
4650
4651 // Is the operand of a splat vector a constant power of two?
4652 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4653 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4654 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4655 return true;
4656
4657 // vscale(power-of-two) is a power-of-two for some targets
4658 if (Val.getOpcode() == ISD::VSCALE &&
4659 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4661 return true;
4662
4663 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4664 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4665 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4667
4668 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4669 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4671
4672 // Looking for `x & -x` pattern:
4673 // If x == 0:
4674 // x & -x -> 0
4675 // If x != 0:
4676 // x & -x -> non-zero pow2
4677 // so if we find the pattern return whether we know `x` is non-zero.
4678 SDValue X;
4679 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4680 return isKnownNeverZero(X, Depth);
4681
4682 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4683 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4684
4685 // More could be done here, though the above checks are enough
4686 // to handle some common cases.
4687 return false;
4688}
4689
4691 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4692 return C1->getValueAPF().getExactLog2Abs() >= 0;
4693
4694 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4695 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4696
4697 return false;
4698}
4699
4701 EVT VT = Op.getValueType();
4702
4703 // Since the number of lanes in a scalable vector is unknown at compile time,
4704 // we track one bit which is implicitly broadcast to all lanes. This means
4705 // that all lanes in a scalable vector are considered demanded.
4706 APInt DemandedElts = VT.isFixedLengthVector()
4708 : APInt(1, 1);
4709 return ComputeNumSignBits(Op, DemandedElts, Depth);
4710}
4711
4712unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4713 unsigned Depth) const {
4714 EVT VT = Op.getValueType();
4715 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4716 unsigned VTBits = VT.getScalarSizeInBits();
4717 unsigned NumElts = DemandedElts.getBitWidth();
4718 unsigned Tmp, Tmp2;
4719 unsigned FirstAnswer = 1;
4720
4721 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4722 const APInt &Val = C->getAPIntValue();
4723 return Val.getNumSignBits();
4724 }
4725
4726 if (Depth >= MaxRecursionDepth)
4727 return 1; // Limit search depth.
4728
4729 if (!DemandedElts)
4730 return 1; // No demanded elts, better to assume we don't know anything.
4731
4732 unsigned Opcode = Op.getOpcode();
4733 switch (Opcode) {
4734 default: break;
4735 case ISD::AssertSext:
4736 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4737 return VTBits-Tmp+1;
4738 case ISD::AssertZext:
4739 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4740 return VTBits-Tmp;
4741 case ISD::MERGE_VALUES:
4742 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4743 Depth + 1);
4744 case ISD::SPLAT_VECTOR: {
4745 // Check if the sign bits of source go down as far as the truncated value.
4746 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4747 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4748 if (NumSrcSignBits > (NumSrcBits - VTBits))
4749 return NumSrcSignBits - (NumSrcBits - VTBits);
4750 break;
4751 }
4752 case ISD::BUILD_VECTOR:
4753 assert(!VT.isScalableVector());
4754 Tmp = VTBits;
4755 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4756 if (!DemandedElts[i])
4757 continue;
4758
4759 SDValue SrcOp = Op.getOperand(i);
4760 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4761 // for constant nodes to ensure we only look at the sign bits.
4762 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4763 APInt T = C->getAPIntValue().trunc(VTBits);
4764 Tmp2 = T.getNumSignBits();
4765 } else {
4766 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4767
4768 if (SrcOp.getValueSizeInBits() != VTBits) {
4769 assert(SrcOp.getValueSizeInBits() > VTBits &&
4770 "Expected BUILD_VECTOR implicit truncation");
4771 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4772 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4773 }
4774 }
4775 Tmp = std::min(Tmp, Tmp2);
4776 }
4777 return Tmp;
4778
4779 case ISD::VECTOR_SHUFFLE: {
4780 // Collect the minimum number of sign bits that are shared by every vector
4781 // element referenced by the shuffle.
4782 APInt DemandedLHS, DemandedRHS;
4783 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4784 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4785 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4786 DemandedLHS, DemandedRHS))
4787 return 1;
4788
4789 Tmp = std::numeric_limits<unsigned>::max();
4790 if (!!DemandedLHS)
4791 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4792 if (!!DemandedRHS) {
4793 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4794 Tmp = std::min(Tmp, Tmp2);
4795 }
4796 // If we don't know anything, early out and try computeKnownBits fall-back.
4797 if (Tmp == 1)
4798 break;
4799 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4800 return Tmp;
4801 }
4802
4803 case ISD::BITCAST: {
4804 if (VT.isScalableVector())
4805 break;
4806 SDValue N0 = Op.getOperand(0);
4807 EVT SrcVT = N0.getValueType();
4808 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4809
4810 // Ignore bitcasts from unsupported types..
4811 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4812 break;
4813
4814 // Fast handling of 'identity' bitcasts.
4815 if (VTBits == SrcBits)
4816 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4817
4818 bool IsLE = getDataLayout().isLittleEndian();
4819
4820 // Bitcast 'large element' scalar/vector to 'small element' vector.
4821 if ((SrcBits % VTBits) == 0) {
4822 assert(VT.isVector() && "Expected bitcast to vector");
4823
4824 unsigned Scale = SrcBits / VTBits;
4825 APInt SrcDemandedElts =
4826 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4827
4828 // Fast case - sign splat can be simply split across the small elements.
4829 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4830 if (Tmp == SrcBits)
4831 return VTBits;
4832
4833 // Slow case - determine how far the sign extends into each sub-element.
4834 Tmp2 = VTBits;
4835 for (unsigned i = 0; i != NumElts; ++i)
4836 if (DemandedElts[i]) {
4837 unsigned SubOffset = i % Scale;
4838 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4839 SubOffset = SubOffset * VTBits;
4840 if (Tmp <= SubOffset)
4841 return 1;
4842 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4843 }
4844 return Tmp2;
4845 }
4846 break;
4847 }
4848
4850 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4851 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4852 return VTBits - Tmp + 1;
4853 case ISD::SIGN_EXTEND:
4854 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4855 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4857 // Max of the input and what this extends.
4858 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4859 Tmp = VTBits-Tmp+1;
4860 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4861 return std::max(Tmp, Tmp2);
4863 if (VT.isScalableVector())
4864 break;
4865 SDValue Src = Op.getOperand(0);
4866 EVT SrcVT = Src.getValueType();
4867 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4868 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4869 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4870 }
4871 case ISD::SRA:
4872 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4873 // SRA X, C -> adds C sign bits.
4874 if (std::optional<uint64_t> ShAmt =
4875 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4876 Tmp = std::min<uint64_t>(Tmp + *ShAmt, VTBits);
4877 return Tmp;
4878 case ISD::SHL:
4879 if (std::optional<ConstantRange> ShAmtRange =
4880 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4881 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4882 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4883 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4884 // shifted out, then we can compute the number of sign bits for the
4885 // operand being extended. A future improvement could be to pass along the
4886 // "shifted left by" information in the recursive calls to
4887 // ComputeKnownSignBits. Allowing us to handle this more generically.
4888 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4889 SDValue Ext = Op.getOperand(0);
4890 EVT ExtVT = Ext.getValueType();
4891 SDValue Extendee = Ext.getOperand(0);
4892 EVT ExtendeeVT = Extendee.getValueType();
4893 uint64_t SizeDifference =
4894 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4895 if (SizeDifference <= MinShAmt) {
4896 Tmp = SizeDifference +
4897 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4898 if (MaxShAmt < Tmp)
4899 return Tmp - MaxShAmt;
4900 }
4901 }
4902 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4903 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4904 if (MaxShAmt < Tmp)
4905 return Tmp - MaxShAmt;
4906 }
4907 break;
4908 case ISD::AND:
4909 case ISD::OR:
4910 case ISD::XOR: // NOT is handled here.
4911 // Logical binary ops preserve the number of sign bits at the worst.
4912 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4913 if (Tmp != 1) {
4914 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4915 FirstAnswer = std::min(Tmp, Tmp2);
4916 // We computed what we know about the sign bits as our first
4917 // answer. Now proceed to the generic code that uses
4918 // computeKnownBits, and pick whichever answer is better.
4919 }
4920 break;
4921
4922 case ISD::SELECT:
4923 case ISD::VSELECT:
4924 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4925 if (Tmp == 1) return 1; // Early out.
4926 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4927 return std::min(Tmp, Tmp2);
4928 case ISD::SELECT_CC:
4929 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4930 if (Tmp == 1) return 1; // Early out.
4931 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4932 return std::min(Tmp, Tmp2);
4933
4934 case ISD::SMIN:
4935 case ISD::SMAX: {
4936 // If we have a clamp pattern, we know that the number of sign bits will be
4937 // the minimum of the clamp min/max range.
4938 bool IsMax = (Opcode == ISD::SMAX);
4939 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4940 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4941 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4942 CstHigh =
4943 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4944 if (CstLow && CstHigh) {
4945 if (!IsMax)
4946 std::swap(CstLow, CstHigh);
4947 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4948 Tmp = CstLow->getAPIntValue().getNumSignBits();
4949 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4950 return std::min(Tmp, Tmp2);
4951 }
4952 }
4953
4954 // Fallback - just get the minimum number of sign bits of the operands.
4955 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4956 if (Tmp == 1)
4957 return 1; // Early out.
4958 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4959 return std::min(Tmp, Tmp2);
4960 }
4961 case ISD::UMIN:
4962 case ISD::UMAX:
4963 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4964 if (Tmp == 1)
4965 return 1; // Early out.
4966 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4967 return std::min(Tmp, Tmp2);
4968 case ISD::SSUBO_CARRY:
4969 case ISD::USUBO_CARRY:
4970 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4971 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4972 return VTBits;
4973 [[fallthrough]];
4974 case ISD::SADDO:
4975 case ISD::UADDO:
4976 case ISD::SADDO_CARRY:
4977 case ISD::UADDO_CARRY:
4978 case ISD::SSUBO:
4979 case ISD::USUBO:
4980 case ISD::SMULO:
4981 case ISD::UMULO:
4982 if (Op.getResNo() != 1)
4983 break;
4984 // The boolean result conforms to getBooleanContents. Fall through.
4985 // If setcc returns 0/-1, all bits are sign bits.
4986 // We know that we have an integer-based boolean since these operations
4987 // are only available for integer.
4988 if (TLI->getBooleanContents(VT.isVector(), false) ==
4990 return VTBits;
4991 break;
4992 case ISD::SETCC:
4993 case ISD::SETCCCARRY:
4994 case ISD::STRICT_FSETCC:
4995 case ISD::STRICT_FSETCCS: {
4996 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4997 // If setcc returns 0/-1, all bits are sign bits.
4998 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5000 return VTBits;
5001 break;
5002 }
5003 case ISD::ROTL:
5004 case ISD::ROTR:
5005 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5006
5007 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5008 if (Tmp == VTBits)
5009 return VTBits;
5010
5011 if (ConstantSDNode *C =
5012 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5013 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5014
5015 // Handle rotate right by N like a rotate left by 32-N.
5016 if (Opcode == ISD::ROTR)
5017 RotAmt = (VTBits - RotAmt) % VTBits;
5018
5019 // If we aren't rotating out all of the known-in sign bits, return the
5020 // number that are left. This handles rotl(sext(x), 1) for example.
5021 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5022 }
5023 break;
5024 case ISD::ADD:
5025 case ISD::ADDC:
5026 // Add can have at most one carry bit. Thus we know that the output
5027 // is, at worst, one more bit than the inputs.
5028 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5029 if (Tmp == 1) return 1; // Early out.
5030
5031 // Special case decrementing a value (ADD X, -1):
5032 if (ConstantSDNode *CRHS =
5033 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5034 if (CRHS->isAllOnes()) {
5035 KnownBits Known =
5036 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5037
5038 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5039 // sign bits set.
5040 if ((Known.Zero | 1).isAllOnes())
5041 return VTBits;
5042
5043 // If we are subtracting one from a positive number, there is no carry
5044 // out of the result.
5045 if (Known.isNonNegative())
5046 return Tmp;
5047 }
5048
5049 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5050 if (Tmp2 == 1) return 1; // Early out.
5051 return std::min(Tmp, Tmp2) - 1;
5052 case ISD::SUB:
5053 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5054 if (Tmp2 == 1) return 1; // Early out.
5055
5056 // Handle NEG.
5057 if (ConstantSDNode *CLHS =
5058 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5059 if (CLHS->isZero()) {
5060 KnownBits Known =
5061 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5062 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5063 // sign bits set.
5064 if ((Known.Zero | 1).isAllOnes())
5065 return VTBits;
5066
5067 // If the input is known to be positive (the sign bit is known clear),
5068 // the output of the NEG has the same number of sign bits as the input.
5069 if (Known.isNonNegative())
5070 return Tmp2;
5071
5072 // Otherwise, we treat this like a SUB.
5073 }
5074
5075 // Sub can have at most one carry bit. Thus we know that the output
5076 // is, at worst, one more bit than the inputs.
5077 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5078 if (Tmp == 1) return 1; // Early out.
5079 return std::min(Tmp, Tmp2) - 1;
5080 case ISD::MUL: {
5081 // The output of the Mul can be at most twice the valid bits in the inputs.
5082 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5083 if (SignBitsOp0 == 1)
5084 break;
5085 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5086 if (SignBitsOp1 == 1)
5087 break;
5088 unsigned OutValidBits =
5089 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5090 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5091 }
5092 case ISD::AVGCEILS:
5093 case ISD::AVGFLOORS:
5094 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5095 if (Tmp == 1)
5096 return 1; // Early out.
5097 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5098 return std::min(Tmp, Tmp2);
5099 case ISD::SREM:
5100 // The sign bit is the LHS's sign bit, except when the result of the
5101 // remainder is zero. The magnitude of the result should be less than or
5102 // equal to the magnitude of the LHS. Therefore, the result should have
5103 // at least as many sign bits as the left hand side.
5104 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5105 case ISD::TRUNCATE: {
5106 // Check if the sign bits of source go down as far as the truncated value.
5107 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5108 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5109 if (NumSrcSignBits > (NumSrcBits - VTBits))
5110 return NumSrcSignBits - (NumSrcBits - VTBits);
5111 break;
5112 }
5113 case ISD::EXTRACT_ELEMENT: {
5114 if (VT.isScalableVector())
5115 break;
5116 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5117 const int BitWidth = Op.getValueSizeInBits();
5118 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5119
5120 // Get reverse index (starting from 1), Op1 value indexes elements from
5121 // little end. Sign starts at big end.
5122 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5123
5124 // If the sign portion ends in our element the subtraction gives correct
5125 // result. Otherwise it gives either negative or > bitwidth result
5126 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5127 }
5129 if (VT.isScalableVector())
5130 break;
5131 // If we know the element index, split the demand between the
5132 // source vector and the inserted element, otherwise assume we need
5133 // the original demanded vector elements and the value.
5134 SDValue InVec = Op.getOperand(0);
5135 SDValue InVal = Op.getOperand(1);
5136 SDValue EltNo = Op.getOperand(2);
5137 bool DemandedVal = true;
5138 APInt DemandedVecElts = DemandedElts;
5139 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5140 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5141 unsigned EltIdx = CEltNo->getZExtValue();
5142 DemandedVal = !!DemandedElts[EltIdx];
5143 DemandedVecElts.clearBit(EltIdx);
5144 }
5145 Tmp = std::numeric_limits<unsigned>::max();
5146 if (DemandedVal) {
5147 // TODO - handle implicit truncation of inserted elements.
5148 if (InVal.getScalarValueSizeInBits() != VTBits)
5149 break;
5150 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5151 Tmp = std::min(Tmp, Tmp2);
5152 }
5153 if (!!DemandedVecElts) {
5154 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5155 Tmp = std::min(Tmp, Tmp2);
5156 }
5157 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5158 return Tmp;
5159 }
5161 assert(!VT.isScalableVector());
5162 SDValue InVec = Op.getOperand(0);
5163 SDValue EltNo = Op.getOperand(1);
5164 EVT VecVT = InVec.getValueType();
5165 // ComputeNumSignBits not yet implemented for scalable vectors.
5166 if (VecVT.isScalableVector())
5167 break;
5168 const unsigned BitWidth = Op.getValueSizeInBits();
5169 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5170 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5171
5172 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5173 // anything about sign bits. But if the sizes match we can derive knowledge
5174 // about sign bits from the vector operand.
5175 if (BitWidth != EltBitWidth)
5176 break;
5177
5178 // If we know the element index, just demand that vector element, else for
5179 // an unknown element index, ignore DemandedElts and demand them all.
5180 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5181 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5182 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5183 DemandedSrcElts =
5184 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5185
5186 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5187 }
5189 // Offset the demanded elts by the subvector index.
5190 SDValue Src = Op.getOperand(0);
5191 // Bail until we can represent demanded elements for scalable vectors.
5192 if (Src.getValueType().isScalableVector())
5193 break;
5194 uint64_t Idx = Op.getConstantOperandVal(1);
5195 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5196 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5197 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5198 }
5199 case ISD::CONCAT_VECTORS: {
5200 if (VT.isScalableVector())
5201 break;
5202 // Determine the minimum number of sign bits across all demanded
5203 // elts of the input vectors. Early out if the result is already 1.
5204 Tmp = std::numeric_limits<unsigned>::max();
5205 EVT SubVectorVT = Op.getOperand(0).getValueType();
5206 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5207 unsigned NumSubVectors = Op.getNumOperands();
5208 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5209 APInt DemandedSub =
5210 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5211 if (!DemandedSub)
5212 continue;
5213 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5214 Tmp = std::min(Tmp, Tmp2);
5215 }
5216 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5217 return Tmp;
5218 }
5219 case ISD::INSERT_SUBVECTOR: {
5220 if (VT.isScalableVector())
5221 break;
5222 // Demand any elements from the subvector and the remainder from the src its
5223 // inserted into.
5224 SDValue Src = Op.getOperand(0);
5225 SDValue Sub = Op.getOperand(1);
5226 uint64_t Idx = Op.getConstantOperandVal(2);
5227 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5228 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5229 APInt DemandedSrcElts = DemandedElts;
5230 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5231
5232 Tmp = std::numeric_limits<unsigned>::max();
5233 if (!!DemandedSubElts) {
5234 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5235 if (Tmp == 1)
5236 return 1; // early-out
5237 }
5238 if (!!DemandedSrcElts) {
5239 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5240 Tmp = std::min(Tmp, Tmp2);
5241 }
5242 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5243 return Tmp;
5244 }
5245 case ISD::LOAD: {
5246 LoadSDNode *LD = cast<LoadSDNode>(Op);
5247 if (const MDNode *Ranges = LD->getRanges()) {
5248 if (DemandedElts != 1)
5249 break;
5250
5252 if (VTBits > CR.getBitWidth()) {
5253 switch (LD->getExtensionType()) {
5254 case ISD::SEXTLOAD:
5255 CR = CR.signExtend(VTBits);
5256 break;
5257 case ISD::ZEXTLOAD:
5258 CR = CR.zeroExtend(VTBits);
5259 break;
5260 default:
5261 break;
5262 }
5263 }
5264
5265 if (VTBits != CR.getBitWidth())
5266 break;
5267 return std::min(CR.getSignedMin().getNumSignBits(),
5269 }
5270
5271 break;
5272 }
5275 case ISD::ATOMIC_SWAP:
5287 case ISD::ATOMIC_LOAD: {
5288 auto *AT = cast<AtomicSDNode>(Op);
5289 // If we are looking at the loaded value.
5290 if (Op.getResNo() == 0) {
5291 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5292 if (Tmp == VTBits)
5293 return 1; // early-out
5294
5295 // For atomic_load, prefer to use the extension type.
5296 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5297 switch (AT->getExtensionType()) {
5298 default:
5299 break;
5300 case ISD::SEXTLOAD:
5301 return VTBits - Tmp + 1;
5302 case ISD::ZEXTLOAD:
5303 return VTBits - Tmp;
5304 }
5305 }
5306
5308 return VTBits - Tmp + 1;
5310 return VTBits - Tmp;
5311 }
5312 break;
5313 }
5314 }
5315
5316 // If we are looking at the loaded value of the SDNode.
5317 if (Op.getResNo() == 0) {
5318 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5319 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5320 unsigned ExtType = LD->getExtensionType();
5321 switch (ExtType) {
5322 default: break;
5323 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5324 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5325 return VTBits - Tmp + 1;
5326 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5327 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5328 return VTBits - Tmp;
5329 case ISD::NON_EXTLOAD:
5330 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5331 // We only need to handle vectors - computeKnownBits should handle
5332 // scalar cases.
5333 Type *CstTy = Cst->getType();
5334 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5335 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5336 VTBits == CstTy->getScalarSizeInBits()) {
5337 Tmp = VTBits;
5338 for (unsigned i = 0; i != NumElts; ++i) {
5339 if (!DemandedElts[i])
5340 continue;
5341 if (Constant *Elt = Cst->getAggregateElement(i)) {
5342 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5343 const APInt &Value = CInt->getValue();
5344 Tmp = std::min(Tmp, Value.getNumSignBits());
5345 continue;
5346 }
5347 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5348 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5349 Tmp = std::min(Tmp, Value.getNumSignBits());
5350 continue;
5351 }
5352 }
5353 // Unknown type. Conservatively assume no bits match sign bit.
5354 return 1;
5355 }
5356 return Tmp;
5357 }
5358 }
5359 break;
5360 }
5361 }
5362 }
5363
5364 // Allow the target to implement this method for its nodes.
5365 if (Opcode >= ISD::BUILTIN_OP_END ||
5366 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5367 Opcode == ISD::INTRINSIC_W_CHAIN ||
5368 Opcode == ISD::INTRINSIC_VOID) {
5369 // TODO: This can probably be removed once target code is audited. This
5370 // is here purely to reduce patch size and review complexity.
5371 if (!VT.isScalableVector()) {
5372 unsigned NumBits =
5373 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5374 if (NumBits > 1)
5375 FirstAnswer = std::max(FirstAnswer, NumBits);
5376 }
5377 }
5378
5379 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5380 // use this information.
5381 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5382 return std::max(FirstAnswer, Known.countMinSignBits());
5383}
5384
5386 unsigned Depth) const {
5387 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5388 return Op.getScalarValueSizeInBits() - SignBits + 1;
5389}
5390
5392 const APInt &DemandedElts,
5393 unsigned Depth) const {
5394 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5395 return Op.getScalarValueSizeInBits() - SignBits + 1;
5396}
5397
5399 unsigned Depth) const {
5400 // Early out for FREEZE.
5401 if (Op.getOpcode() == ISD::FREEZE)
5402 return true;
5403
5404 EVT VT = Op.getValueType();
5405 APInt DemandedElts = VT.isFixedLengthVector()
5407 : APInt(1, 1);
5408 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5409}
5410
5412 const APInt &DemandedElts,
5413 bool PoisonOnly,
5414 unsigned Depth) const {
5415 unsigned Opcode = Op.getOpcode();
5416
5417 // Early out for FREEZE.
5418 if (Opcode == ISD::FREEZE)
5419 return true;
5420
5421 if (Depth >= MaxRecursionDepth)
5422 return false; // Limit search depth.
5423
5424 if (isIntOrFPConstant(Op))
5425 return true;
5426
5427 switch (Opcode) {
5428 case ISD::CONDCODE:
5429 case ISD::VALUETYPE:
5430 case ISD::FrameIndex:
5432 case ISD::CopyFromReg:
5433 return true;
5434
5435 case ISD::POISON:
5436 return false;
5437
5438 case ISD::UNDEF:
5439 return PoisonOnly;
5440
5441 case ISD::BUILD_VECTOR:
5442 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5443 // this shouldn't affect the result.
5444 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5445 if (!DemandedElts[i])
5446 continue;
5448 Depth + 1))
5449 return false;
5450 }
5451 return true;
5452
5454 SDValue Src = Op.getOperand(0);
5455 if (Src.getValueType().isScalableVector())
5456 break;
5457 uint64_t Idx = Op.getConstantOperandVal(1);
5458 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5459 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5460 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5461 Depth + 1);
5462 }
5463
5464 case ISD::INSERT_SUBVECTOR: {
5465 if (Op.getValueType().isScalableVector())
5466 break;
5467 SDValue Src = Op.getOperand(0);
5468 SDValue Sub = Op.getOperand(1);
5469 uint64_t Idx = Op.getConstantOperandVal(2);
5470 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5471 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5472 APInt DemandedSrcElts = DemandedElts;
5473 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5474
5475 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5476 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5477 return false;
5478 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5479 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5480 return false;
5481 return true;
5482 }
5483
5485 SDValue Src = Op.getOperand(0);
5486 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5487 EVT SrcVT = Src.getValueType();
5488 if (SrcVT.isFixedLengthVector() && IndexC &&
5489 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5490 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5491 IndexC->getZExtValue());
5492 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5493 Depth + 1);
5494 }
5495 break;
5496 }
5497
5499 SDValue InVec = Op.getOperand(0);
5500 SDValue InVal = Op.getOperand(1);
5501 SDValue EltNo = Op.getOperand(2);
5502 EVT VT = InVec.getValueType();
5503 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5504 if (IndexC && VT.isFixedLengthVector() &&
5505 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5506 if (DemandedElts[IndexC->getZExtValue()] &&
5508 return false;
5509 APInt InVecDemandedElts = DemandedElts;
5510 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5511 if (!!InVecDemandedElts &&
5512 !isGuaranteedNotToBeUndefOrPoison(InVec, InVecDemandedElts,
5513 PoisonOnly, Depth + 1))
5514 return false;
5515 return true;
5516 }
5517 break;
5518 }
5519
5521 // Check upper (known undef) elements.
5522 if (DemandedElts.ugt(1) && !PoisonOnly)
5523 return false;
5524 // Check element zero.
5525 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5526 Op.getOperand(0), PoisonOnly, Depth + 1))
5527 return false;
5528 return true;
5529
5530 case ISD::SPLAT_VECTOR:
5531 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5532 Depth + 1);
5533
5534 case ISD::VECTOR_SHUFFLE: {
5535 APInt DemandedLHS, DemandedRHS;
5536 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5537 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5538 DemandedElts, DemandedLHS, DemandedRHS,
5539 /*AllowUndefElts=*/false))
5540 return false;
5541 if (!DemandedLHS.isZero() &&
5542 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5543 PoisonOnly, Depth + 1))
5544 return false;
5545 if (!DemandedRHS.isZero() &&
5546 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5547 PoisonOnly, Depth + 1))
5548 return false;
5549 return true;
5550 }
5551
5552 case ISD::SHL:
5553 case ISD::SRL:
5554 case ISD::SRA:
5555 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5556 // enough to check operand 0 if Op can't create undef/poison.
5557 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5558 /*ConsiderFlags*/ true, Depth) &&
5559 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5560 PoisonOnly, Depth + 1);
5561
5562 case ISD::BSWAP:
5563 case ISD::CTPOP:
5564 case ISD::BITREVERSE:
5565 case ISD::AND:
5566 case ISD::OR:
5567 case ISD::XOR:
5568 case ISD::ADD:
5569 case ISD::SUB:
5570 case ISD::MUL:
5571 case ISD::SADDSAT:
5572 case ISD::UADDSAT:
5573 case ISD::SSUBSAT:
5574 case ISD::USUBSAT:
5575 case ISD::SSHLSAT:
5576 case ISD::USHLSAT:
5577 case ISD::SMIN:
5578 case ISD::SMAX:
5579 case ISD::UMIN:
5580 case ISD::UMAX:
5581 case ISD::ZERO_EXTEND:
5582 case ISD::SIGN_EXTEND:
5583 case ISD::ANY_EXTEND:
5584 case ISD::TRUNCATE:
5585 case ISD::VSELECT: {
5586 // If Op can't create undef/poison and none of its operands are undef/poison
5587 // then Op is never undef/poison. A difference from the more common check
5588 // below, outside the switch, is that we handle elementwise operations for
5589 // which the DemandedElts mask is valid for all operands here.
5590 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5591 /*ConsiderFlags*/ true, Depth) &&
5592 all_of(Op->ops(), [&](SDValue V) {
5593 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5594 PoisonOnly, Depth + 1);
5595 });
5596 }
5597
5598 // TODO: Search for noundef attributes from library functions.
5599
5600 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5601
5602 default:
5603 // Allow the target to implement this method for its nodes.
5604 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5605 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5607 Op, DemandedElts, *this, PoisonOnly, Depth);
5608 break;
5609 }
5610
5611 // If Op can't create undef/poison and none of its operands are undef/poison
5612 // then Op is never undef/poison.
5613 // NOTE: TargetNodes can handle this in themselves in
5614 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5615 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5616 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5617 Depth) &&
5618 all_of(Op->ops(), [&](SDValue V) {
5619 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5620 });
5621}
5622
5624 bool ConsiderFlags,
5625 unsigned Depth) const {
5626 EVT VT = Op.getValueType();
5627 APInt DemandedElts = VT.isFixedLengthVector()
5629 : APInt(1, 1);
5630 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5631 Depth);
5632}
5633
5635 bool PoisonOnly, bool ConsiderFlags,
5636 unsigned Depth) const {
5637 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5638 return true;
5639
5640 unsigned Opcode = Op.getOpcode();
5641 switch (Opcode) {
5642 case ISD::AssertSext:
5643 case ISD::AssertZext:
5644 case ISD::AssertAlign:
5646 // Assertion nodes can create poison if the assertion fails.
5647 return true;
5648
5649 case ISD::FREEZE:
5653 case ISD::SADDSAT:
5654 case ISD::UADDSAT:
5655 case ISD::SSUBSAT:
5656 case ISD::USUBSAT:
5657 case ISD::MULHU:
5658 case ISD::MULHS:
5659 case ISD::ABDU:
5660 case ISD::ABDS:
5661 case ISD::SMIN:
5662 case ISD::SMAX:
5663 case ISD::SCMP:
5664 case ISD::UMIN:
5665 case ISD::UMAX:
5666 case ISD::UCMP:
5667 case ISD::AND:
5668 case ISD::XOR:
5669 case ISD::ROTL:
5670 case ISD::ROTR:
5671 case ISD::FSHL:
5672 case ISD::FSHR:
5673 case ISD::BSWAP:
5674 case ISD::CTTZ:
5675 case ISD::CTLZ:
5676 case ISD::CTPOP:
5677 case ISD::BITREVERSE:
5678 case ISD::PARITY:
5679 case ISD::SIGN_EXTEND:
5680 case ISD::TRUNCATE:
5684 case ISD::BITCAST:
5685 case ISD::BUILD_VECTOR:
5686 case ISD::BUILD_PAIR:
5687 case ISD::SPLAT_VECTOR:
5688 case ISD::FABS:
5689 return false;
5690
5691 case ISD::ABS:
5692 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5693 // Different to Intrinsic::abs.
5694 return false;
5695
5696 case ISD::ADDC:
5697 case ISD::SUBC:
5698 case ISD::ADDE:
5699 case ISD::SUBE:
5700 case ISD::SADDO:
5701 case ISD::SSUBO:
5702 case ISD::SMULO:
5703 case ISD::SADDO_CARRY:
5704 case ISD::SSUBO_CARRY:
5705 case ISD::UADDO:
5706 case ISD::USUBO:
5707 case ISD::UMULO:
5708 case ISD::UADDO_CARRY:
5709 case ISD::USUBO_CARRY:
5710 // No poison on result or overflow flags.
5711 return false;
5712
5713 case ISD::SELECT_CC:
5714 case ISD::SETCC: {
5715 // Integer setcc cannot create undef or poison.
5716 if (Op.getOperand(0).getValueType().isInteger())
5717 return false;
5718
5719 // FP compares are more complicated. They can create poison for nan/infinity
5720 // based on options and flags. The options and flags also cause special
5721 // nonan condition codes to be used. Those condition codes may be preserved
5722 // even if the nonan flag is dropped somewhere.
5723 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5724 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5725 if (((unsigned)CCCode & 0x10U))
5726 return true;
5727
5729 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5730 }
5731
5732 case ISD::OR:
5733 case ISD::ZERO_EXTEND:
5734 case ISD::SELECT:
5735 case ISD::VSELECT:
5736 case ISD::ADD:
5737 case ISD::SUB:
5738 case ISD::MUL:
5739 case ISD::FNEG:
5740 case ISD::FADD:
5741 case ISD::FSUB:
5742 case ISD::FMUL:
5743 case ISD::FDIV:
5744 case ISD::FREM:
5745 case ISD::FCOPYSIGN:
5746 case ISD::FMA:
5747 case ISD::FMAD:
5748 case ISD::FP_EXTEND:
5751 // No poison except from flags (which is handled above)
5752 return false;
5753
5754 case ISD::SHL:
5755 case ISD::SRL:
5756 case ISD::SRA:
5757 // If the max shift amount isn't in range, then the shift can
5758 // create poison.
5759 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5760
5763 // If the amount is zero then the result will be poison.
5764 // TODO: Add isKnownNeverZero DemandedElts handling.
5765 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5766
5768 // Check if we demand any upper (undef) elements.
5769 return !PoisonOnly && DemandedElts.ugt(1);
5770
5773 // Ensure that the element index is in bounds.
5774 EVT VecVT = Op.getOperand(0).getValueType();
5775 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5776 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5777 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5778 }
5779
5780 case ISD::VECTOR_SHUFFLE: {
5781 // Check for any demanded shuffle element that is undef.
5782 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5783 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5784 if (Elt < 0 && DemandedElts[Idx])
5785 return true;
5786 return false;
5787 }
5788
5789 default:
5790 // Allow the target to implement this method for its nodes.
5791 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5792 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5794 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5795 break;
5796 }
5797
5798 // Be conservative and return true.
5799 return true;
5800}
5801
5802bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5803 unsigned Opcode = Op.getOpcode();
5804 if (Opcode == ISD::OR)
5805 return Op->getFlags().hasDisjoint() ||
5806 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5807 if (Opcode == ISD::XOR)
5808 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5809 return false;
5810}
5811
5813 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5814 (Op.isAnyAdd() || isADDLike(Op));
5815}
5816
5818 unsigned Depth) const {
5819 EVT VT = Op.getValueType();
5820
5821 // Since the number of lanes in a scalable vector is unknown at compile time,
5822 // we track one bit which is implicitly broadcast to all lanes. This means
5823 // that all lanes in a scalable vector are considered demanded.
5824 APInt DemandedElts = VT.isFixedLengthVector()
5826 : APInt(1, 1);
5827
5828 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5829}
5830
5832 bool SNaN, unsigned Depth) const {
5833 assert(!DemandedElts.isZero() && "No demanded elements");
5834
5835 // If we're told that NaNs won't happen, assume they won't.
5836 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5837 return true;
5838
5839 if (Depth >= MaxRecursionDepth)
5840 return false; // Limit search depth.
5841
5842 // If the value is a constant, we can obviously see if it is a NaN or not.
5843 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5844 return !C->getValueAPF().isNaN() ||
5845 (SNaN && !C->getValueAPF().isSignaling());
5846 }
5847
5848 unsigned Opcode = Op.getOpcode();
5849 switch (Opcode) {
5850 case ISD::FADD:
5851 case ISD::FSUB:
5852 case ISD::FMUL:
5853 case ISD::FDIV:
5854 case ISD::FREM:
5855 case ISD::FSIN:
5856 case ISD::FCOS:
5857 case ISD::FTAN:
5858 case ISD::FASIN:
5859 case ISD::FACOS:
5860 case ISD::FATAN:
5861 case ISD::FATAN2:
5862 case ISD::FSINH:
5863 case ISD::FCOSH:
5864 case ISD::FTANH:
5865 case ISD::FMA:
5866 case ISD::FMAD: {
5867 if (SNaN)
5868 return true;
5869 // TODO: Need isKnownNeverInfinity
5870 return false;
5871 }
5872 case ISD::FCANONICALIZE:
5873 case ISD::FEXP:
5874 case ISD::FEXP2:
5875 case ISD::FEXP10:
5876 case ISD::FTRUNC:
5877 case ISD::FFLOOR:
5878 case ISD::FCEIL:
5879 case ISD::FROUND:
5880 case ISD::FROUNDEVEN:
5881 case ISD::LROUND:
5882 case ISD::LLROUND:
5883 case ISD::FRINT:
5884 case ISD::LRINT:
5885 case ISD::LLRINT:
5886 case ISD::FNEARBYINT:
5887 case ISD::FLDEXP: {
5888 if (SNaN)
5889 return true;
5890 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5891 }
5892 case ISD::FABS:
5893 case ISD::FNEG:
5894 case ISD::FCOPYSIGN: {
5895 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5896 }
5897 case ISD::SELECT:
5898 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5899 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5900 case ISD::FP_EXTEND:
5901 case ISD::FP_ROUND: {
5902 if (SNaN)
5903 return true;
5904 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5905 }
5906 case ISD::SINT_TO_FP:
5907 case ISD::UINT_TO_FP:
5908 return true;
5909 case ISD::FSQRT: // Need is known positive
5910 case ISD::FLOG:
5911 case ISD::FLOG2:
5912 case ISD::FLOG10:
5913 case ISD::FPOWI:
5914 case ISD::FPOW: {
5915 if (SNaN)
5916 return true;
5917 // TODO: Refine on operand
5918 return false;
5919 }
5920 case ISD::FMINNUM:
5921 case ISD::FMAXNUM:
5922 case ISD::FMINIMUMNUM:
5923 case ISD::FMAXIMUMNUM: {
5924 // Only one needs to be known not-nan, since it will be returned if the
5925 // other ends up being one.
5926 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5927 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5928 }
5929 case ISD::FMINNUM_IEEE:
5930 case ISD::FMAXNUM_IEEE: {
5931 if (SNaN)
5932 return true;
5933 // This can return a NaN if either operand is an sNaN, or if both operands
5934 // are NaN.
5935 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5936 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5937 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5938 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5939 }
5940 case ISD::FMINIMUM:
5941 case ISD::FMAXIMUM: {
5942 // TODO: Does this quiet or return the origina NaN as-is?
5943 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5944 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5945 }
5947 SDValue Src = Op.getOperand(0);
5948 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5949 EVT SrcVT = Src.getValueType();
5950 if (SrcVT.isFixedLengthVector() && Idx &&
5951 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5952 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5953 Idx->getZExtValue());
5954 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5955 }
5956 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5957 }
5959 SDValue Src = Op.getOperand(0);
5960 if (Src.getValueType().isFixedLengthVector()) {
5961 unsigned Idx = Op.getConstantOperandVal(1);
5962 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5963 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5964 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5965 }
5966 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5967 }
5968 case ISD::INSERT_SUBVECTOR: {
5969 SDValue BaseVector = Op.getOperand(0);
5970 SDValue SubVector = Op.getOperand(1);
5971 EVT BaseVectorVT = BaseVector.getValueType();
5972 if (BaseVectorVT.isFixedLengthVector()) {
5973 unsigned Idx = Op.getConstantOperandVal(2);
5974 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5975 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5976
5977 // Clear/Extract the bits at the position where the subvector will be
5978 // inserted.
5979 APInt DemandedMask =
5980 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5981 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5982 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5983
5984 bool NeverNaN = true;
5985 if (!DemandedSrcElts.isZero())
5986 NeverNaN &=
5987 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5988 if (NeverNaN && !DemandedSubElts.isZero())
5989 NeverNaN &=
5990 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5991 return NeverNaN;
5992 }
5993 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5994 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5995 }
5996 case ISD::BUILD_VECTOR: {
5997 unsigned NumElts = Op.getNumOperands();
5998 for (unsigned I = 0; I != NumElts; ++I)
5999 if (DemandedElts[I] &&
6000 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6001 return false;
6002 return true;
6003 }
6004 case ISD::AssertNoFPClass: {
6005 FPClassTest NoFPClass =
6006 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6007 if ((NoFPClass & fcNan) == fcNan)
6008 return true;
6009 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6010 return true;
6011 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6012 }
6013 default:
6014 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6015 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6016 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6017 Depth);
6018 }
6019
6020 return false;
6021 }
6022}
6023
6025 assert(Op.getValueType().isFloatingPoint() &&
6026 "Floating point type expected");
6027
6028 // If the value is a constant, we can obviously see if it is a zero or not.
6030 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6031}
6032
6034 if (Depth >= MaxRecursionDepth)
6035 return false; // Limit search depth.
6036
6037 assert(!Op.getValueType().isFloatingPoint() &&
6038 "Floating point types unsupported - use isKnownNeverZeroFloat");
6039
6040 // If the value is a constant, we can obviously see if it is a zero or not.
6042 [](ConstantSDNode *C) { return !C->isZero(); }))
6043 return true;
6044
6045 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6046 // some degree.
6047 switch (Op.getOpcode()) {
6048 default:
6049 break;
6050
6051 case ISD::OR:
6052 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6053 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6054
6055 case ISD::VSELECT:
6056 case ISD::SELECT:
6057 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6058 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6059
6060 case ISD::SHL: {
6061 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6062 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6063 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6064 // 1 << X is never zero.
6065 if (ValKnown.One[0])
6066 return true;
6067 // If max shift cnt of known ones is non-zero, result is non-zero.
6068 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6069 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6070 !ValKnown.One.shl(MaxCnt).isZero())
6071 return true;
6072 break;
6073 }
6074 case ISD::UADDSAT:
6075 case ISD::UMAX:
6076 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6077 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6078
6079 // For smin/smax: If either operand is known negative/positive
6080 // respectively we don't need the other to be known at all.
6081 case ISD::SMAX: {
6082 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6083 if (Op1.isStrictlyPositive())
6084 return true;
6085
6086 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6087 if (Op0.isStrictlyPositive())
6088 return true;
6089
6090 if (Op1.isNonZero() && Op0.isNonZero())
6091 return true;
6092
6093 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6094 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6095 }
6096 case ISD::SMIN: {
6097 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6098 if (Op1.isNegative())
6099 return true;
6100
6101 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6102 if (Op0.isNegative())
6103 return true;
6104
6105 if (Op1.isNonZero() && Op0.isNonZero())
6106 return true;
6107
6108 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6109 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6110 }
6111 case ISD::UMIN:
6112 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6113 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6114
6115 case ISD::ROTL:
6116 case ISD::ROTR:
6117 case ISD::BITREVERSE:
6118 case ISD::BSWAP:
6119 case ISD::CTPOP:
6120 case ISD::ABS:
6121 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6122
6123 case ISD::SRA:
6124 case ISD::SRL: {
6125 if (Op->getFlags().hasExact())
6126 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6127 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6128 if (ValKnown.isNegative())
6129 return true;
6130 // If max shift cnt of known ones is non-zero, result is non-zero.
6131 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6132 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6133 !ValKnown.One.lshr(MaxCnt).isZero())
6134 return true;
6135 break;
6136 }
6137 case ISD::UDIV:
6138 case ISD::SDIV:
6139 // div exact can only produce a zero if the dividend is zero.
6140 // TODO: For udiv this is also true if Op1 u<= Op0
6141 if (Op->getFlags().hasExact())
6142 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6143 break;
6144
6145 case ISD::ADD:
6146 if (Op->getFlags().hasNoUnsignedWrap())
6147 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6148 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6149 return true;
6150 // TODO: There are a lot more cases we can prove for add.
6151 break;
6152
6153 case ISD::SUB: {
6154 if (isNullConstant(Op.getOperand(0)))
6155 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6156
6157 std::optional<bool> ne =
6158 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6159 computeKnownBits(Op.getOperand(1), Depth + 1));
6160 return ne && *ne;
6161 }
6162
6163 case ISD::MUL:
6164 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6165 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6166 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6167 return true;
6168 break;
6169
6170 case ISD::ZERO_EXTEND:
6171 case ISD::SIGN_EXTEND:
6172 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6173 case ISD::VSCALE: {
6175 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6176 ConstantRange CR =
6177 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6178 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6179 return true;
6180 break;
6181 }
6182 }
6183
6185}
6186
6188 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6189 return !C1->isNegative();
6190
6191 return Op.getOpcode() == ISD::FABS;
6192}
6193
6195 // Check the obvious case.
6196 if (A == B) return true;
6197
6198 // For negative and positive zero.
6199 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
6200 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
6201 if (CA->isZero() && CB->isZero()) return true;
6202
6203 // Otherwise they may not be equal.
6204 return false;
6205}
6206
6207// Only bits set in Mask must be negated, other bits may be arbitrary.
6209 if (isBitwiseNot(V, AllowUndefs))
6210 return V.getOperand(0);
6211
6212 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6213 // bits in the non-extended part.
6214 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6215 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6216 return SDValue();
6217 SDValue ExtArg = V.getOperand(0);
6218 if (ExtArg.getScalarValueSizeInBits() >=
6219 MaskC->getAPIntValue().getActiveBits() &&
6220 isBitwiseNot(ExtArg, AllowUndefs) &&
6221 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6222 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6223 return ExtArg.getOperand(0).getOperand(0);
6224 return SDValue();
6225}
6226
6228 // Match masked merge pattern (X & ~M) op (Y & M)
6229 // Including degenerate case (X & ~M) op M
6230 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6231 SDValue Other) {
6232 if (SDValue NotOperand =
6233 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6234 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6235 NotOperand->getOpcode() == ISD::TRUNCATE)
6236 NotOperand = NotOperand->getOperand(0);
6237
6238 if (Other == NotOperand)
6239 return true;
6240 if (Other->getOpcode() == ISD::AND)
6241 return NotOperand == Other->getOperand(0) ||
6242 NotOperand == Other->getOperand(1);
6243 }
6244 return false;
6245 };
6246
6247 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6248 A = A->getOperand(0);
6249
6250 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6251 B = B->getOperand(0);
6252
6253 if (A->getOpcode() == ISD::AND)
6254 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6255 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6256 return false;
6257}
6258
6259// FIXME: unify with llvm::haveNoCommonBitsSet.
6261 assert(A.getValueType() == B.getValueType() &&
6262 "Values must have the same type");
6265 return true;
6268}
6269
6270static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6271 SelectionDAG &DAG) {
6272 if (cast<ConstantSDNode>(Step)->isZero())
6273 return DAG.getConstant(0, DL, VT);
6274
6275 return SDValue();
6276}
6277
6280 SelectionDAG &DAG) {
6281 int NumOps = Ops.size();
6282 assert(NumOps != 0 && "Can't build an empty vector!");
6283 assert(!VT.isScalableVector() &&
6284 "BUILD_VECTOR cannot be used with scalable types");
6285 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6286 "Incorrect element count in BUILD_VECTOR!");
6287
6288 // BUILD_VECTOR of UNDEFs is UNDEF.
6289 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6290 return DAG.getUNDEF(VT);
6291
6292 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6293 SDValue IdentitySrc;
6294 bool IsIdentity = true;
6295 for (int i = 0; i != NumOps; ++i) {
6296 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6297 Ops[i].getOperand(0).getValueType() != VT ||
6298 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6299 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6300 Ops[i].getConstantOperandAPInt(1) != i) {
6301 IsIdentity = false;
6302 break;
6303 }
6304 IdentitySrc = Ops[i].getOperand(0);
6305 }
6306 if (IsIdentity)
6307 return IdentitySrc;
6308
6309 return SDValue();
6310}
6311
6312/// Try to simplify vector concatenation to an input value, undef, or build
6313/// vector.
6316 SelectionDAG &DAG) {
6317 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6318 assert(llvm::all_of(Ops,
6319 [Ops](SDValue Op) {
6320 return Ops[0].getValueType() == Op.getValueType();
6321 }) &&
6322 "Concatenation of vectors with inconsistent value types!");
6323 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6324 VT.getVectorElementCount() &&
6325 "Incorrect element count in vector concatenation!");
6326
6327 if (Ops.size() == 1)
6328 return Ops[0];
6329
6330 // Concat of UNDEFs is UNDEF.
6331 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6332 return DAG.getUNDEF(VT);
6333
6334 // Scan the operands and look for extract operations from a single source
6335 // that correspond to insertion at the same location via this concatenation:
6336 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6337 SDValue IdentitySrc;
6338 bool IsIdentity = true;
6339 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6340 SDValue Op = Ops[i];
6341 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6342 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6343 Op.getOperand(0).getValueType() != VT ||
6344 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6345 Op.getConstantOperandVal(1) != IdentityIndex) {
6346 IsIdentity = false;
6347 break;
6348 }
6349 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6350 "Unexpected identity source vector for concat of extracts");
6351 IdentitySrc = Op.getOperand(0);
6352 }
6353 if (IsIdentity) {
6354 assert(IdentitySrc && "Failed to set source vector of extracts");
6355 return IdentitySrc;
6356 }
6357
6358 // The code below this point is only designed to work for fixed width
6359 // vectors, so we bail out for now.
6360 if (VT.isScalableVector())
6361 return SDValue();
6362
6363 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
6364 // simplified to one big BUILD_VECTOR.
6365 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6366 EVT SVT = VT.getScalarType();
6368 for (SDValue Op : Ops) {
6369 EVT OpVT = Op.getValueType();
6370 if (Op.isUndef())
6371 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6372 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6373 Elts.append(Op->op_begin(), Op->op_end());
6374 else
6375 return SDValue();
6376 }
6377
6378 // BUILD_VECTOR requires all inputs to be of the same type, find the
6379 // maximum type and extend them all.
6380 for (SDValue Op : Elts)
6381 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6382
6383 if (SVT.bitsGT(VT.getScalarType())) {
6384 for (SDValue &Op : Elts) {
6385 if (Op.isUndef())
6386 Op = DAG.getUNDEF(SVT);
6387 else
6388 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6389 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6390 : DAG.getSExtOrTrunc(Op, DL, SVT);
6391 }
6392 }
6393
6394 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6395 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6396 return V;
6397}
6398
6399/// Gets or creates the specified node.
6400SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6401 SDVTList VTs = getVTList(VT);
6403 AddNodeIDNode(ID, Opcode, VTs, {});
6404 void *IP = nullptr;
6405 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6406 return SDValue(E, 0);
6407
6408 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6409 CSEMap.InsertNode(N, IP);
6410
6411 InsertNode(N);
6412 SDValue V = SDValue(N, 0);
6413 NewSDValueDbgMsg(V, "Creating new node: ", this);
6414 return V;
6415}
6416
6417SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6418 SDValue N1) {
6419 SDNodeFlags Flags;
6420 if (Inserter)
6421 Flags = Inserter->getFlags();
6422 return getNode(Opcode, DL, VT, N1, Flags);
6423}
6424
6425SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6426 SDValue N1, const SDNodeFlags Flags) {
6427 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6428
6429 // Constant fold unary operations with a vector integer or float operand.
6430 switch (Opcode) {
6431 default:
6432 // FIXME: Entirely reasonable to perform folding of other unary
6433 // operations here as the need arises.
6434 break;
6435 case ISD::FNEG:
6436 case ISD::FABS:
6437 case ISD::FCEIL:
6438 case ISD::FTRUNC:
6439 case ISD::FFLOOR:
6440 case ISD::FP_EXTEND:
6441 case ISD::FP_TO_SINT:
6442 case ISD::FP_TO_UINT:
6443 case ISD::FP_TO_FP16:
6444 case ISD::FP_TO_BF16:
6445 case ISD::TRUNCATE:
6446 case ISD::ANY_EXTEND:
6447 case ISD::ZERO_EXTEND:
6448 case ISD::SIGN_EXTEND:
6449 case ISD::UINT_TO_FP:
6450 case ISD::SINT_TO_FP:
6451 case ISD::FP16_TO_FP:
6452 case ISD::BF16_TO_FP:
6453 case ISD::BITCAST:
6454 case ISD::ABS:
6455 case ISD::BITREVERSE:
6456 case ISD::BSWAP:
6457 case ISD::CTLZ:
6459 case ISD::CTTZ:
6461 case ISD::CTPOP:
6462 case ISD::STEP_VECTOR: {
6463 SDValue Ops = {N1};
6464 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6465 return Fold;
6466 }
6467 }
6468
6469 unsigned OpOpcode = N1.getNode()->getOpcode();
6470 switch (Opcode) {
6471 case ISD::STEP_VECTOR:
6472 assert(VT.isScalableVector() &&
6473 "STEP_VECTOR can only be used with scalable types");
6474 assert(OpOpcode == ISD::TargetConstant &&
6475 VT.getVectorElementType() == N1.getValueType() &&
6476 "Unexpected step operand");
6477 break;
6478 case ISD::FREEZE:
6479 assert(VT == N1.getValueType() && "Unexpected VT!");
6480 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6481 return N1;
6482 break;
6483 case ISD::TokenFactor:
6484 case ISD::MERGE_VALUES:
6486 return N1; // Factor, merge or concat of one node? No need.
6487 case ISD::BUILD_VECTOR: {
6488 // Attempt to simplify BUILD_VECTOR.
6489 SDValue Ops[] = {N1};
6490 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6491 return V;
6492 break;
6493 }
6494 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6495 case ISD::FP_EXTEND:
6497 "Invalid FP cast!");
6498 if (N1.getValueType() == VT) return N1; // noop conversion.
6499 assert((!VT.isVector() || VT.getVectorElementCount() ==
6501 "Vector element count mismatch!");
6502 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6503 if (N1.isUndef())
6504 return getUNDEF(VT);
6505 break;
6506 case ISD::FP_TO_SINT:
6507 case ISD::FP_TO_UINT:
6508 if (N1.isUndef())
6509 return getUNDEF(VT);
6510 break;
6511 case ISD::SINT_TO_FP:
6512 case ISD::UINT_TO_FP:
6513 // [us]itofp(undef) = 0, because the result value is bounded.
6514 if (N1.isUndef())
6515 return getConstantFP(0.0, DL, VT);
6516 break;
6517 case ISD::SIGN_EXTEND:
6518 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6519 "Invalid SIGN_EXTEND!");
6520 assert(VT.isVector() == N1.getValueType().isVector() &&
6521 "SIGN_EXTEND result type type should be vector iff the operand "
6522 "type is vector!");
6523 if (N1.getValueType() == VT) return N1; // noop extension
6524 assert((!VT.isVector() || VT.getVectorElementCount() ==
6526 "Vector element count mismatch!");
6527 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6528 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6529 SDNodeFlags Flags;
6530 if (OpOpcode == ISD::ZERO_EXTEND)
6531 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6532 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6533 transferDbgValues(N1, NewVal);
6534 return NewVal;
6535 }
6536
6537 if (OpOpcode == ISD::POISON)
6538 return getPOISON(VT);
6539
6540 if (N1.isUndef())
6541 // sext(undef) = 0, because the top bits will all be the same.
6542 return getConstant(0, DL, VT);
6543
6544 // Skip unnecessary sext_inreg pattern:
6545 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6546 if (OpOpcode == ISD::TRUNCATE) {
6547 SDValue OpOp = N1.getOperand(0);
6548 if (OpOp.getValueType() == VT) {
6549 unsigned NumSignExtBits =
6551 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6552 transferDbgValues(N1, OpOp);
6553 return OpOp;
6554 }
6555 }
6556 }
6557 break;
6558 case ISD::ZERO_EXTEND:
6559 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6560 "Invalid ZERO_EXTEND!");
6561 assert(VT.isVector() == N1.getValueType().isVector() &&
6562 "ZERO_EXTEND result type type should be vector iff the operand "
6563 "type is vector!");
6564 if (N1.getValueType() == VT) return N1; // noop extension
6565 assert((!VT.isVector() || VT.getVectorElementCount() ==
6567 "Vector element count mismatch!");
6568 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6569 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6570 SDNodeFlags Flags;
6571 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6572 SDValue NewVal =
6573 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6574 transferDbgValues(N1, NewVal);
6575 return NewVal;
6576 }
6577
6578 if (OpOpcode == ISD::POISON)
6579 return getPOISON(VT);
6580
6581 if (N1.isUndef())
6582 // zext(undef) = 0, because the top bits will be zero.
6583 return getConstant(0, DL, VT);
6584
6585 // Skip unnecessary zext_inreg pattern:
6586 // (zext (trunc x)) -> x iff the upper bits are known zero.
6587 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6588 // use to recognise zext_inreg patterns.
6589 if (OpOpcode == ISD::TRUNCATE) {
6590 SDValue OpOp = N1.getOperand(0);
6591 if (OpOp.getValueType() == VT) {
6592 if (OpOp.getOpcode() != ISD::AND) {
6595 if (MaskedValueIsZero(OpOp, HiBits)) {
6596 transferDbgValues(N1, OpOp);
6597 return OpOp;
6598 }
6599 }
6600 }
6601 }
6602 break;
6603 case ISD::ANY_EXTEND:
6604 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6605 "Invalid ANY_EXTEND!");
6606 assert(VT.isVector() == N1.getValueType().isVector() &&
6607 "ANY_EXTEND result type type should be vector iff the operand "
6608 "type is vector!");
6609 if (N1.getValueType() == VT) return N1; // noop extension
6610 assert((!VT.isVector() || VT.getVectorElementCount() ==
6612 "Vector element count mismatch!");
6613 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6614
6615 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6616 OpOpcode == ISD::ANY_EXTEND) {
6617 SDNodeFlags Flags;
6618 if (OpOpcode == ISD::ZERO_EXTEND)
6619 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6620 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6621 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6622 }
6623 if (N1.isUndef())
6624 return getUNDEF(VT);
6625
6626 // (ext (trunc x)) -> x
6627 if (OpOpcode == ISD::TRUNCATE) {
6628 SDValue OpOp = N1.getOperand(0);
6629 if (OpOp.getValueType() == VT) {
6630 transferDbgValues(N1, OpOp);
6631 return OpOp;
6632 }
6633 }
6634 break;
6635 case ISD::TRUNCATE:
6636 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6637 "Invalid TRUNCATE!");
6638 assert(VT.isVector() == N1.getValueType().isVector() &&
6639 "TRUNCATE result type type should be vector iff the operand "
6640 "type is vector!");
6641 if (N1.getValueType() == VT) return N1; // noop truncate
6642 assert((!VT.isVector() || VT.getVectorElementCount() ==
6644 "Vector element count mismatch!");
6645 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6646 if (OpOpcode == ISD::TRUNCATE)
6647 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6648 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6649 OpOpcode == ISD::ANY_EXTEND) {
6650 // If the source is smaller than the dest, we still need an extend.
6652 VT.getScalarType())) {
6653 SDNodeFlags Flags;
6654 if (OpOpcode == ISD::ZERO_EXTEND)
6655 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6656 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6657 }
6658 if (N1.getOperand(0).getValueType().bitsGT(VT))
6659 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6660 return N1.getOperand(0);
6661 }
6662 if (N1.isUndef())
6663 return getUNDEF(VT);
6664 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6665 return getVScale(DL, VT,
6667 break;
6671 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6672 assert(N1.getValueType().bitsLE(VT) &&
6673 "The input must be the same size or smaller than the result.");
6676 "The destination vector type must have fewer lanes than the input.");
6677 break;
6678 case ISD::ABS:
6679 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6680 if (N1.isUndef())
6681 return getConstant(0, DL, VT);
6682 break;
6683 case ISD::BSWAP:
6684 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6685 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6686 "BSWAP types must be a multiple of 16 bits!");
6687 if (N1.isUndef())
6688 return getUNDEF(VT);
6689 // bswap(bswap(X)) -> X.
6690 if (OpOpcode == ISD::BSWAP)
6691 return N1.getOperand(0);
6692 break;
6693 case ISD::BITREVERSE:
6694 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6695 if (N1.isUndef())
6696 return getUNDEF(VT);
6697 break;
6698 case ISD::BITCAST:
6700 "Cannot BITCAST between types of different sizes!");
6701 if (VT == N1.getValueType()) return N1; // noop conversion.
6702 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6703 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6704 if (N1.isUndef())
6705 return getUNDEF(VT);
6706 break;
6708 assert(VT.isVector() && !N1.getValueType().isVector() &&
6709 (VT.getVectorElementType() == N1.getValueType() ||
6711 N1.getValueType().isInteger() &&
6713 "Illegal SCALAR_TO_VECTOR node!");
6714 if (N1.isUndef())
6715 return getUNDEF(VT);
6716 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6717 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6718 isa<ConstantSDNode>(N1.getOperand(1)) &&
6719 N1.getConstantOperandVal(1) == 0 &&
6720 N1.getOperand(0).getValueType() == VT)
6721 return N1.getOperand(0);
6722 break;
6723 case ISD::FNEG:
6724 // Negation of an unknown bag of bits is still completely undefined.
6725 if (N1.isUndef())
6726 return getUNDEF(VT);
6727
6728 if (OpOpcode == ISD::FNEG) // --X -> X
6729 return N1.getOperand(0);
6730 break;
6731 case ISD::FABS:
6732 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6733 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6734 break;
6735 case ISD::VSCALE:
6736 assert(VT == N1.getValueType() && "Unexpected VT!");
6737 break;
6738 case ISD::CTPOP:
6739 if (N1.getValueType().getScalarType() == MVT::i1)
6740 return N1;
6741 break;
6742 case ISD::CTLZ:
6743 case ISD::CTTZ:
6744 if (N1.getValueType().getScalarType() == MVT::i1)
6745 return getNOT(DL, N1, N1.getValueType());
6746 break;
6747 case ISD::VECREDUCE_ADD:
6748 if (N1.getValueType().getScalarType() == MVT::i1)
6749 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6750 break;
6753 if (N1.getValueType().getScalarType() == MVT::i1)
6754 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6755 break;
6758 if (N1.getValueType().getScalarType() == MVT::i1)
6759 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6760 break;
6761 case ISD::SPLAT_VECTOR:
6762 assert(VT.isVector() && "Wrong return type!");
6763 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6764 // that for now.
6766 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6768 N1.getValueType().isInteger() &&
6770 "Wrong operand type!");
6771 break;
6772 }
6773
6774 SDNode *N;
6775 SDVTList VTs = getVTList(VT);
6776 SDValue Ops[] = {N1};
6777 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6779 AddNodeIDNode(ID, Opcode, VTs, Ops);
6780 void *IP = nullptr;
6781 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6782 E->intersectFlagsWith(Flags);
6783 return SDValue(E, 0);
6784 }
6785
6786 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6787 N->setFlags(Flags);
6788 createOperands(N, Ops);
6789 CSEMap.InsertNode(N, IP);
6790 } else {
6791 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6792 createOperands(N, Ops);
6793 }
6794
6795 InsertNode(N);
6796 SDValue V = SDValue(N, 0);
6797 NewSDValueDbgMsg(V, "Creating new node: ", this);
6798 return V;
6799}
6800
6801static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6802 const APInt &C2) {
6803 switch (Opcode) {
6804 case ISD::ADD: return C1 + C2;
6805 case ISD::SUB: return C1 - C2;
6806 case ISD::MUL: return C1 * C2;
6807 case ISD::AND: return C1 & C2;
6808 case ISD::OR: return C1 | C2;
6809 case ISD::XOR: return C1 ^ C2;
6810 case ISD::SHL: return C1 << C2;
6811 case ISD::SRL: return C1.lshr(C2);
6812 case ISD::SRA: return C1.ashr(C2);
6813 case ISD::ROTL: return C1.rotl(C2);
6814 case ISD::ROTR: return C1.rotr(C2);
6815 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6816 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6817 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6818 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6819 case ISD::SADDSAT: return C1.sadd_sat(C2);
6820 case ISD::UADDSAT: return C1.uadd_sat(C2);
6821 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6822 case ISD::USUBSAT: return C1.usub_sat(C2);
6823 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6824 case ISD::USHLSAT: return C1.ushl_sat(C2);
6825 case ISD::UDIV:
6826 if (!C2.getBoolValue())
6827 break;
6828 return C1.udiv(C2);
6829 case ISD::UREM:
6830 if (!C2.getBoolValue())
6831 break;
6832 return C1.urem(C2);
6833 case ISD::SDIV:
6834 if (!C2.getBoolValue())
6835 break;
6836 return C1.sdiv(C2);
6837 case ISD::SREM:
6838 if (!C2.getBoolValue())
6839 break;
6840 return C1.srem(C2);
6841 case ISD::AVGFLOORS:
6842 return APIntOps::avgFloorS(C1, C2);
6843 case ISD::AVGFLOORU:
6844 return APIntOps::avgFloorU(C1, C2);
6845 case ISD::AVGCEILS:
6846 return APIntOps::avgCeilS(C1, C2);
6847 case ISD::AVGCEILU:
6848 return APIntOps::avgCeilU(C1, C2);
6849 case ISD::ABDS:
6850 return APIntOps::abds(C1, C2);
6851 case ISD::ABDU:
6852 return APIntOps::abdu(C1, C2);
6853 case ISD::MULHS:
6854 return APIntOps::mulhs(C1, C2);
6855 case ISD::MULHU:
6856 return APIntOps::mulhu(C1, C2);
6857 }
6858 return std::nullopt;
6859}
6860// Handle constant folding with UNDEF.
6861// TODO: Handle more cases.
6862static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6863 bool IsUndef1, const APInt &C2,
6864 bool IsUndef2) {
6865 if (!(IsUndef1 || IsUndef2))
6866 return FoldValue(Opcode, C1, C2);
6867
6868 // Fold and(x, undef) -> 0
6869 // Fold mul(x, undef) -> 0
6870 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6871 return APInt::getZero(C1.getBitWidth());
6872
6873 return std::nullopt;
6874}
6875
6877 const GlobalAddressSDNode *GA,
6878 const SDNode *N2) {
6879 if (GA->getOpcode() != ISD::GlobalAddress)
6880 return SDValue();
6881 if (!TLI->isOffsetFoldingLegal(GA))
6882 return SDValue();
6883 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6884 if (!C2)
6885 return SDValue();
6886 int64_t Offset = C2->getSExtValue();
6887 switch (Opcode) {
6888 case ISD::ADD:
6889 case ISD::PTRADD:
6890 break;
6891 case ISD::SUB: Offset = -uint64_t(Offset); break;
6892 default: return SDValue();
6893 }
6894 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6895 GA->getOffset() + uint64_t(Offset));
6896}
6897
6898bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6899 switch (Opcode) {
6900 case ISD::SDIV:
6901 case ISD::UDIV:
6902 case ISD::SREM:
6903 case ISD::UREM: {
6904 // If a divisor is zero/undef or any element of a divisor vector is
6905 // zero/undef, the whole op is undef.
6906 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6907 SDValue Divisor = Ops[1];
6908 if (Divisor.isUndef() || isNullConstant(Divisor))
6909 return true;
6910
6911 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6912 llvm::any_of(Divisor->op_values(),
6913 [](SDValue V) { return V.isUndef() ||
6914 isNullConstant(V); });
6915 // TODO: Handle signed overflow.
6916 }
6917 // TODO: Handle oversized shifts.
6918 default:
6919 return false;
6920 }
6921}
6922
6924 EVT VT, ArrayRef<SDValue> Ops,
6925 SDNodeFlags Flags) {
6926 // If the opcode is a target-specific ISD node, there's nothing we can
6927 // do here and the operand rules may not line up with the below, so
6928 // bail early.
6929 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6930 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6931 // foldCONCAT_VECTORS in getNode before this is called.
6932 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6933 return SDValue();
6934
6935 unsigned NumOps = Ops.size();
6936 if (NumOps == 0)
6937 return SDValue();
6938
6939 if (isUndef(Opcode, Ops))
6940 return getUNDEF(VT);
6941
6942 // Handle unary special cases.
6943 if (NumOps == 1) {
6944 SDValue N1 = Ops[0];
6945
6946 // Constant fold unary operations with an integer constant operand. Even
6947 // opaque constant will be folded, because the folding of unary operations
6948 // doesn't create new constants with different values. Nevertheless, the
6949 // opaque flag is preserved during folding to prevent future folding with
6950 // other constants.
6951 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6952 const APInt &Val = C->getAPIntValue();
6953 switch (Opcode) {
6954 case ISD::SIGN_EXTEND:
6955 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6956 C->isTargetOpcode(), C->isOpaque());
6957 case ISD::TRUNCATE:
6958 if (C->isOpaque())
6959 break;
6960 [[fallthrough]];
6961 case ISD::ZERO_EXTEND:
6962 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6963 C->isTargetOpcode(), C->isOpaque());
6964 case ISD::ANY_EXTEND:
6965 // Some targets like RISCV prefer to sign extend some types.
6966 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6967 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6968 C->isTargetOpcode(), C->isOpaque());
6969 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6970 C->isTargetOpcode(), C->isOpaque());
6971 case ISD::ABS:
6972 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6973 C->isOpaque());
6974 case ISD::BITREVERSE:
6975 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6976 C->isOpaque());
6977 case ISD::BSWAP:
6978 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6979 C->isOpaque());
6980 case ISD::CTPOP:
6981 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6982 C->isOpaque());
6983 case ISD::CTLZ:
6985 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6986 C->isOpaque());
6987 case ISD::CTTZ:
6989 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6990 C->isOpaque());
6991 case ISD::UINT_TO_FP:
6992 case ISD::SINT_TO_FP: {
6994 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6996 return getConstantFP(FPV, DL, VT);
6997 }
6998 case ISD::FP16_TO_FP:
6999 case ISD::BF16_TO_FP: {
7000 bool Ignored;
7001 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7002 : APFloat::BFloat(),
7003 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7004
7005 // This can return overflow, underflow, or inexact; we don't care.
7006 // FIXME need to be more flexible about rounding mode.
7008 &Ignored);
7009 return getConstantFP(FPV, DL, VT);
7010 }
7011 case ISD::STEP_VECTOR:
7012 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7013 return V;
7014 break;
7015 case ISD::BITCAST:
7016 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7017 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7018 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7019 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7020 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7021 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7022 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7023 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7024 break;
7025 }
7026 }
7027
7028 // Constant fold unary operations with a floating point constant operand.
7029 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7030 APFloat V = C->getValueAPF(); // make copy
7031 switch (Opcode) {
7032 case ISD::FNEG:
7033 V.changeSign();
7034 return getConstantFP(V, DL, VT);
7035 case ISD::FABS:
7036 V.clearSign();
7037 return getConstantFP(V, DL, VT);
7038 case ISD::FCEIL: {
7039 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7040 if (fs == APFloat::opOK || fs == APFloat::opInexact)
7041 return getConstantFP(V, DL, VT);
7042 return SDValue();
7043 }
7044 case ISD::FTRUNC: {
7045 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7046 if (fs == APFloat::opOK || fs == APFloat::opInexact)
7047 return getConstantFP(V, DL, VT);
7048 return SDValue();
7049 }
7050 case ISD::FFLOOR: {
7051 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7052 if (fs == APFloat::opOK || fs == APFloat::opInexact)
7053 return getConstantFP(V, DL, VT);
7054 return SDValue();
7055 }
7056 case ISD::FP_EXTEND: {
7057 bool ignored;
7058 // This can return overflow, underflow, or inexact; we don't care.
7059 // FIXME need to be more flexible about rounding mode.
7060 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7061 &ignored);
7062 return getConstantFP(V, DL, VT);
7063 }
7064 case ISD::FP_TO_SINT:
7065 case ISD::FP_TO_UINT: {
7066 bool ignored;
7067 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7068 // FIXME need to be more flexible about rounding mode.
7070 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7071 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7072 break;
7073 return getConstant(IntVal, DL, VT);
7074 }
7075 case ISD::FP_TO_FP16:
7076 case ISD::FP_TO_BF16: {
7077 bool Ignored;
7078 // This can return overflow, underflow, or inexact; we don't care.
7079 // FIXME need to be more flexible about rounding mode.
7080 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7081 : APFloat::BFloat(),
7083 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7084 }
7085 case ISD::BITCAST:
7086 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7087 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7088 VT);
7089 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7090 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7091 VT);
7092 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7093 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7094 VT);
7095 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7096 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7097 break;
7098 }
7099 }
7100
7101 // Early-out if we failed to constant fold a bitcast.
7102 if (Opcode == ISD::BITCAST)
7103 return SDValue();
7104 }
7105
7106 // Handle binops special cases.
7107 if (NumOps == 2) {
7108 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7109 return CFP;
7110
7111 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7112 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7113 if (C1->isOpaque() || C2->isOpaque())
7114 return SDValue();
7115
7116 std::optional<APInt> FoldAttempt =
7117 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7118 if (!FoldAttempt)
7119 return SDValue();
7120
7121 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7122 assert((!Folded || !VT.isVector()) &&
7123 "Can't fold vectors ops with scalar operands");
7124 return Folded;
7125 }
7126 }
7127
7128 // fold (add Sym, c) -> Sym+c
7129 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
7130 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7131 if (TLI->isCommutativeBinOp(Opcode))
7132 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
7133 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7134
7135 // fold (sext_in_reg c1) -> c2
7136 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7137 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7138
7139 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7140 unsigned FromBits = EVT.getScalarSizeInBits();
7141 Val <<= Val.getBitWidth() - FromBits;
7142 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7143 return getConstant(Val, DL, ConstantVT);
7144 };
7145
7146 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7147 const APInt &Val = C1->getAPIntValue();
7148 return SignExtendInReg(Val, VT);
7149 }
7150
7152 SmallVector<SDValue, 8> ScalarOps;
7153 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7154 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7155 SDValue Op = Ops[0].getOperand(I);
7156 if (Op.isUndef()) {
7157 ScalarOps.push_back(getUNDEF(OpVT));
7158 continue;
7159 }
7160 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7161 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7162 }
7163 return getBuildVector(VT, DL, ScalarOps);
7164 }
7165
7166 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7167 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7168 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7169 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7170 Ops[0].getOperand(0).getValueType()));
7171 }
7172 }
7173
7174 // Handle fshl/fshr special cases.
7175 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7176 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7177 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7178 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7179
7180 if (C1 && C2 && C3) {
7181 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7182 return SDValue();
7183 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7184 &V3 = C3->getAPIntValue();
7185
7186 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7187 : APIntOps::fshr(V1, V2, V3);
7188 return getConstant(FoldedVal, DL, VT);
7189 }
7190 }
7191
7192 // Handle fma/fmad special cases.
7193 if (Opcode == ISD::FMA || Opcode == ISD::FMAD) {
7194 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7195 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7196 Ops[2].getValueType() == VT && "FMA types must match!");
7197 ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(Ops[0]);
7198 ConstantFPSDNode *C2 = dyn_cast<ConstantFPSDNode>(Ops[1]);
7199 ConstantFPSDNode *C3 = dyn_cast<ConstantFPSDNode>(Ops[2]);
7200 if (C1 && C2 && C3) {
7201 APFloat V1 = C1->getValueAPF();
7202 const APFloat &V2 = C2->getValueAPF();
7203 const APFloat &V3 = C3->getValueAPF();
7204 if (Opcode == ISD::FMAD) {
7207 } else
7209 return getConstantFP(V1, DL, VT);
7210 }
7211 }
7212
7213 // This is for vector folding only from here on.
7214 if (!VT.isVector())
7215 return SDValue();
7216
7217 ElementCount NumElts = VT.getVectorElementCount();
7218
7219 // See if we can fold through any bitcasted integer ops.
7220 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7221 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7222 (Ops[0].getOpcode() == ISD::BITCAST ||
7223 Ops[1].getOpcode() == ISD::BITCAST)) {
7224 SDValue N1 = peekThroughBitcasts(Ops[0]);
7225 SDValue N2 = peekThroughBitcasts(Ops[1]);
7226 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7227 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7228 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7229 N2.getValueType().isInteger()) {
7230 bool IsLE = getDataLayout().isLittleEndian();
7231 unsigned EltBits = VT.getScalarSizeInBits();
7232 SmallVector<APInt> RawBits1, RawBits2;
7233 BitVector UndefElts1, UndefElts2;
7234 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7235 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7236 SmallVector<APInt> RawBits;
7237 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7238 std::optional<APInt> Fold = FoldValueWithUndef(
7239 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7240 if (!Fold)
7241 break;
7242 RawBits.push_back(*Fold);
7243 }
7244 if (RawBits.size() == NumElts.getFixedValue()) {
7245 // We have constant folded, but we might need to cast this again back
7246 // to the original (possibly legalized) type.
7247 EVT BVVT, BVEltVT;
7248 if (N1.getValueType() == VT) {
7249 BVVT = N1.getValueType();
7250 BVEltVT = BV1->getOperand(0).getValueType();
7251 } else {
7252 BVVT = N2.getValueType();
7253 BVEltVT = BV2->getOperand(0).getValueType();
7254 }
7255 unsigned BVEltBits = BVEltVT.getSizeInBits();
7256 SmallVector<APInt> DstBits;
7257 BitVector DstUndefs;
7259 DstBits, RawBits, DstUndefs,
7260 BitVector(RawBits.size(), false));
7261 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7262 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7263 if (DstUndefs[I])
7264 continue;
7265 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7266 }
7267 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7268 }
7269 }
7270 }
7271 }
7272
7273 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7274 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7275 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7276 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7277 APInt RHSVal;
7278 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7279 APInt NewStep = Opcode == ISD::MUL
7280 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7281 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7282 return getStepVector(DL, VT, NewStep);
7283 }
7284 }
7285
7286 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7287 return !Op.getValueType().isVector() ||
7288 Op.getValueType().getVectorElementCount() == NumElts;
7289 };
7290
7291 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7292 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7293 Op.getOpcode() == ISD::BUILD_VECTOR ||
7294 Op.getOpcode() == ISD::SPLAT_VECTOR;
7295 };
7296
7297 // All operands must be vector types with the same number of elements as
7298 // the result type and must be either UNDEF or a build/splat vector
7299 // or UNDEF scalars.
7300 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7301 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7302 return SDValue();
7303
7304 // If we are comparing vectors, then the result needs to be a i1 boolean that
7305 // is then extended back to the legal result type depending on how booleans
7306 // are represented.
7307 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7308 ISD::NodeType ExtendCode =
7309 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7312
7313 // Find legal integer scalar type for constant promotion and
7314 // ensure that its scalar size is at least as large as source.
7315 EVT LegalSVT = VT.getScalarType();
7316 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7317 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7318 if (LegalSVT.bitsLT(VT.getScalarType()))
7319 return SDValue();
7320 }
7321
7322 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7323 // only have one operand to check. For fixed-length vector types we may have
7324 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7325 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7326
7327 // Constant fold each scalar lane separately.
7328 SmallVector<SDValue, 4> ScalarResults;
7329 for (unsigned I = 0; I != NumVectorElts; I++) {
7330 SmallVector<SDValue, 4> ScalarOps;
7331 for (SDValue Op : Ops) {
7332 EVT InSVT = Op.getValueType().getScalarType();
7333 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7334 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7335 if (Op.isUndef())
7336 ScalarOps.push_back(getUNDEF(InSVT));
7337 else
7338 ScalarOps.push_back(Op);
7339 continue;
7340 }
7341
7342 SDValue ScalarOp =
7343 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7344 EVT ScalarVT = ScalarOp.getValueType();
7345
7346 // Build vector (integer) scalar operands may need implicit
7347 // truncation - do this before constant folding.
7348 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7349 // Don't create illegally-typed nodes unless they're constants or undef
7350 // - if we fail to constant fold we can't guarantee the (dead) nodes
7351 // we're creating will be cleaned up before being visited for
7352 // legalization.
7353 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7354 !isa<ConstantSDNode>(ScalarOp) &&
7355 TLI->getTypeAction(*getContext(), InSVT) !=
7357 return SDValue();
7358 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7359 }
7360
7361 ScalarOps.push_back(ScalarOp);
7362 }
7363
7364 // Constant fold the scalar operands.
7365 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7366
7367 // Scalar folding only succeeded if the result is a constant or UNDEF.
7368 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7369 ScalarResult.getOpcode() != ISD::ConstantFP)
7370 return SDValue();
7371
7372 // Legalize the (integer) scalar constant if necessary. We only do
7373 // this once we know the folding succeeded, since otherwise we would
7374 // get a node with illegal type which has a user.
7375 if (LegalSVT != SVT)
7376 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7377
7378 ScalarResults.push_back(ScalarResult);
7379 }
7380
7381 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7382 : getBuildVector(VT, DL, ScalarResults);
7383 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7384 return V;
7385}
7386
7388 EVT VT, ArrayRef<SDValue> Ops) {
7389 // TODO: Add support for unary/ternary fp opcodes.
7390 if (Ops.size() != 2)
7391 return SDValue();
7392
7393 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7394 // should. That will require dealing with a potentially non-default
7395 // rounding mode, checking the "opStatus" return value from the APFloat
7396 // math calculations, and possibly other variations.
7397 SDValue N1 = Ops[0];
7398 SDValue N2 = Ops[1];
7399 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7400 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7401 if (N1CFP && N2CFP) {
7402 APFloat C1 = N1CFP->getValueAPF(); // make copy
7403 const APFloat &C2 = N2CFP->getValueAPF();
7404 switch (Opcode) {
7405 case ISD::FADD:
7407 return getConstantFP(C1, DL, VT);
7408 case ISD::FSUB:
7410 return getConstantFP(C1, DL, VT);
7411 case ISD::FMUL:
7413 return getConstantFP(C1, DL, VT);
7414 case ISD::FDIV:
7416 return getConstantFP(C1, DL, VT);
7417 case ISD::FREM:
7418 C1.mod(C2);
7419 return getConstantFP(C1, DL, VT);
7420 case ISD::FCOPYSIGN:
7421 C1.copySign(C2);
7422 return getConstantFP(C1, DL, VT);
7423 case ISD::FMINNUM:
7424 return getConstantFP(minnum(C1, C2), DL, VT);
7425 case ISD::FMAXNUM:
7426 return getConstantFP(maxnum(C1, C2), DL, VT);
7427 case ISD::FMINIMUM:
7428 return getConstantFP(minimum(C1, C2), DL, VT);
7429 case ISD::FMAXIMUM:
7430 return getConstantFP(maximum(C1, C2), DL, VT);
7431 case ISD::FMINIMUMNUM:
7432 return getConstantFP(minimumnum(C1, C2), DL, VT);
7433 case ISD::FMAXIMUMNUM:
7434 return getConstantFP(maximumnum(C1, C2), DL, VT);
7435 default: break;
7436 }
7437 }
7438 if (N1CFP && Opcode == ISD::FP_ROUND) {
7439 APFloat C1 = N1CFP->getValueAPF(); // make copy
7440 bool Unused;
7441 // This can return overflow, underflow, or inexact; we don't care.
7442 // FIXME need to be more flexible about rounding mode.
7444 &Unused);
7445 return getConstantFP(C1, DL, VT);
7446 }
7447
7448 switch (Opcode) {
7449 case ISD::FSUB:
7450 // -0.0 - undef --> undef (consistent with "fneg undef")
7451 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7452 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7453 return getUNDEF(VT);
7454 [[fallthrough]];
7455
7456 case ISD::FADD:
7457 case ISD::FMUL:
7458 case ISD::FDIV:
7459 case ISD::FREM:
7460 // If both operands are undef, the result is undef. If 1 operand is undef,
7461 // the result is NaN. This should match the behavior of the IR optimizer.
7462 if (N1.isUndef() && N2.isUndef())
7463 return getUNDEF(VT);
7464 if (N1.isUndef() || N2.isUndef())
7466 }
7467 return SDValue();
7468}
7469
7471 const SDLoc &DL, EVT DstEltVT) {
7472 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7473
7474 // If this is already the right type, we're done.
7475 if (SrcEltVT == DstEltVT)
7476 return SDValue(BV, 0);
7477
7478 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7479 unsigned DstBitSize = DstEltVT.getSizeInBits();
7480
7481 // If this is a conversion of N elements of one type to N elements of another
7482 // type, convert each element. This handles FP<->INT cases.
7483 if (SrcBitSize == DstBitSize) {
7485 for (SDValue Op : BV->op_values()) {
7486 // If the vector element type is not legal, the BUILD_VECTOR operands
7487 // are promoted and implicitly truncated. Make that explicit here.
7488 if (Op.getValueType() != SrcEltVT)
7489 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7490 Ops.push_back(getBitcast(DstEltVT, Op));
7491 }
7492 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7494 return getBuildVector(VT, DL, Ops);
7495 }
7496
7497 // Otherwise, we're growing or shrinking the elements. To avoid having to
7498 // handle annoying details of growing/shrinking FP values, we convert them to
7499 // int first.
7500 if (SrcEltVT.isFloatingPoint()) {
7501 // Convert the input float vector to a int vector where the elements are the
7502 // same sizes.
7503 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7504 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7505 return FoldConstantBuildVector(cast<BuildVectorSDNode>(Tmp), DL,
7506 DstEltVT);
7507 return SDValue();
7508 }
7509
7510 // Now we know the input is an integer vector. If the output is a FP type,
7511 // convert to integer first, then to FP of the right size.
7512 if (DstEltVT.isFloatingPoint()) {
7513 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7514 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7515 return FoldConstantBuildVector(cast<BuildVectorSDNode>(Tmp), DL,
7516 DstEltVT);
7517 return SDValue();
7518 }
7519
7520 // Okay, we know the src/dst types are both integers of differing types.
7521 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7522
7523 // Extract the constant raw bit data.
7524 BitVector UndefElements;
7525 SmallVector<APInt> RawBits;
7526 bool IsLE = getDataLayout().isLittleEndian();
7527 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7528 return SDValue();
7529
7531 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7532 if (UndefElements[I])
7533 Ops.push_back(getUNDEF(DstEltVT));
7534 else
7535 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7536 }
7537
7538 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7539 return getBuildVector(VT, DL, Ops);
7540}
7541
7543 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7544
7545 // There's no need to assert on a byte-aligned pointer. All pointers are at
7546 // least byte aligned.
7547 if (A == Align(1))
7548 return Val;
7549
7550 SDVTList VTs = getVTList(Val.getValueType());
7552 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7553 ID.AddInteger(A.value());
7554
7555 void *IP = nullptr;
7556 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7557 return SDValue(E, 0);
7558
7559 auto *N =
7560 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7561 createOperands(N, {Val});
7562
7563 CSEMap.InsertNode(N, IP);
7564 InsertNode(N);
7565
7566 SDValue V(N, 0);
7567 NewSDValueDbgMsg(V, "Creating new node: ", this);
7568 return V;
7569}
7570
7571SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7572 SDValue N1, SDValue N2) {
7573 SDNodeFlags Flags;
7574 if (Inserter)
7575 Flags = Inserter->getFlags();
7576 return getNode(Opcode, DL, VT, N1, N2, Flags);
7577}
7578
7580 SDValue &N2) const {
7581 if (!TLI->isCommutativeBinOp(Opcode))
7582 return;
7583
7584 // Canonicalize:
7585 // binop(const, nonconst) -> binop(nonconst, const)
7588 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7589 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7590 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7591 std::swap(N1, N2);
7592
7593 // Canonicalize:
7594 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7595 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7597 std::swap(N1, N2);
7598}
7599
7600SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7601 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7603 N2.getOpcode() != ISD::DELETED_NODE &&
7604 "Operand is DELETED_NODE!");
7605
7606 canonicalizeCommutativeBinop(Opcode, N1, N2);
7607
7608 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7609 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7610
7611 // Don't allow undefs in vector splats - we might be returning N2 when folding
7612 // to zero etc.
7613 ConstantSDNode *N2CV =
7614 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7615
7616 switch (Opcode) {
7617 default: break;
7618 case ISD::TokenFactor:
7619 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7620 N2.getValueType() == MVT::Other && "Invalid token factor!");
7621 // Fold trivial token factors.
7622 if (N1.getOpcode() == ISD::EntryToken) return N2;
7623 if (N2.getOpcode() == ISD::EntryToken) return N1;
7624 if (N1 == N2) return N1;
7625 break;
7626 case ISD::BUILD_VECTOR: {
7627 // Attempt to simplify BUILD_VECTOR.
7628 SDValue Ops[] = {N1, N2};
7629 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7630 return V;
7631 break;
7632 }
7633 case ISD::CONCAT_VECTORS: {
7634 SDValue Ops[] = {N1, N2};
7635 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7636 return V;
7637 break;
7638 }
7639 case ISD::AND:
7640 assert(VT.isInteger() && "This operator does not apply to FP types!");
7641 assert(N1.getValueType() == N2.getValueType() &&
7642 N1.getValueType() == VT && "Binary operator types must match!");
7643 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7644 // worth handling here.
7645 if (N2CV && N2CV->isZero())
7646 return N2;
7647 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7648 return N1;
7649 break;
7650 case ISD::OR:
7651 case ISD::XOR:
7652 case ISD::ADD:
7653 case ISD::PTRADD:
7654 case ISD::SUB:
7655 assert(VT.isInteger() && "This operator does not apply to FP types!");
7656 assert(N1.getValueType() == N2.getValueType() &&
7657 N1.getValueType() == VT && "Binary operator types must match!");
7658 // The equal operand types requirement is unnecessarily strong for PTRADD.
7659 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7660 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7661 // logic everywhere where PTRADDs may be folded or combined to properly
7662 // support them. If/when we introduce pointer types to the SDAG, we will
7663 // need to relax this constraint.
7664
7665 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7666 // it's worth handling here.
7667 if (N2CV && N2CV->isZero())
7668 return N1;
7669 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7670 VT.getScalarType() == MVT::i1)
7671 return getNode(ISD::XOR, DL, VT, N1, N2);
7672 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7673 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7674 N2.getOpcode() == ISD::VSCALE) {
7675 const APInt &C1 = N1->getConstantOperandAPInt(0);
7676 const APInt &C2 = N2->getConstantOperandAPInt(0);
7677 return getVScale(DL, VT, C1 + C2);
7678 }
7679 break;
7680 case ISD::MUL:
7681 assert(VT.isInteger() && "This operator does not apply to FP types!");
7682 assert(N1.getValueType() == N2.getValueType() &&
7683 N1.getValueType() == VT && "Binary operator types must match!");
7684 if (VT.getScalarType() == MVT::i1)
7685 return getNode(ISD::AND, DL, VT, N1, N2);
7686 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7687 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7688 const APInt &N2CImm = N2C->getAPIntValue();
7689 return getVScale(DL, VT, MulImm * N2CImm);
7690 }
7691 break;
7692 case ISD::UDIV:
7693 case ISD::UREM:
7694 case ISD::MULHU:
7695 case ISD::MULHS:
7696 case ISD::SDIV:
7697 case ISD::SREM:
7698 case ISD::SADDSAT:
7699 case ISD::SSUBSAT:
7700 case ISD::UADDSAT:
7701 case ISD::USUBSAT:
7702 assert(VT.isInteger() && "This operator does not apply to FP types!");
7703 assert(N1.getValueType() == N2.getValueType() &&
7704 N1.getValueType() == VT && "Binary operator types must match!");
7705 if (VT.getScalarType() == MVT::i1) {
7706 // fold (add_sat x, y) -> (or x, y) for bool types.
7707 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7708 return getNode(ISD::OR, DL, VT, N1, N2);
7709 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7710 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7711 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7712 }
7713 break;
7714 case ISD::SCMP:
7715 case ISD::UCMP:
7716 assert(N1.getValueType() == N2.getValueType() &&
7717 "Types of operands of UCMP/SCMP must match");
7718 assert(N1.getValueType().isVector() == VT.isVector() &&
7719 "Operands and return type of must both be scalars or vectors");
7720 if (VT.isVector())
7723 "Result and operands must have the same number of elements");
7724 break;
7725 case ISD::AVGFLOORS:
7726 case ISD::AVGFLOORU:
7727 case ISD::AVGCEILS:
7728 case ISD::AVGCEILU:
7729 assert(VT.isInteger() && "This operator does not apply to FP types!");
7730 assert(N1.getValueType() == N2.getValueType() &&
7731 N1.getValueType() == VT && "Binary operator types must match!");
7732 break;
7733 case ISD::ABDS:
7734 case ISD::ABDU:
7735 assert(VT.isInteger() && "This operator does not apply to FP types!");
7736 assert(N1.getValueType() == N2.getValueType() &&
7737 N1.getValueType() == VT && "Binary operator types must match!");
7738 if (VT.getScalarType() == MVT::i1)
7739 return getNode(ISD::XOR, DL, VT, N1, N2);
7740 break;
7741 case ISD::SMIN:
7742 case ISD::UMAX:
7743 assert(VT.isInteger() && "This operator does not apply to FP types!");
7744 assert(N1.getValueType() == N2.getValueType() &&
7745 N1.getValueType() == VT && "Binary operator types must match!");
7746 if (VT.getScalarType() == MVT::i1)
7747 return getNode(ISD::OR, DL, VT, N1, N2);
7748 break;
7749 case ISD::SMAX:
7750 case ISD::UMIN:
7751 assert(VT.isInteger() && "This operator does not apply to FP types!");
7752 assert(N1.getValueType() == N2.getValueType() &&
7753 N1.getValueType() == VT && "Binary operator types must match!");
7754 if (VT.getScalarType() == MVT::i1)
7755 return getNode(ISD::AND, DL, VT, N1, N2);
7756 break;
7757 case ISD::FADD:
7758 case ISD::FSUB:
7759 case ISD::FMUL:
7760 case ISD::FDIV:
7761 case ISD::FREM:
7762 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7763 assert(N1.getValueType() == N2.getValueType() &&
7764 N1.getValueType() == VT && "Binary operator types must match!");
7765 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7766 return V;
7767 break;
7768 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7769 assert(N1.getValueType() == VT &&
7772 "Invalid FCOPYSIGN!");
7773 break;
7774 case ISD::SHL:
7775 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7776 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7777 const APInt &ShiftImm = N2C->getAPIntValue();
7778 return getVScale(DL, VT, MulImm << ShiftImm);
7779 }
7780 [[fallthrough]];
7781 case ISD::SRA:
7782 case ISD::SRL:
7783 if (SDValue V = simplifyShift(N1, N2))
7784 return V;
7785 [[fallthrough]];
7786 case ISD::ROTL:
7787 case ISD::ROTR:
7788 assert(VT == N1.getValueType() &&
7789 "Shift operators return type must be the same as their first arg");
7790 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7791 "Shifts only work on integers");
7792 assert((!VT.isVector() || VT == N2.getValueType()) &&
7793 "Vector shift amounts must be in the same as their first arg");
7794 // Verify that the shift amount VT is big enough to hold valid shift
7795 // amounts. This catches things like trying to shift an i1024 value by an
7796 // i8, which is easy to fall into in generic code that uses
7797 // TLI.getShiftAmount().
7800 "Invalid use of small shift amount with oversized value!");
7801
7802 // Always fold shifts of i1 values so the code generator doesn't need to
7803 // handle them. Since we know the size of the shift has to be less than the
7804 // size of the value, the shift/rotate count is guaranteed to be zero.
7805 if (VT == MVT::i1)
7806 return N1;
7807 if (N2CV && N2CV->isZero())
7808 return N1;
7809 break;
7810 case ISD::FP_ROUND:
7812 VT.bitsLE(N1.getValueType()) && N2C &&
7813 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7814 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7815 if (N1.getValueType() == VT) return N1; // noop conversion.
7816 break;
7817 case ISD::AssertNoFPClass: {
7819 "AssertNoFPClass is used for a non-floating type");
7820 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7821 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7822 assert(llvm::to_underlying(NoFPClass) <=
7823 BitmaskEnumDetail::Mask<FPClassTest>() &&
7824 "FPClassTest value too large");
7825 (void)NoFPClass;
7826 break;
7827 }
7828 case ISD::AssertSext:
7829 case ISD::AssertZext: {
7830 EVT EVT = cast<VTSDNode>(N2)->getVT();
7831 assert(VT == N1.getValueType() && "Not an inreg extend!");
7832 assert(VT.isInteger() && EVT.isInteger() &&
7833 "Cannot *_EXTEND_INREG FP types");
7834 assert(!EVT.isVector() &&
7835 "AssertSExt/AssertZExt type should be the vector element type "
7836 "rather than the vector type!");
7837 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7838 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7839 break;
7840 }
7842 EVT EVT = cast<VTSDNode>(N2)->getVT();
7843 assert(VT == N1.getValueType() && "Not an inreg extend!");
7844 assert(VT.isInteger() && EVT.isInteger() &&
7845 "Cannot *_EXTEND_INREG FP types");
7846 assert(EVT.isVector() == VT.isVector() &&
7847 "SIGN_EXTEND_INREG type should be vector iff the operand "
7848 "type is vector!");
7849 assert((!EVT.isVector() ||
7851 "Vector element counts must match in SIGN_EXTEND_INREG");
7852 assert(EVT.bitsLE(VT) && "Not extending!");
7853 if (EVT == VT) return N1; // Not actually extending
7854 break;
7855 }
7857 case ISD::FP_TO_UINT_SAT: {
7858 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7859 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7860 assert(N1.getValueType().isVector() == VT.isVector() &&
7861 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7862 "vector!");
7863 assert((!VT.isVector() || VT.getVectorElementCount() ==
7865 "Vector element counts must match in FP_TO_*INT_SAT");
7866 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7867 "Type to saturate to must be a scalar.");
7868 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7869 "Not extending!");
7870 break;
7871 }
7874 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7875 element type of the vector.");
7876
7877 // Extract from an undefined value or using an undefined index is undefined.
7878 if (N1.isUndef() || N2.isUndef())
7879 return getUNDEF(VT);
7880
7881 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7882 // vectors. For scalable vectors we will provide appropriate support for
7883 // dealing with arbitrary indices.
7884 if (N2C && N1.getValueType().isFixedLengthVector() &&
7885 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7886 return getUNDEF(VT);
7887
7888 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7889 // expanding copies of large vectors from registers. This only works for
7890 // fixed length vectors, since we need to know the exact number of
7891 // elements.
7892 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7894 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7895 return getExtractVectorElt(DL, VT,
7896 N1.getOperand(N2C->getZExtValue() / Factor),
7897 N2C->getZExtValue() % Factor);
7898 }
7899
7900 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7901 // lowering is expanding large vector constants.
7902 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7903 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7906 "BUILD_VECTOR used for scalable vectors");
7907 unsigned Index =
7908 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7909 SDValue Elt = N1.getOperand(Index);
7910
7911 if (VT != Elt.getValueType())
7912 // If the vector element type is not legal, the BUILD_VECTOR operands
7913 // are promoted and implicitly truncated, and the result implicitly
7914 // extended. Make that explicit here.
7915 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7916
7917 return Elt;
7918 }
7919
7920 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7921 // operations are lowered to scalars.
7922 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7923 // If the indices are the same, return the inserted element else
7924 // if the indices are known different, extract the element from
7925 // the original vector.
7926 SDValue N1Op2 = N1.getOperand(2);
7927 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7928
7929 if (N1Op2C && N2C) {
7930 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7931 if (VT == N1.getOperand(1).getValueType())
7932 return N1.getOperand(1);
7933 if (VT.isFloatingPoint()) {
7935 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7936 }
7937 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7938 }
7939 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7940 }
7941 }
7942
7943 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7944 // when vector types are scalarized and v1iX is legal.
7945 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7946 // Here we are completely ignoring the extract element index (N2),
7947 // which is fine for fixed width vectors, since any index other than 0
7948 // is undefined anyway. However, this cannot be ignored for scalable
7949 // vectors - in theory we could support this, but we don't want to do this
7950 // without a profitability check.
7951 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7953 N1.getValueType().getVectorNumElements() == 1) {
7954 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7955 N1.getOperand(1));
7956 }
7957 break;
7959 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7960 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7961 (N1.getValueType().isInteger() == VT.isInteger()) &&
7962 N1.getValueType() != VT &&
7963 "Wrong types for EXTRACT_ELEMENT!");
7964
7965 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7966 // 64-bit integers into 32-bit parts. Instead of building the extract of
7967 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7968 if (N1.getOpcode() == ISD::BUILD_PAIR)
7969 return N1.getOperand(N2C->getZExtValue());
7970
7971 // EXTRACT_ELEMENT of a constant int is also very common.
7972 if (N1C) {
7973 unsigned ElementSize = VT.getSizeInBits();
7974 unsigned Shift = ElementSize * N2C->getZExtValue();
7975 const APInt &Val = N1C->getAPIntValue();
7976 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7977 }
7978 break;
7980 EVT N1VT = N1.getValueType();
7981 assert(VT.isVector() && N1VT.isVector() &&
7982 "Extract subvector VTs must be vectors!");
7984 "Extract subvector VTs must have the same element type!");
7985 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7986 "Cannot extract a scalable vector from a fixed length vector!");
7987 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7989 "Extract subvector must be from larger vector to smaller vector!");
7990 assert(N2C && "Extract subvector index must be a constant");
7991 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7992 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7993 N1VT.getVectorMinNumElements()) &&
7994 "Extract subvector overflow!");
7995 assert(N2C->getAPIntValue().getBitWidth() ==
7997 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7998 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
7999 "Extract index is not a multiple of the output vector length");
8000
8001 // Trivial extraction.
8002 if (VT == N1VT)
8003 return N1;
8004
8005 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8006 if (N1.isUndef())
8007 return getUNDEF(VT);
8008
8009 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8010 // the concat have the same type as the extract.
8011 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8012 VT == N1.getOperand(0).getValueType()) {
8013 unsigned Factor = VT.getVectorMinNumElements();
8014 return N1.getOperand(N2C->getZExtValue() / Factor);
8015 }
8016
8017 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8018 // during shuffle legalization.
8019 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8020 VT == N1.getOperand(1).getValueType())
8021 return N1.getOperand(1);
8022 break;
8023 }
8024 }
8025
8026 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8027 switch (Opcode) {
8028 case ISD::XOR:
8029 case ISD::ADD:
8030 case ISD::PTRADD:
8031 case ISD::SUB:
8033 case ISD::UDIV:
8034 case ISD::SDIV:
8035 case ISD::UREM:
8036 case ISD::SREM:
8037 case ISD::MUL:
8038 case ISD::AND:
8039 case ISD::SSUBSAT:
8040 case ISD::USUBSAT:
8041 case ISD::UMIN:
8042 case ISD::OR:
8043 case ISD::SADDSAT:
8044 case ISD::UADDSAT:
8045 case ISD::UMAX:
8046 case ISD::SMAX:
8047 case ISD::SMIN:
8048 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8049 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8050 }
8051 }
8052
8053 // Canonicalize an UNDEF to the RHS, even over a constant.
8054 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8055 if (TLI->isCommutativeBinOp(Opcode)) {
8056 std::swap(N1, N2);
8057 } else {
8058 switch (Opcode) {
8059 case ISD::PTRADD:
8060 case ISD::SUB:
8061 // fold op(undef, non_undef_arg2) -> undef.
8062 return N1;
8064 case ISD::UDIV:
8065 case ISD::SDIV:
8066 case ISD::UREM:
8067 case ISD::SREM:
8068 case ISD::SSUBSAT:
8069 case ISD::USUBSAT:
8070 // fold op(undef, non_undef_arg2) -> 0.
8071 return getConstant(0, DL, VT);
8072 }
8073 }
8074 }
8075
8076 // Fold a bunch of operators when the RHS is undef.
8077 if (N2.getOpcode() == ISD::UNDEF) {
8078 switch (Opcode) {
8079 case ISD::XOR:
8080 if (N1.getOpcode() == ISD::UNDEF)
8081 // Handle undef ^ undef -> 0 special case. This is a common
8082 // idiom (misuse).
8083 return getConstant(0, DL, VT);
8084 [[fallthrough]];
8085 case ISD::ADD:
8086 case ISD::PTRADD:
8087 case ISD::SUB:
8088 // fold op(arg1, undef) -> undef.
8089 return N2;
8090 case ISD::UDIV:
8091 case ISD::SDIV:
8092 case ISD::UREM:
8093 case ISD::SREM:
8094 // fold op(arg1, undef) -> poison.
8095 return getPOISON(VT);
8096 case ISD::MUL:
8097 case ISD::AND:
8098 case ISD::SSUBSAT:
8099 case ISD::USUBSAT:
8100 case ISD::UMIN:
8101 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8102 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8103 case ISD::OR:
8104 case ISD::SADDSAT:
8105 case ISD::UADDSAT:
8106 case ISD::UMAX:
8107 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8108 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8109 case ISD::SMAX:
8110 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8111 return N1.getOpcode() == ISD::UNDEF
8112 ? N2
8113 : getConstant(
8115 VT);
8116 case ISD::SMIN:
8117 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8118 return N1.getOpcode() == ISD::UNDEF
8119 ? N2
8120 : getConstant(
8122 VT);
8123 }
8124 }
8125
8126 // Perform trivial constant folding.
8127 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8128 return SV;
8129
8130 // Memoize this node if possible.
8131 SDNode *N;
8132 SDVTList VTs = getVTList(VT);
8133 SDValue Ops[] = {N1, N2};
8134 if (VT != MVT::Glue) {
8136 AddNodeIDNode(ID, Opcode, VTs, Ops);
8137 void *IP = nullptr;
8138 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8139 E->intersectFlagsWith(Flags);
8140 return SDValue(E, 0);
8141 }
8142
8143 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8144 N->setFlags(Flags);
8145 createOperands(N, Ops);
8146 CSEMap.InsertNode(N, IP);
8147 } else {
8148 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8149 createOperands(N, Ops);
8150 }
8151
8152 InsertNode(N);
8153 SDValue V = SDValue(N, 0);
8154 NewSDValueDbgMsg(V, "Creating new node: ", this);
8155 return V;
8156}
8157
8158SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8159 SDValue N1, SDValue N2, SDValue N3) {
8160 SDNodeFlags Flags;
8161 if (Inserter)
8162 Flags = Inserter->getFlags();
8163 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8164}
8165
8166SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8167 SDValue N1, SDValue N2, SDValue N3,
8168 const SDNodeFlags Flags) {
8170 N2.getOpcode() != ISD::DELETED_NODE &&
8171 N3.getOpcode() != ISD::DELETED_NODE &&
8172 "Operand is DELETED_NODE!");
8173 // Perform various simplifications.
8174 switch (Opcode) {
8175 case ISD::BUILD_VECTOR: {
8176 // Attempt to simplify BUILD_VECTOR.
8177 SDValue Ops[] = {N1, N2, N3};
8178 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8179 return V;
8180 break;
8181 }
8182 case ISD::CONCAT_VECTORS: {
8183 SDValue Ops[] = {N1, N2, N3};
8184 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8185 return V;
8186 break;
8187 }
8188 case ISD::SETCC: {
8189 assert(VT.isInteger() && "SETCC result type must be an integer!");
8190 assert(N1.getValueType() == N2.getValueType() &&
8191 "SETCC operands must have the same type!");
8192 assert(VT.isVector() == N1.getValueType().isVector() &&
8193 "SETCC type should be vector iff the operand type is vector!");
8194 assert((!VT.isVector() || VT.getVectorElementCount() ==
8196 "SETCC vector element counts must match!");
8197 // Use FoldSetCC to simplify SETCC's.
8198 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8199 return V;
8200 break;
8201 }
8202 case ISD::SELECT:
8203 case ISD::VSELECT:
8204 if (SDValue V = simplifySelect(N1, N2, N3))
8205 return V;
8206 break;
8208 llvm_unreachable("should use getVectorShuffle constructor!");
8209 case ISD::VECTOR_SPLICE: {
8210 if (cast<ConstantSDNode>(N3)->isZero())
8211 return N1;
8212 break;
8213 }
8215 assert(VT.isVector() && VT == N1.getValueType() &&
8216 "INSERT_VECTOR_ELT vector type mismatch");
8218 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8219 assert((!VT.isFloatingPoint() ||
8220 VT.getVectorElementType() == N2.getValueType()) &&
8221 "INSERT_VECTOR_ELT fp scalar type mismatch");
8222 assert((!VT.isInteger() ||
8224 "INSERT_VECTOR_ELT int scalar size mismatch");
8225
8226 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8227 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8228 // for scalable vectors where we will generate appropriate code to
8229 // deal with out-of-bounds cases correctly.
8230 if (N3C && N1.getValueType().isFixedLengthVector() &&
8231 N3C->getZExtValue() >= N1.getValueType().getVectorNumElements())
8232 return getUNDEF(VT);
8233
8234 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8235 if (N3.isUndef())
8236 return getUNDEF(VT);
8237
8238 // If the inserted element is an UNDEF, just use the input vector.
8239 if (N2.isUndef())
8240 return N1;
8241
8242 break;
8243 }
8244 case ISD::INSERT_SUBVECTOR: {
8245 // Inserting undef into undef is still undef.
8246 if (N1.isUndef() && N2.isUndef())
8247 return getUNDEF(VT);
8248
8249 EVT N2VT = N2.getValueType();
8250 assert(VT == N1.getValueType() &&
8251 "Dest and insert subvector source types must match!");
8252 assert(VT.isVector() && N2VT.isVector() &&
8253 "Insert subvector VTs must be vectors!");
8255 "Insert subvector VTs must have the same element type!");
8256 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8257 "Cannot insert a scalable vector into a fixed length vector!");
8258 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8260 "Insert subvector must be from smaller vector to larger vector!");
8261 assert(isa<ConstantSDNode>(N3) &&
8262 "Insert subvector index must be constant");
8263 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8264 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8266 "Insert subvector overflow!");
8269 "Constant index for INSERT_SUBVECTOR has an invalid size");
8270
8271 // Trivial insertion.
8272 if (VT == N2VT)
8273 return N2;
8274
8275 // If this is an insert of an extracted vector into an undef vector, we
8276 // can just use the input to the extract.
8277 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8278 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
8279 return N2.getOperand(0);
8280 break;
8281 }
8282 case ISD::BITCAST:
8283 // Fold bit_convert nodes from a type to themselves.
8284 if (N1.getValueType() == VT)
8285 return N1;
8286 break;
8287 case ISD::VP_TRUNCATE:
8288 case ISD::VP_SIGN_EXTEND:
8289 case ISD::VP_ZERO_EXTEND:
8290 // Don't create noop casts.
8291 if (N1.getValueType() == VT)
8292 return N1;
8293 break;
8294 case ISD::VECTOR_COMPRESS: {
8295 [[maybe_unused]] EVT VecVT = N1.getValueType();
8296 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8297 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8298 assert(VT == VecVT && "Vector and result type don't match.");
8299 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8300 "All inputs must be vectors.");
8301 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8303 "Vector and mask must have same number of elements.");
8304
8305 if (N1.isUndef() || N2.isUndef())
8306 return N3;
8307
8308 break;
8309 }
8313 [[maybe_unused]] EVT AccVT = N1.getValueType();
8314 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8315 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8316 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8317 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8318 "node to have the same type!");
8319 assert(VT.isVector() && VT == AccVT &&
8320 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8321 "the same type as its result!");
8323 AccVT.getVectorElementCount()) &&
8324 "Expected the element count of the second and third operands of the "
8325 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8326 "element count of the first operand and the result!");
8328 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8329 "node to have an element type which is the same as or smaller than "
8330 "the element type of the first operand and result!");
8331 break;
8332 }
8333 }
8334
8335 // Perform trivial constant folding for arithmetic operators.
8336 switch (Opcode) {
8337 case ISD::FMA:
8338 case ISD::FMAD:
8339 case ISD::SETCC:
8340 case ISD::FSHL:
8341 case ISD::FSHR:
8342 if (SDValue SV =
8343 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8344 return SV;
8345 break;
8346 }
8347
8348 // Memoize node if it doesn't produce a glue result.
8349 SDNode *N;
8350 SDVTList VTs = getVTList(VT);
8351 SDValue Ops[] = {N1, N2, N3};
8352 if (VT != MVT::Glue) {
8354 AddNodeIDNode(ID, Opcode, VTs, Ops);
8355 void *IP = nullptr;
8356 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8357 E->intersectFlagsWith(Flags);
8358 return SDValue(E, 0);
8359 }
8360
8361 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8362 N->setFlags(Flags);
8363 createOperands(N, Ops);
8364 CSEMap.InsertNode(N, IP);
8365 } else {
8366 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8367 createOperands(N, Ops);
8368 }
8369
8370 InsertNode(N);
8371 SDValue V = SDValue(N, 0);
8372 NewSDValueDbgMsg(V, "Creating new node: ", this);
8373 return V;
8374}
8375
8376SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8377 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8378 const SDNodeFlags Flags) {
8379 SDValue Ops[] = { N1, N2, N3, N4 };
8380 return getNode(Opcode, DL, VT, Ops, Flags);
8381}
8382
8383SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8384 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8385 SDNodeFlags Flags;
8386 if (Inserter)
8387 Flags = Inserter->getFlags();
8388 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8389}
8390
8391SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8392 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8393 SDValue N5, const SDNodeFlags Flags) {
8394 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8395 return getNode(Opcode, DL, VT, Ops, Flags);
8396}
8397
8398SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8399 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8400 SDValue N5) {
8401 SDNodeFlags Flags;
8402 if (Inserter)
8403 Flags = Inserter->getFlags();
8404 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8405}
8406
8407/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8408/// the incoming stack arguments to be loaded from the stack.
8410 SmallVector<SDValue, 8> ArgChains;
8411
8412 // Include the original chain at the beginning of the list. When this is
8413 // used by target LowerCall hooks, this helps legalize find the
8414 // CALLSEQ_BEGIN node.
8415 ArgChains.push_back(Chain);
8416
8417 // Add a chain value for each stack argument.
8418 for (SDNode *U : getEntryNode().getNode()->users())
8419 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8420 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8421 if (FI->getIndex() < 0)
8422 ArgChains.push_back(SDValue(L, 1));
8423
8424 // Build a tokenfactor for all the chains.
8425 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8426}
8427
8428/// getMemsetValue - Vectorized representation of the memset value
8429/// operand.
8431 const SDLoc &dl) {
8432 assert(!Value.isUndef());
8433
8434 unsigned NumBits = VT.getScalarSizeInBits();
8435 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
8436 assert(C->getAPIntValue().getBitWidth() == 8);
8437 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8438 if (VT.isInteger()) {
8439 bool IsOpaque = VT.getSizeInBits() > 64 ||
8440 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8441 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8442 }
8443 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8444 }
8445
8446 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8447 EVT IntVT = VT.getScalarType();
8448 if (!IntVT.isInteger())
8449 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8450
8451 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8452 if (NumBits > 8) {
8453 // Use a multiplication with 0x010101... to extend the input to the
8454 // required length.
8455 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8456 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8457 DAG.getConstant(Magic, dl, IntVT));
8458 }
8459
8460 if (VT != Value.getValueType() && !VT.isInteger())
8461 Value = DAG.getBitcast(VT.getScalarType(), Value);
8462 if (VT != Value.getValueType())
8463 Value = DAG.getSplatBuildVector(VT, dl, Value);
8464
8465 return Value;
8466}
8467
8468/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8469/// used when a memcpy is turned into a memset when the source is a constant
8470/// string ptr.
8472 const TargetLowering &TLI,
8473 const ConstantDataArraySlice &Slice) {
8474 // Handle vector with all elements zero.
8475 if (Slice.Array == nullptr) {
8476 if (VT.isInteger())
8477 return DAG.getConstant(0, dl, VT);
8478 return DAG.getNode(ISD::BITCAST, dl, VT,
8479 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8480 }
8481
8482 assert(!VT.isVector() && "Can't handle vector type here!");
8483 unsigned NumVTBits = VT.getSizeInBits();
8484 unsigned NumVTBytes = NumVTBits / 8;
8485 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8486
8487 APInt Val(NumVTBits, 0);
8488 if (DAG.getDataLayout().isLittleEndian()) {
8489 for (unsigned i = 0; i != NumBytes; ++i)
8490 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8491 } else {
8492 for (unsigned i = 0; i != NumBytes; ++i)
8493 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8494 }
8495
8496 // If the "cost" of materializing the integer immediate is less than the cost
8497 // of a load, then it is cost effective to turn the load into the immediate.
8498 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8499 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8500 return DAG.getConstant(Val, dl, VT);
8501 return SDValue();
8502}
8503
8505 const SDLoc &DL,
8506 const SDNodeFlags Flags) {
8507 EVT VT = Base.getValueType();
8508 SDValue Index;
8509
8510 if (Offset.isScalable())
8511 Index = getVScale(DL, Base.getValueType(),
8512 APInt(Base.getValueSizeInBits().getFixedValue(),
8513 Offset.getKnownMinValue()));
8514 else
8515 Index = getConstant(Offset.getFixedValue(), DL, VT);
8516
8517 return getMemBasePlusOffset(Base, Index, DL, Flags);
8518}
8519
8521 const SDLoc &DL,
8522 const SDNodeFlags Flags) {
8523 assert(Offset.getValueType().isInteger());
8524 EVT BasePtrVT = Ptr.getValueType();
8525 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8526 BasePtrVT))
8527 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8528 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
8529}
8530
8531/// Returns true if memcpy source is constant data.
8533 uint64_t SrcDelta = 0;
8534 GlobalAddressSDNode *G = nullptr;
8535 if (Src.getOpcode() == ISD::GlobalAddress)
8536 G = cast<GlobalAddressSDNode>(Src);
8537 else if (Src.getOpcode() == ISD::ADD &&
8538 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8539 Src.getOperand(1).getOpcode() == ISD::Constant) {
8540 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8541 SrcDelta = Src.getConstantOperandVal(1);
8542 }
8543 if (!G)
8544 return false;
8545
8546 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8547 SrcDelta + G->getOffset());
8548}
8549
8551 SelectionDAG &DAG) {
8552 // On Darwin, -Os means optimize for size without hurting performance, so
8553 // only really optimize for size when -Oz (MinSize) is used.
8555 return MF.getFunction().hasMinSize();
8556 return DAG.shouldOptForSize();
8557}
8558
8560 SmallVector<SDValue, 32> &OutChains, unsigned From,
8561 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8562 SmallVector<SDValue, 16> &OutStoreChains) {
8563 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8564 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8565 SmallVector<SDValue, 16> GluedLoadChains;
8566 for (unsigned i = From; i < To; ++i) {
8567 OutChains.push_back(OutLoadChains[i]);
8568 GluedLoadChains.push_back(OutLoadChains[i]);
8569 }
8570
8571 // Chain for all loads.
8572 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8573 GluedLoadChains);
8574
8575 for (unsigned i = From; i < To; ++i) {
8576 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8577 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8578 ST->getBasePtr(), ST->getMemoryVT(),
8579 ST->getMemOperand());
8580 OutChains.push_back(NewStore);
8581 }
8582}
8583
8585 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8586 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8587 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8588 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8589 // Turn a memcpy of undef to nop.
8590 // FIXME: We need to honor volatile even is Src is undef.
8591 if (Src.isUndef())
8592 return Chain;
8593
8594 // Expand memcpy to a series of load and store ops if the size operand falls
8595 // below a certain threshold.
8596 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8597 // rather than maybe a humongous number of loads and stores.
8598 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8599 const DataLayout &DL = DAG.getDataLayout();
8600 LLVMContext &C = *DAG.getContext();
8601 std::vector<EVT> MemOps;
8602 bool DstAlignCanChange = false;
8604 MachineFrameInfo &MFI = MF.getFrameInfo();
8605 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8606 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8607 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8608 DstAlignCanChange = true;
8609 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8610 if (!SrcAlign || Alignment > *SrcAlign)
8611 SrcAlign = Alignment;
8612 assert(SrcAlign && "SrcAlign must be set");
8614 // If marked as volatile, perform a copy even when marked as constant.
8615 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8616 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8617 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8618 const MemOp Op = isZeroConstant
8619 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8620 /*IsZeroMemset*/ true, isVol)
8621 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8622 *SrcAlign, isVol, CopyFromConstant);
8623 if (!TLI.findOptimalMemOpLowering(
8624 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8625 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8626 return SDValue();
8627
8628 if (DstAlignCanChange) {
8629 Type *Ty = MemOps[0].getTypeForEVT(C);
8630 Align NewAlign = DL.getABITypeAlign(Ty);
8631
8632 // Don't promote to an alignment that would require dynamic stack
8633 // realignment which may conflict with optimizations such as tail call
8634 // optimization.
8636 if (!TRI->hasStackRealignment(MF))
8637 if (MaybeAlign StackAlign = DL.getStackAlignment())
8638 NewAlign = std::min(NewAlign, *StackAlign);
8639
8640 if (NewAlign > Alignment) {
8641 // Give the stack frame object a larger alignment if needed.
8642 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8643 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8644 Alignment = NewAlign;
8645 }
8646 }
8647
8648 // Prepare AAInfo for loads/stores after lowering this memcpy.
8649 AAMDNodes NewAAInfo = AAInfo;
8650 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8651
8652 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8653 bool isConstant =
8654 BatchAA && SrcVal &&
8655 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8656
8657 MachineMemOperand::Flags MMOFlags =
8659 SmallVector<SDValue, 16> OutLoadChains;
8660 SmallVector<SDValue, 16> OutStoreChains;
8661 SmallVector<SDValue, 32> OutChains;
8662 unsigned NumMemOps = MemOps.size();
8663 uint64_t SrcOff = 0, DstOff = 0;
8664 for (unsigned i = 0; i != NumMemOps; ++i) {
8665 EVT VT = MemOps[i];
8666 unsigned VTSize = VT.getSizeInBits() / 8;
8667 SDValue Value, Store;
8668
8669 if (VTSize > Size) {
8670 // Issuing an unaligned load / store pair that overlaps with the previous
8671 // pair. Adjust the offset accordingly.
8672 assert(i == NumMemOps-1 && i != 0);
8673 SrcOff -= VTSize - Size;
8674 DstOff -= VTSize - Size;
8675 }
8676
8677 if (CopyFromConstant &&
8678 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8679 // It's unlikely a store of a vector immediate can be done in a single
8680 // instruction. It would require a load from a constantpool first.
8681 // We only handle zero vectors here.
8682 // FIXME: Handle other cases where store of vector immediate is done in
8683 // a single instruction.
8684 ConstantDataArraySlice SubSlice;
8685 if (SrcOff < Slice.Length) {
8686 SubSlice = Slice;
8687 SubSlice.move(SrcOff);
8688 } else {
8689 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8690 SubSlice.Array = nullptr;
8691 SubSlice.Offset = 0;
8692 SubSlice.Length = VTSize;
8693 }
8694 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8695 if (Value.getNode()) {
8696 Store = DAG.getStore(
8697 Chain, dl, Value,
8698 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8699 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8700 OutChains.push_back(Store);
8701 }
8702 }
8703
8704 if (!Store.getNode()) {
8705 // The type might not be legal for the target. This should only happen
8706 // if the type is smaller than a legal type, as on PPC, so the right
8707 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8708 // to Load/Store if NVT==VT.
8709 // FIXME does the case above also need this?
8710 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8711 assert(NVT.bitsGE(VT));
8712
8713 bool isDereferenceable =
8714 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8715 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8716 if (isDereferenceable)
8718 if (isConstant)
8719 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8720
8721 Value = DAG.getExtLoad(
8722 ISD::EXTLOAD, dl, NVT, Chain,
8723 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8724 SrcPtrInfo.getWithOffset(SrcOff), VT,
8725 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8726 OutLoadChains.push_back(Value.getValue(1));
8727
8728 Store = DAG.getTruncStore(
8729 Chain, dl, Value,
8730 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8731 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8732 OutStoreChains.push_back(Store);
8733 }
8734 SrcOff += VTSize;
8735 DstOff += VTSize;
8736 Size -= VTSize;
8737 }
8738
8739 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8741 unsigned NumLdStInMemcpy = OutStoreChains.size();
8742
8743 if (NumLdStInMemcpy) {
8744 // It may be that memcpy might be converted to memset if it's memcpy
8745 // of constants. In such a case, we won't have loads and stores, but
8746 // just stores. In the absence of loads, there is nothing to gang up.
8747 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8748 // If target does not care, just leave as it.
8749 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8750 OutChains.push_back(OutLoadChains[i]);
8751 OutChains.push_back(OutStoreChains[i]);
8752 }
8753 } else {
8754 // Ld/St less than/equal limit set by target.
8755 if (NumLdStInMemcpy <= GluedLdStLimit) {
8756 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8757 NumLdStInMemcpy, OutLoadChains,
8758 OutStoreChains);
8759 } else {
8760 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8761 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8762 unsigned GlueIter = 0;
8763
8764 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8765 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8766 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8767
8768 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8769 OutLoadChains, OutStoreChains);
8770 GlueIter += GluedLdStLimit;
8771 }
8772
8773 // Residual ld/st.
8774 if (RemainingLdStInMemcpy) {
8775 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8776 RemainingLdStInMemcpy, OutLoadChains,
8777 OutStoreChains);
8778 }
8779 }
8780 }
8781 }
8782 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8783}
8784
8786 SDValue Chain, SDValue Dst, SDValue Src,
8787 uint64_t Size, Align Alignment,
8788 bool isVol, bool AlwaysInline,
8789 MachinePointerInfo DstPtrInfo,
8790 MachinePointerInfo SrcPtrInfo,
8791 const AAMDNodes &AAInfo) {
8792 // Turn a memmove of undef to nop.
8793 // FIXME: We need to honor volatile even is Src is undef.
8794 if (Src.isUndef())
8795 return Chain;
8796
8797 // Expand memmove to a series of load and store ops if the size operand falls
8798 // below a certain threshold.
8799 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8800 const DataLayout &DL = DAG.getDataLayout();
8801 LLVMContext &C = *DAG.getContext();
8802 std::vector<EVT> MemOps;
8803 bool DstAlignCanChange = false;
8805 MachineFrameInfo &MFI = MF.getFrameInfo();
8806 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8807 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8808 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8809 DstAlignCanChange = true;
8810 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8811 if (!SrcAlign || Alignment > *SrcAlign)
8812 SrcAlign = Alignment;
8813 assert(SrcAlign && "SrcAlign must be set");
8814 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8815 if (!TLI.findOptimalMemOpLowering(
8816 C, MemOps, Limit,
8817 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8818 /*IsVolatile*/ true),
8819 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8820 MF.getFunction().getAttributes()))
8821 return SDValue();
8822
8823 if (DstAlignCanChange) {
8824 Type *Ty = MemOps[0].getTypeForEVT(C);
8825 Align NewAlign = DL.getABITypeAlign(Ty);
8826
8827 // Don't promote to an alignment that would require dynamic stack
8828 // realignment which may conflict with optimizations such as tail call
8829 // optimization.
8831 if (!TRI->hasStackRealignment(MF))
8832 if (MaybeAlign StackAlign = DL.getStackAlignment())
8833 NewAlign = std::min(NewAlign, *StackAlign);
8834
8835 if (NewAlign > Alignment) {
8836 // Give the stack frame object a larger alignment if needed.
8837 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8838 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8839 Alignment = NewAlign;
8840 }
8841 }
8842
8843 // Prepare AAInfo for loads/stores after lowering this memmove.
8844 AAMDNodes NewAAInfo = AAInfo;
8845 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8846
8847 MachineMemOperand::Flags MMOFlags =
8849 uint64_t SrcOff = 0, DstOff = 0;
8850 SmallVector<SDValue, 8> LoadValues;
8851 SmallVector<SDValue, 8> LoadChains;
8852 SmallVector<SDValue, 8> OutChains;
8853 unsigned NumMemOps = MemOps.size();
8854 for (unsigned i = 0; i < NumMemOps; i++) {
8855 EVT VT = MemOps[i];
8856 unsigned VTSize = VT.getSizeInBits() / 8;
8857 SDValue Value;
8858
8859 bool isDereferenceable =
8860 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8861 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8862 if (isDereferenceable)
8864
8865 Value = DAG.getLoad(
8866 VT, dl, Chain,
8867 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8868 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8869 LoadValues.push_back(Value);
8870 LoadChains.push_back(Value.getValue(1));
8871 SrcOff += VTSize;
8872 }
8873 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8874 OutChains.clear();
8875 for (unsigned i = 0; i < NumMemOps; i++) {
8876 EVT VT = MemOps[i];
8877 unsigned VTSize = VT.getSizeInBits() / 8;
8878 SDValue Store;
8879
8880 Store = DAG.getStore(
8881 Chain, dl, LoadValues[i],
8882 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8883 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8884 OutChains.push_back(Store);
8885 DstOff += VTSize;
8886 }
8887
8888 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8889}
8890
8891/// Lower the call to 'memset' intrinsic function into a series of store
8892/// operations.
8893///
8894/// \param DAG Selection DAG where lowered code is placed.
8895/// \param dl Link to corresponding IR location.
8896/// \param Chain Control flow dependency.
8897/// \param Dst Pointer to destination memory location.
8898/// \param Src Value of byte to write into the memory.
8899/// \param Size Number of bytes to write.
8900/// \param Alignment Alignment of the destination in bytes.
8901/// \param isVol True if destination is volatile.
8902/// \param AlwaysInline Makes sure no function call is generated.
8903/// \param DstPtrInfo IR information on the memory pointer.
8904/// \returns New head in the control flow, if lowering was successful, empty
8905/// SDValue otherwise.
8906///
8907/// The function tries to replace 'llvm.memset' intrinsic with several store
8908/// operations and value calculation code. This is usually profitable for small
8909/// memory size or when the semantic requires inlining.
8911 SDValue Chain, SDValue Dst, SDValue Src,
8912 uint64_t Size, Align Alignment, bool isVol,
8913 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8914 const AAMDNodes &AAInfo) {
8915 // Turn a memset of undef to nop.
8916 // FIXME: We need to honor volatile even is Src is undef.
8917 if (Src.isUndef())
8918 return Chain;
8919
8920 // Expand memset to a series of load/store ops if the size operand
8921 // falls below a certain threshold.
8922 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8923 std::vector<EVT> MemOps;
8924 bool DstAlignCanChange = false;
8925 LLVMContext &C = *DAG.getContext();
8927 MachineFrameInfo &MFI = MF.getFrameInfo();
8928 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8929 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8930 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8931 DstAlignCanChange = true;
8932 bool IsZeroVal = isNullConstant(Src);
8933 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8934
8935 if (!TLI.findOptimalMemOpLowering(
8936 C, MemOps, Limit,
8937 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8938 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8939 return SDValue();
8940
8941 if (DstAlignCanChange) {
8942 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8943 const DataLayout &DL = DAG.getDataLayout();
8944 Align NewAlign = DL.getABITypeAlign(Ty);
8945
8946 // Don't promote to an alignment that would require dynamic stack
8947 // realignment which may conflict with optimizations such as tail call
8948 // optimization.
8950 if (!TRI->hasStackRealignment(MF))
8951 if (MaybeAlign StackAlign = DL.getStackAlignment())
8952 NewAlign = std::min(NewAlign, *StackAlign);
8953
8954 if (NewAlign > Alignment) {
8955 // Give the stack frame object a larger alignment if needed.
8956 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8957 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8958 Alignment = NewAlign;
8959 }
8960 }
8961
8962 SmallVector<SDValue, 8> OutChains;
8963 uint64_t DstOff = 0;
8964 unsigned NumMemOps = MemOps.size();
8965
8966 // Find the largest store and generate the bit pattern for it.
8967 EVT LargestVT = MemOps[0];
8968 for (unsigned i = 1; i < NumMemOps; i++)
8969 if (MemOps[i].bitsGT(LargestVT))
8970 LargestVT = MemOps[i];
8971 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8972
8973 // Prepare AAInfo for loads/stores after lowering this memset.
8974 AAMDNodes NewAAInfo = AAInfo;
8975 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8976
8977 for (unsigned i = 0; i < NumMemOps; i++) {
8978 EVT VT = MemOps[i];
8979 unsigned VTSize = VT.getSizeInBits() / 8;
8980 if (VTSize > Size) {
8981 // Issuing an unaligned load / store pair that overlaps with the previous
8982 // pair. Adjust the offset accordingly.
8983 assert(i == NumMemOps-1 && i != 0);
8984 DstOff -= VTSize - Size;
8985 }
8986
8987 // If this store is smaller than the largest store see whether we can get
8988 // the smaller value for free with a truncate or extract vector element and
8989 // then store.
8990 SDValue Value = MemSetValue;
8991 if (VT.bitsLT(LargestVT)) {
8992 unsigned Index;
8993 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8994 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8995 if (!LargestVT.isVector() && !VT.isVector() &&
8996 TLI.isTruncateFree(LargestVT, VT))
8997 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8998 else if (LargestVT.isVector() && !VT.isVector() &&
9000 LargestVT.getTypeForEVT(*DAG.getContext()),
9001 VT.getSizeInBits(), Index) &&
9002 TLI.isTypeLegal(SVT) &&
9003 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9004 // Target which can combine store(extractelement VectorTy, Idx) can get
9005 // the smaller value for free.
9006 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9007 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9008 } else
9009 Value = getMemsetValue(Src, VT, DAG, dl);
9010 }
9011 assert(Value.getValueType() == VT && "Value with wrong type.");
9012 SDValue Store = DAG.getStore(
9013 Chain, dl, Value,
9014 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
9015 DstPtrInfo.getWithOffset(DstOff), Alignment,
9017 NewAAInfo);
9018 OutChains.push_back(Store);
9019 DstOff += VT.getSizeInBits() / 8;
9020 Size -= VTSize;
9021 }
9022
9023 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9024}
9025
9027 unsigned AS) {
9028 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9029 // pointer operands can be losslessly bitcasted to pointers of address space 0
9030 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9031 report_fatal_error("cannot lower memory intrinsic in address space " +
9032 Twine(AS));
9033 }
9034}
9035
9036std::pair<SDValue, SDValue>
9038 SDValue Mem1, SDValue Size, const CallInst *CI) {
9039 const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
9040 if (!LibCallName)
9041 return {};
9042
9045 {Mem0, PT},
9046 {Mem1, PT},
9048
9050 bool IsTailCall = false;
9051 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
9052 IsTailCall = CI && CI->isTailCall() &&
9053 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg);
9054
9055 CLI.setDebugLoc(dl)
9056 .setChain(Chain)
9057 .setLibCallee(
9058 TLI->getLibcallCallingConv(RTLIB::MEMCMP),
9060 getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
9061 std::move(Args))
9062 .setTailCall(IsTailCall);
9063
9064 return TLI->LowerCallTo(CLI);
9065}
9066
9068 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9069 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9070 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9071 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9072 BatchAAResults *BatchAA) {
9073 // Check to see if we should lower the memcpy to loads and stores first.
9074 // For cases within the target-specified limits, this is the best choice.
9075 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
9076 if (ConstantSize) {
9077 // Memcpy with size zero? Just return the original chain.
9078 if (ConstantSize->isZero())
9079 return Chain;
9080
9082 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9083 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9084 if (Result.getNode())
9085 return Result;
9086 }
9087
9088 // Then check to see if we should lower the memcpy with target-specific
9089 // code. If the target chooses to do this, this is the next best.
9090 if (TSI) {
9091 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9092 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9093 DstPtrInfo, SrcPtrInfo);
9094 if (Result.getNode())
9095 return Result;
9096 }
9097
9098 // If we really need inline code and the target declined to provide it,
9099 // use a (potentially long) sequence of loads and stores.
9100 if (AlwaysInline) {
9101 assert(ConstantSize && "AlwaysInline requires a constant size!");
9103 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9104 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9105 }
9106
9109
9110 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9111 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9112 // respect volatile, so they may do things like read or write memory
9113 // beyond the given memory regions. But fixing this isn't easy, and most
9114 // people don't care.
9115
9116 // Emit a library call.
9119 Args.emplace_back(Dst, PtrTy);
9120 Args.emplace_back(Src, PtrTy);
9121 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9122 // FIXME: pass in SDLoc
9124 bool IsTailCall = false;
9125 const char *MemCpyName = TLI->getMemcpyName();
9126
9127 if (OverrideTailCall.has_value()) {
9128 IsTailCall = *OverrideTailCall;
9129 } else {
9130 bool LowersToMemcpy = StringRef(MemCpyName) == StringRef("memcpy");
9131 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
9132 IsTailCall = CI && CI->isTailCall() &&
9134 ReturnsFirstArg && LowersToMemcpy);
9135 }
9136
9137 CLI.setDebugLoc(dl)
9138 .setChain(Chain)
9139 .setLibCallee(
9140 TLI->getLibcallCallingConv(RTLIB::MEMCPY),
9141 Dst.getValueType().getTypeForEVT(*getContext()),
9142 getExternalSymbol(MemCpyName, TLI->getPointerTy(getDataLayout())),
9143 std::move(Args))
9145 .setTailCall(IsTailCall);
9146
9147 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9148 return CallResult.second;
9149}
9150
9152 SDValue Dst, SDValue Src, SDValue Size,
9153 Type *SizeTy, unsigned ElemSz,
9154 bool isTailCall,
9155 MachinePointerInfo DstPtrInfo,
9156 MachinePointerInfo SrcPtrInfo) {
9157 // Emit a library call.
9160 Args.emplace_back(Dst, ArgTy);
9161 Args.emplace_back(Src, ArgTy);
9162 Args.emplace_back(Size, SizeTy);
9163
9164 RTLIB::Libcall LibraryCall =
9166 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9167 report_fatal_error("Unsupported element size");
9168
9170 CLI.setDebugLoc(dl)
9171 .setChain(Chain)
9172 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9174 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9175 TLI->getPointerTy(getDataLayout())),
9176 std::move(Args))
9178 .setTailCall(isTailCall);
9179
9180 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9181 return CallResult.second;
9182}
9183
9185 SDValue Src, SDValue Size, Align Alignment,
9186 bool isVol, const CallInst *CI,
9187 std::optional<bool> OverrideTailCall,
9188 MachinePointerInfo DstPtrInfo,
9189 MachinePointerInfo SrcPtrInfo,
9190 const AAMDNodes &AAInfo,
9191 BatchAAResults *BatchAA) {
9192 // Check to see if we should lower the memmove to loads and stores first.
9193 // For cases within the target-specified limits, this is the best choice.
9194 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
9195 if (ConstantSize) {
9196 // Memmove with size zero? Just return the original chain.
9197 if (ConstantSize->isZero())
9198 return Chain;
9199
9201 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9202 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9203 if (Result.getNode())
9204 return Result;
9205 }
9206
9207 // Then check to see if we should lower the memmove with target-specific
9208 // code. If the target chooses to do this, this is the next best.
9209 if (TSI) {
9210 SDValue Result =
9211 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9212 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9213 if (Result.getNode())
9214 return Result;
9215 }
9216
9219
9220 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9221 // not be safe. See memcpy above for more details.
9222
9223 // Emit a library call.
9226 Args.emplace_back(Dst, PtrTy);
9227 Args.emplace_back(Src, PtrTy);
9228 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9229 // FIXME: pass in SDLoc
9231
9232 bool IsTailCall = false;
9233 if (OverrideTailCall.has_value()) {
9234 IsTailCall = *OverrideTailCall;
9235 } else {
9236 bool LowersToMemmove =
9237 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
9238 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
9239 IsTailCall = CI && CI->isTailCall() &&
9241 ReturnsFirstArg && LowersToMemmove);
9242 }
9243
9244 CLI.setDebugLoc(dl)
9245 .setChain(Chain)
9246 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
9247 Dst.getValueType().getTypeForEVT(*getContext()),
9248 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
9249 TLI->getPointerTy(getDataLayout())),
9250 std::move(Args))
9252 .setTailCall(IsTailCall);
9253
9254 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9255 return CallResult.second;
9256}
9257
9259 SDValue Dst, SDValue Src, SDValue Size,
9260 Type *SizeTy, unsigned ElemSz,
9261 bool isTailCall,
9262 MachinePointerInfo DstPtrInfo,
9263 MachinePointerInfo SrcPtrInfo) {
9264 // Emit a library call.
9266 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9267 Args.emplace_back(Dst, IntPtrTy);
9268 Args.emplace_back(Src, IntPtrTy);
9269 Args.emplace_back(Size, SizeTy);
9270
9271 RTLIB::Libcall LibraryCall =
9273 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9274 report_fatal_error("Unsupported element size");
9275
9277 CLI.setDebugLoc(dl)
9278 .setChain(Chain)
9279 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9281 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9282 TLI->getPointerTy(getDataLayout())),
9283 std::move(Args))
9285 .setTailCall(isTailCall);
9286
9287 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9288 return CallResult.second;
9289}
9290
9292 SDValue Src, SDValue Size, Align Alignment,
9293 bool isVol, bool AlwaysInline,
9294 const CallInst *CI,
9295 MachinePointerInfo DstPtrInfo,
9296 const AAMDNodes &AAInfo) {
9297 // Check to see if we should lower the memset to stores first.
9298 // For cases within the target-specified limits, this is the best choice.
9299 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
9300 if (ConstantSize) {
9301 // Memset with size zero? Just return the original chain.
9302 if (ConstantSize->isZero())
9303 return Chain;
9304
9305 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9306 ConstantSize->getZExtValue(), Alignment,
9307 isVol, false, DstPtrInfo, AAInfo);
9308
9309 if (Result.getNode())
9310 return Result;
9311 }
9312
9313 // Then check to see if we should lower the memset with target-specific
9314 // code. If the target chooses to do this, this is the next best.
9315 if (TSI) {
9316 SDValue Result = TSI->EmitTargetCodeForMemset(
9317 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9318 if (Result.getNode())
9319 return Result;
9320 }
9321
9322 // If we really need inline code and the target declined to provide it,
9323 // use a (potentially long) sequence of loads and stores.
9324 if (AlwaysInline) {
9325 assert(ConstantSize && "AlwaysInline requires a constant size!");
9326 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9327 ConstantSize->getZExtValue(), Alignment,
9328 isVol, true, DstPtrInfo, AAInfo);
9329 assert(Result &&
9330 "getMemsetStores must return a valid sequence when AlwaysInline");
9331 return Result;
9332 }
9333
9335
9336 // Emit a library call.
9337 auto &Ctx = *getContext();
9338 const auto& DL = getDataLayout();
9339
9341 // FIXME: pass in SDLoc
9342 CLI.setDebugLoc(dl).setChain(Chain);
9343
9344 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
9345
9346 bool UseBZero = isNullConstant(Src) && BzeroName;
9347 // If zeroing out and bzero is present, use it.
9348 if (UseBZero) {
9350 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9351 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9352 CLI.setLibCallee(
9353 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
9354 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
9355 } else {
9357 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9358 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9359 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9360 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
9361 Dst.getValueType().getTypeForEVT(Ctx),
9362 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
9363 TLI->getPointerTy(DL)),
9364 std::move(Args));
9365 }
9366 bool LowersToMemset =
9367 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
9368 // If we're going to use bzero, make sure not to tail call unless the
9369 // subsequent return doesn't need a value, as bzero doesn't return the first
9370 // arg unlike memset.
9371 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9372 bool IsTailCall =
9373 CI && CI->isTailCall() &&
9374 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9375 CLI.setDiscardResult().setTailCall(IsTailCall);
9376
9377 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9378 return CallResult.second;
9379}
9380
9383 Type *SizeTy, unsigned ElemSz,
9384 bool isTailCall,
9385 MachinePointerInfo DstPtrInfo) {
9386 // Emit a library call.
9388 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9389 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9390 Args.emplace_back(Size, SizeTy);
9391
9392 RTLIB::Libcall LibraryCall =
9394 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9395 report_fatal_error("Unsupported element size");
9396
9398 CLI.setDebugLoc(dl)
9399 .setChain(Chain)
9400 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9402 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9403 TLI->getPointerTy(getDataLayout())),
9404 std::move(Args))
9406 .setTailCall(isTailCall);
9407
9408 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9409 return CallResult.second;
9410}
9411
9412SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9413 SDVTList VTList, ArrayRef<SDValue> Ops,
9414 MachineMemOperand *MMO,
9415 ISD::LoadExtType ExtType) {
9417 AddNodeIDNode(ID, Opcode, VTList, Ops);
9418 ID.AddInteger(MemVT.getRawBits());
9419 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9420 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9421 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9422 ID.AddInteger(MMO->getFlags());
9423 void* IP = nullptr;
9424 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9425 E->refineAlignment(MMO);
9426 E->refineRanges(MMO);
9427 return SDValue(E, 0);
9428 }
9429
9430 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9431 VTList, MemVT, MMO, ExtType);
9432 createOperands(N, Ops);
9433
9434 CSEMap.InsertNode(N, IP);
9435 InsertNode(N);
9436 SDValue V(N, 0);
9437 NewSDValueDbgMsg(V, "Creating new node: ", this);
9438 return V;
9439}
9440
9442 EVT MemVT, SDVTList VTs, SDValue Chain,
9443 SDValue Ptr, SDValue Cmp, SDValue Swp,
9444 MachineMemOperand *MMO) {
9445 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9447 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9448
9449 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9450 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9451}
9452
9453SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9454 SDValue Chain, SDValue Ptr, SDValue Val,
9455 MachineMemOperand *MMO) {
9456 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9457 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9458 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9459 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9460 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9461 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9462 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9463 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9464 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9465 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9466 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9467 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9468 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9469 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9470 Opcode == ISD::ATOMIC_STORE) &&
9471 "Invalid Atomic Op");
9472
9473 EVT VT = Val.getValueType();
9474
9475 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9476 getVTList(VT, MVT::Other);
9477 SDValue Ops[] = {Chain, Ptr, Val};
9478 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9479}
9480
9482 EVT MemVT, EVT VT, SDValue Chain,
9484 SDVTList VTs = getVTList(VT, MVT::Other);
9485 SDValue Ops[] = {Chain, Ptr};
9486 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9487}
9488
9489/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9491 if (Ops.size() == 1)
9492 return Ops[0];
9493
9495 VTs.reserve(Ops.size());
9496 for (const SDValue &Op : Ops)
9497 VTs.push_back(Op.getValueType());
9498 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9499}
9500
9502 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9503 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9505 const AAMDNodes &AAInfo) {
9506 if (Size.hasValue() && !Size.getValue())
9508
9510 MachineMemOperand *MMO =
9511 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9512
9513 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9514}
9515
9517 SDVTList VTList,
9518 ArrayRef<SDValue> Ops, EVT MemVT,
9519 MachineMemOperand *MMO) {
9520 assert(
9521 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9522 Opcode == ISD::PREFETCH ||
9523 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9524 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9525 "Opcode is not a memory-accessing opcode!");
9526
9527 // Memoize the node unless it returns a glue result.
9529 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9531 AddNodeIDNode(ID, Opcode, VTList, Ops);
9532 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9533 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9534 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9535 ID.AddInteger(MMO->getFlags());
9536 ID.AddInteger(MemVT.getRawBits());
9537 void *IP = nullptr;
9538 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9539 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9540 return SDValue(E, 0);
9541 }
9542
9543 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9544 VTList, MemVT, MMO);
9545 createOperands(N, Ops);
9546
9547 CSEMap.InsertNode(N, IP);
9548 } else {
9549 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9550 VTList, MemVT, MMO);
9551 createOperands(N, Ops);
9552 }
9553 InsertNode(N);
9554 SDValue V(N, 0);
9555 NewSDValueDbgMsg(V, "Creating new node: ", this);
9556 return V;
9557}
9558
9560 SDValue Chain, int FrameIndex) {
9561 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9562 const auto VTs = getVTList(MVT::Other);
9563 SDValue Ops[2] = {
9564 Chain,
9565 getFrameIndex(FrameIndex,
9566 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9567 true)};
9568
9570 AddNodeIDNode(ID, Opcode, VTs, Ops);
9571 ID.AddInteger(FrameIndex);
9572 void *IP = nullptr;
9573 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9574 return SDValue(E, 0);
9575
9576 LifetimeSDNode *N =
9577 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9578 createOperands(N, Ops);
9579 CSEMap.InsertNode(N, IP);
9580 InsertNode(N);
9581 SDValue V(N, 0);
9582 NewSDValueDbgMsg(V, "Creating new node: ", this);
9583 return V;
9584}
9585
9587 uint64_t Guid, uint64_t Index,
9588 uint32_t Attr) {
9589 const unsigned Opcode = ISD::PSEUDO_PROBE;
9590 const auto VTs = getVTList(MVT::Other);
9591 SDValue Ops[] = {Chain};
9593 AddNodeIDNode(ID, Opcode, VTs, Ops);
9594 ID.AddInteger(Guid);
9595 ID.AddInteger(Index);
9596 void *IP = nullptr;
9597 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9598 return SDValue(E, 0);
9599
9600 auto *N = newSDNode<PseudoProbeSDNode>(
9601 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9602 createOperands(N, Ops);
9603 CSEMap.InsertNode(N, IP);
9604 InsertNode(N);
9605 SDValue V(N, 0);
9606 NewSDValueDbgMsg(V, "Creating new node: ", this);
9607 return V;
9608}
9609
9610/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9611/// MachinePointerInfo record from it. This is particularly useful because the
9612/// code generator has many cases where it doesn't bother passing in a
9613/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9615 SelectionDAG &DAG, SDValue Ptr,
9616 int64_t Offset = 0) {
9617 // If this is FI+Offset, we can model it.
9618 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9620 FI->getIndex(), Offset);
9621
9622 // If this is (FI+Offset1)+Offset2, we can model it.
9623 if (Ptr.getOpcode() != ISD::ADD ||
9624 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
9625 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
9626 return Info;
9627
9628 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9630 DAG.getMachineFunction(), FI,
9631 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9632}
9633
9634/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9635/// MachinePointerInfo record from it. This is particularly useful because the
9636/// code generator has many cases where it doesn't bother passing in a
9637/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9639 SelectionDAG &DAG, SDValue Ptr,
9640 SDValue OffsetOp) {
9641 // If the 'Offset' value isn't a constant, we can't handle this.
9642 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
9643 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9644 if (OffsetOp.isUndef())
9645 return InferPointerInfo(Info, DAG, Ptr);
9646 return Info;
9647}
9648
9650 EVT VT, const SDLoc &dl, SDValue Chain,
9652 MachinePointerInfo PtrInfo, EVT MemVT,
9653 Align Alignment,
9654 MachineMemOperand::Flags MMOFlags,
9655 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9656 assert(Chain.getValueType() == MVT::Other &&
9657 "Invalid chain type");
9658
9659 MMOFlags |= MachineMemOperand::MOLoad;
9660 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9661 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9662 // clients.
9663 if (PtrInfo.V.isNull())
9664 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9665
9666 TypeSize Size = MemVT.getStoreSize();
9668 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9669 Alignment, AAInfo, Ranges);
9670 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9671}
9672
9674 EVT VT, const SDLoc &dl, SDValue Chain,
9675 SDValue Ptr, SDValue Offset, EVT MemVT,
9676 MachineMemOperand *MMO) {
9677 if (VT == MemVT) {
9678 ExtType = ISD::NON_EXTLOAD;
9679 } else if (ExtType == ISD::NON_EXTLOAD) {
9680 assert(VT == MemVT && "Non-extending load from different memory type!");
9681 } else {
9682 // Extending load.
9683 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9684 "Should only be an extending load, not truncating!");
9685 assert(VT.isInteger() == MemVT.isInteger() &&
9686 "Cannot convert from FP to Int or Int -> FP!");
9687 assert(VT.isVector() == MemVT.isVector() &&
9688 "Cannot use an ext load to convert to or from a vector!");
9689 assert((!VT.isVector() ||
9691 "Cannot use an ext load to change the number of vector elements!");
9692 }
9693
9694 assert((!MMO->getRanges() ||
9695 (mdconst::extract<ConstantInt>(MMO->getRanges()->getOperand(0))
9696 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9697 MemVT.isInteger())) &&
9698 "Range metadata and load type must match!");
9699
9700 bool Indexed = AM != ISD::UNINDEXED;
9701 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9702
9703 SDVTList VTs = Indexed ?
9704 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9705 SDValue Ops[] = { Chain, Ptr, Offset };
9707 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9708 ID.AddInteger(MemVT.getRawBits());
9709 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9710 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9711 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9712 ID.AddInteger(MMO->getFlags());
9713 void *IP = nullptr;
9714 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9715 E->refineAlignment(MMO);
9716 E->refineRanges(MMO);
9717 return SDValue(E, 0);
9718 }
9719 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9720 ExtType, MemVT, MMO);
9721 createOperands(N, Ops);
9722
9723 CSEMap.InsertNode(N, IP);
9724 InsertNode(N);
9725 SDValue V(N, 0);
9726 NewSDValueDbgMsg(V, "Creating new node: ", this);
9727 return V;
9728}
9729
9732 MaybeAlign Alignment,
9733 MachineMemOperand::Flags MMOFlags,
9734 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9735 SDValue Undef = getUNDEF(Ptr.getValueType());
9736 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9737 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9738}
9739
9742 SDValue Undef = getUNDEF(Ptr.getValueType());
9743 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9744 VT, MMO);
9745}
9746
9748 EVT VT, SDValue Chain, SDValue Ptr,
9749 MachinePointerInfo PtrInfo, EVT MemVT,
9750 MaybeAlign Alignment,
9751 MachineMemOperand::Flags MMOFlags,
9752 const AAMDNodes &AAInfo) {
9753 SDValue Undef = getUNDEF(Ptr.getValueType());
9754 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9755 MemVT, Alignment, MMOFlags, AAInfo);
9756}
9757
9759 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9760 MachineMemOperand *MMO) {
9761 SDValue Undef = getUNDEF(Ptr.getValueType());
9762 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9763 MemVT, MMO);
9764}
9765
9769 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9770 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9771 // Don't propagate the invariant or dereferenceable flags.
9772 auto MMOFlags =
9773 LD->getMemOperand()->getFlags() &
9775 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9776 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9777 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9778}
9779
9782 Align Alignment,
9783 MachineMemOperand::Flags MMOFlags,
9784 const AAMDNodes &AAInfo) {
9785 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9786
9787 MMOFlags |= MachineMemOperand::MOStore;
9788 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9789
9790 if (PtrInfo.V.isNull())
9791 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9792
9795 MachineMemOperand *MMO =
9796 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9797 return getStore(Chain, dl, Val, Ptr, MMO);
9798}
9799
9802 SDValue Undef = getUNDEF(Ptr.getValueType());
9803 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9805}
9806
9810 bool IsTruncating) {
9811 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9812 EVT VT = Val.getValueType();
9813 if (VT == SVT) {
9814 IsTruncating = false;
9815 } else if (!IsTruncating) {
9816 assert(VT == SVT && "No-truncating store from different memory type!");
9817 } else {
9819 "Should only be a truncating store, not extending!");
9820 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9821 assert(VT.isVector() == SVT.isVector() &&
9822 "Cannot use trunc store to convert to or from a vector!");
9823 assert((!VT.isVector() ||
9825 "Cannot use trunc store to change the number of vector elements!");
9826 }
9827
9828 bool Indexed = AM != ISD::UNINDEXED;
9829 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9830 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9831 : getVTList(MVT::Other);
9832 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9834 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9835 ID.AddInteger(SVT.getRawBits());
9836 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9837 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9838 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9839 ID.AddInteger(MMO->getFlags());
9840 void *IP = nullptr;
9841 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9842 cast<StoreSDNode>(E)->refineAlignment(MMO);
9843 return SDValue(E, 0);
9844 }
9845 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9846 IsTruncating, SVT, MMO);
9847 createOperands(N, Ops);
9848
9849 CSEMap.InsertNode(N, IP);
9850 InsertNode(N);
9851 SDValue V(N, 0);
9852 NewSDValueDbgMsg(V, "Creating new node: ", this);
9853 return V;
9854}
9855
9858 EVT SVT, Align Alignment,
9859 MachineMemOperand::Flags MMOFlags,
9860 const AAMDNodes &AAInfo) {
9861 assert(Chain.getValueType() == MVT::Other &&
9862 "Invalid chain type");
9863
9864 MMOFlags |= MachineMemOperand::MOStore;
9865 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9866
9867 if (PtrInfo.V.isNull())
9868 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9869
9872 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9873 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9874}
9875
9877 SDValue Ptr, EVT SVT,
9878 MachineMemOperand *MMO) {
9879 SDValue Undef = getUNDEF(Ptr.getValueType());
9880 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9881}
9882
9886 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9887 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9888 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9889 ST->getMemoryVT(), ST->getMemOperand(), AM,
9890 ST->isTruncatingStore());
9891}
9892
9894 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9895 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9896 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9897 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9898 const MDNode *Ranges, bool IsExpanding) {
9899 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9900
9901 MMOFlags |= MachineMemOperand::MOLoad;
9902 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9903 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9904 // clients.
9905 if (PtrInfo.V.isNull())
9906 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9907
9908 TypeSize Size = MemVT.getStoreSize();
9910 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9911 Alignment, AAInfo, Ranges);
9912 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9913 MMO, IsExpanding);
9914}
9915
9917 ISD::LoadExtType ExtType, EVT VT,
9918 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9919 SDValue Offset, SDValue Mask, SDValue EVL,
9920 EVT MemVT, MachineMemOperand *MMO,
9921 bool IsExpanding) {
9922 bool Indexed = AM != ISD::UNINDEXED;
9923 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9924
9925 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9926 : getVTList(VT, MVT::Other);
9927 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9929 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9930 ID.AddInteger(MemVT.getRawBits());
9931 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9932 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9933 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9934 ID.AddInteger(MMO->getFlags());
9935 void *IP = nullptr;
9936 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9937 E->refineAlignment(MMO);
9938 E->refineRanges(MMO);
9939 return SDValue(E, 0);
9940 }
9941 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9942 ExtType, IsExpanding, MemVT, MMO);
9943 createOperands(N, Ops);
9944
9945 CSEMap.InsertNode(N, IP);
9946 InsertNode(N);
9947 SDValue V(N, 0);
9948 NewSDValueDbgMsg(V, "Creating new node: ", this);
9949 return V;
9950}
9951
9953 SDValue Ptr, SDValue Mask, SDValue EVL,
9954 MachinePointerInfo PtrInfo,
9955 MaybeAlign Alignment,
9956 MachineMemOperand::Flags MMOFlags,
9957 const AAMDNodes &AAInfo, const MDNode *Ranges,
9958 bool IsExpanding) {
9959 SDValue Undef = getUNDEF(Ptr.getValueType());
9960 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9961 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9962 IsExpanding);
9963}
9964
9966 SDValue Ptr, SDValue Mask, SDValue EVL,
9967 MachineMemOperand *MMO, bool IsExpanding) {
9968 SDValue Undef = getUNDEF(Ptr.getValueType());
9969 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9970 Mask, EVL, VT, MMO, IsExpanding);
9971}
9972
9974 EVT VT, SDValue Chain, SDValue Ptr,
9975 SDValue Mask, SDValue EVL,
9976 MachinePointerInfo PtrInfo, EVT MemVT,
9977 MaybeAlign Alignment,
9978 MachineMemOperand::Flags MMOFlags,
9979 const AAMDNodes &AAInfo, bool IsExpanding) {
9980 SDValue Undef = getUNDEF(Ptr.getValueType());
9981 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9982 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9983 IsExpanding);
9984}
9985
9987 EVT VT, SDValue Chain, SDValue Ptr,
9988 SDValue Mask, SDValue EVL, EVT MemVT,
9989 MachineMemOperand *MMO, bool IsExpanding) {
9990 SDValue Undef = getUNDEF(Ptr.getValueType());
9991 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9992 EVL, MemVT, MMO, IsExpanding);
9993}
9994
9998 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9999 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10000 // Don't propagate the invariant or dereferenceable flags.
10001 auto MMOFlags =
10002 LD->getMemOperand()->getFlags() &
10004 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10005 LD->getChain(), Base, Offset, LD->getMask(),
10006 LD->getVectorLength(), LD->getPointerInfo(),
10007 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10008 nullptr, LD->isExpandingLoad());
10009}
10010
10013 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10014 ISD::MemIndexedMode AM, bool IsTruncating,
10015 bool IsCompressing) {
10016 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10017 bool Indexed = AM != ISD::UNINDEXED;
10018 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10019 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10020 : getVTList(MVT::Other);
10021 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10023 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10024 ID.AddInteger(MemVT.getRawBits());
10025 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10026 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10027 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10028 ID.AddInteger(MMO->getFlags());
10029 void *IP = nullptr;
10030 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10031 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10032 return SDValue(E, 0);
10033 }
10034 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10035 IsTruncating, IsCompressing, MemVT, MMO);
10036 createOperands(N, Ops);
10037
10038 CSEMap.InsertNode(N, IP);
10039 InsertNode(N);
10040 SDValue V(N, 0);
10041 NewSDValueDbgMsg(V, "Creating new node: ", this);
10042 return V;
10043}
10044
10046 SDValue Val, SDValue Ptr, SDValue Mask,
10047 SDValue EVL, MachinePointerInfo PtrInfo,
10048 EVT SVT, Align Alignment,
10049 MachineMemOperand::Flags MMOFlags,
10050 const AAMDNodes &AAInfo,
10051 bool IsCompressing) {
10052 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10053
10054 MMOFlags |= MachineMemOperand::MOStore;
10055 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10056
10057 if (PtrInfo.V.isNull())
10058 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10059
10062 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10063 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10064 IsCompressing);
10065}
10066
10068 SDValue Val, SDValue Ptr, SDValue Mask,
10069 SDValue EVL, EVT SVT,
10070 MachineMemOperand *MMO,
10071 bool IsCompressing) {
10072 EVT VT = Val.getValueType();
10073
10074 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10075 if (VT == SVT)
10076 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10077 EVL, VT, MMO, ISD::UNINDEXED,
10078 /*IsTruncating*/ false, IsCompressing);
10079
10081 "Should only be a truncating store, not extending!");
10082 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10083 assert(VT.isVector() == SVT.isVector() &&
10084 "Cannot use trunc store to convert to or from a vector!");
10085 assert((!VT.isVector() ||
10087 "Cannot use trunc store to change the number of vector elements!");
10088
10089 SDVTList VTs = getVTList(MVT::Other);
10090 SDValue Undef = getUNDEF(Ptr.getValueType());
10091 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10093 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10094 ID.AddInteger(SVT.getRawBits());
10095 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10096 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10097 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10098 ID.AddInteger(MMO->getFlags());
10099 void *IP = nullptr;
10100 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10101 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10102 return SDValue(E, 0);
10103 }
10104 auto *N =
10105 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10106 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10107 createOperands(N, Ops);
10108
10109 CSEMap.InsertNode(N, IP);
10110 InsertNode(N);
10111 SDValue V(N, 0);
10112 NewSDValueDbgMsg(V, "Creating new node: ", this);
10113 return V;
10114}
10115
10119 auto *ST = cast<VPStoreSDNode>(OrigStore);
10120 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10121 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10122 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10123 Offset, ST->getMask(), ST->getVectorLength()};
10125 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10126 ID.AddInteger(ST->getMemoryVT().getRawBits());
10127 ID.AddInteger(ST->getRawSubclassData());
10128 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10129 ID.AddInteger(ST->getMemOperand()->getFlags());
10130 void *IP = nullptr;
10131 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10132 return SDValue(E, 0);
10133
10134 auto *N = newSDNode<VPStoreSDNode>(
10135 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10136 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10137 createOperands(N, Ops);
10138
10139 CSEMap.InsertNode(N, IP);
10140 InsertNode(N);
10141 SDValue V(N, 0);
10142 NewSDValueDbgMsg(V, "Creating new node: ", this);
10143 return V;
10144}
10145
10147 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10148 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10149 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10150 bool Indexed = AM != ISD::UNINDEXED;
10151 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10152
10153 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10154 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10155 : getVTList(VT, MVT::Other);
10157 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10158 ID.AddInteger(VT.getRawBits());
10159 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10160 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10161 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10162
10163 void *IP = nullptr;
10164 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10165 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10166 return SDValue(E, 0);
10167 }
10168
10169 auto *N =
10170 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10171 ExtType, IsExpanding, MemVT, MMO);
10172 createOperands(N, Ops);
10173 CSEMap.InsertNode(N, IP);
10174 InsertNode(N);
10175 SDValue V(N, 0);
10176 NewSDValueDbgMsg(V, "Creating new node: ", this);
10177 return V;
10178}
10179
10181 SDValue Ptr, SDValue Stride,
10182 SDValue Mask, SDValue EVL,
10183 MachineMemOperand *MMO,
10184 bool IsExpanding) {
10185 SDValue Undef = getUNDEF(Ptr.getValueType());
10187 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10188}
10189
10191 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10192 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10193 MachineMemOperand *MMO, bool IsExpanding) {
10194 SDValue Undef = getUNDEF(Ptr.getValueType());
10195 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10196 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10197}
10198
10200 SDValue Val, SDValue Ptr,
10201 SDValue Offset, SDValue Stride,
10202 SDValue Mask, SDValue EVL, EVT MemVT,
10203 MachineMemOperand *MMO,
10205 bool IsTruncating, bool IsCompressing) {
10206 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10207 bool Indexed = AM != ISD::UNINDEXED;
10208 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10209 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10210 : getVTList(MVT::Other);
10211 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10213 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10214 ID.AddInteger(MemVT.getRawBits());
10215 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10216 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10217 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10218 void *IP = nullptr;
10219 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10220 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10221 return SDValue(E, 0);
10222 }
10223 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10224 VTs, AM, IsTruncating,
10225 IsCompressing, MemVT, MMO);
10226 createOperands(N, Ops);
10227
10228 CSEMap.InsertNode(N, IP);
10229 InsertNode(N);
10230 SDValue V(N, 0);
10231 NewSDValueDbgMsg(V, "Creating new node: ", this);
10232 return V;
10233}
10234
10236 SDValue Val, SDValue Ptr,
10237 SDValue Stride, SDValue Mask,
10238 SDValue EVL, EVT SVT,
10239 MachineMemOperand *MMO,
10240 bool IsCompressing) {
10241 EVT VT = Val.getValueType();
10242
10243 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10244 if (VT == SVT)
10245 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10246 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10247 /*IsTruncating*/ false, IsCompressing);
10248
10250 "Should only be a truncating store, not extending!");
10251 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10252 assert(VT.isVector() == SVT.isVector() &&
10253 "Cannot use trunc store to convert to or from a vector!");
10254 assert((!VT.isVector() ||
10256 "Cannot use trunc store to change the number of vector elements!");
10257
10258 SDVTList VTs = getVTList(MVT::Other);
10259 SDValue Undef = getUNDEF(Ptr.getValueType());
10260 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10262 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10263 ID.AddInteger(SVT.getRawBits());
10264 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10265 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10266 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10267 void *IP = nullptr;
10268 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10269 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10270 return SDValue(E, 0);
10271 }
10272 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10273 VTs, ISD::UNINDEXED, true,
10274 IsCompressing, SVT, MMO);
10275 createOperands(N, Ops);
10276
10277 CSEMap.InsertNode(N, IP);
10278 InsertNode(N);
10279 SDValue V(N, 0);
10280 NewSDValueDbgMsg(V, "Creating new node: ", this);
10281 return V;
10282}
10283
10286 ISD::MemIndexType IndexType) {
10287 assert(Ops.size() == 6 && "Incompatible number of operands");
10288
10290 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10291 ID.AddInteger(VT.getRawBits());
10292 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10293 dl.getIROrder(), VTs, VT, MMO, IndexType));
10294 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10295 ID.AddInteger(MMO->getFlags());
10296 void *IP = nullptr;
10297 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10298 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10299 return SDValue(E, 0);
10300 }
10301
10302 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10303 VT, MMO, IndexType);
10304 createOperands(N, Ops);
10305
10306 assert(N->getMask().getValueType().getVectorElementCount() ==
10307 N->getValueType(0).getVectorElementCount() &&
10308 "Vector width mismatch between mask and data");
10309 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10310 N->getValueType(0).getVectorElementCount().isScalable() &&
10311 "Scalable flags of index and data do not match");
10313 N->getIndex().getValueType().getVectorElementCount(),
10314 N->getValueType(0).getVectorElementCount()) &&
10315 "Vector width mismatch between index and data");
10316 assert(isa<ConstantSDNode>(N->getScale()) &&
10317 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10318 "Scale should be a constant power of 2");
10319
10320 CSEMap.InsertNode(N, IP);
10321 InsertNode(N);
10322 SDValue V(N, 0);
10323 NewSDValueDbgMsg(V, "Creating new node: ", this);
10324 return V;
10325}
10326
10329 MachineMemOperand *MMO,
10330 ISD::MemIndexType IndexType) {
10331 assert(Ops.size() == 7 && "Incompatible number of operands");
10332
10334 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10335 ID.AddInteger(VT.getRawBits());
10336 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10337 dl.getIROrder(), VTs, VT, MMO, IndexType));
10338 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10339 ID.AddInteger(MMO->getFlags());
10340 void *IP = nullptr;
10341 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10342 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10343 return SDValue(E, 0);
10344 }
10345 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10346 VT, MMO, IndexType);
10347 createOperands(N, Ops);
10348
10349 assert(N->getMask().getValueType().getVectorElementCount() ==
10350 N->getValue().getValueType().getVectorElementCount() &&
10351 "Vector width mismatch between mask and data");
10352 assert(
10353 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10354 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10355 "Scalable flags of index and data do not match");
10357 N->getIndex().getValueType().getVectorElementCount(),
10358 N->getValue().getValueType().getVectorElementCount()) &&
10359 "Vector width mismatch between index and data");
10360 assert(isa<ConstantSDNode>(N->getScale()) &&
10361 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10362 "Scale should be a constant power of 2");
10363
10364 CSEMap.InsertNode(N, IP);
10365 InsertNode(N);
10366 SDValue V(N, 0);
10367 NewSDValueDbgMsg(V, "Creating new node: ", this);
10368 return V;
10369}
10370
10373 SDValue PassThru, EVT MemVT,
10374 MachineMemOperand *MMO,
10376 ISD::LoadExtType ExtTy, bool isExpanding) {
10377 bool Indexed = AM != ISD::UNINDEXED;
10378 assert((Indexed || Offset.isUndef()) &&
10379 "Unindexed masked load with an offset!");
10380 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10381 : getVTList(VT, MVT::Other);
10382 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10384 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10385 ID.AddInteger(MemVT.getRawBits());
10386 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10387 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
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<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10393 return SDValue(E, 0);
10394 }
10395 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10396 AM, ExtTy, isExpanding, MemVT, MMO);
10397 createOperands(N, Ops);
10398
10399 CSEMap.InsertNode(N, IP);
10400 InsertNode(N);
10401 SDValue V(N, 0);
10402 NewSDValueDbgMsg(V, "Creating new node: ", this);
10403 return V;
10404}
10405
10409 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
10410 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10411 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10412 Offset, LD->getMask(), LD->getPassThru(),
10413 LD->getMemoryVT(), LD->getMemOperand(), AM,
10414 LD->getExtensionType(), LD->isExpandingLoad());
10415}
10416
10419 SDValue Mask, EVT MemVT,
10420 MachineMemOperand *MMO,
10421 ISD::MemIndexedMode AM, bool IsTruncating,
10422 bool IsCompressing) {
10423 assert(Chain.getValueType() == MVT::Other &&
10424 "Invalid chain type");
10425 bool Indexed = AM != ISD::UNINDEXED;
10426 assert((Indexed || Offset.isUndef()) &&
10427 "Unindexed masked store with an offset!");
10428 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10429 : getVTList(MVT::Other);
10430 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10432 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10433 ID.AddInteger(MemVT.getRawBits());
10434 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10435 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10436 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10437 ID.AddInteger(MMO->getFlags());
10438 void *IP = nullptr;
10439 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10440 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10441 return SDValue(E, 0);
10442 }
10443 auto *N =
10444 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10445 IsTruncating, IsCompressing, MemVT, MMO);
10446 createOperands(N, Ops);
10447
10448 CSEMap.InsertNode(N, IP);
10449 InsertNode(N);
10450 SDValue V(N, 0);
10451 NewSDValueDbgMsg(V, "Creating new node: ", this);
10452 return V;
10453}
10454
10458 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
10459 assert(ST->getOffset().isUndef() &&
10460 "Masked store is already a indexed store!");
10461 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10462 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10463 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10464}
10465
10468 MachineMemOperand *MMO,
10469 ISD::MemIndexType IndexType,
10470 ISD::LoadExtType ExtTy) {
10471 assert(Ops.size() == 6 && "Incompatible number of operands");
10472
10474 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10475 ID.AddInteger(MemVT.getRawBits());
10476 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10477 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10478 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10479 ID.AddInteger(MMO->getFlags());
10480 void *IP = nullptr;
10481 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10482 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10483 return SDValue(E, 0);
10484 }
10485
10486 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10487 VTs, MemVT, MMO, IndexType, ExtTy);
10488 createOperands(N, Ops);
10489
10490 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10491 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10492 assert(N->getMask().getValueType().getVectorElementCount() ==
10493 N->getValueType(0).getVectorElementCount() &&
10494 "Vector width mismatch between mask and data");
10495 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10496 N->getValueType(0).getVectorElementCount().isScalable() &&
10497 "Scalable flags of index and data do not match");
10499 N->getIndex().getValueType().getVectorElementCount(),
10500 N->getValueType(0).getVectorElementCount()) &&
10501 "Vector width mismatch between index and data");
10502 assert(isa<ConstantSDNode>(N->getScale()) &&
10503 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10504 "Scale should be a constant power of 2");
10505
10506 CSEMap.InsertNode(N, IP);
10507 InsertNode(N);
10508 SDValue V(N, 0);
10509 NewSDValueDbgMsg(V, "Creating new node: ", this);
10510 return V;
10511}
10512
10515 MachineMemOperand *MMO,
10516 ISD::MemIndexType IndexType,
10517 bool IsTrunc) {
10518 assert(Ops.size() == 6 && "Incompatible number of operands");
10519
10521 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10522 ID.AddInteger(MemVT.getRawBits());
10523 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10524 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10525 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10526 ID.AddInteger(MMO->getFlags());
10527 void *IP = nullptr;
10528 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10529 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10530 return SDValue(E, 0);
10531 }
10532
10533 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10534 VTs, MemVT, MMO, IndexType, IsTrunc);
10535 createOperands(N, Ops);
10536
10537 assert(N->getMask().getValueType().getVectorElementCount() ==
10538 N->getValue().getValueType().getVectorElementCount() &&
10539 "Vector width mismatch between mask and data");
10540 assert(
10541 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10542 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10543 "Scalable flags of index and data do not match");
10545 N->getIndex().getValueType().getVectorElementCount(),
10546 N->getValue().getValueType().getVectorElementCount()) &&
10547 "Vector width mismatch between index and data");
10548 assert(isa<ConstantSDNode>(N->getScale()) &&
10549 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10550 "Scale should be a constant power of 2");
10551
10552 CSEMap.InsertNode(N, IP);
10553 InsertNode(N);
10554 SDValue V(N, 0);
10555 NewSDValueDbgMsg(V, "Creating new node: ", this);
10556 return V;
10557}
10558
10560 const SDLoc &dl, ArrayRef<SDValue> Ops,
10561 MachineMemOperand *MMO,
10562 ISD::MemIndexType IndexType) {
10563 assert(Ops.size() == 7 && "Incompatible number of operands");
10564
10567 ID.AddInteger(MemVT.getRawBits());
10568 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10569 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10570 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10571 ID.AddInteger(MMO->getFlags());
10572 void *IP = nullptr;
10573 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10574 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10575 return SDValue(E, 0);
10576 }
10577
10578 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10579 VTs, MemVT, MMO, IndexType);
10580 createOperands(N, Ops);
10581
10582 assert(N->getMask().getValueType().getVectorElementCount() ==
10583 N->getIndex().getValueType().getVectorElementCount() &&
10584 "Vector width mismatch between mask and data");
10585 assert(isa<ConstantSDNode>(N->getScale()) &&
10586 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10587 "Scale should be a constant power of 2");
10588 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10589
10590 CSEMap.InsertNode(N, IP);
10591 InsertNode(N);
10592 SDValue V(N, 0);
10593 NewSDValueDbgMsg(V, "Creating new node: ", this);
10594 return V;
10595}
10596
10598 SDValue Ptr, SDValue Mask, SDValue EVL,
10599 MachineMemOperand *MMO) {
10600 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10601 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10603 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10604 ID.AddInteger(VT.getRawBits());
10605 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10606 VTs, VT, MMO));
10607 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10608 ID.AddInteger(MMO->getFlags());
10609 void *IP = nullptr;
10610 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10611 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10612 return SDValue(E, 0);
10613 }
10614 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10615 VT, MMO);
10616 createOperands(N, Ops);
10617
10618 CSEMap.InsertNode(N, IP);
10619 InsertNode(N);
10620 SDValue V(N, 0);
10621 NewSDValueDbgMsg(V, "Creating new node: ", this);
10622 return V;
10623}
10624
10626 EVT MemVT, MachineMemOperand *MMO) {
10627 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10628 SDVTList VTs = getVTList(MVT::Other);
10629 SDValue Ops[] = {Chain, Ptr};
10632 ID.AddInteger(MemVT.getRawBits());
10633 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10634 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10635 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10636 ID.AddInteger(MMO->getFlags());
10637 void *IP = nullptr;
10638 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10639 return SDValue(E, 0);
10640
10641 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10642 dl.getDebugLoc(), VTs, MemVT, MMO);
10643 createOperands(N, Ops);
10644
10645 CSEMap.InsertNode(N, IP);
10646 InsertNode(N);
10647 SDValue V(N, 0);
10648 NewSDValueDbgMsg(V, "Creating new node: ", this);
10649 return V;
10650}
10651
10653 EVT MemVT, MachineMemOperand *MMO) {
10654 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10655 SDVTList VTs = getVTList(MVT::Other);
10656 SDValue Ops[] = {Chain, Ptr};
10659 ID.AddInteger(MemVT.getRawBits());
10660 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10661 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10662 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10663 ID.AddInteger(MMO->getFlags());
10664 void *IP = nullptr;
10665 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10666 return SDValue(E, 0);
10667
10668 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10669 dl.getDebugLoc(), VTs, MemVT, MMO);
10670 createOperands(N, Ops);
10671
10672 CSEMap.InsertNode(N, IP);
10673 InsertNode(N);
10674 SDValue V(N, 0);
10675 NewSDValueDbgMsg(V, "Creating new node: ", this);
10676 return V;
10677}
10678
10680 // select undef, T, F --> T (if T is a constant), otherwise F
10681 // select, ?, undef, F --> F
10682 // select, ?, T, undef --> T
10683 if (Cond.isUndef())
10684 return isConstantValueOfAnyType(T) ? T : F;
10685 if (T.isUndef())
10686 return F;
10687 if (F.isUndef())
10688 return T;
10689
10690 // select true, T, F --> T
10691 // select false, T, F --> F
10692 if (auto C = isBoolConstant(Cond))
10693 return *C ? T : F;
10694
10695 // select ?, T, T --> T
10696 if (T == F)
10697 return T;
10698
10699 return SDValue();
10700}
10701
10703 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10704 if (X.isUndef())
10705 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10706 // shift X, undef --> undef (because it may shift by the bitwidth)
10707 if (Y.isUndef())
10708 return getUNDEF(X.getValueType());
10709
10710 // shift 0, Y --> 0
10711 // shift X, 0 --> X
10713 return X;
10714
10715 // shift X, C >= bitwidth(X) --> undef
10716 // All vector elements must be too big (or undef) to avoid partial undefs.
10717 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10718 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10719 };
10720 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10721 return getUNDEF(X.getValueType());
10722
10723 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10724 if (X.getValueType().getScalarType() == MVT::i1)
10725 return X;
10726
10727 return SDValue();
10728}
10729
10731 SDNodeFlags Flags) {
10732 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10733 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10734 // operation is poison. That result can be relaxed to undef.
10735 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10736 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10737 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10738 (YC && YC->getValueAPF().isNaN());
10739 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10740 (YC && YC->getValueAPF().isInfinity());
10741
10742 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10743 return getUNDEF(X.getValueType());
10744
10745 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10746 return getUNDEF(X.getValueType());
10747
10748 if (!YC)
10749 return SDValue();
10750
10751 // X + -0.0 --> X
10752 if (Opcode == ISD::FADD)
10753 if (YC->getValueAPF().isNegZero())
10754 return X;
10755
10756 // X - +0.0 --> X
10757 if (Opcode == ISD::FSUB)
10758 if (YC->getValueAPF().isPosZero())
10759 return X;
10760
10761 // X * 1.0 --> X
10762 // X / 1.0 --> X
10763 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10764 if (YC->getValueAPF().isExactlyValue(1.0))
10765 return X;
10766
10767 // X * 0.0 --> 0.0
10768 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10769 if (YC->getValueAPF().isZero())
10770 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10771
10772 return SDValue();
10773}
10774
10776 SDValue Ptr, SDValue SV, unsigned Align) {
10777 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10778 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10779}
10780
10781SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10782 ArrayRef<SDUse> Ops) {
10783 switch (Ops.size()) {
10784 case 0: return getNode(Opcode, DL, VT);
10785 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10786 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10787 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10788 default: break;
10789 }
10790
10791 // Copy from an SDUse array into an SDValue array for use with
10792 // the regular getNode logic.
10793 SmallVector<SDValue, 8> NewOps(Ops);
10794 return getNode(Opcode, DL, VT, NewOps);
10795}
10796
10797SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10798 ArrayRef<SDValue> Ops) {
10799 SDNodeFlags Flags;
10800 if (Inserter)
10801 Flags = Inserter->getFlags();
10802 return getNode(Opcode, DL, VT, Ops, Flags);
10803}
10804
10805SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10806 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10807 unsigned NumOps = Ops.size();
10808 switch (NumOps) {
10809 case 0: return getNode(Opcode, DL, VT);
10810 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10811 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10812 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10813 default: break;
10814 }
10815
10816#ifndef NDEBUG
10817 for (const auto &Op : Ops)
10818 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10819 "Operand is DELETED_NODE!");
10820#endif
10821
10822 switch (Opcode) {
10823 default: break;
10824 case ISD::BUILD_VECTOR:
10825 // Attempt to simplify BUILD_VECTOR.
10826 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10827 return V;
10828 break;
10830 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10831 return V;
10832 break;
10833 case ISD::SELECT_CC:
10834 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10835 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10836 "LHS and RHS of condition must have same type!");
10837 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10838 "True and False arms of SelectCC must have same type!");
10839 assert(Ops[2].getValueType() == VT &&
10840 "select_cc node must be of same type as true and false value!");
10841 assert((!Ops[0].getValueType().isVector() ||
10842 Ops[0].getValueType().getVectorElementCount() ==
10843 VT.getVectorElementCount()) &&
10844 "Expected select_cc with vector result to have the same sized "
10845 "comparison type!");
10846 break;
10847 case ISD::BR_CC:
10848 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10849 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10850 "LHS/RHS of comparison should match types!");
10851 break;
10852 case ISD::VP_ADD:
10853 case ISD::VP_SUB:
10854 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10855 if (VT.getScalarType() == MVT::i1)
10856 Opcode = ISD::VP_XOR;
10857 break;
10858 case ISD::VP_MUL:
10859 // If it is VP_MUL mask operation then turn it to VP_AND
10860 if (VT.getScalarType() == MVT::i1)
10861 Opcode = ISD::VP_AND;
10862 break;
10863 case ISD::VP_REDUCE_MUL:
10864 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10865 if (VT == MVT::i1)
10866 Opcode = ISD::VP_REDUCE_AND;
10867 break;
10868 case ISD::VP_REDUCE_ADD:
10869 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10870 if (VT == MVT::i1)
10871 Opcode = ISD::VP_REDUCE_XOR;
10872 break;
10873 case ISD::VP_REDUCE_SMAX:
10874 case ISD::VP_REDUCE_UMIN:
10875 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10876 // VP_REDUCE_AND.
10877 if (VT == MVT::i1)
10878 Opcode = ISD::VP_REDUCE_AND;
10879 break;
10880 case ISD::VP_REDUCE_SMIN:
10881 case ISD::VP_REDUCE_UMAX:
10882 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10883 // VP_REDUCE_OR.
10884 if (VT == MVT::i1)
10885 Opcode = ISD::VP_REDUCE_OR;
10886 break;
10887 }
10888
10889 // Memoize nodes.
10890 SDNode *N;
10891 SDVTList VTs = getVTList(VT);
10892
10893 if (VT != MVT::Glue) {
10895 AddNodeIDNode(ID, Opcode, VTs, Ops);
10896 void *IP = nullptr;
10897
10898 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10899 E->intersectFlagsWith(Flags);
10900 return SDValue(E, 0);
10901 }
10902
10903 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10904 createOperands(N, Ops);
10905
10906 CSEMap.InsertNode(N, IP);
10907 } else {
10908 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10909 createOperands(N, Ops);
10910 }
10911
10912 N->setFlags(Flags);
10913 InsertNode(N);
10914 SDValue V(N, 0);
10915 NewSDValueDbgMsg(V, "Creating new node: ", this);
10916 return V;
10917}
10918
10919SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10920 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10921 SDNodeFlags Flags;
10922 if (Inserter)
10923 Flags = Inserter->getFlags();
10924 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10925}
10926
10927SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10928 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops,
10929 const SDNodeFlags Flags) {
10930 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10931}
10932
10933SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10934 ArrayRef<SDValue> Ops) {
10935 SDNodeFlags Flags;
10936 if (Inserter)
10937 Flags = Inserter->getFlags();
10938 return getNode(Opcode, DL, VTList, Ops, Flags);
10939}
10940
10941SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10942 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10943 if (VTList.NumVTs == 1)
10944 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10945
10946#ifndef NDEBUG
10947 for (const auto &Op : Ops)
10948 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10949 "Operand is DELETED_NODE!");
10950#endif
10951
10952 switch (Opcode) {
10953 case ISD::SADDO:
10954 case ISD::UADDO:
10955 case ISD::SSUBO:
10956 case ISD::USUBO: {
10957 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10958 "Invalid add/sub overflow op!");
10959 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10960 Ops[0].getValueType() == Ops[1].getValueType() &&
10961 Ops[0].getValueType() == VTList.VTs[0] &&
10962 "Binary operator types must match!");
10963 SDValue N1 = Ops[0], N2 = Ops[1];
10964 canonicalizeCommutativeBinop(Opcode, N1, N2);
10965
10966 // (X +- 0) -> X with zero-overflow.
10967 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10968 /*AllowTruncation*/ true);
10969 if (N2CV && N2CV->isZero()) {
10970 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10971 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10972 }
10973
10974 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
10975 VTList.VTs[1].getScalarType() == MVT::i1) {
10976 SDValue F1 = getFreeze(N1);
10977 SDValue F2 = getFreeze(N2);
10978 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10979 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10980 return getNode(ISD::MERGE_VALUES, DL, VTList,
10981 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10982 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10983 Flags);
10984 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
10985 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
10986 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
10987 return getNode(ISD::MERGE_VALUES, DL, VTList,
10988 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10989 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
10990 Flags);
10991 }
10992 }
10993 break;
10994 }
10995 case ISD::SADDO_CARRY:
10996 case ISD::UADDO_CARRY:
10997 case ISD::SSUBO_CARRY:
10998 case ISD::USUBO_CARRY:
10999 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11000 "Invalid add/sub overflow op!");
11001 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11002 Ops[0].getValueType() == Ops[1].getValueType() &&
11003 Ops[0].getValueType() == VTList.VTs[0] &&
11004 Ops[2].getValueType() == VTList.VTs[1] &&
11005 "Binary operator types must match!");
11006 break;
11007 case ISD::SMUL_LOHI:
11008 case ISD::UMUL_LOHI: {
11009 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11010 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11011 VTList.VTs[0] == Ops[0].getValueType() &&
11012 VTList.VTs[0] == Ops[1].getValueType() &&
11013 "Binary operator types must match!");
11014 // Constant fold.
11015 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
11016 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
11017 if (LHS && RHS) {
11018 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11019 unsigned OutWidth = Width * 2;
11020 APInt Val = LHS->getAPIntValue();
11021 APInt Mul = RHS->getAPIntValue();
11022 if (Opcode == ISD::SMUL_LOHI) {
11023 Val = Val.sext(OutWidth);
11024 Mul = Mul.sext(OutWidth);
11025 } else {
11026 Val = Val.zext(OutWidth);
11027 Mul = Mul.zext(OutWidth);
11028 }
11029 Val *= Mul;
11030
11031 SDValue Hi =
11032 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11033 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11034 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11035 }
11036 break;
11037 }
11038 case ISD::FFREXP: {
11039 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11040 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11041 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11042
11043 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
11044 int FrexpExp;
11045 APFloat FrexpMant =
11046 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11047 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11048 SDValue Result1 =
11049 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
11050 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11051 }
11052
11053 break;
11054 }
11056 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11057 "Invalid STRICT_FP_EXTEND!");
11058 assert(VTList.VTs[0].isFloatingPoint() &&
11059 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11060 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11061 "STRICT_FP_EXTEND result type should be vector iff the operand "
11062 "type is vector!");
11063 assert((!VTList.VTs[0].isVector() ||
11064 VTList.VTs[0].getVectorElementCount() ==
11065 Ops[1].getValueType().getVectorElementCount()) &&
11066 "Vector element count mismatch!");
11067 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11068 "Invalid fpext node, dst <= src!");
11069 break;
11071 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11072 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11073 "STRICT_FP_ROUND result type should be vector iff the operand "
11074 "type is vector!");
11075 assert((!VTList.VTs[0].isVector() ||
11076 VTList.VTs[0].getVectorElementCount() ==
11077 Ops[1].getValueType().getVectorElementCount()) &&
11078 "Vector element count mismatch!");
11079 assert(VTList.VTs[0].isFloatingPoint() &&
11080 Ops[1].getValueType().isFloatingPoint() &&
11081 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11082 Ops[2].getOpcode() == ISD::TargetConstant &&
11083 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11084 "Invalid STRICT_FP_ROUND!");
11085 break;
11086 }
11087
11088 // Memoize the node unless it returns a glue result.
11089 SDNode *N;
11090 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11092 AddNodeIDNode(ID, Opcode, VTList, Ops);
11093 void *IP = nullptr;
11094 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11095 E->intersectFlagsWith(Flags);
11096 return SDValue(E, 0);
11097 }
11098
11099 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11100 createOperands(N, Ops);
11101 CSEMap.InsertNode(N, IP);
11102 } else {
11103 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11104 createOperands(N, Ops);
11105 }
11106
11107 N->setFlags(Flags);
11108 InsertNode(N);
11109 SDValue V(N, 0);
11110 NewSDValueDbgMsg(V, "Creating new node: ", this);
11111 return V;
11112}
11113
11114SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11115 SDVTList VTList) {
11116 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11117}
11118
11119SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11120 SDValue N1) {
11121 SDValue Ops[] = { N1 };
11122 return getNode(Opcode, DL, VTList, Ops);
11123}
11124
11125SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11126 SDValue N1, SDValue N2) {
11127 SDValue Ops[] = { N1, N2 };
11128 return getNode(Opcode, DL, VTList, Ops);
11129}
11130
11131SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11132 SDValue N1, SDValue N2, SDValue N3) {
11133 SDValue Ops[] = { N1, N2, N3 };
11134 return getNode(Opcode, DL, VTList, Ops);
11135}
11136
11137SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11138 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11139 SDValue Ops[] = { N1, N2, N3, N4 };
11140 return getNode(Opcode, DL, VTList, Ops);
11141}
11142
11143SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11144 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11145 SDValue N5) {
11146 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11147 return getNode(Opcode, DL, VTList, Ops);
11148}
11149
11151 if (!VT.isExtended())
11152 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11153
11154 return makeVTList(&(*EVTs.insert(VT).first), 1);
11155}
11156
11159 ID.AddInteger(2U);
11160 ID.AddInteger(VT1.getRawBits());
11161 ID.AddInteger(VT2.getRawBits());
11162
11163 void *IP = nullptr;
11164 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11165 if (!Result) {
11166 EVT *Array = Allocator.Allocate<EVT>(2);
11167 Array[0] = VT1;
11168 Array[1] = VT2;
11169 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11170 VTListMap.InsertNode(Result, IP);
11171 }
11172 return Result->getSDVTList();
11173}
11174
11177 ID.AddInteger(3U);
11178 ID.AddInteger(VT1.getRawBits());
11179 ID.AddInteger(VT2.getRawBits());
11180 ID.AddInteger(VT3.getRawBits());
11181
11182 void *IP = nullptr;
11183 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11184 if (!Result) {
11185 EVT *Array = Allocator.Allocate<EVT>(3);
11186 Array[0] = VT1;
11187 Array[1] = VT2;
11188 Array[2] = VT3;
11189 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11190 VTListMap.InsertNode(Result, IP);
11191 }
11192 return Result->getSDVTList();
11193}
11194
11197 ID.AddInteger(4U);
11198 ID.AddInteger(VT1.getRawBits());
11199 ID.AddInteger(VT2.getRawBits());
11200 ID.AddInteger(VT3.getRawBits());
11201 ID.AddInteger(VT4.getRawBits());
11202
11203 void *IP = nullptr;
11204 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11205 if (!Result) {
11206 EVT *Array = Allocator.Allocate<EVT>(4);
11207 Array[0] = VT1;
11208 Array[1] = VT2;
11209 Array[2] = VT3;
11210 Array[3] = VT4;
11211 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11212 VTListMap.InsertNode(Result, IP);
11213 }
11214 return Result->getSDVTList();
11215}
11216
11218 unsigned NumVTs = VTs.size();
11220 ID.AddInteger(NumVTs);
11221 for (unsigned index = 0; index < NumVTs; index++) {
11222 ID.AddInteger(VTs[index].getRawBits());
11223 }
11224
11225 void *IP = nullptr;
11226 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11227 if (!Result) {
11228 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11229 llvm::copy(VTs, Array);
11230 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11231 VTListMap.InsertNode(Result, IP);
11232 }
11233 return Result->getSDVTList();
11234}
11235
11236
11237/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11238/// specified operands. If the resultant node already exists in the DAG,
11239/// this does not modify the specified node, instead it returns the node that
11240/// already exists. If the resultant node does not exist in the DAG, the
11241/// input node is returned. As a degenerate case, if you specify the same
11242/// input operands as the node already has, the input node is returned.
11244 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11245
11246 // Check to see if there is no change.
11247 if (Op == N->getOperand(0)) return N;
11248
11249 // See if the modified node already exists.
11250 void *InsertPos = nullptr;
11251 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11252 return Existing;
11253
11254 // Nope it doesn't. Remove the node from its current place in the maps.
11255 if (InsertPos)
11256 if (!RemoveNodeFromCSEMaps(N))
11257 InsertPos = nullptr;
11258
11259 // Now we update the operands.
11260 N->OperandList[0].set(Op);
11261
11263 // If this gets put into a CSE map, add it.
11264 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11265 return N;
11266}
11267
11269 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11270
11271 // Check to see if there is no change.
11272 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11273 return N; // No operands changed, just return the input node.
11274
11275 // See if the modified node already exists.
11276 void *InsertPos = nullptr;
11277 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11278 return Existing;
11279
11280 // Nope it doesn't. Remove the node from its current place in the maps.
11281 if (InsertPos)
11282 if (!RemoveNodeFromCSEMaps(N))
11283 InsertPos = nullptr;
11284
11285 // Now we update the operands.
11286 if (N->OperandList[0] != Op1)
11287 N->OperandList[0].set(Op1);
11288 if (N->OperandList[1] != Op2)
11289 N->OperandList[1].set(Op2);
11290
11292 // If this gets put into a CSE map, add it.
11293 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11294 return N;
11295}
11296
11299 SDValue Ops[] = { Op1, Op2, Op3 };
11300 return UpdateNodeOperands(N, Ops);
11301}
11302
11305 SDValue Op3, SDValue Op4) {
11306 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11307 return UpdateNodeOperands(N, Ops);
11308}
11309
11312 SDValue Op3, SDValue Op4, SDValue Op5) {
11313 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11314 return UpdateNodeOperands(N, Ops);
11315}
11316
11319 unsigned NumOps = Ops.size();
11320 assert(N->getNumOperands() == NumOps &&
11321 "Update with wrong number of operands");
11322
11323 // If no operands changed just return the input node.
11324 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11325 return N;
11326
11327 // See if the modified node already exists.
11328 void *InsertPos = nullptr;
11329 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11330 return Existing;
11331
11332 // Nope it doesn't. Remove the node from its current place in the maps.
11333 if (InsertPos)
11334 if (!RemoveNodeFromCSEMaps(N))
11335 InsertPos = nullptr;
11336
11337 // Now we update the operands.
11338 for (unsigned i = 0; i != NumOps; ++i)
11339 if (N->OperandList[i] != Ops[i])
11340 N->OperandList[i].set(Ops[i]);
11341
11343 // If this gets put into a CSE map, add it.
11344 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11345 return N;
11346}
11347
11348/// DropOperands - Release the operands and set this node to have
11349/// zero operands.
11351 // Unlike the code in MorphNodeTo that does this, we don't need to
11352 // watch for dead nodes here.
11353 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11354 SDUse &Use = *I++;
11355 Use.set(SDValue());
11356 }
11357}
11358
11360 ArrayRef<MachineMemOperand *> NewMemRefs) {
11361 if (NewMemRefs.empty()) {
11362 N->clearMemRefs();
11363 return;
11364 }
11365
11366 // Check if we can avoid allocating by storing a single reference directly.
11367 if (NewMemRefs.size() == 1) {
11368 N->MemRefs = NewMemRefs[0];
11369 N->NumMemRefs = 1;
11370 return;
11371 }
11372
11373 MachineMemOperand **MemRefsBuffer =
11374 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11375 llvm::copy(NewMemRefs, MemRefsBuffer);
11376 N->MemRefs = MemRefsBuffer;
11377 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11378}
11379
11380/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11381/// machine opcode.
11382///
11384 EVT VT) {
11385 SDVTList VTs = getVTList(VT);
11386 return SelectNodeTo(N, MachineOpc, VTs, {});
11387}
11388
11390 EVT VT, SDValue Op1) {
11391 SDVTList VTs = getVTList(VT);
11392 SDValue Ops[] = { Op1 };
11393 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11394}
11395
11397 EVT VT, SDValue Op1,
11398 SDValue Op2) {
11399 SDVTList VTs = getVTList(VT);
11400 SDValue Ops[] = { Op1, Op2 };
11401 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11402}
11403
11405 EVT VT, SDValue Op1,
11406 SDValue Op2, SDValue Op3) {
11407 SDVTList VTs = getVTList(VT);
11408 SDValue Ops[] = { Op1, Op2, Op3 };
11409 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11410}
11411
11413 EVT VT, ArrayRef<SDValue> Ops) {
11414 SDVTList VTs = getVTList(VT);
11415 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11416}
11417
11419 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11420 SDVTList VTs = getVTList(VT1, VT2);
11421 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11422}
11423
11425 EVT VT1, EVT VT2) {
11426 SDVTList VTs = getVTList(VT1, VT2);
11427 return SelectNodeTo(N, MachineOpc, VTs, {});
11428}
11429
11431 EVT VT1, EVT VT2, EVT VT3,
11432 ArrayRef<SDValue> Ops) {
11433 SDVTList VTs = getVTList(VT1, VT2, VT3);
11434 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11435}
11436
11438 EVT VT1, EVT VT2,
11439 SDValue Op1, SDValue Op2) {
11440 SDVTList VTs = getVTList(VT1, VT2);
11441 SDValue Ops[] = { Op1, Op2 };
11442 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11443}
11444
11446 SDVTList VTs,ArrayRef<SDValue> Ops) {
11447 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11448 // Reset the NodeID to -1.
11449 New->setNodeId(-1);
11450 if (New != N) {
11451 ReplaceAllUsesWith(N, New);
11453 }
11454 return New;
11455}
11456
11457/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11458/// the line number information on the merged node since it is not possible to
11459/// preserve the information that operation is associated with multiple lines.
11460/// This will make the debugger working better at -O0, were there is a higher
11461/// probability having other instructions associated with that line.
11462///
11463/// For IROrder, we keep the smaller of the two
11464SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11465 DebugLoc NLoc = N->getDebugLoc();
11466 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11467 N->setDebugLoc(DebugLoc());
11468 }
11469 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11470 N->setIROrder(Order);
11471 return N;
11472}
11473
11474/// MorphNodeTo - This *mutates* the specified node to have the specified
11475/// return type, opcode, and operands.
11476///
11477/// Note that MorphNodeTo returns the resultant node. If there is already a
11478/// node of the specified opcode and operands, it returns that node instead of
11479/// the current one. Note that the SDLoc need not be the same.
11480///
11481/// Using MorphNodeTo is faster than creating a new node and swapping it in
11482/// with ReplaceAllUsesWith both because it often avoids allocating a new
11483/// node, and because it doesn't require CSE recalculation for any of
11484/// the node's users.
11485///
11486/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11487/// As a consequence it isn't appropriate to use from within the DAG combiner or
11488/// the legalizer which maintain worklists that would need to be updated when
11489/// deleting things.
11491 SDVTList VTs, ArrayRef<SDValue> Ops) {
11492 // If an identical node already exists, use it.
11493 void *IP = nullptr;
11494 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11496 AddNodeIDNode(ID, Opc, VTs, Ops);
11497 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11498 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11499 }
11500
11501 if (!RemoveNodeFromCSEMaps(N))
11502 IP = nullptr;
11503
11504 // Start the morphing.
11505 N->NodeType = Opc;
11506 N->ValueList = VTs.VTs;
11507 N->NumValues = VTs.NumVTs;
11508
11509 // Clear the operands list, updating used nodes to remove this from their
11510 // use list. Keep track of any operands that become dead as a result.
11511 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11512 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11513 SDUse &Use = *I++;
11514 SDNode *Used = Use.getNode();
11515 Use.set(SDValue());
11516 if (Used->use_empty())
11517 DeadNodeSet.insert(Used);
11518 }
11519
11520 // For MachineNode, initialize the memory references information.
11521 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
11522 MN->clearMemRefs();
11523
11524 // Swap for an appropriately sized array from the recycler.
11525 removeOperands(N);
11526 createOperands(N, Ops);
11527
11528 // Delete any nodes that are still dead after adding the uses for the
11529 // new operands.
11530 if (!DeadNodeSet.empty()) {
11531 SmallVector<SDNode *, 16> DeadNodes;
11532 for (SDNode *N : DeadNodeSet)
11533 if (N->use_empty())
11534 DeadNodes.push_back(N);
11535 RemoveDeadNodes(DeadNodes);
11536 }
11537
11538 if (IP)
11539 CSEMap.InsertNode(N, IP); // Memoize the new node.
11540 return N;
11541}
11542
11544 unsigned OrigOpc = Node->getOpcode();
11545 unsigned NewOpc;
11546 switch (OrigOpc) {
11547 default:
11548 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11549#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11550 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11551#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11552 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11553#include "llvm/IR/ConstrainedOps.def"
11554 }
11555
11556 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11557
11558 // We're taking this node out of the chain, so we need to re-link things.
11559 SDValue InputChain = Node->getOperand(0);
11560 SDValue OutputChain = SDValue(Node, 1);
11561 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11562
11564 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11565 Ops.push_back(Node->getOperand(i));
11566
11567 SDVTList VTs = getVTList(Node->getValueType(0));
11568 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11569
11570 // MorphNodeTo can operate in two ways: if an existing node with the
11571 // specified operands exists, it can just return it. Otherwise, it
11572 // updates the node in place to have the requested operands.
11573 if (Res == Node) {
11574 // If we updated the node in place, reset the node ID. To the isel,
11575 // this should be just like a newly allocated machine node.
11576 Res->setNodeId(-1);
11577 } else {
11578 ReplaceAllUsesWith(Node, Res);
11579 RemoveDeadNode(Node);
11580 }
11581
11582 return Res;
11583}
11584
11585/// getMachineNode - These are used for target selectors to create a new node
11586/// with specified return type(s), MachineInstr opcode, and operands.
11587///
11588/// Note that getMachineNode returns the resultant node. If there is already a
11589/// node of the specified opcode and operands, it returns that node instead of
11590/// the current one.
11592 EVT VT) {
11593 SDVTList VTs = getVTList(VT);
11594 return getMachineNode(Opcode, dl, VTs, {});
11595}
11596
11598 EVT VT, SDValue Op1) {
11599 SDVTList VTs = getVTList(VT);
11600 SDValue Ops[] = { Op1 };
11601 return getMachineNode(Opcode, dl, VTs, Ops);
11602}
11603
11605 EVT VT, SDValue Op1, SDValue Op2) {
11606 SDVTList VTs = getVTList(VT);
11607 SDValue Ops[] = { Op1, Op2 };
11608 return getMachineNode(Opcode, dl, VTs, Ops);
11609}
11610
11612 EVT VT, SDValue Op1, SDValue Op2,
11613 SDValue Op3) {
11614 SDVTList VTs = getVTList(VT);
11615 SDValue Ops[] = { Op1, Op2, Op3 };
11616 return getMachineNode(Opcode, dl, VTs, Ops);
11617}
11618
11620 EVT VT, ArrayRef<SDValue> Ops) {
11621 SDVTList VTs = getVTList(VT);
11622 return getMachineNode(Opcode, dl, VTs, Ops);
11623}
11624
11626 EVT VT1, EVT VT2, SDValue Op1,
11627 SDValue Op2) {
11628 SDVTList VTs = getVTList(VT1, VT2);
11629 SDValue Ops[] = { Op1, Op2 };
11630 return getMachineNode(Opcode, dl, VTs, Ops);
11631}
11632
11634 EVT VT1, EVT VT2, SDValue Op1,
11635 SDValue Op2, SDValue Op3) {
11636 SDVTList VTs = getVTList(VT1, VT2);
11637 SDValue Ops[] = { Op1, Op2, Op3 };
11638 return getMachineNode(Opcode, dl, VTs, Ops);
11639}
11640
11642 EVT VT1, EVT VT2,
11643 ArrayRef<SDValue> Ops) {
11644 SDVTList VTs = getVTList(VT1, VT2);
11645 return getMachineNode(Opcode, dl, VTs, Ops);
11646}
11647
11649 EVT VT1, EVT VT2, EVT VT3,
11650 SDValue Op1, SDValue Op2) {
11651 SDVTList VTs = getVTList(VT1, VT2, VT3);
11652 SDValue Ops[] = { Op1, Op2 };
11653 return getMachineNode(Opcode, dl, VTs, Ops);
11654}
11655
11657 EVT VT1, EVT VT2, EVT VT3,
11658 SDValue Op1, SDValue Op2,
11659 SDValue Op3) {
11660 SDVTList VTs = getVTList(VT1, VT2, VT3);
11661 SDValue Ops[] = { Op1, Op2, Op3 };
11662 return getMachineNode(Opcode, dl, VTs, Ops);
11663}
11664
11666 EVT VT1, EVT VT2, EVT VT3,
11667 ArrayRef<SDValue> Ops) {
11668 SDVTList VTs = getVTList(VT1, VT2, VT3);
11669 return getMachineNode(Opcode, dl, VTs, Ops);
11670}
11671
11673 ArrayRef<EVT> ResultTys,
11674 ArrayRef<SDValue> Ops) {
11675 SDVTList VTs = getVTList(ResultTys);
11676 return getMachineNode(Opcode, dl, VTs, Ops);
11677}
11678
11680 SDVTList VTs,
11681 ArrayRef<SDValue> Ops) {
11682 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11684 void *IP = nullptr;
11685
11686 if (DoCSE) {
11688 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11689 IP = nullptr;
11690 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11691 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11692 }
11693 }
11694
11695 // Allocate a new MachineSDNode.
11696 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11697 createOperands(N, Ops);
11698
11699 if (DoCSE)
11700 CSEMap.InsertNode(N, IP);
11701
11702 InsertNode(N);
11703 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11704 return N;
11705}
11706
11707/// getTargetExtractSubreg - A convenience function for creating
11708/// TargetOpcode::EXTRACT_SUBREG nodes.
11710 SDValue Operand) {
11711 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11712 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11713 VT, Operand, SRIdxVal);
11714 return SDValue(Subreg, 0);
11715}
11716
11717/// getTargetInsertSubreg - A convenience function for creating
11718/// TargetOpcode::INSERT_SUBREG nodes.
11720 SDValue Operand, SDValue Subreg) {
11721 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11722 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11723 VT, Operand, Subreg, SRIdxVal);
11724 return SDValue(Result, 0);
11725}
11726
11727/// getNodeIfExists - Get the specified node if it's already available, or
11728/// else return NULL.
11730 ArrayRef<SDValue> Ops) {
11731 SDNodeFlags Flags;
11732 if (Inserter)
11733 Flags = Inserter->getFlags();
11734 return getNodeIfExists(Opcode, VTList, Ops, Flags);
11735}
11736
11739 const SDNodeFlags Flags) {
11740 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11742 AddNodeIDNode(ID, Opcode, VTList, Ops);
11743 void *IP = nullptr;
11744 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
11745 E->intersectFlagsWith(Flags);
11746 return E;
11747 }
11748 }
11749 return nullptr;
11750}
11751
11752/// doesNodeExist - Check if a node exists without modifying its flags.
11753bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11754 ArrayRef<SDValue> Ops) {
11755 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11757 AddNodeIDNode(ID, Opcode, VTList, Ops);
11758 void *IP = nullptr;
11759 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11760 return true;
11761 }
11762 return false;
11763}
11764
11765/// getDbgValue - Creates a SDDbgValue node.
11766///
11767/// SDNode
11769 SDNode *N, unsigned R, bool IsIndirect,
11770 const DebugLoc &DL, unsigned O) {
11771 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11772 "Expected inlined-at fields to agree");
11773 return new (DbgInfo->getAlloc())
11774 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11775 {}, IsIndirect, DL, O,
11776 /*IsVariadic=*/false);
11777}
11778
11779/// Constant
11781 DIExpression *Expr,
11782 const Value *C,
11783 const DebugLoc &DL, unsigned O) {
11784 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11785 "Expected inlined-at fields to agree");
11786 return new (DbgInfo->getAlloc())
11787 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11788 /*IsIndirect=*/false, DL, O,
11789 /*IsVariadic=*/false);
11790}
11791
11792/// FrameIndex
11794 DIExpression *Expr, unsigned FI,
11795 bool IsIndirect,
11796 const DebugLoc &DL,
11797 unsigned O) {
11798 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11799 "Expected inlined-at fields to agree");
11800 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11801}
11802
11803/// FrameIndex with dependencies
11805 DIExpression *Expr, unsigned FI,
11806 ArrayRef<SDNode *> Dependencies,
11807 bool IsIndirect,
11808 const DebugLoc &DL,
11809 unsigned O) {
11810 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11811 "Expected inlined-at fields to agree");
11812 return new (DbgInfo->getAlloc())
11813 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11814 Dependencies, IsIndirect, DL, O,
11815 /*IsVariadic=*/false);
11816}
11817
11818/// VReg
11820 Register VReg, bool IsIndirect,
11821 const DebugLoc &DL, unsigned O) {
11822 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11823 "Expected inlined-at fields to agree");
11824 return new (DbgInfo->getAlloc())
11825 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11826 {}, IsIndirect, DL, O,
11827 /*IsVariadic=*/false);
11828}
11829
11832 ArrayRef<SDNode *> Dependencies,
11833 bool IsIndirect, const DebugLoc &DL,
11834 unsigned O, bool IsVariadic) {
11835 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11836 "Expected inlined-at fields to agree");
11837 return new (DbgInfo->getAlloc())
11838 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11839 DL, O, IsVariadic);
11840}
11841
11843 unsigned OffsetInBits, unsigned SizeInBits,
11844 bool InvalidateDbg) {
11845 SDNode *FromNode = From.getNode();
11846 SDNode *ToNode = To.getNode();
11847 assert(FromNode && ToNode && "Can't modify dbg values");
11848
11849 // PR35338
11850 // TODO: assert(From != To && "Redundant dbg value transfer");
11851 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11852 if (From == To || FromNode == ToNode)
11853 return;
11854
11855 if (!FromNode->getHasDebugValue())
11856 return;
11857
11858 SDDbgOperand FromLocOp =
11859 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11861
11863 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11864 if (Dbg->isInvalidated())
11865 continue;
11866
11867 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11868
11869 // Create a new location ops vector that is equal to the old vector, but
11870 // with each instance of FromLocOp replaced with ToLocOp.
11871 bool Changed = false;
11872 auto NewLocOps = Dbg->copyLocationOps();
11873 std::replace_if(
11874 NewLocOps.begin(), NewLocOps.end(),
11875 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11876 bool Match = Op == FromLocOp;
11877 Changed |= Match;
11878 return Match;
11879 },
11880 ToLocOp);
11881 // Ignore this SDDbgValue if we didn't find a matching location.
11882 if (!Changed)
11883 continue;
11884
11885 DIVariable *Var = Dbg->getVariable();
11886 auto *Expr = Dbg->getExpression();
11887 // If a fragment is requested, update the expression.
11888 if (SizeInBits) {
11889 // When splitting a larger (e.g., sign-extended) value whose
11890 // lower bits are described with an SDDbgValue, do not attempt
11891 // to transfer the SDDbgValue to the upper bits.
11892 if (auto FI = Expr->getFragmentInfo())
11893 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11894 continue;
11895 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11896 SizeInBits);
11897 if (!Fragment)
11898 continue;
11899 Expr = *Fragment;
11900 }
11901
11902 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11903 // Clone the SDDbgValue and move it to To.
11904 SDDbgValue *Clone = getDbgValueList(
11905 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11906 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11907 Dbg->isVariadic());
11908 ClonedDVs.push_back(Clone);
11909
11910 if (InvalidateDbg) {
11911 // Invalidate value and indicate the SDDbgValue should not be emitted.
11912 Dbg->setIsInvalidated();
11913 Dbg->setIsEmitted();
11914 }
11915 }
11916
11917 for (SDDbgValue *Dbg : ClonedDVs) {
11918 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11919 "Transferred DbgValues should depend on the new SDNode");
11920 AddDbgValue(Dbg, false);
11921 }
11922}
11923
11925 if (!N.getHasDebugValue())
11926 return;
11927
11928 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11929 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11930 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11931 return SDDbgOperand::fromNode(Node, ResNo);
11932 };
11933
11935 for (auto *DV : GetDbgValues(&N)) {
11936 if (DV->isInvalidated())
11937 continue;
11938 switch (N.getOpcode()) {
11939 default:
11940 break;
11941 case ISD::ADD: {
11942 SDValue N0 = N.getOperand(0);
11943 SDValue N1 = N.getOperand(1);
11944 if (!isa<ConstantSDNode>(N0)) {
11945 bool RHSConstant = isa<ConstantSDNode>(N1);
11947 if (RHSConstant)
11948 Offset = N.getConstantOperandVal(1);
11949 // We are not allowed to turn indirect debug values variadic, so
11950 // don't salvage those.
11951 if (!RHSConstant && DV->isIndirect())
11952 continue;
11953
11954 // Rewrite an ADD constant node into a DIExpression. Since we are
11955 // performing arithmetic to compute the variable's *value* in the
11956 // DIExpression, we need to mark the expression with a
11957 // DW_OP_stack_value.
11958 auto *DIExpr = DV->getExpression();
11959 auto NewLocOps = DV->copyLocationOps();
11960 bool Changed = false;
11961 size_t OrigLocOpsSize = NewLocOps.size();
11962 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11963 // We're not given a ResNo to compare against because the whole
11964 // node is going away. We know that any ISD::ADD only has one
11965 // result, so we can assume any node match is using the result.
11966 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11967 NewLocOps[i].getSDNode() != &N)
11968 continue;
11969 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
11970 if (RHSConstant) {
11973 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11974 } else {
11975 // Convert to a variadic expression (if not already).
11976 // convertToVariadicExpression() returns a const pointer, so we use
11977 // a temporary const variable here.
11978 const auto *TmpDIExpr =
11982 ExprOps.push_back(NewLocOps.size());
11983 ExprOps.push_back(dwarf::DW_OP_plus);
11986 NewLocOps.push_back(RHS);
11987 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11988 }
11989 Changed = true;
11990 }
11991 (void)Changed;
11992 assert(Changed && "Salvage target doesn't use N");
11993
11994 bool IsVariadic =
11995 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11996
11997 auto AdditionalDependencies = DV->getAdditionalDependencies();
11998 SDDbgValue *Clone = getDbgValueList(
11999 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12000 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12001 ClonedDVs.push_back(Clone);
12002 DV->setIsInvalidated();
12003 DV->setIsEmitted();
12004 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12005 N0.getNode()->dumprFull(this);
12006 dbgs() << " into " << *DIExpr << '\n');
12007 }
12008 break;
12009 }
12010 case ISD::TRUNCATE: {
12011 SDValue N0 = N.getOperand(0);
12012 TypeSize FromSize = N0.getValueSizeInBits();
12013 TypeSize ToSize = N.getValueSizeInBits(0);
12014
12015 DIExpression *DbgExpression = DV->getExpression();
12016 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12017 auto NewLocOps = DV->copyLocationOps();
12018 bool Changed = false;
12019 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12020 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12021 NewLocOps[i].getSDNode() != &N)
12022 continue;
12023
12024 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12025 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12026 Changed = true;
12027 }
12028 assert(Changed && "Salvage target doesn't use N");
12029 (void)Changed;
12030
12031 SDDbgValue *Clone =
12032 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12033 DV->getAdditionalDependencies(), DV->isIndirect(),
12034 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12035
12036 ClonedDVs.push_back(Clone);
12037 DV->setIsInvalidated();
12038 DV->setIsEmitted();
12039 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12040 dbgs() << " into " << *DbgExpression << '\n');
12041 break;
12042 }
12043 }
12044 }
12045
12046 for (SDDbgValue *Dbg : ClonedDVs) {
12047 assert((!Dbg->getSDNodes().empty() ||
12048 llvm::any_of(Dbg->getLocationOps(),
12049 [&](const SDDbgOperand &Op) {
12050 return Op.getKind() == SDDbgOperand::FRAMEIX;
12051 })) &&
12052 "Salvaged DbgValue should depend on a new SDNode");
12053 AddDbgValue(Dbg, false);
12054 }
12055}
12056
12057/// Creates a SDDbgLabel node.
12059 const DebugLoc &DL, unsigned O) {
12060 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12061 "Expected inlined-at fields to agree");
12062 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12063}
12064
12065namespace {
12066
12067/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12068/// pointed to by a use iterator is deleted, increment the use iterator
12069/// so that it doesn't dangle.
12070///
12071class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12074
12075 void NodeDeleted(SDNode *N, SDNode *E) override {
12076 // Increment the iterator as needed.
12077 while (UI != UE && N == UI->getUser())
12078 ++UI;
12079 }
12080
12081public:
12082 RAUWUpdateListener(SelectionDAG &d,
12085 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12086};
12087
12088} // end anonymous namespace
12089
12090/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12091/// This can cause recursive merging of nodes in the DAG.
12092///
12093/// This version assumes From has a single result value.
12094///
12096 SDNode *From = FromN.getNode();
12097 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12098 "Cannot replace with this method!");
12099 assert(From != To.getNode() && "Cannot replace uses of with self");
12100
12101 // Preserve Debug Values
12102 transferDbgValues(FromN, To);
12103 // Preserve extra info.
12104 copyExtraInfo(From, To.getNode());
12105
12106 // Iterate over all the existing uses of From. New uses will be added
12107 // to the beginning of the use list, which we avoid visiting.
12108 // This specifically avoids visiting uses of From that arise while the
12109 // replacement is happening, because any such uses would be the result
12110 // of CSE: If an existing node looks like From after one of its operands
12111 // is replaced by To, we don't want to replace of all its users with To
12112 // too. See PR3018 for more info.
12113 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12114 RAUWUpdateListener Listener(*this, UI, UE);
12115 while (UI != UE) {
12116 SDNode *User = UI->getUser();
12117
12118 // This node is about to morph, remove its old self from the CSE maps.
12119 RemoveNodeFromCSEMaps(User);
12120
12121 // A user can appear in a use list multiple times, and when this
12122 // happens the uses are usually next to each other in the list.
12123 // To help reduce the number of CSE recomputations, process all
12124 // the uses of this user that we can find this way.
12125 do {
12126 SDUse &Use = *UI;
12127 ++UI;
12128 Use.set(To);
12129 if (To->isDivergent() != From->isDivergent())
12131 } while (UI != UE && UI->getUser() == User);
12132 // Now that we have modified User, add it back to the CSE maps. If it
12133 // already exists there, recursively merge the results together.
12134 AddModifiedNodeToCSEMaps(User);
12135 }
12136
12137 // If we just RAUW'd the root, take note.
12138 if (FromN == getRoot())
12139 setRoot(To);
12140}
12141
12142/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12143/// This can cause recursive merging of nodes in the DAG.
12144///
12145/// This version assumes that for each value of From, there is a
12146/// corresponding value in To in the same position with the same type.
12147///
12149#ifndef NDEBUG
12150 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12151 assert((!From->hasAnyUseOfValue(i) ||
12152 From->getValueType(i) == To->getValueType(i)) &&
12153 "Cannot use this version of ReplaceAllUsesWith!");
12154#endif
12155
12156 // Handle the trivial case.
12157 if (From == To)
12158 return;
12159
12160 // Preserve Debug Info. Only do this if there's a use.
12161 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12162 if (From->hasAnyUseOfValue(i)) {
12163 assert((i < To->getNumValues()) && "Invalid To location");
12165 }
12166 // Preserve extra info.
12167 copyExtraInfo(From, To);
12168
12169 // Iterate over just the existing users of From. See the comments in
12170 // the ReplaceAllUsesWith above.
12171 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12172 RAUWUpdateListener Listener(*this, UI, UE);
12173 while (UI != UE) {
12174 SDNode *User = UI->getUser();
12175
12176 // This node is about to morph, remove its old self from the CSE maps.
12177 RemoveNodeFromCSEMaps(User);
12178
12179 // A user can appear in a use list multiple times, and when this
12180 // happens the uses are usually next to each other in the list.
12181 // To help reduce the number of CSE recomputations, process all
12182 // the uses of this user that we can find this way.
12183 do {
12184 SDUse &Use = *UI;
12185 ++UI;
12186 Use.setNode(To);
12187 if (To->isDivergent() != From->isDivergent())
12189 } while (UI != UE && UI->getUser() == User);
12190
12191 // Now that we have modified User, add it back to the CSE maps. If it
12192 // already exists there, recursively merge the results together.
12193 AddModifiedNodeToCSEMaps(User);
12194 }
12195
12196 // If we just RAUW'd the root, take note.
12197 if (From == getRoot().getNode())
12198 setRoot(SDValue(To, getRoot().getResNo()));
12199}
12200
12201/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12202/// This can cause recursive merging of nodes in the DAG.
12203///
12204/// This version can replace From with any result values. To must match the
12205/// number and types of values returned by From.
12207 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12208 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12209
12210 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12211 // Preserve Debug Info.
12212 transferDbgValues(SDValue(From, i), To[i]);
12213 // Preserve extra info.
12214 copyExtraInfo(From, To[i].getNode());
12215 }
12216
12217 // Iterate over just the existing users of From. See the comments in
12218 // the ReplaceAllUsesWith above.
12219 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12220 RAUWUpdateListener Listener(*this, UI, UE);
12221 while (UI != UE) {
12222 SDNode *User = UI->getUser();
12223
12224 // This node is about to morph, remove its old self from the CSE maps.
12225 RemoveNodeFromCSEMaps(User);
12226
12227 // A user can appear in a use list multiple times, and when this happens the
12228 // uses are usually next to each other in the list. To help reduce the
12229 // number of CSE and divergence recomputations, process all the uses of this
12230 // user that we can find this way.
12231 bool To_IsDivergent = false;
12232 do {
12233 SDUse &Use = *UI;
12234 const SDValue &ToOp = To[Use.getResNo()];
12235 ++UI;
12236 Use.set(ToOp);
12237 To_IsDivergent |= ToOp->isDivergent();
12238 } while (UI != UE && UI->getUser() == User);
12239
12240 if (To_IsDivergent != From->isDivergent())
12242
12243 // Now that we have modified User, add it back to the CSE maps. If it
12244 // already exists there, recursively merge the results together.
12245 AddModifiedNodeToCSEMaps(User);
12246 }
12247
12248 // If we just RAUW'd the root, take note.
12249 if (From == getRoot().getNode())
12250 setRoot(SDValue(To[getRoot().getResNo()]));
12251}
12252
12253/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12254/// uses of other values produced by From.getNode() alone. The Deleted
12255/// vector is handled the same way as for ReplaceAllUsesWith.
12257 // Handle the really simple, really trivial case efficiently.
12258 if (From == To) return;
12259
12260 // Handle the simple, trivial, case efficiently.
12261 if (From.getNode()->getNumValues() == 1) {
12263 return;
12264 }
12265
12266 // Preserve Debug Info.
12268 copyExtraInfo(From.getNode(), To.getNode());
12269
12270 // Iterate over just the existing users of From. See the comments in
12271 // the ReplaceAllUsesWith above.
12272 SDNode::use_iterator UI = From.getNode()->use_begin(),
12273 UE = From.getNode()->use_end();
12274 RAUWUpdateListener Listener(*this, UI, UE);
12275 while (UI != UE) {
12276 SDNode *User = UI->getUser();
12277 bool UserRemovedFromCSEMaps = false;
12278
12279 // A user can appear in a use list multiple times, and when this
12280 // happens the uses are usually next to each other in the list.
12281 // To help reduce the number of CSE recomputations, process all
12282 // the uses of this user that we can find this way.
12283 do {
12284 SDUse &Use = *UI;
12285
12286 // Skip uses of different values from the same node.
12287 if (Use.getResNo() != From.getResNo()) {
12288 ++UI;
12289 continue;
12290 }
12291
12292 // If this node hasn't been modified yet, it's still in the CSE maps,
12293 // so remove its old self from the CSE maps.
12294 if (!UserRemovedFromCSEMaps) {
12295 RemoveNodeFromCSEMaps(User);
12296 UserRemovedFromCSEMaps = true;
12297 }
12298
12299 ++UI;
12300 Use.set(To);
12301 if (To->isDivergent() != From->isDivergent())
12303 } while (UI != UE && UI->getUser() == User);
12304 // We are iterating over all uses of the From node, so if a use
12305 // doesn't use the specific value, no changes are made.
12306 if (!UserRemovedFromCSEMaps)
12307 continue;
12308
12309 // Now that we have modified User, add it back to the CSE maps. If it
12310 // already exists there, recursively merge the results together.
12311 AddModifiedNodeToCSEMaps(User);
12312 }
12313
12314 // If we just RAUW'd the root, take note.
12315 if (From == getRoot())
12316 setRoot(To);
12317}
12318
12319namespace {
12320
12321/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12322/// to record information about a use.
12323struct UseMemo {
12324 SDNode *User;
12325 unsigned Index;
12326 SDUse *Use;
12327};
12328
12329/// operator< - Sort Memos by User.
12330bool operator<(const UseMemo &L, const UseMemo &R) {
12331 return (intptr_t)L.User < (intptr_t)R.User;
12332}
12333
12334/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12335/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12336/// the node already has been taken care of recursively.
12337class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12339
12340 void NodeDeleted(SDNode *N, SDNode *E) override {
12341 for (UseMemo &Memo : Uses)
12342 if (Memo.User == N)
12343 Memo.User = nullptr;
12344 }
12345
12346public:
12347 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12348 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12349};
12350
12351} // end anonymous namespace
12352
12353/// Return true if a glue output should propagate divergence information.
12355 switch (Node->getOpcode()) {
12356 case ISD::CopyFromReg:
12357 case ISD::CopyToReg:
12358 return false;
12359 default:
12360 return true;
12361 }
12362
12363 llvm_unreachable("covered opcode switch");
12364}
12365
12367 if (TLI->isSDNodeAlwaysUniform(N)) {
12368 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12369 "Conflicting divergence information!");
12370 return false;
12371 }
12372 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12373 return true;
12374 for (const auto &Op : N->ops()) {
12375 EVT VT = Op.getValueType();
12376
12377 // Skip Chain. It does not carry divergence.
12378 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12379 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12380 return true;
12381 }
12382 return false;
12383}
12384
12386 SmallVector<SDNode *, 16> Worklist(1, N);
12387 do {
12388 N = Worklist.pop_back_val();
12389 bool IsDivergent = calculateDivergence(N);
12390 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12391 N->SDNodeBits.IsDivergent = IsDivergent;
12392 llvm::append_range(Worklist, N->users());
12393 }
12394 } while (!Worklist.empty());
12395}
12396
12397void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12399 Order.reserve(AllNodes.size());
12400 for (auto &N : allnodes()) {
12401 unsigned NOps = N.getNumOperands();
12402 Degree[&N] = NOps;
12403 if (0 == NOps)
12404 Order.push_back(&N);
12405 }
12406 for (size_t I = 0; I != Order.size(); ++I) {
12407 SDNode *N = Order[I];
12408 for (auto *U : N->users()) {
12409 unsigned &UnsortedOps = Degree[U];
12410 if (0 == --UnsortedOps)
12411 Order.push_back(U);
12412 }
12413 }
12414}
12415
12416#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12417void SelectionDAG::VerifyDAGDivergence() {
12418 std::vector<SDNode *> TopoOrder;
12419 CreateTopologicalOrder(TopoOrder);
12420 for (auto *N : TopoOrder) {
12421 assert(calculateDivergence(N) == N->isDivergent() &&
12422 "Divergence bit inconsistency detected");
12423 }
12424}
12425#endif
12426
12427/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12428/// uses of other values produced by From.getNode() alone. The same value
12429/// may appear in both the From and To list. The Deleted vector is
12430/// handled the same way as for ReplaceAllUsesWith.
12432 const SDValue *To,
12433 unsigned Num){
12434 // Handle the simple, trivial case efficiently.
12435 if (Num == 1)
12436 return ReplaceAllUsesOfValueWith(*From, *To);
12437
12438 transferDbgValues(*From, *To);
12439 copyExtraInfo(From->getNode(), To->getNode());
12440
12441 // Read up all the uses and make records of them. This helps
12442 // processing new uses that are introduced during the
12443 // replacement process.
12445 for (unsigned i = 0; i != Num; ++i) {
12446 unsigned FromResNo = From[i].getResNo();
12447 SDNode *FromNode = From[i].getNode();
12448 for (SDUse &Use : FromNode->uses()) {
12449 if (Use.getResNo() == FromResNo) {
12450 UseMemo Memo = {Use.getUser(), i, &Use};
12451 Uses.push_back(Memo);
12452 }
12453 }
12454 }
12455
12456 // Sort the uses, so that all the uses from a given User are together.
12458 RAUOVWUpdateListener Listener(*this, Uses);
12459
12460 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12461 UseIndex != UseIndexEnd; ) {
12462 // We know that this user uses some value of From. If it is the right
12463 // value, update it.
12464 SDNode *User = Uses[UseIndex].User;
12465 // If the node has been deleted by recursive CSE updates when updating
12466 // another node, then just skip this entry.
12467 if (User == nullptr) {
12468 ++UseIndex;
12469 continue;
12470 }
12471
12472 // This node is about to morph, remove its old self from the CSE maps.
12473 RemoveNodeFromCSEMaps(User);
12474
12475 // The Uses array is sorted, so all the uses for a given User
12476 // are next to each other in the list.
12477 // To help reduce the number of CSE recomputations, process all
12478 // the uses of this user that we can find this way.
12479 do {
12480 unsigned i = Uses[UseIndex].Index;
12481 SDUse &Use = *Uses[UseIndex].Use;
12482 ++UseIndex;
12483
12484 Use.set(To[i]);
12485 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12486
12487 // Now that we have modified User, add it back to the CSE maps. If it
12488 // already exists there, recursively merge the results together.
12489 AddModifiedNodeToCSEMaps(User);
12490 }
12491}
12492
12493/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12494/// based on their topological order. It returns the maximum id and a vector
12495/// of the SDNodes* in assigned order by reference.
12497 unsigned DAGSize = 0;
12498
12499 // SortedPos tracks the progress of the algorithm. Nodes before it are
12500 // sorted, nodes after it are unsorted. When the algorithm completes
12501 // it is at the end of the list.
12502 allnodes_iterator SortedPos = allnodes_begin();
12503
12504 // Visit all the nodes. Move nodes with no operands to the front of
12505 // the list immediately. Annotate nodes that do have operands with their
12506 // operand count. Before we do this, the Node Id fields of the nodes
12507 // may contain arbitrary values. After, the Node Id fields for nodes
12508 // before SortedPos will contain the topological sort index, and the
12509 // Node Id fields for nodes At SortedPos and after will contain the
12510 // count of outstanding operands.
12512 checkForCycles(&N, this);
12513 unsigned Degree = N.getNumOperands();
12514 if (Degree == 0) {
12515 // A node with no uses, add it to the result array immediately.
12516 N.setNodeId(DAGSize++);
12517 allnodes_iterator Q(&N);
12518 if (Q != SortedPos)
12519 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12520 assert(SortedPos != AllNodes.end() && "Overran node list");
12521 ++SortedPos;
12522 } else {
12523 // Temporarily use the Node Id as scratch space for the degree count.
12524 N.setNodeId(Degree);
12525 }
12526 }
12527
12528 // Visit all the nodes. As we iterate, move nodes into sorted order,
12529 // such that by the time the end is reached all nodes will be sorted.
12530 for (SDNode &Node : allnodes()) {
12531 SDNode *N = &Node;
12532 checkForCycles(N, this);
12533 // N is in sorted position, so all its uses have one less operand
12534 // that needs to be sorted.
12535 for (SDNode *P : N->users()) {
12536 unsigned Degree = P->getNodeId();
12537 assert(Degree != 0 && "Invalid node degree");
12538 --Degree;
12539 if (Degree == 0) {
12540 // All of P's operands are sorted, so P may sorted now.
12541 P->setNodeId(DAGSize++);
12542 if (P->getIterator() != SortedPos)
12543 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12544 assert(SortedPos != AllNodes.end() && "Overran node list");
12545 ++SortedPos;
12546 } else {
12547 // Update P's outstanding operand count.
12548 P->setNodeId(Degree);
12549 }
12550 }
12551 if (Node.getIterator() == SortedPos) {
12552#ifndef NDEBUG
12554 SDNode *S = &*++I;
12555 dbgs() << "Overran sorted position:\n";
12556 S->dumprFull(this); dbgs() << "\n";
12557 dbgs() << "Checking if this is due to cycles\n";
12558 checkForCycles(this, true);
12559#endif
12560 llvm_unreachable(nullptr);
12561 }
12562 }
12563
12564 assert(SortedPos == AllNodes.end() &&
12565 "Topological sort incomplete!");
12566 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12567 "First node in topological sort is not the entry token!");
12568 assert(AllNodes.front().getNodeId() == 0 &&
12569 "First node in topological sort has non-zero id!");
12570 assert(AllNodes.front().getNumOperands() == 0 &&
12571 "First node in topological sort has operands!");
12572 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12573 "Last node in topologic sort has unexpected id!");
12574 assert(AllNodes.back().use_empty() &&
12575 "Last node in topologic sort has users!");
12576 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12577 return DAGSize;
12578}
12579
12580/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12581/// value is produced by SD.
12582void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12583 for (SDNode *SD : DB->getSDNodes()) {
12584 if (!SD)
12585 continue;
12586 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12587 SD->setHasDebugValue(true);
12588 }
12589 DbgInfo->add(DB, isParameter);
12590}
12591
12592void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12593
12595 SDValue NewMemOpChain) {
12596 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12597 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12598 // The new memory operation must have the same position as the old load in
12599 // terms of memory dependency. Create a TokenFactor for the old load and new
12600 // memory operation and update uses of the old load's output chain to use that
12601 // TokenFactor.
12602 if (OldChain == NewMemOpChain || OldChain.use_empty())
12603 return NewMemOpChain;
12604
12605 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12606 OldChain, NewMemOpChain);
12607 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12608 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12609 return TokenFactor;
12610}
12611
12613 SDValue NewMemOp) {
12614 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12615 SDValue OldChain = SDValue(OldLoad, 1);
12616 SDValue NewMemOpChain = NewMemOp.getValue(1);
12617 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12618}
12619
12621 Function **OutFunction) {
12622 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12623
12624 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12625 auto *Module = MF->getFunction().getParent();
12626 auto *Function = Module->getFunction(Symbol);
12627
12628 if (OutFunction != nullptr)
12629 *OutFunction = Function;
12630
12631 if (Function != nullptr) {
12632 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12633 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12634 }
12635
12636 std::string ErrorStr;
12637 raw_string_ostream ErrorFormatter(ErrorStr);
12638 ErrorFormatter << "Undefined external symbol ";
12639 ErrorFormatter << '"' << Symbol << '"';
12640 report_fatal_error(Twine(ErrorStr));
12641}
12642
12643//===----------------------------------------------------------------------===//
12644// SDNode Class
12645//===----------------------------------------------------------------------===//
12646
12648 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12649 return Const != nullptr && Const->isZero();
12650}
12651
12653 return V.isUndef() || isNullConstant(V);
12654}
12655
12657 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
12658 return Const != nullptr && Const->isZero() && !Const->isNegative();
12659}
12660
12662 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12663 return Const != nullptr && Const->isAllOnes();
12664}
12665
12667 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12668 return Const != nullptr && Const->isOne();
12669}
12670
12672 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12673 return Const != nullptr && Const->isMinSignedValue();
12674}
12675
12676bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12677 unsigned OperandNo) {
12678 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12679 // TODO: Target-specific opcodes could be added.
12680 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12681 /*AllowTruncation*/ true)) {
12682 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12683 switch (Opcode) {
12684 case ISD::ADD:
12685 case ISD::OR:
12686 case ISD::XOR:
12687 case ISD::UMAX:
12688 return Const.isZero();
12689 case ISD::MUL:
12690 return Const.isOne();
12691 case ISD::AND:
12692 case ISD::UMIN:
12693 return Const.isAllOnes();
12694 case ISD::SMAX:
12695 return Const.isMinSignedValue();
12696 case ISD::SMIN:
12697 return Const.isMaxSignedValue();
12698 case ISD::SUB:
12699 case ISD::SHL:
12700 case ISD::SRA:
12701 case ISD::SRL:
12702 return OperandNo == 1 && Const.isZero();
12703 case ISD::UDIV:
12704 case ISD::SDIV:
12705 return OperandNo == 1 && Const.isOne();
12706 }
12707 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12708 switch (Opcode) {
12709 case ISD::FADD:
12710 return ConstFP->isZero() &&
12711 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12712 case ISD::FSUB:
12713 return OperandNo == 1 && ConstFP->isZero() &&
12714 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12715 case ISD::FMUL:
12716 return ConstFP->isExactlyValue(1.0);
12717 case ISD::FDIV:
12718 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12719 case ISD::FMINNUM:
12720 case ISD::FMAXNUM: {
12721 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12722 EVT VT = V.getValueType();
12723 const fltSemantics &Semantics = VT.getFltSemantics();
12724 APFloat NeutralAF = !Flags.hasNoNaNs()
12725 ? APFloat::getQNaN(Semantics)
12726 : !Flags.hasNoInfs()
12727 ? APFloat::getInf(Semantics)
12728 : APFloat::getLargest(Semantics);
12729 if (Opcode == ISD::FMAXNUM)
12730 NeutralAF.changeSign();
12731
12732 return ConstFP->isExactlyValue(NeutralAF);
12733 }
12734 }
12735 }
12736 return false;
12737}
12738
12740 while (V.getOpcode() == ISD::BITCAST)
12741 V = V.getOperand(0);
12742 return V;
12743}
12744
12746 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12747 V = V.getOperand(0);
12748 return V;
12749}
12750
12752 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12753 V = V.getOperand(0);
12754 return V;
12755}
12756
12758 while (V.getOpcode() == ISD::TRUNCATE)
12759 V = V.getOperand(0);
12760 return V;
12761}
12762
12763bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12764 if (V.getOpcode() != ISD::XOR)
12765 return false;
12766 V = peekThroughBitcasts(V.getOperand(1));
12767 unsigned NumBits = V.getScalarValueSizeInBits();
12768 ConstantSDNode *C =
12769 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12770 return C && (C->getAPIntValue().countr_one() >= NumBits);
12771}
12772
12774 bool AllowTruncation) {
12775 EVT VT = N.getValueType();
12776 APInt DemandedElts = VT.isFixedLengthVector()
12778 : APInt(1, 1);
12779 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12780}
12781
12783 bool AllowUndefs,
12784 bool AllowTruncation) {
12785 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
12786 return CN;
12787
12788 // SplatVectors can truncate their operands. Ignore that case here unless
12789 // AllowTruncation is set.
12790 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12791 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12792 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12793 EVT CVT = CN->getValueType(0);
12794 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12795 if (AllowTruncation || CVT == VecEltVT)
12796 return CN;
12797 }
12798 }
12799
12800 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12801 BitVector UndefElements;
12802 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12803
12804 // BuildVectors can truncate their operands. Ignore that case here unless
12805 // AllowTruncation is set.
12806 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12807 if (CN && (UndefElements.none() || AllowUndefs)) {
12808 EVT CVT = CN->getValueType(0);
12809 EVT NSVT = N.getValueType().getScalarType();
12810 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12811 if (AllowTruncation || (CVT == NSVT))
12812 return CN;
12813 }
12814 }
12815
12816 return nullptr;
12817}
12818
12820 EVT VT = N.getValueType();
12821 APInt DemandedElts = VT.isFixedLengthVector()
12823 : APInt(1, 1);
12824 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12825}
12826
12828 const APInt &DemandedElts,
12829 bool AllowUndefs) {
12830 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
12831 return CN;
12832
12833 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12834 BitVector UndefElements;
12835 ConstantFPSDNode *CN =
12836 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12837 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12838 if (CN && (UndefElements.none() || AllowUndefs))
12839 return CN;
12840 }
12841
12842 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12843 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12844 return CN;
12845
12846 return nullptr;
12847}
12848
12849bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12850 // TODO: may want to use peekThroughBitcast() here.
12851 ConstantSDNode *C =
12852 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12853 return C && C->isZero();
12854}
12855
12856bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12857 ConstantSDNode *C =
12858 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12859 return C && C->isOne();
12860}
12861
12862bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12864 unsigned BitWidth = N.getScalarValueSizeInBits();
12865 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12866 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12867}
12868
12869bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
12870 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12871 return C && APInt::isSameValue(C->getAPIntValue(),
12872 APInt(C->getAPIntValue().getBitWidth(), 1));
12873}
12874
12875bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
12877 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
12878 return C && C->isZero();
12879}
12880
12882 DropOperands();
12883}
12884
12885MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12886 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12887 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12888 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12889 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12890 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12891 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12892
12893 // We check here that the size of the memory operand fits within the size of
12894 // the MMO. This is because the MMO might indicate only a possible address
12895 // range instead of specifying the affected memory addresses precisely.
12896 assert(
12897 (!MMO->getType().isValid() ||
12899 "Size mismatch!");
12900}
12901
12902/// Profile - Gather unique data for the node.
12903///
12905 AddNodeIDNode(ID, this);
12906}
12907
12908namespace {
12909
12910 struct EVTArray {
12911 std::vector<EVT> VTs;
12912
12913 EVTArray() {
12914 VTs.reserve(MVT::VALUETYPE_SIZE);
12915 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12916 VTs.push_back(MVT((MVT::SimpleValueType)i));
12917 }
12918 };
12919
12920} // end anonymous namespace
12921
12922/// getValueTypeList - Return a pointer to the specified value type.
12923///
12924const EVT *SDNode::getValueTypeList(MVT VT) {
12925 static EVTArray SimpleVTArray;
12926
12927 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
12928 return &SimpleVTArray.VTs[VT.SimpleTy];
12929}
12930
12931/// hasAnyUseOfValue - Return true if there are any use of the indicated
12932/// value. This method ignores uses of other values defined by this operation.
12933bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12934 assert(Value < getNumValues() && "Bad value!");
12935
12936 for (SDUse &U : uses())
12937 if (U.getResNo() == Value)
12938 return true;
12939
12940 return false;
12941}
12942
12943/// isOnlyUserOf - Return true if this node is the only use of N.
12944bool SDNode::isOnlyUserOf(const SDNode *N) const {
12945 bool Seen = false;
12946 for (const SDNode *User : N->users()) {
12947 if (User == this)
12948 Seen = true;
12949 else
12950 return false;
12951 }
12952
12953 return Seen;
12954}
12955
12956/// Return true if the only users of N are contained in Nodes.
12958 bool Seen = false;
12959 for (const SDNode *User : N->users()) {
12960 if (llvm::is_contained(Nodes, User))
12961 Seen = true;
12962 else
12963 return false;
12964 }
12965
12966 return Seen;
12967}
12968
12969/// Return true if the referenced return value is an operand of N.
12970bool SDValue::isOperandOf(const SDNode *N) const {
12971 return is_contained(N->op_values(), *this);
12972}
12973
12974bool SDNode::isOperandOf(const SDNode *N) const {
12975 return any_of(N->op_values(),
12976 [this](SDValue Op) { return this == Op.getNode(); });
12977}
12978
12979/// reachesChainWithoutSideEffects - Return true if this operand (which must
12980/// be a chain) reaches the specified operand without crossing any
12981/// side-effecting instructions on any chain path. In practice, this looks
12982/// through token factors and non-volatile loads. In order to remain efficient,
12983/// this only looks a couple of nodes in, it does not do an exhaustive search.
12984///
12985/// Note that we only need to examine chains when we're searching for
12986/// side-effects; SelectionDAG requires that all side-effects are represented
12987/// by chains, even if another operand would force a specific ordering. This
12988/// constraint is necessary to allow transformations like splitting loads.
12990 unsigned Depth) const {
12991 if (*this == Dest) return true;
12992
12993 // Don't search too deeply, we just want to be able to see through
12994 // TokenFactor's etc.
12995 if (Depth == 0) return false;
12996
12997 // If this is a token factor, all inputs to the TF happen in parallel.
12998 if (getOpcode() == ISD::TokenFactor) {
12999 // First, try a shallow search.
13000 if (is_contained((*this)->ops(), Dest)) {
13001 // We found the chain we want as an operand of this TokenFactor.
13002 // Essentially, we reach the chain without side-effects if we could
13003 // serialize the TokenFactor into a simple chain of operations with
13004 // Dest as the last operation. This is automatically true if the
13005 // chain has one use: there are no other ordering constraints.
13006 // If the chain has more than one use, we give up: some other
13007 // use of Dest might force a side-effect between Dest and the current
13008 // node.
13009 if (Dest.hasOneUse())
13010 return true;
13011 }
13012 // Next, try a deep search: check whether every operand of the TokenFactor
13013 // reaches Dest.
13014 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13015 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13016 });
13017 }
13018
13019 // Loads don't have side effects, look through them.
13020 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13021 if (Ld->isUnordered())
13022 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13023 }
13024 return false;
13025}
13026
13027bool SDNode::hasPredecessor(const SDNode *N) const {
13030 Worklist.push_back(this);
13031 return hasPredecessorHelper(N, Visited, Worklist);
13032}
13033
13035 this->Flags &= Flags;
13036}
13037
13038SDValue
13040 ArrayRef<ISD::NodeType> CandidateBinOps,
13041 bool AllowPartials) {
13042 // The pattern must end in an extract from index 0.
13043 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13044 !isNullConstant(Extract->getOperand(1)))
13045 return SDValue();
13046
13047 // Match against one of the candidate binary ops.
13048 SDValue Op = Extract->getOperand(0);
13049 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13050 return Op.getOpcode() == unsigned(BinOp);
13051 }))
13052 return SDValue();
13053
13054 // Floating-point reductions may require relaxed constraints on the final step
13055 // of the reduction because they may reorder intermediate operations.
13056 unsigned CandidateBinOp = Op.getOpcode();
13057 if (Op.getValueType().isFloatingPoint()) {
13058 SDNodeFlags Flags = Op->getFlags();
13059 switch (CandidateBinOp) {
13060 case ISD::FADD:
13061 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13062 return SDValue();
13063 break;
13064 default:
13065 llvm_unreachable("Unhandled FP opcode for binop reduction");
13066 }
13067 }
13068
13069 // Matching failed - attempt to see if we did enough stages that a partial
13070 // reduction from a subvector is possible.
13071 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13072 if (!AllowPartials || !Op)
13073 return SDValue();
13074 EVT OpVT = Op.getValueType();
13075 EVT OpSVT = OpVT.getScalarType();
13076 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13077 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13078 return SDValue();
13079 BinOp = (ISD::NodeType)CandidateBinOp;
13080 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13081 };
13082
13083 // At each stage, we're looking for something that looks like:
13084 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13085 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13086 // i32 undef, i32 undef, i32 undef, i32 undef>
13087 // %a = binop <8 x i32> %op, %s
13088 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13089 // we expect something like:
13090 // <4,5,6,7,u,u,u,u>
13091 // <2,3,u,u,u,u,u,u>
13092 // <1,u,u,u,u,u,u,u>
13093 // While a partial reduction match would be:
13094 // <2,3,u,u,u,u,u,u>
13095 // <1,u,u,u,u,u,u,u>
13096 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13097 SDValue PrevOp;
13098 for (unsigned i = 0; i < Stages; ++i) {
13099 unsigned MaskEnd = (1 << i);
13100
13101 if (Op.getOpcode() != CandidateBinOp)
13102 return PartialReduction(PrevOp, MaskEnd);
13103
13104 SDValue Op0 = Op.getOperand(0);
13105 SDValue Op1 = Op.getOperand(1);
13106
13107 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
13108 if (Shuffle) {
13109 Op = Op1;
13110 } else {
13111 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13112 Op = Op0;
13113 }
13114
13115 // The first operand of the shuffle should be the same as the other operand
13116 // of the binop.
13117 if (!Shuffle || Shuffle->getOperand(0) != Op)
13118 return PartialReduction(PrevOp, MaskEnd);
13119
13120 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13121 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13122 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13123 return PartialReduction(PrevOp, MaskEnd);
13124
13125 PrevOp = Op;
13126 }
13127
13128 // Handle subvector reductions, which tend to appear after the shuffle
13129 // reduction stages.
13130 while (Op.getOpcode() == CandidateBinOp) {
13131 unsigned NumElts = Op.getValueType().getVectorNumElements();
13132 SDValue Op0 = Op.getOperand(0);
13133 SDValue Op1 = Op.getOperand(1);
13134 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13136 Op0.getOperand(0) != Op1.getOperand(0))
13137 break;
13138 SDValue Src = Op0.getOperand(0);
13139 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13140 if (NumSrcElts != (2 * NumElts))
13141 break;
13142 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13143 Op1.getConstantOperandAPInt(1) == NumElts) &&
13144 !(Op1.getConstantOperandAPInt(1) == 0 &&
13145 Op0.getConstantOperandAPInt(1) == NumElts))
13146 break;
13147 Op = Src;
13148 }
13149
13150 BinOp = (ISD::NodeType)CandidateBinOp;
13151 return Op;
13152}
13153
13155 EVT VT = N->getValueType(0);
13156 EVT EltVT = VT.getVectorElementType();
13157 unsigned NE = VT.getVectorNumElements();
13158
13159 SDLoc dl(N);
13160
13161 // If ResNE is 0, fully unroll the vector op.
13162 if (ResNE == 0)
13163 ResNE = NE;
13164 else if (NE > ResNE)
13165 NE = ResNE;
13166
13167 if (N->getNumValues() == 2) {
13168 SmallVector<SDValue, 8> Scalars0, Scalars1;
13169 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13170 EVT VT1 = N->getValueType(1);
13171 EVT EltVT1 = VT1.getVectorElementType();
13172
13173 unsigned i;
13174 for (i = 0; i != NE; ++i) {
13175 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13176 SDValue Operand = N->getOperand(j);
13177 EVT OperandVT = Operand.getValueType();
13178
13179 // A vector operand; extract a single element.
13180 EVT OperandEltVT = OperandVT.getVectorElementType();
13181 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13182 }
13183
13184 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13185 Scalars0.push_back(EltOp);
13186 Scalars1.push_back(EltOp.getValue(1));
13187 }
13188
13189 for (; i < ResNE; ++i) {
13190 Scalars0.push_back(getUNDEF(EltVT));
13191 Scalars1.push_back(getUNDEF(EltVT1));
13192 }
13193
13194 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13195 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13196 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13197 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13198 return getMergeValues({Vec0, Vec1}, dl);
13199 }
13200
13201 assert(N->getNumValues() == 1 &&
13202 "Can't unroll a vector with multiple results!");
13203
13205 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13206
13207 unsigned i;
13208 for (i= 0; i != NE; ++i) {
13209 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13210 SDValue Operand = N->getOperand(j);
13211 EVT OperandVT = Operand.getValueType();
13212 if (OperandVT.isVector()) {
13213 // A vector operand; extract a single element.
13214 EVT OperandEltVT = OperandVT.getVectorElementType();
13215 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13216 } else {
13217 // A scalar operand; just use it as is.
13218 Operands[j] = Operand;
13219 }
13220 }
13221
13222 switch (N->getOpcode()) {
13223 default: {
13224 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13225 N->getFlags()));
13226 break;
13227 }
13228 case ISD::VSELECT:
13229 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13230 break;
13231 case ISD::SHL:
13232 case ISD::SRA:
13233 case ISD::SRL:
13234 case ISD::ROTL:
13235 case ISD::ROTR:
13236 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13238 Operands[1])));
13239 break;
13241 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13242 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13243 Operands[0],
13244 getValueType(ExtVT)));
13245 break;
13246 }
13247 case ISD::ADDRSPACECAST: {
13248 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13249 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13250 ASC->getSrcAddressSpace(),
13251 ASC->getDestAddressSpace()));
13252 break;
13253 }
13254 }
13255 }
13256
13257 for (; i < ResNE; ++i)
13258 Scalars.push_back(getUNDEF(EltVT));
13259
13260 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13261 return getBuildVector(VecVT, dl, Scalars);
13262}
13263
13264std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13265 SDNode *N, unsigned ResNE) {
13266 unsigned Opcode = N->getOpcode();
13267 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13268 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13269 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13270 "Expected an overflow opcode");
13271
13272 EVT ResVT = N->getValueType(0);
13273 EVT OvVT = N->getValueType(1);
13274 EVT ResEltVT = ResVT.getVectorElementType();
13275 EVT OvEltVT = OvVT.getVectorElementType();
13276 SDLoc dl(N);
13277
13278 // If ResNE is 0, fully unroll the vector op.
13279 unsigned NE = ResVT.getVectorNumElements();
13280 if (ResNE == 0)
13281 ResNE = NE;
13282 else if (NE > ResNE)
13283 NE = ResNE;
13284
13285 SmallVector<SDValue, 8> LHSScalars;
13286 SmallVector<SDValue, 8> RHSScalars;
13287 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13288 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13289
13290 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13291 SDVTList VTs = getVTList(ResEltVT, SVT);
13292 SmallVector<SDValue, 8> ResScalars;
13293 SmallVector<SDValue, 8> OvScalars;
13294 for (unsigned i = 0; i < NE; ++i) {
13295 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13296 SDValue Ov =
13297 getSelect(dl, OvEltVT, Res.getValue(1),
13298 getBoolConstant(true, dl, OvEltVT, ResVT),
13299 getConstant(0, dl, OvEltVT));
13300
13301 ResScalars.push_back(Res);
13302 OvScalars.push_back(Ov);
13303 }
13304
13305 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13306 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13307
13308 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13309 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13310 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13311 getBuildVector(NewOvVT, dl, OvScalars));
13312}
13313
13316 unsigned Bytes,
13317 int Dist) const {
13318 if (LD->isVolatile() || Base->isVolatile())
13319 return false;
13320 // TODO: probably too restrictive for atomics, revisit
13321 if (!LD->isSimple())
13322 return false;
13323 if (LD->isIndexed() || Base->isIndexed())
13324 return false;
13325 if (LD->getChain() != Base->getChain())
13326 return false;
13327 EVT VT = LD->getMemoryVT();
13328 if (VT.getSizeInBits() / 8 != Bytes)
13329 return false;
13330
13331 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13332 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13333
13334 int64_t Offset = 0;
13335 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13336 return (Dist * (int64_t)Bytes == Offset);
13337 return false;
13338}
13339
13340/// InferPtrAlignment - Infer alignment of a load / store address. Return
13341/// std::nullopt if it cannot be inferred.
13343 // If this is a GlobalAddress + cst, return the alignment.
13344 const GlobalValue *GV = nullptr;
13345 int64_t GVOffset = 0;
13346 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13347 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13348 KnownBits Known(PtrWidth);
13350 unsigned AlignBits = Known.countMinTrailingZeros();
13351 if (AlignBits)
13352 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13353 }
13354
13355 // If this is a direct reference to a stack slot, use information about the
13356 // stack slot's alignment.
13357 int FrameIdx = INT_MIN;
13358 int64_t FrameOffset = 0;
13359 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
13360 FrameIdx = FI->getIndex();
13361 } else if (isBaseWithConstantOffset(Ptr) &&
13362 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
13363 // Handle FI+Cst
13364 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13365 FrameOffset = Ptr.getConstantOperandVal(1);
13366 }
13367
13368 if (FrameIdx != INT_MIN) {
13370 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13371 }
13372
13373 return std::nullopt;
13374}
13375
13376/// Split the scalar node with EXTRACT_ELEMENT using the provided
13377/// VTs and return the low/high part.
13378std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13379 const SDLoc &DL,
13380 const EVT &LoVT,
13381 const EVT &HiVT) {
13382 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13383 "Split node must be a scalar type");
13384 SDValue Lo =
13386 SDValue Hi =
13388 return std::make_pair(Lo, Hi);
13389}
13390
13391/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13392/// which is split (or expanded) into two not necessarily identical pieces.
13393std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13394 // Currently all types are split in half.
13395 EVT LoVT, HiVT;
13396 if (!VT.isVector())
13397 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13398 else
13399 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13400
13401 return std::make_pair(LoVT, HiVT);
13402}
13403
13404/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13405/// type, dependent on an enveloping VT that has been split into two identical
13406/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13407std::pair<EVT, EVT>
13409 bool *HiIsEmpty) const {
13410 EVT EltTp = VT.getVectorElementType();
13411 // Examples:
13412 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13413 // custom VL=9 with enveloping VL=8/8 yields 8/1
13414 // custom VL=10 with enveloping VL=8/8 yields 8/2
13415 // etc.
13416 ElementCount VTNumElts = VT.getVectorElementCount();
13417 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13418 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13419 "Mixing fixed width and scalable vectors when enveloping a type");
13420 EVT LoVT, HiVT;
13421 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13422 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13423 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13424 *HiIsEmpty = false;
13425 } else {
13426 // Flag that hi type has zero storage size, but return split envelop type
13427 // (this would be easier if vector types with zero elements were allowed).
13428 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13429 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13430 *HiIsEmpty = true;
13431 }
13432 return std::make_pair(LoVT, HiVT);
13433}
13434
13435/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13436/// low/high part.
13437std::pair<SDValue, SDValue>
13438SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13439 const EVT &HiVT) {
13440 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13441 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13442 "Splitting vector with an invalid mixture of fixed and scalable "
13443 "vector types");
13445 N.getValueType().getVectorMinNumElements() &&
13446 "More vector elements requested than available!");
13447 SDValue Lo, Hi;
13448 Lo = getExtractSubvector(DL, LoVT, N, 0);
13449 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13450 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13451 // IDX with the runtime scaling factor of the result vector type. For
13452 // fixed-width result vectors, that runtime scaling factor is 1.
13455 return std::make_pair(Lo, Hi);
13456}
13457
13458std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13459 const SDLoc &DL) {
13460 // Split the vector length parameter.
13461 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13462 EVT VT = N.getValueType();
13464 "Expecting the mask to be an evenly-sized vector");
13465 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13466 SDValue HalfNumElts =
13467 VecVT.isFixedLengthVector()
13468 ? getConstant(HalfMinNumElts, DL, VT)
13469 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13470 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13471 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13472 return std::make_pair(Lo, Hi);
13473}
13474
13475/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13477 EVT VT = N.getValueType();
13480 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13481}
13482
13485 unsigned Start, unsigned Count,
13486 EVT EltVT) {
13487 EVT VT = Op.getValueType();
13488 if (Count == 0)
13489 Count = VT.getVectorNumElements();
13490 if (EltVT == EVT())
13491 EltVT = VT.getVectorElementType();
13492 SDLoc SL(Op);
13493 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13494 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13495 }
13496}
13497
13498// getAddressSpace - Return the address space this GlobalAddress belongs to.
13500 return getGlobal()->getType()->getAddressSpace();
13501}
13502
13505 return Val.MachineCPVal->getType();
13506 return Val.ConstVal->getType();
13507}
13508
13509bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13510 unsigned &SplatBitSize,
13511 bool &HasAnyUndefs,
13512 unsigned MinSplatBits,
13513 bool IsBigEndian) const {
13514 EVT VT = getValueType(0);
13515 assert(VT.isVector() && "Expected a vector type");
13516 unsigned VecWidth = VT.getSizeInBits();
13517 if (MinSplatBits > VecWidth)
13518 return false;
13519
13520 // FIXME: The widths are based on this node's type, but build vectors can
13521 // truncate their operands.
13522 SplatValue = APInt(VecWidth, 0);
13523 SplatUndef = APInt(VecWidth, 0);
13524
13525 // Get the bits. Bits with undefined values (when the corresponding element
13526 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13527 // in SplatValue. If any of the values are not constant, give up and return
13528 // false.
13529 unsigned int NumOps = getNumOperands();
13530 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13531 unsigned EltWidth = VT.getScalarSizeInBits();
13532
13533 for (unsigned j = 0; j < NumOps; ++j) {
13534 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13535 SDValue OpVal = getOperand(i);
13536 unsigned BitPos = j * EltWidth;
13537
13538 if (OpVal.isUndef())
13539 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13540 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13541 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13542 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13543 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13544 else
13545 return false;
13546 }
13547
13548 // The build_vector is all constants or undefs. Find the smallest element
13549 // size that splats the vector.
13550 HasAnyUndefs = (SplatUndef != 0);
13551
13552 // FIXME: This does not work for vectors with elements less than 8 bits.
13553 while (VecWidth > 8) {
13554 // If we can't split in half, stop here.
13555 if (VecWidth & 1)
13556 break;
13557
13558 unsigned HalfSize = VecWidth / 2;
13559 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13560 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13561 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13562 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13563
13564 // If the two halves do not match (ignoring undef bits), stop here.
13565 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13566 MinSplatBits > HalfSize)
13567 break;
13568
13569 SplatValue = HighValue | LowValue;
13570 SplatUndef = HighUndef & LowUndef;
13571
13572 VecWidth = HalfSize;
13573 }
13574
13575 // FIXME: The loop above only tries to split in halves. But if the input
13576 // vector for example is <3 x i16> it wouldn't be able to detect a
13577 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13578 // optimizations. I guess that back in the days when this helper was created
13579 // vectors normally was power-of-2 sized.
13580
13581 SplatBitSize = VecWidth;
13582 return true;
13583}
13584
13586 BitVector *UndefElements) const {
13587 unsigned NumOps = getNumOperands();
13588 if (UndefElements) {
13589 UndefElements->clear();
13590 UndefElements->resize(NumOps);
13591 }
13592 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13593 if (!DemandedElts)
13594 return SDValue();
13595 SDValue Splatted;
13596 for (unsigned i = 0; i != NumOps; ++i) {
13597 if (!DemandedElts[i])
13598 continue;
13599 SDValue Op = getOperand(i);
13600 if (Op.isUndef()) {
13601 if (UndefElements)
13602 (*UndefElements)[i] = true;
13603 } else if (!Splatted) {
13604 Splatted = Op;
13605 } else if (Splatted != Op) {
13606 return SDValue();
13607 }
13608 }
13609
13610 if (!Splatted) {
13611 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13612 assert(getOperand(FirstDemandedIdx).isUndef() &&
13613 "Can only have a splat without a constant for all undefs.");
13614 return getOperand(FirstDemandedIdx);
13615 }
13616
13617 return Splatted;
13618}
13619
13621 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13622 return getSplatValue(DemandedElts, UndefElements);
13623}
13624
13626 SmallVectorImpl<SDValue> &Sequence,
13627 BitVector *UndefElements) const {
13628 unsigned NumOps = getNumOperands();
13629 Sequence.clear();
13630 if (UndefElements) {
13631 UndefElements->clear();
13632 UndefElements->resize(NumOps);
13633 }
13634 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13635 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13636 return false;
13637
13638 // Set the undefs even if we don't find a sequence (like getSplatValue).
13639 if (UndefElements)
13640 for (unsigned I = 0; I != NumOps; ++I)
13641 if (DemandedElts[I] && getOperand(I).isUndef())
13642 (*UndefElements)[I] = true;
13643
13644 // Iteratively widen the sequence length looking for repetitions.
13645 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13646 Sequence.append(SeqLen, SDValue());
13647 for (unsigned I = 0; I != NumOps; ++I) {
13648 if (!DemandedElts[I])
13649 continue;
13650 SDValue &SeqOp = Sequence[I % SeqLen];
13652 if (Op.isUndef()) {
13653 if (!SeqOp)
13654 SeqOp = Op;
13655 continue;
13656 }
13657 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13658 Sequence.clear();
13659 break;
13660 }
13661 SeqOp = Op;
13662 }
13663 if (!Sequence.empty())
13664 return true;
13665 }
13666
13667 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13668 return false;
13669}
13670
13672 BitVector *UndefElements) const {
13673 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13674 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13675}
13676
13679 BitVector *UndefElements) const {
13680 return dyn_cast_or_null<ConstantSDNode>(
13681 getSplatValue(DemandedElts, UndefElements));
13682}
13683
13686 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13687}
13688
13691 BitVector *UndefElements) const {
13692 return dyn_cast_or_null<ConstantFPSDNode>(
13693 getSplatValue(DemandedElts, UndefElements));
13694}
13695
13698 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
13699}
13700
13701int32_t
13703 uint32_t BitWidth) const {
13704 if (ConstantFPSDNode *CN =
13705 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
13706 bool IsExact;
13707 APSInt IntVal(BitWidth);
13708 const APFloat &APF = CN->getValueAPF();
13709 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13710 APFloat::opOK ||
13711 !IsExact)
13712 return -1;
13713
13714 return IntVal.exactLogBase2();
13715 }
13716 return -1;
13717}
13718
13720 bool IsLittleEndian, unsigned DstEltSizeInBits,
13721 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13722 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13723 if (!isConstant())
13724 return false;
13725
13726 unsigned NumSrcOps = getNumOperands();
13727 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13728 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13729 "Invalid bitcast scale");
13730
13731 // Extract raw src bits.
13732 SmallVector<APInt> SrcBitElements(NumSrcOps,
13733 APInt::getZero(SrcEltSizeInBits));
13734 BitVector SrcUndeElements(NumSrcOps, false);
13735
13736 for (unsigned I = 0; I != NumSrcOps; ++I) {
13738 if (Op.isUndef()) {
13739 SrcUndeElements.set(I);
13740 continue;
13741 }
13742 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13743 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13744 assert((CInt || CFP) && "Unknown constant");
13745 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13746 : CFP->getValueAPF().bitcastToAPInt();
13747 }
13748
13749 // Recast to dst width.
13750 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13751 SrcBitElements, UndefElements, SrcUndeElements);
13752 return true;
13753}
13754
13755void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13756 unsigned DstEltSizeInBits,
13757 SmallVectorImpl<APInt> &DstBitElements,
13758 ArrayRef<APInt> SrcBitElements,
13759 BitVector &DstUndefElements,
13760 const BitVector &SrcUndefElements) {
13761 unsigned NumSrcOps = SrcBitElements.size();
13762 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13763 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13764 "Invalid bitcast scale");
13765 assert(NumSrcOps == SrcUndefElements.size() &&
13766 "Vector size mismatch");
13767
13768 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13769 DstUndefElements.clear();
13770 DstUndefElements.resize(NumDstOps, false);
13771 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13772
13773 // Concatenate src elements constant bits together into dst element.
13774 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13775 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13776 for (unsigned I = 0; I != NumDstOps; ++I) {
13777 DstUndefElements.set(I);
13778 APInt &DstBits = DstBitElements[I];
13779 for (unsigned J = 0; J != Scale; ++J) {
13780 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13781 if (SrcUndefElements[Idx])
13782 continue;
13783 DstUndefElements.reset(I);
13784 const APInt &SrcBits = SrcBitElements[Idx];
13785 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13786 "Illegal constant bitwidths");
13787 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13788 }
13789 }
13790 return;
13791 }
13792
13793 // Split src element constant bits into dst elements.
13794 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13795 for (unsigned I = 0; I != NumSrcOps; ++I) {
13796 if (SrcUndefElements[I]) {
13797 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13798 continue;
13799 }
13800 const APInt &SrcBits = SrcBitElements[I];
13801 for (unsigned J = 0; J != Scale; ++J) {
13802 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13803 APInt &DstBits = DstBitElements[Idx];
13804 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13805 }
13806 }
13807}
13808
13810 for (const SDValue &Op : op_values()) {
13811 unsigned Opc = Op.getOpcode();
13812 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13813 return false;
13814 }
13815 return true;
13816}
13817
13818std::optional<std::pair<APInt, APInt>>
13820 unsigned NumOps = getNumOperands();
13821 if (NumOps < 2)
13822 return std::nullopt;
13823
13824 if (!isa<ConstantSDNode>(getOperand(0)) ||
13825 !isa<ConstantSDNode>(getOperand(1)))
13826 return std::nullopt;
13827
13828 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13829 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13830 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13831
13832 if (Stride.isZero())
13833 return std::nullopt;
13834
13835 for (unsigned i = 2; i < NumOps; ++i) {
13836 if (!isa<ConstantSDNode>(getOperand(i)))
13837 return std::nullopt;
13838
13839 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13840 if (Val != (Start + (Stride * i)))
13841 return std::nullopt;
13842 }
13843
13844 return std::make_pair(Start, Stride);
13845}
13846
13848 // Find the first non-undef value in the shuffle mask.
13849 unsigned i, e;
13850 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
13851 /* search */;
13852
13853 // If all elements are undefined, this shuffle can be considered a splat
13854 // (although it should eventually get simplified away completely).
13855 if (i == e)
13856 return true;
13857
13858 // Make sure all remaining elements are either undef or the same as the first
13859 // non-undef value.
13860 for (int Idx = Mask[i]; i != e; ++i)
13861 if (Mask[i] >= 0 && Mask[i] != Idx)
13862 return false;
13863 return true;
13864}
13865
13866// Returns true if it is a constant integer BuildVector or constant integer,
13867// possibly hidden by a bitcast.
13869 SDValue N, bool AllowOpaques) const {
13871
13872 if (auto *C = dyn_cast<ConstantSDNode>(N))
13873 return AllowOpaques || !C->isOpaque();
13874
13876 return true;
13877
13878 // Treat a GlobalAddress supporting constant offset folding as a
13879 // constant integer.
13880 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
13881 if (GA->getOpcode() == ISD::GlobalAddress &&
13882 TLI->isOffsetFoldingLegal(GA))
13883 return true;
13884
13885 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13886 isa<ConstantSDNode>(N.getOperand(0)))
13887 return true;
13888 return false;
13889}
13890
13891// Returns true if it is a constant float BuildVector or constant float.
13893 if (isa<ConstantFPSDNode>(N))
13894 return true;
13895
13897 return true;
13898
13899 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13900 isa<ConstantFPSDNode>(N.getOperand(0)))
13901 return true;
13902
13903 return false;
13904}
13905
13906std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
13907 ConstantSDNode *Const =
13908 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
13909 if (!Const)
13910 return std::nullopt;
13911
13912 EVT VT = N->getValueType(0);
13913 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
13914 switch (TLI->getBooleanContents(N.getValueType())) {
13916 if (CVal.isOne())
13917 return true;
13918 if (CVal.isZero())
13919 return false;
13920 return std::nullopt;
13922 if (CVal.isAllOnes())
13923 return true;
13924 if (CVal.isZero())
13925 return false;
13926 return std::nullopt;
13928 return CVal[0];
13929 }
13930 llvm_unreachable("Unknown BooleanContent enum");
13931}
13932
13933void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13934 assert(!Node->OperandList && "Node already has operands");
13936 "too many operands to fit into SDNode");
13937 SDUse *Ops = OperandRecycler.allocate(
13938 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13939
13940 bool IsDivergent = false;
13941 for (unsigned I = 0; I != Vals.size(); ++I) {
13942 Ops[I].setUser(Node);
13943 Ops[I].setInitial(Vals[I]);
13944 EVT VT = Ops[I].getValueType();
13945
13946 // Skip Chain. It does not carry divergence.
13947 if (VT != MVT::Other &&
13948 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
13949 Ops[I].getNode()->isDivergent()) {
13950 IsDivergent = true;
13951 }
13952 }
13953 Node->NumOperands = Vals.size();
13954 Node->OperandList = Ops;
13955 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13956 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13957 Node->SDNodeBits.IsDivergent = IsDivergent;
13958 }
13960}
13961
13964 size_t Limit = SDNode::getMaxNumOperands();
13965 while (Vals.size() > Limit) {
13966 unsigned SliceIdx = Vals.size() - Limit;
13967 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13968 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13969 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13970 Vals.emplace_back(NewTF);
13971 }
13972 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13973}
13974
13976 EVT VT, SDNodeFlags Flags) {
13977 switch (Opcode) {
13978 default:
13979 return SDValue();
13980 case ISD::ADD:
13981 case ISD::OR:
13982 case ISD::XOR:
13983 case ISD::UMAX:
13984 return getConstant(0, DL, VT);
13985 case ISD::MUL:
13986 return getConstant(1, DL, VT);
13987 case ISD::AND:
13988 case ISD::UMIN:
13989 return getAllOnesConstant(DL, VT);
13990 case ISD::SMAX:
13992 case ISD::SMIN:
13994 case ISD::FADD:
13995 // If flags allow, prefer positive zero since it's generally cheaper
13996 // to materialize on most targets.
13997 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
13998 case ISD::FMUL:
13999 return getConstantFP(1.0, DL, VT);
14000 case ISD::FMINNUM:
14001 case ISD::FMAXNUM: {
14002 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14003 const fltSemantics &Semantics = VT.getFltSemantics();
14004 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14005 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14006 APFloat::getLargest(Semantics);
14007 if (Opcode == ISD::FMAXNUM)
14008 NeutralAF.changeSign();
14009
14010 return getConstantFP(NeutralAF, DL, VT);
14011 }
14012 case ISD::FMINIMUM:
14013 case ISD::FMAXIMUM: {
14014 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14015 const fltSemantics &Semantics = VT.getFltSemantics();
14016 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14017 : APFloat::getLargest(Semantics);
14018 if (Opcode == ISD::FMAXIMUM)
14019 NeutralAF.changeSign();
14020
14021 return getConstantFP(NeutralAF, DL, VT);
14022 }
14023
14024 }
14025}
14026
14027/// Helper used to make a call to a library function that has one argument of
14028/// pointer type.
14029///
14030/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14031/// used to get or set floating-point state. They have one argument of pointer
14032/// type, which points to the memory region containing bits of the
14033/// floating-point state. The value returned by such function is ignored in the
14034/// created call.
14035///
14036/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14037/// \param Ptr Pointer used to save/load state.
14038/// \param InChain Ingoing token chain.
14039/// \returns Outgoing chain token.
14041 SDValue InChain,
14042 const SDLoc &DLoc) {
14043 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14045 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14046 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
14047 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
14048 TLI->getPointerTy(getDataLayout()));
14050 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14051 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
14052 std::move(Args));
14053 return TLI->LowerCallTo(CLI).second;
14054}
14055
14057 assert(From && To && "Invalid SDNode; empty source SDValue?");
14058 auto I = SDEI.find(From);
14059 if (I == SDEI.end())
14060 return;
14061
14062 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14063 // the iterator, hence the need to make a copy to prevent a use-after-free.
14064 NodeExtraInfo NEI = I->second;
14065 if (LLVM_LIKELY(!NEI.PCSections)) {
14066 // No deep copy required for the types of extra info set.
14067 //
14068 // FIXME: Investigate if other types of extra info also need deep copy. This
14069 // depends on the types of nodes they can be attached to: if some extra info
14070 // is only ever attached to nodes where a replacement To node is always the
14071 // node where later use and propagation of the extra info has the intended
14072 // semantics, no deep copy is required.
14073 SDEI[To] = std::move(NEI);
14074 return;
14075 }
14076
14077 const SDNode *EntrySDN = getEntryNode().getNode();
14078
14079 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14080 // through the replacement of From with To. Otherwise, replacements of a node
14081 // (From) with more complex nodes (To and its operands) may result in lost
14082 // extra info where the root node (To) is insignificant in further propagating
14083 // and using extra info when further lowering to MIR.
14084 //
14085 // In the first step pre-populate the visited set with the nodes reachable
14086 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14087 // DAG that is not new and should be left untouched.
14088 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14089 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14090 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14091 if (MaxDepth == 0) {
14092 // Remember this node in case we need to increase MaxDepth and continue
14093 // populating FromReach from this node.
14094 Leafs.emplace_back(N);
14095 return;
14096 }
14097 if (!FromReach.insert(N).second)
14098 return;
14099 for (const SDValue &Op : N->op_values())
14100 Self(Self, Op.getNode(), MaxDepth - 1);
14101 };
14102
14103 // Copy extra info to To and all its transitive operands (that are new).
14105 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14106 if (FromReach.contains(N))
14107 return true;
14108 if (!Visited.insert(N).second)
14109 return true;
14110 if (EntrySDN == N)
14111 return false;
14112 for (const SDValue &Op : N->op_values()) {
14113 if (N == To && Op.getNode() == EntrySDN) {
14114 // Special case: New node's operand is the entry node; just need to
14115 // copy extra info to new node.
14116 break;
14117 }
14118 if (!Self(Self, Op.getNode()))
14119 return false;
14120 }
14121 // Copy only if entry node was not reached.
14122 SDEI[N] = NEI;
14123 return true;
14124 };
14125
14126 // We first try with a lower MaxDepth, assuming that the path to common
14127 // operands between From and To is relatively short. This significantly
14128 // improves performance in the common case. The initial MaxDepth is big
14129 // enough to avoid retry in the common case; the last MaxDepth is large
14130 // enough to avoid having to use the fallback below (and protects from
14131 // potential stack exhaustion from recursion).
14132 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14133 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14134 // StartFrom is the previous (or initial) set of leafs reachable at the
14135 // previous maximum depth.
14137 std::swap(StartFrom, Leafs);
14138 for (const SDNode *N : StartFrom)
14139 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14140 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14141 return;
14142 // This should happen very rarely (reached the entry node).
14143 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14144 assert(!Leafs.empty());
14145 }
14146
14147 // This should not happen - but if it did, that means the subgraph reachable
14148 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14149 // could not visit all reachable common operands. Consequently, we were able
14150 // to reach the entry node.
14151 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14152 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14153 // Best-effort fallback if assertions disabled.
14154 SDEI[To] = std::move(NEI);
14155}
14156
14157#ifndef NDEBUG
14158static void checkForCyclesHelper(const SDNode *N,
14161 const llvm::SelectionDAG *DAG) {
14162 // If this node has already been checked, don't check it again.
14163 if (Checked.count(N))
14164 return;
14165
14166 // If a node has already been visited on this depth-first walk, reject it as
14167 // a cycle.
14168 if (!Visited.insert(N).second) {
14169 errs() << "Detected cycle in SelectionDAG\n";
14170 dbgs() << "Offending node:\n";
14171 N->dumprFull(DAG); dbgs() << "\n";
14172 abort();
14173 }
14174
14175 for (const SDValue &Op : N->op_values())
14176 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14177
14178 Checked.insert(N);
14179 Visited.erase(N);
14180}
14181#endif
14182
14184 const llvm::SelectionDAG *DAG,
14185 bool force) {
14186#ifndef NDEBUG
14187 bool check = force;
14188#ifdef EXPENSIVE_CHECKS
14189 check = true;
14190#endif // EXPENSIVE_CHECKS
14191 if (check) {
14192 assert(N && "Checking nonexistent SDNode");
14195 checkForCyclesHelper(N, visited, checked, DAG);
14196 }
14197#endif // !NDEBUG
14198}
14199
14200void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14201 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14202}
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.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-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...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
Given that RA is a live propagate it s liveness to any other values it uses(according to Uses). void DeadArgumentEliminationPass
Given that RA is a live value
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint32_t Index
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
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.
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:546
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.
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#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 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:119
This file describes how to lower LLVM code to machine code.
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.
Value * RHS
Value * LHS
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
bool isNaN() const
Definition: APFloat.h:1447
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
bool isInfinity() const
Definition: APFloat.h:1446
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
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1319
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
Recycle small arrays allocated from a BumpPtrAllocator.
Definition: ArrayRecycler.h:28
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
iterator begin() const
Definition: ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:142
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:191
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
unsigned getTargetFlags() const
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
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:149
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:124
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
bool isMachineConstantPoolEntry() const
LLVM_ABI Type * getType() const
This class represents a range of values.
Definition: ConstantRange.h:47
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.
Definition: Constants.cpp:1713
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
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.
This class represents an Operation in the Expression.
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.
Definition: DataLayout.cpp:850
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:842
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
Definition: DataLayout.cpp:742
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:846
A debug info location.
Definition: DebugLoc.h:124
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:124
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:330
MachineBasicBlock * MBB
MBB - The current block.
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
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:359
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.
Definition: GlobalValue.h:265
unsigned getAddressSpace() const
Definition: GlobalValue.h:207
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:663
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:296
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.
constexpr bool isValid() const
Definition: LowLevelType.h:146
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)
TypeSize getValue() const
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)
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
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.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, Align base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
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.
LocationSize getSize() const
Return the size in bytes of the memory reference.
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:229
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.
Definition: DerivedTypes.h:700
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:720
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:740
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.
Definition: PointerUnion.h:142
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.
Definition: SelectionDAG.h:163
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:192
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.
ArrayRef< SDDbgValue * > getSDDbgValues(const SDNode *Node) const
Definition: SelectionDAG.h:198
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.
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
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
EVT getValueType() const
Convenience function for get().getValueType().
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.
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 bool isTargetMemoryOpcode(unsigned Opcode) const
Returns true if a node with the given target-specific opcode has a memory operand.
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
virtual SDValue EmitTargetCodeForMemset(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo) const
Emit target-specific code that performs a memset.
virtual SDValue EmitTargetCodeForMemmove(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memmove.
virtual SDValue EmitTargetCodeForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Op1, SDValue Op2, SDValue Op3, Align Alignment, bool isVolatile, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const
Emit target-specific code that performs a memcpy.
SDNodeFlags getFlags() const
Definition: SelectionDAG.h:390
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:229
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.
Definition: SelectionDAG.h:941
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.
Definition: SelectionDAG.h:578
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
Definition: SelectionDAG.h:500
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 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.
Definition: SelectionDAG.h:963
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.
Definition: SelectionDAG.h:956
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 std::optional< uint64_t > 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 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
Definition: SelectionDAG.h:504
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:459
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)
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...
Definition: SelectionDAG.h:398
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
Definition: SelectionDAG.h:558
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.
Definition: SelectionDAG.h:868
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 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
Definition: SelectionDAG.h:498
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
Definition: SelectionDAG.h:506
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)
Definition: SelectionDAG.h:902
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 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
Definition: SelectionDAG.h:499
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()
Definition: SelectionDAG.h:570
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
Definition: SelectionDAG.h:566
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)
Definition: SelectionDAG.h:707
const TargetLibraryInfo & getLibInfo() const
Definition: SelectionDAG.h:505
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
Definition: SelectionDAG.h:493
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.
LLVM_ABI std::optional< uint64_t > 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...
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
Definition: SelectionDAG.h:885
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.
LLVM_ABI std::optional< uint64_t > 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 ~SelectionDAG()
LLVMContext * getContext() const
Definition: SelectionDAG.h:511
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.
Definition: SelectionDAG.h:587
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.
Definition: SelectionDAG.h:581
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.
Definition: SelectionDAG.h:918
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
Definition: SelectionDAG.h:561
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...
Definition: SmallPtrSet.h:380
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:418
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:705
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void reserve(size_type N)
Definition: SmallVector.h:664
iterator erase(const_iterator CI)
Definition: SmallVector.h:738
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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.
const VecDesc * getVectorMappingInfo(StringRef F, const ElementCount &VF, bool Masked) const
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
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.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
MVT getVectorIdxTy(const DataLayout &DL) const
Returns the type to be used for the index operand of: ISD::INSERT_VECTOR_ELT, ISD::EXTRACT_VECTOR_ELT...
const TargetMachine & getTargetMachine() const
virtual 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 isCommutativeBinOp(unsigned Opcode) const
Returns true if the opcode is a commutative binary operation.
virtual ISD::NodeType getExtendForAtomicOps() const
Returns how the platform's atomic operations are extended (ZERO_EXTEND, SIGN_EXTEND,...
const char * getMemcpyName() const
EVT getShiftAmountTy(EVT LHSTy, const DataLayout &DL) const
Returns the type for the shift amount of a shift opcode.
virtual bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, unsigned Index) const
Return true if EXTRACT_SUBVECTOR is cheap for extracting this result type from this source type with ...
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 bool shouldPreservePtrArith(const Function &F, EVT PtrVT) const
True if target has some particular form of dealing with pointer arithmetic semantics for pointers wit...
virtual EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, EVT VT) const
Return the ValueType of the result of SETCC operations.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
BooleanContent getBooleanContents(bool isVec, bool isFloat) const
For targets without i1 registers, this gives the nature of the high-bits of boolean values held in ty...
bool isCondCodeLegal(ISD::CondCode CC, MVT VT) const
Return true if the specified condition code is legal for a comparison of the specified types on this ...
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...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
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.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
virtual unsigned getVectorIdxWidth(const DataLayout &DL) const
Returns the type to be used for the index operand vector operations.
unsigned getVectorTypeBreakdown(LLVMContext &Context, EVT VT, EVT &IntermediateVT, unsigned &NumIntermediates, MVT &RegisterVT) const
Vector types are broken down into some number of legal first class types.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual void computeKnownBitsForFrameIndex(int FIOp, KnownBits &Known, const MachineFunction &MF) const
Determine which of the bits of FrameIndex FIOp are known to be 0.
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.
virtual unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
This method can be implemented by targets that want to expose additional information about sign bits ...
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known, const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth=0) const
Determine which of the bits specified in Mask are known to be either zero or one and return them in t...
virtual bool isSDNodeSourceOfDivergence(const SDNode *N, FunctionLoweringInfo *FLI, UniformityInfo *UA) const
virtual bool isSDNodeAlwaysUniform(const SDNode *N) const
virtual bool isSplatValueForTargetNode(SDValue Op, const APInt &DemandedElts, APInt &UndefElts, const SelectionDAG &DAG, unsigned Depth=0) const
Return true if vector Op has the same value across all DemandedElts, indicating any elements which ma...
virtual bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const
Return true if folding a constant offset with the given GlobalAddress is legal.
virtual const Constant * getTargetConstantFromLoad(LoadSDNode *LD) const
This method returns the constant pool value that will be loaded by LD.
virtual bool isGAPlusOffset(SDNode *N, const GlobalValue *&GA, int64_t &Offset) const
Returns true (and the GlobalValue and the offset) if the node is a GlobalAddress + offset.
virtual bool isGuaranteedNotToBeUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, unsigned Depth) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
virtual bool canCreateUndefOrPoisonForTargetNode(SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, bool PoisonOnly, bool ConsiderFlags, unsigned Depth) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:83
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 TargetFrameLowering * getFrameLowering() 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:608
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:346
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
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:272
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:203
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:233
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:172
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:180
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:169
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:240
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
#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
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.
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
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1281
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:504
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1386
@ 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
@ PARTIAL_REDUCE_SMLA
Definition: ISDOpcodes.h:1510
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1458
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
Definition: ISDOpcodes.h:1401
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1491
@ 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
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1379
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:587
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1170
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:765
@ TargetBlockAddress
Definition: ISDOpcodes.h:186
@ ConstantFP
Definition: ISDOpcodes.h:87
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1381
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1351
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1382
@ 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
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1141
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:835
@ ATOMIC_LOAD_USUB_COND
Definition: ISDOpcodes.h:1392
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:511
@ FATAN2
FATAN2 - atan2, inspired by libm.
Definition: ISDOpcodes.h:1020
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:215
@ GlobalAddress
Definition: ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1364
@ 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
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1476
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:410
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1480
@ 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
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1490
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:505
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:985
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1377
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:975
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:249
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1378
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:1018
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1568
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1384
@ GlobalTLSAddress
Definition: ISDOpcodes.h:89
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
Definition: ISDOpcodes.h:1277
@ FrameIndex
Definition: ISDOpcodes.h:90
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1212
@ ATOMIC_LOAD_USUB_SAT
Definition: ISDOpcodes.h:1393
@ PARTIAL_REDUCE_UMLA
Definition: ISDOpcodes.h:1511
@ 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
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1473
@ 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
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1331
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1477
@ 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
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:1002
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1385
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1187
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:347
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1380
@ 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
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1347
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:228
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1492
@ RegisterMask
Definition: ISDOpcodes.h:85
@ 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
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1387
@ 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
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1485
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:695
@ ATOMIC_LOAD_FMAXIMUM
Definition: ISDOpcodes.h:1388
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:756
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1376
@ 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
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1375
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:601
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:1075
@ 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
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:928
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:793
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1448
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1358
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1383
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
Definition: ISDOpcodes.h:1059
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:351
@ ATOMIC_LOAD_FMINIMUM
Definition: ISDOpcodes.h:1389
@ TargetFrameIndex
Definition: ISDOpcodes.h:182
@ ConstantPool
Definition: ISDOpcodes.h:92
@ 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
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1418
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:960
@ 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
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
Definition: ISDOpcodes.h:1413
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1301
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1493
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:994
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1391
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1373
@ 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
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1081
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1374
@ 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
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1117
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1443
@ 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
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1474
@ 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
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1372
@ ExternalSymbol
Definition: ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:1025
@ 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
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:979
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1546
@ 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
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1481
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:838
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1256
@ BlockAddress
Definition: ISDOpcodes.h:94
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1459
@ 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
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1390
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:521
@ PARTIAL_REDUCE_SUMLA
Definition: ISDOpcodes.h:1512
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:360
@ AssertZext
Definition: ISDOpcodes.h:63
@ CALLSEQ_START
CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of a call sequence,...
Definition: ISDOpcodes.h:1250
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1122
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
Definition: ISDOpcodes.h:1086
@ 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)
Definition: ISDOpcodes.h:1756
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.
Definition: ISDOpcodes.h:1743
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...
Definition: ISDOpcodes.h:1748
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...
Definition: ISDOpcodes.h:1647
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.
Definition: ISDOpcodes.h:1634
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,...
Definition: ISDOpcodes.h:1685
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).
Definition: ISDOpcodes.h:1665
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()...
Definition: PatternMatch.h:980
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
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)
Definition: CommandLine.h:444
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:148
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:349
@ 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:1764
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:1744
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:1605
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:2491
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:260
void * PointerTy
Definition: GenericValue.h:21
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
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:270
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:2155
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:663
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:1587
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
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:1751
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:336
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
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:1669
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:1758
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.
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
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.
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
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:1854
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:223
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition: Analysis.cpp:723
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:1916
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:577
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:378
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:858
#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:390
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:507
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:279
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:295
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:345
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:368
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:354
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:380
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:311
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:318
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:287
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:251
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:216
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:323
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.
Definition: ValueTypes.cpp:330
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:331
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:303
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:448
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:294
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:158
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:916
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:248
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:211
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:101
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:117
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:773
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:235
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:427
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1056
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:267
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.
Definition: KnownBits.cpp:495
void makeNegative()
Make this value negative.
Definition: KnownBits.h:112
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:154
KnownBits byteSwap() const
Definition: KnownBits.h:507
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:282
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:86
KnownBits reverseBits() const
Definition: KnownBits.h:511
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:226
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).
Definition: KnownBits.cpp:187
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:165
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:314
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:370
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:104
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
Definition: KnownBits.cpp:228
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:218
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
Definition: KnownBits.cpp:791
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:304
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:173
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.
Definition: KnownBits.cpp:146
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:189
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:138
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:247
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:908
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1073
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1016
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:107
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:960
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
Definition: KnownBits.cpp:787
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:319
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:98
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:273
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:212
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
Definition: KnownBits.cpp:799
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:803
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:160
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:549
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).
Definition: KnownBits.cpp:285
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:205
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
Definition: KnownBits.cpp:795
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.
bool hasNonNeg() const
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.
Definition: SelectionDAG.h:318
DAGUpdateListener *const Next
Definition: SelectionDAG.h:319
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)