LLVM 20.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"
50#include "llvm/IR/Constant.h"
51#include "llvm/IR/Constants.h"
52#include "llvm/IR/DataLayout.h"
54#include "llvm/IR/DebugLoc.h"
56#include "llvm/IR/Function.h"
57#include "llvm/IR/GlobalValue.h"
58#include "llvm/IR/Metadata.h"
59#include "llvm/IR/Type.h"
63#include "llvm/Support/Debug.h"
67#include "llvm/Support/Mutex.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 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
114}
115
116//===----------------------------------------------------------------------===//
117// ConstantFPSDNode Class
118//===----------------------------------------------------------------------===//
119
120/// isExactlyValue - We don't rely on operator== working on double values, as
121/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
122/// As such, this method can be used to do an exact bit-for-bit comparison of
123/// two floating point values.
125 return getValueAPF().bitwiseIsEqual(V);
126}
127
129 const APFloat& Val) {
130 assert(VT.isFloatingPoint() && "Can only convert between FP types");
131
132 // convert modifies in place, so make a copy.
133 APFloat Val2 = APFloat(Val);
134 bool losesInfo;
137 &losesInfo);
138 return !losesInfo;
139}
140
141//===----------------------------------------------------------------------===//
142// ISD Namespace
143//===----------------------------------------------------------------------===//
144
145bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
146 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
147 unsigned EltSize =
148 N->getValueType(0).getVectorElementType().getSizeInBits();
149 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
150 SplatVal = Op0->getAPIntValue().trunc(EltSize);
151 return true;
152 }
153 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
154 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
155 return true;
156 }
157 }
158
159 auto *BV = dyn_cast<BuildVectorSDNode>(N);
160 if (!BV)
161 return false;
162
163 APInt SplatUndef;
164 unsigned SplatBitSize;
165 bool HasUndefs;
166 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
167 // Endianness does not matter here. We are checking for a splat given the
168 // element size of the vector, and if we find such a splat for little endian
169 // layout, then that should be valid also for big endian (as the full vector
170 // size is known to be a multiple of the element size).
171 const bool IsBigEndian = false;
172 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
173 EltSize, IsBigEndian) &&
174 EltSize == SplatBitSize;
175}
176
177// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
178// specializations of the more general isConstantSplatVector()?
179
180bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
181 // Look through a bit convert.
182 while (N->getOpcode() == ISD::BITCAST)
183 N = N->getOperand(0).getNode();
184
185 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
186 APInt SplatVal;
187 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
188 }
189
190 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
191
192 unsigned i = 0, e = N->getNumOperands();
193
194 // Skip over all of the undef values.
195 while (i != e && N->getOperand(i).isUndef())
196 ++i;
197
198 // Do not accept an all-undef vector.
199 if (i == e) return false;
200
201 // Do not accept build_vectors that aren't all constants or which have non-~0
202 // elements. We have to be a bit careful here, as the type of the constant
203 // may not be the same as the type of the vector elements due to type
204 // legalization (the elements are promoted to a legal type for the target and
205 // a vector of a type may be legal when the base element type is not).
206 // We only want to check enough bits to cover the vector elements, because
207 // we care if the resultant vector is all ones, not whether the individual
208 // constants are.
209 SDValue NotZero = N->getOperand(i);
210 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
211 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
212 if (CN->getAPIntValue().countr_one() < EltSize)
213 return false;
214 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
215 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
216 return false;
217 } else
218 return false;
219
220 // Okay, we have at least one ~0 value, check to see if the rest match or are
221 // undefs. Even with the above element type twiddling, this should be OK, as
222 // the same type legalization should have applied to all the elements.
223 for (++i; i != e; ++i)
224 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
225 return false;
226 return true;
227}
228
229bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
230 // Look through a bit convert.
231 while (N->getOpcode() == ISD::BITCAST)
232 N = N->getOperand(0).getNode();
233
234 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
235 APInt SplatVal;
236 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
237 }
238
239 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
240
241 bool IsAllUndef = true;
242 for (const SDValue &Op : N->op_values()) {
243 if (Op.isUndef())
244 continue;
245 IsAllUndef = false;
246 // Do not accept build_vectors that aren't all constants or which have non-0
247 // elements. We have to be a bit careful here, as the type of the constant
248 // may not be the same as the type of the vector elements due to type
249 // legalization (the elements are promoted to a legal type for the target
250 // and a vector of a type may be legal when the base element type is not).
251 // We only want to check enough bits to cover the vector elements, because
252 // we care if the resultant vector is all zeros, not whether the individual
253 // constants are.
254 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
255 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
256 if (CN->getAPIntValue().countr_zero() < EltSize)
257 return false;
258 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
259 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
260 return false;
261 } else
262 return false;
263 }
264
265 // Do not accept an all-undef vector.
266 if (IsAllUndef)
267 return false;
268 return true;
269}
270
272 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
273}
274
276 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
277}
278
280 if (N->getOpcode() != ISD::BUILD_VECTOR)
281 return false;
282
283 for (const SDValue &Op : N->op_values()) {
284 if (Op.isUndef())
285 continue;
286 if (!isa<ConstantSDNode>(Op))
287 return false;
288 }
289 return true;
290}
291
293 if (N->getOpcode() != ISD::BUILD_VECTOR)
294 return false;
295
296 for (const SDValue &Op : N->op_values()) {
297 if (Op.isUndef())
298 continue;
299 if (!isa<ConstantFPSDNode>(Op))
300 return false;
301 }
302 return true;
303}
304
305bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
306 bool Signed) {
307 assert(N->getValueType(0).isVector() && "Expected a vector!");
308
309 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
310 if (EltSize <= NewEltSize)
311 return false;
312
313 if (N->getOpcode() == ISD::ZERO_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 !Signed;
317 }
318 if (N->getOpcode() == ISD::SIGN_EXTEND) {
319 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
320 NewEltSize) &&
321 Signed;
322 }
323 if (N->getOpcode() != ISD::BUILD_VECTOR)
324 return false;
325
326 for (const SDValue &Op : N->op_values()) {
327 if (Op.isUndef())
328 continue;
329 if (!isa<ConstantSDNode>(Op))
330 return false;
331
332 APInt C = Op->getAsAPIntVal().trunc(EltSize);
333 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
334 return false;
335 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
336 return false;
337 }
338
339 return true;
340}
341
343 // Return false if the node has no operands.
344 // This is "logically inconsistent" with the definition of "all" but
345 // is probably the desired behavior.
346 if (N->getNumOperands() == 0)
347 return false;
348 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
349}
350
352 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
353}
354
355template <typename ConstNodeType>
357 std::function<bool(ConstNodeType *)> Match,
358 bool AllowUndefs) {
359 // FIXME: Add support for scalar UNDEF cases?
360 if (auto *C = dyn_cast<ConstNodeType>(Op))
361 return Match(C);
362
363 // FIXME: Add support for vector UNDEF cases?
364 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
365 ISD::SPLAT_VECTOR != Op.getOpcode())
366 return false;
367
368 EVT SVT = Op.getValueType().getScalarType();
369 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
370 if (AllowUndefs && Op.getOperand(i).isUndef()) {
371 if (!Match(nullptr))
372 return false;
373 continue;
374 }
375
376 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
377 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
378 return false;
379 }
380 return true;
381}
382// Build used template types.
383template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
384 SDValue, std::function<bool(ConstantSDNode *)>, bool);
385template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
386 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
387
389 SDValue LHS, SDValue RHS,
390 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
391 bool AllowUndefs, bool AllowTypeMismatch) {
392 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
393 return false;
394
395 // TODO: Add support for scalar UNDEF cases?
396 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
397 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
398 return Match(LHSCst, RHSCst);
399
400 // TODO: Add support for vector UNDEF cases?
401 if (LHS.getOpcode() != RHS.getOpcode() ||
402 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
403 LHS.getOpcode() != ISD::SPLAT_VECTOR))
404 return false;
405
406 EVT SVT = LHS.getValueType().getScalarType();
407 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
408 SDValue LHSOp = LHS.getOperand(i);
409 SDValue RHSOp = RHS.getOperand(i);
410 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
411 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
412 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
413 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
414 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
415 return false;
416 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
417 LHSOp.getValueType() != RHSOp.getValueType()))
418 return false;
419 if (!Match(LHSCst, RHSCst))
420 return false;
421 }
422 return true;
423}
424
426 switch (VecReduceOpcode) {
427 default:
428 llvm_unreachable("Expected VECREDUCE opcode");
431 case ISD::VP_REDUCE_FADD:
432 case ISD::VP_REDUCE_SEQ_FADD:
433 return ISD::FADD;
436 case ISD::VP_REDUCE_FMUL:
437 case ISD::VP_REDUCE_SEQ_FMUL:
438 return ISD::FMUL;
440 case ISD::VP_REDUCE_ADD:
441 return ISD::ADD;
443 case ISD::VP_REDUCE_MUL:
444 return ISD::MUL;
446 case ISD::VP_REDUCE_AND:
447 return ISD::AND;
449 case ISD::VP_REDUCE_OR:
450 return ISD::OR;
452 case ISD::VP_REDUCE_XOR:
453 return ISD::XOR;
455 case ISD::VP_REDUCE_SMAX:
456 return ISD::SMAX;
458 case ISD::VP_REDUCE_SMIN:
459 return ISD::SMIN;
461 case ISD::VP_REDUCE_UMAX:
462 return ISD::UMAX;
464 case ISD::VP_REDUCE_UMIN:
465 return ISD::UMIN;
467 case ISD::VP_REDUCE_FMAX:
468 return ISD::FMAXNUM;
470 case ISD::VP_REDUCE_FMIN:
471 return ISD::FMINNUM;
473 case ISD::VP_REDUCE_FMAXIMUM:
474 return ISD::FMAXIMUM;
476 case ISD::VP_REDUCE_FMINIMUM:
477 return ISD::FMINIMUM;
478 }
479}
480
481bool ISD::isVPOpcode(unsigned Opcode) {
482 switch (Opcode) {
483 default:
484 return false;
485#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
486 case ISD::VPSD: \
487 return true;
488#include "llvm/IR/VPIntrinsics.def"
489 }
490}
491
492bool ISD::isVPBinaryOp(unsigned Opcode) {
493 switch (Opcode) {
494 default:
495 break;
496#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
497#define VP_PROPERTY_BINARYOP return true;
498#define END_REGISTER_VP_SDNODE(VPSD) break;
499#include "llvm/IR/VPIntrinsics.def"
500 }
501 return false;
502}
503
504bool ISD::isVPReduction(unsigned Opcode) {
505 switch (Opcode) {
506 default:
507 break;
508#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
509#define VP_PROPERTY_REDUCTION(STARTPOS, ...) return true;
510#define END_REGISTER_VP_SDNODE(VPSD) break;
511#include "llvm/IR/VPIntrinsics.def"
512 }
513 return false;
514}
515
516/// The operand position of the vector mask.
517std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
518 switch (Opcode) {
519 default:
520 return std::nullopt;
521#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
522 case ISD::VPSD: \
523 return MASKPOS;
524#include "llvm/IR/VPIntrinsics.def"
525 }
526}
527
528/// The operand position of the explicit vector length parameter.
529std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
530 switch (Opcode) {
531 default:
532 return std::nullopt;
533#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
534 case ISD::VPSD: \
535 return EVLPOS;
536#include "llvm/IR/VPIntrinsics.def"
537 }
538}
539
540std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
541 bool hasFPExcept) {
542 // FIXME: Return strict opcodes in case of fp exceptions.
543 switch (VPOpcode) {
544 default:
545 return std::nullopt;
546#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
547#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
548#define END_REGISTER_VP_SDNODE(VPOPC) break;
549#include "llvm/IR/VPIntrinsics.def"
550 }
551 return std::nullopt;
552}
553
554unsigned ISD::getVPForBaseOpcode(unsigned Opcode) {
555 switch (Opcode) {
556 default:
557 llvm_unreachable("can not translate this Opcode to VP.");
558#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
559#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
560#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
561#include "llvm/IR/VPIntrinsics.def"
562 }
563}
564
566 switch (ExtType) {
567 case ISD::EXTLOAD:
568 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
569 case ISD::SEXTLOAD:
570 return ISD::SIGN_EXTEND;
571 case ISD::ZEXTLOAD:
572 return ISD::ZERO_EXTEND;
573 default:
574 break;
575 }
576
577 llvm_unreachable("Invalid LoadExtType");
578}
579
581 // To perform this operation, we just need to swap the L and G bits of the
582 // operation.
583 unsigned OldL = (Operation >> 2) & 1;
584 unsigned OldG = (Operation >> 1) & 1;
585 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
586 (OldL << 1) | // New G bit
587 (OldG << 2)); // New L bit.
588}
589
591 unsigned Operation = Op;
592 if (isIntegerLike)
593 Operation ^= 7; // Flip L, G, E bits, but not U.
594 else
595 Operation ^= 15; // Flip all of the condition bits.
596
598 Operation &= ~8; // Don't let N and U bits get set.
599
600 return ISD::CondCode(Operation);
601}
602
604 return getSetCCInverseImpl(Op, Type.isInteger());
605}
606
608 bool isIntegerLike) {
609 return getSetCCInverseImpl(Op, isIntegerLike);
610}
611
612/// For an integer comparison, return 1 if the comparison is a signed operation
613/// and 2 if the result is an unsigned comparison. Return zero if the operation
614/// does not depend on the sign of the input (setne and seteq).
615static int isSignedOp(ISD::CondCode Opcode) {
616 switch (Opcode) {
617 default: llvm_unreachable("Illegal integer setcc operation!");
618 case ISD::SETEQ:
619 case ISD::SETNE: return 0;
620 case ISD::SETLT:
621 case ISD::SETLE:
622 case ISD::SETGT:
623 case ISD::SETGE: return 1;
624 case ISD::SETULT:
625 case ISD::SETULE:
626 case ISD::SETUGT:
627 case ISD::SETUGE: return 2;
628 }
629}
630
632 EVT Type) {
633 bool IsInteger = Type.isInteger();
634 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
635 // Cannot fold a signed integer setcc with an unsigned integer setcc.
636 return ISD::SETCC_INVALID;
637
638 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
639
640 // If the N and U bits get set, then the resultant comparison DOES suddenly
641 // care about orderedness, and it is true when ordered.
642 if (Op > ISD::SETTRUE2)
643 Op &= ~16; // Clear the U bit if the N bit is set.
644
645 // Canonicalize illegal integer setcc's.
646 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
647 Op = ISD::SETNE;
648
649 return ISD::CondCode(Op);
650}
651
653 EVT Type) {
654 bool IsInteger = Type.isInteger();
655 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
656 // Cannot fold a signed setcc with an unsigned setcc.
657 return ISD::SETCC_INVALID;
658
659 // Combine all of the condition bits.
660 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
661
662 // Canonicalize illegal integer setcc's.
663 if (IsInteger) {
664 switch (Result) {
665 default: break;
666 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
667 case ISD::SETOEQ: // SETEQ & SETU[LG]E
668 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
669 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
670 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
671 }
672 }
673
674 return Result;
675}
676
677//===----------------------------------------------------------------------===//
678// SDNode Profile Support
679//===----------------------------------------------------------------------===//
680
681/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
682static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
683 ID.AddInteger(OpC);
684}
685
686/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
687/// solely with their pointer.
689 ID.AddPointer(VTList.VTs);
690}
691
692/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
694 ArrayRef<SDValue> Ops) {
695 for (const auto &Op : Ops) {
696 ID.AddPointer(Op.getNode());
697 ID.AddInteger(Op.getResNo());
698 }
699}
700
701/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
703 ArrayRef<SDUse> Ops) {
704 for (const auto &Op : Ops) {
705 ID.AddPointer(Op.getNode());
706 ID.AddInteger(Op.getResNo());
707 }
708}
709
710static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
711 SDVTList VTList, ArrayRef<SDValue> OpList) {
712 AddNodeIDOpcode(ID, OpC);
713 AddNodeIDValueTypes(ID, VTList);
714 AddNodeIDOperands(ID, OpList);
715}
716
717/// If this is an SDNode with special info, add this info to the NodeID data.
719 switch (N->getOpcode()) {
722 case ISD::MCSymbol:
723 llvm_unreachable("Should only be used on nodes with operands");
724 default: break; // Normal nodes don't need extra info.
726 case ISD::Constant: {
727 const ConstantSDNode *C = cast<ConstantSDNode>(N);
728 ID.AddPointer(C->getConstantIntValue());
729 ID.AddBoolean(C->isOpaque());
730 break;
731 }
733 case ISD::ConstantFP:
734 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
735 break;
740 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
741 ID.AddPointer(GA->getGlobal());
742 ID.AddInteger(GA->getOffset());
743 ID.AddInteger(GA->getTargetFlags());
744 break;
745 }
746 case ISD::BasicBlock:
747 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
748 break;
749 case ISD::Register:
750 ID.AddInteger(cast<RegisterSDNode>(N)->getReg());
751 break;
753 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
754 break;
755 case ISD::SRCVALUE:
756 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
757 break;
758 case ISD::FrameIndex:
760 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
761 break;
764 if (cast<LifetimeSDNode>(N)->hasOffset()) {
765 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
766 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
767 }
768 break;
770 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
771 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
772 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
773 break;
774 case ISD::JumpTable:
776 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
777 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
778 break;
781 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
782 ID.AddInteger(CP->getAlign().value());
783 ID.AddInteger(CP->getOffset());
784 if (CP->isMachineConstantPoolEntry())
785 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
786 else
787 ID.AddPointer(CP->getConstVal());
788 ID.AddInteger(CP->getTargetFlags());
789 break;
790 }
791 case ISD::TargetIndex: {
792 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
793 ID.AddInteger(TI->getIndex());
794 ID.AddInteger(TI->getOffset());
795 ID.AddInteger(TI->getTargetFlags());
796 break;
797 }
798 case ISD::LOAD: {
799 const LoadSDNode *LD = cast<LoadSDNode>(N);
800 ID.AddInteger(LD->getMemoryVT().getRawBits());
801 ID.AddInteger(LD->getRawSubclassData());
802 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
803 ID.AddInteger(LD->getMemOperand()->getFlags());
804 break;
805 }
806 case ISD::STORE: {
807 const StoreSDNode *ST = cast<StoreSDNode>(N);
808 ID.AddInteger(ST->getMemoryVT().getRawBits());
809 ID.AddInteger(ST->getRawSubclassData());
810 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
811 ID.AddInteger(ST->getMemOperand()->getFlags());
812 break;
813 }
814 case ISD::VP_LOAD: {
815 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
816 ID.AddInteger(ELD->getMemoryVT().getRawBits());
817 ID.AddInteger(ELD->getRawSubclassData());
818 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
819 ID.AddInteger(ELD->getMemOperand()->getFlags());
820 break;
821 }
822 case ISD::VP_STORE: {
823 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
824 ID.AddInteger(EST->getMemoryVT().getRawBits());
825 ID.AddInteger(EST->getRawSubclassData());
826 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
827 ID.AddInteger(EST->getMemOperand()->getFlags());
828 break;
829 }
830 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
831 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
832 ID.AddInteger(SLD->getMemoryVT().getRawBits());
833 ID.AddInteger(SLD->getRawSubclassData());
834 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
835 break;
836 }
837 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
838 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
839 ID.AddInteger(SST->getMemoryVT().getRawBits());
840 ID.AddInteger(SST->getRawSubclassData());
841 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
842 break;
843 }
844 case ISD::VP_GATHER: {
845 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
846 ID.AddInteger(EG->getMemoryVT().getRawBits());
847 ID.AddInteger(EG->getRawSubclassData());
848 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
849 ID.AddInteger(EG->getMemOperand()->getFlags());
850 break;
851 }
852 case ISD::VP_SCATTER: {
853 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
854 ID.AddInteger(ES->getMemoryVT().getRawBits());
855 ID.AddInteger(ES->getRawSubclassData());
856 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
857 ID.AddInteger(ES->getMemOperand()->getFlags());
858 break;
859 }
860 case ISD::MLOAD: {
861 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
862 ID.AddInteger(MLD->getMemoryVT().getRawBits());
863 ID.AddInteger(MLD->getRawSubclassData());
864 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
865 ID.AddInteger(MLD->getMemOperand()->getFlags());
866 break;
867 }
868 case ISD::MSTORE: {
869 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
870 ID.AddInteger(MST->getMemoryVT().getRawBits());
871 ID.AddInteger(MST->getRawSubclassData());
872 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
873 ID.AddInteger(MST->getMemOperand()->getFlags());
874 break;
875 }
876 case ISD::MGATHER: {
877 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
878 ID.AddInteger(MG->getMemoryVT().getRawBits());
879 ID.AddInteger(MG->getRawSubclassData());
880 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
881 ID.AddInteger(MG->getMemOperand()->getFlags());
882 break;
883 }
884 case ISD::MSCATTER: {
885 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
886 ID.AddInteger(MS->getMemoryVT().getRawBits());
887 ID.AddInteger(MS->getRawSubclassData());
888 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
889 ID.AddInteger(MS->getMemOperand()->getFlags());
890 break;
891 }
894 case ISD::ATOMIC_SWAP:
906 case ISD::ATOMIC_LOAD:
907 case ISD::ATOMIC_STORE: {
908 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
909 ID.AddInteger(AT->getMemoryVT().getRawBits());
910 ID.AddInteger(AT->getRawSubclassData());
911 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
912 ID.AddInteger(AT->getMemOperand()->getFlags());
913 break;
914 }
915 case ISD::VECTOR_SHUFFLE: {
916 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
917 for (int M : Mask)
918 ID.AddInteger(M);
919 break;
920 }
922 case ISD::BlockAddress: {
923 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
924 ID.AddPointer(BA->getBlockAddress());
925 ID.AddInteger(BA->getOffset());
926 ID.AddInteger(BA->getTargetFlags());
927 break;
928 }
929 case ISD::AssertAlign:
930 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
931 break;
932 case ISD::PREFETCH:
935 // Handled by MemIntrinsicSDNode check after the switch.
936 break;
937 } // end switch (N->getOpcode())
938
939 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
940 // to check.
941 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
942 ID.AddInteger(MN->getRawSubclassData());
943 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
944 ID.AddInteger(MN->getMemOperand()->getFlags());
945 ID.AddInteger(MN->getMemoryVT().getRawBits());
946 }
947}
948
949/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
950/// data.
951static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
952 AddNodeIDOpcode(ID, N->getOpcode());
953 // Add the return value info.
954 AddNodeIDValueTypes(ID, N->getVTList());
955 // Add the operand info.
956 AddNodeIDOperands(ID, N->ops());
957
958 // Handle SDNode leafs with special info.
960}
961
962//===----------------------------------------------------------------------===//
963// SelectionDAG Class
964//===----------------------------------------------------------------------===//
965
966/// doNotCSE - Return true if CSE should not be performed for this node.
967static bool doNotCSE(SDNode *N) {
968 if (N->getValueType(0) == MVT::Glue)
969 return true; // Never CSE anything that produces a glue result.
970
971 switch (N->getOpcode()) {
972 default: break;
973 case ISD::HANDLENODE:
974 case ISD::EH_LABEL:
975 return true; // Never CSE these nodes.
976 }
977
978 // Check that remaining values produced are not flags.
979 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
980 if (N->getValueType(i) == MVT::Glue)
981 return true; // Never CSE anything that produces a glue result.
982
983 return false;
984}
985
986/// RemoveDeadNodes - This method deletes all unreachable nodes in the
987/// SelectionDAG.
989 // Create a dummy node (which is not added to allnodes), that adds a reference
990 // to the root node, preventing it from being deleted.
991 HandleSDNode Dummy(getRoot());
992
994
995 // Add all obviously-dead nodes to the DeadNodes worklist.
996 for (SDNode &Node : allnodes())
997 if (Node.use_empty())
998 DeadNodes.push_back(&Node);
999
1000 RemoveDeadNodes(DeadNodes);
1001
1002 // If the root changed (e.g. it was a dead load, update the root).
1003 setRoot(Dummy.getValue());
1004}
1005
1006/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1007/// given list, and any nodes that become unreachable as a result.
1009
1010 // Process the worklist, deleting the nodes and adding their uses to the
1011 // worklist.
1012 while (!DeadNodes.empty()) {
1013 SDNode *N = DeadNodes.pop_back_val();
1014 // Skip to next node if we've already managed to delete the node. This could
1015 // happen if replacing a node causes a node previously added to the node to
1016 // be deleted.
1017 if (N->getOpcode() == ISD::DELETED_NODE)
1018 continue;
1019
1020 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1021 DUL->NodeDeleted(N, nullptr);
1022
1023 // Take the node out of the appropriate CSE map.
1024 RemoveNodeFromCSEMaps(N);
1025
1026 // Next, brutally remove the operand list. This is safe to do, as there are
1027 // no cycles in the graph.
1028 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1029 SDUse &Use = *I++;
1030 SDNode *Operand = Use.getNode();
1031 Use.set(SDValue());
1032
1033 // Now that we removed this operand, see if there are no uses of it left.
1034 if (Operand->use_empty())
1035 DeadNodes.push_back(Operand);
1036 }
1037
1038 DeallocateNode(N);
1039 }
1040}
1041
1043 SmallVector<SDNode*, 16> DeadNodes(1, N);
1044
1045 // Create a dummy node that adds a reference to the root node, preventing
1046 // it from being deleted. (This matters if the root is an operand of the
1047 // dead node.)
1048 HandleSDNode Dummy(getRoot());
1049
1050 RemoveDeadNodes(DeadNodes);
1051}
1052
1054 // First take this out of the appropriate CSE map.
1055 RemoveNodeFromCSEMaps(N);
1056
1057 // Finally, remove uses due to operands of this node, remove from the
1058 // AllNodes list, and delete the node.
1059 DeleteNodeNotInCSEMaps(N);
1060}
1061
1062void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1063 assert(N->getIterator() != AllNodes.begin() &&
1064 "Cannot delete the entry node!");
1065 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1066
1067 // Drop all of the operands and decrement used node's use counts.
1068 N->DropOperands();
1069
1070 DeallocateNode(N);
1071}
1072
1073void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1074 assert(!(V->isVariadic() && isParameter));
1075 if (isParameter)
1076 ByvalParmDbgValues.push_back(V);
1077 else
1078 DbgValues.push_back(V);
1079 for (const SDNode *Node : V->getSDNodes())
1080 if (Node)
1081 DbgValMap[Node].push_back(V);
1082}
1083
1084void SDDbgInfo::erase(const SDNode *Node) {
1085 DbgValMapType::iterator I = DbgValMap.find(Node);
1086 if (I == DbgValMap.end())
1087 return;
1088 for (auto &Val: I->second)
1089 Val->setIsInvalidated();
1090 DbgValMap.erase(I);
1091}
1092
1093void SelectionDAG::DeallocateNode(SDNode *N) {
1094 // If we have operands, deallocate them.
1095 removeOperands(N);
1096
1097 NodeAllocator.Deallocate(AllNodes.remove(N));
1098
1099 // Set the opcode to DELETED_NODE to help catch bugs when node
1100 // memory is reallocated.
1101 // FIXME: There are places in SDag that have grown a dependency on the opcode
1102 // value in the released node.
1103 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1104 N->NodeType = ISD::DELETED_NODE;
1105
1106 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1107 // them and forget about that node.
1108 DbgInfo->erase(N);
1109
1110 // Invalidate extra info.
1111 SDEI.erase(N);
1112}
1113
1114#ifndef NDEBUG
1115/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1116static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1117 switch (N->getOpcode()) {
1118 default:
1119 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1120 TLI->verifyTargetSDNode(N);
1121 break;
1122 case ISD::BUILD_PAIR: {
1123 EVT VT = N->getValueType(0);
1124 assert(N->getNumValues() == 1 && "Too many results!");
1125 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1126 "Wrong return type!");
1127 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1128 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1129 "Mismatched operand types!");
1130 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1131 "Wrong operand type!");
1132 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1133 "Wrong return type size");
1134 break;
1135 }
1136 case ISD::BUILD_VECTOR: {
1137 assert(N->getNumValues() == 1 && "Too many results!");
1138 assert(N->getValueType(0).isVector() && "Wrong return type!");
1139 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1140 "Wrong number of operands!");
1141 EVT EltVT = N->getValueType(0).getVectorElementType();
1142 for (const SDUse &Op : N->ops()) {
1143 assert((Op.getValueType() == EltVT ||
1144 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1145 EltVT.bitsLE(Op.getValueType()))) &&
1146 "Wrong operand type!");
1147 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1148 "Operands must all have the same type");
1149 }
1150 break;
1151 }
1152 }
1153}
1154#endif // NDEBUG
1155
1156/// Insert a newly allocated node into the DAG.
1157///
1158/// Handles insertion into the all nodes list and CSE map, as well as
1159/// verification and other common operations when a new node is allocated.
1160void SelectionDAG::InsertNode(SDNode *N) {
1161 AllNodes.push_back(N);
1162#ifndef NDEBUG
1163 N->PersistentId = NextPersistentId++;
1164 VerifySDNode(N, TLI);
1165#endif
1166 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1167 DUL->NodeInserted(N);
1168}
1169
1170/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1171/// correspond to it. This is useful when we're about to delete or repurpose
1172/// the node. We don't want future request for structurally identical nodes
1173/// to return N anymore.
1174bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1175 bool Erased = false;
1176 switch (N->getOpcode()) {
1177 case ISD::HANDLENODE: return false; // noop.
1178 case ISD::CONDCODE:
1179 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1180 "Cond code doesn't exist!");
1181 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1182 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1183 break;
1185 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1186 break;
1188 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1189 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1190 ESN->getSymbol(), ESN->getTargetFlags()));
1191 break;
1192 }
1193 case ISD::MCSymbol: {
1194 auto *MCSN = cast<MCSymbolSDNode>(N);
1195 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1196 break;
1197 }
1198 case ISD::VALUETYPE: {
1199 EVT VT = cast<VTSDNode>(N)->getVT();
1200 if (VT.isExtended()) {
1201 Erased = ExtendedValueTypeNodes.erase(VT);
1202 } else {
1203 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1204 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1205 }
1206 break;
1207 }
1208 default:
1209 // Remove it from the CSE Map.
1210 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1211 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1212 Erased = CSEMap.RemoveNode(N);
1213 break;
1214 }
1215#ifndef NDEBUG
1216 // Verify that the node was actually in one of the CSE maps, unless it has a
1217 // glue result (which cannot be CSE'd) or is one of the special cases that are
1218 // not subject to CSE.
1219 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1220 !N->isMachineOpcode() && !doNotCSE(N)) {
1221 N->dump(this);
1222 dbgs() << "\n";
1223 llvm_unreachable("Node is not in map!");
1224 }
1225#endif
1226 return Erased;
1227}
1228
1229/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1230/// maps and modified in place. Add it back to the CSE maps, unless an identical
1231/// node already exists, in which case transfer all its users to the existing
1232/// node. This transfer can potentially trigger recursive merging.
1233void
1234SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1235 // For node types that aren't CSE'd, just act as if no identical node
1236 // already exists.
1237 if (!doNotCSE(N)) {
1238 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1239 if (Existing != N) {
1240 // If there was already an existing matching node, use ReplaceAllUsesWith
1241 // to replace the dead one with the existing one. This can cause
1242 // recursive merging of other unrelated nodes down the line.
1243 Existing->intersectFlagsWith(N->getFlags());
1244 ReplaceAllUsesWith(N, Existing);
1245
1246 // N is now dead. Inform the listeners and delete it.
1247 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1248 DUL->NodeDeleted(N, Existing);
1249 DeleteNodeNotInCSEMaps(N);
1250 return;
1251 }
1252 }
1253
1254 // If the node doesn't already exist, we updated it. Inform listeners.
1255 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1256 DUL->NodeUpdated(N);
1257}
1258
1259/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1260/// were replaced with those specified. If this node is never memoized,
1261/// return null, otherwise return a pointer to the slot it would take. If a
1262/// node already exists with these operands, the slot will be non-null.
1263SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1264 void *&InsertPos) {
1265 if (doNotCSE(N))
1266 return nullptr;
1267
1268 SDValue Ops[] = { Op };
1270 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1272 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1273 if (Node)
1274 Node->intersectFlagsWith(N->getFlags());
1275 return Node;
1276}
1277
1278/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1279/// were replaced with those specified. If this node is never memoized,
1280/// return null, otherwise return a pointer to the slot it would take. If a
1281/// node already exists with these operands, the slot will be non-null.
1282SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1283 SDValue Op1, SDValue Op2,
1284 void *&InsertPos) {
1285 if (doNotCSE(N))
1286 return nullptr;
1287
1288 SDValue Ops[] = { Op1, Op2 };
1290 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1292 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1293 if (Node)
1294 Node->intersectFlagsWith(N->getFlags());
1295 return Node;
1296}
1297
1298/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1299/// were replaced with those specified. If this node is never memoized,
1300/// return null, otherwise return a pointer to the slot it would take. If a
1301/// node already exists with these operands, the slot will be non-null.
1302SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1303 void *&InsertPos) {
1304 if (doNotCSE(N))
1305 return nullptr;
1306
1308 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1310 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1311 if (Node)
1312 Node->intersectFlagsWith(N->getFlags());
1313 return Node;
1314}
1315
1317 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1318 : VT.getTypeForEVT(*getContext());
1319
1320 return getDataLayout().getABITypeAlign(Ty);
1321}
1322
1323// EntryNode could meaningfully have debug info if we can find it...
1325 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1326 getVTList(MVT::Other, MVT::Glue)),
1327 Root(getEntryNode()) {
1328 InsertNode(&EntryNode);
1329 DbgInfo = new SDDbgInfo();
1330}
1331
1333 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1334 const TargetLibraryInfo *LibraryInfo,
1335 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1337 FunctionVarLocs const *VarLocs) {
1338 MF = &NewMF;
1339 SDAGISelPass = PassPtr;
1340 ORE = &NewORE;
1343 LibInfo = LibraryInfo;
1344 Context = &MF->getFunction().getContext();
1345 UA = NewUA;
1346 PSI = PSIin;
1347 BFI = BFIin;
1348 MMI = &MMIin;
1349 FnVarLocs = VarLocs;
1350}
1351
1353 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1354 allnodes_clear();
1355 OperandRecycler.clear(OperandAllocator);
1356 delete DbgInfo;
1357}
1358
1360 return MF->getFunction().hasOptSize() ||
1362}
1363
1364void SelectionDAG::allnodes_clear() {
1365 assert(&*AllNodes.begin() == &EntryNode);
1366 AllNodes.remove(AllNodes.begin());
1367 while (!AllNodes.empty())
1368 DeallocateNode(&AllNodes.front());
1369#ifndef NDEBUG
1370 NextPersistentId = 0;
1371#endif
1372}
1373
1374SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1375 void *&InsertPos) {
1376 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1377 if (N) {
1378 switch (N->getOpcode()) {
1379 default: break;
1380 case ISD::Constant:
1381 case ISD::ConstantFP:
1382 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1383 "debug location. Use another overload.");
1384 }
1385 }
1386 return N;
1387}
1388
1389SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1390 const SDLoc &DL, void *&InsertPos) {
1391 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1392 if (N) {
1393 switch (N->getOpcode()) {
1394 case ISD::Constant:
1395 case ISD::ConstantFP:
1396 // Erase debug location from the node if the node is used at several
1397 // different places. Do not propagate one location to all uses as it
1398 // will cause a worse single stepping debugging experience.
1399 if (N->getDebugLoc() != DL.getDebugLoc())
1400 N->setDebugLoc(DebugLoc());
1401 break;
1402 default:
1403 // When the node's point of use is located earlier in the instruction
1404 // sequence than its prior point of use, update its debug info to the
1405 // earlier location.
1406 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1407 N->setDebugLoc(DL.getDebugLoc());
1408 break;
1409 }
1410 }
1411 return N;
1412}
1413
1415 allnodes_clear();
1416 OperandRecycler.clear(OperandAllocator);
1417 OperandAllocator.Reset();
1418 CSEMap.clear();
1419
1420 ExtendedValueTypeNodes.clear();
1421 ExternalSymbols.clear();
1422 TargetExternalSymbols.clear();
1423 MCSymbols.clear();
1424 SDEI.clear();
1425 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1426 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1427
1428 EntryNode.UseList = nullptr;
1429 InsertNode(&EntryNode);
1430 Root = getEntryNode();
1431 DbgInfo->clear();
1432}
1433
1435 return VT.bitsGT(Op.getValueType())
1436 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1437 : getNode(ISD::FP_ROUND, DL, VT, Op,
1438 getIntPtrConstant(0, DL, /*isTarget=*/true));
1439}
1440
1441std::pair<SDValue, SDValue>
1443 const SDLoc &DL, EVT VT) {
1444 assert(!VT.bitsEq(Op.getValueType()) &&
1445 "Strict no-op FP extend/round not allowed.");
1446 SDValue Res =
1447 VT.bitsGT(Op.getValueType())
1448 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1449 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1450 {Chain, Op, getIntPtrConstant(0, DL)});
1451
1452 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1453}
1454
1456 return VT.bitsGT(Op.getValueType()) ?
1457 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1458 getNode(ISD::TRUNCATE, DL, VT, Op);
1459}
1460
1462 return VT.bitsGT(Op.getValueType()) ?
1463 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1464 getNode(ISD::TRUNCATE, DL, VT, Op);
1465}
1466
1468 return VT.bitsGT(Op.getValueType()) ?
1469 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1470 getNode(ISD::TRUNCATE, DL, VT, Op);
1471}
1472
1474 EVT VT) {
1475 assert(!VT.isVector());
1476 auto Type = Op.getValueType();
1477 SDValue DestOp;
1478 if (Type == VT)
1479 return Op;
1480 auto Size = Op.getValueSizeInBits();
1481 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1482 if (DestOp.getValueType() == VT)
1483 return DestOp;
1484
1485 return getAnyExtOrTrunc(DestOp, DL, VT);
1486}
1487
1489 EVT VT) {
1490 assert(!VT.isVector());
1491 auto Type = Op.getValueType();
1492 SDValue DestOp;
1493 if (Type == VT)
1494 return Op;
1495 auto Size = Op.getValueSizeInBits();
1496 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1497 if (DestOp.getValueType() == VT)
1498 return DestOp;
1499
1500 return getSExtOrTrunc(DestOp, DL, VT);
1501}
1502
1504 EVT VT) {
1505 assert(!VT.isVector());
1506 auto Type = Op.getValueType();
1507 SDValue DestOp;
1508 if (Type == VT)
1509 return Op;
1510 auto Size = Op.getValueSizeInBits();
1511 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1512 if (DestOp.getValueType() == VT)
1513 return DestOp;
1514
1515 return getZExtOrTrunc(DestOp, DL, VT);
1516}
1517
1519 EVT OpVT) {
1520 if (VT.bitsLE(Op.getValueType()))
1521 return getNode(ISD::TRUNCATE, SL, VT, Op);
1522
1524 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1525}
1526
1528 EVT OpVT = Op.getValueType();
1529 assert(VT.isInteger() && OpVT.isInteger() &&
1530 "Cannot getZeroExtendInReg FP types");
1531 assert(VT.isVector() == OpVT.isVector() &&
1532 "getZeroExtendInReg type should be vector iff the operand "
1533 "type is vector!");
1534 assert((!VT.isVector() ||
1536 "Vector element counts must match in getZeroExtendInReg");
1537 assert(VT.bitsLE(OpVT) && "Not extending!");
1538 if (OpVT == VT)
1539 return Op;
1541 VT.getScalarSizeInBits());
1542 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1543}
1544
1546 SDValue EVL, const SDLoc &DL,
1547 EVT VT) {
1548 EVT OpVT = Op.getValueType();
1549 assert(VT.isInteger() && OpVT.isInteger() &&
1550 "Cannot getVPZeroExtendInReg FP types");
1551 assert(VT.isVector() && OpVT.isVector() &&
1552 "getVPZeroExtendInReg type and operand type should be vector!");
1554 "Vector element counts must match in getZeroExtendInReg");
1555 assert(VT.bitsLE(OpVT) && "Not extending!");
1556 if (OpVT == VT)
1557 return Op;
1559 VT.getScalarSizeInBits());
1560 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1561 EVL);
1562}
1563
1565 // Only unsigned pointer semantics are supported right now. In the future this
1566 // might delegate to TLI to check pointer signedness.
1567 return getZExtOrTrunc(Op, DL, VT);
1568}
1569
1571 // Only unsigned pointer semantics are supported right now. In the future this
1572 // might delegate to TLI to check pointer signedness.
1573 return getZeroExtendInReg(Op, DL, VT);
1574}
1575
1577 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1578}
1579
1580/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1582 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1583}
1584
1586 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1587 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1588}
1589
1591 SDValue Mask, SDValue EVL, EVT VT) {
1592 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1593 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1594}
1595
1597 SDValue Mask, SDValue EVL) {
1598 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1599}
1600
1602 SDValue Mask, SDValue EVL) {
1603 if (VT.bitsGT(Op.getValueType()))
1604 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1605 if (VT.bitsLT(Op.getValueType()))
1606 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1607 return Op;
1608}
1609
1611 EVT OpVT) {
1612 if (!V)
1613 return getConstant(0, DL, VT);
1614
1615 switch (TLI->getBooleanContents(OpVT)) {
1618 return getConstant(1, DL, VT);
1620 return getAllOnesConstant(DL, VT);
1621 }
1622 llvm_unreachable("Unexpected boolean content enum!");
1623}
1624
1626 bool isT, bool isO) {
1627 EVT EltVT = VT.getScalarType();
1628 assert((EltVT.getSizeInBits() >= 64 ||
1629 (uint64_t)((int64_t)Val >> EltVT.getSizeInBits()) + 1 < 2) &&
1630 "getConstant with a uint64_t value that doesn't fit in the type!");
1631 return getConstant(APInt(EltVT.getSizeInBits(), Val), DL, VT, isT, isO);
1632}
1633
1635 bool isT, bool isO) {
1636 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1637}
1638
1640 EVT VT, bool isT, bool isO) {
1641 assert(VT.isInteger() && "Cannot create FP integer constant!");
1642
1643 EVT EltVT = VT.getScalarType();
1644 const ConstantInt *Elt = &Val;
1645
1646 // In some cases the vector type is legal but the element type is illegal and
1647 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1648 // inserted value (the type does not need to match the vector element type).
1649 // Any extra bits introduced will be truncated away.
1650 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1652 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1653 APInt NewVal;
1654 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1655 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1656 else
1657 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1658 Elt = ConstantInt::get(*getContext(), NewVal);
1659 }
1660 // In other cases the element type is illegal and needs to be expanded, for
1661 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1662 // the value into n parts and use a vector type with n-times the elements.
1663 // Then bitcast to the type requested.
1664 // Legalizing constants too early makes the DAGCombiner's job harder so we
1665 // only legalize if the DAG tells us we must produce legal types.
1666 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1667 TLI->getTypeAction(*getContext(), EltVT) ==
1669 const APInt &NewVal = Elt->getValue();
1670 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1671 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1672
1673 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1674 if (VT.isScalableVector() ||
1676 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1677 "Can only handle an even split!");
1678 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1679
1680 SmallVector<SDValue, 2> ScalarParts;
1681 for (unsigned i = 0; i != Parts; ++i)
1682 ScalarParts.push_back(getConstant(
1683 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1684 ViaEltVT, isT, isO));
1685
1686 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1687 }
1688
1689 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1690 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1691
1692 // Check the temporary vector is the correct size. If this fails then
1693 // getTypeToTransformTo() probably returned a type whose size (in bits)
1694 // isn't a power-of-2 factor of the requested type size.
1695 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1696
1697 SmallVector<SDValue, 2> EltParts;
1698 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1699 EltParts.push_back(getConstant(
1700 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1701 ViaEltVT, isT, isO));
1702
1703 // EltParts is currently in little endian order. If we actually want
1704 // big-endian order then reverse it now.
1705 if (getDataLayout().isBigEndian())
1706 std::reverse(EltParts.begin(), EltParts.end());
1707
1708 // The elements must be reversed when the element order is different
1709 // to the endianness of the elements (because the BITCAST is itself a
1710 // vector shuffle in this situation). However, we do not need any code to
1711 // perform this reversal because getConstant() is producing a vector
1712 // splat.
1713 // This situation occurs in MIPS MSA.
1714
1716 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1717 llvm::append_range(Ops, EltParts);
1718
1719 SDValue V =
1720 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1721 return V;
1722 }
1723
1724 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1725 "APInt size does not match type size!");
1726 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1727 SDVTList VTs = getVTList(EltVT);
1729 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1730 ID.AddPointer(Elt);
1731 ID.AddBoolean(isO);
1732 void *IP = nullptr;
1733 SDNode *N = nullptr;
1734 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1735 if (!VT.isVector())
1736 return SDValue(N, 0);
1737
1738 if (!N) {
1739 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1740 CSEMap.InsertNode(N, IP);
1741 InsertNode(N);
1742 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1743 }
1744
1745 SDValue Result(N, 0);
1746 if (VT.isVector())
1747 Result = getSplat(VT, DL, Result);
1748 return Result;
1749}
1750
1752 bool isTarget) {
1753 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1754}
1755
1757 const SDLoc &DL) {
1758 assert(VT.isInteger() && "Shift amount is not an integer type!");
1759 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1760 return getConstant(Val, DL, ShiftVT);
1761}
1762
1764 const SDLoc &DL) {
1765 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1766 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1767}
1768
1770 bool isTarget) {
1771 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1772}
1773
1775 bool isTarget) {
1776 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1777}
1778
1780 EVT VT, bool isTarget) {
1781 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1782
1783 EVT EltVT = VT.getScalarType();
1784
1785 // Do the map lookup using the actual bit pattern for the floating point
1786 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1787 // we don't have issues with SNANs.
1788 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1789 SDVTList VTs = getVTList(EltVT);
1791 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1792 ID.AddPointer(&V);
1793 void *IP = nullptr;
1794 SDNode *N = nullptr;
1795 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1796 if (!VT.isVector())
1797 return SDValue(N, 0);
1798
1799 if (!N) {
1800 N = newSDNode<ConstantFPSDNode>(isTarget, &V, VTs);
1801 CSEMap.InsertNode(N, IP);
1802 InsertNode(N);
1803 }
1804
1805 SDValue Result(N, 0);
1806 if (VT.isVector())
1807 Result = getSplat(VT, DL, Result);
1808 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1809 return Result;
1810}
1811
1813 bool isTarget) {
1814 EVT EltVT = VT.getScalarType();
1815 if (EltVT == MVT::f32)
1816 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1817 if (EltVT == MVT::f64)
1818 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1819 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1820 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1821 bool Ignored;
1822 APFloat APF = APFloat(Val);
1824 &Ignored);
1825 return getConstantFP(APF, DL, VT, isTarget);
1826 }
1827 llvm_unreachable("Unsupported type in getConstantFP");
1828}
1829
1831 EVT VT, int64_t Offset, bool isTargetGA,
1832 unsigned TargetFlags) {
1833 assert((TargetFlags == 0 || isTargetGA) &&
1834 "Cannot set target flags on target-independent globals");
1835
1836 // Truncate (with sign-extension) the offset value to the pointer size.
1838 if (BitWidth < 64)
1840
1841 unsigned Opc;
1842 if (GV->isThreadLocal())
1844 else
1845 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1846
1847 SDVTList VTs = getVTList(VT);
1849 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1850 ID.AddPointer(GV);
1851 ID.AddInteger(Offset);
1852 ID.AddInteger(TargetFlags);
1853 void *IP = nullptr;
1854 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1855 return SDValue(E, 0);
1856
1857 auto *N = newSDNode<GlobalAddressSDNode>(
1858 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1859 CSEMap.InsertNode(N, IP);
1860 InsertNode(N);
1861 return SDValue(N, 0);
1862}
1863
1864SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1865 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1866 SDVTList VTs = getVTList(VT);
1868 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1869 ID.AddInteger(FI);
1870 void *IP = nullptr;
1871 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1872 return SDValue(E, 0);
1873
1874 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1875 CSEMap.InsertNode(N, IP);
1876 InsertNode(N);
1877 return SDValue(N, 0);
1878}
1879
1880SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1881 unsigned TargetFlags) {
1882 assert((TargetFlags == 0 || isTarget) &&
1883 "Cannot set target flags on target-independent jump tables");
1884 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1885 SDVTList VTs = getVTList(VT);
1887 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1888 ID.AddInteger(JTI);
1889 ID.AddInteger(TargetFlags);
1890 void *IP = nullptr;
1891 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1892 return SDValue(E, 0);
1893
1894 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1895 CSEMap.InsertNode(N, IP);
1896 InsertNode(N);
1897 return SDValue(N, 0);
1898}
1899
1901 const SDLoc &DL) {
1903 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1904 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1905}
1906
1908 MaybeAlign Alignment, int Offset,
1909 bool isTarget, unsigned TargetFlags) {
1910 assert((TargetFlags == 0 || isTarget) &&
1911 "Cannot set target flags on target-independent globals");
1912 if (!Alignment)
1913 Alignment = shouldOptForSize()
1914 ? getDataLayout().getABITypeAlign(C->getType())
1915 : getDataLayout().getPrefTypeAlign(C->getType());
1916 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1917 SDVTList VTs = getVTList(VT);
1919 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1920 ID.AddInteger(Alignment->value());
1921 ID.AddInteger(Offset);
1922 ID.AddPointer(C);
1923 ID.AddInteger(TargetFlags);
1924 void *IP = nullptr;
1925 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1926 return SDValue(E, 0);
1927
1928 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1929 TargetFlags);
1930 CSEMap.InsertNode(N, IP);
1931 InsertNode(N);
1932 SDValue V = SDValue(N, 0);
1933 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1934 return V;
1935}
1936
1938 MaybeAlign Alignment, int Offset,
1939 bool isTarget, unsigned TargetFlags) {
1940 assert((TargetFlags == 0 || isTarget) &&
1941 "Cannot set target flags on target-independent globals");
1942 if (!Alignment)
1943 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1944 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1945 SDVTList VTs = getVTList(VT);
1947 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
1948 ID.AddInteger(Alignment->value());
1949 ID.AddInteger(Offset);
1950 C->addSelectionDAGCSEId(ID);
1951 ID.AddInteger(TargetFlags);
1952 void *IP = nullptr;
1953 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1954 return SDValue(E, 0);
1955
1956 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1957 TargetFlags);
1958 CSEMap.InsertNode(N, IP);
1959 InsertNode(N);
1960 return SDValue(N, 0);
1961}
1962
1965 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), std::nullopt);
1966 ID.AddPointer(MBB);
1967 void *IP = nullptr;
1968 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1969 return SDValue(E, 0);
1970
1971 auto *N = newSDNode<BasicBlockSDNode>(MBB);
1972 CSEMap.InsertNode(N, IP);
1973 InsertNode(N);
1974 return SDValue(N, 0);
1975}
1976
1978 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
1979 ValueTypeNodes.size())
1980 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
1981
1982 SDNode *&N = VT.isExtended() ?
1983 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
1984
1985 if (N) return SDValue(N, 0);
1986 N = newSDNode<VTSDNode>(VT);
1987 InsertNode(N);
1988 return SDValue(N, 0);
1989}
1990
1992 SDNode *&N = ExternalSymbols[Sym];
1993 if (N) return SDValue(N, 0);
1994 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
1995 InsertNode(N);
1996 return SDValue(N, 0);
1997}
1998
2000 SDNode *&N = MCSymbols[Sym];
2001 if (N)
2002 return SDValue(N, 0);
2003 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2004 InsertNode(N);
2005 return SDValue(N, 0);
2006}
2007
2009 unsigned TargetFlags) {
2010 SDNode *&N =
2011 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2012 if (N) return SDValue(N, 0);
2013 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2014 InsertNode(N);
2015 return SDValue(N, 0);
2016}
2017
2019 if ((unsigned)Cond >= CondCodeNodes.size())
2020 CondCodeNodes.resize(Cond+1);
2021
2022 if (!CondCodeNodes[Cond]) {
2023 auto *N = newSDNode<CondCodeSDNode>(Cond);
2024 CondCodeNodes[Cond] = N;
2025 InsertNode(N);
2026 }
2027
2028 return SDValue(CondCodeNodes[Cond], 0);
2029}
2030
2032 bool ConstantFold) {
2033 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2034 "APInt size does not match type size!");
2035
2036 if (MulImm == 0)
2037 return getConstant(0, DL, VT);
2038
2039 if (ConstantFold) {
2040 const MachineFunction &MF = getMachineFunction();
2041 const Function &F = MF.getFunction();
2042 ConstantRange CR = getVScaleRange(&F, 64);
2043 if (const APInt *C = CR.getSingleElement())
2044 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2045 }
2046
2047 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2048}
2049
2051 bool ConstantFold) {
2052 if (EC.isScalable())
2053 return getVScale(DL, VT,
2054 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2055
2056 return getConstant(EC.getKnownMinValue(), DL, VT);
2057}
2058
2060 APInt One(ResVT.getScalarSizeInBits(), 1);
2061 return getStepVector(DL, ResVT, One);
2062}
2063
2065 const APInt &StepVal) {
2066 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2067 if (ResVT.isScalableVector())
2068 return getNode(
2069 ISD::STEP_VECTOR, DL, ResVT,
2070 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2071
2072 SmallVector<SDValue, 16> OpsStepConstants;
2073 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2074 OpsStepConstants.push_back(
2075 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2076 return getBuildVector(ResVT, DL, OpsStepConstants);
2077}
2078
2079/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2080/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2082 std::swap(N1, N2);
2084}
2085
2087 SDValue N2, ArrayRef<int> Mask) {
2088 assert(VT.getVectorNumElements() == Mask.size() &&
2089 "Must have the same number of vector elements as mask elements!");
2090 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2091 "Invalid VECTOR_SHUFFLE");
2092
2093 // Canonicalize shuffle undef, undef -> undef
2094 if (N1.isUndef() && N2.isUndef())
2095 return getUNDEF(VT);
2096
2097 // Validate that all indices in Mask are within the range of the elements
2098 // input to the shuffle.
2099 int NElts = Mask.size();
2100 assert(llvm::all_of(Mask,
2101 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2102 "Index out of range");
2103
2104 // Copy the mask so we can do any needed cleanup.
2105 SmallVector<int, 8> MaskVec(Mask);
2106
2107 // Canonicalize shuffle v, v -> v, undef
2108 if (N1 == N2) {
2109 N2 = getUNDEF(VT);
2110 for (int i = 0; i != NElts; ++i)
2111 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2112 }
2113
2114 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2115 if (N1.isUndef())
2116 commuteShuffle(N1, N2, MaskVec);
2117
2118 if (TLI->hasVectorBlend()) {
2119 // If shuffling a splat, try to blend the splat instead. We do this here so
2120 // that even when this arises during lowering we don't have to re-handle it.
2121 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2122 BitVector UndefElements;
2123 SDValue Splat = BV->getSplatValue(&UndefElements);
2124 if (!Splat)
2125 return;
2126
2127 for (int i = 0; i < NElts; ++i) {
2128 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2129 continue;
2130
2131 // If this input comes from undef, mark it as such.
2132 if (UndefElements[MaskVec[i] - Offset]) {
2133 MaskVec[i] = -1;
2134 continue;
2135 }
2136
2137 // If we can blend a non-undef lane, use that instead.
2138 if (!UndefElements[i])
2139 MaskVec[i] = i + Offset;
2140 }
2141 };
2142 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2143 BlendSplat(N1BV, 0);
2144 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2145 BlendSplat(N2BV, NElts);
2146 }
2147
2148 // Canonicalize all index into lhs, -> shuffle lhs, undef
2149 // Canonicalize all index into rhs, -> shuffle rhs, undef
2150 bool AllLHS = true, AllRHS = true;
2151 bool N2Undef = N2.isUndef();
2152 for (int i = 0; i != NElts; ++i) {
2153 if (MaskVec[i] >= NElts) {
2154 if (N2Undef)
2155 MaskVec[i] = -1;
2156 else
2157 AllLHS = false;
2158 } else if (MaskVec[i] >= 0) {
2159 AllRHS = false;
2160 }
2161 }
2162 if (AllLHS && AllRHS)
2163 return getUNDEF(VT);
2164 if (AllLHS && !N2Undef)
2165 N2 = getUNDEF(VT);
2166 if (AllRHS) {
2167 N1 = getUNDEF(VT);
2168 commuteShuffle(N1, N2, MaskVec);
2169 }
2170 // Reset our undef status after accounting for the mask.
2171 N2Undef = N2.isUndef();
2172 // Re-check whether both sides ended up undef.
2173 if (N1.isUndef() && N2Undef)
2174 return getUNDEF(VT);
2175
2176 // If Identity shuffle return that node.
2177 bool Identity = true, AllSame = true;
2178 for (int i = 0; i != NElts; ++i) {
2179 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2180 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2181 }
2182 if (Identity && NElts)
2183 return N1;
2184
2185 // Shuffling a constant splat doesn't change the result.
2186 if (N2Undef) {
2187 SDValue V = N1;
2188
2189 // Look through any bitcasts. We check that these don't change the number
2190 // (and size) of elements and just changes their types.
2191 while (V.getOpcode() == ISD::BITCAST)
2192 V = V->getOperand(0);
2193
2194 // A splat should always show up as a build vector node.
2195 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2196 BitVector UndefElements;
2197 SDValue Splat = BV->getSplatValue(&UndefElements);
2198 // If this is a splat of an undef, shuffling it is also undef.
2199 if (Splat && Splat.isUndef())
2200 return getUNDEF(VT);
2201
2202 bool SameNumElts =
2203 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2204
2205 // We only have a splat which can skip shuffles if there is a splatted
2206 // value and no undef lanes rearranged by the shuffle.
2207 if (Splat && UndefElements.none()) {
2208 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2209 // number of elements match or the value splatted is a zero constant.
2210 if (SameNumElts || isNullConstant(Splat))
2211 return N1;
2212 }
2213
2214 // If the shuffle itself creates a splat, build the vector directly.
2215 if (AllSame && SameNumElts) {
2216 EVT BuildVT = BV->getValueType(0);
2217 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2218 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2219
2220 // We may have jumped through bitcasts, so the type of the
2221 // BUILD_VECTOR may not match the type of the shuffle.
2222 if (BuildVT != VT)
2223 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2224 return NewBV;
2225 }
2226 }
2227 }
2228
2229 SDVTList VTs = getVTList(VT);
2231 SDValue Ops[2] = { N1, N2 };
2233 for (int i = 0; i != NElts; ++i)
2234 ID.AddInteger(MaskVec[i]);
2235
2236 void* IP = nullptr;
2237 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2238 return SDValue(E, 0);
2239
2240 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2241 // SDNode doesn't have access to it. This memory will be "leaked" when
2242 // the node is deallocated, but recovered when the NodeAllocator is released.
2243 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2244 llvm::copy(MaskVec, MaskAlloc);
2245
2246 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2247 dl.getDebugLoc(), MaskAlloc);
2248 createOperands(N, Ops);
2249
2250 CSEMap.InsertNode(N, IP);
2251 InsertNode(N);
2252 SDValue V = SDValue(N, 0);
2253 NewSDValueDbgMsg(V, "Creating new node: ", this);
2254 return V;
2255}
2256
2258 EVT VT = SV.getValueType(0);
2259 SmallVector<int, 8> MaskVec(SV.getMask());
2261
2262 SDValue Op0 = SV.getOperand(0);
2263 SDValue Op1 = SV.getOperand(1);
2264 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2265}
2266
2268 SDVTList VTs = getVTList(VT);
2270 AddNodeIDNode(ID, ISD::Register, VTs, std::nullopt);
2271 ID.AddInteger(RegNo);
2272 void *IP = nullptr;
2273 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2274 return SDValue(E, 0);
2275
2276 auto *N = newSDNode<RegisterSDNode>(RegNo, VTs);
2277 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2278 CSEMap.InsertNode(N, IP);
2279 InsertNode(N);
2280 return SDValue(N, 0);
2281}
2282
2285 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), std::nullopt);
2286 ID.AddPointer(RegMask);
2287 void *IP = nullptr;
2288 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2289 return SDValue(E, 0);
2290
2291 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2292 CSEMap.InsertNode(N, IP);
2293 InsertNode(N);
2294 return SDValue(N, 0);
2295}
2296
2298 MCSymbol *Label) {
2299 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2300}
2301
2302SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2303 SDValue Root, MCSymbol *Label) {
2305 SDValue Ops[] = { Root };
2306 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2307 ID.AddPointer(Label);
2308 void *IP = nullptr;
2309 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2310 return SDValue(E, 0);
2311
2312 auto *N =
2313 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2314 createOperands(N, Ops);
2315
2316 CSEMap.InsertNode(N, IP);
2317 InsertNode(N);
2318 return SDValue(N, 0);
2319}
2320
2322 int64_t Offset, bool isTarget,
2323 unsigned TargetFlags) {
2324 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2325 SDVTList VTs = getVTList(VT);
2326
2328 AddNodeIDNode(ID, Opc, VTs, std::nullopt);
2329 ID.AddPointer(BA);
2330 ID.AddInteger(Offset);
2331 ID.AddInteger(TargetFlags);
2332 void *IP = nullptr;
2333 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2334 return SDValue(E, 0);
2335
2336 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2337 CSEMap.InsertNode(N, IP);
2338 InsertNode(N);
2339 return SDValue(N, 0);
2340}
2341
2344 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), std::nullopt);
2345 ID.AddPointer(V);
2346
2347 void *IP = nullptr;
2348 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2349 return SDValue(E, 0);
2350
2351 auto *N = newSDNode<SrcValueSDNode>(V);
2352 CSEMap.InsertNode(N, IP);
2353 InsertNode(N);
2354 return SDValue(N, 0);
2355}
2356
2359 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), std::nullopt);
2360 ID.AddPointer(MD);
2361
2362 void *IP = nullptr;
2363 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2364 return SDValue(E, 0);
2365
2366 auto *N = newSDNode<MDNodeSDNode>(MD);
2367 CSEMap.InsertNode(N, IP);
2368 InsertNode(N);
2369 return SDValue(N, 0);
2370}
2371
2373 if (VT == V.getValueType())
2374 return V;
2375
2376 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2377}
2378
2380 unsigned SrcAS, unsigned DestAS) {
2381 SDVTList VTs = getVTList(VT);
2382 SDValue Ops[] = {Ptr};
2385 ID.AddInteger(SrcAS);
2386 ID.AddInteger(DestAS);
2387
2388 void *IP = nullptr;
2389 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2390 return SDValue(E, 0);
2391
2392 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2393 VTs, SrcAS, DestAS);
2394 createOperands(N, Ops);
2395
2396 CSEMap.InsertNode(N, IP);
2397 InsertNode(N);
2398 return SDValue(N, 0);
2399}
2400
2402 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2403}
2404
2405/// getShiftAmountOperand - Return the specified value casted to
2406/// the target's desired shift amount type.
2408 EVT OpTy = Op.getValueType();
2409 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2410 if (OpTy == ShTy || OpTy.isVector()) return Op;
2411
2412 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2413}
2414
2416 SDLoc dl(Node);
2418 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2419 EVT VT = Node->getValueType(0);
2420 SDValue Tmp1 = Node->getOperand(0);
2421 SDValue Tmp2 = Node->getOperand(1);
2422 const MaybeAlign MA(Node->getConstantOperandVal(3));
2423
2424 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2425 Tmp2, MachinePointerInfo(V));
2426 SDValue VAList = VAListLoad;
2427
2428 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2429 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2430 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2431
2432 VAList =
2433 getNode(ISD::AND, dl, VAList.getValueType(), VAList,
2434 getConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2435 }
2436
2437 // Increment the pointer, VAList, to the next vaarg
2438 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2439 getConstant(getDataLayout().getTypeAllocSize(
2440 VT.getTypeForEVT(*getContext())),
2441 dl, VAList.getValueType()));
2442 // Store the incremented VAList to the legalized pointer
2443 Tmp1 =
2444 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2445 // Load the actual argument out of the pointer VAList
2446 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2447}
2448
2450 SDLoc dl(Node);
2452 // This defaults to loading a pointer from the input and storing it to the
2453 // output, returning the chain.
2454 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2455 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2456 SDValue Tmp1 =
2457 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2458 Node->getOperand(2), MachinePointerInfo(VS));
2459 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2460 MachinePointerInfo(VD));
2461}
2462
2464 const DataLayout &DL = getDataLayout();
2465 Type *Ty = VT.getTypeForEVT(*getContext());
2466 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2467
2468 if (TLI->isTypeLegal(VT) || !VT.isVector())
2469 return RedAlign;
2470
2472 const Align StackAlign = TFI->getStackAlign();
2473
2474 // See if we can choose a smaller ABI alignment in cases where it's an
2475 // illegal vector type that will get broken down.
2476 if (RedAlign > StackAlign) {
2477 EVT IntermediateVT;
2478 MVT RegisterVT;
2479 unsigned NumIntermediates;
2480 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2481 NumIntermediates, RegisterVT);
2482 Ty = IntermediateVT.getTypeForEVT(*getContext());
2483 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2484 if (RedAlign2 < RedAlign)
2485 RedAlign = RedAlign2;
2486 }
2487
2488 return RedAlign;
2489}
2490
2492 MachineFrameInfo &MFI = MF->getFrameInfo();
2494 int StackID = 0;
2495 if (Bytes.isScalable())
2496 StackID = TFI->getStackIDForScalableVectors();
2497 // The stack id gives an indication of whether the object is scalable or
2498 // not, so it's safe to pass in the minimum size here.
2499 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2500 false, nullptr, StackID);
2501 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2502}
2503
2505 Type *Ty = VT.getTypeForEVT(*getContext());
2506 Align StackAlign =
2507 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2508 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2509}
2510
2512 TypeSize VT1Size = VT1.getStoreSize();
2513 TypeSize VT2Size = VT2.getStoreSize();
2514 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2515 "Don't know how to choose the maximum size when creating a stack "
2516 "temporary");
2517 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2518 ? VT1Size
2519 : VT2Size;
2520
2521 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2522 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2523 const DataLayout &DL = getDataLayout();
2524 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2525 return CreateStackTemporary(Bytes, Align);
2526}
2527
2529 ISD::CondCode Cond, const SDLoc &dl) {
2530 EVT OpVT = N1.getValueType();
2531
2532 auto GetUndefBooleanConstant = [&]() {
2533 if (VT.getScalarType() == MVT::i1 ||
2534 TLI->getBooleanContents(OpVT) ==
2536 return getUNDEF(VT);
2537 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2538 // so we cannot use getUNDEF(). Return zero instead.
2539 return getConstant(0, dl, VT);
2540 };
2541
2542 // These setcc operations always fold.
2543 switch (Cond) {
2544 default: break;
2545 case ISD::SETFALSE:
2546 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2547 case ISD::SETTRUE:
2548 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2549
2550 case ISD::SETOEQ:
2551 case ISD::SETOGT:
2552 case ISD::SETOGE:
2553 case ISD::SETOLT:
2554 case ISD::SETOLE:
2555 case ISD::SETONE:
2556 case ISD::SETO:
2557 case ISD::SETUO:
2558 case ISD::SETUEQ:
2559 case ISD::SETUNE:
2560 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2561 break;
2562 }
2563
2564 if (OpVT.isInteger()) {
2565 // For EQ and NE, we can always pick a value for the undef to make the
2566 // predicate pass or fail, so we can return undef.
2567 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2568 // icmp eq/ne X, undef -> undef.
2569 if ((N1.isUndef() || N2.isUndef()) &&
2570 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2571 return GetUndefBooleanConstant();
2572
2573 // If both operands are undef, we can return undef for int comparison.
2574 // icmp undef, undef -> undef.
2575 if (N1.isUndef() && N2.isUndef())
2576 return GetUndefBooleanConstant();
2577
2578 // icmp X, X -> true/false
2579 // icmp X, undef -> true/false because undef could be X.
2580 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2581 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2582 }
2583
2584 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2585 const APInt &C2 = N2C->getAPIntValue();
2586 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2587 const APInt &C1 = N1C->getAPIntValue();
2588
2590 dl, VT, OpVT);
2591 }
2592 }
2593
2594 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2595 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2596
2597 if (N1CFP && N2CFP) {
2598 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2599 switch (Cond) {
2600 default: break;
2601 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2602 return GetUndefBooleanConstant();
2603 [[fallthrough]];
2604 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2605 OpVT);
2606 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2607 return GetUndefBooleanConstant();
2608 [[fallthrough]];
2610 R==APFloat::cmpLessThan, dl, VT,
2611 OpVT);
2612 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2613 return GetUndefBooleanConstant();
2614 [[fallthrough]];
2615 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2616 OpVT);
2617 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2618 return GetUndefBooleanConstant();
2619 [[fallthrough]];
2621 VT, OpVT);
2622 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2623 return GetUndefBooleanConstant();
2624 [[fallthrough]];
2626 R==APFloat::cmpEqual, dl, VT,
2627 OpVT);
2628 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2629 return GetUndefBooleanConstant();
2630 [[fallthrough]];
2632 R==APFloat::cmpEqual, dl, VT, OpVT);
2633 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2634 OpVT);
2635 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2636 OpVT);
2638 R==APFloat::cmpEqual, dl, VT,
2639 OpVT);
2640 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2641 OpVT);
2643 R==APFloat::cmpLessThan, dl, VT,
2644 OpVT);
2646 R==APFloat::cmpUnordered, dl, VT,
2647 OpVT);
2649 VT, OpVT);
2650 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2651 OpVT);
2652 }
2653 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2654 // Ensure that the constant occurs on the RHS.
2656 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2657 return SDValue();
2658 return getSetCC(dl, VT, N2, N1, SwappedCond);
2659 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2660 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2661 // If an operand is known to be a nan (or undef that could be a nan), we can
2662 // fold it.
2663 // Choosing NaN for the undef will always make unordered comparison succeed
2664 // and ordered comparison fails.
2665 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2666 switch (ISD::getUnorderedFlavor(Cond)) {
2667 default:
2668 llvm_unreachable("Unknown flavor!");
2669 case 0: // Known false.
2670 return getBoolConstant(false, dl, VT, OpVT);
2671 case 1: // Known true.
2672 return getBoolConstant(true, dl, VT, OpVT);
2673 case 2: // Undefined.
2674 return GetUndefBooleanConstant();
2675 }
2676 }
2677
2678 // Could not fold it.
2679 return SDValue();
2680}
2681
2682/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2683/// use this predicate to simplify operations downstream.
2685 unsigned BitWidth = Op.getScalarValueSizeInBits();
2687}
2688
2689/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2690/// this predicate to simplify operations downstream. Mask is known to be zero
2691/// for bits that V cannot have.
2693 unsigned Depth) const {
2694 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2695}
2696
2697/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2698/// DemandedElts. We use this predicate to simplify operations downstream.
2699/// Mask is known to be zero for bits that V cannot have.
2701 const APInt &DemandedElts,
2702 unsigned Depth) const {
2703 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2704}
2705
2706/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2707/// DemandedElts. We use this predicate to simplify operations downstream.
2709 unsigned Depth /* = 0 */) const {
2710 return computeKnownBits(V, DemandedElts, Depth).isZero();
2711}
2712
2713/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2715 unsigned Depth) const {
2716 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2717}
2718
2720 const APInt &DemandedElts,
2721 unsigned Depth) const {
2722 EVT VT = Op.getValueType();
2723 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2724
2725 unsigned NumElts = VT.getVectorNumElements();
2726 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2727
2728 APInt KnownZeroElements = APInt::getZero(NumElts);
2729 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2730 if (!DemandedElts[EltIdx])
2731 continue; // Don't query elements that are not demanded.
2732 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2733 if (MaskedVectorIsZero(Op, Mask, Depth))
2734 KnownZeroElements.setBit(EltIdx);
2735 }
2736 return KnownZeroElements;
2737}
2738
2739/// isSplatValue - Return true if the vector V has the same value
2740/// across all DemandedElts. For scalable vectors, we don't know the
2741/// number of lanes at compile time. Instead, we use a 1 bit APInt
2742/// to represent a conservative value for all lanes; that is, that
2743/// one bit value is implicitly splatted across all lanes.
2744bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2745 APInt &UndefElts, unsigned Depth) const {
2746 unsigned Opcode = V.getOpcode();
2747 EVT VT = V.getValueType();
2748 assert(VT.isVector() && "Vector type expected");
2749 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2750 "scalable demanded bits are ignored");
2751
2752 if (!DemandedElts)
2753 return false; // No demanded elts, better to assume we don't know anything.
2754
2755 if (Depth >= MaxRecursionDepth)
2756 return false; // Limit search depth.
2757
2758 // Deal with some common cases here that work for both fixed and scalable
2759 // vector types.
2760 switch (Opcode) {
2761 case ISD::SPLAT_VECTOR:
2762 UndefElts = V.getOperand(0).isUndef()
2763 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2764 : APInt(DemandedElts.getBitWidth(), 0);
2765 return true;
2766 case ISD::ADD:
2767 case ISD::SUB:
2768 case ISD::AND:
2769 case ISD::XOR:
2770 case ISD::OR: {
2771 APInt UndefLHS, UndefRHS;
2772 SDValue LHS = V.getOperand(0);
2773 SDValue RHS = V.getOperand(1);
2774 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2775 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
2776 UndefElts = UndefLHS | UndefRHS;
2777 return true;
2778 }
2779 return false;
2780 }
2781 case ISD::ABS:
2782 case ISD::TRUNCATE:
2783 case ISD::SIGN_EXTEND:
2784 case ISD::ZERO_EXTEND:
2785 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2786 default:
2787 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2788 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2789 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2790 Depth);
2791 break;
2792}
2793
2794 // We don't support other cases than those above for scalable vectors at
2795 // the moment.
2796 if (VT.isScalableVector())
2797 return false;
2798
2799 unsigned NumElts = VT.getVectorNumElements();
2800 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2801 UndefElts = APInt::getZero(NumElts);
2802
2803 switch (Opcode) {
2804 case ISD::BUILD_VECTOR: {
2805 SDValue Scl;
2806 for (unsigned i = 0; i != NumElts; ++i) {
2807 SDValue Op = V.getOperand(i);
2808 if (Op.isUndef()) {
2809 UndefElts.setBit(i);
2810 continue;
2811 }
2812 if (!DemandedElts[i])
2813 continue;
2814 if (Scl && Scl != Op)
2815 return false;
2816 Scl = Op;
2817 }
2818 return true;
2819 }
2820 case ISD::VECTOR_SHUFFLE: {
2821 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2822 APInt DemandedLHS = APInt::getZero(NumElts);
2823 APInt DemandedRHS = APInt::getZero(NumElts);
2824 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2825 for (int i = 0; i != (int)NumElts; ++i) {
2826 int M = Mask[i];
2827 if (M < 0) {
2828 UndefElts.setBit(i);
2829 continue;
2830 }
2831 if (!DemandedElts[i])
2832 continue;
2833 if (M < (int)NumElts)
2834 DemandedLHS.setBit(M);
2835 else
2836 DemandedRHS.setBit(M - NumElts);
2837 }
2838
2839 // If we aren't demanding either op, assume there's no splat.
2840 // If we are demanding both ops, assume there's no splat.
2841 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2842 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2843 return false;
2844
2845 // See if the demanded elts of the source op is a splat or we only demand
2846 // one element, which should always be a splat.
2847 // TODO: Handle source ops splats with undefs.
2848 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2849 APInt SrcUndefs;
2850 return (SrcElts.popcount() == 1) ||
2851 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2852 (SrcElts & SrcUndefs).isZero());
2853 };
2854 if (!DemandedLHS.isZero())
2855 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2856 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2857 }
2859 // Offset the demanded elts by the subvector index.
2860 SDValue Src = V.getOperand(0);
2861 // We don't support scalable vectors at the moment.
2862 if (Src.getValueType().isScalableVector())
2863 return false;
2864 uint64_t Idx = V.getConstantOperandVal(1);
2865 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2866 APInt UndefSrcElts;
2867 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2868 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2869 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2870 return true;
2871 }
2872 break;
2873 }
2877 // Widen the demanded elts by the src element count.
2878 SDValue Src = V.getOperand(0);
2879 // We don't support scalable vectors at the moment.
2880 if (Src.getValueType().isScalableVector())
2881 return false;
2882 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2883 APInt UndefSrcElts;
2884 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2885 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2886 UndefElts = UndefSrcElts.trunc(NumElts);
2887 return true;
2888 }
2889 break;
2890 }
2891 case ISD::BITCAST: {
2892 SDValue Src = V.getOperand(0);
2893 EVT SrcVT = Src.getValueType();
2894 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2895 unsigned BitWidth = VT.getScalarSizeInBits();
2896
2897 // Ignore bitcasts from unsupported types.
2898 // TODO: Add fp support?
2899 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2900 break;
2901
2902 // Bitcast 'small element' vector to 'large element' vector.
2903 if ((BitWidth % SrcBitWidth) == 0) {
2904 // See if each sub element is a splat.
2905 unsigned Scale = BitWidth / SrcBitWidth;
2906 unsigned NumSrcElts = SrcVT.getVectorNumElements();
2907 APInt ScaledDemandedElts =
2908 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
2909 for (unsigned I = 0; I != Scale; ++I) {
2910 APInt SubUndefElts;
2911 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
2912 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
2913 SubDemandedElts &= ScaledDemandedElts;
2914 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
2915 return false;
2916 // TODO: Add support for merging sub undef elements.
2917 if (!SubUndefElts.isZero())
2918 return false;
2919 }
2920 return true;
2921 }
2922 break;
2923 }
2924 }
2925
2926 return false;
2927}
2928
2929/// Helper wrapper to main isSplatValue function.
2930bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
2931 EVT VT = V.getValueType();
2932 assert(VT.isVector() && "Vector type expected");
2933
2934 APInt UndefElts;
2935 // Since the number of lanes in a scalable vector is unknown at compile time,
2936 // we track one bit which is implicitly broadcast to all lanes. This means
2937 // that all lanes in a scalable vector are considered demanded.
2938 APInt DemandedElts
2940 return isSplatValue(V, DemandedElts, UndefElts) &&
2941 (AllowUndefs || !UndefElts);
2942}
2943
2946
2947 EVT VT = V.getValueType();
2948 unsigned Opcode = V.getOpcode();
2949 switch (Opcode) {
2950 default: {
2951 APInt UndefElts;
2952 // Since the number of lanes in a scalable vector is unknown at compile time,
2953 // we track one bit which is implicitly broadcast to all lanes. This means
2954 // that all lanes in a scalable vector are considered demanded.
2955 APInt DemandedElts
2957
2958 if (isSplatValue(V, DemandedElts, UndefElts)) {
2959 if (VT.isScalableVector()) {
2960 // DemandedElts and UndefElts are ignored for scalable vectors, since
2961 // the only supported cases are SPLAT_VECTOR nodes.
2962 SplatIdx = 0;
2963 } else {
2964 // Handle case where all demanded elements are UNDEF.
2965 if (DemandedElts.isSubsetOf(UndefElts)) {
2966 SplatIdx = 0;
2967 return getUNDEF(VT);
2968 }
2969 SplatIdx = (UndefElts & DemandedElts).countr_one();
2970 }
2971 return V;
2972 }
2973 break;
2974 }
2975 case ISD::SPLAT_VECTOR:
2976 SplatIdx = 0;
2977 return V;
2978 case ISD::VECTOR_SHUFFLE: {
2979 assert(!VT.isScalableVector());
2980 // Check if this is a shuffle node doing a splat.
2981 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
2982 // getTargetVShiftNode currently struggles without the splat source.
2983 auto *SVN = cast<ShuffleVectorSDNode>(V);
2984 if (!SVN->isSplat())
2985 break;
2986 int Idx = SVN->getSplatIndex();
2987 int NumElts = V.getValueType().getVectorNumElements();
2988 SplatIdx = Idx % NumElts;
2989 return V.getOperand(Idx / NumElts);
2990 }
2991 }
2992
2993 return SDValue();
2994}
2995
2997 int SplatIdx;
2998 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
2999 EVT SVT = SrcVector.getValueType().getScalarType();
3000 EVT LegalSVT = SVT;
3001 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3002 if (!SVT.isInteger())
3003 return SDValue();
3004 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3005 if (LegalSVT.bitsLT(SVT))
3006 return SDValue();
3007 }
3008 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
3009 getVectorIdxConstant(SplatIdx, SDLoc(V)));
3010 }
3011 return SDValue();
3012}
3013
3014std::optional<ConstantRange>
3016 unsigned Depth) const {
3017 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3018 V.getOpcode() == ISD::SRA) &&
3019 "Unknown shift node");
3020 // Shifting more than the bitwidth is not valid.
3021 unsigned BitWidth = V.getScalarValueSizeInBits();
3022
3023 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3024 const APInt &ShAmt = Cst->getAPIntValue();
3025 if (ShAmt.uge(BitWidth))
3026 return std::nullopt;
3027 return ConstantRange(ShAmt);
3028 }
3029
3030 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3031 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3032 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3033 if (!DemandedElts[i])
3034 continue;
3035 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3036 if (!SA) {
3037 MinAmt = MaxAmt = nullptr;
3038 break;
3039 }
3040 const APInt &ShAmt = SA->getAPIntValue();
3041 if (ShAmt.uge(BitWidth))
3042 return std::nullopt;
3043 if (!MinAmt || MinAmt->ugt(ShAmt))
3044 MinAmt = &ShAmt;
3045 if (!MaxAmt || MaxAmt->ult(ShAmt))
3046 MaxAmt = &ShAmt;
3047 }
3048 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3049 "Failed to find matching min/max shift amounts");
3050 if (MinAmt && MaxAmt)
3051 return ConstantRange(*MinAmt, *MaxAmt + 1);
3052 }
3053
3054 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3055 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3056 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3057 if (KnownAmt.getMaxValue().ult(BitWidth))
3058 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3059
3060 return std::nullopt;
3061}
3062
3063std::optional<uint64_t>
3065 unsigned Depth) const {
3066 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3067 V.getOpcode() == ISD::SRA) &&
3068 "Unknown shift node");
3069 if (std::optional<ConstantRange> AmtRange =
3070 getValidShiftAmountRange(V, DemandedElts, Depth))
3071 if (const APInt *ShAmt = AmtRange->getSingleElement())
3072 return ShAmt->getZExtValue();
3073 return std::nullopt;
3074}
3075
3076std::optional<uint64_t>
3078 EVT VT = V.getValueType();
3079 APInt DemandedElts = VT.isFixedLengthVector()
3081 : APInt(1, 1);
3082 return getValidShiftAmount(V, DemandedElts, Depth);
3083}
3084
3085std::optional<uint64_t>
3087 unsigned Depth) const {
3088 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3089 V.getOpcode() == ISD::SRA) &&
3090 "Unknown shift node");
3091 if (std::optional<ConstantRange> AmtRange =
3092 getValidShiftAmountRange(V, DemandedElts, Depth))
3093 return AmtRange->getUnsignedMin().getZExtValue();
3094 return std::nullopt;
3095}
3096
3097std::optional<uint64_t>
3099 EVT VT = V.getValueType();
3100 APInt DemandedElts = VT.isFixedLengthVector()
3102 : APInt(1, 1);
3103 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3104}
3105
3106std::optional<uint64_t>
3108 unsigned Depth) const {
3109 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3110 V.getOpcode() == ISD::SRA) &&
3111 "Unknown shift node");
3112 if (std::optional<ConstantRange> AmtRange =
3113 getValidShiftAmountRange(V, DemandedElts, Depth))
3114 return AmtRange->getUnsignedMax().getZExtValue();
3115 return std::nullopt;
3116}
3117
3118std::optional<uint64_t>
3120 EVT VT = V.getValueType();
3121 APInt DemandedElts = VT.isFixedLengthVector()
3123 : APInt(1, 1);
3124 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3125}
3126
3127/// Determine which bits of Op are known to be either zero or one and return
3128/// them in Known. For vectors, the known bits are those that are shared by
3129/// every vector element.
3131 EVT VT = Op.getValueType();
3132
3133 // Since the number of lanes in a scalable vector is unknown at compile time,
3134 // we track one bit which is implicitly broadcast to all lanes. This means
3135 // that all lanes in a scalable vector are considered demanded.
3136 APInt DemandedElts = VT.isFixedLengthVector()
3138 : APInt(1, 1);
3139 return computeKnownBits(Op, DemandedElts, Depth);
3140}
3141
3142/// Determine which bits of Op are known to be either zero or one and return
3143/// them in Known. The DemandedElts argument allows us to only collect the known
3144/// bits that are shared by the requested vector elements.
3146 unsigned Depth) const {
3147 unsigned BitWidth = Op.getScalarValueSizeInBits();
3148
3149 KnownBits Known(BitWidth); // Don't know anything.
3150
3151 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3152 // We know all of the bits for a constant!
3153 return KnownBits::makeConstant(C->getAPIntValue());
3154 }
3155 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3156 // We know all of the bits for a constant fp!
3157 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3158 }
3159
3160 if (Depth >= MaxRecursionDepth)
3161 return Known; // Limit search depth.
3162
3163 KnownBits Known2;
3164 unsigned NumElts = DemandedElts.getBitWidth();
3165 assert((!Op.getValueType().isFixedLengthVector() ||
3166 NumElts == Op.getValueType().getVectorNumElements()) &&
3167 "Unexpected vector size");
3168
3169 if (!DemandedElts)
3170 return Known; // No demanded elts, better to assume we don't know anything.
3171
3172 unsigned Opcode = Op.getOpcode();
3173 switch (Opcode) {
3174 case ISD::MERGE_VALUES:
3175 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3176 Depth + 1);
3177 case ISD::SPLAT_VECTOR: {
3178 SDValue SrcOp = Op.getOperand(0);
3179 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3180 "Expected SPLAT_VECTOR implicit truncation");
3181 // Implicitly truncate the bits to match the official semantics of
3182 // SPLAT_VECTOR.
3183 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3184 break;
3185 }
3187 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3188 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3189 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3190 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3191 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3192 }
3193 break;
3194 }
3195 case ISD::STEP_VECTOR: {
3196 const APInt &Step = Op.getConstantOperandAPInt(0);
3197
3198 if (Step.isPowerOf2())
3199 Known.Zero.setLowBits(Step.logBase2());
3200
3202
3203 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3204 break;
3205 const APInt MinNumElts =
3206 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3207
3208 bool Overflow;
3209 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3211 .umul_ov(MinNumElts, Overflow);
3212 if (Overflow)
3213 break;
3214
3215 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3216 if (Overflow)
3217 break;
3218
3219 Known.Zero.setHighBits(MaxValue.countl_zero());
3220 break;
3221 }
3222 case ISD::BUILD_VECTOR:
3223 assert(!Op.getValueType().isScalableVector());
3224 // Collect the known bits that are shared by every demanded vector element.
3225 Known.Zero.setAllBits(); Known.One.setAllBits();
3226 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3227 if (!DemandedElts[i])
3228 continue;
3229
3230 SDValue SrcOp = Op.getOperand(i);
3231 Known2 = computeKnownBits(SrcOp, Depth + 1);
3232
3233 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3234 if (SrcOp.getValueSizeInBits() != BitWidth) {
3235 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3236 "Expected BUILD_VECTOR implicit truncation");
3237 Known2 = Known2.trunc(BitWidth);
3238 }
3239
3240 // Known bits are the values that are shared by every demanded element.
3241 Known = Known.intersectWith(Known2);
3242
3243 // If we don't know any bits, early out.
3244 if (Known.isUnknown())
3245 break;
3246 }
3247 break;
3248 case ISD::VECTOR_SHUFFLE: {
3249 assert(!Op.getValueType().isScalableVector());
3250 // Collect the known bits that are shared by every vector element referenced
3251 // by the shuffle.
3252 APInt DemandedLHS, DemandedRHS;
3253 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3254 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3255 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3256 DemandedLHS, DemandedRHS))
3257 break;
3258
3259 // Known bits are the values that are shared by every demanded element.
3260 Known.Zero.setAllBits(); Known.One.setAllBits();
3261 if (!!DemandedLHS) {
3262 SDValue LHS = Op.getOperand(0);
3263 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3264 Known = Known.intersectWith(Known2);
3265 }
3266 // If we don't know any bits, early out.
3267 if (Known.isUnknown())
3268 break;
3269 if (!!DemandedRHS) {
3270 SDValue RHS = Op.getOperand(1);
3271 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3272 Known = Known.intersectWith(Known2);
3273 }
3274 break;
3275 }
3276 case ISD::VSCALE: {
3278 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3279 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3280 break;
3281 }
3282 case ISD::CONCAT_VECTORS: {
3283 if (Op.getValueType().isScalableVector())
3284 break;
3285 // Split DemandedElts and test each of the demanded subvectors.
3286 Known.Zero.setAllBits(); Known.One.setAllBits();
3287 EVT SubVectorVT = Op.getOperand(0).getValueType();
3288 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3289 unsigned NumSubVectors = Op.getNumOperands();
3290 for (unsigned i = 0; i != NumSubVectors; ++i) {
3291 APInt DemandedSub =
3292 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3293 if (!!DemandedSub) {
3294 SDValue Sub = Op.getOperand(i);
3295 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3296 Known = Known.intersectWith(Known2);
3297 }
3298 // If we don't know any bits, early out.
3299 if (Known.isUnknown())
3300 break;
3301 }
3302 break;
3303 }
3304 case ISD::INSERT_SUBVECTOR: {
3305 if (Op.getValueType().isScalableVector())
3306 break;
3307 // Demand any elements from the subvector and the remainder from the src its
3308 // inserted into.
3309 SDValue Src = Op.getOperand(0);
3310 SDValue Sub = Op.getOperand(1);
3311 uint64_t Idx = Op.getConstantOperandVal(2);
3312 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3313 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3314 APInt DemandedSrcElts = DemandedElts;
3315 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3316
3317 Known.One.setAllBits();
3318 Known.Zero.setAllBits();
3319 if (!!DemandedSubElts) {
3320 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3321 if (Known.isUnknown())
3322 break; // early-out.
3323 }
3324 if (!!DemandedSrcElts) {
3325 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3326 Known = Known.intersectWith(Known2);
3327 }
3328 break;
3329 }
3331 // Offset the demanded elts by the subvector index.
3332 SDValue Src = Op.getOperand(0);
3333 // Bail until we can represent demanded elements for scalable vectors.
3334 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3335 break;
3336 uint64_t Idx = Op.getConstantOperandVal(1);
3337 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3338 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3339 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3340 break;
3341 }
3342 case ISD::SCALAR_TO_VECTOR: {
3343 if (Op.getValueType().isScalableVector())
3344 break;
3345 // We know about scalar_to_vector as much as we know about it source,
3346 // which becomes the first element of otherwise unknown vector.
3347 if (DemandedElts != 1)
3348 break;
3349
3350 SDValue N0 = Op.getOperand(0);
3351 Known = computeKnownBits(N0, Depth + 1);
3352 if (N0.getValueSizeInBits() != BitWidth)
3353 Known = Known.trunc(BitWidth);
3354
3355 break;
3356 }
3357 case ISD::BITCAST: {
3358 if (Op.getValueType().isScalableVector())
3359 break;
3360
3361 SDValue N0 = Op.getOperand(0);
3362 EVT SubVT = N0.getValueType();
3363 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3364
3365 // Ignore bitcasts from unsupported types.
3366 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3367 break;
3368
3369 // Fast handling of 'identity' bitcasts.
3370 if (BitWidth == SubBitWidth) {
3371 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3372 break;
3373 }
3374
3375 bool IsLE = getDataLayout().isLittleEndian();
3376
3377 // Bitcast 'small element' vector to 'large element' scalar/vector.
3378 if ((BitWidth % SubBitWidth) == 0) {
3379 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3380
3381 // Collect known bits for the (larger) output by collecting the known
3382 // bits from each set of sub elements and shift these into place.
3383 // We need to separately call computeKnownBits for each set of
3384 // sub elements as the knownbits for each is likely to be different.
3385 unsigned SubScale = BitWidth / SubBitWidth;
3386 APInt SubDemandedElts(NumElts * SubScale, 0);
3387 for (unsigned i = 0; i != NumElts; ++i)
3388 if (DemandedElts[i])
3389 SubDemandedElts.setBit(i * SubScale);
3390
3391 for (unsigned i = 0; i != SubScale; ++i) {
3392 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3393 Depth + 1);
3394 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3395 Known.insertBits(Known2, SubBitWidth * Shifts);
3396 }
3397 }
3398
3399 // Bitcast 'large element' scalar/vector to 'small element' vector.
3400 if ((SubBitWidth % BitWidth) == 0) {
3401 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3402
3403 // Collect known bits for the (smaller) output by collecting the known
3404 // bits from the overlapping larger input elements and extracting the
3405 // sub sections we actually care about.
3406 unsigned SubScale = SubBitWidth / BitWidth;
3407 APInt SubDemandedElts =
3408 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3409 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3410
3411 Known.Zero.setAllBits(); Known.One.setAllBits();
3412 for (unsigned i = 0; i != NumElts; ++i)
3413 if (DemandedElts[i]) {
3414 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3415 unsigned Offset = (Shifts % SubScale) * BitWidth;
3416 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3417 // If we don't know any bits, early out.
3418 if (Known.isUnknown())
3419 break;
3420 }
3421 }
3422 break;
3423 }
3424 case ISD::AND:
3425 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3426 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3427
3428 Known &= Known2;
3429 break;
3430 case ISD::OR:
3431 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3432 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3433
3434 Known |= Known2;
3435 break;
3436 case ISD::XOR:
3437 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3438 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3439
3440 Known ^= Known2;
3441 break;
3442 case ISD::MUL: {
3443 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3444 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3445 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3446 // TODO: SelfMultiply can be poison, but not undef.
3447 if (SelfMultiply)
3448 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3449 Op.getOperand(0), DemandedElts, false, Depth + 1);
3450 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3451
3452 // If the multiplication is known not to overflow, the product of a number
3453 // with itself is non-negative. Only do this if we didn't already computed
3454 // the opposite value for the sign bit.
3455 if (Op->getFlags().hasNoSignedWrap() &&
3456 Op.getOperand(0) == Op.getOperand(1) &&
3457 !Known.isNegative())
3458 Known.makeNonNegative();
3459 break;
3460 }
3461 case ISD::MULHU: {
3462 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3463 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3464 Known = KnownBits::mulhu(Known, Known2);
3465 break;
3466 }
3467 case ISD::MULHS: {
3468 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3469 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3470 Known = KnownBits::mulhs(Known, Known2);
3471 break;
3472 }
3473 case ISD::ABDU: {
3474 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3475 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3476 Known = KnownBits::abdu(Known, Known2);
3477 break;
3478 }
3479 case ISD::ABDS: {
3480 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3481 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3482 Known = KnownBits::abds(Known, Known2);
3483 unsigned SignBits1 =
3484 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3485 if (SignBits1 == 1)
3486 break;
3487 unsigned SignBits0 =
3488 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3489 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3490 break;
3491 }
3492 case ISD::UMUL_LOHI: {
3493 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3494 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3495 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3496 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3497 if (Op.getResNo() == 0)
3498 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3499 else
3500 Known = KnownBits::mulhu(Known, Known2);
3501 break;
3502 }
3503 case ISD::SMUL_LOHI: {
3504 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3505 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3506 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3507 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3508 if (Op.getResNo() == 0)
3509 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3510 else
3511 Known = KnownBits::mulhs(Known, Known2);
3512 break;
3513 }
3514 case ISD::AVGFLOORU: {
3515 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3516 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3517 Known = KnownBits::avgFloorU(Known, Known2);
3518 break;
3519 }
3520 case ISD::AVGCEILU: {
3521 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3522 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3523 Known = KnownBits::avgCeilU(Known, Known2);
3524 break;
3525 }
3526 case ISD::AVGFLOORS: {
3527 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3528 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3529 Known = KnownBits::avgFloorS(Known, Known2);
3530 break;
3531 }
3532 case ISD::AVGCEILS: {
3533 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3534 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3535 Known = KnownBits::avgCeilS(Known, Known2);
3536 break;
3537 }
3538 case ISD::SELECT:
3539 case ISD::VSELECT:
3540 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3541 // If we don't know any bits, early out.
3542 if (Known.isUnknown())
3543 break;
3544 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3545
3546 // Only known if known in both the LHS and RHS.
3547 Known = Known.intersectWith(Known2);
3548 break;
3549 case ISD::SELECT_CC:
3550 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3551 // If we don't know any bits, early out.
3552 if (Known.isUnknown())
3553 break;
3554 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3555
3556 // Only known if known in both the LHS and RHS.
3557 Known = Known.intersectWith(Known2);
3558 break;
3559 case ISD::SMULO:
3560 case ISD::UMULO:
3561 if (Op.getResNo() != 1)
3562 break;
3563 // The boolean result conforms to getBooleanContents.
3564 // If we know the result of a setcc has the top bits zero, use this info.
3565 // We know that we have an integer-based boolean since these operations
3566 // are only available for integer.
3567 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3569 BitWidth > 1)
3570 Known.Zero.setBitsFrom(1);
3571 break;
3572 case ISD::SETCC:
3573 case ISD::SETCCCARRY:
3574 case ISD::STRICT_FSETCC:
3575 case ISD::STRICT_FSETCCS: {
3576 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3577 // If we know the result of a setcc has the top bits zero, use this info.
3578 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3580 BitWidth > 1)
3581 Known.Zero.setBitsFrom(1);
3582 break;
3583 }
3584 case ISD::SHL: {
3585 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3586 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3587
3588 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3589 bool NSW = Op->getFlags().hasNoSignedWrap();
3590
3591 bool ShAmtNonZero = Known2.isNonZero();
3592
3593 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3594
3595 // Minimum shift low bits are known zero.
3596 if (std::optional<uint64_t> ShMinAmt =
3597 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3598 Known.Zero.setLowBits(*ShMinAmt);
3599 break;
3600 }
3601 case ISD::SRL:
3602 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3603 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3604 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3605 Op->getFlags().hasExact());
3606
3607 // Minimum shift high bits are known zero.
3608 if (std::optional<uint64_t> ShMinAmt =
3609 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3610 Known.Zero.setHighBits(*ShMinAmt);
3611 break;
3612 case ISD::SRA:
3613 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3614 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3615 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3616 Op->getFlags().hasExact());
3617 break;
3618 case ISD::FSHL:
3619 case ISD::FSHR:
3620 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3621 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3622
3623 // For fshl, 0-shift returns the 1st arg.
3624 // For fshr, 0-shift returns the 2nd arg.
3625 if (Amt == 0) {
3626 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3627 DemandedElts, Depth + 1);
3628 break;
3629 }
3630
3631 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3632 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3633 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3634 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3635 if (Opcode == ISD::FSHL) {
3636 Known.One <<= Amt;
3637 Known.Zero <<= Amt;
3638 Known2.One.lshrInPlace(BitWidth - Amt);
3639 Known2.Zero.lshrInPlace(BitWidth - Amt);
3640 } else {
3641 Known.One <<= BitWidth - Amt;
3642 Known.Zero <<= BitWidth - Amt;
3643 Known2.One.lshrInPlace(Amt);
3644 Known2.Zero.lshrInPlace(Amt);
3645 }
3646 Known = Known.unionWith(Known2);
3647 }
3648 break;
3649 case ISD::SHL_PARTS:
3650 case ISD::SRA_PARTS:
3651 case ISD::SRL_PARTS: {
3652 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3653
3654 // Collect lo/hi source values and concatenate.
3655 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3656 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3657 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3658 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3659 Known = Known2.concat(Known);
3660
3661 // Collect shift amount.
3662 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3663
3664 if (Opcode == ISD::SHL_PARTS)
3665 Known = KnownBits::shl(Known, Known2);
3666 else if (Opcode == ISD::SRA_PARTS)
3667 Known = KnownBits::ashr(Known, Known2);
3668 else // if (Opcode == ISD::SRL_PARTS)
3669 Known = KnownBits::lshr(Known, Known2);
3670
3671 // TODO: Minimum shift low/high bits are known zero.
3672
3673 if (Op.getResNo() == 0)
3674 Known = Known.extractBits(LoBits, 0);
3675 else
3676 Known = Known.extractBits(HiBits, LoBits);
3677 break;
3678 }
3680 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3681 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3682 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3683 break;
3684 }
3685 case ISD::CTTZ:
3686 case ISD::CTTZ_ZERO_UNDEF: {
3687 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3688 // If we have a known 1, its position is our upper bound.
3689 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3690 unsigned LowBits = llvm::bit_width(PossibleTZ);
3691 Known.Zero.setBitsFrom(LowBits);
3692 break;
3693 }
3694 case ISD::CTLZ:
3695 case ISD::CTLZ_ZERO_UNDEF: {
3696 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3697 // If we have a known 1, its position is our upper bound.
3698 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3699 unsigned LowBits = llvm::bit_width(PossibleLZ);
3700 Known.Zero.setBitsFrom(LowBits);
3701 break;
3702 }
3703 case ISD::CTPOP: {
3704 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3705 // If we know some of the bits are zero, they can't be one.
3706 unsigned PossibleOnes = Known2.countMaxPopulation();
3707 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3708 break;
3709 }
3710 case ISD::PARITY: {
3711 // Parity returns 0 everywhere but the LSB.
3712 Known.Zero.setBitsFrom(1);
3713 break;
3714 }
3715 case ISD::LOAD: {
3716 LoadSDNode *LD = cast<LoadSDNode>(Op);
3717 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3718 if (ISD::isNON_EXTLoad(LD) && Cst) {
3719 // Determine any common known bits from the loaded constant pool value.
3720 Type *CstTy = Cst->getType();
3721 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3722 !Op.getValueType().isScalableVector()) {
3723 // If its a vector splat, then we can (quickly) reuse the scalar path.
3724 // NOTE: We assume all elements match and none are UNDEF.
3725 if (CstTy->isVectorTy()) {
3726 if (const Constant *Splat = Cst->getSplatValue()) {
3727 Cst = Splat;
3728 CstTy = Cst->getType();
3729 }
3730 }
3731 // TODO - do we need to handle different bitwidths?
3732 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3733 // Iterate across all vector elements finding common known bits.
3734 Known.One.setAllBits();
3735 Known.Zero.setAllBits();
3736 for (unsigned i = 0; i != NumElts; ++i) {
3737 if (!DemandedElts[i])
3738 continue;
3739 if (Constant *Elt = Cst->getAggregateElement(i)) {
3740 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3741 const APInt &Value = CInt->getValue();
3742 Known.One &= Value;
3743 Known.Zero &= ~Value;
3744 continue;
3745 }
3746 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3747 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3748 Known.One &= Value;
3749 Known.Zero &= ~Value;
3750 continue;
3751 }
3752 }
3753 Known.One.clearAllBits();
3754 Known.Zero.clearAllBits();
3755 break;
3756 }
3757 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3758 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3759 Known = KnownBits::makeConstant(CInt->getValue());
3760 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3761 Known =
3762 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3763 }
3764 }
3765 }
3766 } else if (Op.getResNo() == 0) {
3767 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
3768 ? LD->getMemoryVT().getFixedSizeInBits()
3769 : BitWidth);
3770 EVT VT = Op.getValueType();
3771 // Fill in any known bits from range information. There are 3 types being
3772 // used. The results VT (same vector elt size as BitWidth), the loaded
3773 // MemoryVT (which may or may not be vector) and the range VTs original
3774 // type. The range matadata needs the full range (i.e
3775 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
3776 // if it is know. These are then extended to the original VT sizes below.
3777 if (const MDNode *MD = LD->getRanges()) {
3779 if (VT.isVector()) {
3780 // Handle truncation to the first demanded element.
3781 // TODO: Figure out which demanded elements are covered
3782 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
3783 break;
3784 Known0 = Known0.trunc(BitWidth);
3785 }
3786 }
3787
3788 if (LD->getMemoryVT().isVector())
3789 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
3790
3791 // Extend the Known bits from memory to the size of the result.
3792 if (ISD::isZEXTLoad(Op.getNode()))
3793 Known = Known0.zext(BitWidth);
3794 else if (ISD::isSEXTLoad(Op.getNode()))
3795 Known = Known0.sext(BitWidth);
3796 else if (ISD::isEXTLoad(Op.getNode()))
3797 Known = Known0.anyext(BitWidth);
3798 else
3799 Known = Known0;
3800 assert(Known.getBitWidth() == BitWidth);
3801 return Known;
3802 }
3803 break;
3804 }
3806 if (Op.getValueType().isScalableVector())
3807 break;
3808 EVT InVT = Op.getOperand(0).getValueType();
3809 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3810 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3811 Known = Known.zext(BitWidth);
3812 break;
3813 }
3814 case ISD::ZERO_EXTEND: {
3815 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3816 Known = Known.zext(BitWidth);
3817 break;
3818 }
3820 if (Op.getValueType().isScalableVector())
3821 break;
3822 EVT InVT = Op.getOperand(0).getValueType();
3823 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3824 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3825 // If the sign bit is known to be zero or one, then sext will extend
3826 // it to the top bits, else it will just zext.
3827 Known = Known.sext(BitWidth);
3828 break;
3829 }
3830 case ISD::SIGN_EXTEND: {
3831 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3832 // If the sign bit is known to be zero or one, then sext will extend
3833 // it to the top bits, else it will just zext.
3834 Known = Known.sext(BitWidth);
3835 break;
3836 }
3838 if (Op.getValueType().isScalableVector())
3839 break;
3840 EVT InVT = Op.getOperand(0).getValueType();
3841 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3842 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3843 Known = Known.anyext(BitWidth);
3844 break;
3845 }
3846 case ISD::ANY_EXTEND: {
3847 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3848 Known = Known.anyext(BitWidth);
3849 break;
3850 }
3851 case ISD::TRUNCATE: {
3852 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3853 Known = Known.trunc(BitWidth);
3854 break;
3855 }
3856 case ISD::AssertZext: {
3857 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3859 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3860 Known.Zero |= (~InMask);
3861 Known.One &= (~Known.Zero);
3862 break;
3863 }
3864 case ISD::AssertAlign: {
3865 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3866 assert(LogOfAlign != 0);
3867
3868 // TODO: Should use maximum with source
3869 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3870 // well as clearing one bits.
3871 Known.Zero.setLowBits(LogOfAlign);
3872 Known.One.clearLowBits(LogOfAlign);
3873 break;
3874 }
3875 case ISD::FGETSIGN:
3876 // All bits are zero except the low bit.
3877 Known.Zero.setBitsFrom(1);
3878 break;
3879 case ISD::ADD:
3880 case ISD::SUB: {
3881 SDNodeFlags Flags = Op.getNode()->getFlags();
3882 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3883 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3885 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
3886 Flags.hasNoUnsignedWrap(), Known, Known2);
3887 break;
3888 }
3889 case ISD::USUBO:
3890 case ISD::SSUBO:
3891 case ISD::USUBO_CARRY:
3892 case ISD::SSUBO_CARRY:
3893 if (Op.getResNo() == 1) {
3894 // If we know the result of a setcc has the top bits zero, use this info.
3895 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3897 BitWidth > 1)
3898 Known.Zero.setBitsFrom(1);
3899 break;
3900 }
3901 [[fallthrough]];
3902 case ISD::SUBC: {
3903 assert(Op.getResNo() == 0 &&
3904 "We only compute knownbits for the difference here.");
3905
3906 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
3907 KnownBits Borrow(1);
3908 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
3909 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3910 // Borrow has bit width 1
3911 Borrow = Borrow.trunc(1);
3912 } else {
3913 Borrow.setAllZero();
3914 }
3915
3916 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3917 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3918 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
3919 break;
3920 }
3921 case ISD::UADDO:
3922 case ISD::SADDO:
3923 case ISD::UADDO_CARRY:
3924 case ISD::SADDO_CARRY:
3925 if (Op.getResNo() == 1) {
3926 // If we know the result of a setcc has the top bits zero, use this info.
3927 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
3929 BitWidth > 1)
3930 Known.Zero.setBitsFrom(1);
3931 break;
3932 }
3933 [[fallthrough]];
3934 case ISD::ADDC:
3935 case ISD::ADDE: {
3936 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
3937
3938 // With ADDE and UADDO_CARRY, a carry bit may be added in.
3939 KnownBits Carry(1);
3940 if (Opcode == ISD::ADDE)
3941 // Can't track carry from glue, set carry to unknown.
3942 Carry.resetAll();
3943 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
3944 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3945 // Carry has bit width 1
3946 Carry = Carry.trunc(1);
3947 } else {
3948 Carry.setAllZero();
3949 }
3950
3951 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3952 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3953 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
3954 break;
3955 }
3956 case ISD::UDIV: {
3957 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3958 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3959 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
3960 break;
3961 }
3962 case ISD::SDIV: {
3963 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3964 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3965 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
3966 break;
3967 }
3968 case ISD::SREM: {
3969 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3970 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3971 Known = KnownBits::srem(Known, Known2);
3972 break;
3973 }
3974 case ISD::UREM: {
3975 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3976 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3977 Known = KnownBits::urem(Known, Known2);
3978 break;
3979 }
3980 case ISD::EXTRACT_ELEMENT: {
3981 Known = computeKnownBits(Op.getOperand(0), Depth+1);
3982 const unsigned Index = Op.getConstantOperandVal(1);
3983 const unsigned EltBitWidth = Op.getValueSizeInBits();
3984
3985 // Remove low part of known bits mask
3986 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3987 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
3988
3989 // Remove high part of known bit mask
3990 Known = Known.trunc(EltBitWidth);
3991 break;
3992 }
3994 SDValue InVec = Op.getOperand(0);
3995 SDValue EltNo = Op.getOperand(1);
3996 EVT VecVT = InVec.getValueType();
3997 // computeKnownBits not yet implemented for scalable vectors.
3998 if (VecVT.isScalableVector())
3999 break;
4000 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4001 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4002
4003 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4004 // anything about the extended bits.
4005 if (BitWidth > EltBitWidth)
4006 Known = Known.trunc(EltBitWidth);
4007
4008 // If we know the element index, just demand that vector element, else for
4009 // an unknown element index, ignore DemandedElts and demand them all.
4010 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4011 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4012 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4013 DemandedSrcElts =
4014 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4015
4016 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4017 if (BitWidth > EltBitWidth)
4018 Known = Known.anyext(BitWidth);
4019 break;
4020 }
4022 if (Op.getValueType().isScalableVector())
4023 break;
4024
4025 // If we know the element index, split the demand between the
4026 // source vector and the inserted element, otherwise assume we need
4027 // the original demanded vector elements and the value.
4028 SDValue InVec = Op.getOperand(0);
4029 SDValue InVal = Op.getOperand(1);
4030 SDValue EltNo = Op.getOperand(2);
4031 bool DemandedVal = true;
4032 APInt DemandedVecElts = DemandedElts;
4033 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4034 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4035 unsigned EltIdx = CEltNo->getZExtValue();
4036 DemandedVal = !!DemandedElts[EltIdx];
4037 DemandedVecElts.clearBit(EltIdx);
4038 }
4039 Known.One.setAllBits();
4040 Known.Zero.setAllBits();
4041 if (DemandedVal) {
4042 Known2 = computeKnownBits(InVal, Depth + 1);
4043 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4044 }
4045 if (!!DemandedVecElts) {
4046 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4047 Known = Known.intersectWith(Known2);
4048 }
4049 break;
4050 }
4051 case ISD::BITREVERSE: {
4052 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4053 Known = Known2.reverseBits();
4054 break;
4055 }
4056 case ISD::BSWAP: {
4057 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4058 Known = Known2.byteSwap();
4059 break;
4060 }
4061 case ISD::ABS: {
4062 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4063 Known = Known2.abs();
4064 Known.Zero.setHighBits(
4065 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4066 break;
4067 }
4068 case ISD::USUBSAT: {
4069 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4070 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4071 Known = KnownBits::usub_sat(Known, Known2);
4072 break;
4073 }
4074 case ISD::UMIN: {
4075 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4076 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4077 Known = KnownBits::umin(Known, Known2);
4078 break;
4079 }
4080 case ISD::UMAX: {
4081 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4082 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4083 Known = KnownBits::umax(Known, Known2);
4084 break;
4085 }
4086 case ISD::SMIN:
4087 case ISD::SMAX: {
4088 // If we have a clamp pattern, we know that the number of sign bits will be
4089 // the minimum of the clamp min/max range.
4090 bool IsMax = (Opcode == ISD::SMAX);
4091 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4092 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4093 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4094 CstHigh =
4095 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4096 if (CstLow && CstHigh) {
4097 if (!IsMax)
4098 std::swap(CstLow, CstHigh);
4099
4100 const APInt &ValueLow = CstLow->getAPIntValue();
4101 const APInt &ValueHigh = CstHigh->getAPIntValue();
4102 if (ValueLow.sle(ValueHigh)) {
4103 unsigned LowSignBits = ValueLow.getNumSignBits();
4104 unsigned HighSignBits = ValueHigh.getNumSignBits();
4105 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4106 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4107 Known.One.setHighBits(MinSignBits);
4108 break;
4109 }
4110 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4111 Known.Zero.setHighBits(MinSignBits);
4112 break;
4113 }
4114 }
4115 }
4116
4117 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4118 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4119 if (IsMax)
4120 Known = KnownBits::smax(Known, Known2);
4121 else
4122 Known = KnownBits::smin(Known, Known2);
4123
4124 // For SMAX, if CstLow is non-negative we know the result will be
4125 // non-negative and thus all sign bits are 0.
4126 // TODO: There's an equivalent of this for smin with negative constant for
4127 // known ones.
4128 if (IsMax && CstLow) {
4129 const APInt &ValueLow = CstLow->getAPIntValue();
4130 if (ValueLow.isNonNegative()) {
4131 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4132 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4133 }
4134 }
4135
4136 break;
4137 }
4138 case ISD::UINT_TO_FP: {
4139 Known.makeNonNegative();
4140 break;
4141 }
4142 case ISD::SINT_TO_FP: {
4143 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4144 if (Known2.isNonNegative())
4145 Known.makeNonNegative();
4146 else if (Known2.isNegative())
4147 Known.makeNegative();
4148 break;
4149 }
4150 case ISD::FP_TO_UINT_SAT: {
4151 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4152 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4154 break;
4155 }
4157 if (Op.getResNo() == 1) {
4158 // The boolean result conforms to getBooleanContents.
4159 // If we know the result of a setcc has the top bits zero, use this info.
4160 // We know that we have an integer-based boolean since these operations
4161 // are only available for integer.
4162 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4164 BitWidth > 1)
4165 Known.Zero.setBitsFrom(1);
4166 break;
4167 }
4168 [[fallthrough]];
4170 case ISD::ATOMIC_SWAP:
4182 case ISD::ATOMIC_LOAD: {
4183 unsigned MemBits =
4184 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4185 // If we are looking at the loaded value.
4186 if (Op.getResNo() == 0) {
4188 Known.Zero.setBitsFrom(MemBits);
4189 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4190 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4191 Known.Zero.setBitsFrom(MemBits);
4192 }
4193 break;
4194 }
4195 case ISD::FrameIndex:
4197 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4198 Known, getMachineFunction());
4199 break;
4200
4201 default:
4202 if (Opcode < ISD::BUILTIN_OP_END)
4203 break;
4204 [[fallthrough]];
4208 // TODO: Probably okay to remove after audit; here to reduce change size
4209 // in initial enablement patch for scalable vectors
4210 if (Op.getValueType().isScalableVector())
4211 break;
4212
4213 // Allow the target to implement this method for its nodes.
4214 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4215 break;
4216 }
4217
4218 return Known;
4219}
4220
4221/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4223 switch (OR) {
4231 }
4232 llvm_unreachable("Unknown OverflowResult");
4233}
4234
4237 // X + 0 never overflow
4238 if (isNullConstant(N1))
4239 return OFK_Never;
4240
4241 // If both operands each have at least two sign bits, the addition
4242 // cannot overflow.
4243 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4244 return OFK_Never;
4245
4246 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4247 return OFK_Sometime;
4248}
4249
4252 // X + 0 never overflow
4253 if (isNullConstant(N1))
4254 return OFK_Never;
4255
4256 // mulhi + 1 never overflow
4257 KnownBits N1Known = computeKnownBits(N1);
4258 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4259 N1Known.getMaxValue().ult(2))
4260 return OFK_Never;
4261
4262 KnownBits N0Known = computeKnownBits(N0);
4263 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4264 N0Known.getMaxValue().ult(2))
4265 return OFK_Never;
4266
4267 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4268 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4269 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4270 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4271}
4272
4275 // X - 0 never overflow
4276 if (isNullConstant(N1))
4277 return OFK_Never;
4278
4279 // If both operands each have at least two sign bits, the subtraction
4280 // cannot overflow.
4281 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4282 return OFK_Never;
4283
4284 KnownBits N0Known = computeKnownBits(N0);
4285 KnownBits N1Known = computeKnownBits(N1);
4286 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4287 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4288 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4289}
4290
4293 // X - 0 never overflow
4294 if (isNullConstant(N1))
4295 return OFK_Never;
4296
4297 KnownBits N0Known = computeKnownBits(N0);
4298 KnownBits N1Known = computeKnownBits(N1);
4299 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4300 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4301 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4302}
4303
4306 // X * 0 and X * 1 never overflow.
4307 if (isNullConstant(N1) || isOneConstant(N1))
4308 return OFK_Never;
4309
4310 KnownBits N0Known = computeKnownBits(N0);
4311 KnownBits N1Known = computeKnownBits(N1);
4312 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4313 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4314 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4315}
4316
4319 // X * 0 and X * 1 never overflow.
4320 if (isNullConstant(N1) || isOneConstant(N1))
4321 return OFK_Never;
4322
4323 // Get the size of the result.
4324 unsigned BitWidth = N0.getScalarValueSizeInBits();
4325
4326 // Sum of the sign bits.
4327 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4328
4329 // If we have enough sign bits, then there's no overflow.
4330 if (SignBits > BitWidth + 1)
4331 return OFK_Never;
4332
4333 if (SignBits == BitWidth + 1) {
4334 // The overflow occurs when the true multiplication of the
4335 // the operands is the minimum negative number.
4336 KnownBits N0Known = computeKnownBits(N0);
4337 KnownBits N1Known = computeKnownBits(N1);
4338 // If one of the operands is non-negative, then there's no
4339 // overflow.
4340 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4341 return OFK_Never;
4342 }
4343
4344 return OFK_Sometime;
4345}
4346
4348 if (Depth >= MaxRecursionDepth)
4349 return false; // Limit search depth.
4350
4351 EVT OpVT = Val.getValueType();
4352 unsigned BitWidth = OpVT.getScalarSizeInBits();
4353
4354 // Is the constant a known power of 2?
4356 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4357 }))
4358 return true;
4359
4360 // A left-shift of a constant one will have exactly one bit set because
4361 // shifting the bit off the end is undefined.
4362 if (Val.getOpcode() == ISD::SHL) {
4363 auto *C = isConstOrConstSplat(Val.getOperand(0));
4364 if (C && C->getAPIntValue() == 1)
4365 return true;
4366 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4367 isKnownNeverZero(Val, Depth);
4368 }
4369
4370 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4371 // one bit set.
4372 if (Val.getOpcode() == ISD::SRL) {
4373 auto *C = isConstOrConstSplat(Val.getOperand(0));
4374 if (C && C->getAPIntValue().isSignMask())
4375 return true;
4376 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4377 isKnownNeverZero(Val, Depth);
4378 }
4379
4380 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4381 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4382
4383 // Are all operands of a build vector constant powers of two?
4384 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4385 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4386 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4387 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4388 return false;
4389 }))
4390 return true;
4391
4392 // Is the operand of a splat vector a constant power of two?
4393 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4394 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4395 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4396 return true;
4397
4398 // vscale(power-of-two) is a power-of-two for some targets
4399 if (Val.getOpcode() == ISD::VSCALE &&
4400 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4402 return true;
4403
4404 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4405 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4406 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4408
4409 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4410 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4412
4413 // Looking for `x & -x` pattern:
4414 // If x == 0:
4415 // x & -x -> 0
4416 // If x != 0:
4417 // x & -x -> non-zero pow2
4418 // so if we find the pattern return whether we know `x` is non-zero.
4419 SDValue X;
4420 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4421 return isKnownNeverZero(X, Depth);
4422
4423 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4424 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4425
4426 // More could be done here, though the above checks are enough
4427 // to handle some common cases.
4428 return false;
4429}
4430
4432 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4433 return C1->getValueAPF().getExactLog2Abs() >= 0;
4434
4435 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4436 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4437
4438 return false;
4439}
4440
4442 EVT VT = Op.getValueType();
4443
4444 // Since the number of lanes in a scalable vector is unknown at compile time,
4445 // we track one bit which is implicitly broadcast to all lanes. This means
4446 // that all lanes in a scalable vector are considered demanded.
4447 APInt DemandedElts = VT.isFixedLengthVector()
4449 : APInt(1, 1);
4450 return ComputeNumSignBits(Op, DemandedElts, Depth);
4451}
4452
4453unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4454 unsigned Depth) const {
4455 EVT VT = Op.getValueType();
4456 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4457 unsigned VTBits = VT.getScalarSizeInBits();
4458 unsigned NumElts = DemandedElts.getBitWidth();
4459 unsigned Tmp, Tmp2;
4460 unsigned FirstAnswer = 1;
4461
4462 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4463 const APInt &Val = C->getAPIntValue();
4464 return Val.getNumSignBits();
4465 }
4466
4467 if (Depth >= MaxRecursionDepth)
4468 return 1; // Limit search depth.
4469
4470 if (!DemandedElts)
4471 return 1; // No demanded elts, better to assume we don't know anything.
4472
4473 unsigned Opcode = Op.getOpcode();
4474 switch (Opcode) {
4475 default: break;
4476 case ISD::AssertSext:
4477 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4478 return VTBits-Tmp+1;
4479 case ISD::AssertZext:
4480 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4481 return VTBits-Tmp;
4482 case ISD::MERGE_VALUES:
4483 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4484 Depth + 1);
4485 case ISD::SPLAT_VECTOR: {
4486 // Check if the sign bits of source go down as far as the truncated value.
4487 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4488 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4489 if (NumSrcSignBits > (NumSrcBits - VTBits))
4490 return NumSrcSignBits - (NumSrcBits - VTBits);
4491 break;
4492 }
4493 case ISD::BUILD_VECTOR:
4494 assert(!VT.isScalableVector());
4495 Tmp = VTBits;
4496 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4497 if (!DemandedElts[i])
4498 continue;
4499
4500 SDValue SrcOp = Op.getOperand(i);
4501 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4502 // for constant nodes to ensure we only look at the sign bits.
4503 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4504 APInt T = C->getAPIntValue().trunc(VTBits);
4505 Tmp2 = T.getNumSignBits();
4506 } else {
4507 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4508
4509 if (SrcOp.getValueSizeInBits() != VTBits) {
4510 assert(SrcOp.getValueSizeInBits() > VTBits &&
4511 "Expected BUILD_VECTOR implicit truncation");
4512 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4513 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4514 }
4515 }
4516 Tmp = std::min(Tmp, Tmp2);
4517 }
4518 return Tmp;
4519
4520 case ISD::VECTOR_SHUFFLE: {
4521 // Collect the minimum number of sign bits that are shared by every vector
4522 // element referenced by the shuffle.
4523 APInt DemandedLHS, DemandedRHS;
4524 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4525 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4526 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4527 DemandedLHS, DemandedRHS))
4528 return 1;
4529
4530 Tmp = std::numeric_limits<unsigned>::max();
4531 if (!!DemandedLHS)
4532 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4533 if (!!DemandedRHS) {
4534 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4535 Tmp = std::min(Tmp, Tmp2);
4536 }
4537 // If we don't know anything, early out and try computeKnownBits fall-back.
4538 if (Tmp == 1)
4539 break;
4540 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4541 return Tmp;
4542 }
4543
4544 case ISD::BITCAST: {
4545 if (VT.isScalableVector())
4546 break;
4547 SDValue N0 = Op.getOperand(0);
4548 EVT SrcVT = N0.getValueType();
4549 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4550
4551 // Ignore bitcasts from unsupported types..
4552 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4553 break;
4554
4555 // Fast handling of 'identity' bitcasts.
4556 if (VTBits == SrcBits)
4557 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4558
4559 bool IsLE = getDataLayout().isLittleEndian();
4560
4561 // Bitcast 'large element' scalar/vector to 'small element' vector.
4562 if ((SrcBits % VTBits) == 0) {
4563 assert(VT.isVector() && "Expected bitcast to vector");
4564
4565 unsigned Scale = SrcBits / VTBits;
4566 APInt SrcDemandedElts =
4567 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4568
4569 // Fast case - sign splat can be simply split across the small elements.
4570 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4571 if (Tmp == SrcBits)
4572 return VTBits;
4573
4574 // Slow case - determine how far the sign extends into each sub-element.
4575 Tmp2 = VTBits;
4576 for (unsigned i = 0; i != NumElts; ++i)
4577 if (DemandedElts[i]) {
4578 unsigned SubOffset = i % Scale;
4579 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4580 SubOffset = SubOffset * VTBits;
4581 if (Tmp <= SubOffset)
4582 return 1;
4583 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4584 }
4585 return Tmp2;
4586 }
4587 break;
4588 }
4589
4591 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4592 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4593 return VTBits - Tmp + 1;
4594 case ISD::SIGN_EXTEND:
4595 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4596 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4598 // Max of the input and what this extends.
4599 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4600 Tmp = VTBits-Tmp+1;
4601 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4602 return std::max(Tmp, Tmp2);
4604 if (VT.isScalableVector())
4605 break;
4606 SDValue Src = Op.getOperand(0);
4607 EVT SrcVT = Src.getValueType();
4608 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4609 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4610 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4611 }
4612 case ISD::SRA:
4613 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4614 // SRA X, C -> adds C sign bits.
4615 if (std::optional<uint64_t> ShAmt =
4616 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4617 Tmp = std::min<uint64_t>(Tmp + *ShAmt, VTBits);
4618 return Tmp;
4619 case ISD::SHL:
4620 if (std::optional<ConstantRange> ShAmtRange =
4621 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4622 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4623 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4624 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4625 // shifted out, then we can compute the number of sign bits for the
4626 // operand being extended. A future improvement could be to pass along the
4627 // "shifted left by" information in the recursive calls to
4628 // ComputeKnownSignBits. Allowing us to handle this more generically.
4629 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4630 SDValue Ext = Op.getOperand(0);
4631 EVT ExtVT = Ext.getValueType();
4632 SDValue Extendee = Ext.getOperand(0);
4633 EVT ExtendeeVT = Extendee.getValueType();
4634 uint64_t SizeDifference =
4635 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4636 if (SizeDifference <= MinShAmt) {
4637 Tmp = SizeDifference +
4638 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4639 if (MaxShAmt < Tmp)
4640 return Tmp - MaxShAmt;
4641 }
4642 }
4643 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4644 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4645 if (MaxShAmt < Tmp)
4646 return Tmp - MaxShAmt;
4647 }
4648 break;
4649 case ISD::AND:
4650 case ISD::OR:
4651 case ISD::XOR: // NOT is handled here.
4652 // Logical binary ops preserve the number of sign bits at the worst.
4653 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4654 if (Tmp != 1) {
4655 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4656 FirstAnswer = std::min(Tmp, Tmp2);
4657 // We computed what we know about the sign bits as our first
4658 // answer. Now proceed to the generic code that uses
4659 // computeKnownBits, and pick whichever answer is better.
4660 }
4661 break;
4662
4663 case ISD::SELECT:
4664 case ISD::VSELECT:
4665 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4666 if (Tmp == 1) return 1; // Early out.
4667 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4668 return std::min(Tmp, Tmp2);
4669 case ISD::SELECT_CC:
4670 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4671 if (Tmp == 1) return 1; // Early out.
4672 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4673 return std::min(Tmp, Tmp2);
4674
4675 case ISD::SMIN:
4676 case ISD::SMAX: {
4677 // If we have a clamp pattern, we know that the number of sign bits will be
4678 // the minimum of the clamp min/max range.
4679 bool IsMax = (Opcode == ISD::SMAX);
4680 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4681 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4682 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4683 CstHigh =
4684 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4685 if (CstLow && CstHigh) {
4686 if (!IsMax)
4687 std::swap(CstLow, CstHigh);
4688 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4689 Tmp = CstLow->getAPIntValue().getNumSignBits();
4690 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4691 return std::min(Tmp, Tmp2);
4692 }
4693 }
4694
4695 // Fallback - just get the minimum number of sign bits of the operands.
4696 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4697 if (Tmp == 1)
4698 return 1; // Early out.
4699 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4700 return std::min(Tmp, Tmp2);
4701 }
4702 case ISD::UMIN:
4703 case ISD::UMAX:
4704 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4705 if (Tmp == 1)
4706 return 1; // Early out.
4707 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4708 return std::min(Tmp, Tmp2);
4709 case ISD::SSUBO_CARRY:
4710 case ISD::USUBO_CARRY:
4711 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4712 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4713 return VTBits;
4714 [[fallthrough]];
4715 case ISD::SADDO:
4716 case ISD::UADDO:
4717 case ISD::SADDO_CARRY:
4718 case ISD::UADDO_CARRY:
4719 case ISD::SSUBO:
4720 case ISD::USUBO:
4721 case ISD::SMULO:
4722 case ISD::UMULO:
4723 if (Op.getResNo() != 1)
4724 break;
4725 // The boolean result conforms to getBooleanContents. Fall through.
4726 // If setcc returns 0/-1, all bits are sign bits.
4727 // We know that we have an integer-based boolean since these operations
4728 // are only available for integer.
4729 if (TLI->getBooleanContents(VT.isVector(), false) ==
4731 return VTBits;
4732 break;
4733 case ISD::SETCC:
4734 case ISD::SETCCCARRY:
4735 case ISD::STRICT_FSETCC:
4736 case ISD::STRICT_FSETCCS: {
4737 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4738 // If setcc returns 0/-1, all bits are sign bits.
4739 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4741 return VTBits;
4742 break;
4743 }
4744 case ISD::ROTL:
4745 case ISD::ROTR:
4746 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4747
4748 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4749 if (Tmp == VTBits)
4750 return VTBits;
4751
4752 if (ConstantSDNode *C =
4753 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4754 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4755
4756 // Handle rotate right by N like a rotate left by 32-N.
4757 if (Opcode == ISD::ROTR)
4758 RotAmt = (VTBits - RotAmt) % VTBits;
4759
4760 // If we aren't rotating out all of the known-in sign bits, return the
4761 // number that are left. This handles rotl(sext(x), 1) for example.
4762 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4763 }
4764 break;
4765 case ISD::ADD:
4766 case ISD::ADDC:
4767 // Add can have at most one carry bit. Thus we know that the output
4768 // is, at worst, one more bit than the inputs.
4769 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4770 if (Tmp == 1) return 1; // Early out.
4771
4772 // Special case decrementing a value (ADD X, -1):
4773 if (ConstantSDNode *CRHS =
4774 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4775 if (CRHS->isAllOnes()) {
4776 KnownBits Known =
4777 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4778
4779 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4780 // sign bits set.
4781 if ((Known.Zero | 1).isAllOnes())
4782 return VTBits;
4783
4784 // If we are subtracting one from a positive number, there is no carry
4785 // out of the result.
4786 if (Known.isNonNegative())
4787 return Tmp;
4788 }
4789
4790 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4791 if (Tmp2 == 1) return 1; // Early out.
4792 return std::min(Tmp, Tmp2) - 1;
4793 case ISD::SUB:
4794 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4795 if (Tmp2 == 1) return 1; // Early out.
4796
4797 // Handle NEG.
4798 if (ConstantSDNode *CLHS =
4799 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4800 if (CLHS->isZero()) {
4801 KnownBits Known =
4802 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4803 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4804 // sign bits set.
4805 if ((Known.Zero | 1).isAllOnes())
4806 return VTBits;
4807
4808 // If the input is known to be positive (the sign bit is known clear),
4809 // the output of the NEG has the same number of sign bits as the input.
4810 if (Known.isNonNegative())
4811 return Tmp2;
4812
4813 // Otherwise, we treat this like a SUB.
4814 }
4815
4816 // Sub can have at most one carry bit. Thus we know that the output
4817 // is, at worst, one more bit than the inputs.
4818 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4819 if (Tmp == 1) return 1; // Early out.
4820 return std::min(Tmp, Tmp2) - 1;
4821 case ISD::MUL: {
4822 // The output of the Mul can be at most twice the valid bits in the inputs.
4823 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4824 if (SignBitsOp0 == 1)
4825 break;
4826 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
4827 if (SignBitsOp1 == 1)
4828 break;
4829 unsigned OutValidBits =
4830 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
4831 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
4832 }
4833 case ISD::AVGCEILS:
4834 case ISD::AVGFLOORS:
4835 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4836 if (Tmp == 1)
4837 return 1; // Early out.
4838 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4839 return std::min(Tmp, Tmp2);
4840 case ISD::SREM:
4841 // The sign bit is the LHS's sign bit, except when the result of the
4842 // remainder is zero. The magnitude of the result should be less than or
4843 // equal to the magnitude of the LHS. Therefore, the result should have
4844 // at least as many sign bits as the left hand side.
4845 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4846 case ISD::TRUNCATE: {
4847 // Check if the sign bits of source go down as far as the truncated value.
4848 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
4849 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4850 if (NumSrcSignBits > (NumSrcBits - VTBits))
4851 return NumSrcSignBits - (NumSrcBits - VTBits);
4852 break;
4853 }
4854 case ISD::EXTRACT_ELEMENT: {
4855 if (VT.isScalableVector())
4856 break;
4857 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
4858 const int BitWidth = Op.getValueSizeInBits();
4859 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
4860
4861 // Get reverse index (starting from 1), Op1 value indexes elements from
4862 // little end. Sign starts at big end.
4863 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
4864
4865 // If the sign portion ends in our element the subtraction gives correct
4866 // result. Otherwise it gives either negative or > bitwidth result
4867 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
4868 }
4870 if (VT.isScalableVector())
4871 break;
4872 // If we know the element index, split the demand between the
4873 // source vector and the inserted element, otherwise assume we need
4874 // the original demanded vector elements and the value.
4875 SDValue InVec = Op.getOperand(0);
4876 SDValue InVal = Op.getOperand(1);
4877 SDValue EltNo = Op.getOperand(2);
4878 bool DemandedVal = true;
4879 APInt DemandedVecElts = DemandedElts;
4880 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4881 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4882 unsigned EltIdx = CEltNo->getZExtValue();
4883 DemandedVal = !!DemandedElts[EltIdx];
4884 DemandedVecElts.clearBit(EltIdx);
4885 }
4886 Tmp = std::numeric_limits<unsigned>::max();
4887 if (DemandedVal) {
4888 // TODO - handle implicit truncation of inserted elements.
4889 if (InVal.getScalarValueSizeInBits() != VTBits)
4890 break;
4891 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
4892 Tmp = std::min(Tmp, Tmp2);
4893 }
4894 if (!!DemandedVecElts) {
4895 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
4896 Tmp = std::min(Tmp, Tmp2);
4897 }
4898 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4899 return Tmp;
4900 }
4902 assert(!VT.isScalableVector());
4903 SDValue InVec = Op.getOperand(0);
4904 SDValue EltNo = Op.getOperand(1);
4905 EVT VecVT = InVec.getValueType();
4906 // ComputeNumSignBits not yet implemented for scalable vectors.
4907 if (VecVT.isScalableVector())
4908 break;
4909 const unsigned BitWidth = Op.getValueSizeInBits();
4910 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
4911 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4912
4913 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
4914 // anything about sign bits. But if the sizes match we can derive knowledge
4915 // about sign bits from the vector operand.
4916 if (BitWidth != EltBitWidth)
4917 break;
4918
4919 // If we know the element index, just demand that vector element, else for
4920 // an unknown element index, ignore DemandedElts and demand them all.
4921 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4922 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4923 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4924 DemandedSrcElts =
4925 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4926
4927 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
4928 }
4930 // Offset the demanded elts by the subvector index.
4931 SDValue Src = Op.getOperand(0);
4932 // Bail until we can represent demanded elements for scalable vectors.
4933 if (Src.getValueType().isScalableVector())
4934 break;
4935 uint64_t Idx = Op.getConstantOperandVal(1);
4936 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
4937 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
4938 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4939 }
4940 case ISD::CONCAT_VECTORS: {
4941 if (VT.isScalableVector())
4942 break;
4943 // Determine the minimum number of sign bits across all demanded
4944 // elts of the input vectors. Early out if the result is already 1.
4945 Tmp = std::numeric_limits<unsigned>::max();
4946 EVT SubVectorVT = Op.getOperand(0).getValueType();
4947 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
4948 unsigned NumSubVectors = Op.getNumOperands();
4949 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
4950 APInt DemandedSub =
4951 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
4952 if (!DemandedSub)
4953 continue;
4954 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
4955 Tmp = std::min(Tmp, Tmp2);
4956 }
4957 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4958 return Tmp;
4959 }
4960 case ISD::INSERT_SUBVECTOR: {
4961 if (VT.isScalableVector())
4962 break;
4963 // Demand any elements from the subvector and the remainder from the src its
4964 // inserted into.
4965 SDValue Src = Op.getOperand(0);
4966 SDValue Sub = Op.getOperand(1);
4967 uint64_t Idx = Op.getConstantOperandVal(2);
4968 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
4969 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
4970 APInt DemandedSrcElts = DemandedElts;
4971 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
4972
4973 Tmp = std::numeric_limits<unsigned>::max();
4974 if (!!DemandedSubElts) {
4975 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
4976 if (Tmp == 1)
4977 return 1; // early-out
4978 }
4979 if (!!DemandedSrcElts) {
4980 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
4981 Tmp = std::min(Tmp, Tmp2);
4982 }
4983 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4984 return Tmp;
4985 }
4986 case ISD::LOAD: {
4987 LoadSDNode *LD = cast<LoadSDNode>(Op);
4988 if (const MDNode *Ranges = LD->getRanges()) {
4989 if (DemandedElts != 1)
4990 break;
4991
4993 if (VTBits > CR.getBitWidth()) {
4994 switch (LD->getExtensionType()) {
4995 case ISD::SEXTLOAD:
4996 CR = CR.signExtend(VTBits);
4997 break;
4998 case ISD::ZEXTLOAD:
4999 CR = CR.zeroExtend(VTBits);
5000 break;
5001 default:
5002 break;
5003 }
5004 }
5005
5006 if (VTBits != CR.getBitWidth())
5007 break;
5008 return std::min(CR.getSignedMin().getNumSignBits(),
5010 }
5011
5012 break;
5013 }
5016 case ISD::ATOMIC_SWAP:
5028 case ISD::ATOMIC_LOAD: {
5029 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
5030 // If we are looking at the loaded value.
5031 if (Op.getResNo() == 0) {
5032 if (Tmp == VTBits)
5033 return 1; // early-out
5035 return VTBits - Tmp + 1;
5037 return VTBits - Tmp;
5038 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5039 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
5040 if (ETy == ISD::SEXTLOAD)
5041 return VTBits - Tmp + 1;
5042 if (ETy == ISD::ZEXTLOAD)
5043 return VTBits - Tmp;
5044 }
5045 }
5046 break;
5047 }
5048 }
5049
5050 // If we are looking at the loaded value of the SDNode.
5051 if (Op.getResNo() == 0) {
5052 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5053 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5054 unsigned ExtType = LD->getExtensionType();
5055 switch (ExtType) {
5056 default: break;
5057 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5058 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5059 return VTBits - Tmp + 1;
5060 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5061 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5062 return VTBits - Tmp;
5063 case ISD::NON_EXTLOAD:
5064 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5065 // We only need to handle vectors - computeKnownBits should handle
5066 // scalar cases.
5067 Type *CstTy = Cst->getType();
5068 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5069 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5070 VTBits == CstTy->getScalarSizeInBits()) {
5071 Tmp = VTBits;
5072 for (unsigned i = 0; i != NumElts; ++i) {
5073 if (!DemandedElts[i])
5074 continue;
5075 if (Constant *Elt = Cst->getAggregateElement(i)) {
5076 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5077 const APInt &Value = CInt->getValue();
5078 Tmp = std::min(Tmp, Value.getNumSignBits());
5079 continue;
5080 }
5081 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5082 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5083 Tmp = std::min(Tmp, Value.getNumSignBits());
5084 continue;
5085 }
5086 }
5087 // Unknown type. Conservatively assume no bits match sign bit.
5088 return 1;
5089 }
5090 return Tmp;
5091 }
5092 }
5093 break;
5094 }
5095 }
5096 }
5097
5098 // Allow the target to implement this method for its nodes.
5099 if (Opcode >= ISD::BUILTIN_OP_END ||
5100 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5101 Opcode == ISD::INTRINSIC_W_CHAIN ||
5102 Opcode == ISD::INTRINSIC_VOID) {
5103 // TODO: This can probably be removed once target code is audited. This
5104 // is here purely to reduce patch size and review complexity.
5105 if (!VT.isScalableVector()) {
5106 unsigned NumBits =
5107 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5108 if (NumBits > 1)
5109 FirstAnswer = std::max(FirstAnswer, NumBits);
5110 }
5111 }
5112
5113 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5114 // use this information.
5115 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5116 return std::max(FirstAnswer, Known.countMinSignBits());
5117}
5118
5120 unsigned Depth) const {
5121 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5122 return Op.getScalarValueSizeInBits() - SignBits + 1;
5123}
5124
5126 const APInt &DemandedElts,
5127 unsigned Depth) const {
5128 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5129 return Op.getScalarValueSizeInBits() - SignBits + 1;
5130}
5131
5133 unsigned Depth) const {
5134 // Early out for FREEZE.
5135 if (Op.getOpcode() == ISD::FREEZE)
5136 return true;
5137
5138 // TODO: Assume we don't know anything for now.
5139 EVT VT = Op.getValueType();
5140 if (VT.isScalableVector())
5141 return false;
5142
5143 APInt DemandedElts = VT.isVector()
5145 : APInt(1, 1);
5146 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5147}
5148
5150 const APInt &DemandedElts,
5151 bool PoisonOnly,
5152 unsigned Depth) const {
5153 unsigned Opcode = Op.getOpcode();
5154
5155 // Early out for FREEZE.
5156 if (Opcode == ISD::FREEZE)
5157 return true;
5158
5159 if (Depth >= MaxRecursionDepth)
5160 return false; // Limit search depth.
5161
5162 if (isIntOrFPConstant(Op))
5163 return true;
5164
5165 switch (Opcode) {
5166 case ISD::CONDCODE:
5167 case ISD::VALUETYPE:
5168 case ISD::FrameIndex:
5170 case ISD::CopyFromReg:
5171 return true;
5172
5173 case ISD::UNDEF:
5174 return PoisonOnly;
5175
5176 case ISD::BUILD_VECTOR:
5177 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5178 // this shouldn't affect the result.
5179 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5180 if (!DemandedElts[i])
5181 continue;
5183 Depth + 1))
5184 return false;
5185 }
5186 return true;
5187
5188 case ISD::VECTOR_SHUFFLE: {
5189 APInt DemandedLHS, DemandedRHS;
5190 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5191 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5192 DemandedElts, DemandedLHS, DemandedRHS,
5193 /*AllowUndefElts=*/false))
5194 return false;
5195 if (!DemandedLHS.isZero() &&
5196 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5197 PoisonOnly, Depth + 1))
5198 return false;
5199 if (!DemandedRHS.isZero() &&
5200 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5201 PoisonOnly, Depth + 1))
5202 return false;
5203 return true;
5204 }
5205
5206 // TODO: Search for noundef attributes from library functions.
5207
5208 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5209
5210 default:
5211 // Allow the target to implement this method for its nodes.
5212 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5213 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5215 Op, DemandedElts, *this, PoisonOnly, Depth);
5216 break;
5217 }
5218
5219 // If Op can't create undef/poison and none of its operands are undef/poison
5220 // then Op is never undef/poison.
5221 // NOTE: TargetNodes can handle this in themselves in
5222 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5223 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5224 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5225 Depth) &&
5226 all_of(Op->ops(), [&](SDValue V) {
5227 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5228 });
5229}
5230
5232 bool ConsiderFlags,
5233 unsigned Depth) const {
5234 // TODO: Assume we don't know anything for now.
5235 EVT VT = Op.getValueType();
5236 if (VT.isScalableVector())
5237 return true;
5238
5239 APInt DemandedElts = VT.isVector()
5241 : APInt(1, 1);
5242 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5243 Depth);
5244}
5245
5247 bool PoisonOnly, bool ConsiderFlags,
5248 unsigned Depth) const {
5249 // TODO: Assume we don't know anything for now.
5250 EVT VT = Op.getValueType();
5251 if (VT.isScalableVector())
5252 return true;
5253
5254 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5255 return true;
5256
5257 unsigned Opcode = Op.getOpcode();
5258 switch (Opcode) {
5259 case ISD::FREEZE:
5262 case ISD::SADDSAT:
5263 case ISD::UADDSAT:
5264 case ISD::SSUBSAT:
5265 case ISD::USUBSAT:
5266 case ISD::MULHU:
5267 case ISD::MULHS:
5268 case ISD::SMIN:
5269 case ISD::SMAX:
5270 case ISD::UMIN:
5271 case ISD::UMAX:
5272 case ISD::AND:
5273 case ISD::XOR:
5274 case ISD::ROTL:
5275 case ISD::ROTR:
5276 case ISD::FSHL:
5277 case ISD::FSHR:
5278 case ISD::BSWAP:
5279 case ISD::CTPOP:
5280 case ISD::BITREVERSE:
5281 case ISD::PARITY:
5282 case ISD::SIGN_EXTEND:
5283 case ISD::TRUNCATE:
5287 case ISD::BITCAST:
5288 case ISD::BUILD_VECTOR:
5289 case ISD::BUILD_PAIR:
5290 return false;
5291
5292 case ISD::SELECT_CC:
5293 case ISD::SETCC: {
5294 // Integer setcc cannot create undef or poison.
5295 if (Op.getOperand(0).getValueType().isInteger())
5296 return false;
5297
5298 // FP compares are more complicated. They can create poison for nan/infinity
5299 // based on options and flags. The options and flags also cause special
5300 // nonan condition codes to be used. Those condition codes may be preserved
5301 // even if the nonan flag is dropped somewhere.
5302 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5303 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5304 if (((unsigned)CCCode & 0x10U))
5305 return true;
5306
5308 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5309 }
5310
5311 case ISD::OR:
5312 case ISD::ZERO_EXTEND:
5313 case ISD::ADD:
5314 case ISD::SUB:
5315 case ISD::MUL:
5316 // No poison except from flags (which is handled above)
5317 return false;
5318
5319 case ISD::SHL:
5320 case ISD::SRL:
5321 case ISD::SRA:
5322 // If the max shift amount isn't in range, then the shift can
5323 // create poison.
5324 return !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedElts,
5325 PoisonOnly, Depth + 1) ||
5326 !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5327
5329 // Check if we demand any upper (undef) elements.
5330 return !PoisonOnly && DemandedElts.ugt(1);
5331
5334 // Ensure that the element index is in bounds.
5335 EVT VecVT = Op.getOperand(0).getValueType();
5336 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5338 Depth + 1)) {
5339 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5340 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5341 }
5342 return true;
5343 }
5344
5345 case ISD::VECTOR_SHUFFLE: {
5346 // Check for any demanded shuffle element that is undef.
5347 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5348 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5349 if (Elt < 0 && DemandedElts[Idx])
5350 return true;
5351 return false;
5352 }
5353
5354 default:
5355 // Allow the target to implement this method for its nodes.
5356 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5357 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5359 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5360 break;
5361 }
5362
5363 // Be conservative and return true.
5364 return true;
5365}
5366
5367bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5368 unsigned Opcode = Op.getOpcode();
5369 if (Opcode == ISD::OR)
5370 return Op->getFlags().hasDisjoint() ||
5371 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5372 if (Opcode == ISD::XOR)
5373 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5374 return false;
5375}
5376
5378 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5379 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5380}
5381
5382bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5383 // If we're told that NaNs won't happen, assume they won't.
5384 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5385 return true;
5386
5387 if (Depth >= MaxRecursionDepth)
5388 return false; // Limit search depth.
5389
5390 // If the value is a constant, we can obviously see if it is a NaN or not.
5391 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5392 return !C->getValueAPF().isNaN() ||
5393 (SNaN && !C->getValueAPF().isSignaling());
5394 }
5395
5396 unsigned Opcode = Op.getOpcode();
5397 switch (Opcode) {
5398 case ISD::FADD:
5399 case ISD::FSUB:
5400 case ISD::FMUL:
5401 case ISD::FDIV:
5402 case ISD::FREM:
5403 case ISD::FSIN:
5404 case ISD::FCOS:
5405 case ISD::FTAN:
5406 case ISD::FASIN:
5407 case ISD::FACOS:
5408 case ISD::FATAN:
5409 case ISD::FSINH:
5410 case ISD::FCOSH:
5411 case ISD::FTANH:
5412 case ISD::FMA:
5413 case ISD::FMAD: {
5414 if (SNaN)
5415 return true;
5416 // TODO: Need isKnownNeverInfinity
5417 return false;
5418 }
5419 case ISD::FCANONICALIZE:
5420 case ISD::FEXP:
5421 case ISD::FEXP2:
5422 case ISD::FEXP10:
5423 case ISD::FTRUNC:
5424 case ISD::FFLOOR:
5425 case ISD::FCEIL:
5426 case ISD::FROUND:
5427 case ISD::FROUNDEVEN:
5428 case ISD::FRINT:
5429 case ISD::LRINT:
5430 case ISD::LLRINT:
5431 case ISD::FNEARBYINT:
5432 case ISD::FLDEXP: {
5433 if (SNaN)
5434 return true;
5435 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5436 }
5437 case ISD::FABS:
5438 case ISD::FNEG:
5439 case ISD::FCOPYSIGN: {
5440 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5441 }
5442 case ISD::SELECT:
5443 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5444 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5445 case ISD::FP_EXTEND:
5446 case ISD::FP_ROUND: {
5447 if (SNaN)
5448 return true;
5449 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5450 }
5451 case ISD::SINT_TO_FP:
5452 case ISD::UINT_TO_FP:
5453 return true;
5454 case ISD::FSQRT: // Need is known positive
5455 case ISD::FLOG:
5456 case ISD::FLOG2:
5457 case ISD::FLOG10:
5458 case ISD::FPOWI:
5459 case ISD::FPOW: {
5460 if (SNaN)
5461 return true;
5462 // TODO: Refine on operand
5463 return false;
5464 }
5465 case ISD::FMINNUM:
5466 case ISD::FMAXNUM: {
5467 // Only one needs to be known not-nan, since it will be returned if the
5468 // other ends up being one.
5469 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5470 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5471 }
5472 case ISD::FMINNUM_IEEE:
5473 case ISD::FMAXNUM_IEEE: {
5474 if (SNaN)
5475 return true;
5476 // This can return a NaN if either operand is an sNaN, or if both operands
5477 // are NaN.
5478 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5479 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5480 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5481 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5482 }
5483 case ISD::FMINIMUM:
5484 case ISD::FMAXIMUM: {
5485 // TODO: Does this quiet or return the origina NaN as-is?
5486 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5487 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5488 }
5490 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5491 }
5492 case ISD::BUILD_VECTOR: {
5493 for (const SDValue &Opnd : Op->ops())
5494 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5495 return false;
5496 return true;
5497 }
5498 default:
5499 if (Opcode >= ISD::BUILTIN_OP_END ||
5500 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5501 Opcode == ISD::INTRINSIC_W_CHAIN ||
5502 Opcode == ISD::INTRINSIC_VOID) {
5503 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5504 }
5505
5506 return false;
5507 }
5508}
5509
5511 assert(Op.getValueType().isFloatingPoint() &&
5512 "Floating point type expected");
5513
5514 // If the value is a constant, we can obviously see if it is a zero or not.
5516 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5517}
5518
5520 if (Depth >= MaxRecursionDepth)
5521 return false; // Limit search depth.
5522
5523 assert(!Op.getValueType().isFloatingPoint() &&
5524 "Floating point types unsupported - use isKnownNeverZeroFloat");
5525
5526 // If the value is a constant, we can obviously see if it is a zero or not.
5528 [](ConstantSDNode *C) { return !C->isZero(); }))
5529 return true;
5530
5531 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5532 // some degree.
5533 switch (Op.getOpcode()) {
5534 default:
5535 break;
5536
5537 case ISD::OR:
5538 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5539 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5540
5541 case ISD::VSELECT:
5542 case ISD::SELECT:
5543 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5544 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5545
5546 case ISD::SHL: {
5547 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5548 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5549 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5550 // 1 << X is never zero.
5551 if (ValKnown.One[0])
5552 return true;
5553 // If max shift cnt of known ones is non-zero, result is non-zero.
5554 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5555 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5556 !ValKnown.One.shl(MaxCnt).isZero())
5557 return true;
5558 break;
5559 }
5560 case ISD::UADDSAT:
5561 case ISD::UMAX:
5562 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5563 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5564
5565 // For smin/smax: If either operand is known negative/positive
5566 // respectively we don't need the other to be known at all.
5567 case ISD::SMAX: {
5568 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5569 if (Op1.isStrictlyPositive())
5570 return true;
5571
5572 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5573 if (Op0.isStrictlyPositive())
5574 return true;
5575
5576 if (Op1.isNonZero() && Op0.isNonZero())
5577 return true;
5578
5579 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5580 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5581 }
5582 case ISD::SMIN: {
5583 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5584 if (Op1.isNegative())
5585 return true;
5586
5587 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5588 if (Op0.isNegative())
5589 return true;
5590
5591 if (Op1.isNonZero() && Op0.isNonZero())
5592 return true;
5593
5594 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5595 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5596 }
5597 case ISD::UMIN:
5598 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5599 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5600
5601 case ISD::ROTL:
5602 case ISD::ROTR:
5603 case ISD::BITREVERSE:
5604 case ISD::BSWAP:
5605 case ISD::CTPOP:
5606 case ISD::ABS:
5607 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5608
5609 case ISD::SRA:
5610 case ISD::SRL: {
5611 if (Op->getFlags().hasExact())
5612 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5613 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5614 if (ValKnown.isNegative())
5615 return true;
5616 // If max shift cnt of known ones is non-zero, result is non-zero.
5617 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5618 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5619 !ValKnown.One.lshr(MaxCnt).isZero())
5620 return true;
5621 break;
5622 }
5623 case ISD::UDIV:
5624 case ISD::SDIV:
5625 // div exact can only produce a zero if the dividend is zero.
5626 // TODO: For udiv this is also true if Op1 u<= Op0
5627 if (Op->getFlags().hasExact())
5628 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5629 break;
5630
5631 case ISD::ADD:
5632 if (Op->getFlags().hasNoUnsignedWrap())
5633 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5634 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5635 return true;
5636 // TODO: There are a lot more cases we can prove for add.
5637 break;
5638
5639 case ISD::SUB: {
5640 if (isNullConstant(Op.getOperand(0)))
5641 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5642
5643 std::optional<bool> ne =
5644 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5645 computeKnownBits(Op.getOperand(1), Depth + 1));
5646 return ne && *ne;
5647 }
5648
5649 case ISD::MUL:
5650 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5651 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5652 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5653 return true;
5654 break;
5655
5656 case ISD::ZERO_EXTEND:
5657 case ISD::SIGN_EXTEND:
5658 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5659 case ISD::VSCALE: {
5661 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
5662 ConstantRange CR =
5663 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
5664 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
5665 return true;
5666 break;
5667 }
5668 }
5669
5671}
5672
5674 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
5675 return !C1->isNegative();
5676
5677 return Op.getOpcode() == ISD::FABS;
5678}
5679
5681 // Check the obvious case.
5682 if (A == B) return true;
5683
5684 // For negative and positive zero.
5685 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5686 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5687 if (CA->isZero() && CB->isZero()) return true;
5688
5689 // Otherwise they may not be equal.
5690 return false;
5691}
5692
5693// Only bits set in Mask must be negated, other bits may be arbitrary.
5695 if (isBitwiseNot(V, AllowUndefs))
5696 return V.getOperand(0);
5697
5698 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5699 // bits in the non-extended part.
5700 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5701 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5702 return SDValue();
5703 SDValue ExtArg = V.getOperand(0);
5704 if (ExtArg.getScalarValueSizeInBits() >=
5705 MaskC->getAPIntValue().getActiveBits() &&
5706 isBitwiseNot(ExtArg, AllowUndefs) &&
5707 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5708 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5709 return ExtArg.getOperand(0).getOperand(0);
5710 return SDValue();
5711}
5712
5714 // Match masked merge pattern (X & ~M) op (Y & M)
5715 // Including degenerate case (X & ~M) op M
5716 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5717 SDValue Other) {
5718 if (SDValue NotOperand =
5719 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5720 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5721 NotOperand->getOpcode() == ISD::TRUNCATE)
5722 NotOperand = NotOperand->getOperand(0);
5723
5724 if (Other == NotOperand)
5725 return true;
5726 if (Other->getOpcode() == ISD::AND)
5727 return NotOperand == Other->getOperand(0) ||
5728 NotOperand == Other->getOperand(1);
5729 }
5730 return false;
5731 };
5732
5733 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
5734 A = A->getOperand(0);
5735
5736 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
5737 B = B->getOperand(0);
5738
5739 if (A->getOpcode() == ISD::AND)
5740 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
5741 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
5742 return false;
5743}
5744
5745// FIXME: unify with llvm::haveNoCommonBitsSet.
5747 assert(A.getValueType() == B.getValueType() &&
5748 "Values must have the same type");
5751 return true;
5754}
5755
5756static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
5757 SelectionDAG &DAG) {
5758 if (cast<ConstantSDNode>(Step)->isZero())
5759 return DAG.getConstant(0, DL, VT);
5760
5761 return SDValue();
5762}
5763
5766 SelectionDAG &DAG) {
5767 int NumOps = Ops.size();
5768 assert(NumOps != 0 && "Can't build an empty vector!");
5769 assert(!VT.isScalableVector() &&
5770 "BUILD_VECTOR cannot be used with scalable types");
5771 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
5772 "Incorrect element count in BUILD_VECTOR!");
5773
5774 // BUILD_VECTOR of UNDEFs is UNDEF.
5775 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5776 return DAG.getUNDEF(VT);
5777
5778 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
5779 SDValue IdentitySrc;
5780 bool IsIdentity = true;
5781 for (int i = 0; i != NumOps; ++i) {
5782 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5783 Ops[i].getOperand(0).getValueType() != VT ||
5784 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
5785 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
5786 Ops[i].getConstantOperandAPInt(1) != i) {
5787 IsIdentity = false;
5788 break;
5789 }
5790 IdentitySrc = Ops[i].getOperand(0);
5791 }
5792 if (IsIdentity)
5793 return IdentitySrc;
5794
5795 return SDValue();
5796}
5797
5798/// Try to simplify vector concatenation to an input value, undef, or build
5799/// vector.
5802 SelectionDAG &DAG) {
5803 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
5804 assert(llvm::all_of(Ops,
5805 [Ops](SDValue Op) {
5806 return Ops[0].getValueType() == Op.getValueType();
5807 }) &&
5808 "Concatenation of vectors with inconsistent value types!");
5809 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
5810 VT.getVectorElementCount() &&
5811 "Incorrect element count in vector concatenation!");
5812
5813 if (Ops.size() == 1)
5814 return Ops[0];
5815
5816 // Concat of UNDEFs is UNDEF.
5817 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
5818 return DAG.getUNDEF(VT);
5819
5820 // Scan the operands and look for extract operations from a single source
5821 // that correspond to insertion at the same location via this concatenation:
5822 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
5823 SDValue IdentitySrc;
5824 bool IsIdentity = true;
5825 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
5826 SDValue Op = Ops[i];
5827 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
5828 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
5829 Op.getOperand(0).getValueType() != VT ||
5830 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
5831 Op.getConstantOperandVal(1) != IdentityIndex) {
5832 IsIdentity = false;
5833 break;
5834 }
5835 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
5836 "Unexpected identity source vector for concat of extracts");
5837 IdentitySrc = Op.getOperand(0);
5838 }
5839 if (IsIdentity) {
5840 assert(IdentitySrc && "Failed to set source vector of extracts");
5841 return IdentitySrc;
5842 }
5843
5844 // The code below this point is only designed to work for fixed width
5845 // vectors, so we bail out for now.
5846 if (VT.isScalableVector())
5847 return SDValue();
5848
5849 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
5850 // simplified to one big BUILD_VECTOR.
5851 // FIXME: Add support for SCALAR_TO_VECTOR as well.
5852 EVT SVT = VT.getScalarType();
5854 for (SDValue Op : Ops) {
5855 EVT OpVT = Op.getValueType();
5856 if (Op.isUndef())
5857 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
5858 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
5859 Elts.append(Op->op_begin(), Op->op_end());
5860 else
5861 return SDValue();
5862 }
5863
5864 // BUILD_VECTOR requires all inputs to be of the same type, find the
5865 // maximum type and extend them all.
5866 for (SDValue Op : Elts)
5867 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
5868
5869 if (SVT.bitsGT(VT.getScalarType())) {
5870 for (SDValue &Op : Elts) {
5871 if (Op.isUndef())
5872 Op = DAG.getUNDEF(SVT);
5873 else
5874 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
5875 ? DAG.getZExtOrTrunc(Op, DL, SVT)
5876 : DAG.getSExtOrTrunc(Op, DL, SVT);
5877 }
5878 }
5879
5880 SDValue V = DAG.getBuildVector(VT, DL, Elts);
5881 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
5882 return V;
5883}
5884
5885/// Gets or creates the specified node.
5886SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
5887 SDVTList VTs = getVTList(VT);
5889 AddNodeIDNode(ID, Opcode, VTs, std::nullopt);
5890 void *IP = nullptr;
5891 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
5892 return SDValue(E, 0);
5893
5894 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
5895 CSEMap.InsertNode(N, IP);
5896
5897 InsertNode(N);
5898 SDValue V = SDValue(N, 0);
5899 NewSDValueDbgMsg(V, "Creating new node: ", this);
5900 return V;
5901}
5902
5903SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5904 SDValue N1) {
5905 SDNodeFlags Flags;
5906 if (Inserter)
5907 Flags = Inserter->getFlags();
5908 return getNode(Opcode, DL, VT, N1, Flags);
5909}
5910
5911SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
5912 SDValue N1, const SDNodeFlags Flags) {
5913 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
5914
5915 // Constant fold unary operations with a vector integer or float operand.
5916 switch (Opcode) {
5917 default:
5918 // FIXME: Entirely reasonable to perform folding of other unary
5919 // operations here as the need arises.
5920 break;
5921 case ISD::FNEG:
5922 case ISD::FABS:
5923 case ISD::FCEIL:
5924 case ISD::FTRUNC:
5925 case ISD::FFLOOR:
5926 case ISD::FP_EXTEND:
5927 case ISD::FP_TO_SINT:
5928 case ISD::FP_TO_UINT:
5929 case ISD::FP_TO_FP16:
5930 case ISD::FP_TO_BF16:
5931 case ISD::TRUNCATE:
5932 case ISD::ANY_EXTEND:
5933 case ISD::ZERO_EXTEND:
5934 case ISD::SIGN_EXTEND:
5935 case ISD::UINT_TO_FP:
5936 case ISD::SINT_TO_FP:
5937 case ISD::FP16_TO_FP:
5938 case ISD::BF16_TO_FP:
5939 case ISD::BITCAST:
5940 case ISD::ABS:
5941 case ISD::BITREVERSE:
5942 case ISD::BSWAP:
5943 case ISD::CTLZ:
5945 case ISD::CTTZ:
5947 case ISD::CTPOP:
5948 case ISD::STEP_VECTOR: {
5949 SDValue Ops = {N1};
5950 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
5951 return Fold;
5952 }
5953 }
5954
5955 unsigned OpOpcode = N1.getNode()->getOpcode();
5956 switch (Opcode) {
5957 case ISD::STEP_VECTOR:
5958 assert(VT.isScalableVector() &&
5959 "STEP_VECTOR can only be used with scalable types");
5960 assert(OpOpcode == ISD::TargetConstant &&
5961 VT.getVectorElementType() == N1.getValueType() &&
5962 "Unexpected step operand");
5963 break;
5964 case ISD::FREEZE:
5965 assert(VT == N1.getValueType() && "Unexpected VT!");
5966 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
5967 /*Depth*/ 1))
5968 return N1;
5969 break;
5970 case ISD::TokenFactor:
5971 case ISD::MERGE_VALUES:
5973 return N1; // Factor, merge or concat of one node? No need.
5974 case ISD::BUILD_VECTOR: {
5975 // Attempt to simplify BUILD_VECTOR.
5976 SDValue Ops[] = {N1};
5977 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
5978 return V;
5979 break;
5980 }
5981 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
5982 case ISD::FP_EXTEND:
5984 "Invalid FP cast!");
5985 if (N1.getValueType() == VT) return N1; // noop conversion.
5986 assert((!VT.isVector() || VT.getVectorElementCount() ==
5988 "Vector element count mismatch!");
5989 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
5990 if (N1.isUndef())
5991 return getUNDEF(VT);
5992 break;
5993 case ISD::FP_TO_SINT:
5994 case ISD::FP_TO_UINT:
5995 if (N1.isUndef())
5996 return getUNDEF(VT);
5997 break;
5998 case ISD::SINT_TO_FP:
5999 case ISD::UINT_TO_FP:
6000 // [us]itofp(undef) = 0, because the result value is bounded.
6001 if (N1.isUndef())
6002 return getConstantFP(0.0, DL, VT);
6003 break;
6004 case ISD::SIGN_EXTEND:
6005 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6006 "Invalid SIGN_EXTEND!");
6007 assert(VT.isVector() == N1.getValueType().isVector() &&
6008 "SIGN_EXTEND result type type should be vector iff the operand "
6009 "type is vector!");
6010 if (N1.getValueType() == VT) return N1; // noop extension
6011 assert((!VT.isVector() || VT.getVectorElementCount() ==
6013 "Vector element count mismatch!");
6014 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6015 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6016 SDNodeFlags Flags;
6017 if (OpOpcode == ISD::ZERO_EXTEND)
6018 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6019 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6020 }
6021 if (OpOpcode == ISD::UNDEF)
6022 // sext(undef) = 0, because the top bits will all be the same.
6023 return getConstant(0, DL, VT);
6024 break;
6025 case ISD::ZERO_EXTEND:
6026 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6027 "Invalid ZERO_EXTEND!");
6028 assert(VT.isVector() == N1.getValueType().isVector() &&
6029 "ZERO_EXTEND result type type should be vector iff the operand "
6030 "type is vector!");
6031 if (N1.getValueType() == VT) return N1; // noop extension
6032 assert((!VT.isVector() || VT.getVectorElementCount() ==
6034 "Vector element count mismatch!");
6035 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6036 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6037 SDNodeFlags Flags;
6038 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6039 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6040 }
6041 if (OpOpcode == ISD::UNDEF)
6042 // zext(undef) = 0, because the top bits will be zero.
6043 return getConstant(0, DL, VT);
6044
6045 // Skip unnecessary zext_inreg pattern:
6046 // (zext (trunc x)) -> x iff the upper bits are known zero.
6047 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6048 // use to recognise zext_inreg patterns.
6049 if (OpOpcode == ISD::TRUNCATE) {
6050 SDValue OpOp = N1.getOperand(0);
6051 if (OpOp.getValueType() == VT) {
6052 if (OpOp.getOpcode() != ISD::AND) {
6055 if (MaskedValueIsZero(OpOp, HiBits)) {
6056 transferDbgValues(N1, OpOp);
6057 return OpOp;
6058 }
6059 }
6060 }
6061 }
6062 break;
6063 case ISD::ANY_EXTEND:
6064 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6065 "Invalid ANY_EXTEND!");
6066 assert(VT.isVector() == N1.getValueType().isVector() &&
6067 "ANY_EXTEND result type type should be vector iff the operand "
6068 "type is vector!");
6069 if (N1.getValueType() == VT) return N1; // noop extension
6070 assert((!VT.isVector() || VT.getVectorElementCount() ==
6072 "Vector element count mismatch!");
6073 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6074
6075 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6076 OpOpcode == ISD::ANY_EXTEND) {
6077 SDNodeFlags Flags;
6078 if (OpOpcode == ISD::ZERO_EXTEND)
6079 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6080 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6081 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6082 }
6083 if (OpOpcode == ISD::UNDEF)
6084 return getUNDEF(VT);
6085
6086 // (ext (trunc x)) -> x
6087 if (OpOpcode == ISD::TRUNCATE) {
6088 SDValue OpOp = N1.getOperand(0);
6089 if (OpOp.getValueType() == VT) {
6090 transferDbgValues(N1, OpOp);
6091 return OpOp;
6092 }
6093 }
6094 break;
6095 case ISD::TRUNCATE:
6096 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6097 "Invalid TRUNCATE!");
6098 assert(VT.isVector() == N1.getValueType().isVector() &&
6099 "TRUNCATE result type type should be vector iff the operand "
6100 "type is vector!");
6101 if (N1.getValueType() == VT) return N1; // noop truncate
6102 assert((!VT.isVector() || VT.getVectorElementCount() ==
6104 "Vector element count mismatch!");
6105 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6106 if (OpOpcode == ISD::TRUNCATE)
6107 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6108 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6109 OpOpcode == ISD::ANY_EXTEND) {
6110 // If the source is smaller than the dest, we still need an extend.
6112 VT.getScalarType()))
6113 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
6114 if (N1.getOperand(0).getValueType().bitsGT(VT))
6115 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6116 return N1.getOperand(0);
6117 }
6118 if (OpOpcode == ISD::UNDEF)
6119 return getUNDEF(VT);
6120 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6121 return getVScale(DL, VT,
6123 break;
6127 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6128 assert(N1.getValueType().bitsLE(VT) &&
6129 "The input must be the same size or smaller than the result.");
6132 "The destination vector type must have fewer lanes than the input.");
6133 break;
6134 case ISD::ABS:
6135 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6136 if (OpOpcode == ISD::UNDEF)
6137 return getConstant(0, DL, VT);
6138 break;
6139 case ISD::BSWAP:
6140 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6141 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6142 "BSWAP types must be a multiple of 16 bits!");
6143 if (OpOpcode == ISD::UNDEF)
6144 return getUNDEF(VT);
6145 // bswap(bswap(X)) -> X.
6146 if (OpOpcode == ISD::BSWAP)
6147 return N1.getOperand(0);
6148 break;
6149 case ISD::BITREVERSE:
6150 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6151 if (OpOpcode == ISD::UNDEF)
6152 return getUNDEF(VT);
6153 break;
6154 case ISD::BITCAST:
6156 "Cannot BITCAST between types of different sizes!");
6157 if (VT == N1.getValueType()) return N1; // noop conversion.
6158 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6159 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6160 if (OpOpcode == ISD::UNDEF)
6161 return getUNDEF(VT);
6162 break;
6164 assert(VT.isVector() && !N1.getValueType().isVector() &&
6165 (VT.getVectorElementType() == N1.getValueType() ||
6167 N1.getValueType().isInteger() &&
6169 "Illegal SCALAR_TO_VECTOR node!");
6170 if (OpOpcode == ISD::UNDEF)
6171 return getUNDEF(VT);
6172 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6173 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6174 isa<ConstantSDNode>(N1.getOperand(1)) &&
6175 N1.getConstantOperandVal(1) == 0 &&
6176 N1.getOperand(0).getValueType() == VT)
6177 return N1.getOperand(0);
6178 break;
6179 case ISD::FNEG:
6180 // Negation of an unknown bag of bits is still completely undefined.
6181 if (OpOpcode == ISD::UNDEF)
6182 return getUNDEF(VT);
6183
6184 if (OpOpcode == ISD::FNEG) // --X -> X
6185 return N1.getOperand(0);
6186 break;
6187 case ISD::FABS:
6188 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6189 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6190 break;
6191 case ISD::VSCALE:
6192 assert(VT == N1.getValueType() && "Unexpected VT!");
6193 break;
6194 case ISD::CTPOP:
6195 if (N1.getValueType().getScalarType() == MVT::i1)
6196 return N1;
6197 break;
6198 case ISD::CTLZ:
6199 case ISD::CTTZ:
6200 if (N1.getValueType().getScalarType() == MVT::i1)
6201 return getNOT(DL, N1, N1.getValueType());
6202 break;
6203 case ISD::VECREDUCE_ADD:
6204 if (N1.getValueType().getScalarType() == MVT::i1)
6205 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6206 break;
6209 if (N1.getValueType().getScalarType() == MVT::i1)
6210 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6211 break;
6214 if (N1.getValueType().getScalarType() == MVT::i1)
6215 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6216 break;
6217 case ISD::SPLAT_VECTOR:
6218 assert(VT.isVector() && "Wrong return type!");
6219 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6220 // that for now.
6222 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6224 N1.getValueType().isInteger() &&
6226 "Wrong operand type!");
6227 break;
6228 }
6229
6230 SDNode *N;
6231 SDVTList VTs = getVTList(VT);
6232 SDValue Ops[] = {N1};
6233 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6235 AddNodeIDNode(ID, Opcode, VTs, Ops);
6236 void *IP = nullptr;
6237 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6238 E->intersectFlagsWith(Flags);
6239 return SDValue(E, 0);
6240 }
6241
6242 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6243 N->setFlags(Flags);
6244 createOperands(N, Ops);
6245 CSEMap.InsertNode(N, IP);
6246 } else {
6247 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6248 createOperands(N, Ops);
6249 }
6250
6251 InsertNode(N);
6252 SDValue V = SDValue(N, 0);
6253 NewSDValueDbgMsg(V, "Creating new node: ", this);
6254 return V;
6255}
6256
6257static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6258 const APInt &C2) {
6259 switch (Opcode) {
6260 case ISD::ADD: return C1 + C2;
6261 case ISD::SUB: return C1 - C2;
6262 case ISD::MUL: return C1 * C2;
6263 case ISD::AND: return C1 & C2;
6264 case ISD::OR: return C1 | C2;
6265 case ISD::XOR: return C1 ^ C2;
6266 case ISD::SHL: return C1 << C2;
6267 case ISD::SRL: return C1.lshr(C2);
6268 case ISD::SRA: return C1.ashr(C2);
6269 case ISD::ROTL: return C1.rotl(C2);
6270 case ISD::ROTR: return C1.rotr(C2);
6271 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6272 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6273 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6274 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6275 case ISD::SADDSAT: return C1.sadd_sat(C2);
6276 case ISD::UADDSAT: return C1.uadd_sat(C2);
6277 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6278 case ISD::USUBSAT: return C1.usub_sat(C2);
6279 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6280 case ISD::USHLSAT: return C1.ushl_sat(C2);
6281 case ISD::UDIV:
6282 if (!C2.getBoolValue())
6283 break;
6284 return C1.udiv(C2);
6285 case ISD::UREM:
6286 if (!C2.getBoolValue())
6287 break;
6288 return C1.urem(C2);
6289 case ISD::SDIV:
6290 if (!C2.getBoolValue())
6291 break;
6292 return C1.sdiv(C2);
6293 case ISD::SREM:
6294 if (!C2.getBoolValue())
6295 break;
6296 return C1.srem(C2);
6297 case ISD::AVGFLOORS:
6298 return APIntOps::avgFloorS(C1, C2);
6299 case ISD::AVGFLOORU:
6300 return APIntOps::avgFloorU(C1, C2);
6301 case ISD::AVGCEILS:
6302 return APIntOps::avgCeilS(C1, C2);
6303 case ISD::AVGCEILU:
6304 return APIntOps::avgCeilU(C1, C2);
6305 case ISD::ABDS:
6306 return APIntOps::abds(C1, C2);
6307 case ISD::ABDU:
6308 return APIntOps::abdu(C1, C2);
6309 case ISD::MULHS:
6310 return APIntOps::mulhs(C1, C2);
6311 case ISD::MULHU:
6312 return APIntOps::mulhu(C1, C2);
6313 }
6314 return std::nullopt;
6315}
6316// Handle constant folding with UNDEF.
6317// TODO: Handle more cases.
6318static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6319 bool IsUndef1, const APInt &C2,
6320 bool IsUndef2) {
6321 if (!(IsUndef1 || IsUndef2))
6322 return FoldValue(Opcode, C1, C2);
6323
6324 // Fold and(x, undef) -> 0
6325 // Fold mul(x, undef) -> 0
6326 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6327 return APInt::getZero(C1.getBitWidth());
6328
6329 return std::nullopt;
6330}
6331
6333 const GlobalAddressSDNode *GA,
6334 const SDNode *N2) {
6335 if (GA->getOpcode() != ISD::GlobalAddress)
6336 return SDValue();
6337 if (!TLI->isOffsetFoldingLegal(GA))
6338 return SDValue();
6339 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6340 if (!C2)
6341 return SDValue();
6342 int64_t Offset = C2->getSExtValue();
6343 switch (Opcode) {
6344 case ISD::ADD: break;
6345 case ISD::SUB: Offset = -uint64_t(Offset); break;
6346 default: return SDValue();
6347 }
6348 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6349 GA->getOffset() + uint64_t(Offset));
6350}
6351
6352bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6353 switch (Opcode) {
6354 case ISD::SDIV:
6355 case ISD::UDIV:
6356 case ISD::SREM:
6357 case ISD::UREM: {
6358 // If a divisor is zero/undef or any element of a divisor vector is
6359 // zero/undef, the whole op is undef.
6360 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6361 SDValue Divisor = Ops[1];
6362 if (Divisor.isUndef() || isNullConstant(Divisor))
6363 return true;
6364
6365 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6366 llvm::any_of(Divisor->op_values(),
6367 [](SDValue V) { return V.isUndef() ||
6368 isNullConstant(V); });
6369 // TODO: Handle signed overflow.
6370 }
6371 // TODO: Handle oversized shifts.
6372 default:
6373 return false;
6374 }
6375}
6376
6378 EVT VT, ArrayRef<SDValue> Ops,
6379 SDNodeFlags Flags) {
6380 // If the opcode is a target-specific ISD node, there's nothing we can
6381 // do here and the operand rules may not line up with the below, so
6382 // bail early.
6383 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6384 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6385 // foldCONCAT_VECTORS in getNode before this is called.
6386 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6387 return SDValue();
6388
6389 unsigned NumOps = Ops.size();
6390 if (NumOps == 0)
6391 return SDValue();
6392
6393 if (isUndef(Opcode, Ops))
6394 return getUNDEF(VT);
6395
6396 // Handle unary special cases.
6397 if (NumOps == 1) {
6398 SDValue N1 = Ops[0];
6399
6400 // Constant fold unary operations with an integer constant operand. Even
6401 // opaque constant will be folded, because the folding of unary operations
6402 // doesn't create new constants with different values. Nevertheless, the
6403 // opaque flag is preserved during folding to prevent future folding with
6404 // other constants.
6405 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6406 const APInt &Val = C->getAPIntValue();
6407 switch (Opcode) {
6408 case ISD::SIGN_EXTEND:
6409 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6410 C->isTargetOpcode(), C->isOpaque());
6411 case ISD::TRUNCATE:
6412 if (C->isOpaque())
6413 break;
6414 [[fallthrough]];
6415 case ISD::ZERO_EXTEND:
6416 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6417 C->isTargetOpcode(), C->isOpaque());
6418 case ISD::ANY_EXTEND:
6419 // Some targets like RISCV prefer to sign extend some types.
6420 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6421 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6422 C->isTargetOpcode(), C->isOpaque());
6423 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6424 C->isTargetOpcode(), C->isOpaque());
6425 case ISD::ABS:
6426 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6427 C->isOpaque());
6428 case ISD::BITREVERSE:
6429 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6430 C->isOpaque());
6431 case ISD::BSWAP:
6432 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6433 C->isOpaque());
6434 case ISD::CTPOP:
6435 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6436 C->isOpaque());
6437 case ISD::CTLZ:
6439 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6440 C->isOpaque());
6441 case ISD::CTTZ:
6443 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6444 C->isOpaque());
6445 case ISD::UINT_TO_FP:
6446 case ISD::SINT_TO_FP: {
6449 (void)apf.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6451 return getConstantFP(apf, DL, VT);
6452 }
6453 case ISD::FP16_TO_FP:
6454 case ISD::BF16_TO_FP: {
6455 bool Ignored;
6456 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6457 : APFloat::BFloat(),
6458 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6459
6460 // This can return overflow, underflow, or inexact; we don't care.
6461 // FIXME need to be more flexible about rounding mode.
6462 (void)FPV.convert(EVTToAPFloatSemantics(VT),
6464 return getConstantFP(FPV, DL, VT);
6465 }
6466 case ISD::STEP_VECTOR:
6467 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6468 return V;
6469 break;
6470 case ISD::BITCAST:
6471 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6472 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6473 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6474 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6475 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6476 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6477 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6478 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6479 break;
6480 }
6481 }
6482
6483 // Constant fold unary operations with a floating point constant operand.
6484 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6485 APFloat V = C->getValueAPF(); // make copy
6486 switch (Opcode) {
6487 case ISD::FNEG:
6488 V.changeSign();
6489 return getConstantFP(V, DL, VT);
6490 case ISD::FABS:
6491 V.clearSign();
6492 return getConstantFP(V, DL, VT);
6493 case ISD::FCEIL: {
6494 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6495 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6496 return getConstantFP(V, DL, VT);
6497 return SDValue();
6498 }
6499 case ISD::FTRUNC: {
6500 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6501 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6502 return getConstantFP(V, DL, VT);
6503 return SDValue();
6504 }
6505 case ISD::FFLOOR: {
6506 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6507 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6508 return getConstantFP(V, DL, VT);
6509 return SDValue();
6510 }
6511 case ISD::FP_EXTEND: {
6512 bool ignored;
6513 // This can return overflow, underflow, or inexact; we don't care.
6514 // FIXME need to be more flexible about rounding mode.
6516 &ignored);
6517 return getConstantFP(V, DL, VT);
6518 }
6519 case ISD::FP_TO_SINT:
6520 case ISD::FP_TO_UINT: {
6521 bool ignored;
6522 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6523 // FIXME need to be more flexible about rounding mode.
6525 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6526 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6527 break;
6528 return getConstant(IntVal, DL, VT);
6529 }
6530 case ISD::FP_TO_FP16:
6531 case ISD::FP_TO_BF16: {
6532 bool Ignored;
6533 // This can return overflow, underflow, or inexact; we don't care.
6534 // FIXME need to be more flexible about rounding mode.
6535 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6536 : APFloat::BFloat(),
6538 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6539 }
6540 case ISD::BITCAST:
6541 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6542 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6543 VT);
6544 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6545 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6546 VT);
6547 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6548 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6549 VT);
6550 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6551 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6552 break;
6553 }
6554 }
6555
6556 // Early-out if we failed to constant fold a bitcast.
6557 if (Opcode == ISD::BITCAST)
6558 return SDValue();
6559 }
6560
6561 // Handle binops special cases.
6562 if (NumOps == 2) {
6563 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6564 return CFP;
6565
6566 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6567 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6568 if (C1->isOpaque() || C2->isOpaque())
6569 return SDValue();
6570
6571 std::optional<APInt> FoldAttempt =
6572 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6573 if (!FoldAttempt)
6574 return SDValue();
6575
6576 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6577 assert((!Folded || !VT.isVector()) &&
6578 "Can't fold vectors ops with scalar operands");
6579 return Folded;
6580 }
6581 }
6582
6583 // fold (add Sym, c) -> Sym+c
6584 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6585 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6586 if (TLI->isCommutativeBinOp(Opcode))
6587 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6588 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6589 }
6590
6591 // This is for vector folding only from here on.
6592 if (!VT.isVector())
6593 return SDValue();
6594
6595 ElementCount NumElts = VT.getVectorElementCount();
6596
6597 // See if we can fold through any bitcasted integer ops.
6598 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6599 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6600 (Ops[0].getOpcode() == ISD::BITCAST ||
6601 Ops[1].getOpcode() == ISD::BITCAST)) {
6602 SDValue N1 = peekThroughBitcasts(Ops[0]);
6603 SDValue N2 = peekThroughBitcasts(Ops[1]);
6604 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6605 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6606 if (BV1 && BV2 && N1.getValueType().isInteger() &&
6607 N2.getValueType().isInteger()) {
6608 bool IsLE = getDataLayout().isLittleEndian();
6609 unsigned EltBits = VT.getScalarSizeInBits();
6610 SmallVector<APInt> RawBits1, RawBits2;
6611 BitVector UndefElts1, UndefElts2;
6612 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6613 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6614 SmallVector<APInt> RawBits;
6615 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6616 std::optional<APInt> Fold = FoldValueWithUndef(
6617 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6618 if (!Fold)
6619 break;
6620 RawBits.push_back(*Fold);
6621 }
6622 if (RawBits.size() == NumElts.getFixedValue()) {
6623 // We have constant folded, but we might need to cast this again back
6624 // to the original (possibly legalized) type.
6625 EVT BVVT, BVEltVT;
6626 if (N1.getValueType() == VT) {
6627 BVVT = N1.getValueType();
6628 BVEltVT = BV1->getOperand(0).getValueType();
6629 } else {
6630 BVVT = N2.getValueType();
6631 BVEltVT = BV2->getOperand(0).getValueType();
6632 }
6633 unsigned BVEltBits = BVEltVT.getSizeInBits();
6634 SmallVector<APInt> DstBits;
6635 BitVector DstUndefs;
6637 DstBits, RawBits, DstUndefs,
6638 BitVector(RawBits.size(), false));
6639 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6640 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6641 if (DstUndefs[I])
6642 continue;
6643 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6644 }
6645 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6646 }
6647 }
6648 }
6649 }
6650
6651 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6652 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6653 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6654 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6655 APInt RHSVal;
6656 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6657 APInt NewStep = Opcode == ISD::MUL
6658 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6659 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6660 return getStepVector(DL, VT, NewStep);
6661 }
6662 }
6663
6664 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6665 return !Op.getValueType().isVector() ||
6666 Op.getValueType().getVectorElementCount() == NumElts;
6667 };
6668
6669 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6670 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6671 Op.getOpcode() == ISD::BUILD_VECTOR ||
6672 Op.getOpcode() == ISD::SPLAT_VECTOR;
6673 };
6674
6675 // All operands must be vector types with the same number of elements as
6676 // the result type and must be either UNDEF or a build/splat vector
6677 // or UNDEF scalars.
6678 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6679 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6680 return SDValue();
6681
6682 // If we are comparing vectors, then the result needs to be a i1 boolean that
6683 // is then extended back to the legal result type depending on how booleans
6684 // are represented.
6685 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6686 ISD::NodeType ExtendCode =
6687 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
6690
6691 // Find legal integer scalar type for constant promotion and
6692 // ensure that its scalar size is at least as large as source.
6693 EVT LegalSVT = VT.getScalarType();
6694 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
6695 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
6696 if (LegalSVT.bitsLT(VT.getScalarType()))
6697 return SDValue();
6698 }
6699
6700 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
6701 // only have one operand to check. For fixed-length vector types we may have
6702 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
6703 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
6704
6705 // Constant fold each scalar lane separately.
6706 SmallVector<SDValue, 4> ScalarResults;
6707 for (unsigned I = 0; I != NumVectorElts; I++) {
6708 SmallVector<SDValue, 4> ScalarOps;
6709 for (SDValue Op : Ops) {
6710 EVT InSVT = Op.getValueType().getScalarType();
6711 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
6712 Op.getOpcode() != ISD::SPLAT_VECTOR) {
6713 if (Op.isUndef())
6714 ScalarOps.push_back(getUNDEF(InSVT));
6715 else
6716 ScalarOps.push_back(Op);
6717 continue;
6718 }
6719
6720 SDValue ScalarOp =
6721 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
6722 EVT ScalarVT = ScalarOp.getValueType();
6723
6724 // Build vector (integer) scalar operands may need implicit
6725 // truncation - do this before constant folding.
6726 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
6727 // Don't create illegally-typed nodes unless they're constants or undef
6728 // - if we fail to constant fold we can't guarantee the (dead) nodes
6729 // we're creating will be cleaned up before being visited for
6730 // legalization.
6731 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
6732 !isa<ConstantSDNode>(ScalarOp) &&
6733 TLI->getTypeAction(*getContext(), InSVT) !=
6735 return SDValue();
6736 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
6737 }
6738
6739 ScalarOps.push_back(ScalarOp);
6740 }
6741
6742 // Constant fold the scalar operands.
6743 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
6744
6745 // Legalize the (integer) scalar constant if necessary.
6746 if (LegalSVT != SVT)
6747 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
6748
6749 // Scalar folding only succeeded if the result is a constant or UNDEF.
6750 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
6751 ScalarResult.getOpcode() != ISD::ConstantFP)
6752 return SDValue();
6753 ScalarResults.push_back(ScalarResult);
6754 }
6755
6756 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
6757 : getBuildVector(VT, DL, ScalarResults);
6758 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
6759 return V;
6760}
6761
6763 EVT VT, ArrayRef<SDValue> Ops) {
6764 // TODO: Add support for unary/ternary fp opcodes.
6765 if (Ops.size() != 2)
6766 return SDValue();
6767
6768 // TODO: We don't do any constant folding for strict FP opcodes here, but we
6769 // should. That will require dealing with a potentially non-default
6770 // rounding mode, checking the "opStatus" return value from the APFloat
6771 // math calculations, and possibly other variations.
6772 SDValue N1 = Ops[0];
6773 SDValue N2 = Ops[1];
6774 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
6775 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
6776 if (N1CFP && N2CFP) {
6777 APFloat C1 = N1CFP->getValueAPF(); // make copy
6778 const APFloat &C2 = N2CFP->getValueAPF();
6779 switch (Opcode) {
6780 case ISD::FADD:
6782 return getConstantFP(C1, DL, VT);
6783 case ISD::FSUB:
6785 return getConstantFP(C1, DL, VT);
6786 case ISD::FMUL:
6788 return getConstantFP(C1, DL, VT);
6789 case ISD::FDIV:
6791 return getConstantFP(C1, DL, VT);
6792 case ISD::FREM:
6793 C1.mod(C2);
6794 return getConstantFP(C1, DL, VT);
6795 case ISD::FCOPYSIGN:
6796 C1.copySign(C2);
6797 return getConstantFP(C1, DL, VT);
6798 case ISD::FMINNUM:
6799 return getConstantFP(minnum(C1, C2), DL, VT);
6800 case ISD::FMAXNUM:
6801 return getConstantFP(maxnum(C1, C2), DL, VT);
6802 case ISD::FMINIMUM:
6803 return getConstantFP(minimum(C1, C2), DL, VT);
6804 case ISD::FMAXIMUM:
6805 return getConstantFP(maximum(C1, C2), DL, VT);
6806 default: break;
6807 }
6808 }
6809 if (N1CFP && Opcode == ISD::FP_ROUND) {
6810 APFloat C1 = N1CFP->getValueAPF(); // make copy
6811 bool Unused;
6812 // This can return overflow, underflow, or inexact; we don't care.
6813 // FIXME need to be more flexible about rounding mode.
6815 &Unused);
6816 return getConstantFP(C1, DL, VT);
6817 }
6818
6819 switch (Opcode) {
6820 case ISD::FSUB:
6821 // -0.0 - undef --> undef (consistent with "fneg undef")
6822 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
6823 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
6824 return getUNDEF(VT);
6825 [[fallthrough]];
6826
6827 case ISD::FADD:
6828 case ISD::FMUL:
6829 case ISD::FDIV:
6830 case ISD::FREM:
6831 // If both operands are undef, the result is undef. If 1 operand is undef,
6832 // the result is NaN. This should match the behavior of the IR optimizer.
6833 if (N1.isUndef() && N2.isUndef())
6834 return getUNDEF(VT);
6835 if (N1.isUndef() || N2.isUndef())
6837 }
6838 return SDValue();
6839}
6840
6842 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
6843
6844 // There's no need to assert on a byte-aligned pointer. All pointers are at
6845 // least byte aligned.
6846 if (A == Align(1))
6847 return Val;
6848
6849 SDVTList VTs = getVTList(Val.getValueType());
6851 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
6852 ID.AddInteger(A.value());
6853
6854 void *IP = nullptr;
6855 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6856 return SDValue(E, 0);
6857
6858 auto *N =
6859 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
6860 createOperands(N, {Val});
6861
6862 CSEMap.InsertNode(N, IP);
6863 InsertNode(N);
6864
6865 SDValue V(N, 0);
6866 NewSDValueDbgMsg(V, "Creating new node: ", this);
6867 return V;
6868}
6869
6870SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6871 SDValue N1, SDValue N2) {
6872 SDNodeFlags Flags;
6873 if (Inserter)
6874 Flags = Inserter->getFlags();
6875 return getNode(Opcode, DL, VT, N1, N2, Flags);
6876}
6877
6879 SDValue &N2) const {
6880 if (!TLI->isCommutativeBinOp(Opcode))
6881 return;
6882
6883 // Canonicalize:
6884 // binop(const, nonconst) -> binop(nonconst, const)
6889 if ((N1C && !N2C) || (N1CFP && !N2CFP))
6890 std::swap(N1, N2);
6891
6892 // Canonicalize:
6893 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
6894 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
6896 std::swap(N1, N2);
6897}
6898
6899SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6900 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
6902 N2.getOpcode() != ISD::DELETED_NODE &&
6903 "Operand is DELETED_NODE!");
6904
6905 canonicalizeCommutativeBinop(Opcode, N1, N2);
6906
6907 auto *N1C = dyn_cast<ConstantSDNode>(N1);
6908 auto *N2C = dyn_cast<ConstantSDNode>(N2);
6909
6910 // Don't allow undefs in vector splats - we might be returning N2 when folding
6911 // to zero etc.
6912 ConstantSDNode *N2CV =
6913 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
6914
6915 switch (Opcode) {
6916 default: break;
6917 case ISD::TokenFactor:
6918 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
6919 N2.getValueType() == MVT::Other && "Invalid token factor!");
6920 // Fold trivial token factors.
6921 if (N1.getOpcode() == ISD::EntryToken) return N2;
6922 if (N2.getOpcode() == ISD::EntryToken) return N1;
6923 if (N1 == N2) return N1;
6924 break;
6925 case ISD::BUILD_VECTOR: {
6926 // Attempt to simplify BUILD_VECTOR.
6927 SDValue Ops[] = {N1, N2};
6928 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6929 return V;
6930 break;
6931 }
6932 case ISD::CONCAT_VECTORS: {
6933 SDValue Ops[] = {N1, N2};
6934 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
6935 return V;
6936 break;
6937 }
6938 case ISD::AND:
6939 assert(VT.isInteger() && "This operator does not apply to FP types!");
6940 assert(N1.getValueType() == N2.getValueType() &&
6941 N1.getValueType() == VT && "Binary operator types must match!");
6942 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
6943 // worth handling here.
6944 if (N2CV && N2CV->isZero())
6945 return N2;
6946 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
6947 return N1;
6948 break;
6949 case ISD::OR:
6950 case ISD::XOR:
6951 case ISD::ADD:
6952 case ISD::SUB:
6953 assert(VT.isInteger() && "This operator does not apply to FP types!");
6954 assert(N1.getValueType() == N2.getValueType() &&
6955 N1.getValueType() == VT && "Binary operator types must match!");
6956 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
6957 // it's worth handling here.
6958 if (N2CV && N2CV->isZero())
6959 return N1;
6960 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
6961 VT.getVectorElementType() == MVT::i1)
6962 return getNode(ISD::XOR, DL, VT, N1, N2);
6963 break;
6964 case ISD::MUL:
6965 assert(VT.isInteger() && "This operator does not apply to FP types!");
6966 assert(N1.getValueType() == N2.getValueType() &&
6967 N1.getValueType() == VT && "Binary operator types must match!");
6968 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
6969 return getNode(ISD::AND, DL, VT, N1, N2);
6970 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
6971 const APInt &MulImm = N1->getConstantOperandAPInt(0);
6972 const APInt &N2CImm = N2C->getAPIntValue();
6973 return getVScale(DL, VT, MulImm * N2CImm);
6974 }
6975 break;
6976 case ISD::UDIV:
6977 case ISD::UREM:
6978 case ISD::MULHU:
6979 case ISD::MULHS:
6980 case ISD::SDIV:
6981 case ISD::SREM:
6982 case ISD::SADDSAT:
6983 case ISD::SSUBSAT:
6984 case ISD::UADDSAT:
6985 case ISD::USUBSAT:
6986 assert(VT.isInteger() && "This operator does not apply to FP types!");
6987 assert(N1.getValueType() == N2.getValueType() &&
6988 N1.getValueType() == VT && "Binary operator types must match!");
6989 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
6990 // fold (add_sat x, y) -> (or x, y) for bool types.
6991 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
6992 return getNode(ISD::OR, DL, VT, N1, N2);
6993 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
6994 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
6995 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
6996 }
6997 break;
6998 case ISD::SCMP:
6999 case ISD::UCMP:
7000 assert(N1.getValueType() == N2.getValueType() &&
7001 "Types of operands of UCMP/SCMP must match");
7002 assert(N1.getValueType().isVector() == VT.isVector() &&
7003 "Operands and return type of must both be scalars or vectors");
7004 if (VT.isVector())
7007 "Result and operands must have the same number of elements");
7008 break;
7009 case ISD::AVGFLOORS:
7010 case ISD::AVGFLOORU:
7011 case ISD::AVGCEILS:
7012 case ISD::AVGCEILU:
7013 assert(VT.isInteger() && "This operator does not apply to FP types!");
7014 assert(N1.getValueType() == N2.getValueType() &&
7015 N1.getValueType() == VT && "Binary operator types must match!");
7016 break;
7017 case ISD::ABDS:
7018 case ISD::ABDU:
7019 assert(VT.isInteger() && "This operator does not apply to FP types!");
7020 assert(N1.getValueType() == N2.getValueType() &&
7021 N1.getValueType() == VT && "Binary operator types must match!");
7022 break;
7023 case ISD::SMIN:
7024 case ISD::UMAX:
7025 assert(VT.isInteger() && "This operator does not apply to FP types!");
7026 assert(N1.getValueType() == N2.getValueType() &&
7027 N1.getValueType() == VT && "Binary operator types must match!");
7028 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7029 return getNode(ISD::OR, DL, VT, N1, N2);
7030 break;
7031 case ISD::SMAX:
7032 case ISD::UMIN:
7033 assert(VT.isInteger() && "This operator does not apply to FP types!");
7034 assert(N1.getValueType() == N2.getValueType() &&
7035 N1.getValueType() == VT && "Binary operator types must match!");
7036 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7037 return getNode(ISD::AND, DL, VT, N1, N2);
7038 break;
7039 case ISD::FADD:
7040 case ISD::FSUB:
7041 case ISD::FMUL:
7042 case ISD::FDIV:
7043 case ISD::FREM:
7044 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7045 assert(N1.getValueType() == N2.getValueType() &&
7046 N1.getValueType() == VT && "Binary operator types must match!");
7047 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7048 return V;
7049 break;
7050 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7051 assert(N1.getValueType() == VT &&
7054 "Invalid FCOPYSIGN!");
7055 break;
7056 case ISD::SHL:
7057 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7058 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7059 const APInt &ShiftImm = N2C->getAPIntValue();
7060 return getVScale(DL, VT, MulImm << ShiftImm);
7061 }
7062 [[fallthrough]];
7063 case ISD::SRA:
7064 case ISD::SRL:
7065 if (SDValue V = simplifyShift(N1, N2))
7066 return V;
7067 [[fallthrough]];
7068 case ISD::ROTL:
7069 case ISD::ROTR:
7070 assert(VT == N1.getValueType() &&
7071 "Shift operators return type must be the same as their first arg");
7072 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7073 "Shifts only work on integers");
7074 assert((!VT.isVector() || VT == N2.getValueType()) &&
7075 "Vector shift amounts must be in the same as their first arg");
7076 // Verify that the shift amount VT is big enough to hold valid shift
7077 // amounts. This catches things like trying to shift an i1024 value by an
7078 // i8, which is easy to fall into in generic code that uses
7079 // TLI.getShiftAmount().
7082 "Invalid use of small shift amount with oversized value!");
7083
7084 // Always fold shifts of i1 values so the code generator doesn't need to
7085 // handle them. Since we know the size of the shift has to be less than the
7086 // size of the value, the shift/rotate count is guaranteed to be zero.
7087 if (VT == MVT::i1)
7088 return N1;
7089 if (N2CV && N2CV->isZero())
7090 return N1;
7091 break;
7092 case ISD::FP_ROUND:
7093 assert(VT.isFloatingPoint() &&
7095 VT.bitsLE(N1.getValueType()) &&
7096 N2C && (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7097 "Invalid FP_ROUND!");
7098 if (N1.getValueType() == VT) return N1; // noop conversion.
7099 break;
7100 case ISD::AssertSext:
7101 case ISD::AssertZext: {
7102 EVT EVT = cast<VTSDNode>(N2)->getVT();
7103 assert(VT == N1.getValueType() && "Not an inreg extend!");
7104 assert(VT.isInteger() && EVT.isInteger() &&
7105 "Cannot *_EXTEND_INREG FP types");
7106 assert(!EVT.isVector() &&
7107 "AssertSExt/AssertZExt type should be the vector element type "
7108 "rather than the vector type!");
7109 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7110 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7111 break;
7112 }
7114 EVT EVT = cast<VTSDNode>(N2)->getVT();
7115 assert(VT == N1.getValueType() && "Not an inreg extend!");
7116 assert(VT.isInteger() && EVT.isInteger() &&
7117 "Cannot *_EXTEND_INREG FP types");
7118 assert(EVT.isVector() == VT.isVector() &&
7119 "SIGN_EXTEND_INREG type should be vector iff the operand "
7120 "type is vector!");
7121 assert((!EVT.isVector() ||
7123 "Vector element counts must match in SIGN_EXTEND_INREG");
7124 assert(EVT.bitsLE(VT) && "Not extending!");
7125 if (EVT == VT) return N1; // Not actually extending
7126
7127 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7128 unsigned FromBits = EVT.getScalarSizeInBits();
7129 Val <<= Val.getBitWidth() - FromBits;
7130 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7131 return getConstant(Val, DL, ConstantVT);
7132 };
7133
7134 if (N1C) {
7135 const APInt &Val = N1C->getAPIntValue();
7136 return SignExtendInReg(Val, VT);
7137 }
7138
7141 llvm::EVT OpVT = N1.getOperand(0).getValueType();
7142 for (int i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
7143 SDValue Op = N1.getOperand(i);
7144 if (Op.isUndef()) {
7145 Ops.push_back(getUNDEF(OpVT));
7146 continue;
7147 }
7148 ConstantSDNode *C = cast<ConstantSDNode>(Op);
7149 APInt Val = C->getAPIntValue();
7150 Ops.push_back(SignExtendInReg(Val, OpVT));
7151 }
7152 return getBuildVector(VT, DL, Ops);
7153 }
7154
7155 if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7156 isa<ConstantSDNode>(N1.getOperand(0)))
7157 return getNode(
7158 ISD::SPLAT_VECTOR, DL, VT,
7159 SignExtendInReg(N1.getConstantOperandAPInt(0),
7160 N1.getOperand(0).getValueType()));
7161 break;
7162 }
7164 case ISD::FP_TO_UINT_SAT: {
7165 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7166 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7167 assert(N1.getValueType().isVector() == VT.isVector() &&
7168 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7169 "vector!");
7170 assert((!VT.isVector() || VT.getVectorElementCount() ==
7172 "Vector element counts must match in FP_TO_*INT_SAT");
7173 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7174 "Type to saturate to must be a scalar.");
7175 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7176 "Not extending!");
7177 break;
7178 }
7181 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7182 element type of the vector.");
7183
7184 // Extract from an undefined value or using an undefined index is undefined.
7185 if (N1.isUndef() || N2.isUndef())
7186 return getUNDEF(VT);
7187
7188 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7189 // vectors. For scalable vectors we will provide appropriate support for
7190 // dealing with arbitrary indices.
7191 if (N2C && N1.getValueType().isFixedLengthVector() &&
7192 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7193 return getUNDEF(VT);
7194
7195 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7196 // expanding copies of large vectors from registers. This only works for
7197 // fixed length vectors, since we need to know the exact number of
7198 // elements.
7199 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7201 unsigned Factor =
7204 N1.getOperand(N2C->getZExtValue() / Factor),
7205 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7206 }
7207
7208 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7209 // lowering is expanding large vector constants.
7210 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7211 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7214 "BUILD_VECTOR used for scalable vectors");
7215 unsigned Index =
7216 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7217 SDValue Elt = N1.getOperand(Index);
7218
7219 if (VT != Elt.getValueType())
7220 // If the vector element type is not legal, the BUILD_VECTOR operands
7221 // are promoted and implicitly truncated, and the result implicitly
7222 // extended. Make that explicit here.
7223 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7224
7225 return Elt;
7226 }
7227
7228 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7229 // operations are lowered to scalars.
7230 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7231 // If the indices are the same, return the inserted element else
7232 // if the indices are known different, extract the element from
7233 // the original vector.
7234 SDValue N1Op2 = N1.getOperand(2);
7235 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7236
7237 if (N1Op2C && N2C) {
7238 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7239 if (VT == N1.getOperand(1).getValueType())
7240 return N1.getOperand(1);
7241 if (VT.isFloatingPoint()) {
7243 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7244 }
7245 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7246 }
7247 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7248 }
7249 }
7250
7251 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7252 // when vector types are scalarized and v1iX is legal.
7253 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7254 // Here we are completely ignoring the extract element index (N2),
7255 // which is fine for fixed width vectors, since any index other than 0
7256 // is undefined anyway. However, this cannot be ignored for scalable
7257 // vectors - in theory we could support this, but we don't want to do this
7258 // without a profitability check.
7259 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7261 N1.getValueType().getVectorNumElements() == 1) {
7262 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7263 N1.getOperand(1));
7264 }
7265 break;
7267 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7268 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7269 (N1.getValueType().isInteger() == VT.isInteger()) &&
7270 N1.getValueType() != VT &&
7271 "Wrong types for EXTRACT_ELEMENT!");
7272
7273 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7274 // 64-bit integers into 32-bit parts. Instead of building the extract of
7275 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7276 if (N1.getOpcode() == ISD::BUILD_PAIR)
7277 return N1.getOperand(N2C->getZExtValue());
7278
7279 // EXTRACT_ELEMENT of a constant int is also very common.
7280 if (N1C) {
7281 unsigned ElementSize = VT.getSizeInBits();
7282 unsigned Shift = ElementSize * N2C->getZExtValue();
7283 const APInt &Val = N1C->getAPIntValue();
7284 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7285 }
7286 break;
7288 EVT N1VT = N1.getValueType();
7289 assert(VT.isVector() && N1VT.isVector() &&
7290 "Extract subvector VTs must be vectors!");
7292 "Extract subvector VTs must have the same element type!");
7293 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7294 "Cannot extract a scalable vector from a fixed length vector!");
7295 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7297 "Extract subvector must be from larger vector to smaller vector!");
7298 assert(N2C && "Extract subvector index must be a constant");
7299 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7300 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7301 N1VT.getVectorMinNumElements()) &&
7302 "Extract subvector overflow!");
7303 assert(N2C->getAPIntValue().getBitWidth() ==
7304 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7305 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7306
7307 // Trivial extraction.
7308 if (VT == N1VT)
7309 return N1;
7310
7311 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7312 if (N1.isUndef())
7313 return getUNDEF(VT);
7314
7315 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7316 // the concat have the same type as the extract.
7317 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7318 VT == N1.getOperand(0).getValueType()) {
7319 unsigned Factor = VT.getVectorMinNumElements();
7320 return N1.getOperand(N2C->getZExtValue() / Factor);
7321 }
7322
7323 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7324 // during shuffle legalization.
7325 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7326 VT == N1.getOperand(1).getValueType())
7327 return N1.getOperand(1);
7328 break;
7329 }
7330 }
7331
7332 // Perform trivial constant folding.
7333 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7334 return SV;
7335
7336 // Canonicalize an UNDEF to the RHS, even over a constant.
7337 if (N1.isUndef()) {
7338 if (TLI->isCommutativeBinOp(Opcode)) {
7339 std::swap(N1, N2);
7340 } else {
7341 switch (Opcode) {
7342 case ISD::SUB:
7343 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7345 case ISD::UDIV:
7346 case ISD::SDIV:
7347 case ISD::UREM:
7348 case ISD::SREM:
7349 case ISD::SSUBSAT:
7350 case ISD::USUBSAT:
7351 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7352 }
7353 }
7354 }
7355
7356 // Fold a bunch of operators when the RHS is undef.
7357 if (N2.isUndef()) {
7358 switch (Opcode) {
7359 case ISD::XOR:
7360 if (N1.isUndef())
7361 // Handle undef ^ undef -> 0 special case. This is a common
7362 // idiom (misuse).
7363 return getConstant(0, DL, VT);
7364 [[fallthrough]];
7365 case ISD::ADD:
7366 case ISD::SUB:
7367 case ISD::UDIV:
7368 case ISD::SDIV:
7369 case ISD::UREM:
7370 case ISD::SREM:
7371 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7372 case ISD::MUL:
7373 case ISD::AND:
7374 case ISD::SSUBSAT:
7375 case ISD::USUBSAT:
7376 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7377 case ISD::OR:
7378 case ISD::SADDSAT:
7379 case ISD::UADDSAT:
7380 return getAllOnesConstant(DL, VT);
7381 }
7382 }
7383
7384 // Memoize this node if possible.
7385 SDNode *N;
7386 SDVTList VTs = getVTList(VT);
7387 SDValue Ops[] = {N1, N2};
7388 if (VT != MVT::Glue) {
7390 AddNodeIDNode(ID, Opcode, VTs, Ops);
7391 void *IP = nullptr;
7392 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7393 E->intersectFlagsWith(Flags);
7394 return SDValue(E, 0);
7395 }
7396
7397 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7398 N->setFlags(Flags);
7399 createOperands(N, Ops);
7400 CSEMap.InsertNode(N, IP);
7401 } else {
7402 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7403 createOperands(N, Ops);
7404 }
7405
7406 InsertNode(N);
7407 SDValue V = SDValue(N, 0);
7408 NewSDValueDbgMsg(V, "Creating new node: ", this);
7409 return V;
7410}
7411
7412SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7413 SDValue N1, SDValue N2, SDValue N3) {
7414 SDNodeFlags Flags;
7415 if (Inserter)
7416 Flags = Inserter->getFlags();
7417 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7418}
7419
7420SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7421 SDValue N1, SDValue N2, SDValue N3,
7422 const SDNodeFlags Flags) {
7424 N2.getOpcode() != ISD::DELETED_NODE &&
7425 N3.getOpcode() != ISD::DELETED_NODE &&
7426 "Operand is DELETED_NODE!");
7427 // Perform various simplifications.
7428 switch (Opcode) {
7429 case ISD::FMA:
7430 case ISD::FMAD: {
7431 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7432 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7433 N3.getValueType() == VT && "FMA types must match!");
7434 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7435 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7436 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7437 if (N1CFP && N2CFP && N3CFP) {
7438 APFloat V1 = N1CFP->getValueAPF();
7439 const APFloat &V2 = N2CFP->getValueAPF();
7440 const APFloat &V3 = N3CFP->getValueAPF();
7441 if (Opcode == ISD::FMAD) {
7444 } else
7446 return getConstantFP(V1, DL, VT);
7447 }
7448 break;
7449 }
7450 case ISD::BUILD_VECTOR: {
7451 // Attempt to simplify BUILD_VECTOR.
7452 SDValue Ops[] = {N1, N2, N3};
7453 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7454 return V;
7455 break;
7456 }
7457 case ISD::CONCAT_VECTORS: {
7458 SDValue Ops[] = {N1, N2, N3};
7459 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7460 return V;
7461 break;
7462 }
7463 case ISD::SETCC: {
7464 assert(VT.isInteger() && "SETCC result type must be an integer!");
7465 assert(N1.getValueType() == N2.getValueType() &&
7466 "SETCC operands must have the same type!");
7467 assert(VT.isVector() == N1.getValueType().isVector() &&
7468 "SETCC type should be vector iff the operand type is vector!");
7469 assert((!VT.isVector() || VT.getVectorElementCount() ==
7471 "SETCC vector element counts must match!");
7472 // Use FoldSetCC to simplify SETCC's.
7473 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7474 return V;
7475 // Vector constant folding.
7476 SDValue Ops[] = {N1, N2, N3};
7477 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7478 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7479 return V;
7480 }
7481 break;
7482 }
7483 case ISD::SELECT:
7484 case ISD::VSELECT:
7485 if (SDValue V = simplifySelect(N1, N2, N3))
7486 return V;
7487 break;
7489 llvm_unreachable("should use getVectorShuffle constructor!");
7490 case ISD::VECTOR_SPLICE: {
7491 if (cast<ConstantSDNode>(N3)->isZero())
7492 return N1;
7493 break;
7494 }
7496 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7497 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7498 // for scalable vectors where we will generate appropriate code to
7499 // deal with out-of-bounds cases correctly.
7500 if (N3C && N1.getValueType().isFixedLengthVector() &&
7502 return getUNDEF(VT);
7503
7504 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7505 if (N3.isUndef())
7506 return getUNDEF(VT);
7507
7508 // If the inserted element is an UNDEF, just use the input vector.
7509 if (N2.isUndef())
7510 return N1;
7511
7512 break;
7513 }
7514 case ISD::INSERT_SUBVECTOR: {
7515 // Inserting undef into undef is still undef.
7516 if (N1.isUndef() && N2.isUndef())
7517 return getUNDEF(VT);
7518
7519 EVT N2VT = N2.getValueType();
7520 assert(VT == N1.getValueType() &&
7521 "Dest and insert subvector source types must match!");
7522 assert(VT.isVector() && N2VT.isVector() &&
7523 "Insert subvector VTs must be vectors!");
7525 "Insert subvector VTs must have the same element type!");
7526 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7527 "Cannot insert a scalable vector into a fixed length vector!");
7528 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7530 "Insert subvector must be from smaller vector to larger vector!");
7531 assert(isa<ConstantSDNode>(N3) &&
7532 "Insert subvector index must be constant");
7533 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7534 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7536 "Insert subvector overflow!");
7538 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7539 "Constant index for INSERT_SUBVECTOR has an invalid size");
7540
7541 // Trivial insertion.
7542 if (VT == N2VT)
7543 return N2;
7544
7545 // If this is an insert of an extracted vector into an undef vector, we
7546 // can just use the input to the extract.
7547 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7548 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7549 return N2.getOperand(0);
7550 break;
7551 }
7552 case ISD::BITCAST:
7553 // Fold bit_convert nodes from a type to themselves.
7554 if (N1.getValueType() == VT)
7555 return N1;
7556 break;
7557 case ISD::VP_TRUNCATE:
7558 case ISD::VP_SIGN_EXTEND:
7559 case ISD::VP_ZERO_EXTEND:
7560 // Don't create noop casts.
7561 if (N1.getValueType() == VT)
7562 return N1;
7563 break;
7564 case ISD::VECTOR_COMPRESS: {
7565 [[maybe_unused]] EVT VecVT = N1.getValueType();
7566 [[maybe_unused]] EVT MaskVT = N2.getValueType();
7567 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
7568 assert(VT == VecVT && "Vector and result type don't match.");
7569 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
7570 "All inputs must be vectors.");
7571 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
7573 "Vector and mask must have same number of elements.");
7574
7575 if (N1.isUndef() || N2.isUndef())
7576 return N3;
7577
7578 break;
7579 }
7580 }
7581
7582 // Memoize node if it doesn't produce a glue result.
7583 SDNode *N;
7584 SDVTList VTs = getVTList(VT);
7585 SDValue Ops[] = {N1, N2, N3};
7586 if (VT != MVT::Glue) {
7588 AddNodeIDNode(ID, Opcode, VTs, Ops);
7589 void *IP = nullptr;
7590 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7591 E->intersectFlagsWith(Flags);
7592 return SDValue(E, 0);
7593 }
7594
7595 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7596 N->setFlags(Flags);
7597 createOperands(N, Ops);
7598 CSEMap.InsertNode(N, IP);
7599 } else {
7600 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7601 createOperands(N, Ops);
7602 }
7603
7604 InsertNode(N);
7605 SDValue V = SDValue(N, 0);
7606 NewSDValueDbgMsg(V, "Creating new node: ", this);
7607 return V;
7608}
7609
7610SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7611 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7612 SDValue Ops[] = { N1, N2, N3, N4 };
7613 return getNode(Opcode, DL, VT, Ops);
7614}
7615
7616SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7617 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7618 SDValue N5) {
7619 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7620 return getNode(Opcode, DL, VT, Ops);
7621}
7622
7623/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7624/// the incoming stack arguments to be loaded from the stack.
7626 SmallVector<SDValue, 8> ArgChains;
7627
7628 // Include the original chain at the beginning of the list. When this is
7629 // used by target LowerCall hooks, this helps legalize find the
7630 // CALLSEQ_BEGIN node.
7631 ArgChains.push_back(Chain);
7632
7633 // Add a chain value for each stack argument.
7634 for (SDNode *U : getEntryNode().getNode()->uses())
7635 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7636 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7637 if (FI->getIndex() < 0)
7638 ArgChains.push_back(SDValue(L, 1));
7639
7640 // Build a tokenfactor for all the chains.
7641 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7642}
7643
7644/// getMemsetValue - Vectorized representation of the memset value
7645/// operand.
7647 const SDLoc &dl) {
7648 assert(!Value.isUndef());
7649
7650 unsigned NumBits = VT.getScalarSizeInBits();
7651 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7652 assert(C->getAPIntValue().getBitWidth() == 8);
7653 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7654 if (VT.isInteger()) {
7655 bool IsOpaque = VT.getSizeInBits() > 64 ||
7656 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7657 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7658 }
7659 return DAG.getConstantFP(APFloat(DAG.EVTToAPFloatSemantics(VT), Val), dl,
7660 VT);
7661 }
7662
7663 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7664 EVT IntVT = VT.getScalarType();
7665 if (!IntVT.isInteger())
7666 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7667
7668 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7669 if (NumBits > 8) {
7670 // Use a multiplication with 0x010101... to extend the input to the
7671 // required length.
7672 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7673 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7674 DAG.getConstant(Magic, dl, IntVT));
7675 }
7676
7677 if (VT != Value.getValueType() && !VT.isInteger())
7678 Value = DAG.getBitcast(VT.getScalarType(), Value);
7679 if (VT != Value.getValueType())
7680 Value = DAG.getSplatBuildVector(VT, dl, Value);
7681
7682 return Value;
7683}
7684
7685/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7686/// used when a memcpy is turned into a memset when the source is a constant
7687/// string ptr.
7689 const TargetLowering &TLI,
7690 const ConstantDataArraySlice &Slice) {
7691 // Handle vector with all elements zero.
7692 if (Slice.Array == nullptr) {
7693 if (VT.isInteger())
7694 return DAG.getConstant(0, dl, VT);
7695 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7696 return DAG.getConstantFP(0.0, dl, VT);
7697 if (VT.isVector()) {
7698 unsigned NumElts = VT.getVectorNumElements();
7699 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
7700 return DAG.getNode(ISD::BITCAST, dl, VT,
7701 DAG.getConstant(0, dl,
7703 EltVT, NumElts)));
7704 }
7705 llvm_unreachable("Expected type!");
7706 }
7707
7708 assert(!VT.isVector() && "Can't handle vector type here!");
7709 unsigned NumVTBits = VT.getSizeInBits();
7710 unsigned NumVTBytes = NumVTBits / 8;
7711 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
7712
7713 APInt Val(NumVTBits, 0);
7714 if (DAG.getDataLayout().isLittleEndian()) {
7715 for (unsigned i = 0; i != NumBytes; ++i)
7716 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
7717 } else {
7718 for (unsigned i = 0; i != NumBytes; ++i)
7719 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
7720 }
7721
7722 // If the "cost" of materializing the integer immediate is less than the cost
7723 // of a load, then it is cost effective to turn the load into the immediate.
7724 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
7725 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
7726 return DAG.getConstant(Val, dl, VT);
7727 return SDValue();
7728}
7729
7731 const SDLoc &DL,
7732 const SDNodeFlags Flags) {
7733 EVT VT = Base.getValueType();
7734 SDValue Index;
7735
7736 if (Offset.isScalable())
7737 Index = getVScale(DL, Base.getValueType(),
7738 APInt(Base.getValueSizeInBits().getFixedValue(),
7739 Offset.getKnownMinValue()));
7740 else
7741 Index = getConstant(Offset.getFixedValue(), DL, VT);
7742
7743 return getMemBasePlusOffset(Base, Index, DL, Flags);
7744}
7745
7747 const SDLoc &DL,
7748 const SDNodeFlags Flags) {
7749 assert(Offset.getValueType().isInteger());
7750 EVT BasePtrVT = Ptr.getValueType();
7751 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
7752}
7753
7754/// Returns true if memcpy source is constant data.
7756 uint64_t SrcDelta = 0;
7757 GlobalAddressSDNode *G = nullptr;
7758 if (Src.getOpcode() == ISD::GlobalAddress)
7759 G = cast<GlobalAddressSDNode>(Src);
7760 else if (Src.getOpcode() == ISD::ADD &&
7761 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
7762 Src.getOperand(1).getOpcode() == ISD::Constant) {
7763 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
7764 SrcDelta = Src.getConstantOperandVal(1);
7765 }
7766 if (!G)
7767 return false;
7768
7769 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
7770 SrcDelta + G->getOffset());
7771}
7772
7774 SelectionDAG &DAG) {
7775 // On Darwin, -Os means optimize for size without hurting performance, so
7776 // only really optimize for size when -Oz (MinSize) is used.
7778 return MF.getFunction().hasMinSize();
7779 return DAG.shouldOptForSize();
7780}
7781
7783 SmallVector<SDValue, 32> &OutChains, unsigned From,
7784 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
7785 SmallVector<SDValue, 16> &OutStoreChains) {
7786 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
7787 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
7788 SmallVector<SDValue, 16> GluedLoadChains;
7789 for (unsigned i = From; i < To; ++i) {
7790 OutChains.push_back(OutLoadChains[i]);
7791 GluedLoadChains.push_back(OutLoadChains[i]);
7792 }
7793
7794 // Chain for all loads.
7795 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
7796 GluedLoadChains);
7797
7798 for (unsigned i = From; i < To; ++i) {
7799 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
7800 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
7801 ST->getBasePtr(), ST->getMemoryVT(),
7802 ST->getMemOperand());
7803 OutChains.push_back(NewStore);
7804 }
7805}
7806
7808 SDValue Chain, SDValue Dst, SDValue Src,
7809 uint64_t Size, Align Alignment,
7810 bool isVol, bool AlwaysInline,
7811 MachinePointerInfo DstPtrInfo,
7812 MachinePointerInfo SrcPtrInfo,
7813 const AAMDNodes &AAInfo, AAResults *AA) {
7814 // Turn a memcpy of undef to nop.
7815 // FIXME: We need to honor volatile even is Src is undef.
7816 if (Src.isUndef())
7817 return Chain;
7818
7819 // Expand memcpy to a series of load and store ops if the size operand falls
7820 // below a certain threshold.
7821 // TODO: In the AlwaysInline case, if the size is big then generate a loop
7822 // rather than maybe a humongous number of loads and stores.
7823 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
7824 const DataLayout &DL = DAG.getDataLayout();
7825 LLVMContext &C = *DAG.getContext();
7826 std::vector<EVT> MemOps;
7827 bool DstAlignCanChange = false;
7829 MachineFrameInfo &MFI = MF.getFrameInfo();
7830 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
7831 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
7832 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
7833 DstAlignCanChange = true;
7834 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
7835 if (!SrcAlign || Alignment > *SrcAlign)
7836 SrcAlign = Alignment;
7837 assert(SrcAlign && "SrcAlign must be set");
7839 // If marked as volatile, perform a copy even when marked as constant.
7840 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
7841 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
7842 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
7843 const MemOp Op = isZeroConstant
7844 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
7845 /*IsZeroMemset*/ true, isVol)
7846 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
7847 *SrcAlign, isVol, CopyFromConstant);
7848 if (!TLI.findOptimalMemOpLowering(
7849 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
7850 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
7851 return SDValue();
7852
7853 if (DstAlignCanChange) {
7854 Type *Ty = MemOps[0].getTypeForEVT(C);
7855 Align NewAlign = DL.getABITypeAlign(Ty);
7856
7857 // Don't promote to an alignment that would require dynamic stack
7858 // realignment which may conflict with optimizations such as tail call
7859 // optimization.
7861 if (!TRI->hasStackRealignment(MF))
7862 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
7863 NewAlign = NewAlign.previous();
7864
7865 if (NewAlign > Alignment) {
7866 // Give the stack frame object a larger alignment if needed.
7867 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
7868 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
7869 Alignment = NewAlign;
7870 }
7871 }
7872
7873 // Prepare AAInfo for loads/stores after lowering this memcpy.
7874 AAMDNodes NewAAInfo = AAInfo;
7875 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
7876
7877 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
7878 bool isConstant =
7879 AA && SrcVal &&
7880 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
7881
7882 MachineMemOperand::Flags MMOFlags =
7884 SmallVector<SDValue, 16> OutLoadChains;
7885 SmallVector<SDValue, 16> OutStoreChains;
7886 SmallVector<SDValue, 32> OutChains;
7887 unsigned NumMemOps = MemOps.size();
7888 uint64_t SrcOff = 0, DstOff = 0;
7889 for (unsigned i = 0; i != NumMemOps; ++i) {
7890 EVT VT = MemOps[i];
7891 unsigned VTSize = VT.getSizeInBits() / 8;
7892 SDValue Value, Store;
7893
7894 if (VTSize > Size) {
7895 // Issuing an unaligned load / store pair that overlaps with the previous
7896 // pair. Adjust the offset accordingly.
7897 assert(i == NumMemOps-1 && i != 0);
7898 SrcOff -= VTSize - Size;
7899 DstOff -= VTSize - Size;
7900 }
7901
7902 if (CopyFromConstant &&
7903 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
7904 // It's unlikely a store of a vector immediate can be done in a single
7905 // instruction. It would require a load from a constantpool first.
7906 // We only handle zero vectors here.
7907 // FIXME: Handle other cases where store of vector immediate is done in
7908 // a single instruction.
7909 ConstantDataArraySlice SubSlice;
7910 if (SrcOff < Slice.Length) {
7911 SubSlice = Slice;
7912 SubSlice.move(SrcOff);
7913 } else {
7914 // This is an out-of-bounds access and hence UB. Pretend we read zero.
7915 SubSlice.Array = nullptr;
7916 SubSlice.Offset = 0;
7917 SubSlice.Length = VTSize;
7918 }
7919 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
7920 if (Value.getNode()) {
7921 Store = DAG.getStore(
7922 Chain, dl, Value,
7923 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7924 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
7925 OutChains.push_back(Store);
7926 }
7927 }
7928
7929 if (!Store.getNode()) {
7930 // The type might not be legal for the target. This should only happen
7931 // if the type is smaller than a legal type, as on PPC, so the right
7932 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
7933 // to Load/Store if NVT==VT.
7934 // FIXME does the case above also need this?
7935 EVT NVT = TLI.getTypeToTransformTo(C, VT);
7936 assert(NVT.bitsGE(VT));
7937
7938 bool isDereferenceable =
7939 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
7940 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
7941 if (isDereferenceable)
7943 if (isConstant)
7944 SrcMMOFlags |= MachineMemOperand::MOInvariant;
7945
7946 Value = DAG.getExtLoad(
7947 ISD::EXTLOAD, dl, NVT, Chain,
7948 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
7949 SrcPtrInfo.getWithOffset(SrcOff), VT,
7950 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
7951 OutLoadChains.push_back(Value.getValue(1));
7952
7953 Store = DAG.getTruncStore(
7954 Chain, dl, Value,
7955 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
7956 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
7957 OutStoreChains.push_back(Store);
7958 }
7959 SrcOff += VTSize;
7960 DstOff += VTSize;
7961 Size -= VTSize;
7962 }
7963
7964 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
7966 unsigned NumLdStInMemcpy = OutStoreChains.size();
7967
7968 if (NumLdStInMemcpy) {
7969 // It may be that memcpy might be converted to memset if it's memcpy
7970 // of constants. In such a case, we won't have loads and stores, but
7971 // just stores. In the absence of loads, there is nothing to gang up.
7972 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
7973 // If target does not care, just leave as it.
7974 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
7975 OutChains.push_back(OutLoadChains[i]);
7976 OutChains.push_back(OutStoreChains[i]);
7977 }
7978 } else {
7979 // Ld/St less than/equal limit set by target.
7980 if (NumLdStInMemcpy <= GluedLdStLimit) {
7981 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
7982 NumLdStInMemcpy, OutLoadChains,
7983 OutStoreChains);
7984 } else {
7985 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
7986 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
7987 unsigned GlueIter = 0;
7988
7989 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
7990 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
7991 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
7992
7993 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
7994 OutLoadChains, OutStoreChains);
7995 GlueIter += GluedLdStLimit;
7996 }
7997
7998 // Residual ld/st.
7999 if (RemainingLdStInMemcpy) {
8000 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8001 RemainingLdStInMemcpy, OutLoadChains,
8002 OutStoreChains);
8003 }
8004 }
8005 }
8006 }
8007 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8008}
8009
8011 SDValue Chain, SDValue Dst, SDValue Src,
8012 uint64_t Size, Align Alignment,
8013 bool isVol, bool AlwaysInline,
8014 MachinePointerInfo DstPtrInfo,
8015 MachinePointerInfo SrcPtrInfo,
8016 const AAMDNodes &AAInfo) {
8017 // Turn a memmove of undef to nop.
8018 // FIXME: We need to honor volatile even is Src is undef.
8019 if (Src.isUndef())
8020 return Chain;
8021
8022 // Expand memmove to a series of load and store ops if the size operand falls
8023 // below a certain threshold.
8024 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8025 const DataLayout &DL = DAG.getDataLayout();
8026 LLVMContext &C = *DAG.getContext();
8027 std::vector<EVT> MemOps;
8028 bool DstAlignCanChange = false;
8030 MachineFrameInfo &MFI = MF.getFrameInfo();
8031 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8032 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8033 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8034 DstAlignCanChange = true;
8035 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8036 if (!SrcAlign || Alignment > *SrcAlign)
8037 SrcAlign = Alignment;
8038 assert(SrcAlign && "SrcAlign must be set");
8039 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8040 if (!TLI.findOptimalMemOpLowering(
8041 MemOps, Limit,
8042 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8043 /*IsVolatile*/ true),
8044 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8045 MF.getFunction().getAttributes()))
8046 return SDValue();
8047
8048 if (DstAlignCanChange) {
8049 Type *Ty = MemOps[0].getTypeForEVT(C);
8050 Align NewAlign = DL.getABITypeAlign(Ty);
8051
8052 // Don't promote to an alignment that would require dynamic stack
8053 // realignment which may conflict with optimizations such as tail call
8054 // optimization.
8056 if (!TRI->hasStackRealignment(MF))
8057 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8058 NewAlign = NewAlign.previous();
8059
8060 if (NewAlign > Alignment) {
8061 // Give the stack frame object a larger alignment if needed.
8062 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8063 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8064 Alignment = NewAlign;
8065 }
8066 }
8067
8068 // Prepare AAInfo for loads/stores after lowering this memmove.
8069 AAMDNodes NewAAInfo = AAInfo;
8070 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8071
8072 MachineMemOperand::Flags MMOFlags =
8074 uint64_t SrcOff = 0, DstOff = 0;
8075 SmallVector<SDValue, 8> LoadValues;
8076 SmallVector<SDValue, 8> LoadChains;
8077 SmallVector<SDValue, 8> OutChains;
8078 unsigned NumMemOps = MemOps.size();
8079 for (unsigned i = 0; i < NumMemOps; i++) {
8080 EVT VT = MemOps[i];
8081 unsigned VTSize = VT.getSizeInBits() / 8;
8082 SDValue Value;
8083
8084 bool isDereferenceable =
8085 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8086 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8087 if (isDereferenceable)
8089
8090 Value = DAG.getLoad(
8091 VT, dl, Chain,
8092 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8093 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8094 LoadValues.push_back(Value);
8095 LoadChains.push_back(Value.getValue(1));
8096 SrcOff += VTSize;
8097 }
8098 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8099 OutChains.clear();
8100 for (unsigned i = 0; i < NumMemOps; i++) {
8101 EVT VT = MemOps[i];
8102 unsigned VTSize = VT.getSizeInBits() / 8;
8103 SDValue Store;
8104
8105 Store = DAG.getStore(
8106 Chain, dl, LoadValues[i],
8107 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8108 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8109 OutChains.push_back(Store);
8110 DstOff += VTSize;
8111 }
8112
8113 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8114}
8115
8116/// Lower the call to 'memset' intrinsic function into a series of store
8117/// operations.
8118///
8119/// \param DAG Selection DAG where lowered code is placed.
8120/// \param dl Link to corresponding IR location.
8121/// \param Chain Control flow dependency.
8122/// \param Dst Pointer to destination memory location.
8123/// \param Src Value of byte to write into the memory.
8124/// \param Size Number of bytes to write.
8125/// \param Alignment Alignment of the destination in bytes.
8126/// \param isVol True if destination is volatile.
8127/// \param AlwaysInline Makes sure no function call is generated.
8128/// \param DstPtrInfo IR information on the memory pointer.
8129/// \returns New head in the control flow, if lowering was successful, empty
8130/// SDValue otherwise.
8131///
8132/// The function tries to replace 'llvm.memset' intrinsic with several store
8133/// operations and value calculation code. This is usually profitable for small
8134/// memory size or when the semantic requires inlining.
8136 SDValue Chain, SDValue Dst, SDValue Src,
8137 uint64_t Size, Align Alignment, bool isVol,
8138 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8139 const AAMDNodes &AAInfo) {
8140 // Turn a memset of undef to nop.
8141 // FIXME: We need to honor volatile even is Src is undef.
8142 if (Src.isUndef())
8143 return Chain;
8144
8145 // Expand memset to a series of load/store ops if the size operand
8146 // falls below a certain threshold.
8147 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8148 std::vector<EVT> MemOps;
8149 bool DstAlignCanChange = false;
8151 MachineFrameInfo &MFI = MF.getFrameInfo();
8152 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8153 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8154 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8155 DstAlignCanChange = true;
8156 bool IsZeroVal = isNullConstant(Src);
8157 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8158
8159 if (!TLI.findOptimalMemOpLowering(
8160 MemOps, Limit,
8161 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8162 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8163 return SDValue();
8164
8165 if (DstAlignCanChange) {
8166 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8167 const DataLayout &DL = DAG.getDataLayout();
8168 Align NewAlign = DL.getABITypeAlign(Ty);
8169
8170 // Don't promote to an alignment that would require dynamic stack
8171 // realignment which may conflict with optimizations such as tail call
8172 // optimization.
8174 if (!TRI->hasStackRealignment(MF))
8175 while (NewAlign > Alignment && DL.exceedsNaturalStackAlignment(NewAlign))
8176 NewAlign = NewAlign.previous();
8177
8178 if (NewAlign > Alignment) {
8179 // Give the stack frame object a larger alignment if needed.
8180 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8181 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8182 Alignment = NewAlign;
8183 }
8184 }
8185
8186 SmallVector<SDValue, 8> OutChains;
8187 uint64_t DstOff = 0;
8188 unsigned NumMemOps = MemOps.size();
8189
8190 // Find the largest store and generate the bit pattern for it.
8191 EVT LargestVT = MemOps[0];
8192 for (unsigned i = 1; i < NumMemOps; i++)
8193 if (MemOps[i].bitsGT(LargestVT))
8194 LargestVT = MemOps[i];
8195 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8196
8197 // Prepare AAInfo for loads/stores after lowering this memset.
8198 AAMDNodes NewAAInfo = AAInfo;
8199 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8200
8201 for (unsigned i = 0; i < NumMemOps; i++) {
8202 EVT VT = MemOps[i];
8203 unsigned VTSize = VT.getSizeInBits() / 8;
8204 if (VTSize > Size) {
8205 // Issuing an unaligned load / store pair that overlaps with the previous
8206 // pair. Adjust the offset accordingly.
8207 assert(i == NumMemOps-1 && i != 0);
8208 DstOff -= VTSize - Size;
8209 }
8210
8211 // If this store is smaller than the largest store see whether we can get
8212 // the smaller value for free with a truncate or extract vector element and
8213 // then store.
8214 SDValue Value = MemSetValue;
8215 if (VT.bitsLT(LargestVT)) {
8216 unsigned Index;
8217 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8218 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8219 if (!LargestVT.isVector() && !VT.isVector() &&
8220 TLI.isTruncateFree(LargestVT, VT))
8221 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8222 else if (LargestVT.isVector() && !VT.isVector() &&
8224 LargestVT.getTypeForEVT(*DAG.getContext()),
8225 VT.getSizeInBits(), Index) &&
8226 TLI.isTypeLegal(SVT) &&
8227 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8228 // Target which can combine store(extractelement VectorTy, Idx) can get
8229 // the smaller value for free.
8230 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8231 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8232 DAG.getVectorIdxConstant(Index, dl));
8233 } else
8234 Value = getMemsetValue(Src, VT, DAG, dl);
8235 }
8236 assert(Value.getValueType() == VT && "Value with wrong type.");
8237 SDValue Store = DAG.getStore(
8238 Chain, dl, Value,
8239 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8240 DstPtrInfo.getWithOffset(DstOff), Alignment,
8242 NewAAInfo);
8243 OutChains.push_back(Store);
8244 DstOff += VT.getSizeInBits() / 8;
8245 Size -= VTSize;
8246 }
8247
8248 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8249}
8250
8252 unsigned AS) {
8253 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8254 // pointer operands can be losslessly bitcasted to pointers of address space 0
8255 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8256 report_fatal_error("cannot lower memory intrinsic in address space " +
8257 Twine(AS));
8258 }
8259}
8260
8262 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
8263 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
8264 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
8265 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, AAResults *AA) {
8266 // Check to see if we should lower the memcpy to loads and stores first.
8267 // For cases within the target-specified limits, this is the best choice.
8268 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8269 if (ConstantSize) {
8270 // Memcpy with size zero? Just return the original chain.
8271 if (ConstantSize->isZero())
8272 return Chain;
8273
8275 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8276 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8277 if (Result.getNode())
8278 return Result;
8279 }
8280
8281 // Then check to see if we should lower the memcpy with target-specific
8282 // code. If the target chooses to do this, this is the next best.
8283 if (TSI) {
8284 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8285 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8286 DstPtrInfo, SrcPtrInfo);
8287 if (Result.getNode())
8288 return Result;
8289 }
8290
8291 // If we really need inline code and the target declined to provide it,
8292 // use a (potentially long) sequence of loads and stores.
8293 if (AlwaysInline) {
8294 assert(ConstantSize && "AlwaysInline requires a constant size!");
8296 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8297 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8298 }
8299
8302
8303 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8304 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8305 // respect volatile, so they may do things like read or write memory
8306 // beyond the given memory regions. But fixing this isn't easy, and most
8307 // people don't care.
8308
8309 // Emit a library call.
8312 Entry.Ty = PointerType::getUnqual(*getContext());
8313 Entry.Node = Dst; Args.push_back(Entry);
8314 Entry.Node = Src; Args.push_back(Entry);
8315
8316 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8317 Entry.Node = Size; Args.push_back(Entry);
8318 // FIXME: pass in SDLoc
8320 bool IsTailCall = false;
8321 if (OverrideTailCall.has_value()) {
8322 IsTailCall = *OverrideTailCall;
8323 } else {
8324 bool LowersToMemcpy =
8325 TLI->getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy");
8326 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8327 IsTailCall = CI && CI->isTailCall() &&
8329 ReturnsFirstArg && LowersToMemcpy);
8330 }
8331
8332 CLI.setDebugLoc(dl)
8333 .setChain(Chain)
8334 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8335 Dst.getValueType().getTypeForEVT(*getContext()),
8336 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8337 TLI->getPointerTy(getDataLayout())),
8338 std::move(Args))
8340 .setTailCall(IsTailCall);
8341
8342 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8343 return CallResult.second;
8344}
8345
8347 SDValue Dst, SDValue Src, SDValue Size,
8348 Type *SizeTy, unsigned ElemSz,
8349 bool isTailCall,
8350 MachinePointerInfo DstPtrInfo,
8351 MachinePointerInfo SrcPtrInfo) {
8352 // Emit a library call.
8355 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8356 Entry.Node = Dst;
8357 Args.push_back(Entry);
8358
8359 Entry.Node = Src;
8360 Args.push_back(Entry);
8361
8362 Entry.Ty = SizeTy;
8363 Entry.Node = Size;
8364 Args.push_back(Entry);
8365
8366 RTLIB::Libcall LibraryCall =
8368 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8369 report_fatal_error("Unsupported element size");
8370
8372 CLI.setDebugLoc(dl)
8373 .setChain(Chain)
8374 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8376 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8377 TLI->getPointerTy(getDataLayout())),
8378 std::move(Args))
8380 .setTailCall(isTailCall);
8381
8382 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8383 return CallResult.second;
8384}
8385
8387 SDValue Src, SDValue Size, Align Alignment,
8388 bool isVol, const CallInst *CI,
8389 std::optional<bool> OverrideTailCall,
8390 MachinePointerInfo DstPtrInfo,
8391 MachinePointerInfo SrcPtrInfo,
8392 const AAMDNodes &AAInfo, AAResults *AA) {
8393 // Check to see if we should lower the memmove to loads and stores first.
8394 // For cases within the target-specified limits, this is the best choice.
8395 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8396 if (ConstantSize) {
8397 // Memmove with size zero? Just return the original chain.
8398 if (ConstantSize->isZero())
8399 return Chain;
8400
8402 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8403 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8404 if (Result.getNode())
8405 return Result;
8406 }
8407
8408 // Then check to see if we should lower the memmove with target-specific
8409 // code. If the target chooses to do this, this is the next best.
8410 if (TSI) {
8411 SDValue Result =
8412 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8413 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8414 if (Result.getNode())
8415 return Result;
8416 }
8417
8420
8421 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8422 // not be safe. See memcpy above for more details.
8423
8424 // Emit a library call.
8427 Entry.Ty = PointerType::getUnqual(*getContext());
8428 Entry.Node = Dst; Args.push_back(Entry);
8429 Entry.Node = Src; Args.push_back(Entry);
8430
8431 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8432 Entry.Node = Size; Args.push_back(Entry);
8433 // FIXME: pass in SDLoc
8435
8436 bool IsTailCall = false;
8437 if (OverrideTailCall.has_value()) {
8438 IsTailCall = *OverrideTailCall;
8439 } else {
8440 bool LowersToMemmove =
8441 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
8442 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8443 IsTailCall = CI && CI->isTailCall() &&
8445 ReturnsFirstArg && LowersToMemmove);
8446 }
8447
8448 CLI.setDebugLoc(dl)
8449 .setChain(Chain)
8450 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8451 Dst.getValueType().getTypeForEVT(*getContext()),
8452 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8453 TLI->getPointerTy(getDataLayout())),
8454 std::move(Args))
8456 .setTailCall(IsTailCall);
8457
8458 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8459 return CallResult.second;
8460}
8461
8463 SDValue Dst, SDValue Src, SDValue Size,
8464 Type *SizeTy, unsigned ElemSz,
8465 bool isTailCall,
8466 MachinePointerInfo DstPtrInfo,
8467 MachinePointerInfo SrcPtrInfo) {
8468 // Emit a library call.
8471 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8472 Entry.Node = Dst;
8473 Args.push_back(Entry);
8474
8475 Entry.Node = Src;
8476 Args.push_back(Entry);
8477
8478 Entry.Ty = SizeTy;
8479 Entry.Node = Size;
8480 Args.push_back(Entry);
8481
8482 RTLIB::Libcall LibraryCall =
8484 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8485 report_fatal_error("Unsupported element size");
8486
8488 CLI.setDebugLoc(dl)
8489 .setChain(Chain)
8490 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8492 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8493 TLI->getPointerTy(getDataLayout())),
8494 std::move(Args))
8496 .setTailCall(isTailCall);
8497
8498 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8499 return CallResult.second;
8500}
8501
8503 SDValue Src, SDValue Size, Align Alignment,
8504 bool isVol, bool AlwaysInline,
8505 const CallInst *CI,
8506 MachinePointerInfo DstPtrInfo,
8507 const AAMDNodes &AAInfo) {
8508 // Check to see if we should lower the memset to stores first.
8509 // For cases within the target-specified limits, this is the best choice.
8510 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8511 if (ConstantSize) {
8512 // Memset with size zero? Just return the original chain.
8513 if (ConstantSize->isZero())
8514 return Chain;
8515
8516 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8517 ConstantSize->getZExtValue(), Alignment,
8518 isVol, false, DstPtrInfo, AAInfo);
8519
8520 if (Result.getNode())
8521 return Result;
8522 }
8523
8524 // Then check to see if we should lower the memset with target-specific
8525 // code. If the target chooses to do this, this is the next best.
8526 if (TSI) {
8527 SDValue Result = TSI->EmitTargetCodeForMemset(
8528 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8529 if (Result.getNode())
8530 return Result;
8531 }
8532
8533 // If we really need inline code and the target declined to provide it,
8534 // use a (potentially long) sequence of loads and stores.
8535 if (AlwaysInline) {
8536 assert(ConstantSize && "AlwaysInline requires a constant size!");
8537 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8538 ConstantSize->getZExtValue(), Alignment,
8539 isVol, true, DstPtrInfo, AAInfo);
8540 assert(Result &&
8541 "getMemsetStores must return a valid sequence when AlwaysInline");
8542 return Result;
8543 }
8544
8546
8547 // Emit a library call.
8548 auto &Ctx = *getContext();
8549 const auto& DL = getDataLayout();
8550
8552 // FIXME: pass in SDLoc
8553 CLI.setDebugLoc(dl).setChain(Chain);
8554
8555 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8556
8557 // Helper function to create an Entry from Node and Type.
8558 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8560 Entry.Node = Node;
8561 Entry.Ty = Ty;
8562 return Entry;
8563 };
8564
8565 bool UseBZero = isNullConstant(Src) && BzeroName;
8566 // If zeroing out and bzero is present, use it.
8567 if (UseBZero) {
8569 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8570 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8571 CLI.setLibCallee(
8572 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8573 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8574 } else {
8576 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8577 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8578 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8579 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8580 Dst.getValueType().getTypeForEVT(Ctx),
8581 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8582 TLI->getPointerTy(DL)),
8583 std::move(Args));
8584 }
8585 bool LowersToMemset =
8586 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
8587 // If we're going to use bzero, make sure not to tail call unless the
8588 // subsequent return doesn't need a value, as bzero doesn't return the first
8589 // arg unlike memset.
8590 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
8591 bool IsTailCall =
8592 CI && CI->isTailCall() &&
8593 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
8594 CLI.setDiscardResult().setTailCall(IsTailCall);
8595
8596 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8597 return CallResult.second;
8598}
8599
8602 Type *SizeTy, unsigned ElemSz,
8603 bool isTailCall,
8604 MachinePointerInfo DstPtrInfo) {
8605 // Emit a library call.
8608 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8609 Entry.Node = Dst;
8610 Args.push_back(Entry);
8611
8612 Entry.Ty = Type::getInt8Ty(*getContext());
8613 Entry.Node = Value;
8614 Args.push_back(Entry);
8615
8616 Entry.Ty = SizeTy;
8617 Entry.Node = Size;
8618 Args.push_back(Entry);
8619
8620 RTLIB::Libcall LibraryCall =
8622 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8623 report_fatal_error("Unsupported element size");
8624
8626 CLI.setDebugLoc(dl)
8627 .setChain(Chain)
8628 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8630 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8631 TLI->getPointerTy(getDataLayout())),
8632 std::move(Args))
8634 .setTailCall(isTailCall);
8635
8636 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8637 return CallResult.second;
8638}
8639
8640SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8641 SDVTList VTList, ArrayRef<SDValue> Ops,
8642 MachineMemOperand *MMO) {
8644 ID.AddInteger(MemVT.getRawBits());
8645 AddNodeIDNode(ID, Opcode, VTList, Ops);
8646 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8647 ID.AddInteger(MMO->getFlags());
8648 void* IP = nullptr;
8649 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8650 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8651 return SDValue(E, 0);
8652 }
8653
8654 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8655 VTList, MemVT, MMO);
8656 createOperands(N, Ops);
8657
8658 CSEMap.InsertNode(N, IP);
8659 InsertNode(N);
8660 return SDValue(N, 0);
8661}
8662
8664 EVT MemVT, SDVTList VTs, SDValue Chain,
8665 SDValue Ptr, SDValue Cmp, SDValue Swp,
8666 MachineMemOperand *MMO) {
8667 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8669 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8670
8671 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8672 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8673}
8674
8675SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8676 SDValue Chain, SDValue Ptr, SDValue Val,
8677 MachineMemOperand *MMO) {
8678 assert((Opcode == ISD::ATOMIC_LOAD_ADD ||
8679 Opcode == ISD::ATOMIC_LOAD_SUB ||
8680 Opcode == ISD::ATOMIC_LOAD_AND ||
8681 Opcode == ISD::ATOMIC_LOAD_CLR ||
8682 Opcode == ISD::ATOMIC_LOAD_OR ||
8683 Opcode == ISD::ATOMIC_LOAD_XOR ||
8684 Opcode == ISD::ATOMIC_LOAD_NAND ||
8685 Opcode == ISD::ATOMIC_LOAD_MIN ||
8686 Opcode == ISD::ATOMIC_LOAD_MAX ||
8687 Opcode == ISD::ATOMIC_LOAD_UMIN ||
8688 Opcode == ISD::ATOMIC_LOAD_UMAX ||
8689 Opcode == ISD::ATOMIC_LOAD_FADD ||
8690 Opcode == ISD::ATOMIC_LOAD_FSUB ||
8691 Opcode == ISD::ATOMIC_LOAD_FMAX ||
8692 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8693 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8694 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8695 Opcode == ISD::ATOMIC_SWAP ||
8696 Opcode == ISD::ATOMIC_STORE) &&
8697 "Invalid Atomic Op");
8698
8699 EVT VT = Val.getValueType();
8700
8701 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8702 getVTList(VT, MVT::Other);
8703 SDValue Ops[] = {Chain, Ptr, Val};
8704 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8705}
8706
8707SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8708 EVT VT, SDValue Chain, SDValue Ptr,
8709 MachineMemOperand *MMO) {
8710 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
8711
8712 SDVTList VTs = getVTList(VT, MVT::Other);
8713 SDValue Ops[] = {Chain, Ptr};
8714 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8715}
8716
8717/// getMergeValues - Create a MERGE_VALUES node from the given operands.
8719 if (Ops.size() == 1)
8720 return Ops[0];
8721
8723 VTs.reserve(Ops.size());
8724 for (const SDValue &Op : Ops)
8725 VTs.push_back(Op.getValueType());
8726 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
8727}
8728
8730 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
8731 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
8733 const AAMDNodes &AAInfo) {
8734 if (Size.hasValue() && !Size.getValue())
8736
8738 MachineMemOperand *MMO =
8739 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
8740
8741 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
8742}
8743
8745 SDVTList VTList,
8746 ArrayRef<SDValue> Ops, EVT MemVT,
8747 MachineMemOperand *MMO) {
8748 assert((Opcode == ISD::INTRINSIC_VOID ||
8749 Opcode == ISD::INTRINSIC_W_CHAIN ||
8750 Opcode == ISD::PREFETCH ||
8751 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
8752 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
8753 "Opcode is not a memory-accessing opcode!");
8754
8755 // Memoize the node unless it returns a glue result.
8757 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
8759 AddNodeIDNode(ID, Opcode, VTList, Ops);
8760 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
8761 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
8762 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8763 ID.AddInteger(MMO->getFlags());
8764 ID.AddInteger(MemVT.getRawBits());
8765 void *IP = nullptr;
8766 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8767 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
8768 return SDValue(E, 0);
8769 }
8770
8771 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8772 VTList, MemVT, MMO);
8773 createOperands(N, Ops);
8774
8775 CSEMap.InsertNode(N, IP);
8776 } else {
8777 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8778 VTList, MemVT, MMO);
8779 createOperands(N, Ops);
8780 }
8781 InsertNode(N);
8782 SDValue V(N, 0);
8783 NewSDValueDbgMsg(V, "Creating new node: ", this);
8784 return V;
8785}
8786
8788 SDValue Chain, int FrameIndex,
8789 int64_t Size, int64_t Offset) {
8790 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
8791 const auto VTs = getVTList(MVT::Other);
8792 SDValue Ops[2] = {
8793 Chain,
8794 getFrameIndex(FrameIndex,
8795 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
8796 true)};
8797
8799 AddNodeIDNode(ID, Opcode, VTs, Ops);
8800 ID.AddInteger(FrameIndex);
8801 ID.AddInteger(Size);
8802 ID.AddInteger(Offset);
8803 void *IP = nullptr;
8804 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
8805 return SDValue(E, 0);
8806
8807 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
8808 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
8809 createOperands(N, Ops);
8810 CSEMap.InsertNode(N, IP);
8811 InsertNode(N);
8812 SDValue V(N, 0);
8813 NewSDValueDbgMsg(V, "Creating new node: ", this);
8814 return V;
8815}
8816
8819 uint32_t Attr) {
8820 const unsigned Opcode = ISD::PSEUDO_PROBE;
8821 const auto VTs = getVTList(MVT::Other);
8822 SDValue Ops[] = {Chain};
8824 AddNodeIDNode(ID, Opcode, VTs, Ops);
8825 ID.AddInteger(Guid);
8826 ID.AddInteger(Index);
8827 void *IP = nullptr;
8828 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
8829 return SDValue(E, 0);
8830
8831 auto *N = newSDNode<PseudoProbeSDNode>(
8832 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
8833 createOperands(N, Ops);
8834 CSEMap.InsertNode(N, IP);
8835 InsertNode(N);
8836 SDValue V(N, 0);
8837 NewSDValueDbgMsg(V, "Creating new node: ", this);
8838 return V;
8839}
8840
8841/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8842/// MachinePointerInfo record from it. This is particularly useful because the
8843/// code generator has many cases where it doesn't bother passing in a
8844/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8846 SelectionDAG &DAG, SDValue Ptr,
8847 int64_t Offset = 0) {
8848 // If this is FI+Offset, we can model it.
8849 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
8851 FI->getIndex(), Offset);
8852
8853 // If this is (FI+Offset1)+Offset2, we can model it.
8854 if (Ptr.getOpcode() != ISD::ADD ||
8855 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
8856 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
8857 return Info;
8858
8859 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
8861 DAG.getMachineFunction(), FI,
8862 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
8863}
8864
8865/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
8866/// MachinePointerInfo record from it. This is particularly useful because the
8867/// code generator has many cases where it doesn't bother passing in a
8868/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
8870 SelectionDAG &DAG, SDValue Ptr,
8871 SDValue OffsetOp) {
8872 // If the 'Offset' value isn't a constant, we can't handle this.
8873 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
8874 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
8875 if (OffsetOp.isUndef())
8876 return InferPointerInfo(Info, DAG, Ptr);
8877 return Info;
8878}
8879
8881 EVT VT, const SDLoc &dl, SDValue Chain,
8883 MachinePointerInfo PtrInfo, EVT MemVT,
8884 Align Alignment,
8885 MachineMemOperand::Flags MMOFlags,
8886 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8887 assert(Chain.getValueType() == MVT::Other &&
8888 "Invalid chain type");
8889
8890 MMOFlags |= MachineMemOperand::MOLoad;
8891 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
8892 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
8893 // clients.
8894 if (PtrInfo.V.isNull())
8895 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
8896
8899 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
8900 Alignment, AAInfo, Ranges);
8901 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
8902}
8903
8905 EVT VT, const SDLoc &dl, SDValue Chain,
8906 SDValue Ptr, SDValue Offset, EVT MemVT,
8907 MachineMemOperand *MMO) {
8908 if (VT == MemVT) {
8909 ExtType = ISD::NON_EXTLOAD;
8910 } else if (ExtType == ISD::NON_EXTLOAD) {
8911 assert(VT == MemVT && "Non-extending load from different memory type!");
8912 } else {
8913 // Extending load.
8914 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
8915 "Should only be an extending load, not truncating!");
8916 assert(VT.isInteger() == MemVT.isInteger() &&
8917 "Cannot convert from FP to Int or Int -> FP!");
8918 assert(VT.isVector() == MemVT.isVector() &&
8919 "Cannot use an ext load to convert to or from a vector!");
8920 assert((!VT.isVector() ||
8922 "Cannot use an ext load to change the number of vector elements!");
8923 }
8924
8925 bool Indexed = AM != ISD::UNINDEXED;
8926 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
8927
8928 SDVTList VTs = Indexed ?
8929 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
8930 SDValue Ops[] = { Chain, Ptr, Offset };
8932 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
8933 ID.AddInteger(MemVT.getRawBits());
8934 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
8935 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
8936 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8937 ID.AddInteger(MMO->getFlags());
8938 void *IP = nullptr;
8939 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8940 cast<LoadSDNode>(E)->refineAlignment(MMO);
8941 return SDValue(E, 0);
8942 }
8943 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
8944 ExtType, MemVT, MMO);
8945 createOperands(N, Ops);
8946
8947 CSEMap.InsertNode(N, IP);
8948 InsertNode(N);
8949 SDValue V(N, 0);
8950 NewSDValueDbgMsg(V, "Creating new node: ", this);
8951 return V;
8952}
8953
8956 MaybeAlign Alignment,
8957 MachineMemOperand::Flags MMOFlags,
8958 const AAMDNodes &AAInfo, const MDNode *Ranges) {
8959 SDValue Undef = getUNDEF(Ptr.getValueType());
8960 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8961 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
8962}
8963
8966 SDValue Undef = getUNDEF(Ptr.getValueType());
8967 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
8968 VT, MMO);
8969}
8970
8972 EVT VT, SDValue Chain, SDValue Ptr,
8973 MachinePointerInfo PtrInfo, EVT MemVT,
8974 MaybeAlign Alignment,
8975 MachineMemOperand::Flags MMOFlags,
8976 const AAMDNodes &AAInfo) {
8977 SDValue Undef = getUNDEF(Ptr.getValueType());
8978 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
8979 MemVT, Alignment, MMOFlags, AAInfo);
8980}
8981
8983 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
8984 MachineMemOperand *MMO) {
8985 SDValue Undef = getUNDEF(Ptr.getValueType());
8986 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
8987 MemVT, MMO);
8988}
8989
8993 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
8994 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
8995 // Don't propagate the invariant or dereferenceable flags.
8996 auto MMOFlags =
8997 LD->getMemOperand()->getFlags() &
8999 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9000 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9001 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9002}
9003
9006 Align Alignment,
9007 MachineMemOperand::Flags MMOFlags,
9008 const AAMDNodes &AAInfo) {
9009 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9010
9011 MMOFlags |= MachineMemOperand::MOStore;
9012 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9013
9014 if (PtrInfo.V.isNull())
9015 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9016
9019 MachineMemOperand *MMO =
9020 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9021 return getStore(Chain, dl, Val, Ptr, MMO);
9022}
9023
9026 assert(Chain.getValueType() == MVT::Other &&
9027 "Invalid chain type");
9028 EVT VT = Val.getValueType();
9029 SDVTList VTs = getVTList(MVT::Other);
9030 SDValue Undef = getUNDEF(Ptr.getValueType());
9031 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9033 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9034 ID.AddInteger(VT.getRawBits());
9035 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9036 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
9037 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9038 ID.AddInteger(MMO->getFlags());
9039 void *IP = nullptr;
9040 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9041 cast<StoreSDNode>(E)->refineAlignment(MMO);
9042 return SDValue(E, 0);
9043 }
9044 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9045 ISD::UNINDEXED, false, VT, MMO);
9046 createOperands(N, Ops);
9047
9048 CSEMap.InsertNode(N, IP);
9049 InsertNode(N);
9050 SDValue V(N, 0);
9051 NewSDValueDbgMsg(V, "Creating new node: ", this);
9052 return V;
9053}
9054
9057 EVT SVT, Align Alignment,
9058 MachineMemOperand::Flags MMOFlags,
9059 const AAMDNodes &AAInfo) {
9060 assert(Chain.getValueType() == MVT::Other &&
9061 "Invalid chain type");
9062
9063 MMOFlags |= MachineMemOperand::MOStore;
9064 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9065
9066 if (PtrInfo.V.isNull())
9067 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9068
9071 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9072 AAInfo);
9073 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9074}
9075
9077 SDValue Ptr, EVT SVT,
9078 MachineMemOperand *MMO) {
9079 EVT VT = Val.getValueType();
9080
9081 assert(Chain.getValueType() == MVT::Other &&
9082 "Invalid chain type");
9083 if (VT == SVT)
9084 return getStore(Chain, dl, Val, Ptr, MMO);
9085
9087 "Should only be a truncating store, not extending!");
9088 assert(VT.isInteger() == SVT.isInteger() &&
9089 "Can't do FP-INT conversion!");
9090 assert(VT.isVector() == SVT.isVector() &&
9091 "Cannot use trunc store to convert to or from a vector!");
9092 assert((!VT.isVector() ||
9094 "Cannot use trunc store to change the number of vector elements!");
9095
9096 SDVTList VTs = getVTList(MVT::Other);
9097 SDValue Undef = getUNDEF(Ptr.getValueType());
9098 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9100 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9101 ID.AddInteger(SVT.getRawBits());
9102 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9103 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
9104 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9105 ID.AddInteger(MMO->getFlags());
9106 void *IP = nullptr;
9107 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9108 cast<StoreSDNode>(E)->refineAlignment(MMO);
9109 return SDValue(E, 0);
9110 }
9111 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9112 ISD::UNINDEXED, true, SVT, MMO);
9113 createOperands(N, Ops);
9114
9115 CSEMap.InsertNode(N, IP);
9116 InsertNode(N);
9117 SDValue V(N, 0);
9118 NewSDValueDbgMsg(V, "Creating new node: ", this);
9119 return V;
9120}
9121
9125 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9126 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9127 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9128 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
9130 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9131 ID.AddInteger(ST->getMemoryVT().getRawBits());
9132 ID.AddInteger(ST->getRawSubclassData());
9133 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9134 ID.AddInteger(ST->getMemOperand()->getFlags());
9135 void *IP = nullptr;
9136 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9137 return SDValue(E, 0);
9138
9139 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9140 ST->isTruncatingStore(), ST->getMemoryVT(),
9141 ST->getMemOperand());
9142 createOperands(N, Ops);
9143
9144 CSEMap.InsertNode(N, IP);
9145 InsertNode(N);
9146 SDValue V(N, 0);
9147 NewSDValueDbgMsg(V, "Creating new node: ", this);
9148 return V;
9149}
9150
9152 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9153 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9154 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9155 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9156 const MDNode *Ranges, bool IsExpanding) {
9157 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9158
9159 MMOFlags |= MachineMemOperand::MOLoad;
9160 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9161 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9162 // clients.
9163 if (PtrInfo.V.isNull())
9164 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9165
9168 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9169 Alignment, AAInfo, Ranges);
9170 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9171 MMO, IsExpanding);
9172}
9173
9175 ISD::LoadExtType ExtType, EVT VT,
9176 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9177 SDValue Offset, SDValue Mask, SDValue EVL,
9178 EVT MemVT, MachineMemOperand *MMO,
9179 bool IsExpanding) {
9180 bool Indexed = AM != ISD::UNINDEXED;
9181 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9182
9183 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9184 : getVTList(VT, MVT::Other);
9185 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9187 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9188 ID.AddInteger(MemVT.getRawBits());
9189 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9190 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9191 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9192 ID.AddInteger(MMO->getFlags());
9193 void *IP = nullptr;
9194 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9195 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
9196 return SDValue(E, 0);
9197 }
9198 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9199 ExtType, IsExpanding, MemVT, MMO);
9200 createOperands(N, Ops);
9201
9202 CSEMap.InsertNode(N, IP);
9203 InsertNode(N);
9204 SDValue V(N, 0);
9205 NewSDValueDbgMsg(V, "Creating new node: ", this);
9206 return V;
9207}
9208
9210 SDValue Ptr, SDValue Mask, SDValue EVL,
9211 MachinePointerInfo PtrInfo,
9212 MaybeAlign Alignment,
9213 MachineMemOperand::Flags MMOFlags,
9214 const AAMDNodes &AAInfo, const MDNode *Ranges,
9215 bool IsExpanding) {
9216 SDValue Undef = getUNDEF(Ptr.getValueType());
9217 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9218 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9219 IsExpanding);
9220}
9221
9223 SDValue Ptr, SDValue Mask, SDValue EVL,
9224 MachineMemOperand *MMO, bool IsExpanding) {
9225 SDValue Undef = getUNDEF(Ptr.getValueType());
9226 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9227 Mask, EVL, VT, MMO, IsExpanding);
9228}
9229
9231 EVT VT, SDValue Chain, SDValue Ptr,
9232 SDValue Mask, SDValue EVL,
9233 MachinePointerInfo PtrInfo, EVT MemVT,
9234 MaybeAlign Alignment,
9235 MachineMemOperand::Flags MMOFlags,
9236 const AAMDNodes &AAInfo, bool IsExpanding) {
9237 SDValue Undef = getUNDEF(Ptr.getValueType());
9238 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9239 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9240 IsExpanding);
9241}
9242
9244 EVT VT, SDValue Chain, SDValue Ptr,
9245 SDValue Mask, SDValue EVL, EVT MemVT,
9246 MachineMemOperand *MMO, bool IsExpanding) {
9247 SDValue Undef = getUNDEF(Ptr.getValueType());
9248 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9249 EVL, MemVT, MMO, IsExpanding);
9250}
9251
9255 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9256 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9257 // Don't propagate the invariant or dereferenceable flags.
9258 auto MMOFlags =
9259 LD->getMemOperand()->getFlags() &
9261 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9262 LD->getChain(), Base, Offset, LD->getMask(),
9263 LD->getVectorLength(), LD->getPointerInfo(),
9264 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9265 nullptr, LD->isExpandingLoad());
9266}
9267
9270 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9271 ISD::MemIndexedMode AM, bool IsTruncating,
9272 bool IsCompressing) {
9273 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9274 bool Indexed = AM != ISD::UNINDEXED;
9275 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9276 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9277 : getVTList(MVT::Other);
9278 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9280 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9281 ID.AddInteger(MemVT.getRawBits());
9282 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9283 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9284 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9285 ID.AddInteger(MMO->getFlags());
9286 void *IP = nullptr;
9287 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9288 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9289 return SDValue(E, 0);
9290 }
9291 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9292 IsTruncating, IsCompressing, MemVT, MMO);
9293 createOperands(N, Ops);
9294
9295 CSEMap.InsertNode(N, IP);
9296 InsertNode(N);
9297 SDValue V(N, 0);
9298 NewSDValueDbgMsg(V, "Creating new node: ", this);
9299 return V;
9300}
9301
9303 SDValue Val, SDValue Ptr, SDValue Mask,
9304 SDValue EVL, MachinePointerInfo PtrInfo,
9305 EVT SVT, Align Alignment,
9306 MachineMemOperand::Flags MMOFlags,
9307 const AAMDNodes &AAInfo,
9308 bool IsCompressing) {
9309 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9310
9311 MMOFlags |= MachineMemOperand::MOStore;
9312 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9313
9314 if (PtrInfo.V.isNull())
9315 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9316
9319 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9320 AAInfo);
9321 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9322 IsCompressing);
9323}
9324
9326 SDValue Val, SDValue Ptr, SDValue Mask,
9327 SDValue EVL, EVT SVT,
9328 MachineMemOperand *MMO,
9329 bool IsCompressing) {
9330 EVT VT = Val.getValueType();
9331
9332 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9333 if (VT == SVT)
9334 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9335 EVL, VT, MMO, ISD::UNINDEXED,
9336 /*IsTruncating*/ false, IsCompressing);
9337
9339 "Should only be a truncating store, not extending!");
9340 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9341 assert(VT.isVector() == SVT.isVector() &&
9342 "Cannot use trunc store to convert to or from a vector!");
9343 assert((!VT.isVector() ||
9345 "Cannot use trunc store to change the number of vector elements!");
9346
9347 SDVTList VTs = getVTList(MVT::Other);
9348 SDValue Undef = getUNDEF(Ptr.getValueType());
9349 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9351 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9352 ID.AddInteger(SVT.getRawBits());
9353 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9354 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9355 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9356 ID.AddInteger(MMO->getFlags());
9357 void *IP = nullptr;
9358 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9359 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9360 return SDValue(E, 0);
9361 }
9362 auto *N =
9363 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9364 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9365 createOperands(N, Ops);
9366
9367 CSEMap.InsertNode(N, IP);
9368 InsertNode(N);
9369 SDValue V(N, 0);
9370 NewSDValueDbgMsg(V, "Creating new node: ", this);
9371 return V;
9372}
9373
9377 auto *ST = cast<VPStoreSDNode>(OrigStore);
9378 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9379 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9380 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9381 Offset, ST->getMask(), ST->getVectorLength()};
9383 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9384 ID.AddInteger(ST->getMemoryVT().getRawBits());
9385 ID.AddInteger(ST->getRawSubclassData());
9386 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9387 ID.AddInteger(ST->getMemOperand()->getFlags());
9388 void *IP = nullptr;
9389 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9390 return SDValue(E, 0);
9391
9392 auto *N = newSDNode<VPStoreSDNode>(
9393 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9394 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9395 createOperands(N, Ops);
9396
9397 CSEMap.InsertNode(N, IP);
9398 InsertNode(N);
9399 SDValue V(N, 0);
9400 NewSDValueDbgMsg(V, "Creating new node: ", this);
9401 return V;
9402}
9403
9405 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9406 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9407 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9408 bool Indexed = AM != ISD::UNINDEXED;
9409 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9410
9411 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9412 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9413 : getVTList(VT, MVT::Other);
9415 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9416 ID.AddInteger(VT.getRawBits());
9417 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9418 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9419 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9420
9421 void *IP = nullptr;
9422 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9423 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9424 return SDValue(E, 0);
9425 }
9426
9427 auto *N =
9428 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9429 ExtType, IsExpanding, MemVT, MMO);
9430 createOperands(N, Ops);
9431 CSEMap.InsertNode(N, IP);
9432 InsertNode(N);
9433 SDValue V(N, 0);
9434 NewSDValueDbgMsg(V, "Creating new node: ", this);
9435 return V;
9436}
9437
9439 SDValue Ptr, SDValue Stride,
9440 SDValue Mask, SDValue EVL,
9441 MachineMemOperand *MMO,
9442 bool IsExpanding) {
9443 SDValue Undef = getUNDEF(Ptr.getValueType());
9445 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9446}
9447
9449 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9450 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9451 MachineMemOperand *MMO, bool IsExpanding) {
9452 SDValue Undef = getUNDEF(Ptr.getValueType());
9453 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9454 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9455}
9456
9458 SDValue Val, SDValue Ptr,
9459 SDValue Offset, SDValue Stride,
9460 SDValue Mask, SDValue EVL, EVT MemVT,
9461 MachineMemOperand *MMO,
9463 bool IsTruncating, bool IsCompressing) {
9464 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9465 bool Indexed = AM != ISD::UNINDEXED;
9466 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9467 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9468 : getVTList(MVT::Other);
9469 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9471 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9472 ID.AddInteger(MemVT.getRawBits());
9473 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9474 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9475 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9476 void *IP = nullptr;
9477 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9478 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9479 return SDValue(E, 0);
9480 }
9481 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9482 VTs, AM, IsTruncating,
9483 IsCompressing, MemVT, MMO);
9484 createOperands(N, Ops);
9485
9486 CSEMap.InsertNode(N, IP);
9487 InsertNode(N);
9488 SDValue V(N, 0);
9489 NewSDValueDbgMsg(V, "Creating new node: ", this);
9490 return V;
9491}
9492
9494 SDValue Val, SDValue Ptr,
9495 SDValue Stride, SDValue Mask,
9496 SDValue EVL, EVT SVT,
9497 MachineMemOperand *MMO,
9498 bool IsCompressing) {
9499 EVT VT = Val.getValueType();
9500
9501 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9502 if (VT == SVT)
9503 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9504 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9505 /*IsTruncating*/ false, IsCompressing);
9506
9508 "Should only be a truncating store, not extending!");
9509 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9510 assert(VT.isVector() == SVT.isVector() &&
9511 "Cannot use trunc store to convert to or from a vector!");
9512 assert((!VT.isVector() ||
9514 "Cannot use trunc store to change the number of vector elements!");
9515
9516 SDVTList VTs = getVTList(MVT::Other);
9517 SDValue Undef = getUNDEF(Ptr.getValueType());
9518 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9520 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9521 ID.AddInteger(SVT.getRawBits());
9522 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9523 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9524 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9525 void *IP = nullptr;
9526 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9527 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9528 return SDValue(E, 0);
9529 }
9530 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9531 VTs, ISD::UNINDEXED, true,
9532 IsCompressing, SVT, MMO);
9533 createOperands(N, Ops);
9534
9535 CSEMap.InsertNode(N, IP);
9536 InsertNode(N);
9537 SDValue V(N, 0);
9538 NewSDValueDbgMsg(V, "Creating new node: ", this);
9539 return V;
9540}
9541
9544 ISD::MemIndexType IndexType) {
9545 assert(Ops.size() == 6 && "Incompatible number of operands");
9546
9548 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9549 ID.AddInteger(VT.getRawBits());
9550 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9551 dl.getIROrder(), VTs, VT, MMO, IndexType));
9552 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9553 ID.AddInteger(MMO->getFlags());
9554 void *IP = nullptr;
9555 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9556 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9557 return SDValue(E, 0);
9558 }
9559
9560 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9561 VT, MMO, IndexType);
9562 createOperands(N, Ops);
9563
9564 assert(N->getMask().getValueType().getVectorElementCount() ==
9565 N->getValueType(0).getVectorElementCount() &&
9566 "Vector width mismatch between mask and data");
9567 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9568 N->getValueType(0).getVectorElementCount().isScalable() &&
9569 "Scalable flags of index and data do not match");
9571 N->getIndex().getValueType().getVectorElementCount(),
9572 N->getValueType(0).getVectorElementCount()) &&
9573 "Vector width mismatch between index and data");
9574 assert(isa<ConstantSDNode>(N->getScale()) &&
9575 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9576 "Scale should be a constant power of 2");
9577
9578 CSEMap.InsertNode(N, IP);
9579 InsertNode(N);
9580 SDValue V(N, 0);
9581 NewSDValueDbgMsg(V, "Creating new node: ", this);
9582 return V;
9583}
9584
9587 MachineMemOperand *MMO,
9588 ISD::MemIndexType IndexType) {
9589 assert(Ops.size() == 7 && "Incompatible number of operands");
9590
9592 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9593 ID.AddInteger(VT.getRawBits());
9594 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9595 dl.getIROrder(), VTs, VT, MMO, IndexType));
9596 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9597 ID.AddInteger(MMO->getFlags());
9598 void *IP = nullptr;
9599 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9600 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9601 return SDValue(E, 0);
9602 }
9603 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9604 VT, MMO, IndexType);
9605 createOperands(N, Ops);
9606
9607 assert(N->getMask().getValueType().getVectorElementCount() ==
9608 N->getValue().getValueType().getVectorElementCount() &&
9609 "Vector width mismatch between mask and data");
9610 assert(
9611 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9612 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9613 "Scalable flags of index and data do not match");
9615 N->getIndex().getValueType().getVectorElementCount(),
9616 N->getValue().getValueType().getVectorElementCount()) &&
9617 "Vector width mismatch between index and data");
9618 assert(isa<ConstantSDNode>(N->getScale()) &&
9619 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9620 "Scale should be a constant power of 2");
9621
9622 CSEMap.InsertNode(N, IP);
9623 InsertNode(N);
9624 SDValue V(N, 0);
9625 NewSDValueDbgMsg(V, "Creating new node: ", this);
9626 return V;
9627}
9628
9631 SDValue PassThru, EVT MemVT,
9632 MachineMemOperand *MMO,
9634 ISD::LoadExtType ExtTy, bool isExpanding) {
9635 bool Indexed = AM != ISD::UNINDEXED;
9636 assert((Indexed || Offset.isUndef()) &&
9637 "Unindexed masked load with an offset!");
9638 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9639 : getVTList(VT, MVT::Other);
9640 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9642 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9643 ID.AddInteger(MemVT.getRawBits());
9644 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9645 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9646 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9647 ID.AddInteger(MMO->getFlags());
9648 void *IP = nullptr;
9649 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9650 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9651 return SDValue(E, 0);
9652 }
9653 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9654 AM, ExtTy, isExpanding, MemVT, MMO);
9655 createOperands(N, Ops);
9656
9657 CSEMap.InsertNode(N, IP);
9658 InsertNode(N);
9659 SDValue V(N, 0);
9660 NewSDValueDbgMsg(V, "Creating new node: ", this);
9661 return V;
9662}
9663
9667 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9668 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9669 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9670 Offset, LD->getMask(), LD->getPassThru(),
9671 LD->getMemoryVT(), LD->getMemOperand(), AM,
9672 LD->getExtensionType(), LD->isExpandingLoad());
9673}
9674
9677 SDValue Mask, EVT MemVT,
9678 MachineMemOperand *MMO,
9679 ISD::MemIndexedMode AM, bool IsTruncating,
9680 bool IsCompressing) {
9681 assert(Chain.getValueType() == MVT::Other &&
9682 "Invalid chain type");
9683 bool Indexed = AM != ISD::UNINDEXED;
9684 assert((Indexed || Offset.isUndef()) &&
9685 "Unindexed masked store with an offset!");
9686 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9687 : getVTList(MVT::Other);
9688 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9690 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9691 ID.AddInteger(MemVT.getRawBits());
9692 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9693 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9694 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9695 ID.AddInteger(MMO->getFlags());
9696 void *IP = nullptr;
9697 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9698 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9699 return SDValue(E, 0);
9700 }
9701 auto *N =
9702 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9703 IsTruncating, IsCompressing, MemVT, MMO);
9704 createOperands(N, Ops);
9705
9706 CSEMap.InsertNode(N, IP);
9707 InsertNode(N);
9708 SDValue V(N, 0);
9709 NewSDValueDbgMsg(V, "Creating new node: ", this);
9710 return V;
9711}
9712
9716 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
9717 assert(ST->getOffset().isUndef() &&
9718 "Masked store is already a indexed store!");
9719 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9720 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
9721 AM, ST->isTruncatingStore(), ST->isCompressingStore());
9722}
9723
9726 MachineMemOperand *MMO,
9727 ISD::MemIndexType IndexType,
9728 ISD::LoadExtType ExtTy) {
9729 assert(Ops.size() == 6 && "Incompatible number of operands");
9730
9732 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
9733 ID.AddInteger(MemVT.getRawBits());
9734 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
9735 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
9736 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9737 ID.AddInteger(MMO->getFlags());
9738 void *IP = nullptr;
9739 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9740 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9741 return SDValue(E, 0);
9742 }
9743
9744 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9745 VTs, MemVT, MMO, IndexType, ExtTy);
9746 createOperands(N, Ops);
9747
9748 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
9749 "Incompatible type of the PassThru value in MaskedGatherSDNode");
9750 assert(N->getMask().getValueType().getVectorElementCount() ==
9751 N->getValueType(0).getVectorElementCount() &&
9752 "Vector width mismatch between mask and data");
9753 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9754 N->getValueType(0).getVectorElementCount().isScalable() &&
9755 "Scalable flags of index and data do not match");
9757 N->getIndex().getValueType().getVectorElementCount(),
9758 N->getValueType(0).getVectorElementCount()) &&
9759 "Vector width mismatch between index and data");
9760 assert(isa<ConstantSDNode>(N->getScale()) &&
9761 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9762 "Scale should be a constant power of 2");
9763
9764 CSEMap.InsertNode(N, IP);
9765 InsertNode(N);
9766 SDValue V(N, 0);
9767 NewSDValueDbgMsg(V, "Creating new node: ", this);
9768 return V;
9769}
9770
9773 MachineMemOperand *MMO,
9774 ISD::MemIndexType IndexType,
9775 bool IsTrunc) {
9776 assert(Ops.size() == 6 && "Incompatible number of operands");
9777
9779 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
9780 ID.AddInteger(MemVT.getRawBits());
9781 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
9782 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
9783 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9784 ID.AddInteger(MMO->getFlags());
9785 void *IP = nullptr;
9786 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9787 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
9788 return SDValue(E, 0);
9789 }
9790
9791 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9792 VTs, MemVT, MMO, IndexType, IsTrunc);
9793 createOperands(N, Ops);
9794
9795 assert(N->getMask().getValueType().getVectorElementCount() ==
9796 N->getValue().getValueType().getVectorElementCount() &&
9797 "Vector width mismatch between mask and data");
9798 assert(
9799 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9800 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9801 "Scalable flags of index and data do not match");
9803 N->getIndex().getValueType().getVectorElementCount(),
9804 N->getValue().getValueType().getVectorElementCount()) &&
9805 "Vector width mismatch between index and data");
9806 assert(isa<ConstantSDNode>(N->getScale()) &&
9807 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9808 "Scale should be a constant power of 2");
9809
9810 CSEMap.InsertNode(N, IP);
9811 InsertNode(N);
9812 SDValue V(N, 0);
9813 NewSDValueDbgMsg(V, "Creating new node: ", this);
9814 return V;
9815}
9816
9818 const SDLoc &dl, ArrayRef<SDValue> Ops,
9819 MachineMemOperand *MMO,
9820 ISD::MemIndexType IndexType) {
9821 assert(Ops.size() == 7 && "Incompatible number of operands");
9822
9825 ID.AddInteger(MemVT.getRawBits());
9826 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
9827 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
9828 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9829 ID.AddInteger(MMO->getFlags());
9830 void *IP = nullptr;
9831 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9832 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
9833 return SDValue(E, 0);
9834 }
9835
9836 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
9837 VTs, MemVT, MMO, IndexType);
9838 createOperands(N, Ops);
9839
9840 assert(N->getMask().getValueType().getVectorElementCount() ==
9841 N->getIndex().getValueType().getVectorElementCount() &&
9842 "Vector width mismatch between mask and data");
9843 assert(isa<ConstantSDNode>(N->getScale()) &&
9844 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9845 "Scale should be a constant power of 2");
9846 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
9847
9848 CSEMap.InsertNode(N, IP);
9849 InsertNode(N);
9850 SDValue V(N, 0);
9851 NewSDValueDbgMsg(V, "Creating new node: ", this);
9852 return V;
9853}
9854
9856 EVT MemVT, MachineMemOperand *MMO) {
9857 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9858 SDVTList VTs = getVTList(MVT::Other);
9859 SDValue Ops[] = {Chain, Ptr};
9862 ID.AddInteger(MemVT.getRawBits());
9863 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9864 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9865 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9866 ID.AddInteger(MMO->getFlags());
9867 void *IP = nullptr;
9868 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9869 return SDValue(E, 0);
9870
9871 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
9872 dl.getDebugLoc(), VTs, MemVT, MMO);
9873 createOperands(N, Ops);
9874
9875 CSEMap.InsertNode(N, IP);
9876 InsertNode(N);
9877 SDValue V(N, 0);
9878 NewSDValueDbgMsg(V, "Creating new node: ", this);
9879 return V;
9880}
9881
9883 EVT MemVT, MachineMemOperand *MMO) {
9884 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9885 SDVTList VTs = getVTList(MVT::Other);
9886 SDValue Ops[] = {Chain, Ptr};
9889 ID.AddInteger(MemVT.getRawBits());
9890 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
9891 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
9892 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9893 ID.AddInteger(MMO->getFlags());
9894 void *IP = nullptr;
9895 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9896 return SDValue(E, 0);
9897
9898 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
9899 dl.getDebugLoc(), VTs, MemVT, MMO);
9900 createOperands(N, Ops);
9901
9902 CSEMap.InsertNode(N, IP);
9903 InsertNode(N);
9904 SDValue V(N, 0);
9905 NewSDValueDbgMsg(V, "Creating new node: ", this);
9906 return V;
9907}
9908
9910 // select undef, T, F --> T (if T is a constant), otherwise F
9911 // select, ?, undef, F --> F
9912 // select, ?, T, undef --> T
9913 if (Cond.isUndef())
9914 return isConstantValueOfAnyType(T) ? T : F;
9915 if (T.isUndef())
9916 return F;
9917 if (F.isUndef())
9918 return T;
9919
9920 // select true, T, F --> T
9921 // select false, T, F --> F
9922 if (auto *CondC = dyn_cast<ConstantSDNode>(Cond))
9923 return CondC->isZero() ? F : T;
9924
9925 // TODO: This should simplify VSELECT with non-zero constant condition using
9926 // something like this (but check boolean contents to be complete?):
9927 if (ConstantSDNode *CondC = isConstOrConstSplat(Cond, /*AllowUndefs*/ false,
9928 /*AllowTruncation*/ true))
9929 if (CondC->isZero())
9930 return F;
9931
9932 // select ?, T, T --> T
9933 if (T == F)
9934 return T;
9935
9936 return SDValue();
9937}
9938
9940 // shift undef, Y --> 0 (can always assume that the undef value is 0)
9941 if (X.isUndef())
9942 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
9943 // shift X, undef --> undef (because it may shift by the bitwidth)
9944 if (Y.isUndef())
9945 return getUNDEF(X.getValueType());
9946
9947 // shift 0, Y --> 0
9948 // shift X, 0 --> X
9950 return X;
9951
9952 // shift X, C >= bitwidth(X) --> undef
9953 // All vector elements must be too big (or undef) to avoid partial undefs.
9954 auto isShiftTooBig = [X](ConstantSDNode *Val) {
9955 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
9956 };
9957 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
9958 return getUNDEF(X.getValueType());
9959
9960 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
9961 if (X.getValueType().getScalarType() == MVT::i1)
9962 return X;
9963
9964 return SDValue();
9965}
9966
9968 SDNodeFlags Flags) {
9969 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
9970 // (an undef operand can be chosen to be Nan/Inf), then the result of this
9971 // operation is poison. That result can be relaxed to undef.
9972 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
9973 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
9974 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
9975 (YC && YC->getValueAPF().isNaN());
9976 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
9977 (YC && YC->getValueAPF().isInfinity());
9978
9979 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
9980 return getUNDEF(X.getValueType());
9981
9982 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
9983 return getUNDEF(X.getValueType());
9984
9985 if (!YC)
9986 return SDValue();
9987
9988 // X + -0.0 --> X
9989 if (Opcode == ISD::FADD)
9990 if (YC->getValueAPF().isNegZero())
9991 return X;
9992
9993 // X - +0.0 --> X
9994 if (Opcode == ISD::FSUB)
9995 if (YC->getValueAPF().isPosZero())
9996 return X;
9997
9998 // X * 1.0 --> X
9999 // X / 1.0 --> X
10000 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10001 if (YC->getValueAPF().isExactlyValue(1.0))
10002 return X;
10003
10004 // X * 0.0 --> 0.0
10005 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10006 if (YC->getValueAPF().isZero())
10007 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10008
10009 return SDValue();
10010}
10011
10013 SDValue Ptr, SDValue SV, unsigned Align) {
10014 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10015 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10016}
10017
10018SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10019 ArrayRef<SDUse> Ops) {
10020 switch (Ops.size()) {
10021 case 0: return getNode(Opcode, DL, VT);
10022 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
10023 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10024 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10025 default: break;
10026 }
10027
10028 // Copy from an SDUse array into an SDValue array for use with
10029 // the regular getNode logic.
10030 SmallVector<SDValue, 8> NewOps(Ops.begin(), Ops.end());
10031 return getNode(Opcode, DL, VT, NewOps);
10032}
10033
10034SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10035 ArrayRef<SDValue> Ops) {
10036 SDNodeFlags Flags;
10037 if (Inserter)
10038 Flags = Inserter->getFlags();
10039 return getNode(Opcode, DL, VT, Ops, Flags);
10040}
10041
10042SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10043 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10044 unsigned NumOps = Ops.size();
10045 switch (NumOps) {
10046 case 0: return getNode(Opcode, DL, VT);
10047 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10048 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10049 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10050 default: break;
10051 }
10052
10053#ifndef NDEBUG
10054 for (const auto &Op : Ops)
10055 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10056 "Operand is DELETED_NODE!");
10057#endif
10058
10059 switch (Opcode) {
10060 default: break;
10061 case ISD::BUILD_VECTOR:
10062 // Attempt to simplify BUILD_VECTOR.
10063 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10064 return V;
10065 break;
10067 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10068 return V;
10069 break;
10070 case ISD::SELECT_CC:
10071 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10072 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10073 "LHS and RHS of condition must have same type!");
10074 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10075 "True and False arms of SelectCC must have same type!");
10076 assert(Ops[2].getValueType() == VT &&
10077 "select_cc node must be of same type as true and false value!");
10078 assert((!Ops[0].getValueType().isVector() ||
10079 Ops[0].getValueType().getVectorElementCount() ==
10080 VT.getVectorElementCount()) &&
10081 "Expected select_cc with vector result to have the same sized "
10082 "comparison type!");
10083 break;
10084 case ISD::BR_CC:
10085 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10086 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10087 "LHS/RHS of comparison should match types!");
10088 break;
10089 case ISD::VP_ADD:
10090 case ISD::VP_SUB:
10091 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10092 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10093 Opcode = ISD::VP_XOR;
10094 break;
10095 case ISD::VP_MUL:
10096 // If it is VP_MUL mask operation then turn it to VP_AND
10097 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10098 Opcode = ISD::VP_AND;
10099 break;
10100 case ISD::VP_REDUCE_MUL:
10101 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10102 if (VT == MVT::i1)
10103 Opcode = ISD::VP_REDUCE_AND;
10104 break;
10105 case ISD::VP_REDUCE_ADD:
10106 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10107 if (VT == MVT::i1)
10108 Opcode = ISD::VP_REDUCE_XOR;
10109 break;
10110 case ISD::VP_REDUCE_SMAX:
10111 case ISD::VP_REDUCE_UMIN:
10112 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10113 // VP_REDUCE_AND.
10114 if (VT == MVT::i1)
10115 Opcode = ISD::VP_REDUCE_AND;
10116 break;
10117 case ISD::VP_REDUCE_SMIN:
10118 case ISD::VP_REDUCE_UMAX:
10119 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10120 // VP_REDUCE_OR.
10121 if (VT == MVT::i1)
10122 Opcode = ISD::VP_REDUCE_OR;
10123 break;
10124 }
10125
10126 // Memoize nodes.
10127 SDNode *N;
10128 SDVTList VTs = getVTList(VT);
10129
10130 if (VT != MVT::Glue) {
10132 AddNodeIDNode(ID, Opcode, VTs, Ops);
10133 void *IP = nullptr;
10134
10135 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10136 return SDValue(E, 0);
10137
10138 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10139 createOperands(N, Ops);
10140
10141 CSEMap.InsertNode(N, IP);
10142 } else {
10143 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10144 createOperands(N, Ops);
10145 }
10146
10147 N->setFlags(Flags);
10148 InsertNode(N);
10149 SDValue V(N, 0);
10150 NewSDValueDbgMsg(V, "Creating new node: ", this);
10151 return V;
10152}
10153
10154SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10155 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10156 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
10157}
10158
10159SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10160 ArrayRef<SDValue> Ops) {
10161 SDNodeFlags Flags;
10162 if (Inserter)
10163 Flags = Inserter->getFlags();
10164 return getNode(Opcode, DL, VTList, Ops, Flags);
10165}
10166
10167SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10168 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10169 if (VTList.NumVTs == 1)
10170 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10171
10172#ifndef NDEBUG
10173 for (const auto &Op : Ops)
10174 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10175 "Operand is DELETED_NODE!");
10176#endif
10177
10178 switch (Opcode) {
10179 case ISD::SADDO:
10180 case ISD::UADDO:
10181 case ISD::SSUBO:
10182 case ISD::USUBO: {
10183 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10184 "Invalid add/sub overflow op!");
10185 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10186 Ops[0].getValueType() == Ops[1].getValueType() &&
10187 Ops[0].getValueType() == VTList.VTs[0] &&
10188 "Binary operator types must match!");
10189 SDValue N1 = Ops[0], N2 = Ops[1];
10190 canonicalizeCommutativeBinop(Opcode, N1, N2);
10191
10192 // (X +- 0) -> X with zero-overflow.
10193 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10194 /*AllowTruncation*/ true);
10195 if (N2CV && N2CV->isZero()) {
10196 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10197 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10198 }
10199
10200 if (VTList.VTs[0].isVector() &&
10201 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
10202 VTList.VTs[1].getVectorElementType() == MVT::i1) {
10203 SDValue F1 = getFreeze(N1);
10204 SDValue F2 = getFreeze(N2);
10205 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10206 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10207 return getNode(ISD::MERGE_VALUES, DL, VTList,
10208 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10209 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10210 Flags);
10211 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
10212 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
10213 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
10214 return getNode(ISD::MERGE_VALUES, DL, VTList,
10215 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10216 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
10217 Flags);
10218 }
10219 }
10220 break;
10221 }
10222 case ISD::SADDO_CARRY:
10223 case ISD::UADDO_CARRY:
10224 case ISD::SSUBO_CARRY:
10225 case ISD::USUBO_CARRY:
10226 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
10227 "Invalid add/sub overflow op!");
10228 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10229 Ops[0].getValueType() == Ops[1].getValueType() &&
10230 Ops[0].getValueType() == VTList.VTs[0] &&
10231 Ops[2].getValueType() == VTList.VTs[1] &&
10232 "Binary operator types must match!");
10233 break;
10234 case ISD::SMUL_LOHI:
10235 case ISD::UMUL_LOHI: {
10236 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
10237 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10238 VTList.VTs[0] == Ops[0].getValueType() &&
10239 VTList.VTs[0] == Ops[1].getValueType() &&
10240 "Binary operator types must match!");
10241 // Constant fold.
10242 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10243 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10244 if (LHS && RHS) {
10245 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10246 unsigned OutWidth = Width * 2;
10247 APInt Val = LHS->getAPIntValue();
10248 APInt Mul = RHS->getAPIntValue();
10249 if (Opcode == ISD::SMUL_LOHI) {
10250 Val = Val.sext(OutWidth);
10251 Mul = Mul.sext(OutWidth);
10252 } else {
10253 Val = Val.zext(OutWidth);
10254 Mul = Mul.zext(OutWidth);
10255 }
10256 Val *= Mul;
10257
10258 SDValue Hi =
10259 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10260 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10261 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10262 }
10263 break;
10264 }
10265 case ISD::FFREXP: {
10266 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10267 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10268 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10269
10270 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10271 int FrexpExp;
10272 APFloat FrexpMant =
10273 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10274 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10275 SDValue Result1 =
10276 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10277 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10278 }
10279
10280 break;
10281 }
10283 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10284 "Invalid STRICT_FP_EXTEND!");
10285 assert(VTList.VTs[0].isFloatingPoint() &&
10286 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10287 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10288 "STRICT_FP_EXTEND result type should be vector iff the operand "
10289 "type is vector!");
10290 assert((!VTList.VTs[0].isVector() ||
10291 VTList.VTs[0].getVectorElementCount() ==
10292 Ops[1].getValueType().getVectorElementCount()) &&
10293 "Vector element count mismatch!");
10294 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10295 "Invalid fpext node, dst <= src!");
10296 break;
10298 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10299 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10300 "STRICT_FP_ROUND result type should be vector iff the operand "
10301 "type is vector!");
10302 assert((!VTList.VTs[0].isVector() ||
10303 VTList.VTs[0].getVectorElementCount() ==
10304 Ops[1].getValueType().getVectorElementCount()) &&
10305 "Vector element count mismatch!");
10306 assert(VTList.VTs[0].isFloatingPoint() &&
10307 Ops[1].getValueType().isFloatingPoint() &&
10308 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10309 isa<ConstantSDNode>(Ops[2]) &&
10310 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10311 "Invalid STRICT_FP_ROUND!");
10312 break;
10313#if 0
10314 // FIXME: figure out how to safely handle things like
10315 // int foo(int x) { return 1 << (x & 255); }
10316 // int bar() { return foo(256); }
10317 case ISD::SRA_PARTS:
10318 case ISD::SRL_PARTS:
10319 case ISD::SHL_PARTS:
10320 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10321 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10322 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10323 else if (N3.getOpcode() == ISD::AND)
10324 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10325 // If the and is only masking out bits that cannot effect the shift,
10326 // eliminate the and.
10327 unsigned NumBits = VT.getScalarSizeInBits()*2;
10328 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10329 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10330 }
10331 break;
10332#endif
10333 }
10334
10335 // Memoize the node unless it returns a glue result.
10336 SDNode *N;
10337 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10339 AddNodeIDNode(ID, Opcode, VTList, Ops);
10340 void *IP = nullptr;
10341 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
10342 return SDValue(E, 0);
10343
10344 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10345 createOperands(N, Ops);
10346 CSEMap.InsertNode(N, IP);
10347 } else {
10348 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10349 createOperands(N, Ops);
10350 }
10351
10352 N->setFlags(Flags);
10353 InsertNode(N);
10354 SDValue V(N, 0);
10355 NewSDValueDbgMsg(V, "Creating new node: ", this);
10356 return V;
10357}
10358
10359SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10360 SDVTList VTList) {
10361 return getNode(Opcode, DL, VTList, std::nullopt);
10362}
10363
10364SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10365 SDValue N1) {
10366 SDValue Ops[] = { N1 };
10367 return getNode(Opcode, DL, VTList, Ops);
10368}
10369
10370SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10371 SDValue N1, SDValue N2) {
10372 SDValue Ops[] = { N1, N2 };
10373 return getNode(Opcode, DL, VTList, Ops);
10374}
10375
10376SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10377 SDValue N1, SDValue N2, SDValue N3) {
10378 SDValue Ops[] = { N1, N2, N3 };
10379 return getNode(Opcode, DL, VTList, Ops);
10380}
10381
10382SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10383 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10384 SDValue Ops[] = { N1, N2, N3, N4 };
10385 return getNode(Opcode, DL, VTList, Ops);
10386}
10387
10388SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10389 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10390 SDValue N5) {
10391 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10392 return getNode(Opcode, DL, VTList, Ops);
10393}
10394
10396 return makeVTList(SDNode::getValueTypeList(VT), 1);
10397}
10398
10401 ID.AddInteger(2U);
10402 ID.AddInteger(VT1.getRawBits());
10403 ID.AddInteger(VT2.getRawBits());
10404
10405 void *IP = nullptr;
10406 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10407 if (!Result) {
10408 EVT *Array = Allocator.Allocate<EVT>(2);
10409 Array[0] = VT1;
10410 Array[1] = VT2;
10411 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10412 VTListMap.InsertNode(Result, IP);
10413 }
10414 return Result->getSDVTList();
10415}
10416
10419 ID.AddInteger(3U);
10420 ID.AddInteger(VT1.getRawBits());
10421 ID.AddInteger(VT2.getRawBits());
10422 ID.AddInteger(VT3.getRawBits());
10423
10424 void *IP = nullptr;
10425 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10426 if (!Result) {
10427 EVT *Array = Allocator.Allocate<EVT>(3);
10428 Array[0] = VT1;
10429 Array[1] = VT2;
10430 Array[2] = VT3;
10431 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10432 VTListMap.InsertNode(Result, IP);
10433 }
10434 return Result->getSDVTList();
10435}
10436
10439 ID.AddInteger(4U);
10440 ID.AddInteger(VT1.getRawBits());
10441 ID.AddInteger(VT2.getRawBits());
10442 ID.AddInteger(VT3.getRawBits());
10443 ID.AddInteger(VT4.getRawBits());
10444
10445 void *IP = nullptr;
10446 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10447 if (!Result) {
10448 EVT *Array = Allocator.Allocate<EVT>(4);
10449 Array[0] = VT1;
10450 Array[1] = VT2;
10451 Array[2] = VT3;
10452 Array[3] = VT4;
10453 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10454 VTListMap.InsertNode(Result, IP);
10455 }
10456 return Result->getSDVTList();
10457}
10458
10460 unsigned NumVTs = VTs.size();
10462 ID.AddInteger(NumVTs);
10463 for (unsigned index = 0; index < NumVTs; index++) {
10464 ID.AddInteger(VTs[index].getRawBits());
10465 }
10466
10467 void *IP = nullptr;
10468 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10469 if (!Result) {
10470 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10471 llvm::copy(VTs, Array);
10472 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10473 VTListMap.InsertNode(Result, IP);
10474 }
10475 return Result->getSDVTList();
10476}
10477
10478
10479/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10480/// specified operands. If the resultant node already exists in the DAG,
10481/// this does not modify the specified node, instead it returns the node that
10482/// already exists. If the resultant node does not exist in the DAG, the
10483/// input node is returned. As a degenerate case, if you specify the same
10484/// input operands as the node already has, the input node is returned.
10486 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10487
10488 // Check to see if there is no change.
10489 if (Op == N->getOperand(0)) return N;
10490
10491 // See if the modified node already exists.
10492 void *InsertPos = nullptr;
10493 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10494 return Existing;
10495
10496 // Nope it doesn't. Remove the node from its current place in the maps.
10497 if (InsertPos)
10498 if (!RemoveNodeFromCSEMaps(N))
10499 InsertPos = nullptr;
10500
10501 // Now we update the operands.
10502 N->OperandList[0].set(Op);
10503
10505 // If this gets put into a CSE map, add it.
10506 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10507 return N;
10508}
10509
10511 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10512
10513 // Check to see if there is no change.
10514 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10515 return N; // No operands changed, just return the input node.
10516
10517 // See if the modified node already exists.
10518 void *InsertPos = nullptr;
10519 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10520 return Existing;
10521
10522 // Nope it doesn't. Remove the node from its current place in the maps.
10523 if (InsertPos)
10524 if (!RemoveNodeFromCSEMaps(N))
10525 InsertPos = nullptr;
10526
10527 // Now we update the operands.
10528 if (N->OperandList[0] != Op1)
10529 N->OperandList[0].set(Op1);
10530 if (N->OperandList[1] != Op2)
10531 N->OperandList[1].set(Op2);
10532
10534 // If this gets put into a CSE map, add it.
10535 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10536 return N;
10537}
10538
10541 SDValue Ops[] = { Op1, Op2, Op3 };
10542 return UpdateNodeOperands(N, Ops);
10543}
10544
10547 SDValue Op3, SDValue Op4) {
10548 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10549 return UpdateNodeOperands(N, Ops);
10550}
10551
10554 SDValue Op3, SDValue Op4, SDValue Op5) {
10555 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10556 return UpdateNodeOperands(N, Ops);
10557}
10558
10561 unsigned NumOps = Ops.size();
10562 assert(N->getNumOperands() == NumOps &&
10563 "Update with wrong number of operands");
10564
10565 // If no operands changed just return the input node.
10566 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10567 return N;
10568
10569 // See if the modified node already exists.
10570 void *InsertPos = nullptr;
10571 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10572 return Existing;
10573
10574 // Nope it doesn't. Remove the node from its current place in the maps.
10575 if (InsertPos)
10576 if (!RemoveNodeFromCSEMaps(N))
10577 InsertPos = nullptr;
10578
10579 // Now we update the operands.
10580 for (unsigned i = 0; i != NumOps; ++i)
10581 if (N->OperandList[i] != Ops[i])
10582 N->OperandList[i].set(Ops[i]);
10583
10585 // If this gets put into a CSE map, add it.
10586 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10587 return N;
10588}
10589
10590/// DropOperands - Release the operands and set this node to have
10591/// zero operands.
10593 // Unlike the code in MorphNodeTo that does this, we don't need to
10594 // watch for dead nodes here.
10595 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10596 SDUse &Use = *I++;
10597 Use.set(SDValue());
10598 }
10599}
10600
10602 ArrayRef<MachineMemOperand *> NewMemRefs) {
10603 if (NewMemRefs.empty()) {
10604 N->clearMemRefs();
10605 return;
10606 }
10607
10608 // Check if we can avoid allocating by storing a single reference directly.
10609 if (NewMemRefs.size() == 1) {
10610 N->MemRefs = NewMemRefs[0];
10611 N->NumMemRefs = 1;
10612 return;
10613 }
10614
10615 MachineMemOperand **MemRefsBuffer =
10616 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10617 llvm::copy(NewMemRefs, MemRefsBuffer);
10618 N->MemRefs = MemRefsBuffer;
10619 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10620}
10621
10622/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10623/// machine opcode.
10624///
10626 EVT VT) {
10627 SDVTList VTs = getVTList(VT);
10628 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10629}
10630
10632 EVT VT, SDValue Op1) {
10633 SDVTList VTs = getVTList(VT);
10634 SDValue Ops[] = { Op1 };
10635 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10636}
10637
10639 EVT VT, SDValue Op1,
10640 SDValue Op2) {
10641 SDVTList VTs = getVTList(VT);
10642 SDValue Ops[] = { Op1, Op2 };
10643 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10644}
10645
10647 EVT VT, SDValue Op1,
10648 SDValue Op2, SDValue Op3) {
10649 SDVTList VTs = getVTList(VT);
10650 SDValue Ops[] = { Op1, Op2, Op3 };
10651 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10652}
10653
10655 EVT VT, ArrayRef<SDValue> Ops) {
10656 SDVTList VTs = getVTList(VT);
10657 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10658}
10659
10661 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10662 SDVTList VTs = getVTList(VT1, VT2);
10663 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10664}
10665
10667 EVT VT1, EVT VT2) {
10668 SDVTList VTs = getVTList(VT1, VT2);
10669 return SelectNodeTo(N, MachineOpc, VTs, std::nullopt);
10670}
10671
10673 EVT VT1, EVT VT2, EVT VT3,
10674 ArrayRef<SDValue> Ops) {
10675 SDVTList VTs = getVTList(VT1, VT2, VT3);
10676 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10677}
10678
10680 EVT VT1, EVT VT2,
10681 SDValue Op1, SDValue Op2) {
10682 SDVTList VTs = getVTList(VT1, VT2);
10683 SDValue Ops[] = { Op1, Op2 };
10684 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10685}
10686
10688 SDVTList VTs,ArrayRef<SDValue> Ops) {
10689 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10690 // Reset the NodeID to -1.
10691 New->setNodeId(-1);
10692 if (New != N) {
10693 ReplaceAllUsesWith(N, New);
10695 }
10696 return New;
10697}
10698
10699/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10700/// the line number information on the merged node since it is not possible to
10701/// preserve the information that operation is associated with multiple lines.
10702/// This will make the debugger working better at -O0, were there is a higher
10703/// probability having other instructions associated with that line.
10704///
10705/// For IROrder, we keep the smaller of the two
10706SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
10707 DebugLoc NLoc = N->getDebugLoc();
10708 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
10709 N->setDebugLoc(DebugLoc());
10710 }
10711 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
10712 N->setIROrder(Order);
10713 return N;
10714}
10715
10716/// MorphNodeTo - This *mutates* the specified node to have the specified
10717/// return type, opcode, and operands.
10718///
10719/// Note that MorphNodeTo returns the resultant node. If there is already a
10720/// node of the specified opcode and operands, it returns that node instead of
10721/// the current one. Note that the SDLoc need not be the same.
10722///
10723/// Using MorphNodeTo is faster than creating a new node and swapping it in
10724/// with ReplaceAllUsesWith both because it often avoids allocating a new
10725/// node, and because it doesn't require CSE recalculation for any of
10726/// the node's users.
10727///
10728/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
10729/// As a consequence it isn't appropriate to use from within the DAG combiner or
10730/// the legalizer which maintain worklists that would need to be updated when
10731/// deleting things.
10733 SDVTList VTs, ArrayRef<SDValue> Ops) {
10734 // If an identical node already exists, use it.
10735 void *IP = nullptr;
10736 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
10738 AddNodeIDNode(ID, Opc, VTs, Ops);
10739 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
10740 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
10741 }
10742
10743 if (!RemoveNodeFromCSEMaps(N))
10744 IP = nullptr;
10745
10746 // Start the morphing.
10747 N->NodeType = Opc;
10748 N->ValueList = VTs.VTs;
10749 N->NumValues = VTs.NumVTs;
10750
10751 // Clear the operands list, updating used nodes to remove this from their
10752 // use list. Keep track of any operands that become dead as a result.
10753 SmallPtrSet<SDNode*, 16> DeadNodeSet;
10754 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
10755 SDUse &Use = *I++;
10756 SDNode *Used = Use.getNode();
10757 Use.set(SDValue());
10758 if (Used->use_empty())
10759 DeadNodeSet.insert(Used);
10760 }
10761
10762 // For MachineNode, initialize the memory references information.
10763 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
10764 MN->clearMemRefs();
10765
10766 // Swap for an appropriately sized array from the recycler.
10767 removeOperands(N);
10768 createOperands(N, Ops);
10769
10770 // Delete any nodes that are still dead after adding the uses for the
10771 // new operands.
10772 if (!DeadNodeSet.empty()) {
10773 SmallVector<SDNode *, 16> DeadNodes;
10774 for (SDNode *N : DeadNodeSet)
10775 if (N->use_empty())
10776 DeadNodes.push_back(N);
10777 RemoveDeadNodes(DeadNodes);
10778 }
10779
10780 if (IP)
10781 CSEMap.InsertNode(N, IP); // Memoize the new node.
10782 return N;
10783}
10784
10786 unsigned OrigOpc = Node->getOpcode();
10787 unsigned NewOpc;
10788 switch (OrigOpc) {
10789 default:
10790 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
10791#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10792 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
10793#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
10794 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
10795#include "llvm/IR/ConstrainedOps.def"
10796 }
10797
10798 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
10799
10800 // We're taking this node out of the chain, so we need to re-link things.
10801 SDValue InputChain = Node->getOperand(0);
10802 SDValue OutputChain = SDValue(Node, 1);
10803 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
10804
10806 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
10807 Ops.push_back(Node->getOperand(i));
10808
10809 SDVTList VTs = getVTList(Node->getValueType(0));
10810 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
10811
10812 // MorphNodeTo can operate in two ways: if an existing node with the
10813 // specified operands exists, it can just return it. Otherwise, it
10814 // updates the node in place to have the requested operands.
10815 if (Res == Node) {
10816 // If we updated the node in place, reset the node ID. To the isel,
10817 // this should be just like a newly allocated machine node.
10818 Res->setNodeId(-1);
10819 } else {
10820 ReplaceAllUsesWith(Node, Res);
10821 RemoveDeadNode(Node);
10822 }
10823
10824 return Res;
10825}
10826
10827/// getMachineNode - These are used for target selectors to create a new node
10828/// with specified return type(s), MachineInstr opcode, and operands.
10829///
10830/// Note that getMachineNode returns the resultant node. If there is already a
10831/// node of the specified opcode and operands, it returns that node instead of
10832/// the current one.
10834 EVT VT) {
10835 SDVTList VTs = getVTList(VT);
10836 return getMachineNode(Opcode, dl, VTs, std::nullopt);
10837}
10838
10840 EVT VT, SDValue Op1) {
10841 SDVTList VTs = getVTList(VT);
10842 SDValue Ops[] = { Op1 };
10843 return getMachineNode(Opcode, dl, VTs, Ops);
10844}
10845
10847 EVT VT, SDValue Op1, SDValue Op2) {
10848 SDVTList VTs = getVTList(VT);
10849 SDValue Ops[] = { Op1, Op2 };
10850 return getMachineNode(Opcode, dl, VTs, Ops);
10851}
10852
10854 EVT VT, SDValue Op1, SDValue Op2,
10855 SDValue Op3) {
10856 SDVTList VTs = getVTList(VT);
10857 SDValue Ops[] = { Op1, Op2, Op3 };
10858 return getMachineNode(Opcode, dl, VTs, Ops);
10859}
10860
10862 EVT VT, ArrayRef<SDValue> Ops) {
10863 SDVTList VTs = getVTList(VT);
10864 return getMachineNode(Opcode, dl, VTs, Ops);
10865}
10866
10868 EVT VT1, EVT VT2, SDValue Op1,
10869 SDValue Op2) {
10870 SDVTList VTs = getVTList(VT1, VT2);
10871 SDValue Ops[] = { Op1, Op2 };
10872 return getMachineNode(Opcode, dl, VTs, Ops);
10873}
10874
10876 EVT VT1, EVT VT2, SDValue Op1,
10877 SDValue Op2, SDValue Op3) {
10878 SDVTList VTs = getVTList(VT1, VT2);
10879 SDValue Ops[] = { Op1, Op2, Op3 };
10880 return getMachineNode(Opcode, dl, VTs, Ops);
10881}
10882
10884 EVT VT1, EVT VT2,
10885 ArrayRef<SDValue> Ops) {
10886 SDVTList VTs = getVTList(VT1, VT2);
10887 return getMachineNode(Opcode, dl, VTs, Ops);
10888}
10889
10891 EVT VT1, EVT VT2, EVT VT3,
10892 SDValue Op1, SDValue Op2) {
10893 SDVTList VTs = getVTList(VT1, VT2, VT3);
10894 SDValue Ops[] = { Op1, Op2 };
10895 return getMachineNode(Opcode, dl, VTs, Ops);
10896}
10897
10899 EVT VT1, EVT VT2, EVT VT3,
10900 SDValue Op1, SDValue Op2,
10901 SDValue Op3) {
10902 SDVTList VTs = getVTList(VT1, VT2, VT3);
10903 SDValue Ops[] = { Op1, Op2, Op3 };
10904 return getMachineNode(Opcode, dl, VTs, Ops);
10905}
10906
10908 EVT VT1, EVT VT2, EVT VT3,
10909 ArrayRef<SDValue> Ops) {
10910 SDVTList VTs = getVTList(VT1, VT2, VT3);
10911 return getMachineNode(Opcode, dl, VTs, Ops);
10912}
10913
10915 ArrayRef<EVT> ResultTys,
10916 ArrayRef<SDValue> Ops) {
10917 SDVTList VTs = getVTList(ResultTys);
10918 return getMachineNode(Opcode, dl, VTs, Ops);
10919}
10920
10922 SDVTList VTs,
10923 ArrayRef<SDValue> Ops) {
10924 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
10926 void *IP = nullptr;
10927
10928 if (DoCSE) {
10930 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
10931 IP = nullptr;
10932 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10933 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
10934 }
10935 }
10936
10937 // Allocate a new MachineSDNode.
10938 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10939 createOperands(N, Ops);
10940
10941 if (DoCSE)
10942 CSEMap.InsertNode(N, IP);
10943
10944 InsertNode(N);
10945 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
10946 return N;
10947}
10948
10949/// getTargetExtractSubreg - A convenience function for creating
10950/// TargetOpcode::EXTRACT_SUBREG nodes.
10952 SDValue Operand) {
10953 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10954 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
10955 VT, Operand, SRIdxVal);
10956 return SDValue(Subreg, 0);
10957}
10958
10959/// getTargetInsertSubreg - A convenience function for creating
10960/// TargetOpcode::INSERT_SUBREG nodes.
10962 SDValue Operand, SDValue Subreg) {
10963 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
10964 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
10965 VT, Operand, Subreg, SRIdxVal);
10966 return SDValue(Result, 0);
10967}
10968
10969/// getNodeIfExists - Get the specified node if it's already available, or
10970/// else return NULL.
10972 ArrayRef<SDValue> Ops) {
10973 SDNodeFlags Flags;
10974 if (Inserter)
10975 Flags = Inserter->getFlags();
10976 return getNodeIfExists(Opcode, VTList, Ops, Flags);
10977}
10978
10981 const SDNodeFlags Flags) {
10982 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10984 AddNodeIDNode(ID, Opcode, VTList, Ops);
10985 void *IP = nullptr;
10986 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
10987 E->intersectFlagsWith(Flags);
10988 return E;
10989 }
10990 }
10991 return nullptr;
10992}
10993
10994/// doesNodeExist - Check if a node exists without modifying its flags.
10995bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
10996 ArrayRef<SDValue> Ops) {
10997 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
10999 AddNodeIDNode(ID, Opcode, VTList, Ops);
11000 void *IP = nullptr;
11001 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11002 return true;
11003 }
11004 return false;
11005}
11006
11007/// getDbgValue - Creates a SDDbgValue node.
11008///
11009/// SDNode
11011 SDNode *N, unsigned R, bool IsIndirect,
11012 const DebugLoc &DL, unsigned O) {
11013 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11014 "Expected inlined-at fields to agree");
11015 return new (DbgInfo->getAlloc())
11016 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11017 {}, IsIndirect, DL, O,
11018 /*IsVariadic=*/false);
11019}
11020
11021/// Constant
11023 DIExpression *Expr,
11024 const Value *C,
11025 const DebugLoc &DL, unsigned O) {
11026 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11027 "Expected inlined-at fields to agree");
11028 return new (DbgInfo->getAlloc())
11029 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11030 /*IsIndirect=*/false, DL, O,
11031 /*IsVariadic=*/false);
11032}
11033
11034/// FrameIndex
11036 DIExpression *Expr, unsigned FI,
11037 bool IsIndirect,
11038 const DebugLoc &DL,
11039 unsigned O) {
11040 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11041 "Expected inlined-at fields to agree");
11042 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11043}
11044
11045/// FrameIndex with dependencies
11047 DIExpression *Expr, unsigned FI,
11048 ArrayRef<SDNode *> Dependencies,
11049 bool IsIndirect,
11050 const DebugLoc &DL,
11051 unsigned O) {
11052 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11053 "Expected inlined-at fields to agree");
11054 return new (DbgInfo->getAlloc())
11055 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11056 Dependencies, IsIndirect, DL, O,
11057 /*IsVariadic=*/false);
11058}
11059
11060/// VReg
11062 unsigned VReg, bool IsIndirect,
11063 const DebugLoc &DL, unsigned O) {
11064 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11065 "Expected inlined-at fields to agree");
11066 return new (DbgInfo->getAlloc())
11067 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11068 {}, IsIndirect, DL, O,
11069 /*IsVariadic=*/false);
11070}
11071
11074 ArrayRef<SDNode *> Dependencies,
11075 bool IsIndirect, const DebugLoc &DL,
11076 unsigned O, bool IsVariadic) {
11077 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11078 "Expected inlined-at fields to agree");
11079 return new (DbgInfo->getAlloc())
11080 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11081 DL, O, IsVariadic);
11082}
11083
11085 unsigned OffsetInBits, unsigned SizeInBits,
11086 bool InvalidateDbg) {
11087 SDNode *FromNode = From.getNode();
11088 SDNode *ToNode = To.getNode();
11089 assert(FromNode && ToNode && "Can't modify dbg values");
11090
11091 // PR35338
11092 // TODO: assert(From != To && "Redundant dbg value transfer");
11093 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11094 if (From == To || FromNode == ToNode)
11095 return;
11096
11097 if (!FromNode->getHasDebugValue())
11098 return;
11099
11100 SDDbgOperand FromLocOp =
11101 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11103
11105 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11106 if (Dbg->isInvalidated())
11107 continue;
11108
11109 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11110
11111 // Create a new location ops vector that is equal to the old vector, but
11112 // with each instance of FromLocOp replaced with ToLocOp.
11113 bool Changed = false;
11114 auto NewLocOps = Dbg->copyLocationOps();
11115 std::replace_if(
11116 NewLocOps.begin(), NewLocOps.end(),
11117 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11118 bool Match = Op == FromLocOp;
11119 Changed |= Match;
11120 return Match;
11121 },
11122 ToLocOp);
11123 // Ignore this SDDbgValue if we didn't find a matching location.
11124 if (!Changed)
11125 continue;
11126
11127 DIVariable *Var = Dbg->getVariable();
11128 auto *Expr = Dbg->getExpression();
11129 // If a fragment is requested, update the expression.
11130 if (SizeInBits) {
11131 // When splitting a larger (e.g., sign-extended) value whose
11132 // lower bits are described with an SDDbgValue, do not attempt
11133 // to transfer the SDDbgValue to the upper bits.
11134 if (auto FI = Expr->getFragmentInfo())
11135 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11136 continue;
11137 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11138 SizeInBits);
11139 if (!Fragment)
11140 continue;
11141 Expr = *Fragment;
11142 }
11143
11144 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11145 // Clone the SDDbgValue and move it to To.
11146 SDDbgValue *Clone = getDbgValueList(
11147 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11148 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11149 Dbg->isVariadic());
11150 ClonedDVs.push_back(Clone);
11151
11152 if (InvalidateDbg) {
11153 // Invalidate value and indicate the SDDbgValue should not be emitted.
11154 Dbg->setIsInvalidated();
11155 Dbg->setIsEmitted();
11156 }
11157 }
11158
11159 for (SDDbgValue *Dbg : ClonedDVs) {
11160 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11161 "Transferred DbgValues should depend on the new SDNode");
11162 AddDbgValue(Dbg, false);
11163 }
11164}
11165
11167 if (!N.getHasDebugValue())
11168 return;
11169
11171 for (auto *DV : GetDbgValues(&N)) {
11172 if (DV->isInvalidated())
11173 continue;
11174 switch (N.getOpcode()) {
11175 default:
11176 break;
11177 case ISD::ADD: {
11178 SDValue N0 = N.getOperand(0);
11179 SDValue N1 = N.getOperand(1);
11180 if (!isa<ConstantSDNode>(N0)) {
11181 bool RHSConstant = isa<ConstantSDNode>(N1);
11183 if (RHSConstant)
11184 Offset = N.getConstantOperandVal(1);
11185 // We are not allowed to turn indirect debug values variadic, so
11186 // don't salvage those.
11187 if (!RHSConstant && DV->isIndirect())
11188 continue;
11189
11190 // Rewrite an ADD constant node into a DIExpression. Since we are
11191 // performing arithmetic to compute the variable's *value* in the
11192 // DIExpression, we need to mark the expression with a
11193 // DW_OP_stack_value.
11194 auto *DIExpr = DV->getExpression();
11195 auto NewLocOps = DV->copyLocationOps();
11196 bool Changed = false;
11197 size_t OrigLocOpsSize = NewLocOps.size();
11198 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11199 // We're not given a ResNo to compare against because the whole
11200 // node is going away. We know that any ISD::ADD only has one
11201 // result, so we can assume any node match is using the result.
11202 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11203 NewLocOps[i].getSDNode() != &N)
11204 continue;
11205 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11206 if (RHSConstant) {
11209 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11210 } else {
11211 // Convert to a variadic expression (if not already).
11212 // convertToVariadicExpression() returns a const pointer, so we use
11213 // a temporary const variable here.
11214 const auto *TmpDIExpr =
11218 ExprOps.push_back(NewLocOps.size());
11219 ExprOps.push_back(dwarf::DW_OP_plus);
11222 NewLocOps.push_back(RHS);
11223 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11224 }
11225 Changed = true;
11226 }
11227 (void)Changed;
11228 assert(Changed && "Salvage target doesn't use N");
11229
11230 bool IsVariadic =
11231 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11232
11233 auto AdditionalDependencies = DV->getAdditionalDependencies();
11234 SDDbgValue *Clone = getDbgValueList(
11235 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
11236 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
11237 ClonedDVs.push_back(Clone);
11238 DV->setIsInvalidated();
11239 DV->setIsEmitted();
11240 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11241 N0.getNode()->dumprFull(this);
11242 dbgs() << " into " << *DIExpr << '\n');
11243 }
11244 break;
11245 }
11246 case ISD::TRUNCATE: {
11247 SDValue N0 = N.getOperand(0);
11248 TypeSize FromSize = N0.getValueSizeInBits();
11249 TypeSize ToSize = N.getValueSizeInBits(0);
11250
11251 DIExpression *DbgExpression = DV->getExpression();
11252 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11253 auto NewLocOps = DV->copyLocationOps();
11254 bool Changed = false;
11255 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11256 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11257 NewLocOps[i].getSDNode() != &N)
11258 continue;
11259
11260 NewLocOps[i] = SDDbgOperand::fromNode(N0.getNode(), N0.getResNo());
11261 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11262 Changed = true;
11263 }
11264 assert(Changed && "Salvage target doesn't use N");
11265 (void)Changed;
11266
11267 SDDbgValue *Clone =
11268 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11269 DV->getAdditionalDependencies(), DV->isIndirect(),
11270 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11271
11272 ClonedDVs.push_back(Clone);
11273 DV->setIsInvalidated();
11274 DV->setIsEmitted();
11275 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11276 dbgs() << " into " << *DbgExpression << '\n');
11277 break;
11278 }
11279 }
11280 }
11281
11282 for (SDDbgValue *Dbg : ClonedDVs) {
11283 assert(!Dbg->getSDNodes().empty() &&
11284 "Salvaged DbgValue should depend on a new SDNode");
11285 AddDbgValue(Dbg, false);
11286 }
11287}
11288
11289/// Creates a SDDbgLabel node.
11291 const DebugLoc &DL, unsigned O) {
11292 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11293 "Expected inlined-at fields to agree");
11294 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11295}
11296
11297namespace {
11298
11299/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11300/// pointed to by a use iterator is deleted, increment the use iterator
11301/// so that it doesn't dangle.
11302///
11303class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11306
11307 void NodeDeleted(SDNode *N, SDNode *E) override {
11308 // Increment the iterator as needed.
11309 while (UI != UE && N == *UI)
11310 ++UI;
11311 }
11312
11313public:
11314 RAUWUpdateListener(SelectionDAG &d,
11317 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11318};
11319
11320} // end anonymous namespace
11321
11322/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11323/// This can cause recursive merging of nodes in the DAG.
11324///
11325/// This version assumes From has a single result value.
11326///
11328 SDNode *From = FromN.getNode();
11329 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11330 "Cannot replace with this method!");
11331 assert(From != To.getNode() && "Cannot replace uses of with self");
11332
11333 // Preserve Debug Values
11334 transferDbgValues(FromN, To);
11335 // Preserve extra info.
11336 copyExtraInfo(From, To.getNode());
11337
11338 // Iterate over all the existing uses of From. New uses will be added
11339 // to the beginning of the use list, which we avoid visiting.
11340 // This specifically avoids visiting uses of From that arise while the
11341 // replacement is happening, because any such uses would be the result
11342 // of CSE: If an existing node looks like From after one of its operands
11343 // is replaced by To, we don't want to replace of all its users with To
11344 // too. See PR3018 for more info.
11345 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11346 RAUWUpdateListener Listener(*this, UI, UE);
11347 while (UI != UE) {
11348 SDNode *User = *UI;
11349
11350 // This node is about to morph, remove its old self from the CSE maps.
11351 RemoveNodeFromCSEMaps(User);
11352
11353 // A user can appear in a use list multiple times, and when this
11354 // happens the uses are usually next to each other in the list.
11355 // To help reduce the number of CSE recomputations, process all
11356 // the uses of this user that we can find this way.
11357 do {
11358 SDUse &Use = UI.getUse();
11359 ++UI;
11360 Use.set(To);
11361 if (To->isDivergent() != From->isDivergent())
11363 } while (UI != UE && *UI == User);
11364 // Now that we have modified User, add it back to the CSE maps. If it
11365 // already exists there, recursively merge the results together.
11366 AddModifiedNodeToCSEMaps(User);
11367 }
11368
11369 // If we just RAUW'd the root, take note.
11370 if (FromN == getRoot())
11371 setRoot(To);
11372}
11373
11374/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11375/// This can cause recursive merging of nodes in the DAG.
11376///
11377/// This version assumes that for each value of From, there is a
11378/// corresponding value in To in the same position with the same type.
11379///
11381#ifndef NDEBUG
11382 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11383 assert((!From->hasAnyUseOfValue(i) ||
11384 From->getValueType(i) == To->getValueType(i)) &&
11385 "Cannot use this version of ReplaceAllUsesWith!");
11386#endif
11387
11388 // Handle the trivial case.
11389 if (From == To)
11390 return;
11391
11392 // Preserve Debug Info. Only do this if there's a use.
11393 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11394 if (From->hasAnyUseOfValue(i)) {
11395 assert((i < To->getNumValues()) && "Invalid To location");
11397 }
11398 // Preserve extra info.
11399 copyExtraInfo(From, To);
11400
11401 // Iterate over just the existing users of From. See the comments in
11402 // the ReplaceAllUsesWith above.
11403 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11404 RAUWUpdateListener Listener(*this, UI, UE);
11405 while (UI != UE) {
11406 SDNode *User = *UI;
11407
11408 // This node is about to morph, remove its old self from the CSE maps.
11409 RemoveNodeFromCSEMaps(User);
11410
11411 // A user can appear in a use list multiple times, and when this
11412 // happens the uses are usually next to each other in the list.
11413 // To help reduce the number of CSE recomputations, process all
11414 // the uses of this user that we can find this way.
11415 do {
11416 SDUse &Use = UI.getUse();
11417 ++UI;
11418 Use.setNode(To);
11419 if (To->isDivergent() != From->isDivergent())
11421 } while (UI != UE && *UI == User);
11422
11423 // Now that we have modified User, add it back to the CSE maps. If it
11424 // already exists there, recursively merge the results together.
11425 AddModifiedNodeToCSEMaps(User);
11426 }
11427
11428 // If we just RAUW'd the root, take note.
11429 if (From == getRoot().getNode())
11430 setRoot(SDValue(To, getRoot().getResNo()));
11431}
11432
11433/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11434/// This can cause recursive merging of nodes in the DAG.
11435///
11436/// This version can replace From with any result values. To must match the
11437/// number and types of values returned by From.
11439 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11440 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11441
11442 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11443 // Preserve Debug Info.
11444 transferDbgValues(SDValue(From, i), To[i]);
11445 // Preserve extra info.
11446 copyExtraInfo(From, To[i].getNode());
11447 }
11448
11449 // Iterate over just the existing users of From. See the comments in
11450 // the ReplaceAllUsesWith above.
11451 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11452 RAUWUpdateListener Listener(*this, UI, UE);
11453 while (UI != UE) {
11454 SDNode *User = *UI;
11455
11456 // This node is about to morph, remove its old self from the CSE maps.
11457 RemoveNodeFromCSEMaps(User);
11458
11459 // A user can appear in a use list multiple times, and when this happens the
11460 // uses are usually next to each other in the list. To help reduce the
11461 // number of CSE and divergence recomputations, process all the uses of this
11462 // user that we can find this way.
11463 bool To_IsDivergent = false;
11464 do {
11465 SDUse &Use = UI.getUse();
11466 const SDValue &ToOp = To[Use.getResNo()];
11467 ++UI;
11468 Use.set(ToOp);
11469 To_IsDivergent |= ToOp->isDivergent();
11470 } while (UI != UE && *UI == User);
11471
11472 if (To_IsDivergent != From->isDivergent())
11474
11475 // Now that we have modified User, add it back to the CSE maps. If it
11476 // already exists there, recursively merge the results together.
11477 AddModifiedNodeToCSEMaps(User);
11478 }
11479
11480 // If we just RAUW'd the root, take note.
11481 if (From == getRoot().getNode())
11482 setRoot(SDValue(To[getRoot().getResNo()]));
11483}
11484
11485/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11486/// uses of other values produced by From.getNode() alone. The Deleted
11487/// vector is handled the same way as for ReplaceAllUsesWith.
11489 // Handle the really simple, really trivial case efficiently.
11490 if (From == To) return;
11491
11492 // Handle the simple, trivial, case efficiently.
11493 if (From.getNode()->getNumValues() == 1) {
11495 return;
11496 }
11497
11498 // Preserve Debug Info.
11500 copyExtraInfo(From.getNode(), To.getNode());
11501
11502 // Iterate over just the existing users of From. See the comments in
11503 // the ReplaceAllUsesWith above.
11504 SDNode::use_iterator UI = From.getNode()->use_begin(),
11505 UE = From.getNode()->use_end();
11506 RAUWUpdateListener Listener(*this, UI, UE);
11507 while (UI != UE) {
11508 SDNode *User = *UI;
11509 bool UserRemovedFromCSEMaps = false;
11510
11511 // A user can appear in a use list multiple times, and when this
11512 // happens the uses are usually next to each other in the list.
11513 // To help reduce the number of CSE recomputations, process all
11514 // the uses of this user that we can find this way.
11515 do {
11516 SDUse &Use = UI.getUse();
11517
11518 // Skip uses of different values from the same node.
11519 if (Use.getResNo() != From.getResNo()) {
11520 ++UI;
11521 continue;
11522 }
11523
11524 // If this node hasn't been modified yet, it's still in the CSE maps,
11525 // so remove its old self from the CSE maps.
11526 if (!UserRemovedFromCSEMaps) {
11527 RemoveNodeFromCSEMaps(User);
11528 UserRemovedFromCSEMaps = true;
11529 }
11530
11531 ++UI;
11532 Use.set(To);
11533 if (To->isDivergent() != From->isDivergent())
11535 } while (UI != UE && *UI == User);
11536 // We are iterating over all uses of the From node, so if a use
11537 // doesn't use the specific value, no changes are made.
11538 if (!UserRemovedFromCSEMaps)
11539 continue;
11540
11541 // Now that we have modified User, add it back to the CSE maps. If it
11542 // already exists there, recursively merge the results together.
11543 AddModifiedNodeToCSEMaps(User);
11544 }
11545
11546 // If we just RAUW'd the root, take note.
11547 if (From == getRoot())
11548 setRoot(To);
11549}
11550
11551namespace {
11552
11553/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11554/// to record information about a use.
11555struct UseMemo {
11556 SDNode *User;
11557 unsigned Index;
11558 SDUse *Use;
11559};
11560
11561/// operator< - Sort Memos by User.
11562bool operator<(const UseMemo &L, const UseMemo &R) {
11563 return (intptr_t)L.User < (intptr_t)R.User;
11564}
11565
11566/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11567/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11568/// the node already has been taken care of recursively.
11569class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11571
11572 void NodeDeleted(SDNode *N, SDNode *E) override {
11573 for (UseMemo &Memo : Uses)
11574 if (Memo.User == N)
11575 Memo.User = nullptr;
11576 }
11577
11578public:
11579 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11580 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11581};
11582
11583} // end anonymous namespace
11584
11586 if (TLI->isSDNodeAlwaysUniform(N)) {
11587 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11588 "Conflicting divergence information!");
11589 return false;
11590 }
11591 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11592 return true;
11593 for (const auto &Op : N->ops()) {
11594 if (Op.Val.getValueType() != MVT::Other && Op.getNode()->isDivergent())
11595 return true;
11596 }
11597 return false;
11598}
11599
11601 SmallVector<SDNode *, 16> Worklist(1, N);
11602 do {
11603 N = Worklist.pop_back_val();
11604 bool IsDivergent = calculateDivergence(N);
11605 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11606 N->SDNodeBits.IsDivergent = IsDivergent;
11607 llvm::append_range(Worklist, N->uses());
11608 }
11609 } while (!Worklist.empty());
11610}
11611
11612void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11614 Order.reserve(AllNodes.size());
11615 for (auto &N : allnodes()) {
11616 unsigned NOps = N.getNumOperands();
11617 Degree[&N] = NOps;
11618 if (0 == NOps)
11619 Order.push_back(&N);
11620 }
11621 for (size_t I = 0; I != Order.size(); ++I) {
11622 SDNode *N = Order[I];
11623 for (auto *U : N->uses()) {
11624 unsigned &UnsortedOps = Degree[U];
11625 if (0 == --UnsortedOps)
11626 Order.push_back(U);
11627 }
11628 }
11629}
11630
11631#ifndef NDEBUG
11633 std::vector<SDNode *> TopoOrder;
11634 CreateTopologicalOrder(TopoOrder);
11635 for (auto *N : TopoOrder) {
11636 assert(calculateDivergence(N) == N->isDivergent() &&
11637 "Divergence bit inconsistency detected");
11638 }
11639}
11640#endif
11641
11642/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11643/// uses of other values produced by From.getNode() alone. The same value
11644/// may appear in both the From and To list. The Deleted vector is
11645/// handled the same way as for ReplaceAllUsesWith.
11647 const SDValue *To,
11648 unsigned Num){
11649 // Handle the simple, trivial case efficiently.
11650 if (Num == 1)
11651 return ReplaceAllUsesOfValueWith(*From, *To);
11652
11653 transferDbgValues(*From, *To);
11654 copyExtraInfo(From->getNode(), To->getNode());
11655
11656 // Read up all the uses and make records of them. This helps
11657 // processing new uses that are introduced during the
11658 // replacement process.
11660 for (unsigned i = 0; i != Num; ++i) {
11661 unsigned FromResNo = From[i].getResNo();
11662 SDNode *FromNode = From[i].getNode();
11663 for (SDNode::use_iterator UI = FromNode->use_begin(),
11664 E = FromNode->use_end(); UI != E; ++UI) {
11665 SDUse &Use = UI.getUse();
11666 if (Use.getResNo() == FromResNo) {
11667 UseMemo Memo = { *UI, i, &Use };
11668 Uses.push_back(Memo);
11669 }
11670 }
11671 }
11672
11673 // Sort the uses, so that all the uses from a given User are together.
11675 RAUOVWUpdateListener Listener(*this, Uses);
11676
11677 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11678 UseIndex != UseIndexEnd; ) {
11679 // We know that this user uses some value of From. If it is the right
11680 // value, update it.
11681 SDNode *User = Uses[UseIndex].User;
11682 // If the node has been deleted by recursive CSE updates when updating
11683 // another node, then just skip this entry.
11684 if (User == nullptr) {
11685 ++UseIndex;
11686 continue;
11687 }
11688
11689 // This node is about to morph, remove its old self from the CSE maps.
11690 RemoveNodeFromCSEMaps(User);
11691
11692 // The Uses array is sorted, so all the uses for a given User
11693 // are next to each other in the list.
11694 // To help reduce the number of CSE recomputations, process all
11695 // the uses of this user that we can find this way.
11696 do {
11697 unsigned i = Uses[UseIndex].Index;
11698 SDUse &Use = *Uses[UseIndex].Use;
11699 ++UseIndex;
11700
11701 Use.set(To[i]);
11702 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
11703
11704 // Now that we have modified User, add it back to the CSE maps. If it
11705 // already exists there, recursively merge the results together.
11706 AddModifiedNodeToCSEMaps(User);
11707 }
11708}
11709
11710/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
11711/// based on their topological order. It returns the maximum id and a vector
11712/// of the SDNodes* in assigned order by reference.
11714 unsigned DAGSize = 0;
11715
11716 // SortedPos tracks the progress of the algorithm. Nodes before it are
11717 // sorted, nodes after it are unsorted. When the algorithm completes
11718 // it is at the end of the list.
11719 allnodes_iterator SortedPos = allnodes_begin();
11720
11721 // Visit all the nodes. Move nodes with no operands to the front of
11722 // the list immediately. Annotate nodes that do have operands with their
11723 // operand count. Before we do this, the Node Id fields of the nodes
11724 // may contain arbitrary values. After, the Node Id fields for nodes
11725 // before SortedPos will contain the topological sort index, and the
11726 // Node Id fields for nodes At SortedPos and after will contain the
11727 // count of outstanding operands.
11729 checkForCycles(&N, this);
11730 unsigned Degree = N.getNumOperands();
11731 if (Degree == 0) {
11732 // A node with no uses, add it to the result array immediately.
11733 N.setNodeId(DAGSize++);
11734 allnodes_iterator Q(&N);
11735 if (Q != SortedPos)
11736 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
11737 assert(SortedPos != AllNodes.end() && "Overran node list");
11738 ++SortedPos;
11739 } else {
11740 // Temporarily use the Node Id as scratch space for the degree count.
11741 N.setNodeId(Degree);
11742 }
11743 }
11744
11745 // Visit all the nodes. As we iterate, move nodes into sorted order,
11746 // such that by the time the end is reached all nodes will be sorted.
11747 for (SDNode &Node : allnodes()) {
11748 SDNode *N = &Node;
11749 checkForCycles(N, this);
11750 // N is in sorted position, so all its uses have one less operand
11751 // that needs to be sorted.
11752 for (SDNode *P : N->uses()) {
11753 unsigned Degree = P->getNodeId();
11754 assert(Degree != 0 && "Invalid node degree");
11755 --Degree;
11756 if (Degree == 0) {
11757 // All of P's operands are sorted, so P may sorted now.
11758 P->setNodeId(DAGSize++);
11759 if (P->getIterator() != SortedPos)
11760 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
11761 assert(SortedPos != AllNodes.end() && "Overran node list");
11762 ++SortedPos;
11763 } else {
11764 // Update P's outstanding operand count.
11765 P->setNodeId(Degree);
11766 }
11767 }
11768 if (Node.getIterator() == SortedPos) {
11769#ifndef NDEBUG
11771 SDNode *S = &*++I;
11772 dbgs() << "Overran sorted position:\n";
11773 S->dumprFull(this); dbgs() << "\n";
11774 dbgs() << "Checking if this is due to cycles\n";
11775 checkForCycles(this, true);
11776#endif
11777 llvm_unreachable(nullptr);
11778 }
11779 }
11780
11781 assert(SortedPos == AllNodes.end() &&
11782 "Topological sort incomplete!");
11783 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
11784 "First node in topological sort is not the entry token!");
11785 assert(AllNodes.front().getNodeId() == 0 &&
11786 "First node in topological sort has non-zero id!");
11787 assert(AllNodes.front().getNumOperands() == 0 &&
11788 "First node in topological sort has operands!");
11789 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
11790 "Last node in topologic sort has unexpected id!");
11791 assert(AllNodes.back().use_empty() &&
11792 "Last node in topologic sort has users!");
11793 assert(DAGSize == allnodes_size() && "Node count mismatch!");
11794 return DAGSize;
11795}
11796
11797/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
11798/// value is produced by SD.
11799void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
11800 for (SDNode *SD : DB->getSDNodes()) {
11801 if (!SD)
11802 continue;
11803 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
11804 SD->setHasDebugValue(true);
11805 }
11806 DbgInfo->add(DB, isParameter);
11807}
11808
11809void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
11810
11812 SDValue NewMemOpChain) {
11813 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
11814 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
11815 // The new memory operation must have the same position as the old load in
11816 // terms of memory dependency. Create a TokenFactor for the old load and new
11817 // memory operation and update uses of the old load's output chain to use that
11818 // TokenFactor.
11819 if (OldChain == NewMemOpChain || OldChain.use_empty())
11820 return NewMemOpChain;
11821
11822 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
11823 OldChain, NewMemOpChain);
11824 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
11825 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
11826 return TokenFactor;
11827}
11828
11830 SDValue NewMemOp) {
11831 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
11832 SDValue OldChain = SDValue(OldLoad, 1);
11833 SDValue NewMemOpChain = NewMemOp.getValue(1);
11834 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
11835}
11836
11838 Function **OutFunction) {
11839 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
11840
11841 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
11842 auto *Module = MF->getFunction().getParent();
11843 auto *Function = Module->getFunction(Symbol);
11844
11845 if (OutFunction != nullptr)
11846 *OutFunction = Function;
11847
11848 if (Function != nullptr) {
11849 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
11850 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
11851 }
11852
11853 std::string ErrorStr;
11854 raw_string_ostream ErrorFormatter(ErrorStr);
11855 ErrorFormatter << "Undefined external symbol ";
11856 ErrorFormatter << '"' << Symbol << '"';
11857 report_fatal_error(Twine(ErrorStr));
11858}
11859
11860//===----------------------------------------------------------------------===//
11861// SDNode Class
11862//===----------------------------------------------------------------------===//
11863
11865 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11866 return Const != nullptr && Const->isZero();
11867}
11868
11870 return V.isUndef() || isNullConstant(V);
11871}
11872
11874 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
11875 return Const != nullptr && Const->isZero() && !Const->isNegative();
11876}
11877
11879 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11880 return Const != nullptr && Const->isAllOnes();
11881}
11882
11884 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11885 return Const != nullptr && Const->isOne();
11886}
11887
11889 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
11890 return Const != nullptr && Const->isMinSignedValue();
11891}
11892
11893bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
11894 unsigned OperandNo) {
11895 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
11896 // TODO: Target-specific opcodes could be added.
11897 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
11898 /*AllowTruncation*/ true)) {
11899 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
11900 switch (Opcode) {
11901 case ISD::ADD:
11902 case ISD::OR:
11903 case ISD::XOR:
11904 case ISD::UMAX:
11905 return Const.isZero();
11906 case ISD::MUL:
11907 return Const.isOne();
11908 case ISD::AND:
11909 case ISD::UMIN:
11910 return Const.isAllOnes();
11911 case ISD::SMAX:
11912 return Const.isMinSignedValue();
11913 case ISD::SMIN:
11914 return Const.isMaxSignedValue();
11915 case ISD::SUB:
11916 case ISD::SHL:
11917 case ISD::SRA:
11918 case ISD::SRL:
11919 return OperandNo == 1 && Const.isZero();
11920 case ISD::UDIV:
11921 case ISD::SDIV:
11922 return OperandNo == 1 && Const.isOne();
11923 }
11924 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
11925 switch (Opcode) {
11926 case ISD::FADD:
11927 return ConstFP->isZero() &&
11928 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
11929 case ISD::FSUB:
11930 return OperandNo == 1 && ConstFP->isZero() &&
11931 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
11932 case ISD::FMUL:
11933 return ConstFP->isExactlyValue(1.0);
11934 case ISD::FDIV:
11935 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
11936 case ISD::FMINNUM:
11937 case ISD::FMAXNUM: {
11938 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
11939 EVT VT = V.getValueType();
11941 APFloat NeutralAF = !Flags.hasNoNaNs()
11942 ? APFloat::getQNaN(Semantics)
11943 : !Flags.hasNoInfs()
11944 ? APFloat::getInf(Semantics)
11945 : APFloat::getLargest(Semantics);
11946 if (Opcode == ISD::FMAXNUM)
11947 NeutralAF.changeSign();
11948
11949 return ConstFP->isExactlyValue(NeutralAF);
11950 }
11951 }
11952 }
11953 return false;
11954}
11955
11957 while (V.getOpcode() == ISD::BITCAST)
11958 V = V.getOperand(0);
11959 return V;
11960}
11961
11963 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
11964 V = V.getOperand(0);
11965 return V;
11966}
11967
11969 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
11970 V = V.getOperand(0);
11971 return V;
11972}
11973
11975 while (V.getOpcode() == ISD::TRUNCATE)
11976 V = V.getOperand(0);
11977 return V;
11978}
11979
11980bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
11981 if (V.getOpcode() != ISD::XOR)
11982 return false;
11983 V = peekThroughBitcasts(V.getOperand(1));
11984 unsigned NumBits = V.getScalarValueSizeInBits();
11985 ConstantSDNode *C =
11986 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
11987 return C && (C->getAPIntValue().countr_one() >= NumBits);
11988}
11989
11991 bool AllowTruncation) {
11992 EVT VT = N.getValueType();
11993 APInt DemandedElts = VT.isFixedLengthVector()
11995 : APInt(1, 1);
11996 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
11997}
11998
12000 bool AllowUndefs,
12001 bool AllowTruncation) {
12002 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
12003 return CN;
12004
12005 // SplatVectors can truncate their operands. Ignore that case here unless
12006 // AllowTruncation is set.
12007 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12008 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12009 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12010 EVT CVT = CN->getValueType(0);
12011 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12012 if (AllowTruncation || CVT == VecEltVT)
12013 return CN;
12014 }
12015 }
12016
12017 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12018 BitVector UndefElements;
12019 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12020
12021 // BuildVectors can truncate their operands. Ignore that case here unless
12022 // AllowTruncation is set.
12023 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12024 if (CN && (UndefElements.none() || AllowUndefs)) {
12025 EVT CVT = CN->getValueType(0);
12026 EVT NSVT = N.getValueType().getScalarType();
12027 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12028 if (AllowTruncation || (CVT == NSVT))
12029 return CN;
12030 }
12031 }
12032
12033 return nullptr;
12034}
12035
12037 EVT VT = N.getValueType();
12038 APInt DemandedElts = VT.isFixedLengthVector()
12040 : APInt(1, 1);
12041 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12042}
12043
12045 const APInt &DemandedElts,
12046 bool AllowUndefs) {
12047 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
12048 return CN;
12049
12050 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12051 BitVector UndefElements;
12052 ConstantFPSDNode *CN =
12053 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12054 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12055 if (CN && (UndefElements.none() || AllowUndefs))
12056 return CN;
12057 }
12058
12059 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12060 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12061 return CN;
12062
12063 return nullptr;
12064}
12065
12066bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12067 // TODO: may want to use peekThroughBitcast() here.
12068 ConstantSDNode *C =
12069 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12070 return C && C->isZero();
12071}
12072
12073bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12074 ConstantSDNode *C =
12075 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12076 return C && C->isOne();
12077}
12078
12079bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12081 unsigned BitWidth = N.getScalarValueSizeInBits();
12082 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12083 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12084}
12085
12087 DropOperands();
12088}
12089
12090MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12091 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12092 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12093 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12094 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12095 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12096 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12097
12098 // We check here that the size of the memory operand fits within the size of
12099 // the MMO. This is because the MMO might indicate only a possible address
12100 // range instead of specifying the affected memory addresses precisely.
12101 assert(
12102 (!MMO->getType().isValid() ||
12104 "Size mismatch!");
12105}
12106
12107/// Profile - Gather unique data for the node.
12108///
12110 AddNodeIDNode(ID, this);
12111}
12112
12113namespace {
12114
12115 struct EVTArray {
12116 std::vector<EVT> VTs;
12117
12118 EVTArray() {
12119 VTs.reserve(MVT::VALUETYPE_SIZE);
12120 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12121 VTs.push_back(MVT((MVT::SimpleValueType)i));
12122 }
12123 };
12124
12125} // end anonymous namespace
12126
12127/// getValueTypeList - Return a pointer to the specified value type.
12128///
12129const EVT *SDNode::getValueTypeList(EVT VT) {
12130 static std::set<EVT, EVT::compareRawBits> EVTs;
12131 static EVTArray SimpleVTArray;
12132 static sys::SmartMutex<true> VTMutex;
12133
12134 if (VT.isExtended()) {
12135 sys::SmartScopedLock<true> Lock(VTMutex);
12136 return &(*EVTs.insert(VT).first);
12137 }
12138 assert(VT.getSimpleVT() < MVT::VALUETYPE_SIZE && "Value type out of range!");
12139 return &SimpleVTArray.VTs[VT.getSimpleVT().SimpleTy];
12140}
12141
12142/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
12143/// indicated value. This method ignores uses of other values defined by this
12144/// operation.
12145bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
12146 assert(Value < getNumValues() && "Bad value!");
12147
12148 // TODO: Only iterate over uses of a given value of the node
12149 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI) {
12150 if (UI.getUse().getResNo() == Value) {
12151 if (NUses == 0)
12152 return false;
12153 --NUses;
12154 }
12155 }
12156
12157 // Found exactly the right number of uses?
12158 return NUses == 0;
12159}
12160
12161/// hasAnyUseOfValue - Return true if there are any use of the indicated
12162/// value. This method ignores uses of other values defined by this operation.
12163bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12164 assert(Value < getNumValues() && "Bad value!");
12165
12166 for (SDNode::use_iterator UI = use_begin(), E = use_end(); UI != E; ++UI)
12167 if (UI.getUse().getResNo() == Value)
12168 return true;
12169
12170 return false;
12171}
12172
12173/// isOnlyUserOf - Return true if this node is the only use of N.
12174bool SDNode::isOnlyUserOf(const SDNode *N) const {
12175 bool Seen = false;
12176 for (const SDNode *User : N->uses()) {
12177 if (User == this)
12178 Seen = true;
12179 else
12180 return false;
12181 }
12182
12183 return Seen;
12184}
12185
12186/// Return true if the only users of N are contained in Nodes.
12188 bool Seen = false;
12189 for (const SDNode *User : N->uses()) {
12190 if (llvm::is_contained(Nodes, User))
12191 Seen = true;
12192 else
12193 return false;
12194 }
12195
12196 return Seen;
12197}
12198
12199/// isOperand - Return true if this node is an operand of N.
12200bool SDValue::isOperandOf(const SDNode *N) const {
12201 return is_contained(N->op_values(), *this);
12202}
12203
12204bool SDNode::isOperandOf(const SDNode *N) const {
12205 return any_of(N->op_values(),
12206 [this](SDValue Op) { return this == Op.getNode(); });
12207}
12208
12209/// reachesChainWithoutSideEffects - Return true if this operand (which must
12210/// be a chain) reaches the specified operand without crossing any
12211/// side-effecting instructions on any chain path. In practice, this looks
12212/// through token factors and non-volatile loads. In order to remain efficient,
12213/// this only looks a couple of nodes in, it does not do an exhaustive search.
12214///
12215/// Note that we only need to examine chains when we're searching for
12216/// side-effects; SelectionDAG requires that all side-effects are represented
12217/// by chains, even if another operand would force a specific ordering. This
12218/// constraint is necessary to allow transformations like splitting loads.
12220 unsigned Depth) const {
12221 if (*this == Dest) return true;
12222
12223 // Don't search too deeply, we just want to be able to see through
12224 // TokenFactor's etc.
12225 if (Depth == 0) return false;
12226
12227 // If this is a token factor, all inputs to the TF happen in parallel.
12228 if (getOpcode() == ISD::TokenFactor) {
12229 // First, try a shallow search.
12230 if (is_contained((*this)->ops(), Dest)) {
12231 // We found the chain we want as an operand of this TokenFactor.
12232 // Essentially, we reach the chain without side-effects if we could
12233 // serialize the TokenFactor into a simple chain of operations with
12234 // Dest as the last operation. This is automatically true if the
12235 // chain has one use: there are no other ordering constraints.
12236 // If the chain has more than one use, we give up: some other
12237 // use of Dest might force a side-effect between Dest and the current
12238 // node.
12239 if (Dest.hasOneUse())
12240 return true;
12241 }
12242 // Next, try a deep search: check whether every operand of the TokenFactor
12243 // reaches Dest.
12244 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12245 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12246 });
12247 }
12248
12249 // Loads don't have side effects, look through them.
12250 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12251 if (Ld->isUnordered())
12252 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12253 }
12254 return false;
12255}
12256
12257bool SDNode::hasPredecessor(const SDNode *N) const {
12260 Worklist.push_back(this);
12261 return hasPredecessorHelper(N, Visited, Worklist);
12262}
12263
12265 this->Flags.intersectWith(Flags);
12266}
12267
12268SDValue
12270 ArrayRef<ISD::NodeType> CandidateBinOps,
12271 bool AllowPartials) {
12272 // The pattern must end in an extract from index 0.
12273 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12274 !isNullConstant(Extract->getOperand(1)))
12275 return SDValue();
12276
12277 // Match against one of the candidate binary ops.
12278 SDValue Op = Extract->getOperand(0);
12279 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12280 return Op.getOpcode() == unsigned(BinOp);
12281 }))
12282 return SDValue();
12283
12284 // Floating-point reductions may require relaxed constraints on the final step
12285 // of the reduction because they may reorder intermediate operations.
12286 unsigned CandidateBinOp = Op.getOpcode();
12287 if (Op.getValueType().isFloatingPoint()) {
12288 SDNodeFlags Flags = Op->getFlags();
12289 switch (CandidateBinOp) {
12290 case ISD::FADD:
12291 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12292 return SDValue();
12293 break;
12294 default:
12295 llvm_unreachable("Unhandled FP opcode for binop reduction");
12296 }
12297 }
12298
12299 // Matching failed - attempt to see if we did enough stages that a partial
12300 // reduction from a subvector is possible.
12301 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12302 if (!AllowPartials || !Op)
12303 return SDValue();
12304 EVT OpVT = Op.getValueType();
12305 EVT OpSVT = OpVT.getScalarType();
12306 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12307 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12308 return SDValue();
12309 BinOp = (ISD::NodeType)CandidateBinOp;
12310 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12312 };
12313
12314 // At each stage, we're looking for something that looks like:
12315 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12316 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12317 // i32 undef, i32 undef, i32 undef, i32 undef>
12318 // %a = binop <8 x i32> %op, %s
12319 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12320 // we expect something like:
12321 // <4,5,6,7,u,u,u,u>
12322 // <2,3,u,u,u,u,u,u>
12323 // <1,u,u,u,u,u,u,u>
12324 // While a partial reduction match would be:
12325 // <2,3,u,u,u,u,u,u>
12326 // <1,u,u,u,u,u,u,u>
12327 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12328 SDValue PrevOp;
12329 for (unsigned i = 0; i < Stages; ++i) {
12330 unsigned MaskEnd = (1 << i);
12331
12332 if (Op.getOpcode() != CandidateBinOp)
12333 return PartialReduction(PrevOp, MaskEnd);
12334
12335 SDValue Op0 = Op.getOperand(0);
12336 SDValue Op1 = Op.getOperand(1);
12337
12338 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12339 if (Shuffle) {
12340 Op = Op1;
12341 } else {
12342 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12343 Op = Op0;
12344 }
12345
12346 // The first operand of the shuffle should be the same as the other operand
12347 // of the binop.
12348 if (!Shuffle || Shuffle->getOperand(0) != Op)
12349 return PartialReduction(PrevOp, MaskEnd);
12350
12351 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12352 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12353 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12354 return PartialReduction(PrevOp, MaskEnd);
12355
12356 PrevOp = Op;
12357 }
12358
12359 // Handle subvector reductions, which tend to appear after the shuffle
12360 // reduction stages.
12361 while (Op.getOpcode() == CandidateBinOp) {
12362 unsigned NumElts = Op.getValueType().getVectorNumElements();
12363 SDValue Op0 = Op.getOperand(0);
12364 SDValue Op1 = Op.getOperand(1);
12365 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12367 Op0.getOperand(0) != Op1.getOperand(0))
12368 break;
12369 SDValue Src = Op0.getOperand(0);
12370 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12371 if (NumSrcElts != (2 * NumElts))
12372 break;
12373 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12374 Op1.getConstantOperandAPInt(1) == NumElts) &&
12375 !(Op1.getConstantOperandAPInt(1) == 0 &&
12376 Op0.getConstantOperandAPInt(1) == NumElts))
12377 break;
12378 Op = Src;
12379 }
12380
12381 BinOp = (ISD::NodeType)CandidateBinOp;
12382 return Op;
12383}
12384
12386 EVT VT = N->getValueType(0);
12387 EVT EltVT = VT.getVectorElementType();
12388 unsigned NE = VT.getVectorNumElements();
12389
12390 SDLoc dl(N);
12391
12392 // If ResNE is 0, fully unroll the vector op.
12393 if (ResNE == 0)
12394 ResNE = NE;
12395 else if (NE > ResNE)
12396 NE = ResNE;
12397
12398 if (N->getNumValues() == 2) {
12399 SmallVector<SDValue, 8> Scalars0, Scalars1;
12400 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12401 EVT VT1 = N->getValueType(1);
12402 EVT EltVT1 = VT1.getVectorElementType();
12403
12404 unsigned i;
12405 for (i = 0; i != NE; ++i) {
12406 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12407 SDValue Operand = N->getOperand(j);
12408 EVT OperandVT = Operand.getValueType();
12409
12410 // A vector operand; extract a single element.
12411 EVT OperandEltVT = OperandVT.getVectorElementType();
12412 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12413 Operand, getVectorIdxConstant(i, dl));
12414 }
12415
12416 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12417 Scalars0.push_back(EltOp);
12418 Scalars1.push_back(EltOp.getValue(1));
12419 }
12420
12421 SDValue Vec0 = getBuildVector(VT, dl, Scalars0);
12422 SDValue Vec1 = getBuildVector(VT1, dl, Scalars1);
12423 return getMergeValues({Vec0, Vec1}, dl);
12424 }
12425
12426 assert(N->getNumValues() == 1 &&
12427 "Can't unroll a vector with multiple results!");
12428
12430 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12431
12432 unsigned i;
12433 for (i= 0; i != NE; ++i) {
12434 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12435 SDValue Operand = N->getOperand(j);
12436 EVT OperandVT = Operand.getValueType();
12437 if (OperandVT.isVector()) {
12438 // A vector operand; extract a single element.
12439 EVT OperandEltVT = OperandVT.getVectorElementType();
12440 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12441 Operand, getVectorIdxConstant(i, dl));
12442 } else {
12443 // A scalar operand; just use it as is.
12444 Operands[j] = Operand;
12445 }
12446 }
12447
12448 switch (N->getOpcode()) {
12449 default: {
12450 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12451 N->getFlags()));
12452 break;
12453 }
12454 case ISD::VSELECT:
12455 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12456 break;
12457 case ISD::SHL:
12458 case ISD::SRA:
12459 case ISD::SRL:
12460 case ISD::ROTL:
12461 case ISD::ROTR:
12462 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12464 Operands[1])));
12465 break;
12467 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12468 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12469 Operands[0],
12470 getValueType(ExtVT)));
12471 }
12472 }
12473 }
12474
12475 for (; i < ResNE; ++i)
12476 Scalars.push_back(getUNDEF(EltVT));
12477
12478 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12479 return getBuildVector(VecVT, dl, Scalars);
12480}
12481
12482std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12483 SDNode *N, unsigned ResNE) {
12484 unsigned Opcode = N->getOpcode();
12485 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12486 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12487 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12488 "Expected an overflow opcode");
12489
12490 EVT ResVT = N->getValueType(0);
12491 EVT OvVT = N->getValueType(1);
12492 EVT ResEltVT = ResVT.getVectorElementType();
12493 EVT OvEltVT = OvVT.getVectorElementType();
12494 SDLoc dl(N);
12495
12496 // If ResNE is 0, fully unroll the vector op.
12497 unsigned NE = ResVT.getVectorNumElements();
12498 if (ResNE == 0)
12499 ResNE = NE;
12500 else if (NE > ResNE)
12501 NE = ResNE;
12502
12503 SmallVector<SDValue, 8> LHSScalars;
12504 SmallVector<SDValue, 8> RHSScalars;
12505 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12506 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12507
12508 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12509 SDVTList VTs = getVTList(ResEltVT, SVT);
12510 SmallVector<SDValue, 8> ResScalars;
12511 SmallVector<SDValue, 8> OvScalars;
12512 for (unsigned i = 0; i < NE; ++i) {
12513 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12514 SDValue Ov =
12515 getSelect(dl, OvEltVT, Res.getValue(1),
12516 getBoolConstant(true, dl, OvEltVT, ResVT),
12517 getConstant(0, dl, OvEltVT));
12518
12519 ResScalars.push_back(Res);
12520 OvScalars.push_back(Ov);
12521 }
12522
12523 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12524 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12525
12526 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12527 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12528 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12529 getBuildVector(NewOvVT, dl, OvScalars));
12530}
12531
12534 unsigned Bytes,
12535 int Dist) const {
12536 if (LD->isVolatile() || Base->isVolatile())
12537 return false;
12538 // TODO: probably too restrictive for atomics, revisit
12539 if (!LD->isSimple())
12540 return false;
12541 if (LD->isIndexed() || Base->isIndexed())
12542 return false;
12543 if (LD->getChain() != Base->getChain())
12544 return false;
12545 EVT VT = LD->getMemoryVT();
12546 if (VT.getSizeInBits() / 8 != Bytes)
12547 return false;
12548
12549 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12550 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12551
12552 int64_t Offset = 0;
12553 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12554 return (Dist * (int64_t)Bytes == Offset);
12555 return false;
12556}
12557
12558/// InferPtrAlignment - Infer alignment of a load / store address. Return
12559/// std::nullopt if it cannot be inferred.
12561 // If this is a GlobalAddress + cst, return the alignment.
12562 const GlobalValue *GV = nullptr;
12563 int64_t GVOffset = 0;
12564 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12565 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12566 KnownBits Known(PtrWidth);
12568 unsigned AlignBits = Known.countMinTrailingZeros();
12569 if (AlignBits)
12570 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12571 }
12572
12573 // If this is a direct reference to a stack slot, use information about the
12574 // stack slot's alignment.
12575 int FrameIdx = INT_MIN;
12576 int64_t FrameOffset = 0;
12577 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12578 FrameIdx = FI->getIndex();
12579 } else if (isBaseWithConstantOffset(Ptr) &&
12580 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12581 // Handle FI+Cst
12582 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12583 FrameOffset = Ptr.getConstantOperandVal(1);
12584 }
12585
12586 if (FrameIdx != INT_MIN) {
12588 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12589 }
12590
12591 return std::nullopt;
12592}
12593
12594/// Split the scalar node with EXTRACT_ELEMENT using the provided
12595/// VTs and return the low/high part.
12596std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12597 const SDLoc &DL,
12598 const EVT &LoVT,
12599 const EVT &HiVT) {
12600 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12601 "Split node must be a scalar type");
12602 SDValue Lo =
12604 SDValue Hi =
12606 return std::make_pair(Lo, Hi);
12607}
12608
12609/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12610/// which is split (or expanded) into two not necessarily identical pieces.
12611std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12612 // Currently all types are split in half.
12613 EVT LoVT, HiVT;
12614 if (!VT.isVector())
12615 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12616 else
12617 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12618
12619 return std::make_pair(LoVT, HiVT);
12620}
12621
12622/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12623/// type, dependent on an enveloping VT that has been split into two identical
12624/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12625std::pair<EVT, EVT>
12627 bool *HiIsEmpty) const {
12628 EVT EltTp = VT.getVectorElementType();
12629 // Examples:
12630 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12631 // custom VL=9 with enveloping VL=8/8 yields 8/1
12632 // custom VL=10 with enveloping VL=8/8 yields 8/2
12633 // etc.
12634 ElementCount VTNumElts = VT.getVectorElementCount();
12635 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12636 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12637 "Mixing fixed width and scalable vectors when enveloping a type");
12638 EVT LoVT, HiVT;
12639 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12640 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12641 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12642 *HiIsEmpty = false;
12643 } else {
12644 // Flag that hi type has zero storage size, but return split envelop type
12645 // (this would be easier if vector types with zero elements were allowed).
12646 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12647 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12648 *HiIsEmpty = true;
12649 }
12650 return std::make_pair(LoVT, HiVT);
12651}
12652
12653/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12654/// low/high part.
12655std::pair<SDValue, SDValue>
12656SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12657 const EVT &HiVT) {
12658 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12659 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12660 "Splitting vector with an invalid mixture of fixed and scalable "
12661 "vector types");
12663 N.getValueType().getVectorMinNumElements() &&
12664 "More vector elements requested than available!");
12665 SDValue Lo, Hi;
12666 Lo =
12668 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12669 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12670 // IDX with the runtime scaling factor of the result vector type. For
12671 // fixed-width result vectors, that runtime scaling factor is 1.
12674 return std::make_pair(Lo, Hi);
12675}
12676
12677std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
12678 const SDLoc &DL) {
12679 // Split the vector length parameter.
12680 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
12681 EVT VT = N.getValueType();
12683 "Expecting the mask to be an evenly-sized vector");
12684 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
12685 SDValue HalfNumElts =
12686 VecVT.isFixedLengthVector()
12687 ? getConstant(HalfMinNumElts, DL, VT)
12688 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
12689 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
12690 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
12691 return std::make_pair(Lo, Hi);
12692}
12693
12694/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
12696 EVT VT = N.getValueType();
12699 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
12701}
12702
12705 unsigned Start, unsigned Count,
12706 EVT EltVT) {
12707 EVT VT = Op.getValueType();
12708 if (Count == 0)
12709 Count = VT.getVectorNumElements();
12710 if (EltVT == EVT())
12711 EltVT = VT.getVectorElementType();
12712 SDLoc SL(Op);
12713 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
12714 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
12715 getVectorIdxConstant(i, SL)));
12716 }
12717}
12718
12719// getAddressSpace - Return the address space this GlobalAddress belongs to.
12721 return getGlobal()->getType()->getAddressSpace();
12722}
12723
12726 return Val.MachineCPVal->getType();
12727 return Val.ConstVal->getType();
12728}
12729
12730bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
12731 unsigned &SplatBitSize,
12732 bool &HasAnyUndefs,
12733 unsigned MinSplatBits,
12734 bool IsBigEndian) const {
12735 EVT VT = getValueType(0);
12736 assert(VT.isVector() && "Expected a vector type");
12737 unsigned VecWidth = VT.getSizeInBits();
12738 if (MinSplatBits > VecWidth)
12739 return false;
12740
12741 // FIXME: The widths are based on this node's type, but build vectors can
12742 // truncate their operands.
12743 SplatValue = APInt(VecWidth, 0);
12744 SplatUndef = APInt(VecWidth, 0);
12745
12746 // Get the bits. Bits with undefined values (when the corresponding element
12747 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
12748 // in SplatValue. If any of the values are not constant, give up and return
12749 // false.
12750 unsigned int NumOps = getNumOperands();
12751 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
12752 unsigned EltWidth = VT.getScalarSizeInBits();
12753
12754 for (unsigned j = 0; j < NumOps; ++j) {
12755 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
12756 SDValue OpVal = getOperand(i);
12757 unsigned BitPos = j * EltWidth;
12758
12759 if (OpVal.isUndef())
12760 SplatUndef.setBits(BitPos, BitPos + EltWidth);
12761 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
12762 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
12763 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
12764 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
12765 else
12766 return false;
12767 }
12768
12769 // The build_vector is all constants or undefs. Find the smallest element
12770 // size that splats the vector.
12771 HasAnyUndefs = (SplatUndef != 0);
12772
12773 // FIXME: This does not work for vectors with elements less than 8 bits.
12774 while (VecWidth > 8) {
12775 // If we can't split in half, stop here.
12776 if (VecWidth & 1)
12777 break;
12778
12779 unsigned HalfSize = VecWidth / 2;
12780 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
12781 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
12782 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
12783 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
12784
12785 // If the two halves do not match (ignoring undef bits), stop here.
12786 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
12787 MinSplatBits > HalfSize)
12788 break;
12789
12790 SplatValue = HighValue | LowValue;
12791 SplatUndef = HighUndef & LowUndef;
12792
12793 VecWidth = HalfSize;
12794 }
12795
12796 // FIXME: The loop above only tries to split in halves. But if the input
12797 // vector for example is <3 x i16> it wouldn't be able to detect a
12798 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
12799 // optimizations. I guess that back in the days when this helper was created
12800 // vectors normally was power-of-2 sized.
12801
12802 SplatBitSize = VecWidth;
12803 return true;
12804}
12805
12807 BitVector *UndefElements) const {
12808 unsigned NumOps = getNumOperands();
12809 if (UndefElements) {
12810 UndefElements->clear();
12811 UndefElements->resize(NumOps);
12812 }
12813 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12814 if (!DemandedElts)
12815 return SDValue();
12816 SDValue Splatted;
12817 for (unsigned i = 0; i != NumOps; ++i) {
12818 if (!DemandedElts[i])
12819 continue;
12820 SDValue Op = getOperand(i);
12821 if (Op.isUndef()) {
12822 if (UndefElements)
12823 (*UndefElements)[i] = true;
12824 } else if (!Splatted) {
12825 Splatted = Op;
12826 } else if (Splatted != Op) {
12827 return SDValue();
12828 }
12829 }
12830
12831 if (!Splatted) {
12832 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
12833 assert(getOperand(FirstDemandedIdx).isUndef() &&
12834 "Can only have a splat without a constant for all undefs.");
12835 return getOperand(FirstDemandedIdx);
12836 }
12837
12838 return Splatted;
12839}
12840
12842 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12843 return getSplatValue(DemandedElts, UndefElements);
12844}
12845
12847 SmallVectorImpl<SDValue> &Sequence,
12848 BitVector *UndefElements) const {
12849 unsigned NumOps = getNumOperands();
12850 Sequence.clear();
12851 if (UndefElements) {
12852 UndefElements->clear();
12853 UndefElements->resize(NumOps);
12854 }
12855 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
12856 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
12857 return false;
12858
12859 // Set the undefs even if we don't find a sequence (like getSplatValue).
12860 if (UndefElements)
12861 for (unsigned I = 0; I != NumOps; ++I)
12862 if (DemandedElts[I] && getOperand(I).isUndef())
12863 (*UndefElements)[I] = true;
12864
12865 // Iteratively widen the sequence length looking for repetitions.
12866 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
12867 Sequence.append(SeqLen, SDValue());
12868 for (unsigned I = 0; I != NumOps; ++I) {
12869 if (!DemandedElts[I])
12870 continue;
12871 SDValue &SeqOp = Sequence[I % SeqLen];
12873 if (Op.isUndef()) {
12874 if (!SeqOp)
12875 SeqOp = Op;
12876 continue;
12877 }
12878 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
12879 Sequence.clear();
12880 break;
12881 }
12882 SeqOp = Op;
12883 }
12884 if (!Sequence.empty())
12885 return true;
12886 }
12887
12888 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
12889 return false;
12890}
12891
12893 BitVector *UndefElements) const {
12894 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
12895 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
12896}
12897
12900 BitVector *UndefElements) const {
12901 return dyn_cast_or_null<ConstantSDNode>(
12902 getSplatValue(DemandedElts, UndefElements));
12903}
12904
12907 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
12908}
12909
12912 BitVector *UndefElements) const {
12913 return dyn_cast_or_null<ConstantFPSDNode>(
12914 getSplatValue(DemandedElts, UndefElements));
12915}
12916
12919 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
12920}
12921
12922int32_t
12924 uint32_t BitWidth) const {
12925 if (ConstantFPSDNode *CN =
12926 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
12927 bool IsExact;
12928 APSInt IntVal(BitWidth);
12929 const APFloat &APF = CN->getValueAPF();
12930 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
12931 APFloat::opOK ||
12932 !IsExact)
12933 return -1;
12934
12935 return IntVal.exactLogBase2();
12936 }
12937 return -1;
12938}
12939
12941 bool IsLittleEndian, unsigned DstEltSizeInBits,
12942 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
12943 // Early-out if this contains anything but Undef/Constant/ConstantFP.
12944 if (!isConstant())
12945 return false;
12946
12947 unsigned NumSrcOps = getNumOperands();
12948 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
12949 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12950 "Invalid bitcast scale");
12951
12952 // Extract raw src bits.
12953 SmallVector<APInt> SrcBitElements(NumSrcOps,
12954 APInt::getZero(SrcEltSizeInBits));
12955 BitVector SrcUndeElements(NumSrcOps, false);
12956
12957 for (unsigned I = 0; I != NumSrcOps; ++I) {
12959 if (Op.isUndef()) {
12960 SrcUndeElements.set(I);
12961 continue;
12962 }
12963 auto *CInt = dyn_cast<ConstantSDNode>(Op);
12964 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
12965 assert((CInt || CFP) && "Unknown constant");
12966 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
12967 : CFP->getValueAPF().bitcastToAPInt();
12968 }
12969
12970 // Recast to dst width.
12971 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
12972 SrcBitElements, UndefElements, SrcUndeElements);
12973 return true;
12974}
12975
12976void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
12977 unsigned DstEltSizeInBits,
12978 SmallVectorImpl<APInt> &DstBitElements,
12979 ArrayRef<APInt> SrcBitElements,
12980 BitVector &DstUndefElements,
12981 const BitVector &SrcUndefElements) {
12982 unsigned NumSrcOps = SrcBitElements.size();
12983 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
12984 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
12985 "Invalid bitcast scale");
12986 assert(NumSrcOps == SrcUndefElements.size() &&
12987 "Vector size mismatch");
12988
12989 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
12990 DstUndefElements.clear();
12991 DstUndefElements.resize(NumDstOps, false);
12992 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
12993
12994 // Concatenate src elements constant bits together into dst element.
12995 if (SrcEltSizeInBits <= DstEltSizeInBits) {
12996 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
12997 for (unsigned I = 0; I != NumDstOps; ++I) {
12998 DstUndefElements.set(I);
12999 APInt &DstBits = DstBitElements[I];
13000 for (unsigned J = 0; J != Scale; ++J) {
13001 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13002 if (SrcUndefElements[Idx])
13003 continue;
13004 DstUndefElements.reset(I);
13005 const APInt &SrcBits = SrcBitElements[Idx];
13006 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13007 "Illegal constant bitwidths");
13008 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13009 }
13010 }
13011 return;
13012 }
13013
13014 // Split src element constant bits into dst elements.
13015 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13016 for (unsigned I = 0; I != NumSrcOps; ++I) {
13017 if (SrcUndefElements[I]) {
13018 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13019 continue;
13020 }
13021 const APInt &SrcBits = SrcBitElements[I];
13022 for (unsigned J = 0; J != Scale; ++J) {
13023 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13024 APInt &DstBits = DstBitElements[Idx];
13025 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13026 }
13027 }
13028}
13029
13031 for (const SDValue &Op : op_values()) {
13032 unsigned Opc = Op.getOpcode();
13033 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13034 return false;
13035 }
13036 return true;
13037}
13038
13039std::optional<std::pair<APInt, APInt>>
13041 unsigned NumOps = getNumOperands();
13042 if (NumOps < 2)
13043 return std::nullopt;
13044
13045 if (!isa<ConstantSDNode>(getOperand(0)) ||
13046 !isa<ConstantSDNode>(getOperand(1)))
13047 return std::nullopt;
13048
13049 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13050 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13051 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13052
13053 if (Stride.isZero())
13054 return std::nullopt;
13055
13056 for (unsigned i = 2; i < NumOps; ++i) {
13057 if (!isa<ConstantSDNode>(getOperand(i)))
13058 return std::nullopt;
13059
13060 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13061 if (Val != (Start + (Stride * i)))
13062 return std::nullopt;
13063 }
13064
13065 return std::make_pair(Start, Stride);
13066}
13067
13068bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
13069 // Find the first non-undef value in the shuffle mask.
13070 unsigned i, e;
13071 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
13072 /* search */;
13073
13074 // If all elements are undefined, this shuffle can be considered a splat
13075 // (although it should eventually get simplified away completely).
13076 if (i == e)
13077 return true;
13078
13079 // Make sure all remaining elements are either undef or the same as the first
13080 // non-undef value.
13081 for (int Idx = Mask[i]; i != e; ++i)
13082 if (Mask[i] >= 0 && Mask[i] != Idx)
13083 return false;
13084 return true;
13085}
13086
13087// Returns the SDNode if it is a constant integer BuildVector
13088// or constant integer.
13090 if (isa<ConstantSDNode>(N))
13091 return N.getNode();
13093 return N.getNode();
13094 // Treat a GlobalAddress supporting constant offset folding as a
13095 // constant integer.
13096 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N))
13097 if (GA->getOpcode() == ISD::GlobalAddress &&
13098 TLI->isOffsetFoldingLegal(GA))
13099 return GA;
13100 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13101 isa<ConstantSDNode>(N.getOperand(0)))
13102 return N.getNode();
13103 return nullptr;
13104}
13105
13106// Returns the SDNode if it is a constant float BuildVector
13107// or constant float.
13109 if (isa<ConstantFPSDNode>(N))
13110 return N.getNode();
13111
13113 return N.getNode();
13114
13115 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13116 isa<ConstantFPSDNode>(N.getOperand(0)))
13117 return N.getNode();
13118
13119 return nullptr;
13120}
13121
13122void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13123 assert(!Node->OperandList && "Node already has operands");
13125 "too many operands to fit into SDNode");
13126 SDUse *Ops = OperandRecycler.allocate(
13127 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13128
13129 bool IsDivergent = false;
13130 for (unsigned I = 0; I != Vals.size(); ++I) {
13131 Ops[I].setUser(Node);
13132 Ops[I].setInitial(Vals[I]);
13133 if (Ops[I].Val.getValueType() != MVT::Other) // Skip Chain. It does not carry divergence.
13134 IsDivergent |= Ops[I].getNode()->isDivergent();
13135 }
13136 Node->NumOperands = Vals.size();
13137 Node->OperandList = Ops;
13138 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13139 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13140 Node->SDNodeBits.IsDivergent = IsDivergent;
13141 }
13143}
13144
13147 size_t Limit = SDNode::getMaxNumOperands();
13148 while (Vals.size() > Limit) {
13149 unsigned SliceIdx = Vals.size() - Limit;
13150 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13151 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13152 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13153 Vals.emplace_back(NewTF);
13154 }
13155 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13156}
13157
13159 EVT VT, SDNodeFlags Flags) {
13160 switch (Opcode) {
13161 default:
13162 return SDValue();
13163 case ISD::ADD:
13164 case ISD::OR:
13165 case ISD::XOR:
13166 case ISD::UMAX:
13167 return getConstant(0, DL, VT);
13168 case ISD::MUL:
13169 return getConstant(1, DL, VT);
13170 case ISD::AND:
13171 case ISD::UMIN:
13172 return getAllOnesConstant(DL, VT);
13173 case ISD::SMAX:
13175 case ISD::SMIN:
13177 case ISD::FADD:
13178 return getConstantFP(-0.0, DL, VT);
13179 case ISD::FMUL:
13180 return getConstantFP(1.0, DL, VT);
13181 case ISD::FMINNUM:
13182 case ISD::FMAXNUM: {
13183 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13184 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
13185 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
13186 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
13187 APFloat::getLargest(Semantics);
13188 if (Opcode == ISD::FMAXNUM)
13189 NeutralAF.changeSign();
13190
13191 return getConstantFP(NeutralAF, DL, VT);
13192 }
13193 case ISD::FMINIMUM:
13194 case ISD::FMAXIMUM: {
13195 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
13196 const fltSemantics &Semantics = EVTToAPFloatSemantics(VT);
13197 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13198 : APFloat::getLargest(Semantics);
13199 if (Opcode == ISD::FMAXIMUM)
13200 NeutralAF.changeSign();
13201
13202 return getConstantFP(NeutralAF, DL, VT);
13203 }
13204
13205 }
13206}
13207
13208/// Helper used to make a call to a library function that has one argument of
13209/// pointer type.
13210///
13211/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
13212/// used to get or set floating-point state. They have one argument of pointer
13213/// type, which points to the memory region containing bits of the
13214/// floating-point state. The value returned by such function is ignored in the
13215/// created call.
13216///
13217/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
13218/// \param Ptr Pointer used to save/load state.
13219/// \param InChain Ingoing token chain.
13220/// \returns Outgoing chain token.
13222 SDValue InChain,
13223 const SDLoc &DLoc) {
13224 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
13227 Entry.Node = Ptr;
13228 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
13229 Args.push_back(Entry);
13230 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
13231 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
13232 TLI->getPointerTy(getDataLayout()));
13234 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
13235 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
13236 std::move(Args));
13237 return TLI->LowerCallTo(CLI).second;
13238}
13239
13241 assert(From && To && "Invalid SDNode; empty source SDValue?");
13242 auto I = SDEI.find(From);
13243 if (I == SDEI.end())
13244 return;
13245
13246 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13247 // the iterator, hence the need to make a copy to prevent a use-after-free.
13248 NodeExtraInfo NEI = I->second;
13249 if (LLVM_LIKELY(!NEI.PCSections) && LLVM_LIKELY(!NEI.MMRA)) {
13250 // No deep copy required for the types of extra info set.
13251 //
13252 // FIXME: Investigate if other types of extra info also need deep copy. This
13253 // depends on the types of nodes they can be attached to: if some extra info
13254 // is only ever attached to nodes where a replacement To node is always the
13255 // node where later use and propagation of the extra info has the intended
13256 // semantics, no deep copy is required.
13257 SDEI[To] = std::move(NEI);
13258 return;
13259 }
13260
13261 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13262 // through the replacement of From with To. Otherwise, replacements of a node
13263 // (From) with more complex nodes (To and its operands) may result in lost
13264 // extra info where the root node (To) is insignificant in further propagating
13265 // and using extra info when further lowering to MIR.
13266 //
13267 // In the first step pre-populate the visited set with the nodes reachable
13268 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13269 // DAG that is not new and should be left untouched.
13270 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13271 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13272 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13273 if (MaxDepth == 0) {
13274 // Remember this node in case we need to increase MaxDepth and continue
13275 // populating FromReach from this node.
13276 Leafs.emplace_back(N);
13277 return;
13278 }
13279 if (!FromReach.insert(N).second)
13280 return;
13281 for (const SDValue &Op : N->op_values())
13282 Self(Self, Op.getNode(), MaxDepth - 1);
13283 };
13284
13285 // Copy extra info to To and all its transitive operands (that are new).
13287 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13288 if (FromReach.contains(N))
13289 return true;
13290 if (!Visited.insert(N).second)
13291 return true;
13292 if (getEntryNode().getNode() == N)
13293 return false;
13294 for (const SDValue &Op : N->op_values()) {
13295 if (!Self(Self, Op.getNode()))
13296 return false;
13297 }
13298 // Copy only if entry node was not reached.
13299 SDEI[N] = NEI;
13300 return true;
13301 };
13302
13303 // We first try with a lower MaxDepth, assuming that the path to common
13304 // operands between From and To is relatively short. This significantly
13305 // improves performance in the common case. The initial MaxDepth is big
13306 // enough to avoid retry in the common case; the last MaxDepth is large
13307 // enough to avoid having to use the fallback below (and protects from
13308 // potential stack exhaustion from recursion).
13309 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13310 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13311 // StartFrom is the previous (or initial) set of leafs reachable at the
13312 // previous maximum depth.
13314 std::swap(StartFrom, Leafs);
13315 for (const SDNode *N : StartFrom)
13316 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13317 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13318 return;
13319 // This should happen very rarely (reached the entry node).
13320 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13321 assert(!Leafs.empty());
13322 }
13323
13324 // This should not happen - but if it did, that means the subgraph reachable
13325 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13326 // could not visit all reachable common operands. Consequently, we were able
13327 // to reach the entry node.
13328 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13329 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13330 // Best-effort fallback if assertions disabled.
13331 SDEI[To] = std::move(NEI);
13332}
13333
13334#ifndef NDEBUG
13335static void checkForCyclesHelper(const SDNode *N,
13338 const llvm::SelectionDAG *DAG) {
13339 // If this node has already been checked, don't check it again.
13340 if (Checked.count(N))
13341 return;
13342
13343 // If a node has already been visited on this depth-first walk, reject it as
13344 // a cycle.
13345 if (!Visited.insert(N).second) {
13346 errs() << "Detected cycle in SelectionDAG\n";
13347 dbgs() << "Offending node:\n";
13348 N->dumprFull(DAG); dbgs() << "\n";
13349 abort();
13350 }
13351
13352 for (const SDValue &Op : N->op_values())
13353 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13354
13355 Checked.insert(N);
13356 Visited.erase(N);
13357}
13358#endif
13359
13361 const llvm::SelectionDAG *DAG,
13362 bool force) {
13363#ifndef NDEBUG
13364 bool check = force;
13365#ifdef EXPENSIVE_CHECKS
13366 check = true;
13367#endif // EXPENSIVE_CHECKS
13368 if (check) {
13369 assert(N && "Checking nonexistent SDNode");
13372 checkForCyclesHelper(N, visited, checked, DAG);
13373 }
13374#endif // !NDEBUG
13375}
13376
13377void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13378 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13379}
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
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:468
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:240
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
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
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.
Rewrite Partial Register Uses
#define check(cond)
static const unsigned MaxDepth
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:512
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 ...
unsigned 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
Contains matchers for matching SelectionDAG nodes and values.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 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 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, AAResults *AA)
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 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 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 VerifySDNode(SDNode *N, const TargetLowering *TLI)
VerifySDNode - Check the given SDNode. Aborts if it is invalid.
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 Constant * ConstantFold(Instruction *I, const DataLayout &DL, const SmallDenseMap< Value *, Constant * > &ConstantPool)
Try to fold instruction I into a constant.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
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:191
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Value * RHS
Value * LHS
static unsigned getSize(unsigned Kind)
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition: APFloat.h:1026
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1119
void copySign(const APFloat &RHS)
Definition: APFloat.h:1213
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5317
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1101
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:1337
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1092
bool isFinite() const
Definition: APFloat.h:1359
bool isNaN() const
Definition: APFloat.h:1352
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1243
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1110
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1146
bool isZero() const
Definition: APFloat.h:1350
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1044
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1235
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1004
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1137
bool isPosZero() const
Definition: APFloat.h:1365
bool isNegZero() const
Definition: APFloat.h:1366
void changeSign()
Definition: APFloat.h:1208
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1015
bool isInfinity() const
Definition: APFloat.h:1351
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1941
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2025
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:214
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1387
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:981
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:209
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1500
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1372
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1629
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1366
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:608
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1002
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1472
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:906
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1310
APInt abs() const
Get the absolute value.
Definition: APInt.h:1753
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:1996
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:351
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1162
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:360
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1636
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1448
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1091
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:189
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:309
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1377
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition: APInt.cpp:1124
APInt reverseBits() const
Definition: APInt.cpp:737
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:814
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1146
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1598
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1587
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1557
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:620
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:199
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2056
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2070
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition: APInt.cpp:1111
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:368
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1397
unsigned logBase2() const
Definition: APInt.h:1719
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2006
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:807
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1299
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1706
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:314
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1130
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1347
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:853
APInt byteSwap() const
Definition: APInt.cpp:715
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1237
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:420
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:286
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:180
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1369
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:453
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1217
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:266
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:219
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:838
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:831
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1201
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2015
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
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:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
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:195
This is an SDNode representing atomic operations.
static BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
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:890
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
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 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.
bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
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.
std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
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.
ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
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_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition: Allocator.h:148
void Reset()
Deallocate all but the current slab and reset the current pointer to the beginning of it,...
Definition: Allocator.h:123
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static 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:269
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:149
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:146
bool isMachineConstantPoolEntry() const
This class represents a range of values.
Definition: ConstantRange.h:47
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 ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
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.
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:42
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1686
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
DWARF expression.
static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static 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 const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static 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:110
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:238
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:878
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:865
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:763
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:874
A debug info location.
Definition: DebugLoc.h:33
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition: DenseMap.h:103
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition: FoldingSet.h:327
MachineBasicBlock * MBB
MBB - The current block.
Data structure describing the variable locations in a function.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:698
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:695
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:350
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:358
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:263
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
This class is used to form a handle around another node that is persistent and is updated across invo...
static 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:145
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
This SDNode is used for LIFETIME_START/LIFETIME_END values, which indicate the offet and size that ar...
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:41
Metadata node.
Definition: Metadata.h:1067
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.
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 LLVMTargetMachine & 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.
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.
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:65
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:193
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
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:662
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:679
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.
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:161
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:190
void add(SDDbgValue *V, bool isParameter)
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:196
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(unsigned 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.
void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
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.
MemSDNodeBitfields MemSDNodeBits
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.
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 bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
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.
bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
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
Return true if the type of the node type undefined.
bool hasNUsesOfValue(unsigned NUses, unsigned Value) const
Return true if there are exactly NUSES uses of the indicated value.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
SDNode * getNode() const
Convenience function for get().getNode().
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.
bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
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 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:383
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:227
Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
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.
SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
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())
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 getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
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:568
SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
bool isKnownNeverSNaN(SDValue Op, unsigned Depth=0) const
SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
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:490
SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
void updateDivergence(SDNode *N)
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...
SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
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),...
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.
SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
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,...
SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
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,...
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(), AAResults *AA=nullptr)
SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
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())
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...
SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
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)
SDNode * isConstantIntBuildVectorOrConstantInt(SDValue N) const
Test whether the given value is a constant int or similar node.
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.
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...
SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, unsigned VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
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...
SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
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.
bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
bool calculateDivergence(SDNode *N)
SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
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,...
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...
SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
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.
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...
Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
bool shouldOptForSize() const
SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
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:494
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:452
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)
SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
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.
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:391
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...
void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
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.
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:548
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
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:843
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...
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.
void DeleteNode(SDNode *N)
Remove the specified node from the system.
SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
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...
SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
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.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:488
SDNode * isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
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(), AAResults *AA=nullptr)
SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
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)
OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
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.
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())
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
Definition: SelectionDAG.h:676
SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
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.
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.
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,...
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.
SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
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:877
SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
SDValue getRegister(unsigned Reg, EVT VT)
void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue getBasicBlock(MachineBasicBlock *MBB)
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...
bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
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 ...
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)
static const fltSemantics & EVTToAPFloatSemantics(EVT VT)
Returns an APFloat semantics tag appropriate for the given type.
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:489
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...
std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
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...
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,...
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:560
SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
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)
SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getValueType(EVT)
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
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...
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:556
SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
bool isKnownNeverNaN(SDValue Op, 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.
SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
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:691
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.
bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
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.
SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
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)
SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
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,...
SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
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:483
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 ...
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.
OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
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:860
SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
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...
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.
SDValue getRegisterMask(const uint32_t *RegMask)
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...
SDValue getCondCode(ISD::CondCode Cond)
SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex, int64_t Size, int64_t Offset=-1)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the por...
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
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.
OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
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...
LLVMContext * getContext() const
Definition: SelectionDAG.h:501
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:577
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=0, const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
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...
SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
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.
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.
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...
SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
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:571
SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
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:893
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.
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 ...
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...
SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
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 ...
SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
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:551
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
static bool isSplatMask(const int *Mask, EVT VT)
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.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:323
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:361
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:412
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:344
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:479
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void reserve(size_type N)
Definition: SmallVector.h:676
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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.
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.
virtual 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,...
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 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 on this target.
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.
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 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 ...
virtual void verifyTargetSDNode(const SDNode *N) const
Check the given SDNode. Aborts if it is invalid.
virtual bool findOptimalMemOpLowering(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.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
virtual bool isKnownNeverNaNForTargetNode(SDValue Op, const SelectionDAG &DAG, bool SNaN=false, unsigned Depth=0) const
If SNaN is false,.
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 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:77
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetOptions Options
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
virtual const TargetFrameLowering * getFrameLowering() const
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, XROS, or DriverKit).
Definition: Triple.h:560
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition: TypeSize.h:345
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void set(Value *Val)
Definition: Value.h:882
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:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:206
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:185
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:232
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
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:179
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition: TypeSize.h:239
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
SmartMutex - A mutex with a compile time constant parameter that indicates whether this mutex should ...
Definition: Mutex.h:28
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition: APInt.cpp:3100
const APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition: APInt.h:2222
APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition: APInt.cpp:3087
APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition: APInt.cpp:3077
const APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition: APInt.h:2217
APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition: APInt.cpp:3092
APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition: APInt.cpp:2978
APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition: APInt.cpp:3072
APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition: APInt.cpp:3082
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
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...
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:40
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition: ISDOpcodes.h:779
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition: ISDOpcodes.h:243
@ CTLZ_ZERO_UNDEF
Definition: ISDOpcodes.h:752
@ TargetConstantPool
Definition: ISDOpcodes.h:174
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
Definition: ISDOpcodes.h:1214
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:490
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1319
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition: ISDOpcodes.h:44
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
Definition: ISDOpcodes.h:1382
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
Definition: ISDOpcodes.h:1330
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1415
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:511
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition: ISDOpcodes.h:257
@ ATOMIC_LOAD_NAND
Definition: ISDOpcodes.h:1312
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:573
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1103
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:743
@ TargetBlockAddress
Definition: ISDOpcodes.h:176
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1314
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, ptr, val) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1284
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1315
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:276
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition: ISDOpcodes.h:501
@ ADD
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:246
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
Definition: ISDOpcodes.h:1074
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:813
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:497
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition: ISDOpcodes.h:205
@ GlobalAddress
Definition: ISDOpcodes.h:78
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
Definition: ISDOpcodes.h:1297
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:820
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:557
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1400
@ FADD
Simple binary floating point operators.
Definition: ISDOpcodes.h:397
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
Definition: ISDOpcodes.h:1404
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:716
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:850
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1414
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:491
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:943
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1310
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:933
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:236
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1311
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:976
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1455
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1317
@ GlobalTLSAddress
Definition: ISDOpcodes.h:79
@ 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:1210
@ FrameIndex
Definition: ISDOpcodes.h:80
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:1145
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:804
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:684
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:634
@ TargetExternalSymbol
Definition: ISDOpcodes.h:175
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1397
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:751
@ TargetJumpTable
Definition: ISDOpcodes.h:173
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition: ISDOpcodes.h:183
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
Definition: ISDOpcodes.h:1264
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1401
@ 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:787
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:960
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1318
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1120
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1313
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:660
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:514
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition: ISDOpcodes.h:356
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition: ISDOpcodes.h:756
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1280
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:218
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1416
@ RegisterMask
Definition: ISDOpcodes.h:75
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition: ISDOpcodes.h:229
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition: ISDOpcodes.h:641
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition: ISDOpcodes.h:68
@ ATOMIC_LOAD_FMIN
Definition: ISDOpcodes.h:1320
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition: ISDOpcodes.h:215
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition: ISDOpcodes.h:330
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition: ISDOpcodes.h:170
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
Definition: ISDOpcodes.h:1409
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:673
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:734
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1309
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:614
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1308
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:587
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:1021
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition: ISDOpcodes.h:47
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition: ISDOpcodes.h:549
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:810
@ TargetConstantFP
Definition: ISDOpcodes.h:165
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:886
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:771
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1372
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1291
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1316
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:1008
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition: ISDOpcodes.h:366
@ SMULO
Same for multiplication.
Definition: ISDOpcodes.h:338
@ TargetFrameIndex
Definition: ISDOpcodes.h:172
@ ConstantPool
Definition: ISDOpcodes.h:82
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition: ISDOpcodes.h:839
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:828
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:696
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1347
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:918
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:765
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:310
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
Definition: ISDOpcodes.h:1342
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1234
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1417
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:952
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1322
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1306
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:479
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1027
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1307
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:866
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition: ISDOpcodes.h:164
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:484
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:708
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition: ISDOpcodes.h:190
@ GET_FPENV_MEM
Gets the current floating-point environment.
Definition: ISDOpcodes.h:1050
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1367
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition: ISDOpcodes.h:704
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:679
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1398
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:286
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition: ISDOpcodes.h:650
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition: ISDOpcodes.h:223
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition: ISDOpcodes.h:538
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition: ISDOpcodes.h:52
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition: ISDOpcodes.h:626
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1305
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:981
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:899
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition: ISDOpcodes.h:668
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:861
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:937
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1446
@ 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:885
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1405
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:816
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1189
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1383
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:793
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition: ISDOpcodes.h:61
@ ATOMIC_LOAD_UINC_WRAP
Definition: ISDOpcodes.h:1321
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:507
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition: ISDOpcodes.h:347
@ AssertZext
Definition: ISDOpcodes.h:62
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1055
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition: ISDOpcodes.h:691
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition: ISDOpcodes.h:320
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition: ISDOpcodes.h:198
@ TargetGlobalTLSAddress
Definition: ISDOpcodes.h:171
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition: ISDOpcodes.h:529
unsigned getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantSDNode predicate.
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.
static const int FIRST_TARGET_MEMORY_OPCODE
FIRST_TARGET_MEMORY_OPCODE - Target-specific pre-isel operations which do not reference a specific me...
Definition: ISDOpcodes.h:1467
bool isExtOpcode(unsigned Opcode)
Definition: ISDOpcodes.h:1649
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...
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...
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
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:1636
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:1641
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.
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...
bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1540
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 isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
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...
bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Definition: ISDOpcodes.h:1527
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:1578
bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
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:1558
bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
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< 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:893
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
Libcall
RTLIB::Libcall enum - This enum defines all of the runtime library calls the backend can emit.
Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
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:443
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition: Dwarf.h:147
std::lock_guard< SmartMutex< mt_only > > SmartScopedLock
Definition: Mutex.h:69
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:353
@ Offset
Definition: DWP.cpp:480
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:361
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition: Analysis.cpp:233
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:1722
bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:255
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:1546
SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
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 are tuples (A,...
Definition: STLExtras.h:2400
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
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:317
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition: APFloat.h:1508
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2067
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:656
MaybeAlign getAlign(const Function &F, unsigned Index)
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:1528
bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
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:1432
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
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:1729
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 2019 maximumNumber semantics.
Definition: APFloat.h:1469
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:340
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:291
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
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:1736
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
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...
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:54
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...
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 2019 minimumNumber semantics.
Definition: APFloat.h:1455
@ Mul
Product of integers.
bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
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:535
DWARFExpression::Operation Op
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:1824
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition: Analysis.cpp:714
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:1879
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
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:573
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
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:1482
bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
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:382
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
#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 const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:276
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:240
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:253
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:250
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:254
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:278
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:277
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:274
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:252
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:275
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:266
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Align previous() const
Definition: Alignment.h:88
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:34
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:380
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:136
intptr_t getRawBits() const
Definition: ValueTypes.h:497
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:73
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:274
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:290
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:146
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:340
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:358
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:349
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:370
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:306
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:64
bool isFixedLengthVector() const
Definition: ValueTypes.h:177
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:167
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:313
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:282
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:246
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:203
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:173
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:318
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:141
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:326
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:298
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:438
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:151
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:290
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:149
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:902
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:244
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:202
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:97
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:76
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:113
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:762
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:231
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:428
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1042
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:62
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:263
static 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:496
void makeNegative()
Make this value negative.
Definition: KnownBits.h:108
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:150
KnownBits byteSwap() const
Definition: KnownBits.h:456
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:278
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:82
KnownBits reverseBits() const
Definition: KnownBits.h:460
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:222
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:178
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:161
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:70
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:310
static 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:100
static 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:214
static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
Definition: KnownBits.cpp:782
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:300
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:169
static 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:137
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:185
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:134
static KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
Definition: KnownBits.cpp:247
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:894
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1059
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1002
static 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:51
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:103
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:946
static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
Definition: KnownBits.cpp:777
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:315
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:94
static 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:44
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:269
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:208
static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
Definition: KnownBits.cpp:792
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:797
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:156
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:550
static 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 KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:196
static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
Definition: KnownBits.cpp:787
This class contains a discriminated union of information about pointers in memory operands,...
bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
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 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.
void intersectWith(const SDNodeFlags Flags)
Clear any flags in this flag set that aren't also set in Flags.
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:311
DAGUpdateListener *const Next
Definition: SelectionDAG.h:312
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)