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"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
68#include "llvm/Support/Mutex.h"
74#include <algorithm>
75#include <cassert>
76#include <cstdint>
77#include <cstdlib>
78#include <deque>
79#include <limits>
80#include <optional>
81#include <set>
82#include <string>
83#include <utility>
84#include <vector>
85
86using namespace llvm;
87using namespace llvm::SDPatternMatch;
88
89/// makeVTList - Return an instance of the SDVTList struct initialized with the
90/// specified members.
91static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
92 SDVTList Res = {VTs, NumVTs};
93 return Res;
94}
95
96// Default null implementations of the callbacks.
100
101void SelectionDAG::DAGNodeDeletedListener::anchor() {}
102void SelectionDAG::DAGNodeInsertedListener::anchor() {}
103
104#define DEBUG_TYPE "selectiondag"
105
106static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
107 cl::Hidden, cl::init(true),
108 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
109
110static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
111 cl::desc("Number limit for gluing ld/st of memcpy."),
112 cl::Hidden, cl::init(0));
113
115 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
116 cl::desc("DAG combiner limit number of steps when searching DAG "
117 "for predecessor nodes"));
118
120 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
121}
122
124
125//===----------------------------------------------------------------------===//
126// ConstantFPSDNode Class
127//===----------------------------------------------------------------------===//
128
129/// isExactlyValue - We don't rely on operator== working on double values, as
130/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
131/// As such, this method can be used to do an exact bit-for-bit comparison of
132/// two floating point values.
134 return getValueAPF().bitwiseIsEqual(V);
135}
136
138 const APFloat& Val) {
139 assert(VT.isFloatingPoint() && "Can only convert between FP types");
140
141 // convert modifies in place, so make a copy.
142 APFloat Val2 = APFloat(Val);
143 bool losesInfo;
145 &losesInfo);
146 return !losesInfo;
147}
148
149//===----------------------------------------------------------------------===//
150// ISD Namespace
151//===----------------------------------------------------------------------===//
152
153bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
154 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
155 unsigned EltSize =
156 N->getValueType(0).getVectorElementType().getSizeInBits();
157 if (auto *Op0 = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
158 SplatVal = Op0->getAPIntValue().trunc(EltSize);
159 return true;
160 }
161 if (auto *Op0 = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
162 SplatVal = Op0->getValueAPF().bitcastToAPInt().trunc(EltSize);
163 return true;
164 }
165 }
166
167 auto *BV = dyn_cast<BuildVectorSDNode>(N);
168 if (!BV)
169 return false;
170
171 APInt SplatUndef;
172 unsigned SplatBitSize;
173 bool HasUndefs;
174 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
175 // Endianness does not matter here. We are checking for a splat given the
176 // element size of the vector, and if we find such a splat for little endian
177 // layout, then that should be valid also for big endian (as the full vector
178 // size is known to be a multiple of the element size).
179 const bool IsBigEndian = false;
180 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
181 EltSize, IsBigEndian) &&
182 EltSize == SplatBitSize;
183}
184
185// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
186// specializations of the more general isConstantSplatVector()?
187
188bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
189 // Look through a bit convert.
190 while (N->getOpcode() == ISD::BITCAST)
191 N = N->getOperand(0).getNode();
192
193 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
194 APInt SplatVal;
195 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
196 }
197
198 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
199
200 unsigned i = 0, e = N->getNumOperands();
201
202 // Skip over all of the undef values.
203 while (i != e && N->getOperand(i).isUndef())
204 ++i;
205
206 // Do not accept an all-undef vector.
207 if (i == e) return false;
208
209 // Do not accept build_vectors that aren't all constants or which have non-~0
210 // elements. We have to be a bit careful here, as the type of the constant
211 // may not be the same as the type of the vector elements due to type
212 // legalization (the elements are promoted to a legal type for the target and
213 // a vector of a type may be legal when the base element type is not).
214 // We only want to check enough bits to cover the vector elements, because
215 // we care if the resultant vector is all ones, not whether the individual
216 // constants are.
217 SDValue NotZero = N->getOperand(i);
218 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
219 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(NotZero)) {
220 if (CN->getAPIntValue().countr_one() < EltSize)
221 return false;
222 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(NotZero)) {
223 if (CFPN->getValueAPF().bitcastToAPInt().countr_one() < EltSize)
224 return false;
225 } else
226 return false;
227
228 // Okay, we have at least one ~0 value, check to see if the rest match or are
229 // undefs. Even with the above element type twiddling, this should be OK, as
230 // the same type legalization should have applied to all the elements.
231 for (++i; i != e; ++i)
232 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
233 return false;
234 return true;
235}
236
237bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
238 // Look through a bit convert.
239 while (N->getOpcode() == ISD::BITCAST)
240 N = N->getOperand(0).getNode();
241
242 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
243 APInt SplatVal;
244 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
245 }
246
247 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
248
249 bool IsAllUndef = true;
250 for (const SDValue &Op : N->op_values()) {
251 if (Op.isUndef())
252 continue;
253 IsAllUndef = false;
254 // Do not accept build_vectors that aren't all constants or which have non-0
255 // elements. We have to be a bit careful here, as the type of the constant
256 // may not be the same as the type of the vector elements due to type
257 // legalization (the elements are promoted to a legal type for the target
258 // and a vector of a type may be legal when the base element type is not).
259 // We only want to check enough bits to cover the vector elements, because
260 // we care if the resultant vector is all zeros, not whether the individual
261 // constants are.
262 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
263 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op)) {
264 if (CN->getAPIntValue().countr_zero() < EltSize)
265 return false;
266 } else if (ConstantFPSDNode *CFPN = dyn_cast<ConstantFPSDNode>(Op)) {
267 if (CFPN->getValueAPF().bitcastToAPInt().countr_zero() < EltSize)
268 return false;
269 } else
270 return false;
271 }
272
273 // Do not accept an all-undef vector.
274 if (IsAllUndef)
275 return false;
276 return true;
277}
278
280 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
281}
282
284 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
285}
286
288 if (N->getOpcode() != ISD::BUILD_VECTOR)
289 return false;
290
291 for (const SDValue &Op : N->op_values()) {
292 if (Op.isUndef())
293 continue;
294 if (!isa<ConstantSDNode>(Op))
295 return false;
296 }
297 return true;
298}
299
301 if (N->getOpcode() != ISD::BUILD_VECTOR)
302 return false;
303
304 for (const SDValue &Op : N->op_values()) {
305 if (Op.isUndef())
306 continue;
307 if (!isa<ConstantFPSDNode>(Op))
308 return false;
309 }
310 return true;
311}
312
313bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
314 bool Signed) {
315 assert(N->getValueType(0).isVector() && "Expected a vector!");
316
317 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
318 if (EltSize <= NewEltSize)
319 return false;
320
321 if (N->getOpcode() == ISD::ZERO_EXTEND) {
322 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
323 NewEltSize) &&
324 !Signed;
325 }
326 if (N->getOpcode() == ISD::SIGN_EXTEND) {
327 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
328 NewEltSize) &&
329 Signed;
330 }
331 if (N->getOpcode() != ISD::BUILD_VECTOR)
332 return false;
333
334 for (const SDValue &Op : N->op_values()) {
335 if (Op.isUndef())
336 continue;
337 if (!isa<ConstantSDNode>(Op))
338 return false;
339
340 APInt C = Op->getAsAPIntVal().trunc(EltSize);
341 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
342 return false;
343 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
344 return false;
345 }
346
347 return true;
348}
349
351 // Return false if the node has no operands.
352 // This is "logically inconsistent" with the definition of "all" but
353 // is probably the desired behavior.
354 if (N->getNumOperands() == 0)
355 return false;
356 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
357}
358
360 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
361}
362
363template <typename ConstNodeType>
365 std::function<bool(ConstNodeType *)> Match,
366 bool AllowUndefs) {
367 // FIXME: Add support for scalar UNDEF cases?
368 if (auto *C = dyn_cast<ConstNodeType>(Op))
369 return Match(C);
370
371 // FIXME: Add support for vector UNDEF cases?
372 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
373 ISD::SPLAT_VECTOR != Op.getOpcode())
374 return false;
375
376 EVT SVT = Op.getValueType().getScalarType();
377 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
378 if (AllowUndefs && Op.getOperand(i).isUndef()) {
379 if (!Match(nullptr))
380 return false;
381 continue;
382 }
383
384 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
385 if (!Cst || Cst->getValueType(0) != SVT || !Match(Cst))
386 return false;
387 }
388 return true;
389}
390// Build used template types.
391template bool ISD::matchUnaryPredicateImpl<ConstantSDNode>(
392 SDValue, std::function<bool(ConstantSDNode *)>, bool);
393template bool ISD::matchUnaryPredicateImpl<ConstantFPSDNode>(
394 SDValue, std::function<bool(ConstantFPSDNode *)>, bool);
395
397 SDValue LHS, SDValue RHS,
398 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
399 bool AllowUndefs, bool AllowTypeMismatch) {
400 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
401 return false;
402
403 // TODO: Add support for scalar UNDEF cases?
404 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
405 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
406 return Match(LHSCst, RHSCst);
407
408 // TODO: Add support for vector UNDEF cases?
409 if (LHS.getOpcode() != RHS.getOpcode() ||
410 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
411 LHS.getOpcode() != ISD::SPLAT_VECTOR))
412 return false;
413
414 EVT SVT = LHS.getValueType().getScalarType();
415 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
416 SDValue LHSOp = LHS.getOperand(i);
417 SDValue RHSOp = RHS.getOperand(i);
418 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
419 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
420 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
421 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
422 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
423 return false;
424 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
425 LHSOp.getValueType() != RHSOp.getValueType()))
426 return false;
427 if (!Match(LHSCst, RHSCst))
428 return false;
429 }
430 return true;
431}
432
434 switch (VecReduceOpcode) {
435 default:
436 llvm_unreachable("Expected VECREDUCE opcode");
439 case ISD::VP_REDUCE_FADD:
440 case ISD::VP_REDUCE_SEQ_FADD:
441 return ISD::FADD;
444 case ISD::VP_REDUCE_FMUL:
445 case ISD::VP_REDUCE_SEQ_FMUL:
446 return ISD::FMUL;
448 case ISD::VP_REDUCE_ADD:
449 return ISD::ADD;
451 case ISD::VP_REDUCE_MUL:
452 return ISD::MUL;
454 case ISD::VP_REDUCE_AND:
455 return ISD::AND;
457 case ISD::VP_REDUCE_OR:
458 return ISD::OR;
460 case ISD::VP_REDUCE_XOR:
461 return ISD::XOR;
463 case ISD::VP_REDUCE_SMAX:
464 return ISD::SMAX;
466 case ISD::VP_REDUCE_SMIN:
467 return ISD::SMIN;
469 case ISD::VP_REDUCE_UMAX:
470 return ISD::UMAX;
472 case ISD::VP_REDUCE_UMIN:
473 return ISD::UMIN;
475 case ISD::VP_REDUCE_FMAX:
476 return ISD::FMAXNUM;
478 case ISD::VP_REDUCE_FMIN:
479 return ISD::FMINNUM;
481 case ISD::VP_REDUCE_FMAXIMUM:
482 return ISD::FMAXIMUM;
484 case ISD::VP_REDUCE_FMINIMUM:
485 return ISD::FMINIMUM;
486 }
487}
488
489bool ISD::isVPOpcode(unsigned Opcode) {
490 switch (Opcode) {
491 default:
492 return false;
493#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
494 case ISD::VPSD: \
495 return true;
496#include "llvm/IR/VPIntrinsics.def"
497 }
498}
499
500bool ISD::isVPBinaryOp(unsigned Opcode) {
501 switch (Opcode) {
502 default:
503 break;
504#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
505#define VP_PROPERTY_BINARYOP return true;
506#define END_REGISTER_VP_SDNODE(VPSD) break;
507#include "llvm/IR/VPIntrinsics.def"
508 }
509 return false;
510}
511
512bool ISD::isVPReduction(unsigned Opcode) {
513 switch (Opcode) {
514 default:
515 return false;
516 case ISD::VP_REDUCE_ADD:
517 case ISD::VP_REDUCE_MUL:
518 case ISD::VP_REDUCE_AND:
519 case ISD::VP_REDUCE_OR:
520 case ISD::VP_REDUCE_XOR:
521 case ISD::VP_REDUCE_SMAX:
522 case ISD::VP_REDUCE_SMIN:
523 case ISD::VP_REDUCE_UMAX:
524 case ISD::VP_REDUCE_UMIN:
525 case ISD::VP_REDUCE_FMAX:
526 case ISD::VP_REDUCE_FMIN:
527 case ISD::VP_REDUCE_FMAXIMUM:
528 case ISD::VP_REDUCE_FMINIMUM:
529 case ISD::VP_REDUCE_FADD:
530 case ISD::VP_REDUCE_FMUL:
531 case ISD::VP_REDUCE_SEQ_FADD:
532 case ISD::VP_REDUCE_SEQ_FMUL:
533 return true;
534 }
535}
536
537/// The operand position of the vector mask.
538std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
539 switch (Opcode) {
540 default:
541 return std::nullopt;
542#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
543 case ISD::VPSD: \
544 return MASKPOS;
545#include "llvm/IR/VPIntrinsics.def"
546 }
547}
548
549/// The operand position of the explicit vector length parameter.
550std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
551 switch (Opcode) {
552 default:
553 return std::nullopt;
554#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
555 case ISD::VPSD: \
556 return EVLPOS;
557#include "llvm/IR/VPIntrinsics.def"
558 }
559}
560
561std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
562 bool hasFPExcept) {
563 // FIXME: Return strict opcodes in case of fp exceptions.
564 switch (VPOpcode) {
565 default:
566 return std::nullopt;
567#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
568#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
569#define END_REGISTER_VP_SDNODE(VPOPC) break;
570#include "llvm/IR/VPIntrinsics.def"
571 }
572 return std::nullopt;
573}
574
575std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
576 switch (Opcode) {
577 default:
578 return std::nullopt;
579#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
580#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
581#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
582#include "llvm/IR/VPIntrinsics.def"
583 }
584}
585
587 switch (ExtType) {
588 case ISD::EXTLOAD:
589 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
590 case ISD::SEXTLOAD:
591 return ISD::SIGN_EXTEND;
592 case ISD::ZEXTLOAD:
593 return ISD::ZERO_EXTEND;
594 default:
595 break;
596 }
597
598 llvm_unreachable("Invalid LoadExtType");
599}
600
602 // To perform this operation, we just need to swap the L and G bits of the
603 // operation.
604 unsigned OldL = (Operation >> 2) & 1;
605 unsigned OldG = (Operation >> 1) & 1;
606 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
607 (OldL << 1) | // New G bit
608 (OldG << 2)); // New L bit.
609}
610
612 unsigned Operation = Op;
613 if (isIntegerLike)
614 Operation ^= 7; // Flip L, G, E bits, but not U.
615 else
616 Operation ^= 15; // Flip all of the condition bits.
617
619 Operation &= ~8; // Don't let N and U bits get set.
620
621 return ISD::CondCode(Operation);
622}
623
625 return getSetCCInverseImpl(Op, Type.isInteger());
626}
627
629 bool isIntegerLike) {
630 return getSetCCInverseImpl(Op, isIntegerLike);
631}
632
633/// For an integer comparison, return 1 if the comparison is a signed operation
634/// and 2 if the result is an unsigned comparison. Return zero if the operation
635/// does not depend on the sign of the input (setne and seteq).
636static int isSignedOp(ISD::CondCode Opcode) {
637 switch (Opcode) {
638 default: llvm_unreachable("Illegal integer setcc operation!");
639 case ISD::SETEQ:
640 case ISD::SETNE: return 0;
641 case ISD::SETLT:
642 case ISD::SETLE:
643 case ISD::SETGT:
644 case ISD::SETGE: return 1;
645 case ISD::SETULT:
646 case ISD::SETULE:
647 case ISD::SETUGT:
648 case ISD::SETUGE: return 2;
649 }
650}
651
653 EVT Type) {
654 bool IsInteger = Type.isInteger();
655 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
656 // Cannot fold a signed integer setcc with an unsigned integer setcc.
657 return ISD::SETCC_INVALID;
658
659 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
660
661 // If the N and U bits get set, then the resultant comparison DOES suddenly
662 // care about orderedness, and it is true when ordered.
663 if (Op > ISD::SETTRUE2)
664 Op &= ~16; // Clear the U bit if the N bit is set.
665
666 // Canonicalize illegal integer setcc's.
667 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
668 Op = ISD::SETNE;
669
670 return ISD::CondCode(Op);
671}
672
674 EVT Type) {
675 bool IsInteger = Type.isInteger();
676 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
677 // Cannot fold a signed setcc with an unsigned setcc.
678 return ISD::SETCC_INVALID;
679
680 // Combine all of the condition bits.
681 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
682
683 // Canonicalize illegal integer setcc's.
684 if (IsInteger) {
685 switch (Result) {
686 default: break;
687 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
688 case ISD::SETOEQ: // SETEQ & SETU[LG]E
689 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
690 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
691 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
692 }
693 }
694
695 return Result;
696}
697
698//===----------------------------------------------------------------------===//
699// SDNode Profile Support
700//===----------------------------------------------------------------------===//
701
702/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
703static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
704 ID.AddInteger(OpC);
705}
706
707/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
708/// solely with their pointer.
710 ID.AddPointer(VTList.VTs);
711}
712
713/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
715 ArrayRef<SDValue> Ops) {
716 for (const auto &Op : Ops) {
717 ID.AddPointer(Op.getNode());
718 ID.AddInteger(Op.getResNo());
719 }
720}
721
722/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
724 ArrayRef<SDUse> Ops) {
725 for (const auto &Op : Ops) {
726 ID.AddPointer(Op.getNode());
727 ID.AddInteger(Op.getResNo());
728 }
729}
730
731static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
732 SDVTList VTList, ArrayRef<SDValue> OpList) {
733 AddNodeIDOpcode(ID, OpC);
734 AddNodeIDValueTypes(ID, VTList);
735 AddNodeIDOperands(ID, OpList);
736}
737
738/// If this is an SDNode with special info, add this info to the NodeID data.
740 switch (N->getOpcode()) {
743 case ISD::MCSymbol:
744 llvm_unreachable("Should only be used on nodes with operands");
745 default: break; // Normal nodes don't need extra info.
747 case ISD::Constant: {
748 const ConstantSDNode *C = cast<ConstantSDNode>(N);
749 ID.AddPointer(C->getConstantIntValue());
750 ID.AddBoolean(C->isOpaque());
751 break;
752 }
754 case ISD::ConstantFP:
755 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
756 break;
761 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
762 ID.AddPointer(GA->getGlobal());
763 ID.AddInteger(GA->getOffset());
764 ID.AddInteger(GA->getTargetFlags());
765 break;
766 }
767 case ISD::BasicBlock:
768 ID.AddPointer(cast<BasicBlockSDNode>(N)->getBasicBlock());
769 break;
770 case ISD::Register:
771 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
772 break;
774 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
775 break;
776 case ISD::SRCVALUE:
777 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
778 break;
779 case ISD::FrameIndex:
781 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
782 break;
785 if (cast<LifetimeSDNode>(N)->hasOffset()) {
786 ID.AddInteger(cast<LifetimeSDNode>(N)->getSize());
787 ID.AddInteger(cast<LifetimeSDNode>(N)->getOffset());
788 }
789 break;
791 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
792 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
793 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
794 break;
795 case ISD::JumpTable:
797 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
798 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
799 break;
802 const ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
803 ID.AddInteger(CP->getAlign().value());
804 ID.AddInteger(CP->getOffset());
805 if (CP->isMachineConstantPoolEntry())
806 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
807 else
808 ID.AddPointer(CP->getConstVal());
809 ID.AddInteger(CP->getTargetFlags());
810 break;
811 }
812 case ISD::TargetIndex: {
813 const TargetIndexSDNode *TI = cast<TargetIndexSDNode>(N);
814 ID.AddInteger(TI->getIndex());
815 ID.AddInteger(TI->getOffset());
816 ID.AddInteger(TI->getTargetFlags());
817 break;
818 }
819 case ISD::LOAD: {
820 const LoadSDNode *LD = cast<LoadSDNode>(N);
821 ID.AddInteger(LD->getMemoryVT().getRawBits());
822 ID.AddInteger(LD->getRawSubclassData());
823 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
824 ID.AddInteger(LD->getMemOperand()->getFlags());
825 break;
826 }
827 case ISD::STORE: {
828 const StoreSDNode *ST = cast<StoreSDNode>(N);
829 ID.AddInteger(ST->getMemoryVT().getRawBits());
830 ID.AddInteger(ST->getRawSubclassData());
831 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
832 ID.AddInteger(ST->getMemOperand()->getFlags());
833 break;
834 }
835 case ISD::VP_LOAD: {
836 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
837 ID.AddInteger(ELD->getMemoryVT().getRawBits());
838 ID.AddInteger(ELD->getRawSubclassData());
839 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
840 ID.AddInteger(ELD->getMemOperand()->getFlags());
841 break;
842 }
843 case ISD::VP_STORE: {
844 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
845 ID.AddInteger(EST->getMemoryVT().getRawBits());
846 ID.AddInteger(EST->getRawSubclassData());
847 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
848 ID.AddInteger(EST->getMemOperand()->getFlags());
849 break;
850 }
851 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
852 const VPStridedLoadSDNode *SLD = cast<VPStridedLoadSDNode>(N);
853 ID.AddInteger(SLD->getMemoryVT().getRawBits());
854 ID.AddInteger(SLD->getRawSubclassData());
855 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
856 break;
857 }
858 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
859 const VPStridedStoreSDNode *SST = cast<VPStridedStoreSDNode>(N);
860 ID.AddInteger(SST->getMemoryVT().getRawBits());
861 ID.AddInteger(SST->getRawSubclassData());
862 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
863 break;
864 }
865 case ISD::VP_GATHER: {
866 const VPGatherSDNode *EG = cast<VPGatherSDNode>(N);
867 ID.AddInteger(EG->getMemoryVT().getRawBits());
868 ID.AddInteger(EG->getRawSubclassData());
869 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
870 ID.AddInteger(EG->getMemOperand()->getFlags());
871 break;
872 }
873 case ISD::VP_SCATTER: {
874 const VPScatterSDNode *ES = cast<VPScatterSDNode>(N);
875 ID.AddInteger(ES->getMemoryVT().getRawBits());
876 ID.AddInteger(ES->getRawSubclassData());
877 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
878 ID.AddInteger(ES->getMemOperand()->getFlags());
879 break;
880 }
881 case ISD::MLOAD: {
882 const MaskedLoadSDNode *MLD = cast<MaskedLoadSDNode>(N);
883 ID.AddInteger(MLD->getMemoryVT().getRawBits());
884 ID.AddInteger(MLD->getRawSubclassData());
885 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
886 ID.AddInteger(MLD->getMemOperand()->getFlags());
887 break;
888 }
889 case ISD::MSTORE: {
890 const MaskedStoreSDNode *MST = cast<MaskedStoreSDNode>(N);
891 ID.AddInteger(MST->getMemoryVT().getRawBits());
892 ID.AddInteger(MST->getRawSubclassData());
893 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
894 ID.AddInteger(MST->getMemOperand()->getFlags());
895 break;
896 }
897 case ISD::MGATHER: {
898 const MaskedGatherSDNode *MG = cast<MaskedGatherSDNode>(N);
899 ID.AddInteger(MG->getMemoryVT().getRawBits());
900 ID.AddInteger(MG->getRawSubclassData());
901 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
902 ID.AddInteger(MG->getMemOperand()->getFlags());
903 break;
904 }
905 case ISD::MSCATTER: {
906 const MaskedScatterSDNode *MS = cast<MaskedScatterSDNode>(N);
907 ID.AddInteger(MS->getMemoryVT().getRawBits());
908 ID.AddInteger(MS->getRawSubclassData());
909 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
910 ID.AddInteger(MS->getMemOperand()->getFlags());
911 break;
912 }
915 case ISD::ATOMIC_SWAP:
927 case ISD::ATOMIC_LOAD:
928 case ISD::ATOMIC_STORE: {
929 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
930 ID.AddInteger(AT->getMemoryVT().getRawBits());
931 ID.AddInteger(AT->getRawSubclassData());
932 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
933 ID.AddInteger(AT->getMemOperand()->getFlags());
934 break;
935 }
936 case ISD::VECTOR_SHUFFLE: {
937 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
938 for (int M : Mask)
939 ID.AddInteger(M);
940 break;
941 }
943 case ISD::BlockAddress: {
944 const BlockAddressSDNode *BA = cast<BlockAddressSDNode>(N);
945 ID.AddPointer(BA->getBlockAddress());
946 ID.AddInteger(BA->getOffset());
947 ID.AddInteger(BA->getTargetFlags());
948 break;
949 }
950 case ISD::AssertAlign:
951 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
952 break;
953 case ISD::PREFETCH:
956 // Handled by MemIntrinsicSDNode check after the switch.
957 break;
958 } // end switch (N->getOpcode())
959
960 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
961 // to check.
962 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
963 ID.AddInteger(MN->getRawSubclassData());
964 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
965 ID.AddInteger(MN->getMemOperand()->getFlags());
966 ID.AddInteger(MN->getMemoryVT().getRawBits());
967 }
968}
969
970/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
971/// data.
972static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
973 AddNodeIDOpcode(ID, N->getOpcode());
974 // Add the return value info.
975 AddNodeIDValueTypes(ID, N->getVTList());
976 // Add the operand info.
977 AddNodeIDOperands(ID, N->ops());
978
979 // Handle SDNode leafs with special info.
981}
982
983//===----------------------------------------------------------------------===//
984// SelectionDAG Class
985//===----------------------------------------------------------------------===//
986
987/// doNotCSE - Return true if CSE should not be performed for this node.
988static bool doNotCSE(SDNode *N) {
989 if (N->getValueType(0) == MVT::Glue)
990 return true; // Never CSE anything that produces a glue result.
991
992 switch (N->getOpcode()) {
993 default: break;
994 case ISD::HANDLENODE:
995 case ISD::EH_LABEL:
996 return true; // Never CSE these nodes.
997 }
998
999 // Check that remaining values produced are not flags.
1000 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1001 if (N->getValueType(i) == MVT::Glue)
1002 return true; // Never CSE anything that produces a glue result.
1003
1004 return false;
1005}
1006
1007/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1008/// SelectionDAG.
1010 // Create a dummy node (which is not added to allnodes), that adds a reference
1011 // to the root node, preventing it from being deleted.
1012 HandleSDNode Dummy(getRoot());
1013
1014 SmallVector<SDNode*, 128> DeadNodes;
1015
1016 // Add all obviously-dead nodes to the DeadNodes worklist.
1017 for (SDNode &Node : allnodes())
1018 if (Node.use_empty())
1019 DeadNodes.push_back(&Node);
1020
1021 RemoveDeadNodes(DeadNodes);
1022
1023 // If the root changed (e.g. it was a dead load, update the root).
1024 setRoot(Dummy.getValue());
1025}
1026
1027/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1028/// given list, and any nodes that become unreachable as a result.
1030
1031 // Process the worklist, deleting the nodes and adding their uses to the
1032 // worklist.
1033 while (!DeadNodes.empty()) {
1034 SDNode *N = DeadNodes.pop_back_val();
1035 // Skip to next node if we've already managed to delete the node. This could
1036 // happen if replacing a node causes a node previously added to the node to
1037 // be deleted.
1038 if (N->getOpcode() == ISD::DELETED_NODE)
1039 continue;
1040
1041 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1042 DUL->NodeDeleted(N, nullptr);
1043
1044 // Take the node out of the appropriate CSE map.
1045 RemoveNodeFromCSEMaps(N);
1046
1047 // Next, brutally remove the operand list. This is safe to do, as there are
1048 // no cycles in the graph.
1049 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1050 SDUse &Use = *I++;
1051 SDNode *Operand = Use.getNode();
1052 Use.set(SDValue());
1053
1054 // Now that we removed this operand, see if there are no uses of it left.
1055 if (Operand->use_empty())
1056 DeadNodes.push_back(Operand);
1057 }
1058
1059 DeallocateNode(N);
1060 }
1061}
1062
1064 SmallVector<SDNode*, 16> DeadNodes(1, N);
1065
1066 // Create a dummy node that adds a reference to the root node, preventing
1067 // it from being deleted. (This matters if the root is an operand of the
1068 // dead node.)
1069 HandleSDNode Dummy(getRoot());
1070
1071 RemoveDeadNodes(DeadNodes);
1072}
1073
1075 // First take this out of the appropriate CSE map.
1076 RemoveNodeFromCSEMaps(N);
1077
1078 // Finally, remove uses due to operands of this node, remove from the
1079 // AllNodes list, and delete the node.
1080 DeleteNodeNotInCSEMaps(N);
1081}
1082
1083void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1084 assert(N->getIterator() != AllNodes.begin() &&
1085 "Cannot delete the entry node!");
1086 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1087
1088 // Drop all of the operands and decrement used node's use counts.
1089 N->DropOperands();
1090
1091 DeallocateNode(N);
1092}
1093
1094void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1095 assert(!(V->isVariadic() && isParameter));
1096 if (isParameter)
1097 ByvalParmDbgValues.push_back(V);
1098 else
1099 DbgValues.push_back(V);
1100 for (const SDNode *Node : V->getSDNodes())
1101 if (Node)
1102 DbgValMap[Node].push_back(V);
1103}
1104
1105void SDDbgInfo::erase(const SDNode *Node) {
1106 DbgValMapType::iterator I = DbgValMap.find(Node);
1107 if (I == DbgValMap.end())
1108 return;
1109 for (auto &Val: I->second)
1110 Val->setIsInvalidated();
1111 DbgValMap.erase(I);
1112}
1113
1114void SelectionDAG::DeallocateNode(SDNode *N) {
1115 // If we have operands, deallocate them.
1116 removeOperands(N);
1117
1118 NodeAllocator.Deallocate(AllNodes.remove(N));
1119
1120 // Set the opcode to DELETED_NODE to help catch bugs when node
1121 // memory is reallocated.
1122 // FIXME: There are places in SDag that have grown a dependency on the opcode
1123 // value in the released node.
1124 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1125 N->NodeType = ISD::DELETED_NODE;
1126
1127 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1128 // them and forget about that node.
1129 DbgInfo->erase(N);
1130
1131 // Invalidate extra info.
1132 SDEI.erase(N);
1133}
1134
1135#ifndef NDEBUG
1136/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1137static void VerifySDNode(SDNode *N, const TargetLowering *TLI) {
1138 switch (N->getOpcode()) {
1139 default:
1140 if (N->getOpcode() > ISD::BUILTIN_OP_END)
1141 TLI->verifyTargetSDNode(N);
1142 break;
1143 case ISD::BUILD_PAIR: {
1144 EVT VT = N->getValueType(0);
1145 assert(N->getNumValues() == 1 && "Too many results!");
1146 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1147 "Wrong return type!");
1148 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1149 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1150 "Mismatched operand types!");
1151 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1152 "Wrong operand type!");
1153 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1154 "Wrong return type size");
1155 break;
1156 }
1157 case ISD::BUILD_VECTOR: {
1158 assert(N->getNumValues() == 1 && "Too many results!");
1159 assert(N->getValueType(0).isVector() && "Wrong return type!");
1160 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1161 "Wrong number of operands!");
1162 EVT EltVT = N->getValueType(0).getVectorElementType();
1163 for (const SDUse &Op : N->ops()) {
1164 assert((Op.getValueType() == EltVT ||
1165 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1166 EltVT.bitsLE(Op.getValueType()))) &&
1167 "Wrong operand type!");
1168 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1169 "Operands must all have the same type");
1170 }
1171 break;
1172 }
1173 }
1174}
1175#endif // NDEBUG
1176
1177/// Insert a newly allocated node into the DAG.
1178///
1179/// Handles insertion into the all nodes list and CSE map, as well as
1180/// verification and other common operations when a new node is allocated.
1181void SelectionDAG::InsertNode(SDNode *N) {
1182 AllNodes.push_back(N);
1183#ifndef NDEBUG
1184 N->PersistentId = NextPersistentId++;
1185 VerifySDNode(N, TLI);
1186#endif
1187 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1188 DUL->NodeInserted(N);
1189}
1190
1191/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1192/// correspond to it. This is useful when we're about to delete or repurpose
1193/// the node. We don't want future request for structurally identical nodes
1194/// to return N anymore.
1195bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1196 bool Erased = false;
1197 switch (N->getOpcode()) {
1198 case ISD::HANDLENODE: return false; // noop.
1199 case ISD::CONDCODE:
1200 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1201 "Cond code doesn't exist!");
1202 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1203 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1204 break;
1206 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1207 break;
1209 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1210 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1211 ESN->getSymbol(), ESN->getTargetFlags()));
1212 break;
1213 }
1214 case ISD::MCSymbol: {
1215 auto *MCSN = cast<MCSymbolSDNode>(N);
1216 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1217 break;
1218 }
1219 case ISD::VALUETYPE: {
1220 EVT VT = cast<VTSDNode>(N)->getVT();
1221 if (VT.isExtended()) {
1222 Erased = ExtendedValueTypeNodes.erase(VT);
1223 } else {
1224 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1225 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1226 }
1227 break;
1228 }
1229 default:
1230 // Remove it from the CSE Map.
1231 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1232 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1233 Erased = CSEMap.RemoveNode(N);
1234 break;
1235 }
1236#ifndef NDEBUG
1237 // Verify that the node was actually in one of the CSE maps, unless it has a
1238 // glue result (which cannot be CSE'd) or is one of the special cases that are
1239 // not subject to CSE.
1240 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1241 !N->isMachineOpcode() && !doNotCSE(N)) {
1242 N->dump(this);
1243 dbgs() << "\n";
1244 llvm_unreachable("Node is not in map!");
1245 }
1246#endif
1247 return Erased;
1248}
1249
1250/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1251/// maps and modified in place. Add it back to the CSE maps, unless an identical
1252/// node already exists, in which case transfer all its users to the existing
1253/// node. This transfer can potentially trigger recursive merging.
1254void
1255SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1256 // For node types that aren't CSE'd, just act as if no identical node
1257 // already exists.
1258 if (!doNotCSE(N)) {
1259 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1260 if (Existing != N) {
1261 // If there was already an existing matching node, use ReplaceAllUsesWith
1262 // to replace the dead one with the existing one. This can cause
1263 // recursive merging of other unrelated nodes down the line.
1264 Existing->intersectFlagsWith(N->getFlags());
1265 ReplaceAllUsesWith(N, Existing);
1266
1267 // N is now dead. Inform the listeners and delete it.
1268 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1269 DUL->NodeDeleted(N, Existing);
1270 DeleteNodeNotInCSEMaps(N);
1271 return;
1272 }
1273 }
1274
1275 // If the node doesn't already exist, we updated it. Inform listeners.
1276 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1277 DUL->NodeUpdated(N);
1278}
1279
1280/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1281/// were replaced with those specified. If this node is never memoized,
1282/// return null, otherwise return a pointer to the slot it would take. If a
1283/// node already exists with these operands, the slot will be non-null.
1284SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1285 void *&InsertPos) {
1286 if (doNotCSE(N))
1287 return nullptr;
1288
1289 SDValue Ops[] = { Op };
1291 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1293 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1294 if (Node)
1295 Node->intersectFlagsWith(N->getFlags());
1296 return Node;
1297}
1298
1299/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1300/// were replaced with those specified. If this node is never memoized,
1301/// return null, otherwise return a pointer to the slot it would take. If a
1302/// node already exists with these operands, the slot will be non-null.
1303SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1304 SDValue Op1, SDValue Op2,
1305 void *&InsertPos) {
1306 if (doNotCSE(N))
1307 return nullptr;
1308
1309 SDValue Ops[] = { Op1, Op2 };
1311 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1313 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1314 if (Node)
1315 Node->intersectFlagsWith(N->getFlags());
1316 return Node;
1317}
1318
1319/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1320/// were replaced with those specified. If this node is never memoized,
1321/// return null, otherwise return a pointer to the slot it would take. If a
1322/// node already exists with these operands, the slot will be non-null.
1323SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1324 void *&InsertPos) {
1325 if (doNotCSE(N))
1326 return nullptr;
1327
1329 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1331 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1332 if (Node)
1333 Node->intersectFlagsWith(N->getFlags());
1334 return Node;
1335}
1336
1338 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1339 : VT.getTypeForEVT(*getContext());
1340
1341 return getDataLayout().getABITypeAlign(Ty);
1342}
1343
1344// EntryNode could meaningfully have debug info if we can find it...
1346 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1347 getVTList(MVT::Other, MVT::Glue)),
1348 Root(getEntryNode()) {
1349 InsertNode(&EntryNode);
1350 DbgInfo = new SDDbgInfo();
1351}
1352
1354 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1355 const TargetLibraryInfo *LibraryInfo,
1356 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1358 FunctionVarLocs const *VarLocs) {
1359 MF = &NewMF;
1360 SDAGISelPass = PassPtr;
1361 ORE = &NewORE;
1364 LibInfo = LibraryInfo;
1365 Context = &MF->getFunction().getContext();
1366 UA = NewUA;
1367 PSI = PSIin;
1368 BFI = BFIin;
1369 MMI = &MMIin;
1370 FnVarLocs = VarLocs;
1371}
1372
1374 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1375 allnodes_clear();
1376 OperandRecycler.clear(OperandAllocator);
1377 delete DbgInfo;
1378}
1379
1381 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1382}
1383
1384void SelectionDAG::allnodes_clear() {
1385 assert(&*AllNodes.begin() == &EntryNode);
1386 AllNodes.remove(AllNodes.begin());
1387 while (!AllNodes.empty())
1388 DeallocateNode(&AllNodes.front());
1389#ifndef NDEBUG
1390 NextPersistentId = 0;
1391#endif
1392}
1393
1394SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1395 void *&InsertPos) {
1396 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1397 if (N) {
1398 switch (N->getOpcode()) {
1399 default: break;
1400 case ISD::Constant:
1401 case ISD::ConstantFP:
1402 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1403 "debug location. Use another overload.");
1404 }
1405 }
1406 return N;
1407}
1408
1409SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1410 const SDLoc &DL, void *&InsertPos) {
1411 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1412 if (N) {
1413 switch (N->getOpcode()) {
1414 case ISD::Constant:
1415 case ISD::ConstantFP:
1416 // Erase debug location from the node if the node is used at several
1417 // different places. Do not propagate one location to all uses as it
1418 // will cause a worse single stepping debugging experience.
1419 if (N->getDebugLoc() != DL.getDebugLoc())
1420 N->setDebugLoc(DebugLoc());
1421 break;
1422 default:
1423 // When the node's point of use is located earlier in the instruction
1424 // sequence than its prior point of use, update its debug info to the
1425 // earlier location.
1426 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1427 N->setDebugLoc(DL.getDebugLoc());
1428 break;
1429 }
1430 }
1431 return N;
1432}
1433
1435 allnodes_clear();
1436 OperandRecycler.clear(OperandAllocator);
1437 OperandAllocator.Reset();
1438 CSEMap.clear();
1439
1440 ExtendedValueTypeNodes.clear();
1441 ExternalSymbols.clear();
1442 TargetExternalSymbols.clear();
1443 MCSymbols.clear();
1444 SDEI.clear();
1445 std::fill(CondCodeNodes.begin(), CondCodeNodes.end(), nullptr);
1446 std::fill(ValueTypeNodes.begin(), ValueTypeNodes.end(), nullptr);
1447
1448 EntryNode.UseList = nullptr;
1449 InsertNode(&EntryNode);
1450 Root = getEntryNode();
1451 DbgInfo->clear();
1452}
1453
1455 return VT.bitsGT(Op.getValueType())
1456 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1457 : getNode(ISD::FP_ROUND, DL, VT, Op,
1458 getIntPtrConstant(0, DL, /*isTarget=*/true));
1459}
1460
1461std::pair<SDValue, SDValue>
1463 const SDLoc &DL, EVT VT) {
1464 assert(!VT.bitsEq(Op.getValueType()) &&
1465 "Strict no-op FP extend/round not allowed.");
1466 SDValue Res =
1467 VT.bitsGT(Op.getValueType())
1468 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1469 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1470 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1471
1472 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1473}
1474
1476 return VT.bitsGT(Op.getValueType()) ?
1477 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1478 getNode(ISD::TRUNCATE, DL, VT, Op);
1479}
1480
1482 return VT.bitsGT(Op.getValueType()) ?
1483 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1484 getNode(ISD::TRUNCATE, DL, VT, Op);
1485}
1486
1488 return VT.bitsGT(Op.getValueType()) ?
1489 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1490 getNode(ISD::TRUNCATE, DL, VT, Op);
1491}
1492
1494 EVT VT) {
1495 assert(!VT.isVector());
1496 auto Type = Op.getValueType();
1497 SDValue DestOp;
1498 if (Type == VT)
1499 return Op;
1500 auto Size = Op.getValueSizeInBits();
1501 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1502 if (DestOp.getValueType() == VT)
1503 return DestOp;
1504
1505 return getAnyExtOrTrunc(DestOp, DL, VT);
1506}
1507
1509 EVT VT) {
1510 assert(!VT.isVector());
1511 auto Type = Op.getValueType();
1512 SDValue DestOp;
1513 if (Type == VT)
1514 return Op;
1515 auto Size = Op.getValueSizeInBits();
1516 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1517 if (DestOp.getValueType() == VT)
1518 return DestOp;
1519
1520 return getSExtOrTrunc(DestOp, DL, VT);
1521}
1522
1524 EVT VT) {
1525 assert(!VT.isVector());
1526 auto Type = Op.getValueType();
1527 SDValue DestOp;
1528 if (Type == VT)
1529 return Op;
1530 auto Size = Op.getValueSizeInBits();
1531 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1532 if (DestOp.getValueType() == VT)
1533 return DestOp;
1534
1535 return getZExtOrTrunc(DestOp, DL, VT);
1536}
1537
1539 EVT OpVT) {
1540 if (VT.bitsLE(Op.getValueType()))
1541 return getNode(ISD::TRUNCATE, SL, VT, Op);
1542
1544 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1545}
1546
1548 EVT OpVT = Op.getValueType();
1549 assert(VT.isInteger() && OpVT.isInteger() &&
1550 "Cannot getZeroExtendInReg FP types");
1551 assert(VT.isVector() == OpVT.isVector() &&
1552 "getZeroExtendInReg type should be vector iff the operand "
1553 "type is vector!");
1554 assert((!VT.isVector() ||
1556 "Vector element counts must match in getZeroExtendInReg");
1557 assert(VT.bitsLE(OpVT) && "Not extending!");
1558 if (OpVT == VT)
1559 return Op;
1561 VT.getScalarSizeInBits());
1562 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1563}
1564
1566 SDValue EVL, const SDLoc &DL,
1567 EVT VT) {
1568 EVT OpVT = Op.getValueType();
1569 assert(VT.isInteger() && OpVT.isInteger() &&
1570 "Cannot getVPZeroExtendInReg FP types");
1571 assert(VT.isVector() && OpVT.isVector() &&
1572 "getVPZeroExtendInReg type and operand type should be vector!");
1574 "Vector element counts must match in getZeroExtendInReg");
1575 assert(VT.bitsLE(OpVT) && "Not extending!");
1576 if (OpVT == VT)
1577 return Op;
1579 VT.getScalarSizeInBits());
1580 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1581 EVL);
1582}
1583
1585 // Only unsigned pointer semantics are supported right now. In the future this
1586 // might delegate to TLI to check pointer signedness.
1587 return getZExtOrTrunc(Op, DL, VT);
1588}
1589
1591 // Only unsigned pointer semantics are supported right now. In the future this
1592 // might delegate to TLI to check pointer signedness.
1593 return getZeroExtendInReg(Op, DL, VT);
1594}
1595
1597 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1598}
1599
1600/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1602 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1603}
1604
1606 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1607 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1608}
1609
1611 SDValue Mask, SDValue EVL, EVT VT) {
1612 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1613 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1614}
1615
1617 SDValue Mask, SDValue EVL) {
1618 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1619}
1620
1622 SDValue Mask, SDValue EVL) {
1623 if (VT.bitsGT(Op.getValueType()))
1624 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1625 if (VT.bitsLT(Op.getValueType()))
1626 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1627 return Op;
1628}
1629
1631 EVT OpVT) {
1632 if (!V)
1633 return getConstant(0, DL, VT);
1634
1635 switch (TLI->getBooleanContents(OpVT)) {
1638 return getConstant(1, DL, VT);
1640 return getAllOnesConstant(DL, VT);
1641 }
1642 llvm_unreachable("Unexpected boolean content enum!");
1643}
1644
1646 bool isT, bool isO) {
1647 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1648 DL, VT, isT, isO);
1649}
1650
1652 bool isT, bool isO) {
1653 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1654}
1655
1657 EVT VT, bool isT, bool isO) {
1658 assert(VT.isInteger() && "Cannot create FP integer constant!");
1659
1660 EVT EltVT = VT.getScalarType();
1661 const ConstantInt *Elt = &Val;
1662
1663 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1664 // to-be-splatted scalar ConstantInt.
1665 if (isa<VectorType>(Elt->getType()))
1666 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1667
1668 // In some cases the vector type is legal but the element type is illegal and
1669 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1670 // inserted value (the type does not need to match the vector element type).
1671 // Any extra bits introduced will be truncated away.
1672 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1674 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1675 APInt NewVal;
1676 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1677 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1678 else
1679 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1680 Elt = ConstantInt::get(*getContext(), NewVal);
1681 }
1682 // In other cases the element type is illegal and needs to be expanded, for
1683 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1684 // the value into n parts and use a vector type with n-times the elements.
1685 // Then bitcast to the type requested.
1686 // Legalizing constants too early makes the DAGCombiner's job harder so we
1687 // only legalize if the DAG tells us we must produce legal types.
1688 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1689 TLI->getTypeAction(*getContext(), EltVT) ==
1691 const APInt &NewVal = Elt->getValue();
1692 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1693 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1694
1695 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1696 if (VT.isScalableVector() ||
1698 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1699 "Can only handle an even split!");
1700 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1701
1702 SmallVector<SDValue, 2> ScalarParts;
1703 for (unsigned i = 0; i != Parts; ++i)
1704 ScalarParts.push_back(getConstant(
1705 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1706 ViaEltVT, isT, isO));
1707
1708 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1709 }
1710
1711 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1712 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1713
1714 // Check the temporary vector is the correct size. If this fails then
1715 // getTypeToTransformTo() probably returned a type whose size (in bits)
1716 // isn't a power-of-2 factor of the requested type size.
1717 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1718
1719 SmallVector<SDValue, 2> EltParts;
1720 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1721 EltParts.push_back(getConstant(
1722 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1723 ViaEltVT, isT, isO));
1724
1725 // EltParts is currently in little endian order. If we actually want
1726 // big-endian order then reverse it now.
1727 if (getDataLayout().isBigEndian())
1728 std::reverse(EltParts.begin(), EltParts.end());
1729
1730 // The elements must be reversed when the element order is different
1731 // to the endianness of the elements (because the BITCAST is itself a
1732 // vector shuffle in this situation). However, we do not need any code to
1733 // perform this reversal because getConstant() is producing a vector
1734 // splat.
1735 // This situation occurs in MIPS MSA.
1736
1738 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1739 llvm::append_range(Ops, EltParts);
1740
1741 SDValue V =
1742 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1743 return V;
1744 }
1745
1746 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1747 "APInt size does not match type size!");
1748 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1749 SDVTList VTs = getVTList(EltVT);
1751 AddNodeIDNode(ID, Opc, VTs, {});
1752 ID.AddPointer(Elt);
1753 ID.AddBoolean(isO);
1754 void *IP = nullptr;
1755 SDNode *N = nullptr;
1756 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1757 if (!VT.isVector())
1758 return SDValue(N, 0);
1759
1760 if (!N) {
1761 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1762 CSEMap.InsertNode(N, IP);
1763 InsertNode(N);
1764 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1765 }
1766
1767 SDValue Result(N, 0);
1768 if (VT.isVector())
1769 Result = getSplat(VT, DL, Result);
1770 return Result;
1771}
1772
1774 bool isT, bool isO) {
1775 unsigned Size = VT.getScalarSizeInBits();
1776 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1777}
1778
1780 bool IsOpaque) {
1782 IsTarget, IsOpaque);
1783}
1784
1786 bool isTarget) {
1787 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1788}
1789
1791 const SDLoc &DL) {
1792 assert(VT.isInteger() && "Shift amount is not an integer type!");
1793 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1794 return getConstant(Val, DL, ShiftVT);
1795}
1796
1798 const SDLoc &DL) {
1799 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1800 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1801}
1802
1804 bool isTarget) {
1805 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1806}
1807
1809 bool isTarget) {
1810 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1811}
1812
1814 EVT VT, bool isTarget) {
1815 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1816
1817 EVT EltVT = VT.getScalarType();
1818 const ConstantFP *Elt = &V;
1819
1820 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1821 // the to-be-splatted scalar ConstantFP.
1822 if (isa<VectorType>(Elt->getType()))
1823 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1824
1825 // Do the map lookup using the actual bit pattern for the floating point
1826 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1827 // we don't have issues with SNANs.
1828 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1829 SDVTList VTs = getVTList(EltVT);
1831 AddNodeIDNode(ID, Opc, VTs, {});
1832 ID.AddPointer(Elt);
1833 void *IP = nullptr;
1834 SDNode *N = nullptr;
1835 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1836 if (!VT.isVector())
1837 return SDValue(N, 0);
1838
1839 if (!N) {
1840 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1841 CSEMap.InsertNode(N, IP);
1842 InsertNode(N);
1843 }
1844
1845 SDValue Result(N, 0);
1846 if (VT.isVector())
1847 Result = getSplat(VT, DL, Result);
1848 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1849 return Result;
1850}
1851
1853 bool isTarget) {
1854 EVT EltVT = VT.getScalarType();
1855 if (EltVT == MVT::f32)
1856 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1857 if (EltVT == MVT::f64)
1858 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1859 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1860 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1861 bool Ignored;
1862 APFloat APF = APFloat(Val);
1864 &Ignored);
1865 return getConstantFP(APF, DL, VT, isTarget);
1866 }
1867 llvm_unreachable("Unsupported type in getConstantFP");
1868}
1869
1871 EVT VT, int64_t Offset, bool isTargetGA,
1872 unsigned TargetFlags) {
1873 assert((TargetFlags == 0 || isTargetGA) &&
1874 "Cannot set target flags on target-independent globals");
1875
1876 // Truncate (with sign-extension) the offset value to the pointer size.
1878 if (BitWidth < 64)
1880
1881 unsigned Opc;
1882 if (GV->isThreadLocal())
1884 else
1885 Opc = isTargetGA ? ISD::TargetGlobalAddress : ISD::GlobalAddress;
1886
1887 SDVTList VTs = getVTList(VT);
1889 AddNodeIDNode(ID, Opc, VTs, {});
1890 ID.AddPointer(GV);
1891 ID.AddInteger(Offset);
1892 ID.AddInteger(TargetFlags);
1893 void *IP = nullptr;
1894 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1895 return SDValue(E, 0);
1896
1897 auto *N = newSDNode<GlobalAddressSDNode>(
1898 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1899 CSEMap.InsertNode(N, IP);
1900 InsertNode(N);
1901 return SDValue(N, 0);
1902}
1903
1904SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1905 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1906 SDVTList VTs = getVTList(VT);
1908 AddNodeIDNode(ID, Opc, VTs, {});
1909 ID.AddInteger(FI);
1910 void *IP = nullptr;
1911 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1912 return SDValue(E, 0);
1913
1914 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1915 CSEMap.InsertNode(N, IP);
1916 InsertNode(N);
1917 return SDValue(N, 0);
1918}
1919
1920SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1921 unsigned TargetFlags) {
1922 assert((TargetFlags == 0 || isTarget) &&
1923 "Cannot set target flags on target-independent jump tables");
1924 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1925 SDVTList VTs = getVTList(VT);
1927 AddNodeIDNode(ID, Opc, VTs, {});
1928 ID.AddInteger(JTI);
1929 ID.AddInteger(TargetFlags);
1930 void *IP = nullptr;
1931 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1932 return SDValue(E, 0);
1933
1934 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1935 CSEMap.InsertNode(N, IP);
1936 InsertNode(N);
1937 return SDValue(N, 0);
1938}
1939
1941 const SDLoc &DL) {
1943 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1944 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1945}
1946
1948 MaybeAlign Alignment, int Offset,
1949 bool isTarget, unsigned TargetFlags) {
1950 assert((TargetFlags == 0 || isTarget) &&
1951 "Cannot set target flags on target-independent globals");
1952 if (!Alignment)
1953 Alignment = shouldOptForSize()
1954 ? getDataLayout().getABITypeAlign(C->getType())
1955 : getDataLayout().getPrefTypeAlign(C->getType());
1956 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1957 SDVTList VTs = getVTList(VT);
1959 AddNodeIDNode(ID, Opc, VTs, {});
1960 ID.AddInteger(Alignment->value());
1961 ID.AddInteger(Offset);
1962 ID.AddPointer(C);
1963 ID.AddInteger(TargetFlags);
1964 void *IP = nullptr;
1965 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1966 return SDValue(E, 0);
1967
1968 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1969 TargetFlags);
1970 CSEMap.InsertNode(N, IP);
1971 InsertNode(N);
1972 SDValue V = SDValue(N, 0);
1973 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1974 return V;
1975}
1976
1978 MaybeAlign Alignment, int Offset,
1979 bool isTarget, unsigned TargetFlags) {
1980 assert((TargetFlags == 0 || isTarget) &&
1981 "Cannot set target flags on target-independent globals");
1982 if (!Alignment)
1983 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1984 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1985 SDVTList VTs = getVTList(VT);
1987 AddNodeIDNode(ID, Opc, VTs, {});
1988 ID.AddInteger(Alignment->value());
1989 ID.AddInteger(Offset);
1990 C->addSelectionDAGCSEId(ID);
1991 ID.AddInteger(TargetFlags);
1992 void *IP = nullptr;
1993 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1994 return SDValue(E, 0);
1995
1996 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1997 TargetFlags);
1998 CSEMap.InsertNode(N, IP);
1999 InsertNode(N);
2000 return SDValue(N, 0);
2001}
2002
2005 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2006 ID.AddPointer(MBB);
2007 void *IP = nullptr;
2008 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2009 return SDValue(E, 0);
2010
2011 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2012 CSEMap.InsertNode(N, IP);
2013 InsertNode(N);
2014 return SDValue(N, 0);
2015}
2016
2018 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2019 ValueTypeNodes.size())
2020 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2021
2022 SDNode *&N = VT.isExtended() ?
2023 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2024
2025 if (N) return SDValue(N, 0);
2026 N = newSDNode<VTSDNode>(VT);
2027 InsertNode(N);
2028 return SDValue(N, 0);
2029}
2030
2032 SDNode *&N = ExternalSymbols[Sym];
2033 if (N) return SDValue(N, 0);
2034 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2035 InsertNode(N);
2036 return SDValue(N, 0);
2037}
2038
2040 SDNode *&N = MCSymbols[Sym];
2041 if (N)
2042 return SDValue(N, 0);
2043 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2044 InsertNode(N);
2045 return SDValue(N, 0);
2046}
2047
2049 unsigned TargetFlags) {
2050 SDNode *&N =
2051 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2052 if (N) return SDValue(N, 0);
2053 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2054 InsertNode(N);
2055 return SDValue(N, 0);
2056}
2057
2059 if ((unsigned)Cond >= CondCodeNodes.size())
2060 CondCodeNodes.resize(Cond+1);
2061
2062 if (!CondCodeNodes[Cond]) {
2063 auto *N = newSDNode<CondCodeSDNode>(Cond);
2064 CondCodeNodes[Cond] = N;
2065 InsertNode(N);
2066 }
2067
2068 return SDValue(CondCodeNodes[Cond], 0);
2069}
2070
2072 bool ConstantFold) {
2073 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2074 "APInt size does not match type size!");
2075
2076 if (MulImm == 0)
2077 return getConstant(0, DL, VT);
2078
2079 if (ConstantFold) {
2080 const MachineFunction &MF = getMachineFunction();
2081 const Function &F = MF.getFunction();
2082 ConstantRange CR = getVScaleRange(&F, 64);
2083 if (const APInt *C = CR.getSingleElement())
2084 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2085 }
2086
2087 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2088}
2089
2091 bool ConstantFold) {
2092 if (EC.isScalable())
2093 return getVScale(DL, VT,
2094 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2095
2096 return getConstant(EC.getKnownMinValue(), DL, VT);
2097}
2098
2100 APInt One(ResVT.getScalarSizeInBits(), 1);
2101 return getStepVector(DL, ResVT, One);
2102}
2103
2105 const APInt &StepVal) {
2106 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2107 if (ResVT.isScalableVector())
2108 return getNode(
2109 ISD::STEP_VECTOR, DL, ResVT,
2110 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2111
2112 SmallVector<SDValue, 16> OpsStepConstants;
2113 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2114 OpsStepConstants.push_back(
2115 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2116 return getBuildVector(ResVT, DL, OpsStepConstants);
2117}
2118
2119/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2120/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2122 std::swap(N1, N2);
2124}
2125
2127 SDValue N2, ArrayRef<int> Mask) {
2128 assert(VT.getVectorNumElements() == Mask.size() &&
2129 "Must have the same number of vector elements as mask elements!");
2130 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2131 "Invalid VECTOR_SHUFFLE");
2132
2133 // Canonicalize shuffle undef, undef -> undef
2134 if (N1.isUndef() && N2.isUndef())
2135 return getUNDEF(VT);
2136
2137 // Validate that all indices in Mask are within the range of the elements
2138 // input to the shuffle.
2139 int NElts = Mask.size();
2140 assert(llvm::all_of(Mask,
2141 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2142 "Index out of range");
2143
2144 // Copy the mask so we can do any needed cleanup.
2145 SmallVector<int, 8> MaskVec(Mask);
2146
2147 // Canonicalize shuffle v, v -> v, undef
2148 if (N1 == N2) {
2149 N2 = getUNDEF(VT);
2150 for (int i = 0; i != NElts; ++i)
2151 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2152 }
2153
2154 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2155 if (N1.isUndef())
2156 commuteShuffle(N1, N2, MaskVec);
2157
2158 if (TLI->hasVectorBlend()) {
2159 // If shuffling a splat, try to blend the splat instead. We do this here so
2160 // that even when this arises during lowering we don't have to re-handle it.
2161 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2162 BitVector UndefElements;
2163 SDValue Splat = BV->getSplatValue(&UndefElements);
2164 if (!Splat)
2165 return;
2166
2167 for (int i = 0; i < NElts; ++i) {
2168 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2169 continue;
2170
2171 // If this input comes from undef, mark it as such.
2172 if (UndefElements[MaskVec[i] - Offset]) {
2173 MaskVec[i] = -1;
2174 continue;
2175 }
2176
2177 // If we can blend a non-undef lane, use that instead.
2178 if (!UndefElements[i])
2179 MaskVec[i] = i + Offset;
2180 }
2181 };
2182 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2183 BlendSplat(N1BV, 0);
2184 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2185 BlendSplat(N2BV, NElts);
2186 }
2187
2188 // Canonicalize all index into lhs, -> shuffle lhs, undef
2189 // Canonicalize all index into rhs, -> shuffle rhs, undef
2190 bool AllLHS = true, AllRHS = true;
2191 bool N2Undef = N2.isUndef();
2192 for (int i = 0; i != NElts; ++i) {
2193 if (MaskVec[i] >= NElts) {
2194 if (N2Undef)
2195 MaskVec[i] = -1;
2196 else
2197 AllLHS = false;
2198 } else if (MaskVec[i] >= 0) {
2199 AllRHS = false;
2200 }
2201 }
2202 if (AllLHS && AllRHS)
2203 return getUNDEF(VT);
2204 if (AllLHS && !N2Undef)
2205 N2 = getUNDEF(VT);
2206 if (AllRHS) {
2207 N1 = getUNDEF(VT);
2208 commuteShuffle(N1, N2, MaskVec);
2209 }
2210 // Reset our undef status after accounting for the mask.
2211 N2Undef = N2.isUndef();
2212 // Re-check whether both sides ended up undef.
2213 if (N1.isUndef() && N2Undef)
2214 return getUNDEF(VT);
2215
2216 // If Identity shuffle return that node.
2217 bool Identity = true, AllSame = true;
2218 for (int i = 0; i != NElts; ++i) {
2219 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2220 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2221 }
2222 if (Identity && NElts)
2223 return N1;
2224
2225 // Shuffling a constant splat doesn't change the result.
2226 if (N2Undef) {
2227 SDValue V = N1;
2228
2229 // Look through any bitcasts. We check that these don't change the number
2230 // (and size) of elements and just changes their types.
2231 while (V.getOpcode() == ISD::BITCAST)
2232 V = V->getOperand(0);
2233
2234 // A splat should always show up as a build vector node.
2235 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2236 BitVector UndefElements;
2237 SDValue Splat = BV->getSplatValue(&UndefElements);
2238 // If this is a splat of an undef, shuffling it is also undef.
2239 if (Splat && Splat.isUndef())
2240 return getUNDEF(VT);
2241
2242 bool SameNumElts =
2243 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2244
2245 // We only have a splat which can skip shuffles if there is a splatted
2246 // value and no undef lanes rearranged by the shuffle.
2247 if (Splat && UndefElements.none()) {
2248 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2249 // number of elements match or the value splatted is a zero constant.
2250 if (SameNumElts || isNullConstant(Splat))
2251 return N1;
2252 }
2253
2254 // If the shuffle itself creates a splat, build the vector directly.
2255 if (AllSame && SameNumElts) {
2256 EVT BuildVT = BV->getValueType(0);
2257 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2258 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2259
2260 // We may have jumped through bitcasts, so the type of the
2261 // BUILD_VECTOR may not match the type of the shuffle.
2262 if (BuildVT != VT)
2263 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2264 return NewBV;
2265 }
2266 }
2267 }
2268
2269 SDVTList VTs = getVTList(VT);
2271 SDValue Ops[2] = { N1, N2 };
2273 for (int i = 0; i != NElts; ++i)
2274 ID.AddInteger(MaskVec[i]);
2275
2276 void* IP = nullptr;
2277 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2278 return SDValue(E, 0);
2279
2280 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2281 // SDNode doesn't have access to it. This memory will be "leaked" when
2282 // the node is deallocated, but recovered when the NodeAllocator is released.
2283 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2284 llvm::copy(MaskVec, MaskAlloc);
2285
2286 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2287 dl.getDebugLoc(), MaskAlloc);
2288 createOperands(N, Ops);
2289
2290 CSEMap.InsertNode(N, IP);
2291 InsertNode(N);
2292 SDValue V = SDValue(N, 0);
2293 NewSDValueDbgMsg(V, "Creating new node: ", this);
2294 return V;
2295}
2296
2298 EVT VT = SV.getValueType(0);
2299 SmallVector<int, 8> MaskVec(SV.getMask());
2301
2302 SDValue Op0 = SV.getOperand(0);
2303 SDValue Op1 = SV.getOperand(1);
2304 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2305}
2306
2308 SDVTList VTs = getVTList(VT);
2310 AddNodeIDNode(ID, ISD::Register, VTs, {});
2311 ID.AddInteger(Reg.id());
2312 void *IP = nullptr;
2313 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2314 return SDValue(E, 0);
2315
2316 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2317 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2318 CSEMap.InsertNode(N, IP);
2319 InsertNode(N);
2320 return SDValue(N, 0);
2321}
2322
2325 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2326 ID.AddPointer(RegMask);
2327 void *IP = nullptr;
2328 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2329 return SDValue(E, 0);
2330
2331 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2332 CSEMap.InsertNode(N, IP);
2333 InsertNode(N);
2334 return SDValue(N, 0);
2335}
2336
2338 MCSymbol *Label) {
2339 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2340}
2341
2342SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2343 SDValue Root, MCSymbol *Label) {
2345 SDValue Ops[] = { Root };
2346 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2347 ID.AddPointer(Label);
2348 void *IP = nullptr;
2349 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2350 return SDValue(E, 0);
2351
2352 auto *N =
2353 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2354 createOperands(N, Ops);
2355
2356 CSEMap.InsertNode(N, IP);
2357 InsertNode(N);
2358 return SDValue(N, 0);
2359}
2360
2362 int64_t Offset, bool isTarget,
2363 unsigned TargetFlags) {
2364 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2365 SDVTList VTs = getVTList(VT);
2366
2368 AddNodeIDNode(ID, Opc, VTs, {});
2369 ID.AddPointer(BA);
2370 ID.AddInteger(Offset);
2371 ID.AddInteger(TargetFlags);
2372 void *IP = nullptr;
2373 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2374 return SDValue(E, 0);
2375
2376 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2377 CSEMap.InsertNode(N, IP);
2378 InsertNode(N);
2379 return SDValue(N, 0);
2380}
2381
2384 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2385 ID.AddPointer(V);
2386
2387 void *IP = nullptr;
2388 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2389 return SDValue(E, 0);
2390
2391 auto *N = newSDNode<SrcValueSDNode>(V);
2392 CSEMap.InsertNode(N, IP);
2393 InsertNode(N);
2394 return SDValue(N, 0);
2395}
2396
2399 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2400 ID.AddPointer(MD);
2401
2402 void *IP = nullptr;
2403 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2404 return SDValue(E, 0);
2405
2406 auto *N = newSDNode<MDNodeSDNode>(MD);
2407 CSEMap.InsertNode(N, IP);
2408 InsertNode(N);
2409 return SDValue(N, 0);
2410}
2411
2413 if (VT == V.getValueType())
2414 return V;
2415
2416 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2417}
2418
2420 unsigned SrcAS, unsigned DestAS) {
2421 SDVTList VTs = getVTList(VT);
2422 SDValue Ops[] = {Ptr};
2425 ID.AddInteger(SrcAS);
2426 ID.AddInteger(DestAS);
2427
2428 void *IP = nullptr;
2429 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2430 return SDValue(E, 0);
2431
2432 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2433 VTs, SrcAS, DestAS);
2434 createOperands(N, Ops);
2435
2436 CSEMap.InsertNode(N, IP);
2437 InsertNode(N);
2438 return SDValue(N, 0);
2439}
2440
2442 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2443}
2444
2445/// getShiftAmountOperand - Return the specified value casted to
2446/// the target's desired shift amount type.
2448 EVT OpTy = Op.getValueType();
2449 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2450 if (OpTy == ShTy || OpTy.isVector()) return Op;
2451
2452 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2453}
2454
2456 SDValue Op2) {
2457 EVT FullTy = Op2.getValueType();
2458
2459 unsigned Stride = ReducedTy.getVectorMinNumElements();
2460 unsigned ScaleFactor = FullTy.getVectorMinNumElements() / Stride;
2461
2462 // Collect all of the subvectors
2463 std::deque<SDValue> Subvectors = {Op1};
2464 for (unsigned I = 0; I < ScaleFactor; I++) {
2465 auto SourceIndex = getVectorIdxConstant(I * Stride, DL);
2466 Subvectors.push_back(
2467 getNode(ISD::EXTRACT_SUBVECTOR, DL, ReducedTy, {Op2, SourceIndex}));
2468 }
2469
2470 // Flatten the subvector tree
2471 while (Subvectors.size() > 1) {
2472 Subvectors.push_back(
2473 getNode(ISD::ADD, DL, ReducedTy, {Subvectors[0], Subvectors[1]}));
2474 Subvectors.pop_front();
2475 Subvectors.pop_front();
2476 }
2477
2478 assert(Subvectors.size() == 1 &&
2479 "There should only be one subvector after tree flattening");
2480
2481 return Subvectors[0];
2482}
2483
2484/// Given a store node \p StoreNode, return true if it is safe to fold that node
2485/// into \p FPNode, which expands to a library call with output pointers.
2487 SDNode *FPNode) {
2489 SmallVector<const SDNode *, 8> DeferredNodes;
2491
2492 // Skip FPNode use by StoreNode (that's the use we want to fold into FPNode).
2493 for (SDValue Op : StoreNode->ops())
2494 if (Op.getNode() != FPNode)
2495 Worklist.push_back(Op.getNode());
2496
2498 while (!Worklist.empty()) {
2499 const SDNode *Node = Worklist.pop_back_val();
2500 auto [_, Inserted] = Visited.insert(Node);
2501 if (!Inserted)
2502 continue;
2503
2504 if (MaxSteps > 0 && Visited.size() >= MaxSteps)
2505 return false;
2506
2507 // Reached the FPNode (would result in a cycle).
2508 // OR Reached CALLSEQ_START (would result in nested call sequences).
2509 if (Node == FPNode || Node->getOpcode() == ISD::CALLSEQ_START)
2510 return false;
2511
2512 if (Node->getOpcode() == ISD::CALLSEQ_END) {
2513 // Defer looking into call sequences (so we can check we're outside one).
2514 // We still need to look through these for the predecessor check.
2515 DeferredNodes.push_back(Node);
2516 continue;
2517 }
2518
2519 for (SDValue Op : Node->ops())
2520 Worklist.push_back(Op.getNode());
2521 }
2522
2523 // True if we're outside a call sequence and don't have the FPNode as a
2524 // predecessor. No cycles or nested call sequences possible.
2525 return !SDNode::hasPredecessorHelper(FPNode, Visited, DeferredNodes,
2526 MaxSteps);
2527}
2528
2531 std::optional<unsigned> CallRetResNo) {
2532 LLVMContext &Ctx = *getContext();
2533 EVT VT = Node->getValueType(0);
2534 unsigned NumResults = Node->getNumValues();
2535
2536 const char *LCName = TLI->getLibcallName(LC);
2537 if (!LC || !LCName)
2538 return false;
2539
2540 auto getVecDesc = [&]() -> VecDesc const * {
2541 for (bool Masked : {false, true}) {
2542 if (VecDesc const *VD = getLibInfo().getVectorMappingInfo(
2543 LCName, VT.getVectorElementCount(), Masked)) {
2544 return VD;
2545 }
2546 }
2547 return nullptr;
2548 };
2549
2550 // For vector types, we must find a vector mapping for the libcall.
2551 VecDesc const *VD = nullptr;
2552 if (VT.isVector() && !(VD = getVecDesc()))
2553 return false;
2554
2555 // Find users of the node that store the results (and share input chains). The
2556 // destination pointers can be used instead of creating stack allocations.
2557 SDValue StoresInChain;
2558 SmallVector<StoreSDNode *, 2> ResultStores(NumResults);
2559 for (SDNode *User : Node->users()) {
2561 continue;
2562 auto *ST = cast<StoreSDNode>(User);
2563 SDValue StoreValue = ST->getValue();
2564 unsigned ResNo = StoreValue.getResNo();
2565 // Ensure the store corresponds to an output pointer.
2566 if (CallRetResNo == ResNo)
2567 continue;
2568 // Ensure the store to the default address space and not atomic or volatile.
2569 if (!ST->isSimple() || ST->getAddressSpace() != 0)
2570 continue;
2571 // Ensure all store chains are the same (so they don't alias).
2572 if (StoresInChain && ST->getChain() != StoresInChain)
2573 continue;
2574 // Ensure the store is properly aligned.
2575 Type *StoreType = StoreValue.getValueType().getTypeForEVT(Ctx);
2576 if (ST->getAlign() <
2577 getDataLayout().getABITypeAlign(StoreType->getScalarType()))
2578 continue;
2579 // Avoid:
2580 // 1. Creating cyclic dependencies.
2581 // 2. Expanding the node to a call within a call sequence.
2583 continue;
2584 ResultStores[ResNo] = ST;
2585 StoresInChain = ST->getChain();
2586 }
2587
2589 auto AddArgListEntry = [&](SDValue Node, Type *Ty) {
2591 Entry.Ty = Ty;
2592 Entry.Node = Node;
2593 Args.push_back(Entry);
2594 };
2595
2596 // Pass the arguments.
2597 for (const SDValue &Op : Node->op_values()) {
2598 EVT ArgVT = Op.getValueType();
2599 Type *ArgTy = ArgVT.getTypeForEVT(Ctx);
2600 AddArgListEntry(Op, ArgTy);
2601 }
2602
2603 // Pass the output pointers.
2604 SmallVector<SDValue, 2> ResultPtrs(NumResults);
2606 for (auto [ResNo, ST] : llvm::enumerate(ResultStores)) {
2607 if (ResNo == CallRetResNo)
2608 continue;
2609 EVT ResVT = Node->getValueType(ResNo);
2610 SDValue ResultPtr = ST ? ST->getBasePtr() : CreateStackTemporary(ResVT);
2611 ResultPtrs[ResNo] = ResultPtr;
2612 AddArgListEntry(ResultPtr, PointerTy);
2613 }
2614
2615 SDLoc DL(Node);
2616
2617 // Pass the vector mask (if required).
2618 if (VD && VD->isMasked()) {
2619 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), Ctx, VT);
2620 SDValue Mask = getBoolConstant(true, DL, MaskVT, VT);
2621 AddArgListEntry(Mask, MaskVT.getTypeForEVT(Ctx));
2622 }
2623
2624 Type *RetType = CallRetResNo.has_value()
2625 ? Node->getValueType(*CallRetResNo).getTypeForEVT(Ctx)
2626 : Type::getVoidTy(Ctx);
2627 SDValue InChain = StoresInChain ? StoresInChain : getEntryNode();
2628 SDValue Callee = getExternalSymbol(VD ? VD->getVectorFnName().data() : LCName,
2629 TLI->getPointerTy(getDataLayout()));
2631 CLI.setDebugLoc(DL).setChain(InChain).setLibCallee(
2632 TLI->getLibcallCallingConv(LC), RetType, Callee, std::move(Args));
2633
2634 auto [Call, CallChain] = TLI->LowerCallTo(CLI);
2635
2636 for (auto [ResNo, ResultPtr] : llvm::enumerate(ResultPtrs)) {
2637 if (ResNo == CallRetResNo) {
2638 Results.push_back(Call);
2639 continue;
2640 }
2641 MachinePointerInfo PtrInfo;
2642 if (StoreSDNode *ST = ResultStores[ResNo]) {
2643 // Replace store with the library call.
2644 ReplaceAllUsesOfValueWith(SDValue(ST, 0), CallChain);
2645 PtrInfo = ST->getPointerInfo();
2646 } else {
2648 getMachineFunction(), cast<FrameIndexSDNode>(ResultPtr)->getIndex());
2649 }
2650 SDValue LoadResult =
2651 getLoad(Node->getValueType(ResNo), DL, CallChain, ResultPtr, PtrInfo);
2652 Results.push_back(LoadResult);
2653 }
2654
2655 if (CallRetResNo && !Node->hasAnyUseOfValue(*CallRetResNo)) {
2656 // FIXME: Find a way to avoid updating the root. This is needed for x86,
2657 // which uses a floating-point stack. If (for example) the node to be
2658 // expanded has two results one floating-point which is returned by the
2659 // call, and one integer result, returned via an output pointer. If only the
2660 // integer result is used then the `CopyFromReg` for the FP result may be
2661 // optimized out. This prevents an FP stack pop from being emitted for it.
2662 // Setting the root like this ensures there will be a use of the
2663 // `CopyFromReg` chain, and ensures the FP pop will be emitted.
2664 SDValue NewRoot =
2665 getNode(ISD::TokenFactor, DL, MVT::Other, getRoot(), CallChain);
2666 setRoot(NewRoot);
2667 // Ensure the new root is reachable from the results.
2668 Results[0] = getMergeValues({Results[0], NewRoot}, DL);
2669 }
2670
2671 return true;
2672}
2673
2675 SDLoc dl(Node);
2677 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2678 EVT VT = Node->getValueType(0);
2679 SDValue Tmp1 = Node->getOperand(0);
2680 SDValue Tmp2 = Node->getOperand(1);
2681 const MaybeAlign MA(Node->getConstantOperandVal(3));
2682
2683 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2684 Tmp2, MachinePointerInfo(V));
2685 SDValue VAList = VAListLoad;
2686
2687 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2688 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2689 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2690
2691 VAList = getNode(
2692 ISD::AND, dl, VAList.getValueType(), VAList,
2693 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2694 }
2695
2696 // Increment the pointer, VAList, to the next vaarg
2697 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2698 getConstant(getDataLayout().getTypeAllocSize(
2699 VT.getTypeForEVT(*getContext())),
2700 dl, VAList.getValueType()));
2701 // Store the incremented VAList to the legalized pointer
2702 Tmp1 =
2703 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2704 // Load the actual argument out of the pointer VAList
2705 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2706}
2707
2709 SDLoc dl(Node);
2711 // This defaults to loading a pointer from the input and storing it to the
2712 // output, returning the chain.
2713 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2714 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2715 SDValue Tmp1 =
2716 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2717 Node->getOperand(2), MachinePointerInfo(VS));
2718 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2719 MachinePointerInfo(VD));
2720}
2721
2723 const DataLayout &DL = getDataLayout();
2724 Type *Ty = VT.getTypeForEVT(*getContext());
2725 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2726
2727 if (TLI->isTypeLegal(VT) || !VT.isVector())
2728 return RedAlign;
2729
2731 const Align StackAlign = TFI->getStackAlign();
2732
2733 // See if we can choose a smaller ABI alignment in cases where it's an
2734 // illegal vector type that will get broken down.
2735 if (RedAlign > StackAlign) {
2736 EVT IntermediateVT;
2737 MVT RegisterVT;
2738 unsigned NumIntermediates;
2739 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2740 NumIntermediates, RegisterVT);
2741 Ty = IntermediateVT.getTypeForEVT(*getContext());
2742 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2743 if (RedAlign2 < RedAlign)
2744 RedAlign = RedAlign2;
2745
2746 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2747 // If the stack is not realignable, the alignment should be limited to the
2748 // StackAlignment
2749 RedAlign = std::min(RedAlign, StackAlign);
2750 }
2751
2752 return RedAlign;
2753}
2754
2756 MachineFrameInfo &MFI = MF->getFrameInfo();
2758 int StackID = 0;
2759 if (Bytes.isScalable())
2760 StackID = TFI->getStackIDForScalableVectors();
2761 // The stack id gives an indication of whether the object is scalable or
2762 // not, so it's safe to pass in the minimum size here.
2763 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2764 false, nullptr, StackID);
2765 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2766}
2767
2769 Type *Ty = VT.getTypeForEVT(*getContext());
2770 Align StackAlign =
2771 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2772 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2773}
2774
2776 TypeSize VT1Size = VT1.getStoreSize();
2777 TypeSize VT2Size = VT2.getStoreSize();
2778 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2779 "Don't know how to choose the maximum size when creating a stack "
2780 "temporary");
2781 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2782 ? VT1Size
2783 : VT2Size;
2784
2785 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2786 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2787 const DataLayout &DL = getDataLayout();
2788 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2789 return CreateStackTemporary(Bytes, Align);
2790}
2791
2793 ISD::CondCode Cond, const SDLoc &dl) {
2794 EVT OpVT = N1.getValueType();
2795
2796 auto GetUndefBooleanConstant = [&]() {
2797 if (VT.getScalarType() == MVT::i1 ||
2798 TLI->getBooleanContents(OpVT) ==
2800 return getUNDEF(VT);
2801 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2802 // so we cannot use getUNDEF(). Return zero instead.
2803 return getConstant(0, dl, VT);
2804 };
2805
2806 // These setcc operations always fold.
2807 switch (Cond) {
2808 default: break;
2809 case ISD::SETFALSE:
2810 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2811 case ISD::SETTRUE:
2812 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2813
2814 case ISD::SETOEQ:
2815 case ISD::SETOGT:
2816 case ISD::SETOGE:
2817 case ISD::SETOLT:
2818 case ISD::SETOLE:
2819 case ISD::SETONE:
2820 case ISD::SETO:
2821 case ISD::SETUO:
2822 case ISD::SETUEQ:
2823 case ISD::SETUNE:
2824 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2825 break;
2826 }
2827
2828 if (OpVT.isInteger()) {
2829 // For EQ and NE, we can always pick a value for the undef to make the
2830 // predicate pass or fail, so we can return undef.
2831 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2832 // icmp eq/ne X, undef -> undef.
2833 if ((N1.isUndef() || N2.isUndef()) &&
2834 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2835 return GetUndefBooleanConstant();
2836
2837 // If both operands are undef, we can return undef for int comparison.
2838 // icmp undef, undef -> undef.
2839 if (N1.isUndef() && N2.isUndef())
2840 return GetUndefBooleanConstant();
2841
2842 // icmp X, X -> true/false
2843 // icmp X, undef -> true/false because undef could be X.
2844 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2845 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2846 }
2847
2848 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2)) {
2849 const APInt &C2 = N2C->getAPIntValue();
2850 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1)) {
2851 const APInt &C1 = N1C->getAPIntValue();
2852
2854 dl, VT, OpVT);
2855 }
2856 }
2857
2858 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2859 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2860
2861 if (N1CFP && N2CFP) {
2862 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2863 switch (Cond) {
2864 default: break;
2865 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2866 return GetUndefBooleanConstant();
2867 [[fallthrough]];
2868 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2869 OpVT);
2870 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2871 return GetUndefBooleanConstant();
2872 [[fallthrough]];
2874 R==APFloat::cmpLessThan, dl, VT,
2875 OpVT);
2876 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2877 return GetUndefBooleanConstant();
2878 [[fallthrough]];
2879 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2880 OpVT);
2881 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2882 return GetUndefBooleanConstant();
2883 [[fallthrough]];
2885 VT, OpVT);
2886 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2887 return GetUndefBooleanConstant();
2888 [[fallthrough]];
2890 R==APFloat::cmpEqual, dl, VT,
2891 OpVT);
2892 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2893 return GetUndefBooleanConstant();
2894 [[fallthrough]];
2896 R==APFloat::cmpEqual, dl, VT, OpVT);
2897 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2898 OpVT);
2899 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2900 OpVT);
2902 R==APFloat::cmpEqual, dl, VT,
2903 OpVT);
2904 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2905 OpVT);
2907 R==APFloat::cmpLessThan, dl, VT,
2908 OpVT);
2910 R==APFloat::cmpUnordered, dl, VT,
2911 OpVT);
2913 VT, OpVT);
2914 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2915 OpVT);
2916 }
2917 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2918 // Ensure that the constant occurs on the RHS.
2920 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2921 return SDValue();
2922 return getSetCC(dl, VT, N2, N1, SwappedCond);
2923 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2924 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2925 // If an operand is known to be a nan (or undef that could be a nan), we can
2926 // fold it.
2927 // Choosing NaN for the undef will always make unordered comparison succeed
2928 // and ordered comparison fails.
2929 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2930 switch (ISD::getUnorderedFlavor(Cond)) {
2931 default:
2932 llvm_unreachable("Unknown flavor!");
2933 case 0: // Known false.
2934 return getBoolConstant(false, dl, VT, OpVT);
2935 case 1: // Known true.
2936 return getBoolConstant(true, dl, VT, OpVT);
2937 case 2: // Undefined.
2938 return GetUndefBooleanConstant();
2939 }
2940 }
2941
2942 // Could not fold it.
2943 return SDValue();
2944}
2945
2946/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2947/// use this predicate to simplify operations downstream.
2949 unsigned BitWidth = Op.getScalarValueSizeInBits();
2951}
2952
2953/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2954/// this predicate to simplify operations downstream. Mask is known to be zero
2955/// for bits that V cannot have.
2957 unsigned Depth) const {
2958 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2959}
2960
2961/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2962/// DemandedElts. We use this predicate to simplify operations downstream.
2963/// Mask is known to be zero for bits that V cannot have.
2965 const APInt &DemandedElts,
2966 unsigned Depth) const {
2967 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2968}
2969
2970/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2971/// DemandedElts. We use this predicate to simplify operations downstream.
2973 unsigned Depth /* = 0 */) const {
2974 return computeKnownBits(V, DemandedElts, Depth).isZero();
2975}
2976
2977/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2979 unsigned Depth) const {
2980 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2981}
2982
2984 const APInt &DemandedElts,
2985 unsigned Depth) const {
2986 EVT VT = Op.getValueType();
2987 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2988
2989 unsigned NumElts = VT.getVectorNumElements();
2990 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2991
2992 APInt KnownZeroElements = APInt::getZero(NumElts);
2993 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2994 if (!DemandedElts[EltIdx])
2995 continue; // Don't query elements that are not demanded.
2996 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2997 if (MaskedVectorIsZero(Op, Mask, Depth))
2998 KnownZeroElements.setBit(EltIdx);
2999 }
3000 return KnownZeroElements;
3001}
3002
3003/// isSplatValue - Return true if the vector V has the same value
3004/// across all DemandedElts. For scalable vectors, we don't know the
3005/// number of lanes at compile time. Instead, we use a 1 bit APInt
3006/// to represent a conservative value for all lanes; that is, that
3007/// one bit value is implicitly splatted across all lanes.
3008bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
3009 APInt &UndefElts, unsigned Depth) const {
3010 unsigned Opcode = V.getOpcode();
3011 EVT VT = V.getValueType();
3012 assert(VT.isVector() && "Vector type expected");
3013 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
3014 "scalable demanded bits are ignored");
3015
3016 if (!DemandedElts)
3017 return false; // No demanded elts, better to assume we don't know anything.
3018
3019 if (Depth >= MaxRecursionDepth)
3020 return false; // Limit search depth.
3021
3022 // Deal with some common cases here that work for both fixed and scalable
3023 // vector types.
3024 switch (Opcode) {
3025 case ISD::SPLAT_VECTOR:
3026 UndefElts = V.getOperand(0).isUndef()
3027 ? APInt::getAllOnes(DemandedElts.getBitWidth())
3028 : APInt(DemandedElts.getBitWidth(), 0);
3029 return true;
3030 case ISD::ADD:
3031 case ISD::SUB:
3032 case ISD::AND:
3033 case ISD::XOR:
3034 case ISD::OR: {
3035 APInt UndefLHS, UndefRHS;
3036 SDValue LHS = V.getOperand(0);
3037 SDValue RHS = V.getOperand(1);
3038 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
3039 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
3040 UndefElts = UndefLHS | UndefRHS;
3041 return true;
3042 }
3043 return false;
3044 }
3045 case ISD::ABS:
3046 case ISD::TRUNCATE:
3047 case ISD::SIGN_EXTEND:
3048 case ISD::ZERO_EXTEND:
3049 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
3050 default:
3051 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
3052 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
3053 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
3054 Depth);
3055 break;
3056}
3057
3058 // We don't support other cases than those above for scalable vectors at
3059 // the moment.
3060 if (VT.isScalableVector())
3061 return false;
3062
3063 unsigned NumElts = VT.getVectorNumElements();
3064 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3065 UndefElts = APInt::getZero(NumElts);
3066
3067 switch (Opcode) {
3068 case ISD::BUILD_VECTOR: {
3069 SDValue Scl;
3070 for (unsigned i = 0; i != NumElts; ++i) {
3071 SDValue Op = V.getOperand(i);
3072 if (Op.isUndef()) {
3073 UndefElts.setBit(i);
3074 continue;
3075 }
3076 if (!DemandedElts[i])
3077 continue;
3078 if (Scl && Scl != Op)
3079 return false;
3080 Scl = Op;
3081 }
3082 return true;
3083 }
3084 case ISD::VECTOR_SHUFFLE: {
3085 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3086 APInt DemandedLHS = APInt::getZero(NumElts);
3087 APInt DemandedRHS = APInt::getZero(NumElts);
3088 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3089 for (int i = 0; i != (int)NumElts; ++i) {
3090 int M = Mask[i];
3091 if (M < 0) {
3092 UndefElts.setBit(i);
3093 continue;
3094 }
3095 if (!DemandedElts[i])
3096 continue;
3097 if (M < (int)NumElts)
3098 DemandedLHS.setBit(M);
3099 else
3100 DemandedRHS.setBit(M - NumElts);
3101 }
3102
3103 // If we aren't demanding either op, assume there's no splat.
3104 // If we are demanding both ops, assume there's no splat.
3105 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3106 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3107 return false;
3108
3109 // See if the demanded elts of the source op is a splat or we only demand
3110 // one element, which should always be a splat.
3111 // TODO: Handle source ops splats with undefs.
3112 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3113 APInt SrcUndefs;
3114 return (SrcElts.popcount() == 1) ||
3115 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3116 (SrcElts & SrcUndefs).isZero());
3117 };
3118 if (!DemandedLHS.isZero())
3119 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3120 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3121 }
3123 // Offset the demanded elts by the subvector index.
3124 SDValue Src = V.getOperand(0);
3125 // We don't support scalable vectors at the moment.
3126 if (Src.getValueType().isScalableVector())
3127 return false;
3128 uint64_t Idx = V.getConstantOperandVal(1);
3129 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3130 APInt UndefSrcElts;
3131 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3132 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3133 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3134 return true;
3135 }
3136 break;
3137 }
3141 // Widen the demanded elts by the src element count.
3142 SDValue Src = V.getOperand(0);
3143 // We don't support scalable vectors at the moment.
3144 if (Src.getValueType().isScalableVector())
3145 return false;
3146 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3147 APInt UndefSrcElts;
3148 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3149 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3150 UndefElts = UndefSrcElts.trunc(NumElts);
3151 return true;
3152 }
3153 break;
3154 }
3155 case ISD::BITCAST: {
3156 SDValue Src = V.getOperand(0);
3157 EVT SrcVT = Src.getValueType();
3158 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3159 unsigned BitWidth = VT.getScalarSizeInBits();
3160
3161 // Ignore bitcasts from unsupported types.
3162 // TODO: Add fp support?
3163 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3164 break;
3165
3166 // Bitcast 'small element' vector to 'large element' vector.
3167 if ((BitWidth % SrcBitWidth) == 0) {
3168 // See if each sub element is a splat.
3169 unsigned Scale = BitWidth / SrcBitWidth;
3170 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3171 APInt ScaledDemandedElts =
3172 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3173 for (unsigned I = 0; I != Scale; ++I) {
3174 APInt SubUndefElts;
3175 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3176 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3177 SubDemandedElts &= ScaledDemandedElts;
3178 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3179 return false;
3180 // TODO: Add support for merging sub undef elements.
3181 if (!SubUndefElts.isZero())
3182 return false;
3183 }
3184 return true;
3185 }
3186 break;
3187 }
3188 }
3189
3190 return false;
3191}
3192
3193/// Helper wrapper to main isSplatValue function.
3194bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3195 EVT VT = V.getValueType();
3196 assert(VT.isVector() && "Vector type expected");
3197
3198 APInt UndefElts;
3199 // Since the number of lanes in a scalable vector is unknown at compile time,
3200 // we track one bit which is implicitly broadcast to all lanes. This means
3201 // that all lanes in a scalable vector are considered demanded.
3202 APInt DemandedElts
3204 return isSplatValue(V, DemandedElts, UndefElts) &&
3205 (AllowUndefs || !UndefElts);
3206}
3207
3210
3211 EVT VT = V.getValueType();
3212 unsigned Opcode = V.getOpcode();
3213 switch (Opcode) {
3214 default: {
3215 APInt UndefElts;
3216 // Since the number of lanes in a scalable vector is unknown at compile time,
3217 // we track one bit which is implicitly broadcast to all lanes. This means
3218 // that all lanes in a scalable vector are considered demanded.
3219 APInt DemandedElts
3221
3222 if (isSplatValue(V, DemandedElts, UndefElts)) {
3223 if (VT.isScalableVector()) {
3224 // DemandedElts and UndefElts are ignored for scalable vectors, since
3225 // the only supported cases are SPLAT_VECTOR nodes.
3226 SplatIdx = 0;
3227 } else {
3228 // Handle case where all demanded elements are UNDEF.
3229 if (DemandedElts.isSubsetOf(UndefElts)) {
3230 SplatIdx = 0;
3231 return getUNDEF(VT);
3232 }
3233 SplatIdx = (UndefElts & DemandedElts).countr_one();
3234 }
3235 return V;
3236 }
3237 break;
3238 }
3239 case ISD::SPLAT_VECTOR:
3240 SplatIdx = 0;
3241 return V;
3242 case ISD::VECTOR_SHUFFLE: {
3243 assert(!VT.isScalableVector());
3244 // Check if this is a shuffle node doing a splat.
3245 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3246 // getTargetVShiftNode currently struggles without the splat source.
3247 auto *SVN = cast<ShuffleVectorSDNode>(V);
3248 if (!SVN->isSplat())
3249 break;
3250 int Idx = SVN->getSplatIndex();
3251 int NumElts = V.getValueType().getVectorNumElements();
3252 SplatIdx = Idx % NumElts;
3253 return V.getOperand(Idx / NumElts);
3254 }
3255 }
3256
3257 return SDValue();
3258}
3259
3261 int SplatIdx;
3262 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3263 EVT SVT = SrcVector.getValueType().getScalarType();
3264 EVT LegalSVT = SVT;
3265 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3266 if (!SVT.isInteger())
3267 return SDValue();
3268 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3269 if (LegalSVT.bitsLT(SVT))
3270 return SDValue();
3271 }
3272 return getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), LegalSVT, SrcVector,
3273 getVectorIdxConstant(SplatIdx, SDLoc(V)));
3274 }
3275 return SDValue();
3276}
3277
3278std::optional<ConstantRange>
3280 unsigned Depth) const {
3281 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3282 V.getOpcode() == ISD::SRA) &&
3283 "Unknown shift node");
3284 // Shifting more than the bitwidth is not valid.
3285 unsigned BitWidth = V.getScalarValueSizeInBits();
3286
3287 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3288 const APInt &ShAmt = Cst->getAPIntValue();
3289 if (ShAmt.uge(BitWidth))
3290 return std::nullopt;
3291 return ConstantRange(ShAmt);
3292 }
3293
3294 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3295 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3296 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3297 if (!DemandedElts[i])
3298 continue;
3299 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3300 if (!SA) {
3301 MinAmt = MaxAmt = nullptr;
3302 break;
3303 }
3304 const APInt &ShAmt = SA->getAPIntValue();
3305 if (ShAmt.uge(BitWidth))
3306 return std::nullopt;
3307 if (!MinAmt || MinAmt->ugt(ShAmt))
3308 MinAmt = &ShAmt;
3309 if (!MaxAmt || MaxAmt->ult(ShAmt))
3310 MaxAmt = &ShAmt;
3311 }
3312 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3313 "Failed to find matching min/max shift amounts");
3314 if (MinAmt && MaxAmt)
3315 return ConstantRange(*MinAmt, *MaxAmt + 1);
3316 }
3317
3318 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3319 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3320 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3321 if (KnownAmt.getMaxValue().ult(BitWidth))
3322 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3323
3324 return std::nullopt;
3325}
3326
3327std::optional<uint64_t>
3329 unsigned Depth) const {
3330 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3331 V.getOpcode() == ISD::SRA) &&
3332 "Unknown shift node");
3333 if (std::optional<ConstantRange> AmtRange =
3334 getValidShiftAmountRange(V, DemandedElts, Depth))
3335 if (const APInt *ShAmt = AmtRange->getSingleElement())
3336 return ShAmt->getZExtValue();
3337 return std::nullopt;
3338}
3339
3340std::optional<uint64_t>
3342 EVT VT = V.getValueType();
3343 APInt DemandedElts = VT.isFixedLengthVector()
3345 : APInt(1, 1);
3346 return getValidShiftAmount(V, DemandedElts, Depth);
3347}
3348
3349std::optional<uint64_t>
3351 unsigned Depth) const {
3352 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3353 V.getOpcode() == ISD::SRA) &&
3354 "Unknown shift node");
3355 if (std::optional<ConstantRange> AmtRange =
3356 getValidShiftAmountRange(V, DemandedElts, Depth))
3357 return AmtRange->getUnsignedMin().getZExtValue();
3358 return std::nullopt;
3359}
3360
3361std::optional<uint64_t>
3363 EVT VT = V.getValueType();
3364 APInt DemandedElts = VT.isFixedLengthVector()
3366 : APInt(1, 1);
3367 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3368}
3369
3370std::optional<uint64_t>
3372 unsigned Depth) const {
3373 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3374 V.getOpcode() == ISD::SRA) &&
3375 "Unknown shift node");
3376 if (std::optional<ConstantRange> AmtRange =
3377 getValidShiftAmountRange(V, DemandedElts, Depth))
3378 return AmtRange->getUnsignedMax().getZExtValue();
3379 return std::nullopt;
3380}
3381
3382std::optional<uint64_t>
3384 EVT VT = V.getValueType();
3385 APInt DemandedElts = VT.isFixedLengthVector()
3387 : APInt(1, 1);
3388 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3389}
3390
3391/// Determine which bits of Op are known to be either zero or one and return
3392/// them in Known. For vectors, the known bits are those that are shared by
3393/// every vector element.
3395 EVT VT = Op.getValueType();
3396
3397 // Since the number of lanes in a scalable vector is unknown at compile time,
3398 // we track one bit which is implicitly broadcast to all lanes. This means
3399 // that all lanes in a scalable vector are considered demanded.
3400 APInt DemandedElts = VT.isFixedLengthVector()
3402 : APInt(1, 1);
3403 return computeKnownBits(Op, DemandedElts, Depth);
3404}
3405
3406/// Determine which bits of Op are known to be either zero or one and return
3407/// them in Known. The DemandedElts argument allows us to only collect the known
3408/// bits that are shared by the requested vector elements.
3410 unsigned Depth) const {
3411 unsigned BitWidth = Op.getScalarValueSizeInBits();
3412
3413 KnownBits Known(BitWidth); // Don't know anything.
3414
3415 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
3416 // We know all of the bits for a constant!
3417 return KnownBits::makeConstant(C->getAPIntValue());
3418 }
3419 if (auto *C = dyn_cast<ConstantFPSDNode>(Op)) {
3420 // We know all of the bits for a constant fp!
3421 return KnownBits::makeConstant(C->getValueAPF().bitcastToAPInt());
3422 }
3423
3424 if (Depth >= MaxRecursionDepth)
3425 return Known; // Limit search depth.
3426
3427 KnownBits Known2;
3428 unsigned NumElts = DemandedElts.getBitWidth();
3429 assert((!Op.getValueType().isFixedLengthVector() ||
3430 NumElts == Op.getValueType().getVectorNumElements()) &&
3431 "Unexpected vector size");
3432
3433 if (!DemandedElts)
3434 return Known; // No demanded elts, better to assume we don't know anything.
3435
3436 unsigned Opcode = Op.getOpcode();
3437 switch (Opcode) {
3438 case ISD::MERGE_VALUES:
3439 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3440 Depth + 1);
3441 case ISD::SPLAT_VECTOR: {
3442 SDValue SrcOp = Op.getOperand(0);
3443 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3444 "Expected SPLAT_VECTOR implicit truncation");
3445 // Implicitly truncate the bits to match the official semantics of
3446 // SPLAT_VECTOR.
3447 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3448 break;
3449 }
3451 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3452 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3453 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3454 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3455 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3456 }
3457 break;
3458 }
3459 case ISD::STEP_VECTOR: {
3460 const APInt &Step = Op.getConstantOperandAPInt(0);
3461
3462 if (Step.isPowerOf2())
3463 Known.Zero.setLowBits(Step.logBase2());
3464
3466
3467 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3468 break;
3469 const APInt MinNumElts =
3470 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3471
3472 bool Overflow;
3473 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3475 .umul_ov(MinNumElts, Overflow);
3476 if (Overflow)
3477 break;
3478
3479 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3480 if (Overflow)
3481 break;
3482
3483 Known.Zero.setHighBits(MaxValue.countl_zero());
3484 break;
3485 }
3486 case ISD::BUILD_VECTOR:
3487 assert(!Op.getValueType().isScalableVector());
3488 // Collect the known bits that are shared by every demanded vector element.
3489 Known.Zero.setAllBits(); Known.One.setAllBits();
3490 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3491 if (!DemandedElts[i])
3492 continue;
3493
3494 SDValue SrcOp = Op.getOperand(i);
3495 Known2 = computeKnownBits(SrcOp, Depth + 1);
3496
3497 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3498 if (SrcOp.getValueSizeInBits() != BitWidth) {
3499 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3500 "Expected BUILD_VECTOR implicit truncation");
3501 Known2 = Known2.trunc(BitWidth);
3502 }
3503
3504 // Known bits are the values that are shared by every demanded element.
3505 Known = Known.intersectWith(Known2);
3506
3507 // If we don't know any bits, early out.
3508 if (Known.isUnknown())
3509 break;
3510 }
3511 break;
3512 case ISD::VECTOR_SHUFFLE: {
3513 assert(!Op.getValueType().isScalableVector());
3514 // Collect the known bits that are shared by every vector element referenced
3515 // by the shuffle.
3516 APInt DemandedLHS, DemandedRHS;
3517 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
3518 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3519 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3520 DemandedLHS, DemandedRHS))
3521 break;
3522
3523 // Known bits are the values that are shared by every demanded element.
3524 Known.Zero.setAllBits(); Known.One.setAllBits();
3525 if (!!DemandedLHS) {
3526 SDValue LHS = Op.getOperand(0);
3527 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3528 Known = Known.intersectWith(Known2);
3529 }
3530 // If we don't know any bits, early out.
3531 if (Known.isUnknown())
3532 break;
3533 if (!!DemandedRHS) {
3534 SDValue RHS = Op.getOperand(1);
3535 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3536 Known = Known.intersectWith(Known2);
3537 }
3538 break;
3539 }
3540 case ISD::VSCALE: {
3542 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3543 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3544 break;
3545 }
3546 case ISD::CONCAT_VECTORS: {
3547 if (Op.getValueType().isScalableVector())
3548 break;
3549 // Split DemandedElts and test each of the demanded subvectors.
3550 Known.Zero.setAllBits(); Known.One.setAllBits();
3551 EVT SubVectorVT = Op.getOperand(0).getValueType();
3552 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3553 unsigned NumSubVectors = Op.getNumOperands();
3554 for (unsigned i = 0; i != NumSubVectors; ++i) {
3555 APInt DemandedSub =
3556 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3557 if (!!DemandedSub) {
3558 SDValue Sub = Op.getOperand(i);
3559 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3560 Known = Known.intersectWith(Known2);
3561 }
3562 // If we don't know any bits, early out.
3563 if (Known.isUnknown())
3564 break;
3565 }
3566 break;
3567 }
3568 case ISD::INSERT_SUBVECTOR: {
3569 if (Op.getValueType().isScalableVector())
3570 break;
3571 // Demand any elements from the subvector and the remainder from the src its
3572 // inserted into.
3573 SDValue Src = Op.getOperand(0);
3574 SDValue Sub = Op.getOperand(1);
3575 uint64_t Idx = Op.getConstantOperandVal(2);
3576 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3577 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3578 APInt DemandedSrcElts = DemandedElts;
3579 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
3580
3581 Known.One.setAllBits();
3582 Known.Zero.setAllBits();
3583 if (!!DemandedSubElts) {
3584 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3585 if (Known.isUnknown())
3586 break; // early-out.
3587 }
3588 if (!!DemandedSrcElts) {
3589 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3590 Known = Known.intersectWith(Known2);
3591 }
3592 break;
3593 }
3595 // Offset the demanded elts by the subvector index.
3596 SDValue Src = Op.getOperand(0);
3597 // Bail until we can represent demanded elements for scalable vectors.
3598 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3599 break;
3600 uint64_t Idx = Op.getConstantOperandVal(1);
3601 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3602 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3603 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3604 break;
3605 }
3606 case ISD::SCALAR_TO_VECTOR: {
3607 if (Op.getValueType().isScalableVector())
3608 break;
3609 // We know about scalar_to_vector as much as we know about it source,
3610 // which becomes the first element of otherwise unknown vector.
3611 if (DemandedElts != 1)
3612 break;
3613
3614 SDValue N0 = Op.getOperand(0);
3615 Known = computeKnownBits(N0, Depth + 1);
3616 if (N0.getValueSizeInBits() != BitWidth)
3617 Known = Known.trunc(BitWidth);
3618
3619 break;
3620 }
3621 case ISD::BITCAST: {
3622 if (Op.getValueType().isScalableVector())
3623 break;
3624
3625 SDValue N0 = Op.getOperand(0);
3626 EVT SubVT = N0.getValueType();
3627 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3628
3629 // Ignore bitcasts from unsupported types.
3630 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3631 break;
3632
3633 // Fast handling of 'identity' bitcasts.
3634 if (BitWidth == SubBitWidth) {
3635 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3636 break;
3637 }
3638
3639 bool IsLE = getDataLayout().isLittleEndian();
3640
3641 // Bitcast 'small element' vector to 'large element' scalar/vector.
3642 if ((BitWidth % SubBitWidth) == 0) {
3643 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3644
3645 // Collect known bits for the (larger) output by collecting the known
3646 // bits from each set of sub elements and shift these into place.
3647 // We need to separately call computeKnownBits for each set of
3648 // sub elements as the knownbits for each is likely to be different.
3649 unsigned SubScale = BitWidth / SubBitWidth;
3650 APInt SubDemandedElts(NumElts * SubScale, 0);
3651 for (unsigned i = 0; i != NumElts; ++i)
3652 if (DemandedElts[i])
3653 SubDemandedElts.setBit(i * SubScale);
3654
3655 for (unsigned i = 0; i != SubScale; ++i) {
3656 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3657 Depth + 1);
3658 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3659 Known.insertBits(Known2, SubBitWidth * Shifts);
3660 }
3661 }
3662
3663 // Bitcast 'large element' scalar/vector to 'small element' vector.
3664 if ((SubBitWidth % BitWidth) == 0) {
3665 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3666
3667 // Collect known bits for the (smaller) output by collecting the known
3668 // bits from the overlapping larger input elements and extracting the
3669 // sub sections we actually care about.
3670 unsigned SubScale = SubBitWidth / BitWidth;
3671 APInt SubDemandedElts =
3672 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3673 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3674
3675 Known.Zero.setAllBits(); Known.One.setAllBits();
3676 for (unsigned i = 0; i != NumElts; ++i)
3677 if (DemandedElts[i]) {
3678 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3679 unsigned Offset = (Shifts % SubScale) * BitWidth;
3680 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3681 // If we don't know any bits, early out.
3682 if (Known.isUnknown())
3683 break;
3684 }
3685 }
3686 break;
3687 }
3688 case ISD::AND:
3689 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3690 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3691
3692 Known &= Known2;
3693 break;
3694 case ISD::OR:
3695 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3696 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3697
3698 Known |= Known2;
3699 break;
3700 case ISD::XOR:
3701 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3702 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3703
3704 Known ^= Known2;
3705 break;
3706 case ISD::MUL: {
3707 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3708 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3709 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3710 // TODO: SelfMultiply can be poison, but not undef.
3711 if (SelfMultiply)
3712 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3713 Op.getOperand(0), DemandedElts, false, Depth + 1);
3714 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3715
3716 // If the multiplication is known not to overflow, the product of a number
3717 // with itself is non-negative. Only do this if we didn't already computed
3718 // the opposite value for the sign bit.
3719 if (Op->getFlags().hasNoSignedWrap() &&
3720 Op.getOperand(0) == Op.getOperand(1) &&
3721 !Known.isNegative())
3722 Known.makeNonNegative();
3723 break;
3724 }
3725 case ISD::MULHU: {
3726 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3727 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3728 Known = KnownBits::mulhu(Known, Known2);
3729 break;
3730 }
3731 case ISD::MULHS: {
3732 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3733 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3734 Known = KnownBits::mulhs(Known, Known2);
3735 break;
3736 }
3737 case ISD::ABDU: {
3738 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3739 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3740 Known = KnownBits::abdu(Known, Known2);
3741 break;
3742 }
3743 case ISD::ABDS: {
3744 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3745 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3746 Known = KnownBits::abds(Known, Known2);
3747 unsigned SignBits1 =
3748 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3749 if (SignBits1 == 1)
3750 break;
3751 unsigned SignBits0 =
3752 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3753 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3754 break;
3755 }
3756 case ISD::UMUL_LOHI: {
3757 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3758 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3759 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3760 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3761 if (Op.getResNo() == 0)
3762 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3763 else
3764 Known = KnownBits::mulhu(Known, Known2);
3765 break;
3766 }
3767 case ISD::SMUL_LOHI: {
3768 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3769 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3770 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3771 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3772 if (Op.getResNo() == 0)
3773 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3774 else
3775 Known = KnownBits::mulhs(Known, Known2);
3776 break;
3777 }
3778 case ISD::AVGFLOORU: {
3779 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3780 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3781 Known = KnownBits::avgFloorU(Known, Known2);
3782 break;
3783 }
3784 case ISD::AVGCEILU: {
3785 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3786 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3787 Known = KnownBits::avgCeilU(Known, Known2);
3788 break;
3789 }
3790 case ISD::AVGFLOORS: {
3791 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3792 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3793 Known = KnownBits::avgFloorS(Known, Known2);
3794 break;
3795 }
3796 case ISD::AVGCEILS: {
3797 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3798 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3799 Known = KnownBits::avgCeilS(Known, Known2);
3800 break;
3801 }
3802 case ISD::SELECT:
3803 case ISD::VSELECT:
3804 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3805 // If we don't know any bits, early out.
3806 if (Known.isUnknown())
3807 break;
3808 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3809
3810 // Only known if known in both the LHS and RHS.
3811 Known = Known.intersectWith(Known2);
3812 break;
3813 case ISD::SELECT_CC:
3814 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3815 // If we don't know any bits, early out.
3816 if (Known.isUnknown())
3817 break;
3818 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3819
3820 // Only known if known in both the LHS and RHS.
3821 Known = Known.intersectWith(Known2);
3822 break;
3823 case ISD::SMULO:
3824 case ISD::UMULO:
3825 if (Op.getResNo() != 1)
3826 break;
3827 // The boolean result conforms to getBooleanContents.
3828 // If we know the result of a setcc has the top bits zero, use this info.
3829 // We know that we have an integer-based boolean since these operations
3830 // are only available for integer.
3831 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3833 BitWidth > 1)
3834 Known.Zero.setBitsFrom(1);
3835 break;
3836 case ISD::SETCC:
3837 case ISD::SETCCCARRY:
3838 case ISD::STRICT_FSETCC:
3839 case ISD::STRICT_FSETCCS: {
3840 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3841 // If we know the result of a setcc has the top bits zero, use this info.
3842 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3844 BitWidth > 1)
3845 Known.Zero.setBitsFrom(1);
3846 break;
3847 }
3848 case ISD::SHL: {
3849 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3850 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3851
3852 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3853 bool NSW = Op->getFlags().hasNoSignedWrap();
3854
3855 bool ShAmtNonZero = Known2.isNonZero();
3856
3857 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3858
3859 // Minimum shift low bits are known zero.
3860 if (std::optional<uint64_t> ShMinAmt =
3861 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3862 Known.Zero.setLowBits(*ShMinAmt);
3863 break;
3864 }
3865 case ISD::SRL:
3866 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3867 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3868 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3869 Op->getFlags().hasExact());
3870
3871 // Minimum shift high bits are known zero.
3872 if (std::optional<uint64_t> ShMinAmt =
3873 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3874 Known.Zero.setHighBits(*ShMinAmt);
3875 break;
3876 case ISD::SRA:
3877 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3878 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3879 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3880 Op->getFlags().hasExact());
3881 break;
3882 case ISD::FSHL:
3883 case ISD::FSHR:
3884 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3885 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3886
3887 // For fshl, 0-shift returns the 1st arg.
3888 // For fshr, 0-shift returns the 2nd arg.
3889 if (Amt == 0) {
3890 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3891 DemandedElts, Depth + 1);
3892 break;
3893 }
3894
3895 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3896 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3897 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3898 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3899 if (Opcode == ISD::FSHL) {
3900 Known.One <<= Amt;
3901 Known.Zero <<= Amt;
3902 Known2.One.lshrInPlace(BitWidth - Amt);
3903 Known2.Zero.lshrInPlace(BitWidth - Amt);
3904 } else {
3905 Known.One <<= BitWidth - Amt;
3906 Known.Zero <<= BitWidth - Amt;
3907 Known2.One.lshrInPlace(Amt);
3908 Known2.Zero.lshrInPlace(Amt);
3909 }
3910 Known = Known.unionWith(Known2);
3911 }
3912 break;
3913 case ISD::SHL_PARTS:
3914 case ISD::SRA_PARTS:
3915 case ISD::SRL_PARTS: {
3916 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3917
3918 // Collect lo/hi source values and concatenate.
3919 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3920 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3921 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3922 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3923 Known = Known2.concat(Known);
3924
3925 // Collect shift amount.
3926 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3927
3928 if (Opcode == ISD::SHL_PARTS)
3929 Known = KnownBits::shl(Known, Known2);
3930 else if (Opcode == ISD::SRA_PARTS)
3931 Known = KnownBits::ashr(Known, Known2);
3932 else // if (Opcode == ISD::SRL_PARTS)
3933 Known = KnownBits::lshr(Known, Known2);
3934
3935 // TODO: Minimum shift low/high bits are known zero.
3936
3937 if (Op.getResNo() == 0)
3938 Known = Known.extractBits(LoBits, 0);
3939 else
3940 Known = Known.extractBits(HiBits, LoBits);
3941 break;
3942 }
3944 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3945 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3946 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3947 break;
3948 }
3949 case ISD::CTTZ:
3950 case ISD::CTTZ_ZERO_UNDEF: {
3951 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3952 // If we have a known 1, its position is our upper bound.
3953 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3954 unsigned LowBits = llvm::bit_width(PossibleTZ);
3955 Known.Zero.setBitsFrom(LowBits);
3956 break;
3957 }
3958 case ISD::CTLZ:
3959 case ISD::CTLZ_ZERO_UNDEF: {
3960 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3961 // If we have a known 1, its position is our upper bound.
3962 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3963 unsigned LowBits = llvm::bit_width(PossibleLZ);
3964 Known.Zero.setBitsFrom(LowBits);
3965 break;
3966 }
3967 case ISD::CTPOP: {
3968 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3969 // If we know some of the bits are zero, they can't be one.
3970 unsigned PossibleOnes = Known2.countMaxPopulation();
3971 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3972 break;
3973 }
3974 case ISD::PARITY: {
3975 // Parity returns 0 everywhere but the LSB.
3976 Known.Zero.setBitsFrom(1);
3977 break;
3978 }
3979 case ISD::MGATHER:
3980 case ISD::MLOAD: {
3981 ISD::LoadExtType ETy =
3982 (Opcode == ISD::MGATHER)
3983 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3984 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3985 if (ETy == ISD::ZEXTLOAD) {
3986 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3987 KnownBits Known0(MemVT.getScalarSizeInBits());
3988 return Known0.zext(BitWidth);
3989 }
3990 break;
3991 }
3992 case ISD::LOAD: {
3993 LoadSDNode *LD = cast<LoadSDNode>(Op);
3994 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3995 if (ISD::isNON_EXTLoad(LD) && Cst) {
3996 // Determine any common known bits from the loaded constant pool value.
3997 Type *CstTy = Cst->getType();
3998 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3999 !Op.getValueType().isScalableVector()) {
4000 // If its a vector splat, then we can (quickly) reuse the scalar path.
4001 // NOTE: We assume all elements match and none are UNDEF.
4002 if (CstTy->isVectorTy()) {
4003 if (const Constant *Splat = Cst->getSplatValue()) {
4004 Cst = Splat;
4005 CstTy = Cst->getType();
4006 }
4007 }
4008 // TODO - do we need to handle different bitwidths?
4009 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
4010 // Iterate across all vector elements finding common known bits.
4011 Known.One.setAllBits();
4012 Known.Zero.setAllBits();
4013 for (unsigned i = 0; i != NumElts; ++i) {
4014 if (!DemandedElts[i])
4015 continue;
4016 if (Constant *Elt = Cst->getAggregateElement(i)) {
4017 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
4018 const APInt &Value = CInt->getValue();
4019 Known.One &= Value;
4020 Known.Zero &= ~Value;
4021 continue;
4022 }
4023 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
4024 APInt Value = CFP->getValueAPF().bitcastToAPInt();
4025 Known.One &= Value;
4026 Known.Zero &= ~Value;
4027 continue;
4028 }
4029 }
4030 Known.One.clearAllBits();
4031 Known.Zero.clearAllBits();
4032 break;
4033 }
4034 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4035 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4036 Known = KnownBits::makeConstant(CInt->getValue());
4037 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4038 Known =
4039 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4040 }
4041 }
4042 }
4043 } else if (Op.getResNo() == 0) {
4044 KnownBits Known0(!LD->getMemoryVT().isScalableVT()
4045 ? LD->getMemoryVT().getFixedSizeInBits()
4046 : BitWidth);
4047 EVT VT = Op.getValueType();
4048 // Fill in any known bits from range information. There are 3 types being
4049 // used. The results VT (same vector elt size as BitWidth), the loaded
4050 // MemoryVT (which may or may not be vector) and the range VTs original
4051 // type. The range matadata needs the full range (i.e
4052 // MemoryVT().getSizeInBits()), which is truncated to the correct elt size
4053 // if it is know. These are then extended to the original VT sizes below.
4054 if (const MDNode *MD = LD->getRanges()) {
4056 if (VT.isVector()) {
4057 // Handle truncation to the first demanded element.
4058 // TODO: Figure out which demanded elements are covered
4059 if (DemandedElts != 1 || !getDataLayout().isLittleEndian())
4060 break;
4061 Known0 = Known0.trunc(BitWidth);
4062 }
4063 }
4064
4065 if (LD->getMemoryVT().isVector())
4066 Known0 = Known0.trunc(LD->getMemoryVT().getScalarSizeInBits());
4067
4068 // Extend the Known bits from memory to the size of the result.
4069 if (ISD::isZEXTLoad(Op.getNode()))
4070 Known = Known0.zext(BitWidth);
4071 else if (ISD::isSEXTLoad(Op.getNode()))
4072 Known = Known0.sext(BitWidth);
4073 else if (ISD::isEXTLoad(Op.getNode()))
4074 Known = Known0.anyext(BitWidth);
4075 else
4076 Known = Known0;
4077 assert(Known.getBitWidth() == BitWidth);
4078 return Known;
4079 }
4080 break;
4081 }
4083 if (Op.getValueType().isScalableVector())
4084 break;
4085 EVT InVT = Op.getOperand(0).getValueType();
4086 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4087 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4088 Known = Known.zext(BitWidth);
4089 break;
4090 }
4091 case ISD::ZERO_EXTEND: {
4092 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4093 Known = Known.zext(BitWidth);
4094 break;
4095 }
4097 if (Op.getValueType().isScalableVector())
4098 break;
4099 EVT InVT = Op.getOperand(0).getValueType();
4100 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4101 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4102 // If the sign bit is known to be zero or one, then sext will extend
4103 // it to the top bits, else it will just zext.
4104 Known = Known.sext(BitWidth);
4105 break;
4106 }
4107 case ISD::SIGN_EXTEND: {
4108 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4109 // If the sign bit is known to be zero or one, then sext will extend
4110 // it to the top bits, else it will just zext.
4111 Known = Known.sext(BitWidth);
4112 break;
4113 }
4115 if (Op.getValueType().isScalableVector())
4116 break;
4117 EVT InVT = Op.getOperand(0).getValueType();
4118 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4119 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4120 Known = Known.anyext(BitWidth);
4121 break;
4122 }
4123 case ISD::ANY_EXTEND: {
4124 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4125 Known = Known.anyext(BitWidth);
4126 break;
4127 }
4128 case ISD::TRUNCATE: {
4129 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4130 Known = Known.trunc(BitWidth);
4131 break;
4132 }
4133 case ISD::AssertZext: {
4134 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4136 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4137 Known.Zero |= (~InMask);
4138 Known.One &= (~Known.Zero);
4139 break;
4140 }
4141 case ISD::AssertAlign: {
4142 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4143 assert(LogOfAlign != 0);
4144
4145 // TODO: Should use maximum with source
4146 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4147 // well as clearing one bits.
4148 Known.Zero.setLowBits(LogOfAlign);
4149 Known.One.clearLowBits(LogOfAlign);
4150 break;
4151 }
4152 case ISD::FGETSIGN:
4153 // All bits are zero except the low bit.
4154 Known.Zero.setBitsFrom(1);
4155 break;
4156 case ISD::ADD:
4157 case ISD::SUB: {
4158 SDNodeFlags Flags = Op.getNode()->getFlags();
4159 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4160 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4162 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4163 Flags.hasNoUnsignedWrap(), Known, Known2);
4164 break;
4165 }
4166 case ISD::USUBO:
4167 case ISD::SSUBO:
4168 case ISD::USUBO_CARRY:
4169 case ISD::SSUBO_CARRY:
4170 if (Op.getResNo() == 1) {
4171 // If we know the result of a setcc has the top bits zero, use this info.
4172 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4174 BitWidth > 1)
4175 Known.Zero.setBitsFrom(1);
4176 break;
4177 }
4178 [[fallthrough]];
4179 case ISD::SUBC: {
4180 assert(Op.getResNo() == 0 &&
4181 "We only compute knownbits for the difference here.");
4182
4183 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4184 KnownBits Borrow(1);
4185 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4186 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4187 // Borrow has bit width 1
4188 Borrow = Borrow.trunc(1);
4189 } else {
4190 Borrow.setAllZero();
4191 }
4192
4193 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4194 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4195 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4196 break;
4197 }
4198 case ISD::UADDO:
4199 case ISD::SADDO:
4200 case ISD::UADDO_CARRY:
4201 case ISD::SADDO_CARRY:
4202 if (Op.getResNo() == 1) {
4203 // If we know the result of a setcc has the top bits zero, use this info.
4204 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4206 BitWidth > 1)
4207 Known.Zero.setBitsFrom(1);
4208 break;
4209 }
4210 [[fallthrough]];
4211 case ISD::ADDC:
4212 case ISD::ADDE: {
4213 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4214
4215 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4216 KnownBits Carry(1);
4217 if (Opcode == ISD::ADDE)
4218 // Can't track carry from glue, set carry to unknown.
4219 Carry.resetAll();
4220 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4221 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4222 // Carry has bit width 1
4223 Carry = Carry.trunc(1);
4224 } else {
4225 Carry.setAllZero();
4226 }
4227
4228 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4229 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4230 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4231 break;
4232 }
4233 case ISD::UDIV: {
4234 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4235 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4236 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4237 break;
4238 }
4239 case ISD::SDIV: {
4240 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4241 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4242 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4243 break;
4244 }
4245 case ISD::SREM: {
4246 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4247 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4248 Known = KnownBits::srem(Known, Known2);
4249 break;
4250 }
4251 case ISD::UREM: {
4252 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4253 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4254 Known = KnownBits::urem(Known, Known2);
4255 break;
4256 }
4257 case ISD::EXTRACT_ELEMENT: {
4258 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4259 const unsigned Index = Op.getConstantOperandVal(1);
4260 const unsigned EltBitWidth = Op.getValueSizeInBits();
4261
4262 // Remove low part of known bits mask
4263 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4264 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4265
4266 // Remove high part of known bit mask
4267 Known = Known.trunc(EltBitWidth);
4268 break;
4269 }
4271 SDValue InVec = Op.getOperand(0);
4272 SDValue EltNo = Op.getOperand(1);
4273 EVT VecVT = InVec.getValueType();
4274 // computeKnownBits not yet implemented for scalable vectors.
4275 if (VecVT.isScalableVector())
4276 break;
4277 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4278 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4279
4280 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4281 // anything about the extended bits.
4282 if (BitWidth > EltBitWidth)
4283 Known = Known.trunc(EltBitWidth);
4284
4285 // If we know the element index, just demand that vector element, else for
4286 // an unknown element index, ignore DemandedElts and demand them all.
4287 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4288 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4289 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4290 DemandedSrcElts =
4291 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4292
4293 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4294 if (BitWidth > EltBitWidth)
4295 Known = Known.anyext(BitWidth);
4296 break;
4297 }
4299 if (Op.getValueType().isScalableVector())
4300 break;
4301
4302 // If we know the element index, split the demand between the
4303 // source vector and the inserted element, otherwise assume we need
4304 // the original demanded vector elements and the value.
4305 SDValue InVec = Op.getOperand(0);
4306 SDValue InVal = Op.getOperand(1);
4307 SDValue EltNo = Op.getOperand(2);
4308 bool DemandedVal = true;
4309 APInt DemandedVecElts = DemandedElts;
4310 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4311 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4312 unsigned EltIdx = CEltNo->getZExtValue();
4313 DemandedVal = !!DemandedElts[EltIdx];
4314 DemandedVecElts.clearBit(EltIdx);
4315 }
4316 Known.One.setAllBits();
4317 Known.Zero.setAllBits();
4318 if (DemandedVal) {
4319 Known2 = computeKnownBits(InVal, Depth + 1);
4320 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4321 }
4322 if (!!DemandedVecElts) {
4323 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4324 Known = Known.intersectWith(Known2);
4325 }
4326 break;
4327 }
4328 case ISD::BITREVERSE: {
4329 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4330 Known = Known2.reverseBits();
4331 break;
4332 }
4333 case ISD::BSWAP: {
4334 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4335 Known = Known2.byteSwap();
4336 break;
4337 }
4338 case ISD::ABS: {
4339 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4340 Known = Known2.abs();
4341 Known.Zero.setHighBits(
4342 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4343 break;
4344 }
4345 case ISD::USUBSAT: {
4346 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4347 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4348 Known = KnownBits::usub_sat(Known, Known2);
4349 break;
4350 }
4351 case ISD::UMIN: {
4352 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4353 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4354 Known = KnownBits::umin(Known, Known2);
4355 break;
4356 }
4357 case ISD::UMAX: {
4358 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4359 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4360 Known = KnownBits::umax(Known, Known2);
4361 break;
4362 }
4363 case ISD::SMIN:
4364 case ISD::SMAX: {
4365 // If we have a clamp pattern, we know that the number of sign bits will be
4366 // the minimum of the clamp min/max range.
4367 bool IsMax = (Opcode == ISD::SMAX);
4368 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4369 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4370 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4371 CstHigh =
4372 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4373 if (CstLow && CstHigh) {
4374 if (!IsMax)
4375 std::swap(CstLow, CstHigh);
4376
4377 const APInt &ValueLow = CstLow->getAPIntValue();
4378 const APInt &ValueHigh = CstHigh->getAPIntValue();
4379 if (ValueLow.sle(ValueHigh)) {
4380 unsigned LowSignBits = ValueLow.getNumSignBits();
4381 unsigned HighSignBits = ValueHigh.getNumSignBits();
4382 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4383 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4384 Known.One.setHighBits(MinSignBits);
4385 break;
4386 }
4387 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4388 Known.Zero.setHighBits(MinSignBits);
4389 break;
4390 }
4391 }
4392 }
4393
4394 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4395 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4396 if (IsMax)
4397 Known = KnownBits::smax(Known, Known2);
4398 else
4399 Known = KnownBits::smin(Known, Known2);
4400
4401 // For SMAX, if CstLow is non-negative we know the result will be
4402 // non-negative and thus all sign bits are 0.
4403 // TODO: There's an equivalent of this for smin with negative constant for
4404 // known ones.
4405 if (IsMax && CstLow) {
4406 const APInt &ValueLow = CstLow->getAPIntValue();
4407 if (ValueLow.isNonNegative()) {
4408 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4409 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4410 }
4411 }
4412
4413 break;
4414 }
4415 case ISD::UINT_TO_FP: {
4416 Known.makeNonNegative();
4417 break;
4418 }
4419 case ISD::SINT_TO_FP: {
4420 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4421 if (Known2.isNonNegative())
4422 Known.makeNonNegative();
4423 else if (Known2.isNegative())
4424 Known.makeNegative();
4425 break;
4426 }
4427 case ISD::FP_TO_UINT_SAT: {
4428 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4429 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4431 break;
4432 }
4434 if (Op.getResNo() == 1) {
4435 // The boolean result conforms to getBooleanContents.
4436 // If we know the result of a setcc has the top bits zero, use this info.
4437 // We know that we have an integer-based boolean since these operations
4438 // are only available for integer.
4439 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4441 BitWidth > 1)
4442 Known.Zero.setBitsFrom(1);
4443 break;
4444 }
4445 [[fallthrough]];
4447 case ISD::ATOMIC_SWAP:
4459 case ISD::ATOMIC_LOAD: {
4460 unsigned MemBits =
4461 cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
4462 // If we are looking at the loaded value.
4463 if (Op.getResNo() == 0) {
4465 Known.Zero.setBitsFrom(MemBits);
4466 else if (Op->getOpcode() == ISD::ATOMIC_LOAD &&
4467 cast<AtomicSDNode>(Op)->getExtensionType() == ISD::ZEXTLOAD)
4468 Known.Zero.setBitsFrom(MemBits);
4469 }
4470 break;
4471 }
4472 case ISD::FrameIndex:
4474 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4475 Known, getMachineFunction());
4476 break;
4477
4478 default:
4479 if (Opcode < ISD::BUILTIN_OP_END)
4480 break;
4481 [[fallthrough]];
4485 // TODO: Probably okay to remove after audit; here to reduce change size
4486 // in initial enablement patch for scalable vectors
4487 if (Op.getValueType().isScalableVector())
4488 break;
4489
4490 // Allow the target to implement this method for its nodes.
4491 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4492 break;
4493 }
4494
4495 return Known;
4496}
4497
4498/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4500 switch (OR) {
4508 }
4509 llvm_unreachable("Unknown OverflowResult");
4510}
4511
4514 // X + 0 never overflow
4515 if (isNullConstant(N1))
4516 return OFK_Never;
4517
4518 // If both operands each have at least two sign bits, the addition
4519 // cannot overflow.
4520 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4521 return OFK_Never;
4522
4523 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4524 return OFK_Sometime;
4525}
4526
4529 // X + 0 never overflow
4530 if (isNullConstant(N1))
4531 return OFK_Never;
4532
4533 // mulhi + 1 never overflow
4534 KnownBits N1Known = computeKnownBits(N1);
4535 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4536 N1Known.getMaxValue().ult(2))
4537 return OFK_Never;
4538
4539 KnownBits N0Known = computeKnownBits(N0);
4540 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4541 N0Known.getMaxValue().ult(2))
4542 return OFK_Never;
4543
4544 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4545 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4546 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4547 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4548}
4549
4552 // X - 0 never overflow
4553 if (isNullConstant(N1))
4554 return OFK_Never;
4555
4556 // If both operands each have at least two sign bits, the subtraction
4557 // cannot overflow.
4558 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4559 return OFK_Never;
4560
4561 KnownBits N0Known = computeKnownBits(N0);
4562 KnownBits N1Known = computeKnownBits(N1);
4563 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4564 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4565 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4566}
4567
4570 // X - 0 never overflow
4571 if (isNullConstant(N1))
4572 return OFK_Never;
4573
4574 KnownBits N0Known = computeKnownBits(N0);
4575 KnownBits N1Known = computeKnownBits(N1);
4576 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4577 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4578 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4579}
4580
4583 // X * 0 and X * 1 never overflow.
4584 if (isNullConstant(N1) || isOneConstant(N1))
4585 return OFK_Never;
4586
4587 KnownBits N0Known = computeKnownBits(N0);
4588 KnownBits N1Known = computeKnownBits(N1);
4589 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4590 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4591 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4592}
4593
4596 // X * 0 and X * 1 never overflow.
4597 if (isNullConstant(N1) || isOneConstant(N1))
4598 return OFK_Never;
4599
4600 // Get the size of the result.
4601 unsigned BitWidth = N0.getScalarValueSizeInBits();
4602
4603 // Sum of the sign bits.
4604 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4605
4606 // If we have enough sign bits, then there's no overflow.
4607 if (SignBits > BitWidth + 1)
4608 return OFK_Never;
4609
4610 if (SignBits == BitWidth + 1) {
4611 // The overflow occurs when the true multiplication of the
4612 // the operands is the minimum negative number.
4613 KnownBits N0Known = computeKnownBits(N0);
4614 KnownBits N1Known = computeKnownBits(N1);
4615 // If one of the operands is non-negative, then there's no
4616 // overflow.
4617 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4618 return OFK_Never;
4619 }
4620
4621 return OFK_Sometime;
4622}
4623
4625 if (Depth >= MaxRecursionDepth)
4626 return false; // Limit search depth.
4627
4628 EVT OpVT = Val.getValueType();
4629 unsigned BitWidth = OpVT.getScalarSizeInBits();
4630
4631 // Is the constant a known power of 2?
4633 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4634 }))
4635 return true;
4636
4637 // A left-shift of a constant one will have exactly one bit set because
4638 // shifting the bit off the end is undefined.
4639 if (Val.getOpcode() == ISD::SHL) {
4640 auto *C = isConstOrConstSplat(Val.getOperand(0));
4641 if (C && C->getAPIntValue() == 1)
4642 return true;
4643 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4644 isKnownNeverZero(Val, Depth);
4645 }
4646
4647 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4648 // one bit set.
4649 if (Val.getOpcode() == ISD::SRL) {
4650 auto *C = isConstOrConstSplat(Val.getOperand(0));
4651 if (C && C->getAPIntValue().isSignMask())
4652 return true;
4653 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4654 isKnownNeverZero(Val, Depth);
4655 }
4656
4657 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4658 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4659
4660 // Are all operands of a build vector constant powers of two?
4661 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4662 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4663 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4664 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4665 return false;
4666 }))
4667 return true;
4668
4669 // Is the operand of a splat vector a constant power of two?
4670 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4671 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4672 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4673 return true;
4674
4675 // vscale(power-of-two) is a power-of-two for some targets
4676 if (Val.getOpcode() == ISD::VSCALE &&
4677 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4679 return true;
4680
4681 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4682 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4683 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4685
4686 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4687 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4689
4690 // Looking for `x & -x` pattern:
4691 // If x == 0:
4692 // x & -x -> 0
4693 // If x != 0:
4694 // x & -x -> non-zero pow2
4695 // so if we find the pattern return whether we know `x` is non-zero.
4696 SDValue X;
4697 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4698 return isKnownNeverZero(X, Depth);
4699
4700 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4701 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4702
4703 // More could be done here, though the above checks are enough
4704 // to handle some common cases.
4705 return false;
4706}
4707
4709 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4710 return C1->getValueAPF().getExactLog2Abs() >= 0;
4711
4712 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4713 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4714
4715 return false;
4716}
4717
4719 EVT VT = Op.getValueType();
4720
4721 // Since the number of lanes in a scalable vector is unknown at compile time,
4722 // we track one bit which is implicitly broadcast to all lanes. This means
4723 // that all lanes in a scalable vector are considered demanded.
4724 APInt DemandedElts = VT.isFixedLengthVector()
4726 : APInt(1, 1);
4727 return ComputeNumSignBits(Op, DemandedElts, Depth);
4728}
4729
4730unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4731 unsigned Depth) const {
4732 EVT VT = Op.getValueType();
4733 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4734 unsigned VTBits = VT.getScalarSizeInBits();
4735 unsigned NumElts = DemandedElts.getBitWidth();
4736 unsigned Tmp, Tmp2;
4737 unsigned FirstAnswer = 1;
4738
4739 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4740 const APInt &Val = C->getAPIntValue();
4741 return Val.getNumSignBits();
4742 }
4743
4744 if (Depth >= MaxRecursionDepth)
4745 return 1; // Limit search depth.
4746
4747 if (!DemandedElts)
4748 return 1; // No demanded elts, better to assume we don't know anything.
4749
4750 unsigned Opcode = Op.getOpcode();
4751 switch (Opcode) {
4752 default: break;
4753 case ISD::AssertSext:
4754 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4755 return VTBits-Tmp+1;
4756 case ISD::AssertZext:
4757 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4758 return VTBits-Tmp;
4759 case ISD::MERGE_VALUES:
4760 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4761 Depth + 1);
4762 case ISD::SPLAT_VECTOR: {
4763 // Check if the sign bits of source go down as far as the truncated value.
4764 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4765 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4766 if (NumSrcSignBits > (NumSrcBits - VTBits))
4767 return NumSrcSignBits - (NumSrcBits - VTBits);
4768 break;
4769 }
4770 case ISD::BUILD_VECTOR:
4771 assert(!VT.isScalableVector());
4772 Tmp = VTBits;
4773 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4774 if (!DemandedElts[i])
4775 continue;
4776
4777 SDValue SrcOp = Op.getOperand(i);
4778 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4779 // for constant nodes to ensure we only look at the sign bits.
4780 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SrcOp)) {
4781 APInt T = C->getAPIntValue().trunc(VTBits);
4782 Tmp2 = T.getNumSignBits();
4783 } else {
4784 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4785
4786 if (SrcOp.getValueSizeInBits() != VTBits) {
4787 assert(SrcOp.getValueSizeInBits() > VTBits &&
4788 "Expected BUILD_VECTOR implicit truncation");
4789 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4790 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4791 }
4792 }
4793 Tmp = std::min(Tmp, Tmp2);
4794 }
4795 return Tmp;
4796
4797 case ISD::VECTOR_SHUFFLE: {
4798 // Collect the minimum number of sign bits that are shared by every vector
4799 // element referenced by the shuffle.
4800 APInt DemandedLHS, DemandedRHS;
4801 const ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op);
4802 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4803 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4804 DemandedLHS, DemandedRHS))
4805 return 1;
4806
4807 Tmp = std::numeric_limits<unsigned>::max();
4808 if (!!DemandedLHS)
4809 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4810 if (!!DemandedRHS) {
4811 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4812 Tmp = std::min(Tmp, Tmp2);
4813 }
4814 // If we don't know anything, early out and try computeKnownBits fall-back.
4815 if (Tmp == 1)
4816 break;
4817 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4818 return Tmp;
4819 }
4820
4821 case ISD::BITCAST: {
4822 if (VT.isScalableVector())
4823 break;
4824 SDValue N0 = Op.getOperand(0);
4825 EVT SrcVT = N0.getValueType();
4826 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4827
4828 // Ignore bitcasts from unsupported types..
4829 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4830 break;
4831
4832 // Fast handling of 'identity' bitcasts.
4833 if (VTBits == SrcBits)
4834 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4835
4836 bool IsLE = getDataLayout().isLittleEndian();
4837
4838 // Bitcast 'large element' scalar/vector to 'small element' vector.
4839 if ((SrcBits % VTBits) == 0) {
4840 assert(VT.isVector() && "Expected bitcast to vector");
4841
4842 unsigned Scale = SrcBits / VTBits;
4843 APInt SrcDemandedElts =
4844 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4845
4846 // Fast case - sign splat can be simply split across the small elements.
4847 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4848 if (Tmp == SrcBits)
4849 return VTBits;
4850
4851 // Slow case - determine how far the sign extends into each sub-element.
4852 Tmp2 = VTBits;
4853 for (unsigned i = 0; i != NumElts; ++i)
4854 if (DemandedElts[i]) {
4855 unsigned SubOffset = i % Scale;
4856 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4857 SubOffset = SubOffset * VTBits;
4858 if (Tmp <= SubOffset)
4859 return 1;
4860 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4861 }
4862 return Tmp2;
4863 }
4864 break;
4865 }
4866
4868 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4869 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4870 return VTBits - Tmp + 1;
4871 case ISD::SIGN_EXTEND:
4872 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4873 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4875 // Max of the input and what this extends.
4876 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4877 Tmp = VTBits-Tmp+1;
4878 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4879 return std::max(Tmp, Tmp2);
4881 if (VT.isScalableVector())
4882 break;
4883 SDValue Src = Op.getOperand(0);
4884 EVT SrcVT = Src.getValueType();
4885 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4886 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4887 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4888 }
4889 case ISD::SRA:
4890 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4891 // SRA X, C -> adds C sign bits.
4892 if (std::optional<uint64_t> ShAmt =
4893 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4894 Tmp = std::min<uint64_t>(Tmp + *ShAmt, VTBits);
4895 return Tmp;
4896 case ISD::SHL:
4897 if (std::optional<ConstantRange> ShAmtRange =
4898 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4899 uint64_t MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4900 uint64_t MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4901 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4902 // shifted out, then we can compute the number of sign bits for the
4903 // operand being extended. A future improvement could be to pass along the
4904 // "shifted left by" information in the recursive calls to
4905 // ComputeKnownSignBits. Allowing us to handle this more generically.
4906 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4907 SDValue Ext = Op.getOperand(0);
4908 EVT ExtVT = Ext.getValueType();
4909 SDValue Extendee = Ext.getOperand(0);
4910 EVT ExtendeeVT = Extendee.getValueType();
4911 uint64_t SizeDifference =
4912 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4913 if (SizeDifference <= MinShAmt) {
4914 Tmp = SizeDifference +
4915 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4916 if (MaxShAmt < Tmp)
4917 return Tmp - MaxShAmt;
4918 }
4919 }
4920 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4921 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4922 if (MaxShAmt < Tmp)
4923 return Tmp - MaxShAmt;
4924 }
4925 break;
4926 case ISD::AND:
4927 case ISD::OR:
4928 case ISD::XOR: // NOT is handled here.
4929 // Logical binary ops preserve the number of sign bits at the worst.
4930 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4931 if (Tmp != 1) {
4932 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4933 FirstAnswer = std::min(Tmp, Tmp2);
4934 // We computed what we know about the sign bits as our first
4935 // answer. Now proceed to the generic code that uses
4936 // computeKnownBits, and pick whichever answer is better.
4937 }
4938 break;
4939
4940 case ISD::SELECT:
4941 case ISD::VSELECT:
4942 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4943 if (Tmp == 1) return 1; // Early out.
4944 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4945 return std::min(Tmp, Tmp2);
4946 case ISD::SELECT_CC:
4947 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4948 if (Tmp == 1) return 1; // Early out.
4949 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4950 return std::min(Tmp, Tmp2);
4951
4952 case ISD::SMIN:
4953 case ISD::SMAX: {
4954 // If we have a clamp pattern, we know that the number of sign bits will be
4955 // the minimum of the clamp min/max range.
4956 bool IsMax = (Opcode == ISD::SMAX);
4957 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4958 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4959 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4960 CstHigh =
4961 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4962 if (CstLow && CstHigh) {
4963 if (!IsMax)
4964 std::swap(CstLow, CstHigh);
4965 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4966 Tmp = CstLow->getAPIntValue().getNumSignBits();
4967 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4968 return std::min(Tmp, Tmp2);
4969 }
4970 }
4971
4972 // Fallback - just get the minimum number of sign bits of the operands.
4973 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4974 if (Tmp == 1)
4975 return 1; // Early out.
4976 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4977 return std::min(Tmp, Tmp2);
4978 }
4979 case ISD::UMIN:
4980 case ISD::UMAX:
4981 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4982 if (Tmp == 1)
4983 return 1; // Early out.
4984 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4985 return std::min(Tmp, Tmp2);
4986 case ISD::SSUBO_CARRY:
4987 case ISD::USUBO_CARRY:
4988 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4989 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4990 return VTBits;
4991 [[fallthrough]];
4992 case ISD::SADDO:
4993 case ISD::UADDO:
4994 case ISD::SADDO_CARRY:
4995 case ISD::UADDO_CARRY:
4996 case ISD::SSUBO:
4997 case ISD::USUBO:
4998 case ISD::SMULO:
4999 case ISD::UMULO:
5000 if (Op.getResNo() != 1)
5001 break;
5002 // The boolean result conforms to getBooleanContents. Fall through.
5003 // If setcc returns 0/-1, all bits are sign bits.
5004 // We know that we have an integer-based boolean since these operations
5005 // are only available for integer.
5006 if (TLI->getBooleanContents(VT.isVector(), false) ==
5008 return VTBits;
5009 break;
5010 case ISD::SETCC:
5011 case ISD::SETCCCARRY:
5012 case ISD::STRICT_FSETCC:
5013 case ISD::STRICT_FSETCCS: {
5014 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5015 // If setcc returns 0/-1, all bits are sign bits.
5016 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5018 return VTBits;
5019 break;
5020 }
5021 case ISD::ROTL:
5022 case ISD::ROTR:
5023 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5024
5025 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5026 if (Tmp == VTBits)
5027 return VTBits;
5028
5029 if (ConstantSDNode *C =
5030 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5031 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5032
5033 // Handle rotate right by N like a rotate left by 32-N.
5034 if (Opcode == ISD::ROTR)
5035 RotAmt = (VTBits - RotAmt) % VTBits;
5036
5037 // If we aren't rotating out all of the known-in sign bits, return the
5038 // number that are left. This handles rotl(sext(x), 1) for example.
5039 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5040 }
5041 break;
5042 case ISD::ADD:
5043 case ISD::ADDC:
5044 // Add can have at most one carry bit. Thus we know that the output
5045 // is, at worst, one more bit than the inputs.
5046 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5047 if (Tmp == 1) return 1; // Early out.
5048
5049 // Special case decrementing a value (ADD X, -1):
5050 if (ConstantSDNode *CRHS =
5051 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5052 if (CRHS->isAllOnes()) {
5053 KnownBits Known =
5054 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5055
5056 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5057 // sign bits set.
5058 if ((Known.Zero | 1).isAllOnes())
5059 return VTBits;
5060
5061 // If we are subtracting one from a positive number, there is no carry
5062 // out of the result.
5063 if (Known.isNonNegative())
5064 return Tmp;
5065 }
5066
5067 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5068 if (Tmp2 == 1) return 1; // Early out.
5069 return std::min(Tmp, Tmp2) - 1;
5070 case ISD::SUB:
5071 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5072 if (Tmp2 == 1) return 1; // Early out.
5073
5074 // Handle NEG.
5075 if (ConstantSDNode *CLHS =
5076 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5077 if (CLHS->isZero()) {
5078 KnownBits Known =
5079 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5080 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5081 // sign bits set.
5082 if ((Known.Zero | 1).isAllOnes())
5083 return VTBits;
5084
5085 // If the input is known to be positive (the sign bit is known clear),
5086 // the output of the NEG has the same number of sign bits as the input.
5087 if (Known.isNonNegative())
5088 return Tmp2;
5089
5090 // Otherwise, we treat this like a SUB.
5091 }
5092
5093 // Sub can have at most one carry bit. Thus we know that the output
5094 // is, at worst, one more bit than the inputs.
5095 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5096 if (Tmp == 1) return 1; // Early out.
5097 return std::min(Tmp, Tmp2) - 1;
5098 case ISD::MUL: {
5099 // The output of the Mul can be at most twice the valid bits in the inputs.
5100 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5101 if (SignBitsOp0 == 1)
5102 break;
5103 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5104 if (SignBitsOp1 == 1)
5105 break;
5106 unsigned OutValidBits =
5107 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5108 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5109 }
5110 case ISD::AVGCEILS:
5111 case ISD::AVGFLOORS:
5112 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5113 if (Tmp == 1)
5114 return 1; // Early out.
5115 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5116 return std::min(Tmp, Tmp2);
5117 case ISD::SREM:
5118 // The sign bit is the LHS's sign bit, except when the result of the
5119 // remainder is zero. The magnitude of the result should be less than or
5120 // equal to the magnitude of the LHS. Therefore, the result should have
5121 // at least as many sign bits as the left hand side.
5122 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5123 case ISD::TRUNCATE: {
5124 // Check if the sign bits of source go down as far as the truncated value.
5125 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5126 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5127 if (NumSrcSignBits > (NumSrcBits - VTBits))
5128 return NumSrcSignBits - (NumSrcBits - VTBits);
5129 break;
5130 }
5131 case ISD::EXTRACT_ELEMENT: {
5132 if (VT.isScalableVector())
5133 break;
5134 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5135 const int BitWidth = Op.getValueSizeInBits();
5136 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5137
5138 // Get reverse index (starting from 1), Op1 value indexes elements from
5139 // little end. Sign starts at big end.
5140 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5141
5142 // If the sign portion ends in our element the subtraction gives correct
5143 // result. Otherwise it gives either negative or > bitwidth result
5144 return std::clamp(KnownSign - rIndex * BitWidth, 0, BitWidth);
5145 }
5147 if (VT.isScalableVector())
5148 break;
5149 // If we know the element index, split the demand between the
5150 // source vector and the inserted element, otherwise assume we need
5151 // the original demanded vector elements and the value.
5152 SDValue InVec = Op.getOperand(0);
5153 SDValue InVal = Op.getOperand(1);
5154 SDValue EltNo = Op.getOperand(2);
5155 bool DemandedVal = true;
5156 APInt DemandedVecElts = DemandedElts;
5157 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5158 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5159 unsigned EltIdx = CEltNo->getZExtValue();
5160 DemandedVal = !!DemandedElts[EltIdx];
5161 DemandedVecElts.clearBit(EltIdx);
5162 }
5163 Tmp = std::numeric_limits<unsigned>::max();
5164 if (DemandedVal) {
5165 // TODO - handle implicit truncation of inserted elements.
5166 if (InVal.getScalarValueSizeInBits() != VTBits)
5167 break;
5168 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5169 Tmp = std::min(Tmp, Tmp2);
5170 }
5171 if (!!DemandedVecElts) {
5172 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5173 Tmp = std::min(Tmp, Tmp2);
5174 }
5175 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5176 return Tmp;
5177 }
5179 assert(!VT.isScalableVector());
5180 SDValue InVec = Op.getOperand(0);
5181 SDValue EltNo = Op.getOperand(1);
5182 EVT VecVT = InVec.getValueType();
5183 // ComputeNumSignBits not yet implemented for scalable vectors.
5184 if (VecVT.isScalableVector())
5185 break;
5186 const unsigned BitWidth = Op.getValueSizeInBits();
5187 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5188 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5189
5190 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5191 // anything about sign bits. But if the sizes match we can derive knowledge
5192 // about sign bits from the vector operand.
5193 if (BitWidth != EltBitWidth)
5194 break;
5195
5196 // If we know the element index, just demand that vector element, else for
5197 // an unknown element index, ignore DemandedElts and demand them all.
5198 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5199 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5200 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5201 DemandedSrcElts =
5202 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5203
5204 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5205 }
5207 // Offset the demanded elts by the subvector index.
5208 SDValue Src = Op.getOperand(0);
5209 // Bail until we can represent demanded elements for scalable vectors.
5210 if (Src.getValueType().isScalableVector())
5211 break;
5212 uint64_t Idx = Op.getConstantOperandVal(1);
5213 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5214 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5215 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5216 }
5217 case ISD::CONCAT_VECTORS: {
5218 if (VT.isScalableVector())
5219 break;
5220 // Determine the minimum number of sign bits across all demanded
5221 // elts of the input vectors. Early out if the result is already 1.
5222 Tmp = std::numeric_limits<unsigned>::max();
5223 EVT SubVectorVT = Op.getOperand(0).getValueType();
5224 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5225 unsigned NumSubVectors = Op.getNumOperands();
5226 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5227 APInt DemandedSub =
5228 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5229 if (!DemandedSub)
5230 continue;
5231 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5232 Tmp = std::min(Tmp, Tmp2);
5233 }
5234 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5235 return Tmp;
5236 }
5237 case ISD::INSERT_SUBVECTOR: {
5238 if (VT.isScalableVector())
5239 break;
5240 // Demand any elements from the subvector and the remainder from the src its
5241 // inserted into.
5242 SDValue Src = Op.getOperand(0);
5243 SDValue Sub = Op.getOperand(1);
5244 uint64_t Idx = Op.getConstantOperandVal(2);
5245 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5246 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5247 APInt DemandedSrcElts = DemandedElts;
5248 DemandedSrcElts.insertBits(APInt::getZero(NumSubElts), Idx);
5249
5250 Tmp = std::numeric_limits<unsigned>::max();
5251 if (!!DemandedSubElts) {
5252 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5253 if (Tmp == 1)
5254 return 1; // early-out
5255 }
5256 if (!!DemandedSrcElts) {
5257 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5258 Tmp = std::min(Tmp, Tmp2);
5259 }
5260 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5261 return Tmp;
5262 }
5263 case ISD::LOAD: {
5264 LoadSDNode *LD = cast<LoadSDNode>(Op);
5265 if (const MDNode *Ranges = LD->getRanges()) {
5266 if (DemandedElts != 1)
5267 break;
5268
5270 if (VTBits > CR.getBitWidth()) {
5271 switch (LD->getExtensionType()) {
5272 case ISD::SEXTLOAD:
5273 CR = CR.signExtend(VTBits);
5274 break;
5275 case ISD::ZEXTLOAD:
5276 CR = CR.zeroExtend(VTBits);
5277 break;
5278 default:
5279 break;
5280 }
5281 }
5282
5283 if (VTBits != CR.getBitWidth())
5284 break;
5285 return std::min(CR.getSignedMin().getNumSignBits(),
5287 }
5288
5289 break;
5290 }
5293 case ISD::ATOMIC_SWAP:
5305 case ISD::ATOMIC_LOAD: {
5306 Tmp = cast<AtomicSDNode>(Op)->getMemoryVT().getScalarSizeInBits();
5307 // If we are looking at the loaded value.
5308 if (Op.getResNo() == 0) {
5309 if (Tmp == VTBits)
5310 return 1; // early-out
5312 return VTBits - Tmp + 1;
5314 return VTBits - Tmp;
5315 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5316 ISD::LoadExtType ETy = cast<AtomicSDNode>(Op)->getExtensionType();
5317 if (ETy == ISD::SEXTLOAD)
5318 return VTBits - Tmp + 1;
5319 if (ETy == ISD::ZEXTLOAD)
5320 return VTBits - Tmp;
5321 }
5322 }
5323 break;
5324 }
5325 }
5326
5327 // If we are looking at the loaded value of the SDNode.
5328 if (Op.getResNo() == 0) {
5329 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5330 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5331 unsigned ExtType = LD->getExtensionType();
5332 switch (ExtType) {
5333 default: break;
5334 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5335 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5336 return VTBits - Tmp + 1;
5337 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5338 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5339 return VTBits - Tmp;
5340 case ISD::NON_EXTLOAD:
5341 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5342 // We only need to handle vectors - computeKnownBits should handle
5343 // scalar cases.
5344 Type *CstTy = Cst->getType();
5345 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5346 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5347 VTBits == CstTy->getScalarSizeInBits()) {
5348 Tmp = VTBits;
5349 for (unsigned i = 0; i != NumElts; ++i) {
5350 if (!DemandedElts[i])
5351 continue;
5352 if (Constant *Elt = Cst->getAggregateElement(i)) {
5353 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5354 const APInt &Value = CInt->getValue();
5355 Tmp = std::min(Tmp, Value.getNumSignBits());
5356 continue;
5357 }
5358 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5359 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5360 Tmp = std::min(Tmp, Value.getNumSignBits());
5361 continue;
5362 }
5363 }
5364 // Unknown type. Conservatively assume no bits match sign bit.
5365 return 1;
5366 }
5367 return Tmp;
5368 }
5369 }
5370 break;
5371 }
5372 }
5373 }
5374
5375 // Allow the target to implement this method for its nodes.
5376 if (Opcode >= ISD::BUILTIN_OP_END ||
5377 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5378 Opcode == ISD::INTRINSIC_W_CHAIN ||
5379 Opcode == ISD::INTRINSIC_VOID) {
5380 // TODO: This can probably be removed once target code is audited. This
5381 // is here purely to reduce patch size and review complexity.
5382 if (!VT.isScalableVector()) {
5383 unsigned NumBits =
5384 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5385 if (NumBits > 1)
5386 FirstAnswer = std::max(FirstAnswer, NumBits);
5387 }
5388 }
5389
5390 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5391 // use this information.
5392 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5393 return std::max(FirstAnswer, Known.countMinSignBits());
5394}
5395
5397 unsigned Depth) const {
5398 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5399 return Op.getScalarValueSizeInBits() - SignBits + 1;
5400}
5401
5403 const APInt &DemandedElts,
5404 unsigned Depth) const {
5405 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5406 return Op.getScalarValueSizeInBits() - SignBits + 1;
5407}
5408
5410 unsigned Depth) const {
5411 // Early out for FREEZE.
5412 if (Op.getOpcode() == ISD::FREEZE)
5413 return true;
5414
5415 EVT VT = Op.getValueType();
5416 APInt DemandedElts = VT.isFixedLengthVector()
5418 : APInt(1, 1);
5419 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5420}
5421
5423 const APInt &DemandedElts,
5424 bool PoisonOnly,
5425 unsigned Depth) const {
5426 unsigned Opcode = Op.getOpcode();
5427
5428 // Early out for FREEZE.
5429 if (Opcode == ISD::FREEZE)
5430 return true;
5431
5432 if (Depth >= MaxRecursionDepth)
5433 return false; // Limit search depth.
5434
5435 if (isIntOrFPConstant(Op))
5436 return true;
5437
5438 switch (Opcode) {
5439 case ISD::CONDCODE:
5440 case ISD::VALUETYPE:
5441 case ISD::FrameIndex:
5443 case ISD::CopyFromReg:
5444 return true;
5445
5446 case ISD::UNDEF:
5447 return PoisonOnly;
5448
5449 case ISD::BUILD_VECTOR:
5450 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5451 // this shouldn't affect the result.
5452 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5453 if (!DemandedElts[i])
5454 continue;
5456 Depth + 1))
5457 return false;
5458 }
5459 return true;
5460
5461 case ISD::SPLAT_VECTOR:
5462 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5463 Depth + 1);
5464
5465 case ISD::VECTOR_SHUFFLE: {
5466 APInt DemandedLHS, DemandedRHS;
5467 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5468 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5469 DemandedElts, DemandedLHS, DemandedRHS,
5470 /*AllowUndefElts=*/false))
5471 return false;
5472 if (!DemandedLHS.isZero() &&
5473 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5474 PoisonOnly, Depth + 1))
5475 return false;
5476 if (!DemandedRHS.isZero() &&
5477 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5478 PoisonOnly, Depth + 1))
5479 return false;
5480 return true;
5481 }
5482
5483 // TODO: Search for noundef attributes from library functions.
5484
5485 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5486
5487 default:
5488 // Allow the target to implement this method for its nodes.
5489 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5490 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5492 Op, DemandedElts, *this, PoisonOnly, Depth);
5493 break;
5494 }
5495
5496 // If Op can't create undef/poison and none of its operands are undef/poison
5497 // then Op is never undef/poison.
5498 // NOTE: TargetNodes can handle this in themselves in
5499 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5500 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5501 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5502 Depth) &&
5503 all_of(Op->ops(), [&](SDValue V) {
5504 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5505 });
5506}
5507
5509 bool ConsiderFlags,
5510 unsigned Depth) const {
5511 EVT VT = Op.getValueType();
5512 APInt DemandedElts = VT.isFixedLengthVector()
5514 : APInt(1, 1);
5515 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5516 Depth);
5517}
5518
5520 bool PoisonOnly, bool ConsiderFlags,
5521 unsigned Depth) const {
5522 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5523 return true;
5524
5525 unsigned Opcode = Op.getOpcode();
5526 switch (Opcode) {
5527 case ISD::FREEZE:
5530 case ISD::SADDSAT:
5531 case ISD::UADDSAT:
5532 case ISD::SSUBSAT:
5533 case ISD::USUBSAT:
5534 case ISD::MULHU:
5535 case ISD::MULHS:
5536 case ISD::SMIN:
5537 case ISD::SMAX:
5538 case ISD::UMIN:
5539 case ISD::UMAX:
5540 case ISD::AND:
5541 case ISD::XOR:
5542 case ISD::ROTL:
5543 case ISD::ROTR:
5544 case ISD::FSHL:
5545 case ISD::FSHR:
5546 case ISD::BSWAP:
5547 case ISD::CTPOP:
5548 case ISD::BITREVERSE:
5549 case ISD::PARITY:
5550 case ISD::SIGN_EXTEND:
5551 case ISD::TRUNCATE:
5555 case ISD::BITCAST:
5556 case ISD::BUILD_VECTOR:
5557 case ISD::BUILD_PAIR:
5558 case ISD::SPLAT_VECTOR:
5559 return false;
5560
5561 case ISD::SELECT_CC:
5562 case ISD::SETCC: {
5563 // Integer setcc cannot create undef or poison.
5564 if (Op.getOperand(0).getValueType().isInteger())
5565 return false;
5566
5567 // FP compares are more complicated. They can create poison for nan/infinity
5568 // based on options and flags. The options and flags also cause special
5569 // nonan condition codes to be used. Those condition codes may be preserved
5570 // even if the nonan flag is dropped somewhere.
5571 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5572 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5573 if (((unsigned)CCCode & 0x10U))
5574 return true;
5575
5577 return Options.NoNaNsFPMath || Options.NoInfsFPMath;
5578 }
5579
5580 case ISD::OR:
5581 case ISD::ZERO_EXTEND:
5582 case ISD::ADD:
5583 case ISD::SUB:
5584 case ISD::MUL:
5585 // No poison except from flags (which is handled above)
5586 return false;
5587
5588 case ISD::SHL:
5589 case ISD::SRL:
5590 case ISD::SRA:
5591 // If the max shift amount isn't in range, then the shift can
5592 // create poison.
5593 return !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedElts,
5594 PoisonOnly, Depth + 1) ||
5595 !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5596
5598 // Check if we demand any upper (undef) elements.
5599 return !PoisonOnly && DemandedElts.ugt(1);
5600
5603 // Ensure that the element index is in bounds.
5604 EVT VecVT = Op.getOperand(0).getValueType();
5605 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5607 Depth + 1)) {
5608 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5609 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5610 }
5611 return true;
5612 }
5613
5614 case ISD::VECTOR_SHUFFLE: {
5615 // Check for any demanded shuffle element that is undef.
5616 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5617 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5618 if (Elt < 0 && DemandedElts[Idx])
5619 return true;
5620 return false;
5621 }
5622
5623 default:
5624 // Allow the target to implement this method for its nodes.
5625 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5626 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5628 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5629 break;
5630 }
5631
5632 // Be conservative and return true.
5633 return true;
5634}
5635
5636bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5637 unsigned Opcode = Op.getOpcode();
5638 if (Opcode == ISD::OR)
5639 return Op->getFlags().hasDisjoint() ||
5640 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5641 if (Opcode == ISD::XOR)
5642 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5643 return false;
5644}
5645
5647 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5648 (Op.getOpcode() == ISD::ADD || isADDLike(Op));
5649}
5650
5651bool SelectionDAG::isKnownNeverNaN(SDValue Op, bool SNaN, unsigned Depth) const {
5652 // If we're told that NaNs won't happen, assume they won't.
5653 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5654 return true;
5655
5656 if (Depth >= MaxRecursionDepth)
5657 return false; // Limit search depth.
5658
5659 // If the value is a constant, we can obviously see if it is a NaN or not.
5660 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) {
5661 return !C->getValueAPF().isNaN() ||
5662 (SNaN && !C->getValueAPF().isSignaling());
5663 }
5664
5665 unsigned Opcode = Op.getOpcode();
5666 switch (Opcode) {
5667 case ISD::FADD:
5668 case ISD::FSUB:
5669 case ISD::FMUL:
5670 case ISD::FDIV:
5671 case ISD::FREM:
5672 case ISD::FSIN:
5673 case ISD::FCOS:
5674 case ISD::FTAN:
5675 case ISD::FASIN:
5676 case ISD::FACOS:
5677 case ISD::FATAN:
5678 case ISD::FATAN2:
5679 case ISD::FSINH:
5680 case ISD::FCOSH:
5681 case ISD::FTANH:
5682 case ISD::FMA:
5683 case ISD::FMAD: {
5684 if (SNaN)
5685 return true;
5686 // TODO: Need isKnownNeverInfinity
5687 return false;
5688 }
5689 case ISD::FCANONICALIZE:
5690 case ISD::FEXP:
5691 case ISD::FEXP2:
5692 case ISD::FEXP10:
5693 case ISD::FTRUNC:
5694 case ISD::FFLOOR:
5695 case ISD::FCEIL:
5696 case ISD::FROUND:
5697 case ISD::FROUNDEVEN:
5698 case ISD::LROUND:
5699 case ISD::LLROUND:
5700 case ISD::FRINT:
5701 case ISD::LRINT:
5702 case ISD::LLRINT:
5703 case ISD::FNEARBYINT:
5704 case ISD::FLDEXP: {
5705 if (SNaN)
5706 return true;
5707 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5708 }
5709 case ISD::FABS:
5710 case ISD::FNEG:
5711 case ISD::FCOPYSIGN: {
5712 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5713 }
5714 case ISD::SELECT:
5715 return isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1) &&
5716 isKnownNeverNaN(Op.getOperand(2), SNaN, Depth + 1);
5717 case ISD::FP_EXTEND:
5718 case ISD::FP_ROUND: {
5719 if (SNaN)
5720 return true;
5721 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5722 }
5723 case ISD::SINT_TO_FP:
5724 case ISD::UINT_TO_FP:
5725 return true;
5726 case ISD::FSQRT: // Need is known positive
5727 case ISD::FLOG:
5728 case ISD::FLOG2:
5729 case ISD::FLOG10:
5730 case ISD::FPOWI:
5731 case ISD::FPOW: {
5732 if (SNaN)
5733 return true;
5734 // TODO: Refine on operand
5735 return false;
5736 }
5737 case ISD::FMINNUM:
5738 case ISD::FMAXNUM:
5739 case ISD::FMINIMUMNUM:
5740 case ISD::FMAXIMUMNUM: {
5741 // Only one needs to be known not-nan, since it will be returned if the
5742 // other ends up being one.
5743 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) ||
5744 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5745 }
5746 case ISD::FMINNUM_IEEE:
5747 case ISD::FMAXNUM_IEEE: {
5748 if (SNaN)
5749 return true;
5750 // This can return a NaN if either operand is an sNaN, or if both operands
5751 // are NaN.
5752 return (isKnownNeverNaN(Op.getOperand(0), false, Depth + 1) &&
5753 isKnownNeverSNaN(Op.getOperand(1), Depth + 1)) ||
5754 (isKnownNeverNaN(Op.getOperand(1), false, Depth + 1) &&
5755 isKnownNeverSNaN(Op.getOperand(0), Depth + 1));
5756 }
5757 case ISD::FMINIMUM:
5758 case ISD::FMAXIMUM: {
5759 // TODO: Does this quiet or return the origina NaN as-is?
5760 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1) &&
5761 isKnownNeverNaN(Op.getOperand(1), SNaN, Depth + 1);
5762 }
5764 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
5765 }
5766 case ISD::BUILD_VECTOR: {
5767 for (const SDValue &Opnd : Op->ops())
5768 if (!isKnownNeverNaN(Opnd, SNaN, Depth + 1))
5769 return false;
5770 return true;
5771 }
5772 default:
5773 if (Opcode >= ISD::BUILTIN_OP_END ||
5774 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5775 Opcode == ISD::INTRINSIC_W_CHAIN ||
5776 Opcode == ISD::INTRINSIC_VOID) {
5777 return TLI->isKnownNeverNaNForTargetNode(Op, *this, SNaN, Depth);
5778 }
5779
5780 return false;
5781 }
5782}
5783
5785 assert(Op.getValueType().isFloatingPoint() &&
5786 "Floating point type expected");
5787
5788 // If the value is a constant, we can obviously see if it is a zero or not.
5790 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5791}
5792
5794 if (Depth >= MaxRecursionDepth)
5795 return false; // Limit search depth.
5796
5797 assert(!Op.getValueType().isFloatingPoint() &&
5798 "Floating point types unsupported - use isKnownNeverZeroFloat");
5799
5800 // If the value is a constant, we can obviously see if it is a zero or not.
5802 [](ConstantSDNode *C) { return !C->isZero(); }))
5803 return true;
5804
5805 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5806 // some degree.
5807 switch (Op.getOpcode()) {
5808 default:
5809 break;
5810
5811 case ISD::OR:
5812 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5813 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5814
5815 case ISD::VSELECT:
5816 case ISD::SELECT:
5817 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5818 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5819
5820 case ISD::SHL: {
5821 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5822 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5823 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5824 // 1 << X is never zero.
5825 if (ValKnown.One[0])
5826 return true;
5827 // If max shift cnt of known ones is non-zero, result is non-zero.
5828 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5829 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5830 !ValKnown.One.shl(MaxCnt).isZero())
5831 return true;
5832 break;
5833 }
5834 case ISD::UADDSAT:
5835 case ISD::UMAX:
5836 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5837 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5838
5839 // For smin/smax: If either operand is known negative/positive
5840 // respectively we don't need the other to be known at all.
5841 case ISD::SMAX: {
5842 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5843 if (Op1.isStrictlyPositive())
5844 return true;
5845
5846 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5847 if (Op0.isStrictlyPositive())
5848 return true;
5849
5850 if (Op1.isNonZero() && Op0.isNonZero())
5851 return true;
5852
5853 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5854 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5855 }
5856 case ISD::SMIN: {
5857 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
5858 if (Op1.isNegative())
5859 return true;
5860
5861 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
5862 if (Op0.isNegative())
5863 return true;
5864
5865 if (Op1.isNonZero() && Op0.isNonZero())
5866 return true;
5867
5868 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5869 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5870 }
5871 case ISD::UMIN:
5872 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5873 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5874
5875 case ISD::ROTL:
5876 case ISD::ROTR:
5877 case ISD::BITREVERSE:
5878 case ISD::BSWAP:
5879 case ISD::CTPOP:
5880 case ISD::ABS:
5881 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5882
5883 case ISD::SRA:
5884 case ISD::SRL: {
5885 if (Op->getFlags().hasExact())
5886 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5887 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5888 if (ValKnown.isNegative())
5889 return true;
5890 // If max shift cnt of known ones is non-zero, result is non-zero.
5891 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5892 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5893 !ValKnown.One.lshr(MaxCnt).isZero())
5894 return true;
5895 break;
5896 }
5897 case ISD::UDIV:
5898 case ISD::SDIV:
5899 // div exact can only produce a zero if the dividend is zero.
5900 // TODO: For udiv this is also true if Op1 u<= Op0
5901 if (Op->getFlags().hasExact())
5902 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5903 break;
5904
5905 case ISD::ADD:
5906 if (Op->getFlags().hasNoUnsignedWrap())
5907 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5908 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5909 return true;
5910 // TODO: There are a lot more cases we can prove for add.
5911 break;
5912
5913 case ISD::SUB: {
5914 if (isNullConstant(Op.getOperand(0)))
5915 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
5916
5917 std::optional<bool> ne =
5918 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
5919 computeKnownBits(Op.getOperand(1), Depth + 1));
5920 return ne && *ne;
5921 }
5922
5923 case ISD::MUL:
5924 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5925 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5926 isKnownNeverZero(Op.getOperand(0), Depth + 1))
5927 return true;
5928 break;
5929
5930 case ISD::ZERO_EXTEND:
5931 case ISD::SIGN_EXTEND:
5932 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5933 case ISD::VSCALE: {
5935 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
5936 ConstantRange CR =
5937 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
5938 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
5939 return true;
5940 break;
5941 }
5942 }
5943
5945}
5946
5948 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
5949 return !C1->isNegative();
5950
5951 return Op.getOpcode() == ISD::FABS;
5952}
5953
5955 // Check the obvious case.
5956 if (A == B) return true;
5957
5958 // For negative and positive zero.
5959 if (const ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A))
5960 if (const ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B))
5961 if (CA->isZero() && CB->isZero()) return true;
5962
5963 // Otherwise they may not be equal.
5964 return false;
5965}
5966
5967// Only bits set in Mask must be negated, other bits may be arbitrary.
5969 if (isBitwiseNot(V, AllowUndefs))
5970 return V.getOperand(0);
5971
5972 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
5973 // bits in the non-extended part.
5974 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
5975 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
5976 return SDValue();
5977 SDValue ExtArg = V.getOperand(0);
5978 if (ExtArg.getScalarValueSizeInBits() >=
5979 MaskC->getAPIntValue().getActiveBits() &&
5980 isBitwiseNot(ExtArg, AllowUndefs) &&
5981 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
5982 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
5983 return ExtArg.getOperand(0).getOperand(0);
5984 return SDValue();
5985}
5986
5988 // Match masked merge pattern (X & ~M) op (Y & M)
5989 // Including degenerate case (X & ~M) op M
5990 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
5991 SDValue Other) {
5992 if (SDValue NotOperand =
5993 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
5994 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
5995 NotOperand->getOpcode() == ISD::TRUNCATE)
5996 NotOperand = NotOperand->getOperand(0);
5997
5998 if (Other == NotOperand)
5999 return true;
6000 if (Other->getOpcode() == ISD::AND)
6001 return NotOperand == Other->getOperand(0) ||
6002 NotOperand == Other->getOperand(1);
6003 }
6004 return false;
6005 };
6006
6007 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6008 A = A->getOperand(0);
6009
6010 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6011 B = B->getOperand(0);
6012
6013 if (A->getOpcode() == ISD::AND)
6014 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6015 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6016 return false;
6017}
6018
6019// FIXME: unify with llvm::haveNoCommonBitsSet.
6021 assert(A.getValueType() == B.getValueType() &&
6022 "Values must have the same type");
6025 return true;
6028}
6029
6030static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6031 SelectionDAG &DAG) {
6032 if (cast<ConstantSDNode>(Step)->isZero())
6033 return DAG.getConstant(0, DL, VT);
6034
6035 return SDValue();
6036}
6037
6040 SelectionDAG &DAG) {
6041 int NumOps = Ops.size();
6042 assert(NumOps != 0 && "Can't build an empty vector!");
6043 assert(!VT.isScalableVector() &&
6044 "BUILD_VECTOR cannot be used with scalable types");
6045 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6046 "Incorrect element count in BUILD_VECTOR!");
6047
6048 // BUILD_VECTOR of UNDEFs is UNDEF.
6049 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6050 return DAG.getUNDEF(VT);
6051
6052 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6053 SDValue IdentitySrc;
6054 bool IsIdentity = true;
6055 for (int i = 0; i != NumOps; ++i) {
6056 if (Ops[i].getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6057 Ops[i].getOperand(0).getValueType() != VT ||
6058 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6059 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6060 Ops[i].getConstantOperandAPInt(1) != i) {
6061 IsIdentity = false;
6062 break;
6063 }
6064 IdentitySrc = Ops[i].getOperand(0);
6065 }
6066 if (IsIdentity)
6067 return IdentitySrc;
6068
6069 return SDValue();
6070}
6071
6072/// Try to simplify vector concatenation to an input value, undef, or build
6073/// vector.
6076 SelectionDAG &DAG) {
6077 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6078 assert(llvm::all_of(Ops,
6079 [Ops](SDValue Op) {
6080 return Ops[0].getValueType() == Op.getValueType();
6081 }) &&
6082 "Concatenation of vectors with inconsistent value types!");
6083 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6084 VT.getVectorElementCount() &&
6085 "Incorrect element count in vector concatenation!");
6086
6087 if (Ops.size() == 1)
6088 return Ops[0];
6089
6090 // Concat of UNDEFs is UNDEF.
6091 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6092 return DAG.getUNDEF(VT);
6093
6094 // Scan the operands and look for extract operations from a single source
6095 // that correspond to insertion at the same location via this concatenation:
6096 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6097 SDValue IdentitySrc;
6098 bool IsIdentity = true;
6099 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6100 SDValue Op = Ops[i];
6101 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6102 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6103 Op.getOperand(0).getValueType() != VT ||
6104 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6105 Op.getConstantOperandVal(1) != IdentityIndex) {
6106 IsIdentity = false;
6107 break;
6108 }
6109 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6110 "Unexpected identity source vector for concat of extracts");
6111 IdentitySrc = Op.getOperand(0);
6112 }
6113 if (IsIdentity) {
6114 assert(IdentitySrc && "Failed to set source vector of extracts");
6115 return IdentitySrc;
6116 }
6117
6118 // The code below this point is only designed to work for fixed width
6119 // vectors, so we bail out for now.
6120 if (VT.isScalableVector())
6121 return SDValue();
6122
6123 // A CONCAT_VECTOR with all UNDEF/BUILD_VECTOR operands can be
6124 // simplified to one big BUILD_VECTOR.
6125 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6126 EVT SVT = VT.getScalarType();
6128 for (SDValue Op : Ops) {
6129 EVT OpVT = Op.getValueType();
6130 if (Op.isUndef())
6131 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6132 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6133 Elts.append(Op->op_begin(), Op->op_end());
6134 else
6135 return SDValue();
6136 }
6137
6138 // BUILD_VECTOR requires all inputs to be of the same type, find the
6139 // maximum type and extend them all.
6140 for (SDValue Op : Elts)
6141 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6142
6143 if (SVT.bitsGT(VT.getScalarType())) {
6144 for (SDValue &Op : Elts) {
6145 if (Op.isUndef())
6146 Op = DAG.getUNDEF(SVT);
6147 else
6148 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6149 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6150 : DAG.getSExtOrTrunc(Op, DL, SVT);
6151 }
6152 }
6153
6154 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6155 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6156 return V;
6157}
6158
6159/// Gets or creates the specified node.
6160SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6161 SDVTList VTs = getVTList(VT);
6163 AddNodeIDNode(ID, Opcode, VTs, {});
6164 void *IP = nullptr;
6165 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6166 return SDValue(E, 0);
6167
6168 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6169 CSEMap.InsertNode(N, IP);
6170
6171 InsertNode(N);
6172 SDValue V = SDValue(N, 0);
6173 NewSDValueDbgMsg(V, "Creating new node: ", this);
6174 return V;
6175}
6176
6177SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6178 SDValue N1) {
6179 SDNodeFlags Flags;
6180 if (Inserter)
6181 Flags = Inserter->getFlags();
6182 return getNode(Opcode, DL, VT, N1, Flags);
6183}
6184
6185SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6186 SDValue N1, const SDNodeFlags Flags) {
6187 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6188
6189 // Constant fold unary operations with a vector integer or float operand.
6190 switch (Opcode) {
6191 default:
6192 // FIXME: Entirely reasonable to perform folding of other unary
6193 // operations here as the need arises.
6194 break;
6195 case ISD::FNEG:
6196 case ISD::FABS:
6197 case ISD::FCEIL:
6198 case ISD::FTRUNC:
6199 case ISD::FFLOOR:
6200 case ISD::FP_EXTEND:
6201 case ISD::FP_TO_SINT:
6202 case ISD::FP_TO_UINT:
6203 case ISD::FP_TO_FP16:
6204 case ISD::FP_TO_BF16:
6205 case ISD::TRUNCATE:
6206 case ISD::ANY_EXTEND:
6207 case ISD::ZERO_EXTEND:
6208 case ISD::SIGN_EXTEND:
6209 case ISD::UINT_TO_FP:
6210 case ISD::SINT_TO_FP:
6211 case ISD::FP16_TO_FP:
6212 case ISD::BF16_TO_FP:
6213 case ISD::BITCAST:
6214 case ISD::ABS:
6215 case ISD::BITREVERSE:
6216 case ISD::BSWAP:
6217 case ISD::CTLZ:
6219 case ISD::CTTZ:
6221 case ISD::CTPOP:
6222 case ISD::STEP_VECTOR: {
6223 SDValue Ops = {N1};
6224 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6225 return Fold;
6226 }
6227 }
6228
6229 unsigned OpOpcode = N1.getNode()->getOpcode();
6230 switch (Opcode) {
6231 case ISD::STEP_VECTOR:
6232 assert(VT.isScalableVector() &&
6233 "STEP_VECTOR can only be used with scalable types");
6234 assert(OpOpcode == ISD::TargetConstant &&
6235 VT.getVectorElementType() == N1.getValueType() &&
6236 "Unexpected step operand");
6237 break;
6238 case ISD::FREEZE:
6239 assert(VT == N1.getValueType() && "Unexpected VT!");
6240 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly*/ false,
6241 /*Depth*/ 1))
6242 return N1;
6243 break;
6244 case ISD::TokenFactor:
6245 case ISD::MERGE_VALUES:
6247 return N1; // Factor, merge or concat of one node? No need.
6248 case ISD::BUILD_VECTOR: {
6249 // Attempt to simplify BUILD_VECTOR.
6250 SDValue Ops[] = {N1};
6251 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6252 return V;
6253 break;
6254 }
6255 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6256 case ISD::FP_EXTEND:
6258 "Invalid FP cast!");
6259 if (N1.getValueType() == VT) return N1; // noop conversion.
6260 assert((!VT.isVector() || VT.getVectorElementCount() ==
6262 "Vector element count mismatch!");
6263 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6264 if (N1.isUndef())
6265 return getUNDEF(VT);
6266 break;
6267 case ISD::FP_TO_SINT:
6268 case ISD::FP_TO_UINT:
6269 if (N1.isUndef())
6270 return getUNDEF(VT);
6271 break;
6272 case ISD::SINT_TO_FP:
6273 case ISD::UINT_TO_FP:
6274 // [us]itofp(undef) = 0, because the result value is bounded.
6275 if (N1.isUndef())
6276 return getConstantFP(0.0, DL, VT);
6277 break;
6278 case ISD::SIGN_EXTEND:
6279 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6280 "Invalid SIGN_EXTEND!");
6281 assert(VT.isVector() == N1.getValueType().isVector() &&
6282 "SIGN_EXTEND result type type should be vector iff the operand "
6283 "type is vector!");
6284 if (N1.getValueType() == VT) return N1; // noop extension
6285 assert((!VT.isVector() || VT.getVectorElementCount() ==
6287 "Vector element count mismatch!");
6288 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6289 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6290 SDNodeFlags Flags;
6291 if (OpOpcode == ISD::ZERO_EXTEND)
6292 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6293 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6294 }
6295 if (OpOpcode == ISD::UNDEF)
6296 // sext(undef) = 0, because the top bits will all be the same.
6297 return getConstant(0, DL, VT);
6298 break;
6299 case ISD::ZERO_EXTEND:
6300 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6301 "Invalid ZERO_EXTEND!");
6302 assert(VT.isVector() == N1.getValueType().isVector() &&
6303 "ZERO_EXTEND result type type should be vector iff the operand "
6304 "type is vector!");
6305 if (N1.getValueType() == VT) return N1; // noop extension
6306 assert((!VT.isVector() || VT.getVectorElementCount() ==
6308 "Vector element count mismatch!");
6309 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6310 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6311 SDNodeFlags Flags;
6312 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6313 return getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6314 }
6315 if (OpOpcode == ISD::UNDEF)
6316 // zext(undef) = 0, because the top bits will be zero.
6317 return getConstant(0, DL, VT);
6318
6319 // Skip unnecessary zext_inreg pattern:
6320 // (zext (trunc x)) -> x iff the upper bits are known zero.
6321 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6322 // use to recognise zext_inreg patterns.
6323 if (OpOpcode == ISD::TRUNCATE) {
6324 SDValue OpOp = N1.getOperand(0);
6325 if (OpOp.getValueType() == VT) {
6326 if (OpOp.getOpcode() != ISD::AND) {
6329 if (MaskedValueIsZero(OpOp, HiBits)) {
6330 transferDbgValues(N1, OpOp);
6331 return OpOp;
6332 }
6333 }
6334 }
6335 }
6336 break;
6337 case ISD::ANY_EXTEND:
6338 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6339 "Invalid ANY_EXTEND!");
6340 assert(VT.isVector() == N1.getValueType().isVector() &&
6341 "ANY_EXTEND result type type should be vector iff the operand "
6342 "type is vector!");
6343 if (N1.getValueType() == VT) return N1; // noop extension
6344 assert((!VT.isVector() || VT.getVectorElementCount() ==
6346 "Vector element count mismatch!");
6347 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6348
6349 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6350 OpOpcode == ISD::ANY_EXTEND) {
6351 SDNodeFlags Flags;
6352 if (OpOpcode == ISD::ZERO_EXTEND)
6353 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6354 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6355 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6356 }
6357 if (OpOpcode == ISD::UNDEF)
6358 return getUNDEF(VT);
6359
6360 // (ext (trunc x)) -> x
6361 if (OpOpcode == ISD::TRUNCATE) {
6362 SDValue OpOp = N1.getOperand(0);
6363 if (OpOp.getValueType() == VT) {
6364 transferDbgValues(N1, OpOp);
6365 return OpOp;
6366 }
6367 }
6368 break;
6369 case ISD::TRUNCATE:
6370 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6371 "Invalid TRUNCATE!");
6372 assert(VT.isVector() == N1.getValueType().isVector() &&
6373 "TRUNCATE result type type should be vector iff the operand "
6374 "type is vector!");
6375 if (N1.getValueType() == VT) return N1; // noop truncate
6376 assert((!VT.isVector() || VT.getVectorElementCount() ==
6378 "Vector element count mismatch!");
6379 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6380 if (OpOpcode == ISD::TRUNCATE)
6381 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6382 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6383 OpOpcode == ISD::ANY_EXTEND) {
6384 // If the source is smaller than the dest, we still need an extend.
6386 VT.getScalarType()))
6387 return getNode(OpOpcode, DL, VT, N1.getOperand(0));
6388 if (N1.getOperand(0).getValueType().bitsGT(VT))
6389 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6390 return N1.getOperand(0);
6391 }
6392 if (OpOpcode == ISD::UNDEF)
6393 return getUNDEF(VT);
6394 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6395 return getVScale(DL, VT,
6397 break;
6401 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6402 assert(N1.getValueType().bitsLE(VT) &&
6403 "The input must be the same size or smaller than the result.");
6406 "The destination vector type must have fewer lanes than the input.");
6407 break;
6408 case ISD::ABS:
6409 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6410 if (OpOpcode == ISD::UNDEF)
6411 return getConstant(0, DL, VT);
6412 break;
6413 case ISD::BSWAP:
6414 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6415 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6416 "BSWAP types must be a multiple of 16 bits!");
6417 if (OpOpcode == ISD::UNDEF)
6418 return getUNDEF(VT);
6419 // bswap(bswap(X)) -> X.
6420 if (OpOpcode == ISD::BSWAP)
6421 return N1.getOperand(0);
6422 break;
6423 case ISD::BITREVERSE:
6424 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6425 if (OpOpcode == ISD::UNDEF)
6426 return getUNDEF(VT);
6427 break;
6428 case ISD::BITCAST:
6430 "Cannot BITCAST between types of different sizes!");
6431 if (VT == N1.getValueType()) return N1; // noop conversion.
6432 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6433 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6434 if (OpOpcode == ISD::UNDEF)
6435 return getUNDEF(VT);
6436 break;
6438 assert(VT.isVector() && !N1.getValueType().isVector() &&
6439 (VT.getVectorElementType() == N1.getValueType() ||
6441 N1.getValueType().isInteger() &&
6443 "Illegal SCALAR_TO_VECTOR node!");
6444 if (OpOpcode == ISD::UNDEF)
6445 return getUNDEF(VT);
6446 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6447 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6448 isa<ConstantSDNode>(N1.getOperand(1)) &&
6449 N1.getConstantOperandVal(1) == 0 &&
6450 N1.getOperand(0).getValueType() == VT)
6451 return N1.getOperand(0);
6452 break;
6453 case ISD::FNEG:
6454 // Negation of an unknown bag of bits is still completely undefined.
6455 if (OpOpcode == ISD::UNDEF)
6456 return getUNDEF(VT);
6457
6458 if (OpOpcode == ISD::FNEG) // --X -> X
6459 return N1.getOperand(0);
6460 break;
6461 case ISD::FABS:
6462 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6463 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6464 break;
6465 case ISD::VSCALE:
6466 assert(VT == N1.getValueType() && "Unexpected VT!");
6467 break;
6468 case ISD::CTPOP:
6469 if (N1.getValueType().getScalarType() == MVT::i1)
6470 return N1;
6471 break;
6472 case ISD::CTLZ:
6473 case ISD::CTTZ:
6474 if (N1.getValueType().getScalarType() == MVT::i1)
6475 return getNOT(DL, N1, N1.getValueType());
6476 break;
6477 case ISD::VECREDUCE_ADD:
6478 if (N1.getValueType().getScalarType() == MVT::i1)
6479 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6480 break;
6483 if (N1.getValueType().getScalarType() == MVT::i1)
6484 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6485 break;
6488 if (N1.getValueType().getScalarType() == MVT::i1)
6489 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6490 break;
6491 case ISD::SPLAT_VECTOR:
6492 assert(VT.isVector() && "Wrong return type!");
6493 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6494 // that for now.
6496 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6498 N1.getValueType().isInteger() &&
6500 "Wrong operand type!");
6501 break;
6502 }
6503
6504 SDNode *N;
6505 SDVTList VTs = getVTList(VT);
6506 SDValue Ops[] = {N1};
6507 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6509 AddNodeIDNode(ID, Opcode, VTs, Ops);
6510 void *IP = nullptr;
6511 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6512 E->intersectFlagsWith(Flags);
6513 return SDValue(E, 0);
6514 }
6515
6516 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6517 N->setFlags(Flags);
6518 createOperands(N, Ops);
6519 CSEMap.InsertNode(N, IP);
6520 } else {
6521 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6522 createOperands(N, Ops);
6523 }
6524
6525 InsertNode(N);
6526 SDValue V = SDValue(N, 0);
6527 NewSDValueDbgMsg(V, "Creating new node: ", this);
6528 return V;
6529}
6530
6531static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6532 const APInt &C2) {
6533 switch (Opcode) {
6534 case ISD::ADD: return C1 + C2;
6535 case ISD::SUB: return C1 - C2;
6536 case ISD::MUL: return C1 * C2;
6537 case ISD::AND: return C1 & C2;
6538 case ISD::OR: return C1 | C2;
6539 case ISD::XOR: return C1 ^ C2;
6540 case ISD::SHL: return C1 << C2;
6541 case ISD::SRL: return C1.lshr(C2);
6542 case ISD::SRA: return C1.ashr(C2);
6543 case ISD::ROTL: return C1.rotl(C2);
6544 case ISD::ROTR: return C1.rotr(C2);
6545 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6546 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6547 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6548 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6549 case ISD::SADDSAT: return C1.sadd_sat(C2);
6550 case ISD::UADDSAT: return C1.uadd_sat(C2);
6551 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6552 case ISD::USUBSAT: return C1.usub_sat(C2);
6553 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6554 case ISD::USHLSAT: return C1.ushl_sat(C2);
6555 case ISD::UDIV:
6556 if (!C2.getBoolValue())
6557 break;
6558 return C1.udiv(C2);
6559 case ISD::UREM:
6560 if (!C2.getBoolValue())
6561 break;
6562 return C1.urem(C2);
6563 case ISD::SDIV:
6564 if (!C2.getBoolValue())
6565 break;
6566 return C1.sdiv(C2);
6567 case ISD::SREM:
6568 if (!C2.getBoolValue())
6569 break;
6570 return C1.srem(C2);
6571 case ISD::AVGFLOORS:
6572 return APIntOps::avgFloorS(C1, C2);
6573 case ISD::AVGFLOORU:
6574 return APIntOps::avgFloorU(C1, C2);
6575 case ISD::AVGCEILS:
6576 return APIntOps::avgCeilS(C1, C2);
6577 case ISD::AVGCEILU:
6578 return APIntOps::avgCeilU(C1, C2);
6579 case ISD::ABDS:
6580 return APIntOps::abds(C1, C2);
6581 case ISD::ABDU:
6582 return APIntOps::abdu(C1, C2);
6583 case ISD::MULHS:
6584 return APIntOps::mulhs(C1, C2);
6585 case ISD::MULHU:
6586 return APIntOps::mulhu(C1, C2);
6587 }
6588 return std::nullopt;
6589}
6590// Handle constant folding with UNDEF.
6591// TODO: Handle more cases.
6592static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6593 bool IsUndef1, const APInt &C2,
6594 bool IsUndef2) {
6595 if (!(IsUndef1 || IsUndef2))
6596 return FoldValue(Opcode, C1, C2);
6597
6598 // Fold and(x, undef) -> 0
6599 // Fold mul(x, undef) -> 0
6600 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6601 return APInt::getZero(C1.getBitWidth());
6602
6603 return std::nullopt;
6604}
6605
6607 const GlobalAddressSDNode *GA,
6608 const SDNode *N2) {
6609 if (GA->getOpcode() != ISD::GlobalAddress)
6610 return SDValue();
6611 if (!TLI->isOffsetFoldingLegal(GA))
6612 return SDValue();
6613 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6614 if (!C2)
6615 return SDValue();
6616 int64_t Offset = C2->getSExtValue();
6617 switch (Opcode) {
6618 case ISD::ADD: break;
6619 case ISD::SUB: Offset = -uint64_t(Offset); break;
6620 default: return SDValue();
6621 }
6622 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6623 GA->getOffset() + uint64_t(Offset));
6624}
6625
6626bool SelectionDAG::isUndef(unsigned Opcode, ArrayRef<SDValue> Ops) {
6627 switch (Opcode) {
6628 case ISD::SDIV:
6629 case ISD::UDIV:
6630 case ISD::SREM:
6631 case ISD::UREM: {
6632 // If a divisor is zero/undef or any element of a divisor vector is
6633 // zero/undef, the whole op is undef.
6634 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6635 SDValue Divisor = Ops[1];
6636 if (Divisor.isUndef() || isNullConstant(Divisor))
6637 return true;
6638
6639 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6640 llvm::any_of(Divisor->op_values(),
6641 [](SDValue V) { return V.isUndef() ||
6642 isNullConstant(V); });
6643 // TODO: Handle signed overflow.
6644 }
6645 // TODO: Handle oversized shifts.
6646 default:
6647 return false;
6648 }
6649}
6650
6652 EVT VT, ArrayRef<SDValue> Ops,
6653 SDNodeFlags Flags) {
6654 // If the opcode is a target-specific ISD node, there's nothing we can
6655 // do here and the operand rules may not line up with the below, so
6656 // bail early.
6657 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6658 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6659 // foldCONCAT_VECTORS in getNode before this is called.
6660 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6661 return SDValue();
6662
6663 unsigned NumOps = Ops.size();
6664 if (NumOps == 0)
6665 return SDValue();
6666
6667 if (isUndef(Opcode, Ops))
6668 return getUNDEF(VT);
6669
6670 // Handle unary special cases.
6671 if (NumOps == 1) {
6672 SDValue N1 = Ops[0];
6673
6674 // Constant fold unary operations with an integer constant operand. Even
6675 // opaque constant will be folded, because the folding of unary operations
6676 // doesn't create new constants with different values. Nevertheless, the
6677 // opaque flag is preserved during folding to prevent future folding with
6678 // other constants.
6679 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6680 const APInt &Val = C->getAPIntValue();
6681 switch (Opcode) {
6682 case ISD::SIGN_EXTEND:
6683 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6684 C->isTargetOpcode(), C->isOpaque());
6685 case ISD::TRUNCATE:
6686 if (C->isOpaque())
6687 break;
6688 [[fallthrough]];
6689 case ISD::ZERO_EXTEND:
6690 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6691 C->isTargetOpcode(), C->isOpaque());
6692 case ISD::ANY_EXTEND:
6693 // Some targets like RISCV prefer to sign extend some types.
6694 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6695 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6696 C->isTargetOpcode(), C->isOpaque());
6697 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6698 C->isTargetOpcode(), C->isOpaque());
6699 case ISD::ABS:
6700 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6701 C->isOpaque());
6702 case ISD::BITREVERSE:
6703 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6704 C->isOpaque());
6705 case ISD::BSWAP:
6706 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6707 C->isOpaque());
6708 case ISD::CTPOP:
6709 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6710 C->isOpaque());
6711 case ISD::CTLZ:
6713 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6714 C->isOpaque());
6715 case ISD::CTTZ:
6717 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6718 C->isOpaque());
6719 case ISD::UINT_TO_FP:
6720 case ISD::SINT_TO_FP: {
6722 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6724 return getConstantFP(FPV, DL, VT);
6725 }
6726 case ISD::FP16_TO_FP:
6727 case ISD::BF16_TO_FP: {
6728 bool Ignored;
6729 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6730 : APFloat::BFloat(),
6731 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6732
6733 // This can return overflow, underflow, or inexact; we don't care.
6734 // FIXME need to be more flexible about rounding mode.
6736 &Ignored);
6737 return getConstantFP(FPV, DL, VT);
6738 }
6739 case ISD::STEP_VECTOR:
6740 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6741 return V;
6742 break;
6743 case ISD::BITCAST:
6744 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6745 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6746 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6747 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6748 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6749 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6750 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6751 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6752 break;
6753 }
6754 }
6755
6756 // Constant fold unary operations with a floating point constant operand.
6757 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6758 APFloat V = C->getValueAPF(); // make copy
6759 switch (Opcode) {
6760 case ISD::FNEG:
6761 V.changeSign();
6762 return getConstantFP(V, DL, VT);
6763 case ISD::FABS:
6764 V.clearSign();
6765 return getConstantFP(V, DL, VT);
6766 case ISD::FCEIL: {
6767 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6768 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6769 return getConstantFP(V, DL, VT);
6770 return SDValue();
6771 }
6772 case ISD::FTRUNC: {
6773 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6774 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6775 return getConstantFP(V, DL, VT);
6776 return SDValue();
6777 }
6778 case ISD::FFLOOR: {
6779 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6780 if (fs == APFloat::opOK || fs == APFloat::opInexact)
6781 return getConstantFP(V, DL, VT);
6782 return SDValue();
6783 }
6784 case ISD::FP_EXTEND: {
6785 bool ignored;
6786 // This can return overflow, underflow, or inexact; we don't care.
6787 // FIXME need to be more flexible about rounding mode.
6788 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
6789 &ignored);
6790 return getConstantFP(V, DL, VT);
6791 }
6792 case ISD::FP_TO_SINT:
6793 case ISD::FP_TO_UINT: {
6794 bool ignored;
6795 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
6796 // FIXME need to be more flexible about rounding mode.
6798 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
6799 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
6800 break;
6801 return getConstant(IntVal, DL, VT);
6802 }
6803 case ISD::FP_TO_FP16:
6804 case ISD::FP_TO_BF16: {
6805 bool Ignored;
6806 // This can return overflow, underflow, or inexact; we don't care.
6807 // FIXME need to be more flexible about rounding mode.
6808 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
6809 : APFloat::BFloat(),
6811 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6812 }
6813 case ISD::BITCAST:
6814 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
6815 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6816 VT);
6817 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
6818 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
6819 VT);
6820 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
6821 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
6822 VT);
6823 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
6824 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
6825 break;
6826 }
6827 }
6828
6829 // Early-out if we failed to constant fold a bitcast.
6830 if (Opcode == ISD::BITCAST)
6831 return SDValue();
6832 }
6833
6834 // Handle binops special cases.
6835 if (NumOps == 2) {
6836 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
6837 return CFP;
6838
6839 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6840 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
6841 if (C1->isOpaque() || C2->isOpaque())
6842 return SDValue();
6843
6844 std::optional<APInt> FoldAttempt =
6845 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
6846 if (!FoldAttempt)
6847 return SDValue();
6848
6849 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
6850 assert((!Folded || !VT.isVector()) &&
6851 "Can't fold vectors ops with scalar operands");
6852 return Folded;
6853 }
6854 }
6855
6856 // fold (add Sym, c) -> Sym+c
6857 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[0]))
6858 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
6859 if (TLI->isCommutativeBinOp(Opcode))
6860 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Ops[1]))
6861 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
6862
6863 // fold (sext_in_reg c1) -> c2
6864 if (Opcode == ISD::SIGN_EXTEND_INREG) {
6865 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
6866
6867 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
6868 unsigned FromBits = EVT.getScalarSizeInBits();
6869 Val <<= Val.getBitWidth() - FromBits;
6870 Val.ashrInPlace(Val.getBitWidth() - FromBits);
6871 return getConstant(Val, DL, ConstantVT);
6872 };
6873
6874 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
6875 const APInt &Val = C1->getAPIntValue();
6876 return SignExtendInReg(Val, VT);
6877 }
6878
6880 SmallVector<SDValue, 8> ScalarOps;
6881 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
6882 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
6883 SDValue Op = Ops[0].getOperand(I);
6884 if (Op.isUndef()) {
6885 ScalarOps.push_back(getUNDEF(OpVT));
6886 continue;
6887 }
6888 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
6889 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
6890 }
6891 return getBuildVector(VT, DL, ScalarOps);
6892 }
6893
6894 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
6895 isa<ConstantSDNode>(Ops[0].getOperand(0)))
6896 return getNode(ISD::SPLAT_VECTOR, DL, VT,
6897 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
6898 Ops[0].getOperand(0).getValueType()));
6899 }
6900 }
6901
6902 // This is for vector folding only from here on.
6903 if (!VT.isVector())
6904 return SDValue();
6905
6906 ElementCount NumElts = VT.getVectorElementCount();
6907
6908 // See if we can fold through any bitcasted integer ops.
6909 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
6910 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
6911 (Ops[0].getOpcode() == ISD::BITCAST ||
6912 Ops[1].getOpcode() == ISD::BITCAST)) {
6913 SDValue N1 = peekThroughBitcasts(Ops[0]);
6914 SDValue N2 = peekThroughBitcasts(Ops[1]);
6915 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
6916 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
6917 if (BV1 && BV2 && N1.getValueType().isInteger() &&
6918 N2.getValueType().isInteger()) {
6919 bool IsLE = getDataLayout().isLittleEndian();
6920 unsigned EltBits = VT.getScalarSizeInBits();
6921 SmallVector<APInt> RawBits1, RawBits2;
6922 BitVector UndefElts1, UndefElts2;
6923 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
6924 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
6925 SmallVector<APInt> RawBits;
6926 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
6927 std::optional<APInt> Fold = FoldValueWithUndef(
6928 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
6929 if (!Fold)
6930 break;
6931 RawBits.push_back(*Fold);
6932 }
6933 if (RawBits.size() == NumElts.getFixedValue()) {
6934 // We have constant folded, but we might need to cast this again back
6935 // to the original (possibly legalized) type.
6936 EVT BVVT, BVEltVT;
6937 if (N1.getValueType() == VT) {
6938 BVVT = N1.getValueType();
6939 BVEltVT = BV1->getOperand(0).getValueType();
6940 } else {
6941 BVVT = N2.getValueType();
6942 BVEltVT = BV2->getOperand(0).getValueType();
6943 }
6944 unsigned BVEltBits = BVEltVT.getSizeInBits();
6945 SmallVector<APInt> DstBits;
6946 BitVector DstUndefs;
6948 DstBits, RawBits, DstUndefs,
6949 BitVector(RawBits.size(), false));
6950 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
6951 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
6952 if (DstUndefs[I])
6953 continue;
6954 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
6955 }
6956 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
6957 }
6958 }
6959 }
6960 }
6961
6962 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
6963 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
6964 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
6965 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
6966 APInt RHSVal;
6967 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
6968 APInt NewStep = Opcode == ISD::MUL
6969 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
6970 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
6971 return getStepVector(DL, VT, NewStep);
6972 }
6973 }
6974
6975 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
6976 return !Op.getValueType().isVector() ||
6977 Op.getValueType().getVectorElementCount() == NumElts;
6978 };
6979
6980 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
6981 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
6982 Op.getOpcode() == ISD::BUILD_VECTOR ||
6983 Op.getOpcode() == ISD::SPLAT_VECTOR;
6984 };
6985
6986 // All operands must be vector types with the same number of elements as
6987 // the result type and must be either UNDEF or a build/splat vector
6988 // or UNDEF scalars.
6989 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
6990 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
6991 return SDValue();
6992
6993 // If we are comparing vectors, then the result needs to be a i1 boolean that
6994 // is then extended back to the legal result type depending on how booleans
6995 // are represented.
6996 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
6997 ISD::NodeType ExtendCode =
6998 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7001
7002 // Find legal integer scalar type for constant promotion and
7003 // ensure that its scalar size is at least as large as source.
7004 EVT LegalSVT = VT.getScalarType();
7005 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7006 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7007 if (LegalSVT.bitsLT(VT.getScalarType()))
7008 return SDValue();
7009 }
7010
7011 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7012 // only have one operand to check. For fixed-length vector types we may have
7013 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7014 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7015
7016 // Constant fold each scalar lane separately.
7017 SmallVector<SDValue, 4> ScalarResults;
7018 for (unsigned I = 0; I != NumVectorElts; I++) {
7019 SmallVector<SDValue, 4> ScalarOps;
7020 for (SDValue Op : Ops) {
7021 EVT InSVT = Op.getValueType().getScalarType();
7022 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7023 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7024 if (Op.isUndef())
7025 ScalarOps.push_back(getUNDEF(InSVT));
7026 else
7027 ScalarOps.push_back(Op);
7028 continue;
7029 }
7030
7031 SDValue ScalarOp =
7032 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7033 EVT ScalarVT = ScalarOp.getValueType();
7034
7035 // Build vector (integer) scalar operands may need implicit
7036 // truncation - do this before constant folding.
7037 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7038 // Don't create illegally-typed nodes unless they're constants or undef
7039 // - if we fail to constant fold we can't guarantee the (dead) nodes
7040 // we're creating will be cleaned up before being visited for
7041 // legalization.
7042 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7043 !isa<ConstantSDNode>(ScalarOp) &&
7044 TLI->getTypeAction(*getContext(), InSVT) !=
7046 return SDValue();
7047 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7048 }
7049
7050 ScalarOps.push_back(ScalarOp);
7051 }
7052
7053 // Constant fold the scalar operands.
7054 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7055
7056 // Scalar folding only succeeded if the result is a constant or UNDEF.
7057 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7058 ScalarResult.getOpcode() != ISD::ConstantFP)
7059 return SDValue();
7060
7061 // Legalize the (integer) scalar constant if necessary. We only do
7062 // this once we know the folding succeeded, since otherwise we would
7063 // get a node with illegal type which has a user.
7064 if (LegalSVT != SVT)
7065 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7066
7067 ScalarResults.push_back(ScalarResult);
7068 }
7069
7070 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7071 : getBuildVector(VT, DL, ScalarResults);
7072 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7073 return V;
7074}
7075
7077 EVT VT, ArrayRef<SDValue> Ops) {
7078 // TODO: Add support for unary/ternary fp opcodes.
7079 if (Ops.size() != 2)
7080 return SDValue();
7081
7082 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7083 // should. That will require dealing with a potentially non-default
7084 // rounding mode, checking the "opStatus" return value from the APFloat
7085 // math calculations, and possibly other variations.
7086 SDValue N1 = Ops[0];
7087 SDValue N2 = Ops[1];
7088 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7089 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7090 if (N1CFP && N2CFP) {
7091 APFloat C1 = N1CFP->getValueAPF(); // make copy
7092 const APFloat &C2 = N2CFP->getValueAPF();
7093 switch (Opcode) {
7094 case ISD::FADD:
7096 return getConstantFP(C1, DL, VT);
7097 case ISD::FSUB:
7099 return getConstantFP(C1, DL, VT);
7100 case ISD::FMUL:
7102 return getConstantFP(C1, DL, VT);
7103 case ISD::FDIV:
7105 return getConstantFP(C1, DL, VT);
7106 case ISD::FREM:
7107 C1.mod(C2);
7108 return getConstantFP(C1, DL, VT);
7109 case ISD::FCOPYSIGN:
7110 C1.copySign(C2);
7111 return getConstantFP(C1, DL, VT);
7112 case ISD::FMINNUM:
7113 return getConstantFP(minnum(C1, C2), DL, VT);
7114 case ISD::FMAXNUM:
7115 return getConstantFP(maxnum(C1, C2), DL, VT);
7116 case ISD::FMINIMUM:
7117 return getConstantFP(minimum(C1, C2), DL, VT);
7118 case ISD::FMAXIMUM:
7119 return getConstantFP(maximum(C1, C2), DL, VT);
7120 case ISD::FMINIMUMNUM:
7121 return getConstantFP(minimumnum(C1, C2), DL, VT);
7122 case ISD::FMAXIMUMNUM:
7123 return getConstantFP(maximumnum(C1, C2), DL, VT);
7124 default: break;
7125 }
7126 }
7127 if (N1CFP && Opcode == ISD::FP_ROUND) {
7128 APFloat C1 = N1CFP->getValueAPF(); // make copy
7129 bool Unused;
7130 // This can return overflow, underflow, or inexact; we don't care.
7131 // FIXME need to be more flexible about rounding mode.
7133 &Unused);
7134 return getConstantFP(C1, DL, VT);
7135 }
7136
7137 switch (Opcode) {
7138 case ISD::FSUB:
7139 // -0.0 - undef --> undef (consistent with "fneg undef")
7140 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7141 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7142 return getUNDEF(VT);
7143 [[fallthrough]];
7144
7145 case ISD::FADD:
7146 case ISD::FMUL:
7147 case ISD::FDIV:
7148 case ISD::FREM:
7149 // If both operands are undef, the result is undef. If 1 operand is undef,
7150 // the result is NaN. This should match the behavior of the IR optimizer.
7151 if (N1.isUndef() && N2.isUndef())
7152 return getUNDEF(VT);
7153 if (N1.isUndef() || N2.isUndef())
7155 }
7156 return SDValue();
7157}
7158
7160 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7161
7162 // There's no need to assert on a byte-aligned pointer. All pointers are at
7163 // least byte aligned.
7164 if (A == Align(1))
7165 return Val;
7166
7167 SDVTList VTs = getVTList(Val.getValueType());
7169 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7170 ID.AddInteger(A.value());
7171
7172 void *IP = nullptr;
7173 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7174 return SDValue(E, 0);
7175
7176 auto *N =
7177 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7178 createOperands(N, {Val});
7179
7180 CSEMap.InsertNode(N, IP);
7181 InsertNode(N);
7182
7183 SDValue V(N, 0);
7184 NewSDValueDbgMsg(V, "Creating new node: ", this);
7185 return V;
7186}
7187
7188SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7189 SDValue N1, SDValue N2) {
7190 SDNodeFlags Flags;
7191 if (Inserter)
7192 Flags = Inserter->getFlags();
7193 return getNode(Opcode, DL, VT, N1, N2, Flags);
7194}
7195
7197 SDValue &N2) const {
7198 if (!TLI->isCommutativeBinOp(Opcode))
7199 return;
7200
7201 // Canonicalize:
7202 // binop(const, nonconst) -> binop(nonconst, const)
7205 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7206 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7207 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7208 std::swap(N1, N2);
7209
7210 // Canonicalize:
7211 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7212 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7214 std::swap(N1, N2);
7215}
7216
7217SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7218 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7220 N2.getOpcode() != ISD::DELETED_NODE &&
7221 "Operand is DELETED_NODE!");
7222
7223 canonicalizeCommutativeBinop(Opcode, N1, N2);
7224
7225 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7226 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7227
7228 // Don't allow undefs in vector splats - we might be returning N2 when folding
7229 // to zero etc.
7230 ConstantSDNode *N2CV =
7231 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7232
7233 switch (Opcode) {
7234 default: break;
7235 case ISD::TokenFactor:
7236 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7237 N2.getValueType() == MVT::Other && "Invalid token factor!");
7238 // Fold trivial token factors.
7239 if (N1.getOpcode() == ISD::EntryToken) return N2;
7240 if (N2.getOpcode() == ISD::EntryToken) return N1;
7241 if (N1 == N2) return N1;
7242 break;
7243 case ISD::BUILD_VECTOR: {
7244 // Attempt to simplify BUILD_VECTOR.
7245 SDValue Ops[] = {N1, N2};
7246 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7247 return V;
7248 break;
7249 }
7250 case ISD::CONCAT_VECTORS: {
7251 SDValue Ops[] = {N1, N2};
7252 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7253 return V;
7254 break;
7255 }
7256 case ISD::AND:
7257 assert(VT.isInteger() && "This operator does not apply to FP types!");
7258 assert(N1.getValueType() == N2.getValueType() &&
7259 N1.getValueType() == VT && "Binary operator types must match!");
7260 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7261 // worth handling here.
7262 if (N2CV && N2CV->isZero())
7263 return N2;
7264 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7265 return N1;
7266 break;
7267 case ISD::OR:
7268 case ISD::XOR:
7269 case ISD::ADD:
7270 case ISD::SUB:
7271 assert(VT.isInteger() && "This operator does not apply to FP types!");
7272 assert(N1.getValueType() == N2.getValueType() &&
7273 N1.getValueType() == VT && "Binary operator types must match!");
7274 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7275 // it's worth handling here.
7276 if (N2CV && N2CV->isZero())
7277 return N1;
7278 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) && VT.isVector() &&
7279 VT.getVectorElementType() == MVT::i1)
7280 return getNode(ISD::XOR, DL, VT, N1, N2);
7281 break;
7282 case ISD::MUL:
7283 assert(VT.isInteger() && "This operator does not apply to FP types!");
7284 assert(N1.getValueType() == N2.getValueType() &&
7285 N1.getValueType() == VT && "Binary operator types must match!");
7286 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7287 return getNode(ISD::AND, DL, VT, N1, N2);
7288 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7289 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7290 const APInt &N2CImm = N2C->getAPIntValue();
7291 return getVScale(DL, VT, MulImm * N2CImm);
7292 }
7293 break;
7294 case ISD::UDIV:
7295 case ISD::UREM:
7296 case ISD::MULHU:
7297 case ISD::MULHS:
7298 case ISD::SDIV:
7299 case ISD::SREM:
7300 case ISD::SADDSAT:
7301 case ISD::SSUBSAT:
7302 case ISD::UADDSAT:
7303 case ISD::USUBSAT:
7304 assert(VT.isInteger() && "This operator does not apply to FP types!");
7305 assert(N1.getValueType() == N2.getValueType() &&
7306 N1.getValueType() == VT && "Binary operator types must match!");
7307 if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
7308 // fold (add_sat x, y) -> (or x, y) for bool types.
7309 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7310 return getNode(ISD::OR, DL, VT, N1, N2);
7311 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7312 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7313 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7314 }
7315 break;
7316 case ISD::SCMP:
7317 case ISD::UCMP:
7318 assert(N1.getValueType() == N2.getValueType() &&
7319 "Types of operands of UCMP/SCMP must match");
7320 assert(N1.getValueType().isVector() == VT.isVector() &&
7321 "Operands and return type of must both be scalars or vectors");
7322 if (VT.isVector())
7325 "Result and operands must have the same number of elements");
7326 break;
7327 case ISD::AVGFLOORS:
7328 case ISD::AVGFLOORU:
7329 case ISD::AVGCEILS:
7330 case ISD::AVGCEILU:
7331 assert(VT.isInteger() && "This operator does not apply to FP types!");
7332 assert(N1.getValueType() == N2.getValueType() &&
7333 N1.getValueType() == VT && "Binary operator types must match!");
7334 break;
7335 case ISD::ABDS:
7336 case ISD::ABDU:
7337 assert(VT.isInteger() && "This operator does not apply to FP types!");
7338 assert(N1.getValueType() == N2.getValueType() &&
7339 N1.getValueType() == VT && "Binary operator types must match!");
7340 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7341 return getNode(ISD::XOR, DL, VT, N1, N2);
7342 break;
7343 case ISD::SMIN:
7344 case ISD::UMAX:
7345 assert(VT.isInteger() && "This operator does not apply to FP types!");
7346 assert(N1.getValueType() == N2.getValueType() &&
7347 N1.getValueType() == VT && "Binary operator types must match!");
7348 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7349 return getNode(ISD::OR, DL, VT, N1, N2);
7350 break;
7351 case ISD::SMAX:
7352 case ISD::UMIN:
7353 assert(VT.isInteger() && "This operator does not apply to FP types!");
7354 assert(N1.getValueType() == N2.getValueType() &&
7355 N1.getValueType() == VT && "Binary operator types must match!");
7356 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
7357 return getNode(ISD::AND, DL, VT, N1, N2);
7358 break;
7359 case ISD::FADD:
7360 case ISD::FSUB:
7361 case ISD::FMUL:
7362 case ISD::FDIV:
7363 case ISD::FREM:
7364 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7365 assert(N1.getValueType() == N2.getValueType() &&
7366 N1.getValueType() == VT && "Binary operator types must match!");
7367 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7368 return V;
7369 break;
7370 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7371 assert(N1.getValueType() == VT &&
7374 "Invalid FCOPYSIGN!");
7375 break;
7376 case ISD::SHL:
7377 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7378 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7379 const APInt &ShiftImm = N2C->getAPIntValue();
7380 return getVScale(DL, VT, MulImm << ShiftImm);
7381 }
7382 [[fallthrough]];
7383 case ISD::SRA:
7384 case ISD::SRL:
7385 if (SDValue V = simplifyShift(N1, N2))
7386 return V;
7387 [[fallthrough]];
7388 case ISD::ROTL:
7389 case ISD::ROTR:
7390 assert(VT == N1.getValueType() &&
7391 "Shift operators return type must be the same as their first arg");
7392 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7393 "Shifts only work on integers");
7394 assert((!VT.isVector() || VT == N2.getValueType()) &&
7395 "Vector shift amounts must be in the same as their first arg");
7396 // Verify that the shift amount VT is big enough to hold valid shift
7397 // amounts. This catches things like trying to shift an i1024 value by an
7398 // i8, which is easy to fall into in generic code that uses
7399 // TLI.getShiftAmount().
7402 "Invalid use of small shift amount with oversized value!");
7403
7404 // Always fold shifts of i1 values so the code generator doesn't need to
7405 // handle them. Since we know the size of the shift has to be less than the
7406 // size of the value, the shift/rotate count is guaranteed to be zero.
7407 if (VT == MVT::i1)
7408 return N1;
7409 if (N2CV && N2CV->isZero())
7410 return N1;
7411 break;
7412 case ISD::FP_ROUND:
7414 VT.bitsLE(N1.getValueType()) && N2C &&
7415 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7416 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7417 if (N1.getValueType() == VT) return N1; // noop conversion.
7418 break;
7419 case ISD::AssertSext:
7420 case ISD::AssertZext: {
7421 EVT EVT = cast<VTSDNode>(N2)->getVT();
7422 assert(VT == N1.getValueType() && "Not an inreg extend!");
7423 assert(VT.isInteger() && EVT.isInteger() &&
7424 "Cannot *_EXTEND_INREG FP types");
7425 assert(!EVT.isVector() &&
7426 "AssertSExt/AssertZExt type should be the vector element type "
7427 "rather than the vector type!");
7428 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7429 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7430 break;
7431 }
7433 EVT EVT = cast<VTSDNode>(N2)->getVT();
7434 assert(VT == N1.getValueType() && "Not an inreg extend!");
7435 assert(VT.isInteger() && EVT.isInteger() &&
7436 "Cannot *_EXTEND_INREG FP types");
7437 assert(EVT.isVector() == VT.isVector() &&
7438 "SIGN_EXTEND_INREG type should be vector iff the operand "
7439 "type is vector!");
7440 assert((!EVT.isVector() ||
7442 "Vector element counts must match in SIGN_EXTEND_INREG");
7443 assert(EVT.bitsLE(VT) && "Not extending!");
7444 if (EVT == VT) return N1; // Not actually extending
7445 break;
7446 }
7448 case ISD::FP_TO_UINT_SAT: {
7449 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7450 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7451 assert(N1.getValueType().isVector() == VT.isVector() &&
7452 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7453 "vector!");
7454 assert((!VT.isVector() || VT.getVectorElementCount() ==
7456 "Vector element counts must match in FP_TO_*INT_SAT");
7457 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7458 "Type to saturate to must be a scalar.");
7459 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7460 "Not extending!");
7461 break;
7462 }
7465 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7466 element type of the vector.");
7467
7468 // Extract from an undefined value or using an undefined index is undefined.
7469 if (N1.isUndef() || N2.isUndef())
7470 return getUNDEF(VT);
7471
7472 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7473 // vectors. For scalable vectors we will provide appropriate support for
7474 // dealing with arbitrary indices.
7475 if (N2C && N1.getValueType().isFixedLengthVector() &&
7476 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7477 return getUNDEF(VT);
7478
7479 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7480 // expanding copies of large vectors from registers. This only works for
7481 // fixed length vectors, since we need to know the exact number of
7482 // elements.
7483 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7485 unsigned Factor =
7488 N1.getOperand(N2C->getZExtValue() / Factor),
7489 getVectorIdxConstant(N2C->getZExtValue() % Factor, DL));
7490 }
7491
7492 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7493 // lowering is expanding large vector constants.
7494 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7495 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7498 "BUILD_VECTOR used for scalable vectors");
7499 unsigned Index =
7500 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7501 SDValue Elt = N1.getOperand(Index);
7502
7503 if (VT != Elt.getValueType())
7504 // If the vector element type is not legal, the BUILD_VECTOR operands
7505 // are promoted and implicitly truncated, and the result implicitly
7506 // extended. Make that explicit here.
7507 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7508
7509 return Elt;
7510 }
7511
7512 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7513 // operations are lowered to scalars.
7514 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7515 // If the indices are the same, return the inserted element else
7516 // if the indices are known different, extract the element from
7517 // the original vector.
7518 SDValue N1Op2 = N1.getOperand(2);
7519 ConstantSDNode *N1Op2C = dyn_cast<ConstantSDNode>(N1Op2);
7520
7521 if (N1Op2C && N2C) {
7522 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7523 if (VT == N1.getOperand(1).getValueType())
7524 return N1.getOperand(1);
7525 if (VT.isFloatingPoint()) {
7527 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7528 }
7529 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7530 }
7531 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7532 }
7533 }
7534
7535 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7536 // when vector types are scalarized and v1iX is legal.
7537 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7538 // Here we are completely ignoring the extract element index (N2),
7539 // which is fine for fixed width vectors, since any index other than 0
7540 // is undefined anyway. However, this cannot be ignored for scalable
7541 // vectors - in theory we could support this, but we don't want to do this
7542 // without a profitability check.
7543 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7545 N1.getValueType().getVectorNumElements() == 1) {
7546 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7547 N1.getOperand(1));
7548 }
7549 break;
7551 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7552 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7553 (N1.getValueType().isInteger() == VT.isInteger()) &&
7554 N1.getValueType() != VT &&
7555 "Wrong types for EXTRACT_ELEMENT!");
7556
7557 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7558 // 64-bit integers into 32-bit parts. Instead of building the extract of
7559 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7560 if (N1.getOpcode() == ISD::BUILD_PAIR)
7561 return N1.getOperand(N2C->getZExtValue());
7562
7563 // EXTRACT_ELEMENT of a constant int is also very common.
7564 if (N1C) {
7565 unsigned ElementSize = VT.getSizeInBits();
7566 unsigned Shift = ElementSize * N2C->getZExtValue();
7567 const APInt &Val = N1C->getAPIntValue();
7568 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7569 }
7570 break;
7572 EVT N1VT = N1.getValueType();
7573 assert(VT.isVector() && N1VT.isVector() &&
7574 "Extract subvector VTs must be vectors!");
7576 "Extract subvector VTs must have the same element type!");
7577 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7578 "Cannot extract a scalable vector from a fixed length vector!");
7579 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7581 "Extract subvector must be from larger vector to smaller vector!");
7582 assert(N2C && "Extract subvector index must be a constant");
7583 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7584 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7585 N1VT.getVectorMinNumElements()) &&
7586 "Extract subvector overflow!");
7587 assert(N2C->getAPIntValue().getBitWidth() ==
7588 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7589 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7590
7591 // Trivial extraction.
7592 if (VT == N1VT)
7593 return N1;
7594
7595 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7596 if (N1.isUndef())
7597 return getUNDEF(VT);
7598
7599 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7600 // the concat have the same type as the extract.
7601 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7602 VT == N1.getOperand(0).getValueType()) {
7603 unsigned Factor = VT.getVectorMinNumElements();
7604 return N1.getOperand(N2C->getZExtValue() / Factor);
7605 }
7606
7607 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7608 // during shuffle legalization.
7609 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7610 VT == N1.getOperand(1).getValueType())
7611 return N1.getOperand(1);
7612 break;
7613 }
7614 }
7615
7616 // Perform trivial constant folding.
7617 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
7618 return SV;
7619
7620 // Canonicalize an UNDEF to the RHS, even over a constant.
7621 if (N1.isUndef()) {
7622 if (TLI->isCommutativeBinOp(Opcode)) {
7623 std::swap(N1, N2);
7624 } else {
7625 switch (Opcode) {
7626 case ISD::SUB:
7627 return getUNDEF(VT); // fold op(undef, arg2) -> undef
7629 case ISD::UDIV:
7630 case ISD::SDIV:
7631 case ISD::UREM:
7632 case ISD::SREM:
7633 case ISD::SSUBSAT:
7634 case ISD::USUBSAT:
7635 return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7636 }
7637 }
7638 }
7639
7640 // Fold a bunch of operators when the RHS is undef.
7641 if (N2.isUndef()) {
7642 switch (Opcode) {
7643 case ISD::XOR:
7644 if (N1.isUndef())
7645 // Handle undef ^ undef -> 0 special case. This is a common
7646 // idiom (misuse).
7647 return getConstant(0, DL, VT);
7648 [[fallthrough]];
7649 case ISD::ADD:
7650 case ISD::SUB:
7651 case ISD::UDIV:
7652 case ISD::SDIV:
7653 case ISD::UREM:
7654 case ISD::SREM:
7655 return getUNDEF(VT); // fold op(arg1, undef) -> undef
7656 case ISD::MUL:
7657 case ISD::AND:
7658 case ISD::SSUBSAT:
7659 case ISD::USUBSAT:
7660 return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7661 case ISD::OR:
7662 case ISD::SADDSAT:
7663 case ISD::UADDSAT:
7664 return getAllOnesConstant(DL, VT);
7665 }
7666 }
7667
7668 // Memoize this node if possible.
7669 SDNode *N;
7670 SDVTList VTs = getVTList(VT);
7671 SDValue Ops[] = {N1, N2};
7672 if (VT != MVT::Glue) {
7674 AddNodeIDNode(ID, Opcode, VTs, Ops);
7675 void *IP = nullptr;
7676 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7677 E->intersectFlagsWith(Flags);
7678 return SDValue(E, 0);
7679 }
7680
7681 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7682 N->setFlags(Flags);
7683 createOperands(N, Ops);
7684 CSEMap.InsertNode(N, IP);
7685 } else {
7686 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7687 createOperands(N, Ops);
7688 }
7689
7690 InsertNode(N);
7691 SDValue V = SDValue(N, 0);
7692 NewSDValueDbgMsg(V, "Creating new node: ", this);
7693 return V;
7694}
7695
7696SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7697 SDValue N1, SDValue N2, SDValue N3) {
7698 SDNodeFlags Flags;
7699 if (Inserter)
7700 Flags = Inserter->getFlags();
7701 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
7702}
7703
7704SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7705 SDValue N1, SDValue N2, SDValue N3,
7706 const SDNodeFlags Flags) {
7708 N2.getOpcode() != ISD::DELETED_NODE &&
7709 N3.getOpcode() != ISD::DELETED_NODE &&
7710 "Operand is DELETED_NODE!");
7711 // Perform various simplifications.
7712 switch (Opcode) {
7713 case ISD::FMA:
7714 case ISD::FMAD: {
7715 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7716 assert(N1.getValueType() == VT && N2.getValueType() == VT &&
7717 N3.getValueType() == VT && "FMA types must match!");
7718 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
7719 ConstantFPSDNode *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
7720 ConstantFPSDNode *N3CFP = dyn_cast<ConstantFPSDNode>(N3);
7721 if (N1CFP && N2CFP && N3CFP) {
7722 APFloat V1 = N1CFP->getValueAPF();
7723 const APFloat &V2 = N2CFP->getValueAPF();
7724 const APFloat &V3 = N3CFP->getValueAPF();
7725 if (Opcode == ISD::FMAD) {
7728 } else
7730 return getConstantFP(V1, DL, VT);
7731 }
7732 break;
7733 }
7734 case ISD::BUILD_VECTOR: {
7735 // Attempt to simplify BUILD_VECTOR.
7736 SDValue Ops[] = {N1, N2, N3};
7737 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7738 return V;
7739 break;
7740 }
7741 case ISD::CONCAT_VECTORS: {
7742 SDValue Ops[] = {N1, N2, N3};
7743 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7744 return V;
7745 break;
7746 }
7747 case ISD::SETCC: {
7748 assert(VT.isInteger() && "SETCC result type must be an integer!");
7749 assert(N1.getValueType() == N2.getValueType() &&
7750 "SETCC operands must have the same type!");
7751 assert(VT.isVector() == N1.getValueType().isVector() &&
7752 "SETCC type should be vector iff the operand type is vector!");
7753 assert((!VT.isVector() || VT.getVectorElementCount() ==
7755 "SETCC vector element counts must match!");
7756 // Use FoldSetCC to simplify SETCC's.
7757 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
7758 return V;
7759 // Vector constant folding.
7760 SDValue Ops[] = {N1, N2, N3};
7761 if (SDValue V = FoldConstantArithmetic(Opcode, DL, VT, Ops)) {
7762 NewSDValueDbgMsg(V, "New node vector constant folding: ", this);
7763 return V;
7764 }
7765 break;
7766 }
7767 case ISD::SELECT:
7768 case ISD::VSELECT:
7769 if (SDValue V = simplifySelect(N1, N2, N3))
7770 return V;
7771 break;
7773 llvm_unreachable("should use getVectorShuffle constructor!");
7774 case ISD::VECTOR_SPLICE: {
7775 if (cast<ConstantSDNode>(N3)->isZero())
7776 return N1;
7777 break;
7778 }
7780 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3);
7781 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
7782 // for scalable vectors where we will generate appropriate code to
7783 // deal with out-of-bounds cases correctly.
7784 if (N3C && N1.getValueType().isFixedLengthVector() &&
7786 return getUNDEF(VT);
7787
7788 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
7789 if (N3.isUndef())
7790 return getUNDEF(VT);
7791
7792 // If the inserted element is an UNDEF, just use the input vector.
7793 if (N2.isUndef())
7794 return N1;
7795
7796 break;
7797 }
7798 case ISD::INSERT_SUBVECTOR: {
7799 // Inserting undef into undef is still undef.
7800 if (N1.isUndef() && N2.isUndef())
7801 return getUNDEF(VT);
7802
7803 EVT N2VT = N2.getValueType();
7804 assert(VT == N1.getValueType() &&
7805 "Dest and insert subvector source types must match!");
7806 assert(VT.isVector() && N2VT.isVector() &&
7807 "Insert subvector VTs must be vectors!");
7809 "Insert subvector VTs must have the same element type!");
7810 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
7811 "Cannot insert a scalable vector into a fixed length vector!");
7812 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7814 "Insert subvector must be from smaller vector to larger vector!");
7815 assert(isa<ConstantSDNode>(N3) &&
7816 "Insert subvector index must be constant");
7817 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
7818 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
7820 "Insert subvector overflow!");
7822 TLI->getVectorIdxTy(getDataLayout()).getFixedSizeInBits() &&
7823 "Constant index for INSERT_SUBVECTOR has an invalid size");
7824
7825 // Trivial insertion.
7826 if (VT == N2VT)
7827 return N2;
7828
7829 // If this is an insert of an extracted vector into an undef vector, we
7830 // can just use the input to the extract.
7831 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7832 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
7833 return N2.getOperand(0);
7834 break;
7835 }
7836 case ISD::BITCAST:
7837 // Fold bit_convert nodes from a type to themselves.
7838 if (N1.getValueType() == VT)
7839 return N1;
7840 break;
7841 case ISD::VP_TRUNCATE:
7842 case ISD::VP_SIGN_EXTEND:
7843 case ISD::VP_ZERO_EXTEND:
7844 // Don't create noop casts.
7845 if (N1.getValueType() == VT)
7846 return N1;
7847 break;
7848 case ISD::VECTOR_COMPRESS: {
7849 [[maybe_unused]] EVT VecVT = N1.getValueType();
7850 [[maybe_unused]] EVT MaskVT = N2.getValueType();
7851 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
7852 assert(VT == VecVT && "Vector and result type don't match.");
7853 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
7854 "All inputs must be vectors.");
7855 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
7857 "Vector and mask must have same number of elements.");
7858
7859 if (N1.isUndef() || N2.isUndef())
7860 return N3;
7861
7862 break;
7863 }
7864 }
7865
7866 // Memoize node if it doesn't produce a glue result.
7867 SDNode *N;
7868 SDVTList VTs = getVTList(VT);
7869 SDValue Ops[] = {N1, N2, N3};
7870 if (VT != MVT::Glue) {
7872 AddNodeIDNode(ID, Opcode, VTs, Ops);
7873 void *IP = nullptr;
7874 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7875 E->intersectFlagsWith(Flags);
7876 return SDValue(E, 0);
7877 }
7878
7879 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7880 N->setFlags(Flags);
7881 createOperands(N, Ops);
7882 CSEMap.InsertNode(N, IP);
7883 } else {
7884 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7885 createOperands(N, Ops);
7886 }
7887
7888 InsertNode(N);
7889 SDValue V = SDValue(N, 0);
7890 NewSDValueDbgMsg(V, "Creating new node: ", this);
7891 return V;
7892}
7893
7894SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7895 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7896 const SDNodeFlags Flags) {
7897 SDValue Ops[] = { N1, N2, N3, N4 };
7898 return getNode(Opcode, DL, VT, Ops, Flags);
7899}
7900
7901SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7902 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
7903 SDNodeFlags Flags;
7904 if (Inserter)
7905 Flags = Inserter->getFlags();
7906 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
7907}
7908
7909SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7910 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7911 SDValue N5, const SDNodeFlags Flags) {
7912 SDValue Ops[] = { N1, N2, N3, N4, N5 };
7913 return getNode(Opcode, DL, VT, Ops, Flags);
7914}
7915
7916SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7917 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
7918 SDValue N5) {
7919 SDNodeFlags Flags;
7920 if (Inserter)
7921 Flags = Inserter->getFlags();
7922 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
7923}
7924
7925/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
7926/// the incoming stack arguments to be loaded from the stack.
7928 SmallVector<SDValue, 8> ArgChains;
7929
7930 // Include the original chain at the beginning of the list. When this is
7931 // used by target LowerCall hooks, this helps legalize find the
7932 // CALLSEQ_BEGIN node.
7933 ArgChains.push_back(Chain);
7934
7935 // Add a chain value for each stack argument.
7936 for (SDNode *U : getEntryNode().getNode()->users())
7937 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
7938 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
7939 if (FI->getIndex() < 0)
7940 ArgChains.push_back(SDValue(L, 1));
7941
7942 // Build a tokenfactor for all the chains.
7943 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
7944}
7945
7946/// getMemsetValue - Vectorized representation of the memset value
7947/// operand.
7949 const SDLoc &dl) {
7950 assert(!Value.isUndef());
7951
7952 unsigned NumBits = VT.getScalarSizeInBits();
7953 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Value)) {
7954 assert(C->getAPIntValue().getBitWidth() == 8);
7955 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
7956 if (VT.isInteger()) {
7957 bool IsOpaque = VT.getSizeInBits() > 64 ||
7958 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
7959 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
7960 }
7961 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
7962 }
7963
7964 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
7965 EVT IntVT = VT.getScalarType();
7966 if (!IntVT.isInteger())
7967 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
7968
7969 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
7970 if (NumBits > 8) {
7971 // Use a multiplication with 0x010101... to extend the input to the
7972 // required length.
7973 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
7974 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
7975 DAG.getConstant(Magic, dl, IntVT));
7976 }
7977
7978 if (VT != Value.getValueType() && !VT.isInteger())
7979 Value = DAG.getBitcast(VT.getScalarType(), Value);
7980 if (VT != Value.getValueType())
7981 Value = DAG.getSplatBuildVector(VT, dl, Value);
7982
7983 return Value;
7984}
7985
7986/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
7987/// used when a memcpy is turned into a memset when the source is a constant
7988/// string ptr.
7990 const TargetLowering &TLI,
7991 const ConstantDataArraySlice &Slice) {
7992 // Handle vector with all elements zero.
7993 if (Slice.Array == nullptr) {
7994 if (VT.isInteger())
7995 return DAG.getConstant(0, dl, VT);
7996 if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f128)
7997 return DAG.getConstantFP(0.0, dl, VT);
7998 if (VT.isVector()) {
7999 unsigned NumElts = VT.getVectorNumElements();
8000 MVT EltVT = (VT.getVectorElementType() == MVT::f32) ? MVT::i32 : MVT::i64;
8001 return DAG.getNode(ISD::BITCAST, dl, VT,
8002 DAG.getConstant(0, dl,
8004 EltVT, NumElts)));
8005 }
8006 llvm_unreachable("Expected type!");
8007 }
8008
8009 assert(!VT.isVector() && "Can't handle vector type here!");
8010 unsigned NumVTBits = VT.getSizeInBits();
8011 unsigned NumVTBytes = NumVTBits / 8;
8012 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8013
8014 APInt Val(NumVTBits, 0);
8015 if (DAG.getDataLayout().isLittleEndian()) {
8016 for (unsigned i = 0; i != NumBytes; ++i)
8017 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8018 } else {
8019 for (unsigned i = 0; i != NumBytes; ++i)
8020 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8021 }
8022
8023 // If the "cost" of materializing the integer immediate is less than the cost
8024 // of a load, then it is cost effective to turn the load into the immediate.
8025 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8026 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8027 return DAG.getConstant(Val, dl, VT);
8028 return SDValue();
8029}
8030
8032 const SDLoc &DL,
8033 const SDNodeFlags Flags) {
8034 EVT VT = Base.getValueType();
8035 SDValue Index;
8036
8037 if (Offset.isScalable())
8038 Index = getVScale(DL, Base.getValueType(),
8039 APInt(Base.getValueSizeInBits().getFixedValue(),
8040 Offset.getKnownMinValue()));
8041 else
8042 Index = getConstant(Offset.getFixedValue(), DL, VT);
8043
8044 return getMemBasePlusOffset(Base, Index, DL, Flags);
8045}
8046
8048 const SDLoc &DL,
8049 const SDNodeFlags Flags) {
8050 assert(Offset.getValueType().isInteger());
8051 EVT BasePtrVT = Ptr.getValueType();
8052 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, Flags);
8053}
8054
8055/// Returns true if memcpy source is constant data.
8057 uint64_t SrcDelta = 0;
8058 GlobalAddressSDNode *G = nullptr;
8059 if (Src.getOpcode() == ISD::GlobalAddress)
8060 G = cast<GlobalAddressSDNode>(Src);
8061 else if (Src.getOpcode() == ISD::ADD &&
8062 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8063 Src.getOperand(1).getOpcode() == ISD::Constant) {
8064 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8065 SrcDelta = Src.getConstantOperandVal(1);
8066 }
8067 if (!G)
8068 return false;
8069
8070 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8071 SrcDelta + G->getOffset());
8072}
8073
8075 SelectionDAG &DAG) {
8076 // On Darwin, -Os means optimize for size without hurting performance, so
8077 // only really optimize for size when -Oz (MinSize) is used.
8079 return MF.getFunction().hasMinSize();
8080 return DAG.shouldOptForSize();
8081}
8082
8084 SmallVector<SDValue, 32> &OutChains, unsigned From,
8085 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8086 SmallVector<SDValue, 16> &OutStoreChains) {
8087 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8088 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8089 SmallVector<SDValue, 16> GluedLoadChains;
8090 for (unsigned i = From; i < To; ++i) {
8091 OutChains.push_back(OutLoadChains[i]);
8092 GluedLoadChains.push_back(OutLoadChains[i]);
8093 }
8094
8095 // Chain for all loads.
8096 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8097 GluedLoadChains);
8098
8099 for (unsigned i = From; i < To; ++i) {
8100 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8101 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8102 ST->getBasePtr(), ST->getMemoryVT(),
8103 ST->getMemOperand());
8104 OutChains.push_back(NewStore);
8105 }
8106}
8107
8109 SDValue Chain, SDValue Dst, SDValue Src,
8110 uint64_t Size, Align Alignment,
8111 bool isVol, bool AlwaysInline,
8112 MachinePointerInfo DstPtrInfo,
8113 MachinePointerInfo SrcPtrInfo,
8114 const AAMDNodes &AAInfo, AAResults *AA) {
8115 // Turn a memcpy of undef to nop.
8116 // FIXME: We need to honor volatile even is Src is undef.
8117 if (Src.isUndef())
8118 return Chain;
8119
8120 // Expand memcpy to a series of load and store ops if the size operand falls
8121 // below a certain threshold.
8122 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8123 // rather than maybe a humongous number of loads and stores.
8124 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8125 const DataLayout &DL = DAG.getDataLayout();
8126 LLVMContext &C = *DAG.getContext();
8127 std::vector<EVT> MemOps;
8128 bool DstAlignCanChange = false;
8130 MachineFrameInfo &MFI = MF.getFrameInfo();
8131 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8132 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8133 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8134 DstAlignCanChange = true;
8135 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8136 if (!SrcAlign || Alignment > *SrcAlign)
8137 SrcAlign = Alignment;
8138 assert(SrcAlign && "SrcAlign must be set");
8140 // If marked as volatile, perform a copy even when marked as constant.
8141 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8142 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8143 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8144 const MemOp Op = isZeroConstant
8145 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8146 /*IsZeroMemset*/ true, isVol)
8147 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8148 *SrcAlign, isVol, CopyFromConstant);
8149 if (!TLI.findOptimalMemOpLowering(
8150 MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8151 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8152 return SDValue();
8153
8154 if (DstAlignCanChange) {
8155 Type *Ty = MemOps[0].getTypeForEVT(C);
8156 Align NewAlign = DL.getABITypeAlign(Ty);
8157
8158 // Don't promote to an alignment that would require dynamic stack
8159 // realignment which may conflict with optimizations such as tail call
8160 // optimization.
8162 if (!TRI->hasStackRealignment(MF))
8163 if (MaybeAlign StackAlign = DL.getStackAlignment())
8164 NewAlign = std::min(NewAlign, *StackAlign);
8165
8166 if (NewAlign > Alignment) {
8167 // Give the stack frame object a larger alignment if needed.
8168 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8169 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8170 Alignment = NewAlign;
8171 }
8172 }
8173
8174 // Prepare AAInfo for loads/stores after lowering this memcpy.
8175 AAMDNodes NewAAInfo = AAInfo;
8176 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8177
8178 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8179 bool isConstant =
8180 AA && SrcVal &&
8181 AA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8182
8183 MachineMemOperand::Flags MMOFlags =
8185 SmallVector<SDValue, 16> OutLoadChains;
8186 SmallVector<SDValue, 16> OutStoreChains;
8187 SmallVector<SDValue, 32> OutChains;
8188 unsigned NumMemOps = MemOps.size();
8189 uint64_t SrcOff = 0, DstOff = 0;
8190 for (unsigned i = 0; i != NumMemOps; ++i) {
8191 EVT VT = MemOps[i];
8192 unsigned VTSize = VT.getSizeInBits() / 8;
8193 SDValue Value, Store;
8194
8195 if (VTSize > Size) {
8196 // Issuing an unaligned load / store pair that overlaps with the previous
8197 // pair. Adjust the offset accordingly.
8198 assert(i == NumMemOps-1 && i != 0);
8199 SrcOff -= VTSize - Size;
8200 DstOff -= VTSize - Size;
8201 }
8202
8203 if (CopyFromConstant &&
8204 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8205 // It's unlikely a store of a vector immediate can be done in a single
8206 // instruction. It would require a load from a constantpool first.
8207 // We only handle zero vectors here.
8208 // FIXME: Handle other cases where store of vector immediate is done in
8209 // a single instruction.
8210 ConstantDataArraySlice SubSlice;
8211 if (SrcOff < Slice.Length) {
8212 SubSlice = Slice;
8213 SubSlice.move(SrcOff);
8214 } else {
8215 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8216 SubSlice.Array = nullptr;
8217 SubSlice.Offset = 0;
8218 SubSlice.Length = VTSize;
8219 }
8220 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8221 if (Value.getNode()) {
8222 Store = DAG.getStore(
8223 Chain, dl, Value,
8224 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8225 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8226 OutChains.push_back(Store);
8227 }
8228 }
8229
8230 if (!Store.getNode()) {
8231 // The type might not be legal for the target. This should only happen
8232 // if the type is smaller than a legal type, as on PPC, so the right
8233 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8234 // to Load/Store if NVT==VT.
8235 // FIXME does the case above also need this?
8236 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8237 assert(NVT.bitsGE(VT));
8238
8239 bool isDereferenceable =
8240 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8241 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8242 if (isDereferenceable)
8244 if (isConstant)
8245 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8246
8247 Value = DAG.getExtLoad(
8248 ISD::EXTLOAD, dl, NVT, Chain,
8249 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8250 SrcPtrInfo.getWithOffset(SrcOff), VT,
8251 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8252 OutLoadChains.push_back(Value.getValue(1));
8253
8254 Store = DAG.getTruncStore(
8255 Chain, dl, Value,
8256 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8257 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8258 OutStoreChains.push_back(Store);
8259 }
8260 SrcOff += VTSize;
8261 DstOff += VTSize;
8262 Size -= VTSize;
8263 }
8264
8265 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8267 unsigned NumLdStInMemcpy = OutStoreChains.size();
8268
8269 if (NumLdStInMemcpy) {
8270 // It may be that memcpy might be converted to memset if it's memcpy
8271 // of constants. In such a case, we won't have loads and stores, but
8272 // just stores. In the absence of loads, there is nothing to gang up.
8273 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8274 // If target does not care, just leave as it.
8275 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8276 OutChains.push_back(OutLoadChains[i]);
8277 OutChains.push_back(OutStoreChains[i]);
8278 }
8279 } else {
8280 // Ld/St less than/equal limit set by target.
8281 if (NumLdStInMemcpy <= GluedLdStLimit) {
8282 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8283 NumLdStInMemcpy, OutLoadChains,
8284 OutStoreChains);
8285 } else {
8286 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8287 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8288 unsigned GlueIter = 0;
8289
8290 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8291 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8292 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8293
8294 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8295 OutLoadChains, OutStoreChains);
8296 GlueIter += GluedLdStLimit;
8297 }
8298
8299 // Residual ld/st.
8300 if (RemainingLdStInMemcpy) {
8301 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8302 RemainingLdStInMemcpy, OutLoadChains,
8303 OutStoreChains);
8304 }
8305 }
8306 }
8307 }
8308 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8309}
8310
8312 SDValue Chain, SDValue Dst, SDValue Src,
8313 uint64_t Size, Align Alignment,
8314 bool isVol, bool AlwaysInline,
8315 MachinePointerInfo DstPtrInfo,
8316 MachinePointerInfo SrcPtrInfo,
8317 const AAMDNodes &AAInfo) {
8318 // Turn a memmove of undef to nop.
8319 // FIXME: We need to honor volatile even is Src is undef.
8320 if (Src.isUndef())
8321 return Chain;
8322
8323 // Expand memmove to a series of load and store ops if the size operand falls
8324 // below a certain threshold.
8325 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8326 const DataLayout &DL = DAG.getDataLayout();
8327 LLVMContext &C = *DAG.getContext();
8328 std::vector<EVT> MemOps;
8329 bool DstAlignCanChange = false;
8331 MachineFrameInfo &MFI = MF.getFrameInfo();
8332 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8333 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8334 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8335 DstAlignCanChange = true;
8336 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8337 if (!SrcAlign || Alignment > *SrcAlign)
8338 SrcAlign = Alignment;
8339 assert(SrcAlign && "SrcAlign must be set");
8340 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8341 if (!TLI.findOptimalMemOpLowering(
8342 MemOps, Limit,
8343 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8344 /*IsVolatile*/ true),
8345 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8346 MF.getFunction().getAttributes()))
8347 return SDValue();
8348
8349 if (DstAlignCanChange) {
8350 Type *Ty = MemOps[0].getTypeForEVT(C);
8351 Align NewAlign = DL.getABITypeAlign(Ty);
8352
8353 // Don't promote to an alignment that would require dynamic stack
8354 // realignment which may conflict with optimizations such as tail call
8355 // optimization.
8357 if (!TRI->hasStackRealignment(MF))
8358 if (MaybeAlign StackAlign = DL.getStackAlignment())
8359 NewAlign = std::min(NewAlign, *StackAlign);
8360
8361 if (NewAlign > Alignment) {
8362 // Give the stack frame object a larger alignment if needed.
8363 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8364 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8365 Alignment = NewAlign;
8366 }
8367 }
8368
8369 // Prepare AAInfo for loads/stores after lowering this memmove.
8370 AAMDNodes NewAAInfo = AAInfo;
8371 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8372
8373 MachineMemOperand::Flags MMOFlags =
8375 uint64_t SrcOff = 0, DstOff = 0;
8376 SmallVector<SDValue, 8> LoadValues;
8377 SmallVector<SDValue, 8> LoadChains;
8378 SmallVector<SDValue, 8> OutChains;
8379 unsigned NumMemOps = MemOps.size();
8380 for (unsigned i = 0; i < NumMemOps; i++) {
8381 EVT VT = MemOps[i];
8382 unsigned VTSize = VT.getSizeInBits() / 8;
8383 SDValue Value;
8384
8385 bool isDereferenceable =
8386 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8387 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8388 if (isDereferenceable)
8390
8391 Value = DAG.getLoad(
8392 VT, dl, Chain,
8393 DAG.getMemBasePlusOffset(Src, TypeSize::getFixed(SrcOff), dl),
8394 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8395 LoadValues.push_back(Value);
8396 LoadChains.push_back(Value.getValue(1));
8397 SrcOff += VTSize;
8398 }
8399 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8400 OutChains.clear();
8401 for (unsigned i = 0; i < NumMemOps; i++) {
8402 EVT VT = MemOps[i];
8403 unsigned VTSize = VT.getSizeInBits() / 8;
8404 SDValue Store;
8405
8406 Store = DAG.getStore(
8407 Chain, dl, LoadValues[i],
8408 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8409 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8410 OutChains.push_back(Store);
8411 DstOff += VTSize;
8412 }
8413
8414 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8415}
8416
8417/// Lower the call to 'memset' intrinsic function into a series of store
8418/// operations.
8419///
8420/// \param DAG Selection DAG where lowered code is placed.
8421/// \param dl Link to corresponding IR location.
8422/// \param Chain Control flow dependency.
8423/// \param Dst Pointer to destination memory location.
8424/// \param Src Value of byte to write into the memory.
8425/// \param Size Number of bytes to write.
8426/// \param Alignment Alignment of the destination in bytes.
8427/// \param isVol True if destination is volatile.
8428/// \param AlwaysInline Makes sure no function call is generated.
8429/// \param DstPtrInfo IR information on the memory pointer.
8430/// \returns New head in the control flow, if lowering was successful, empty
8431/// SDValue otherwise.
8432///
8433/// The function tries to replace 'llvm.memset' intrinsic with several store
8434/// operations and value calculation code. This is usually profitable for small
8435/// memory size or when the semantic requires inlining.
8437 SDValue Chain, SDValue Dst, SDValue Src,
8438 uint64_t Size, Align Alignment, bool isVol,
8439 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8440 const AAMDNodes &AAInfo) {
8441 // Turn a memset of undef to nop.
8442 // FIXME: We need to honor volatile even is Src is undef.
8443 if (Src.isUndef())
8444 return Chain;
8445
8446 // Expand memset to a series of load/store ops if the size operand
8447 // falls below a certain threshold.
8448 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8449 std::vector<EVT> MemOps;
8450 bool DstAlignCanChange = false;
8452 MachineFrameInfo &MFI = MF.getFrameInfo();
8453 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8454 FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Dst);
8455 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8456 DstAlignCanChange = true;
8457 bool IsZeroVal = isNullConstant(Src);
8458 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8459
8460 if (!TLI.findOptimalMemOpLowering(
8461 MemOps, Limit,
8462 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8463 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8464 return SDValue();
8465
8466 if (DstAlignCanChange) {
8467 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8468 const DataLayout &DL = DAG.getDataLayout();
8469 Align NewAlign = DL.getABITypeAlign(Ty);
8470
8471 // Don't promote to an alignment that would require dynamic stack
8472 // realignment which may conflict with optimizations such as tail call
8473 // optimization.
8475 if (!TRI->hasStackRealignment(MF))
8476 if (MaybeAlign StackAlign = DL.getStackAlignment())
8477 NewAlign = std::min(NewAlign, *StackAlign);
8478
8479 if (NewAlign > Alignment) {
8480 // Give the stack frame object a larger alignment if needed.
8481 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8482 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8483 Alignment = NewAlign;
8484 }
8485 }
8486
8487 SmallVector<SDValue, 8> OutChains;
8488 uint64_t DstOff = 0;
8489 unsigned NumMemOps = MemOps.size();
8490
8491 // Find the largest store and generate the bit pattern for it.
8492 EVT LargestVT = MemOps[0];
8493 for (unsigned i = 1; i < NumMemOps; i++)
8494 if (MemOps[i].bitsGT(LargestVT))
8495 LargestVT = MemOps[i];
8496 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8497
8498 // Prepare AAInfo for loads/stores after lowering this memset.
8499 AAMDNodes NewAAInfo = AAInfo;
8500 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8501
8502 for (unsigned i = 0; i < NumMemOps; i++) {
8503 EVT VT = MemOps[i];
8504 unsigned VTSize = VT.getSizeInBits() / 8;
8505 if (VTSize > Size) {
8506 // Issuing an unaligned load / store pair that overlaps with the previous
8507 // pair. Adjust the offset accordingly.
8508 assert(i == NumMemOps-1 && i != 0);
8509 DstOff -= VTSize - Size;
8510 }
8511
8512 // If this store is smaller than the largest store see whether we can get
8513 // the smaller value for free with a truncate or extract vector element and
8514 // then store.
8515 SDValue Value = MemSetValue;
8516 if (VT.bitsLT(LargestVT)) {
8517 unsigned Index;
8518 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8519 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8520 if (!LargestVT.isVector() && !VT.isVector() &&
8521 TLI.isTruncateFree(LargestVT, VT))
8522 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8523 else if (LargestVT.isVector() && !VT.isVector() &&
8525 LargestVT.getTypeForEVT(*DAG.getContext()),
8526 VT.getSizeInBits(), Index) &&
8527 TLI.isTypeLegal(SVT) &&
8528 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8529 // Target which can combine store(extractelement VectorTy, Idx) can get
8530 // the smaller value for free.
8531 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8532 Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, TailValue,
8533 DAG.getVectorIdxConstant(Index, dl));
8534 } else
8535 Value = getMemsetValue(Src, VT, DAG, dl);
8536 }
8537 assert(Value.getValueType() == VT && "Value with wrong type.");
8538 SDValue Store = DAG.getStore(
8539 Chain, dl, Value,
8540 DAG.getMemBasePlusOffset(Dst, TypeSize::getFixed(DstOff), dl),
8541 DstPtrInfo.getWithOffset(DstOff), Alignment,
8543 NewAAInfo);
8544 OutChains.push_back(Store);
8545 DstOff += VT.getSizeInBits() / 8;
8546 Size -= VTSize;
8547 }
8548
8549 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8550}
8551
8553 unsigned AS) {
8554 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
8555 // pointer operands can be losslessly bitcasted to pointers of address space 0
8556 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
8557 report_fatal_error("cannot lower memory intrinsic in address space " +
8558 Twine(AS));
8559 }
8560}
8561
8563 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
8564 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
8565 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
8566 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, AAResults *AA) {
8567 // Check to see if we should lower the memcpy to loads and stores first.
8568 // For cases within the target-specified limits, this is the best choice.
8569 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8570 if (ConstantSize) {
8571 // Memcpy with size zero? Just return the original chain.
8572 if (ConstantSize->isZero())
8573 return Chain;
8574
8576 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8577 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8578 if (Result.getNode())
8579 return Result;
8580 }
8581
8582 // Then check to see if we should lower the memcpy with target-specific
8583 // code. If the target chooses to do this, this is the next best.
8584 if (TSI) {
8585 SDValue Result = TSI->EmitTargetCodeForMemcpy(
8586 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
8587 DstPtrInfo, SrcPtrInfo);
8588 if (Result.getNode())
8589 return Result;
8590 }
8591
8592 // If we really need inline code and the target declined to provide it,
8593 // use a (potentially long) sequence of loads and stores.
8594 if (AlwaysInline) {
8595 assert(ConstantSize && "AlwaysInline requires a constant size!");
8597 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8598 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, AA);
8599 }
8600
8603
8604 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
8605 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
8606 // respect volatile, so they may do things like read or write memory
8607 // beyond the given memory regions. But fixing this isn't easy, and most
8608 // people don't care.
8609
8610 // Emit a library call.
8613 Entry.Ty = PointerType::getUnqual(*getContext());
8614 Entry.Node = Dst; Args.push_back(Entry);
8615 Entry.Node = Src; Args.push_back(Entry);
8616
8617 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8618 Entry.Node = Size; Args.push_back(Entry);
8619 // FIXME: pass in SDLoc
8621 bool IsTailCall = false;
8622 if (OverrideTailCall.has_value()) {
8623 IsTailCall = *OverrideTailCall;
8624 } else {
8625 bool LowersToMemcpy =
8626 TLI->getLibcallName(RTLIB::MEMCPY) == StringRef("memcpy");
8627 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8628 IsTailCall = CI && CI->isTailCall() &&
8630 ReturnsFirstArg && LowersToMemcpy);
8631 }
8632
8633 CLI.setDebugLoc(dl)
8634 .setChain(Chain)
8635 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMCPY),
8636 Dst.getValueType().getTypeForEVT(*getContext()),
8637 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMCPY),
8638 TLI->getPointerTy(getDataLayout())),
8639 std::move(Args))
8641 .setTailCall(IsTailCall);
8642
8643 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8644 return CallResult.second;
8645}
8646
8648 SDValue Dst, SDValue Src, SDValue Size,
8649 Type *SizeTy, unsigned ElemSz,
8650 bool isTailCall,
8651 MachinePointerInfo DstPtrInfo,
8652 MachinePointerInfo SrcPtrInfo) {
8653 // Emit a library call.
8656 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8657 Entry.Node = Dst;
8658 Args.push_back(Entry);
8659
8660 Entry.Node = Src;
8661 Args.push_back(Entry);
8662
8663 Entry.Ty = SizeTy;
8664 Entry.Node = Size;
8665 Args.push_back(Entry);
8666
8667 RTLIB::Libcall LibraryCall =
8669 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8670 report_fatal_error("Unsupported element size");
8671
8673 CLI.setDebugLoc(dl)
8674 .setChain(Chain)
8675 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8677 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8678 TLI->getPointerTy(getDataLayout())),
8679 std::move(Args))
8681 .setTailCall(isTailCall);
8682
8683 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8684 return CallResult.second;
8685}
8686
8688 SDValue Src, SDValue Size, Align Alignment,
8689 bool isVol, const CallInst *CI,
8690 std::optional<bool> OverrideTailCall,
8691 MachinePointerInfo DstPtrInfo,
8692 MachinePointerInfo SrcPtrInfo,
8693 const AAMDNodes &AAInfo, AAResults *AA) {
8694 // Check to see if we should lower the memmove to loads and stores first.
8695 // For cases within the target-specified limits, this is the best choice.
8696 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8697 if (ConstantSize) {
8698 // Memmove with size zero? Just return the original chain.
8699 if (ConstantSize->isZero())
8700 return Chain;
8701
8703 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
8704 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
8705 if (Result.getNode())
8706 return Result;
8707 }
8708
8709 // Then check to see if we should lower the memmove with target-specific
8710 // code. If the target chooses to do this, this is the next best.
8711 if (TSI) {
8712 SDValue Result =
8713 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
8714 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
8715 if (Result.getNode())
8716 return Result;
8717 }
8718
8721
8722 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
8723 // not be safe. See memcpy above for more details.
8724
8725 // Emit a library call.
8728 Entry.Ty = PointerType::getUnqual(*getContext());
8729 Entry.Node = Dst; Args.push_back(Entry);
8730 Entry.Node = Src; Args.push_back(Entry);
8731
8732 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8733 Entry.Node = Size; Args.push_back(Entry);
8734 // FIXME: pass in SDLoc
8736
8737 bool IsTailCall = false;
8738 if (OverrideTailCall.has_value()) {
8739 IsTailCall = *OverrideTailCall;
8740 } else {
8741 bool LowersToMemmove =
8742 TLI->getLibcallName(RTLIB::MEMMOVE) == StringRef("memmove");
8743 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI);
8744 IsTailCall = CI && CI->isTailCall() &&
8746 ReturnsFirstArg && LowersToMemmove);
8747 }
8748
8749 CLI.setDebugLoc(dl)
8750 .setChain(Chain)
8751 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMMOVE),
8752 Dst.getValueType().getTypeForEVT(*getContext()),
8753 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMMOVE),
8754 TLI->getPointerTy(getDataLayout())),
8755 std::move(Args))
8757 .setTailCall(IsTailCall);
8758
8759 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
8760 return CallResult.second;
8761}
8762
8764 SDValue Dst, SDValue Src, SDValue Size,
8765 Type *SizeTy, unsigned ElemSz,
8766 bool isTailCall,
8767 MachinePointerInfo DstPtrInfo,
8768 MachinePointerInfo SrcPtrInfo) {
8769 // Emit a library call.
8772 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8773 Entry.Node = Dst;
8774 Args.push_back(Entry);
8775
8776 Entry.Node = Src;
8777 Args.push_back(Entry);
8778
8779 Entry.Ty = SizeTy;
8780 Entry.Node = Size;
8781 Args.push_back(Entry);
8782
8783 RTLIB::Libcall LibraryCall =
8785 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8786 report_fatal_error("Unsupported element size");
8787
8789 CLI.setDebugLoc(dl)
8790 .setChain(Chain)
8791 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8793 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8794 TLI->getPointerTy(getDataLayout())),
8795 std::move(Args))
8797 .setTailCall(isTailCall);
8798
8799 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8800 return CallResult.second;
8801}
8802
8804 SDValue Src, SDValue Size, Align Alignment,
8805 bool isVol, bool AlwaysInline,
8806 const CallInst *CI,
8807 MachinePointerInfo DstPtrInfo,
8808 const AAMDNodes &AAInfo) {
8809 // Check to see if we should lower the memset to stores first.
8810 // For cases within the target-specified limits, this is the best choice.
8811 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
8812 if (ConstantSize) {
8813 // Memset with size zero? Just return the original chain.
8814 if (ConstantSize->isZero())
8815 return Chain;
8816
8817 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8818 ConstantSize->getZExtValue(), Alignment,
8819 isVol, false, DstPtrInfo, AAInfo);
8820
8821 if (Result.getNode())
8822 return Result;
8823 }
8824
8825 // Then check to see if we should lower the memset with target-specific
8826 // code. If the target chooses to do this, this is the next best.
8827 if (TSI) {
8828 SDValue Result = TSI->EmitTargetCodeForMemset(
8829 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
8830 if (Result.getNode())
8831 return Result;
8832 }
8833
8834 // If we really need inline code and the target declined to provide it,
8835 // use a (potentially long) sequence of loads and stores.
8836 if (AlwaysInline) {
8837 assert(ConstantSize && "AlwaysInline requires a constant size!");
8838 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
8839 ConstantSize->getZExtValue(), Alignment,
8840 isVol, true, DstPtrInfo, AAInfo);
8841 assert(Result &&
8842 "getMemsetStores must return a valid sequence when AlwaysInline");
8843 return Result;
8844 }
8845
8847
8848 // Emit a library call.
8849 auto &Ctx = *getContext();
8850 const auto& DL = getDataLayout();
8851
8853 // FIXME: pass in SDLoc
8854 CLI.setDebugLoc(dl).setChain(Chain);
8855
8856 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
8857
8858 // Helper function to create an Entry from Node and Type.
8859 const auto CreateEntry = [](SDValue Node, Type *Ty) {
8861 Entry.Node = Node;
8862 Entry.Ty = Ty;
8863 return Entry;
8864 };
8865
8866 bool UseBZero = isNullConstant(Src) && BzeroName;
8867 // If zeroing out and bzero is present, use it.
8868 if (UseBZero) {
8870 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8871 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8872 CLI.setLibCallee(
8873 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
8874 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
8875 } else {
8877 Args.push_back(CreateEntry(Dst, PointerType::getUnqual(Ctx)));
8878 Args.push_back(CreateEntry(Src, Src.getValueType().getTypeForEVT(Ctx)));
8879 Args.push_back(CreateEntry(Size, DL.getIntPtrType(Ctx)));
8880 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
8881 Dst.getValueType().getTypeForEVT(Ctx),
8882 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
8883 TLI->getPointerTy(DL)),
8884 std::move(Args));
8885 }
8886 bool LowersToMemset =
8887 TLI->getLibcallName(RTLIB::MEMSET) == StringRef("memset");
8888 // If we're going to use bzero, make sure not to tail call unless the
8889 // subsequent return doesn't need a value, as bzero doesn't return the first
8890 // arg unlike memset.
8891 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
8892 bool IsTailCall =
8893 CI && CI->isTailCall() &&
8894 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
8895 CLI.setDiscardResult().setTailCall(IsTailCall);
8896
8897 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8898 return CallResult.second;
8899}
8900
8903 Type *SizeTy, unsigned ElemSz,
8904 bool isTailCall,
8905 MachinePointerInfo DstPtrInfo) {
8906 // Emit a library call.
8909 Entry.Ty = getDataLayout().getIntPtrType(*getContext());
8910 Entry.Node = Dst;
8911 Args.push_back(Entry);
8912
8913 Entry.Ty = Type::getInt8Ty(*getContext());
8914 Entry.Node = Value;
8915 Args.push_back(Entry);
8916
8917 Entry.Ty = SizeTy;
8918 Entry.Node = Size;
8919 Args.push_back(Entry);
8920
8921 RTLIB::Libcall LibraryCall =
8923 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
8924 report_fatal_error("Unsupported element size");
8925
8927 CLI.setDebugLoc(dl)
8928 .setChain(Chain)
8929 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
8931 getExternalSymbol(TLI->getLibcallName(LibraryCall),
8932 TLI->getPointerTy(getDataLayout())),
8933 std::move(Args))
8935 .setTailCall(isTailCall);
8936
8937 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
8938 return CallResult.second;
8939}
8940
8941SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8942 SDVTList VTList, ArrayRef<SDValue> Ops,
8943 MachineMemOperand *MMO) {
8945 ID.AddInteger(MemVT.getRawBits());
8946 AddNodeIDNode(ID, Opcode, VTList, Ops);
8947 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
8948 ID.AddInteger(MMO->getFlags());
8949 void* IP = nullptr;
8950 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
8951 cast<AtomicSDNode>(E)->refineAlignment(MMO);
8952 return SDValue(E, 0);
8953 }
8954
8955 auto *N = newSDNode<AtomicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
8956 VTList, MemVT, MMO);
8957 createOperands(N, Ops);
8958
8959 CSEMap.InsertNode(N, IP);
8960 InsertNode(N);
8961 return SDValue(N, 0);
8962}
8963
8965 EVT MemVT, SDVTList VTs, SDValue Chain,
8966 SDValue Ptr, SDValue Cmp, SDValue Swp,
8967 MachineMemOperand *MMO) {
8968 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
8970 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
8971
8972 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
8973 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
8974}
8975
8976SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
8977 SDValue Chain, SDValue Ptr, SDValue Val,
8978 MachineMemOperand *MMO) {
8979 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
8980 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
8981 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
8982 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
8983 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
8984 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
8985 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
8986 Opcode == ISD::ATOMIC_LOAD_FMIN ||
8987 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
8988 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
8989 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
8990 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
8991 Opcode == ISD::ATOMIC_STORE) &&
8992 "Invalid Atomic Op");
8993
8994 EVT VT = Val.getValueType();
8995
8996 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
8997 getVTList(VT, MVT::Other);
8998 SDValue Ops[] = {Chain, Ptr, Val};
8999 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9000}
9001
9002SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9003 EVT VT, SDValue Chain, SDValue Ptr,
9004 MachineMemOperand *MMO) {
9005 assert(Opcode == ISD::ATOMIC_LOAD && "Invalid Atomic Op");
9006
9007 SDVTList VTs = getVTList(VT, MVT::Other);
9008 SDValue Ops[] = {Chain, Ptr};
9009 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9010}
9011
9012/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9014 if (Ops.size() == 1)
9015 return Ops[0];
9016
9018 VTs.reserve(Ops.size());
9019 for (const SDValue &Op : Ops)
9020 VTs.push_back(Op.getValueType());
9021 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9022}
9023
9025 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9026 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9028 const AAMDNodes &AAInfo) {
9029 if (Size.hasValue() && !Size.getValue())
9031
9033 MachineMemOperand *MMO =
9034 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9035
9036 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9037}
9038
9040 SDVTList VTList,
9041 ArrayRef<SDValue> Ops, EVT MemVT,
9042 MachineMemOperand *MMO) {
9043 assert((Opcode == ISD::INTRINSIC_VOID ||
9044 Opcode == ISD::INTRINSIC_W_CHAIN ||
9045 Opcode == ISD::PREFETCH ||
9046 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9047 (int)Opcode >= ISD::FIRST_TARGET_MEMORY_OPCODE)) &&
9048 "Opcode is not a memory-accessing opcode!");
9049
9050 // Memoize the node unless it returns a glue result.
9052 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9054 AddNodeIDNode(ID, Opcode, VTList, Ops);
9055 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9056 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9057 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9058 ID.AddInteger(MMO->getFlags());
9059 ID.AddInteger(MemVT.getRawBits());
9060 void *IP = nullptr;
9061 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9062 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9063 return SDValue(E, 0);
9064 }
9065
9066 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9067 VTList, MemVT, MMO);
9068 createOperands(N, Ops);
9069
9070 CSEMap.InsertNode(N, IP);
9071 } else {
9072 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9073 VTList, MemVT, MMO);
9074 createOperands(N, Ops);
9075 }
9076 InsertNode(N);
9077 SDValue V(N, 0);
9078 NewSDValueDbgMsg(V, "Creating new node: ", this);
9079 return V;
9080}
9081
9083 SDValue Chain, int FrameIndex,
9084 int64_t Size, int64_t Offset) {
9085 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9086 const auto VTs = getVTList(MVT::Other);
9087 SDValue Ops[2] = {
9088 Chain,
9089 getFrameIndex(FrameIndex,
9090 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9091 true)};
9092
9094 AddNodeIDNode(ID, Opcode, VTs, Ops);
9095 ID.AddInteger(FrameIndex);
9096 ID.AddInteger(Size);
9097 ID.AddInteger(Offset);
9098 void *IP = nullptr;
9099 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9100 return SDValue(E, 0);
9101
9102 LifetimeSDNode *N = newSDNode<LifetimeSDNode>(
9103 Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs, Size, Offset);
9104 createOperands(N, Ops);
9105 CSEMap.InsertNode(N, IP);
9106 InsertNode(N);
9107 SDValue V(N, 0);
9108 NewSDValueDbgMsg(V, "Creating new node: ", this);
9109 return V;
9110}
9111
9113 uint64_t Guid, uint64_t Index,
9114 uint32_t Attr) {
9115 const unsigned Opcode = ISD::PSEUDO_PROBE;
9116 const auto VTs = getVTList(MVT::Other);
9117 SDValue Ops[] = {Chain};
9119 AddNodeIDNode(ID, Opcode, VTs, Ops);
9120 ID.AddInteger(Guid);
9121 ID.AddInteger(Index);
9122 void *IP = nullptr;
9123 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9124 return SDValue(E, 0);
9125
9126 auto *N = newSDNode<PseudoProbeSDNode>(
9127 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9128 createOperands(N, Ops);
9129 CSEMap.InsertNode(N, IP);
9130 InsertNode(N);
9131 SDValue V(N, 0);
9132 NewSDValueDbgMsg(V, "Creating new node: ", this);
9133 return V;
9134}
9135
9136/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9137/// MachinePointerInfo record from it. This is particularly useful because the
9138/// code generator has many cases where it doesn't bother passing in a
9139/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9141 SelectionDAG &DAG, SDValue Ptr,
9142 int64_t Offset = 0) {
9143 // If this is FI+Offset, we can model it.
9144 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9146 FI->getIndex(), Offset);
9147
9148 // If this is (FI+Offset1)+Offset2, we can model it.
9149 if (Ptr.getOpcode() != ISD::ADD ||
9150 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
9151 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
9152 return Info;
9153
9154 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9156 DAG.getMachineFunction(), FI,
9157 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9158}
9159
9160/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9161/// MachinePointerInfo record from it. This is particularly useful because the
9162/// code generator has many cases where it doesn't bother passing in a
9163/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9165 SelectionDAG &DAG, SDValue Ptr,
9166 SDValue OffsetOp) {
9167 // If the 'Offset' value isn't a constant, we can't handle this.
9168 if (ConstantSDNode *OffsetNode = dyn_cast<ConstantSDNode>(OffsetOp))
9169 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9170 if (OffsetOp.isUndef())
9171 return InferPointerInfo(Info, DAG, Ptr);
9172 return Info;
9173}
9174
9176 EVT VT, const SDLoc &dl, SDValue Chain,
9178 MachinePointerInfo PtrInfo, EVT MemVT,
9179 Align Alignment,
9180 MachineMemOperand::Flags MMOFlags,
9181 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9182 assert(Chain.getValueType() == MVT::Other &&
9183 "Invalid chain type");
9184
9185 MMOFlags |= MachineMemOperand::MOLoad;
9186 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9187 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9188 // clients.
9189 if (PtrInfo.V.isNull())
9190 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9191
9194 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9195 Alignment, AAInfo, Ranges);
9196 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9197}
9198
9200 EVT VT, const SDLoc &dl, SDValue Chain,
9201 SDValue Ptr, SDValue Offset, EVT MemVT,
9202 MachineMemOperand *MMO) {
9203 if (VT == MemVT) {
9204 ExtType = ISD::NON_EXTLOAD;
9205 } else if (ExtType == ISD::NON_EXTLOAD) {
9206 assert(VT == MemVT && "Non-extending load from different memory type!");
9207 } else {
9208 // Extending load.
9209 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9210 "Should only be an extending load, not truncating!");
9211 assert(VT.isInteger() == MemVT.isInteger() &&
9212 "Cannot convert from FP to Int or Int -> FP!");
9213 assert(VT.isVector() == MemVT.isVector() &&
9214 "Cannot use an ext load to convert to or from a vector!");
9215 assert((!VT.isVector() ||
9217 "Cannot use an ext load to change the number of vector elements!");
9218 }
9219
9220 bool Indexed = AM != ISD::UNINDEXED;
9221 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9222
9223 SDVTList VTs = Indexed ?
9224 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9225 SDValue Ops[] = { Chain, Ptr, Offset };
9227 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9228 ID.AddInteger(MemVT.getRawBits());
9229 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9230 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9231 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9232 ID.AddInteger(MMO->getFlags());
9233 void *IP = nullptr;
9234 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9235 cast<LoadSDNode>(E)->refineAlignment(MMO);
9236 return SDValue(E, 0);
9237 }
9238 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9239 ExtType, MemVT, MMO);
9240 createOperands(N, Ops);
9241
9242 CSEMap.InsertNode(N, IP);
9243 InsertNode(N);
9244 SDValue V(N, 0);
9245 NewSDValueDbgMsg(V, "Creating new node: ", this);
9246 return V;
9247}
9248
9251 MaybeAlign Alignment,
9252 MachineMemOperand::Flags MMOFlags,
9253 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9254 SDValue Undef = getUNDEF(Ptr.getValueType());
9255 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9256 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9257}
9258
9261 SDValue Undef = getUNDEF(Ptr.getValueType());
9262 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9263 VT, MMO);
9264}
9265
9267 EVT VT, SDValue Chain, SDValue Ptr,
9268 MachinePointerInfo PtrInfo, EVT MemVT,
9269 MaybeAlign Alignment,
9270 MachineMemOperand::Flags MMOFlags,
9271 const AAMDNodes &AAInfo) {
9272 SDValue Undef = getUNDEF(Ptr.getValueType());
9273 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9274 MemVT, Alignment, MMOFlags, AAInfo);
9275}
9276
9278 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9279 MachineMemOperand *MMO) {
9280 SDValue Undef = getUNDEF(Ptr.getValueType());
9281 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9282 MemVT, MMO);
9283}
9284
9288 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9289 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9290 // Don't propagate the invariant or dereferenceable flags.
9291 auto MMOFlags =
9292 LD->getMemOperand()->getFlags() &
9294 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9295 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9296 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9297}
9298
9301 Align Alignment,
9302 MachineMemOperand::Flags MMOFlags,
9303 const AAMDNodes &AAInfo) {
9304 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9305
9306 MMOFlags |= MachineMemOperand::MOStore;
9307 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9308
9309 if (PtrInfo.V.isNull())
9310 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9311
9314 MachineMemOperand *MMO =
9315 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9316 return getStore(Chain, dl, Val, Ptr, MMO);
9317}
9318
9321 assert(Chain.getValueType() == MVT::Other &&
9322 "Invalid chain type");
9323 EVT VT = Val.getValueType();
9324 SDVTList VTs = getVTList(MVT::Other);
9325 SDValue Undef = getUNDEF(Ptr.getValueType());
9326 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9328 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9329 ID.AddInteger(VT.getRawBits());
9330 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9331 dl.getIROrder(), VTs, ISD::UNINDEXED, false, VT, MMO));
9332 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9333 ID.AddInteger(MMO->getFlags());
9334 void *IP = nullptr;
9335 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9336 cast<StoreSDNode>(E)->refineAlignment(MMO);
9337 return SDValue(E, 0);
9338 }
9339 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9340 ISD::UNINDEXED, false, VT, MMO);
9341 createOperands(N, Ops);
9342
9343 CSEMap.InsertNode(N, IP);
9344 InsertNode(N);
9345 SDValue V(N, 0);
9346 NewSDValueDbgMsg(V, "Creating new node: ", this);
9347 return V;
9348}
9349
9352 EVT SVT, Align Alignment,
9353 MachineMemOperand::Flags MMOFlags,
9354 const AAMDNodes &AAInfo) {
9355 assert(Chain.getValueType() == MVT::Other &&
9356 "Invalid chain type");
9357
9358 MMOFlags |= MachineMemOperand::MOStore;
9359 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9360
9361 if (PtrInfo.V.isNull())
9362 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9363
9366 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9367 AAInfo);
9368 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9369}
9370
9372 SDValue Ptr, EVT SVT,
9373 MachineMemOperand *MMO) {
9374 EVT VT = Val.getValueType();
9375
9376 assert(Chain.getValueType() == MVT::Other &&
9377 "Invalid chain type");
9378 if (VT == SVT)
9379 return getStore(Chain, dl, Val, Ptr, MMO);
9380
9382 "Should only be a truncating store, not extending!");
9383 assert(VT.isInteger() == SVT.isInteger() &&
9384 "Can't do FP-INT conversion!");
9385 assert(VT.isVector() == SVT.isVector() &&
9386 "Cannot use trunc store to convert to or from a vector!");
9387 assert((!VT.isVector() ||
9389 "Cannot use trunc store to change the number of vector elements!");
9390
9391 SDVTList VTs = getVTList(MVT::Other);
9392 SDValue Undef = getUNDEF(Ptr.getValueType());
9393 SDValue Ops[] = { Chain, Val, Ptr, Undef };
9395 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9396 ID.AddInteger(SVT.getRawBits());
9397 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9398 dl.getIROrder(), VTs, ISD::UNINDEXED, true, SVT, MMO));
9399 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9400 ID.AddInteger(MMO->getFlags());
9401 void *IP = nullptr;
9402 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9403 cast<StoreSDNode>(E)->refineAlignment(MMO);
9404 return SDValue(E, 0);
9405 }
9406 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9407 ISD::UNINDEXED, true, SVT, MMO);
9408 createOperands(N, Ops);
9409
9410 CSEMap.InsertNode(N, IP);
9411 InsertNode(N);
9412 SDValue V(N, 0);
9413 NewSDValueDbgMsg(V, "Creating new node: ", this);
9414 return V;
9415}
9416
9420 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9421 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9422 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9423 SDValue Ops[] = { ST->getChain(), ST->getValue(), Base, Offset };
9425 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9426 ID.AddInteger(ST->getMemoryVT().getRawBits());
9427 ID.AddInteger(ST->getRawSubclassData());
9428 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9429 ID.AddInteger(ST->getMemOperand()->getFlags());
9430 void *IP = nullptr;
9431 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9432 return SDValue(E, 0);
9433
9434 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9435 ST->isTruncatingStore(), ST->getMemoryVT(),
9436 ST->getMemOperand());
9437 createOperands(N, Ops);
9438
9439 CSEMap.InsertNode(N, IP);
9440 InsertNode(N);
9441 SDValue V(N, 0);
9442 NewSDValueDbgMsg(V, "Creating new node: ", this);
9443 return V;
9444}
9445
9447 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9448 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9449 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9450 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9451 const MDNode *Ranges, bool IsExpanding) {
9452 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9453
9454 MMOFlags |= MachineMemOperand::MOLoad;
9455 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9456 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9457 // clients.
9458 if (PtrInfo.V.isNull())
9459 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9460
9463 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9464 Alignment, AAInfo, Ranges);
9465 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9466 MMO, IsExpanding);
9467}
9468
9470 ISD::LoadExtType ExtType, EVT VT,
9471 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9472 SDValue Offset, SDValue Mask, SDValue EVL,
9473 EVT MemVT, MachineMemOperand *MMO,
9474 bool IsExpanding) {
9475 bool Indexed = AM != ISD::UNINDEXED;
9476 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9477
9478 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9479 : getVTList(VT, MVT::Other);
9480 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9482 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9483 ID.AddInteger(MemVT.getRawBits());
9484 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9485 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9486 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9487 ID.AddInteger(MMO->getFlags());
9488 void *IP = nullptr;
9489 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9490 cast<VPLoadSDNode>(E)->refineAlignment(MMO);
9491 return SDValue(E, 0);
9492 }
9493 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9494 ExtType, IsExpanding, MemVT, MMO);
9495 createOperands(N, Ops);
9496
9497 CSEMap.InsertNode(N, IP);
9498 InsertNode(N);
9499 SDValue V(N, 0);
9500 NewSDValueDbgMsg(V, "Creating new node: ", this);
9501 return V;
9502}
9503
9505 SDValue Ptr, SDValue Mask, SDValue EVL,
9506 MachinePointerInfo PtrInfo,
9507 MaybeAlign Alignment,
9508 MachineMemOperand::Flags MMOFlags,
9509 const AAMDNodes &AAInfo, const MDNode *Ranges,
9510 bool IsExpanding) {
9511 SDValue Undef = getUNDEF(Ptr.getValueType());
9512 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9513 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9514 IsExpanding);
9515}
9516
9518 SDValue Ptr, SDValue Mask, SDValue EVL,
9519 MachineMemOperand *MMO, bool IsExpanding) {
9520 SDValue Undef = getUNDEF(Ptr.getValueType());
9521 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9522 Mask, EVL, VT, MMO, IsExpanding);
9523}
9524
9526 EVT VT, SDValue Chain, SDValue Ptr,
9527 SDValue Mask, SDValue EVL,
9528 MachinePointerInfo PtrInfo, EVT MemVT,
9529 MaybeAlign Alignment,
9530 MachineMemOperand::Flags MMOFlags,
9531 const AAMDNodes &AAInfo, bool IsExpanding) {
9532 SDValue Undef = getUNDEF(Ptr.getValueType());
9533 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9534 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
9535 IsExpanding);
9536}
9537
9539 EVT VT, SDValue Chain, SDValue Ptr,
9540 SDValue Mask, SDValue EVL, EVT MemVT,
9541 MachineMemOperand *MMO, bool IsExpanding) {
9542 SDValue Undef = getUNDEF(Ptr.getValueType());
9543 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9544 EVL, MemVT, MMO, IsExpanding);
9545}
9546
9550 auto *LD = cast<VPLoadSDNode>(OrigLoad);
9551 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9552 // Don't propagate the invariant or dereferenceable flags.
9553 auto MMOFlags =
9554 LD->getMemOperand()->getFlags() &
9556 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9557 LD->getChain(), Base, Offset, LD->getMask(),
9558 LD->getVectorLength(), LD->getPointerInfo(),
9559 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
9560 nullptr, LD->isExpandingLoad());
9561}
9562
9565 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
9566 ISD::MemIndexedMode AM, bool IsTruncating,
9567 bool IsCompressing) {
9568 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9569 bool Indexed = AM != ISD::UNINDEXED;
9570 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9571 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9572 : getVTList(MVT::Other);
9573 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
9575 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9576 ID.AddInteger(MemVT.getRawBits());
9577 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9578 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9579 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9580 ID.AddInteger(MMO->getFlags());
9581 void *IP = nullptr;
9582 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9583 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9584 return SDValue(E, 0);
9585 }
9586 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9587 IsTruncating, IsCompressing, MemVT, MMO);
9588 createOperands(N, Ops);
9589
9590 CSEMap.InsertNode(N, IP);
9591 InsertNode(N);
9592 SDValue V(N, 0);
9593 NewSDValueDbgMsg(V, "Creating new node: ", this);
9594 return V;
9595}
9596
9598 SDValue Val, SDValue Ptr, SDValue Mask,
9599 SDValue EVL, MachinePointerInfo PtrInfo,
9600 EVT SVT, Align Alignment,
9601 MachineMemOperand::Flags MMOFlags,
9602 const AAMDNodes &AAInfo,
9603 bool IsCompressing) {
9604 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9605
9606 MMOFlags |= MachineMemOperand::MOStore;
9607 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9608
9609 if (PtrInfo.V.isNull())
9610 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9611
9614 PtrInfo, MMOFlags, LocationSize::precise(SVT.getStoreSize()), Alignment,
9615 AAInfo);
9616 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
9617 IsCompressing);
9618}
9619
9621 SDValue Val, SDValue Ptr, SDValue Mask,
9622 SDValue EVL, EVT SVT,
9623 MachineMemOperand *MMO,
9624 bool IsCompressing) {
9625 EVT VT = Val.getValueType();
9626
9627 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9628 if (VT == SVT)
9629 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
9630 EVL, VT, MMO, ISD::UNINDEXED,
9631 /*IsTruncating*/ false, IsCompressing);
9632
9634 "Should only be a truncating store, not extending!");
9635 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9636 assert(VT.isVector() == SVT.isVector() &&
9637 "Cannot use trunc store to convert to or from a vector!");
9638 assert((!VT.isVector() ||
9640 "Cannot use trunc store to change the number of vector elements!");
9641
9642 SDVTList VTs = getVTList(MVT::Other);
9643 SDValue Undef = getUNDEF(Ptr.getValueType());
9644 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
9646 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9647 ID.AddInteger(SVT.getRawBits());
9648 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
9649 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9650 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9651 ID.AddInteger(MMO->getFlags());
9652 void *IP = nullptr;
9653 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9654 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
9655 return SDValue(E, 0);
9656 }
9657 auto *N =
9658 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9659 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
9660 createOperands(N, Ops);
9661
9662 CSEMap.InsertNode(N, IP);
9663 InsertNode(N);
9664 SDValue V(N, 0);
9665 NewSDValueDbgMsg(V, "Creating new node: ", this);
9666 return V;
9667}
9668
9672 auto *ST = cast<VPStoreSDNode>(OrigStore);
9673 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
9674 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
9675 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
9676 Offset, ST->getMask(), ST->getVectorLength()};
9678 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
9679 ID.AddInteger(ST->getMemoryVT().getRawBits());
9680 ID.AddInteger(ST->getRawSubclassData());
9681 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
9682 ID.AddInteger(ST->getMemOperand()->getFlags());
9683 void *IP = nullptr;
9684 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9685 return SDValue(E, 0);
9686
9687 auto *N = newSDNode<VPStoreSDNode>(
9688 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
9689 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
9690 createOperands(N, Ops);
9691
9692 CSEMap.InsertNode(N, IP);
9693 InsertNode(N);
9694 SDValue V(N, 0);
9695 NewSDValueDbgMsg(V, "Creating new node: ", this);
9696 return V;
9697}
9698
9700 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
9701 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
9702 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
9703 bool Indexed = AM != ISD::UNINDEXED;
9704 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9705
9706 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
9707 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9708 : getVTList(VT, MVT::Other);
9710 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
9711 ID.AddInteger(VT.getRawBits());
9712 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
9713 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9714 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9715
9716 void *IP = nullptr;
9717 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9718 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
9719 return SDValue(E, 0);
9720 }
9721
9722 auto *N =
9723 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
9724 ExtType, IsExpanding, MemVT, MMO);
9725 createOperands(N, Ops);
9726 CSEMap.InsertNode(N, IP);
9727 InsertNode(N);
9728 SDValue V(N, 0);
9729 NewSDValueDbgMsg(V, "Creating new node: ", this);
9730 return V;
9731}
9732
9734 SDValue Ptr, SDValue Stride,
9735 SDValue Mask, SDValue EVL,
9736 MachineMemOperand *MMO,
9737 bool IsExpanding) {
9738 SDValue Undef = getUNDEF(Ptr.getValueType());
9740 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
9741}
9742
9744 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
9745 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
9746 MachineMemOperand *MMO, bool IsExpanding) {
9747 SDValue Undef = getUNDEF(Ptr.getValueType());
9748 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
9749 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
9750}
9751
9753 SDValue Val, SDValue Ptr,
9754 SDValue Offset, SDValue Stride,
9755 SDValue Mask, SDValue EVL, EVT MemVT,
9756 MachineMemOperand *MMO,
9758 bool IsTruncating, bool IsCompressing) {
9759 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9760 bool Indexed = AM != ISD::UNINDEXED;
9761 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
9762 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9763 : getVTList(MVT::Other);
9764 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
9766 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9767 ID.AddInteger(MemVT.getRawBits());
9768 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9769 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9770 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9771 void *IP = nullptr;
9772 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9773 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9774 return SDValue(E, 0);
9775 }
9776 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9777 VTs, AM, IsTruncating,
9778 IsCompressing, MemVT, MMO);
9779 createOperands(N, Ops);
9780
9781 CSEMap.InsertNode(N, IP);
9782 InsertNode(N);
9783 SDValue V(N, 0);
9784 NewSDValueDbgMsg(V, "Creating new node: ", this);
9785 return V;
9786}
9787
9789 SDValue Val, SDValue Ptr,
9790 SDValue Stride, SDValue Mask,
9791 SDValue EVL, EVT SVT,
9792 MachineMemOperand *MMO,
9793 bool IsCompressing) {
9794 EVT VT = Val.getValueType();
9795
9796 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9797 if (VT == SVT)
9798 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
9799 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
9800 /*IsTruncating*/ false, IsCompressing);
9801
9803 "Should only be a truncating store, not extending!");
9804 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9805 assert(VT.isVector() == SVT.isVector() &&
9806 "Cannot use trunc store to convert to or from a vector!");
9807 assert((!VT.isVector() ||
9809 "Cannot use trunc store to change the number of vector elements!");
9810
9811 SDVTList VTs = getVTList(MVT::Other);
9812 SDValue Undef = getUNDEF(Ptr.getValueType());
9813 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
9815 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
9816 ID.AddInteger(SVT.getRawBits());
9817 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
9818 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
9819 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9820 void *IP = nullptr;
9821 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
9822 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
9823 return SDValue(E, 0);
9824 }
9825 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
9826 VTs, ISD::UNINDEXED, true,
9827 IsCompressing, SVT, MMO);
9828 createOperands(N, Ops);
9829
9830 CSEMap.InsertNode(N, IP);
9831 InsertNode(N);
9832 SDValue V(N, 0);
9833 NewSDValueDbgMsg(V, "Creating new node: ", this);
9834 return V;
9835}
9836
9839 ISD::MemIndexType IndexType) {
9840 assert(Ops.size() == 6 && "Incompatible number of operands");
9841
9843 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
9844 ID.AddInteger(VT.getRawBits());
9845 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
9846 dl.getIROrder(), VTs, VT, MMO, IndexType));
9847 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9848 ID.AddInteger(MMO->getFlags());
9849 void *IP = nullptr;
9850 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9851 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
9852 return SDValue(E, 0);
9853 }
9854
9855 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9856 VT, MMO, IndexType);
9857 createOperands(N, Ops);
9858
9859 assert(N->getMask().getValueType().getVectorElementCount() ==
9860 N->getValueType(0).getVectorElementCount() &&
9861 "Vector width mismatch between mask and data");
9862 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9863 N->getValueType(0).getVectorElementCount().isScalable() &&
9864 "Scalable flags of index and data do not match");
9866 N->getIndex().getValueType().getVectorElementCount(),
9867 N->getValueType(0).getVectorElementCount()) &&
9868 "Vector width mismatch between index and data");
9869 assert(isa<ConstantSDNode>(N->getScale()) &&
9870 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9871 "Scale should be a constant power of 2");
9872
9873 CSEMap.InsertNode(N, IP);
9874 InsertNode(N);
9875 SDValue V(N, 0);
9876 NewSDValueDbgMsg(V, "Creating new node: ", this);
9877 return V;
9878}
9879
9882 MachineMemOperand *MMO,
9883 ISD::MemIndexType IndexType) {
9884 assert(Ops.size() == 7 && "Incompatible number of operands");
9885
9887 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
9888 ID.AddInteger(VT.getRawBits());
9889 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
9890 dl.getIROrder(), VTs, VT, MMO, IndexType));
9891 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9892 ID.AddInteger(MMO->getFlags());
9893 void *IP = nullptr;
9894 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9895 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
9896 return SDValue(E, 0);
9897 }
9898 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9899 VT, MMO, IndexType);
9900 createOperands(N, Ops);
9901
9902 assert(N->getMask().getValueType().getVectorElementCount() ==
9903 N->getValue().getValueType().getVectorElementCount() &&
9904 "Vector width mismatch between mask and data");
9905 assert(
9906 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
9907 N->getValue().getValueType().getVectorElementCount().isScalable() &&
9908 "Scalable flags of index and data do not match");
9910 N->getIndex().getValueType().getVectorElementCount(),
9911 N->getValue().getValueType().getVectorElementCount()) &&
9912 "Vector width mismatch between index and data");
9913 assert(isa<ConstantSDNode>(N->getScale()) &&
9914 N->getScale()->getAsAPIntVal().isPowerOf2() &&
9915 "Scale should be a constant power of 2");
9916
9917 CSEMap.InsertNode(N, IP);
9918 InsertNode(N);
9919 SDValue V(N, 0);
9920 NewSDValueDbgMsg(V, "Creating new node: ", this);
9921 return V;
9922}
9923
9926 SDValue PassThru, EVT MemVT,
9927 MachineMemOperand *MMO,
9929 ISD::LoadExtType ExtTy, bool isExpanding) {
9930 bool Indexed = AM != ISD::UNINDEXED;
9931 assert((Indexed || Offset.isUndef()) &&
9932 "Unindexed masked load with an offset!");
9933 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
9934 : getVTList(VT, MVT::Other);
9935 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
9937 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
9938 ID.AddInteger(MemVT.getRawBits());
9939 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
9940 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
9941 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9942 ID.AddInteger(MMO->getFlags());
9943 void *IP = nullptr;
9944 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9945 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
9946 return SDValue(E, 0);
9947 }
9948 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
9949 AM, ExtTy, isExpanding, MemVT, MMO);
9950 createOperands(N, Ops);
9951
9952 CSEMap.InsertNode(N, IP);
9953 InsertNode(N);
9954 SDValue V(N, 0);
9955 NewSDValueDbgMsg(V, "Creating new node: ", this);
9956 return V;
9957}
9958
9962 MaskedLoadSDNode *LD = cast<MaskedLoadSDNode>(OrigLoad);
9963 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
9964 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
9965 Offset, LD->getMask(), LD->getPassThru(),
9966 LD->getMemoryVT(), LD->getMemOperand(), AM,
9967 LD->getExtensionType(), LD->isExpandingLoad());
9968}
9969
9972 SDValue Mask, EVT MemVT,
9973 MachineMemOperand *MMO,
9974 ISD::MemIndexedMode AM, bool IsTruncating,
9975 bool IsCompressing) {
9976 assert(Chain.getValueType() == MVT::Other &&
9977 "Invalid chain type");
9978 bool Indexed = AM != ISD::UNINDEXED;
9979 assert((Indexed || Offset.isUndef()) &&
9980 "Unindexed masked store with an offset!");
9981 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
9982 : getVTList(MVT::Other);
9983 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
9985 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
9986 ID.AddInteger(MemVT.getRawBits());
9987 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
9988 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
9989 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9990 ID.AddInteger(MMO->getFlags());
9991 void *IP = nullptr;
9992 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9993 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
9994 return SDValue(E, 0);
9995 }
9996 auto *N =
9997 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9998 IsTruncating, IsCompressing, MemVT, MMO);
9999 createOperands(N, Ops);
10000
10001 CSEMap.InsertNode(N, IP);
10002 InsertNode(N);
10003 SDValue V(N, 0);
10004 NewSDValueDbgMsg(V, "Creating new node: ", this);
10005 return V;
10006}
10007
10011 MaskedStoreSDNode *ST = cast<MaskedStoreSDNode>(OrigStore);
10012 assert(ST->getOffset().isUndef() &&
10013 "Masked store is already a indexed store!");
10014 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10015 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10016 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10017}
10018
10021 MachineMemOperand *MMO,
10022 ISD::MemIndexType IndexType,
10023 ISD::LoadExtType ExtTy) {
10024 assert(Ops.size() == 6 && "Incompatible number of operands");
10025
10027 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10028 ID.AddInteger(MemVT.getRawBits());
10029 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10030 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10031 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10032 ID.AddInteger(MMO->getFlags());
10033 void *IP = nullptr;
10034 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10035 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10036 return SDValue(E, 0);
10037 }
10038
10039 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10040 VTs, MemVT, MMO, IndexType, ExtTy);
10041 createOperands(N, Ops);
10042
10043 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10044 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10045 assert(N->getMask().getValueType().getVectorElementCount() ==
10046 N->getValueType(0).getVectorElementCount() &&
10047 "Vector width mismatch between mask and data");
10048 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10049 N->getValueType(0).getVectorElementCount().isScalable() &&
10050 "Scalable flags of index and data do not match");
10052 N->getIndex().getValueType().getVectorElementCount(),
10053 N->getValueType(0).getVectorElementCount()) &&
10054 "Vector width mismatch between index and data");
10055 assert(isa<ConstantSDNode>(N->getScale()) &&
10056 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10057 "Scale should be a constant power of 2");
10058
10059 CSEMap.InsertNode(N, IP);
10060 InsertNode(N);
10061 SDValue V(N, 0);
10062 NewSDValueDbgMsg(V, "Creating new node: ", this);
10063 return V;
10064}
10065
10068 MachineMemOperand *MMO,
10069 ISD::MemIndexType IndexType,
10070 bool IsTrunc) {
10071 assert(Ops.size() == 6 && "Incompatible number of operands");
10072
10074 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10075 ID.AddInteger(MemVT.getRawBits());
10076 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10077 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10078 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10079 ID.AddInteger(MMO->getFlags());
10080 void *IP = nullptr;
10081 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10082 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10083 return SDValue(E, 0);
10084 }
10085
10086 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10087 VTs, MemVT, MMO, IndexType, IsTrunc);
10088 createOperands(N, Ops);
10089
10090 assert(N->getMask().getValueType().getVectorElementCount() ==
10091 N->getValue().getValueType().getVectorElementCount() &&
10092 "Vector width mismatch between mask and data");
10093 assert(
10094 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10095 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10096 "Scalable flags of index and data do not match");
10098 N->getIndex().getValueType().getVectorElementCount(),
10099 N->getValue().getValueType().getVectorElementCount()) &&
10100 "Vector width mismatch between index and data");
10101 assert(isa<ConstantSDNode>(N->getScale()) &&
10102 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10103 "Scale should be a constant power of 2");
10104
10105 CSEMap.InsertNode(N, IP);
10106 InsertNode(N);
10107 SDValue V(N, 0);
10108 NewSDValueDbgMsg(V, "Creating new node: ", this);
10109 return V;
10110}
10111
10113 const SDLoc &dl, ArrayRef<SDValue> Ops,
10114 MachineMemOperand *MMO,
10115 ISD::MemIndexType IndexType) {
10116 assert(Ops.size() == 7 && "Incompatible number of operands");
10117
10120 ID.AddInteger(MemVT.getRawBits());
10121 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10122 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10123 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10124 ID.AddInteger(MMO->getFlags());
10125 void *IP = nullptr;
10126 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10127 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10128 return SDValue(E, 0);
10129 }
10130
10131 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10132 VTs, MemVT, MMO, IndexType);
10133 createOperands(N, Ops);
10134
10135 assert(N->getMask().getValueType().getVectorElementCount() ==
10136 N->getIndex().getValueType().getVectorElementCount() &&
10137 "Vector width mismatch between mask and data");
10138 assert(isa<ConstantSDNode>(N->getScale()) &&
10139 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10140 "Scale should be a constant power of 2");
10141 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10142
10143 CSEMap.InsertNode(N, IP);
10144 InsertNode(N);
10145 SDValue V(N, 0);
10146 NewSDValueDbgMsg(V, "Creating new node: ", this);
10147 return V;
10148}
10149
10151 EVT MemVT, MachineMemOperand *MMO) {
10152 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10153 SDVTList VTs = getVTList(MVT::Other);
10154 SDValue Ops[] = {Chain, Ptr};
10157 ID.AddInteger(MemVT.getRawBits());
10158 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10159 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10160 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10161 ID.AddInteger(MMO->getFlags());
10162 void *IP = nullptr;
10163 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10164 return SDValue(E, 0);
10165
10166 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10167 dl.getDebugLoc(), VTs, MemVT, MMO);
10168 createOperands(N, Ops);
10169
10170 CSEMap.InsertNode(N, IP);
10171 InsertNode(N);
10172 SDValue V(N, 0);
10173 NewSDValueDbgMsg(V, "Creating new node: ", this);
10174 return V;
10175}
10176
10178 EVT MemVT, MachineMemOperand *MMO) {
10179 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10180 SDVTList VTs = getVTList(MVT::Other);
10181 SDValue Ops[] = {Chain, Ptr};
10184 ID.AddInteger(MemVT.getRawBits());
10185 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10186 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10187 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10188 ID.AddInteger(MMO->getFlags());
10189 void *IP = nullptr;
10190 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10191 return SDValue(E, 0);
10192
10193 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10194 dl.getDebugLoc(), VTs, MemVT, MMO);
10195 createOperands(N, Ops);
10196
10197 CSEMap.InsertNode(N, IP);
10198 InsertNode(N);
10199 SDValue V(N, 0);
10200 NewSDValueDbgMsg(V, "Creating new node: ", this);
10201 return V;
10202}
10203
10205 // select undef, T, F --> T (if T is a constant), otherwise F
10206 // select, ?, undef, F --> F
10207 // select, ?, T, undef --> T
10208 if (Cond.isUndef())
10209 return isConstantValueOfAnyType(T) ? T : F;
10210 if (T.isUndef())
10211 return F;
10212 if (F.isUndef())
10213 return T;
10214
10215 // select true, T, F --> T
10216 // select false, T, F --> F
10217 if (auto C = isBoolConstant(Cond, /*AllowTruncation=*/true))
10218 return *C ? T : F;
10219
10220 // select ?, T, T --> T
10221 if (T == F)
10222 return T;
10223
10224 return SDValue();
10225}
10226
10228 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10229 if (X.isUndef())
10230 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10231 // shift X, undef --> undef (because it may shift by the bitwidth)
10232 if (Y.isUndef())
10233 return getUNDEF(X.getValueType());
10234
10235 // shift 0, Y --> 0
10236 // shift X, 0 --> X
10238 return X;
10239
10240 // shift X, C >= bitwidth(X) --> undef
10241 // All vector elements must be too big (or undef) to avoid partial undefs.
10242 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10243 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10244 };
10245 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10246 return getUNDEF(X.getValueType());
10247
10248 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10249 if (X.getValueType().getScalarType() == MVT::i1)
10250 return X;
10251
10252 return SDValue();
10253}
10254
10256 SDNodeFlags Flags) {
10257 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10258 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10259 // operation is poison. That result can be relaxed to undef.
10260 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10261 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10262 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10263 (YC && YC->getValueAPF().isNaN());
10264 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10265 (YC && YC->getValueAPF().isInfinity());
10266
10267 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10268 return getUNDEF(X.getValueType());
10269
10270 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10271 return getUNDEF(X.getValueType());
10272
10273 if (!YC)
10274 return SDValue();
10275
10276 // X + -0.0 --> X
10277 if (Opcode == ISD::FADD)
10278 if (YC->getValueAPF().isNegZero())
10279 return X;
10280
10281 // X - +0.0 --> X
10282 if (Opcode == ISD::FSUB)
10283 if (YC->getValueAPF().isPosZero())
10284 return X;
10285
10286 // X * 1.0 --> X
10287 // X / 1.0 --> X
10288 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10289 if (YC->getValueAPF().isExactlyValue(1.0))
10290 return X;
10291
10292 // X * 0.0 --> 0.0
10293 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10294 if (YC->getValueAPF().isZero())
10295 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10296
10297 return SDValue();
10298}
10299
10301 SDValue Ptr, SDValue SV, unsigned Align) {
10302 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10303 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10304}
10305
10306SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10307 ArrayRef<SDUse> Ops) {
10308 switch (Ops.size()) {
10309 case 0: return getNode(Opcode, DL, VT);
10310 case 1: return getNode(Opcode, DL, VT, static_cast<const SDValue>(Ops[0]));
10311 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10312 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10313 default: break;
10314 }
10315
10316 // Copy from an SDUse array into an SDValue array for use with
10317 // the regular getNode logic.
10318 SmallVector<SDValue, 8> NewOps(Ops);
10319 return getNode(Opcode, DL, VT, NewOps);
10320}
10321
10322SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10323 ArrayRef<SDValue> Ops) {
10324 SDNodeFlags Flags;
10325 if (Inserter)
10326 Flags = Inserter->getFlags();
10327 return getNode(Opcode, DL, VT, Ops, Flags);
10328}
10329
10330SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10331 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10332 unsigned NumOps = Ops.size();
10333 switch (NumOps) {
10334 case 0: return getNode(Opcode, DL, VT);
10335 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10336 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10337 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10338 default: break;
10339 }
10340
10341#ifndef NDEBUG
10342 for (const auto &Op : Ops)
10343 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10344 "Operand is DELETED_NODE!");
10345#endif
10346
10347 switch (Opcode) {
10348 default: break;
10349 case ISD::BUILD_VECTOR:
10350 // Attempt to simplify BUILD_VECTOR.
10351 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10352 return V;
10353 break;
10355 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10356 return V;
10357 break;
10358 case ISD::SELECT_CC:
10359 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10360 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10361 "LHS and RHS of condition must have same type!");
10362 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10363 "True and False arms of SelectCC must have same type!");
10364 assert(Ops[2].getValueType() == VT &&
10365 "select_cc node must be of same type as true and false value!");
10366 assert((!Ops[0].getValueType().isVector() ||
10367 Ops[0].getValueType().getVectorElementCount() ==
10368 VT.getVectorElementCount()) &&
10369 "Expected select_cc with vector result to have the same sized "
10370 "comparison type!");
10371 break;
10372 case ISD::BR_CC:
10373 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10374 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10375 "LHS/RHS of comparison should match types!");
10376 break;
10377 case ISD::VP_ADD:
10378 case ISD::VP_SUB:
10379 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10380 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10381 Opcode = ISD::VP_XOR;
10382 break;
10383 case ISD::VP_MUL:
10384 // If it is VP_MUL mask operation then turn it to VP_AND
10385 if (VT.isVector() && VT.getVectorElementType() == MVT::i1)
10386 Opcode = ISD::VP_AND;
10387 break;
10388 case ISD::VP_REDUCE_MUL:
10389 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10390 if (VT == MVT::i1)
10391 Opcode = ISD::VP_REDUCE_AND;
10392 break;
10393 case ISD::VP_REDUCE_ADD:
10394 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10395 if (VT == MVT::i1)
10396 Opcode = ISD::VP_REDUCE_XOR;
10397 break;
10398 case ISD::VP_REDUCE_SMAX:
10399 case ISD::VP_REDUCE_UMIN:
10400 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10401 // VP_REDUCE_AND.
10402 if (VT == MVT::i1)
10403 Opcode = ISD::VP_REDUCE_AND;
10404 break;
10405 case ISD::VP_REDUCE_SMIN:
10406 case ISD::VP_REDUCE_UMAX:
10407 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10408 // VP_REDUCE_OR.
10409 if (VT == MVT::i1)
10410 Opcode = ISD::VP_REDUCE_OR;
10411 break;
10412 }
10413
10414 // Memoize nodes.
10415 SDNode *N;
10416 SDVTList VTs = getVTList(VT);
10417
10418 if (VT != MVT::Glue) {
10420 AddNodeIDNode(ID, Opcode, VTs, Ops);
10421 void *IP = nullptr;
10422
10423 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10424 E->intersectFlagsWith(Flags);
10425 return SDValue(E, 0);
10426 }
10427
10428 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10429 createOperands(N, Ops);
10430
10431 CSEMap.InsertNode(N, IP);
10432 } else {
10433 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10434 createOperands(N, Ops);
10435 }
10436
10437 N->setFlags(Flags);
10438 InsertNode(N);
10439 SDValue V(N, 0);
10440 NewSDValueDbgMsg(V, "Creating new node: ", this);
10441 return V;
10442}
10443
10444SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10445 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10446 return getNode(Opcode, DL, getVTList(ResultTys), Ops);
10447}
10448
10449SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10450 ArrayRef<SDValue> Ops) {
10451 SDNodeFlags Flags;
10452 if (Inserter)
10453 Flags = Inserter->getFlags();
10454 return getNode(Opcode, DL, VTList, Ops, Flags);
10455}
10456
10457SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10458 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10459 if (VTList.NumVTs == 1)
10460 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10461
10462#ifndef NDEBUG
10463 for (const auto &Op : Ops)
10464 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10465 "Operand is DELETED_NODE!");
10466#endif
10467
10468 switch (Opcode) {
10469 case ISD::SADDO:
10470 case ISD::UADDO:
10471 case ISD::SSUBO:
10472 case ISD::USUBO: {
10473 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10474 "Invalid add/sub overflow op!");
10475 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10476 Ops[0].getValueType() == Ops[1].getValueType() &&
10477 Ops[0].getValueType() == VTList.VTs[0] &&
10478 "Binary operator types must match!");
10479 SDValue N1 = Ops[0], N2 = Ops[1];
10480 canonicalizeCommutativeBinop(Opcode, N1, N2);
10481
10482 // (X +- 0) -> X with zero-overflow.
10483 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10484 /*AllowTruncation*/ true);
10485 if (N2CV && N2CV->isZero()) {
10486 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10487 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10488 }
10489
10490 if (VTList.VTs[0].isVector() &&
10491 VTList.VTs[0].getVectorElementType() == MVT::i1 &&
10492 VTList.VTs[1].getVectorElementType() == MVT::i1) {
10493 SDValue F1 = getFreeze(N1);
10494 SDValue F2 = getFreeze(N2);
10495 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10496 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10497 return getNode(ISD::MERGE_VALUES, DL, VTList,
10498 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10499 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
10500 Flags);
10501 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
10502 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
10503 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
10504 return getNode(ISD::MERGE_VALUES, DL, VTList,
10505 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10506 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
10507 Flags);
10508 }
10509 }
10510 break;
10511 }
10512 case ISD::SADDO_CARRY:
10513 case ISD::UADDO_CARRY:
10514 case ISD::SSUBO_CARRY:
10515 case ISD::USUBO_CARRY:
10516 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
10517 "Invalid add/sub overflow op!");
10518 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10519 Ops[0].getValueType() == Ops[1].getValueType() &&
10520 Ops[0].getValueType() == VTList.VTs[0] &&
10521 Ops[2].getValueType() == VTList.VTs[1] &&
10522 "Binary operator types must match!");
10523 break;
10524 case ISD::SMUL_LOHI:
10525 case ISD::UMUL_LOHI: {
10526 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
10527 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
10528 VTList.VTs[0] == Ops[0].getValueType() &&
10529 VTList.VTs[0] == Ops[1].getValueType() &&
10530 "Binary operator types must match!");
10531 // Constant fold.
10532 ConstantSDNode *LHS = dyn_cast<ConstantSDNode>(Ops[0]);
10533 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(Ops[1]);
10534 if (LHS && RHS) {
10535 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
10536 unsigned OutWidth = Width * 2;
10537 APInt Val = LHS->getAPIntValue();
10538 APInt Mul = RHS->getAPIntValue();
10539 if (Opcode == ISD::SMUL_LOHI) {
10540 Val = Val.sext(OutWidth);
10541 Mul = Mul.sext(OutWidth);
10542 } else {
10543 Val = Val.zext(OutWidth);
10544 Mul = Mul.zext(OutWidth);
10545 }
10546 Val *= Mul;
10547
10548 SDValue Hi =
10549 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
10550 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
10551 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
10552 }
10553 break;
10554 }
10555 case ISD::FFREXP: {
10556 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
10557 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
10558 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
10559
10560 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Ops[0])) {
10561 int FrexpExp;
10562 APFloat FrexpMant =
10563 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
10564 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
10565 SDValue Result1 =
10566 getConstant(FrexpMant.isFinite() ? FrexpExp : 0, DL, VTList.VTs[1]);
10567 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
10568 }
10569
10570 break;
10571 }
10573 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10574 "Invalid STRICT_FP_EXTEND!");
10575 assert(VTList.VTs[0].isFloatingPoint() &&
10576 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
10577 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10578 "STRICT_FP_EXTEND result type should be vector iff the operand "
10579 "type is vector!");
10580 assert((!VTList.VTs[0].isVector() ||
10581 VTList.VTs[0].getVectorElementCount() ==
10582 Ops[1].getValueType().getVectorElementCount()) &&
10583 "Vector element count mismatch!");
10584 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
10585 "Invalid fpext node, dst <= src!");
10586 break;
10588 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
10589 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
10590 "STRICT_FP_ROUND result type should be vector iff the operand "
10591 "type is vector!");
10592 assert((!VTList.VTs[0].isVector() ||
10593 VTList.VTs[0].getVectorElementCount() ==
10594 Ops[1].getValueType().getVectorElementCount()) &&
10595 "Vector element count mismatch!");
10596 assert(VTList.VTs[0].isFloatingPoint() &&
10597 Ops[1].getValueType().isFloatingPoint() &&
10598 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
10599 Ops[2].getOpcode() == ISD::TargetConstant &&
10600 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
10601 "Invalid STRICT_FP_ROUND!");
10602 break;
10603#if 0
10604 // FIXME: figure out how to safely handle things like
10605 // int foo(int x) { return 1 << (x & 255); }
10606 // int bar() { return foo(256); }
10607 case ISD::SRA_PARTS:
10608 case ISD::SRL_PARTS:
10609 case ISD::SHL_PARTS:
10610 if (N3.getOpcode() == ISD::SIGN_EXTEND_INREG &&
10611 cast<VTSDNode>(N3.getOperand(1))->getVT() != MVT::i1)
10612 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10613 else if (N3.getOpcode() == ISD::AND)
10614 if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(N3.getOperand(1))) {
10615 // If the and is only masking out bits that cannot effect the shift,
10616 // eliminate the and.
10617 unsigned NumBits = VT.getScalarSizeInBits()*2;
10618 if ((AndRHS->getValue() & (NumBits-1)) == NumBits-1)
10619 return getNode(Opcode, DL, VT, N1, N2, N3.getOperand(0));
10620 }
10621 break;
10622#endif
10623 }
10624
10625 // Memoize the node unless it returns a glue result.
10626 SDNode *N;
10627 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10629 AddNodeIDNode(ID, Opcode, VTList, Ops);
10630 void *IP = nullptr;
10631 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10632 E->intersectFlagsWith(Flags);
10633 return SDValue(E, 0);
10634 }
10635
10636 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10637 createOperands(N, Ops);
10638 CSEMap.InsertNode(N, IP);
10639 } else {
10640 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
10641 createOperands(N, Ops);
10642 }
10643
10644 N->setFlags(Flags);
10645 InsertNode(N);
10646 SDValue V(N, 0);
10647 NewSDValueDbgMsg(V, "Creating new node: ", this);
10648 return V;
10649}
10650
10651SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10652 SDVTList VTList) {
10653 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
10654}
10655
10656SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10657 SDValue N1) {
10658 SDValue Ops[] = { N1 };
10659 return getNode(Opcode, DL, VTList, Ops);
10660}
10661
10662SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10663 SDValue N1, SDValue N2) {
10664 SDValue Ops[] = { N1, N2 };
10665 return getNode(Opcode, DL, VTList, Ops);
10666}
10667
10668SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10669 SDValue N1, SDValue N2, SDValue N3) {
10670 SDValue Ops[] = { N1, N2, N3 };
10671 return getNode(Opcode, DL, VTList, Ops);
10672}
10673
10674SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10675 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
10676 SDValue Ops[] = { N1, N2, N3, N4 };
10677 return getNode(Opcode, DL, VTList, Ops);
10678}
10679
10680SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10681 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
10682 SDValue N5) {
10683 SDValue Ops[] = { N1, N2, N3, N4, N5 };
10684 return getNode(Opcode, DL, VTList, Ops);
10685}
10686
10688 if (!VT.isExtended())
10689 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
10690
10691 return makeVTList(&(*EVTs.insert(VT).first), 1);
10692}
10693
10696 ID.AddInteger(2U);
10697 ID.AddInteger(VT1.getRawBits());
10698 ID.AddInteger(VT2.getRawBits());
10699
10700 void *IP = nullptr;
10701 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10702 if (!Result) {
10703 EVT *Array = Allocator.Allocate<EVT>(2);
10704 Array[0] = VT1;
10705 Array[1] = VT2;
10706 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
10707 VTListMap.InsertNode(Result, IP);
10708 }
10709 return Result->getSDVTList();
10710}
10711
10714 ID.AddInteger(3U);
10715 ID.AddInteger(VT1.getRawBits());
10716 ID.AddInteger(VT2.getRawBits());
10717 ID.AddInteger(VT3.getRawBits());
10718
10719 void *IP = nullptr;
10720 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10721 if (!Result) {
10722 EVT *Array = Allocator.Allocate<EVT>(3);
10723 Array[0] = VT1;
10724 Array[1] = VT2;
10725 Array[2] = VT3;
10726 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
10727 VTListMap.InsertNode(Result, IP);
10728 }
10729 return Result->getSDVTList();
10730}
10731
10734 ID.AddInteger(4U);
10735 ID.AddInteger(VT1.getRawBits());
10736 ID.AddInteger(VT2.getRawBits());
10737 ID.AddInteger(VT3.getRawBits());
10738 ID.AddInteger(VT4.getRawBits());
10739
10740 void *IP = nullptr;
10741 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10742 if (!Result) {
10743 EVT *Array = Allocator.Allocate<EVT>(4);
10744 Array[0] = VT1;
10745 Array[1] = VT2;
10746 Array[2] = VT3;
10747 Array[3] = VT4;
10748 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
10749 VTListMap.InsertNode(Result, IP);
10750 }
10751 return Result->getSDVTList();
10752}
10753
10755 unsigned NumVTs = VTs.size();
10757 ID.AddInteger(NumVTs);
10758 for (unsigned index = 0; index < NumVTs; index++) {
10759 ID.AddInteger(VTs[index].getRawBits());
10760 }
10761
10762 void *IP = nullptr;
10763 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
10764 if (!Result) {
10765 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
10766 llvm::copy(VTs, Array);
10767 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
10768 VTListMap.InsertNode(Result, IP);
10769 }
10770 return Result->getSDVTList();
10771}
10772
10773
10774/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
10775/// specified operands. If the resultant node already exists in the DAG,
10776/// this does not modify the specified node, instead it returns the node that
10777/// already exists. If the resultant node does not exist in the DAG, the
10778/// input node is returned. As a degenerate case, if you specify the same
10779/// input operands as the node already has, the input node is returned.
10781 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
10782
10783 // Check to see if there is no change.
10784 if (Op == N->getOperand(0)) return N;
10785
10786 // See if the modified node already exists.
10787 void *InsertPos = nullptr;
10788 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
10789 return Existing;
10790
10791 // Nope it doesn't. Remove the node from its current place in the maps.
10792 if (InsertPos)
10793 if (!RemoveNodeFromCSEMaps(N))
10794 InsertPos = nullptr;
10795
10796 // Now we update the operands.
10797 N->OperandList[0].set(Op);
10798
10800 // If this gets put into a CSE map, add it.
10801 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10802 return N;
10803}
10804
10806 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
10807
10808 // Check to see if there is no change.
10809 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
10810 return N; // No operands changed, just return the input node.
10811
10812 // See if the modified node already exists.
10813 void *InsertPos = nullptr;
10814 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
10815 return Existing;
10816
10817 // Nope it doesn't. Remove the node from its current place in the maps.
10818 if (InsertPos)
10819 if (!RemoveNodeFromCSEMaps(N))
10820 InsertPos = nullptr;
10821
10822 // Now we update the operands.
10823 if (N->OperandList[0] != Op1)
10824 N->OperandList[0].set(Op1);
10825 if (N->OperandList[1] != Op2)
10826 N->OperandList[1].set(Op2);
10827
10829 // If this gets put into a CSE map, add it.
10830 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10831 return N;
10832}
10833
10836 SDValue Ops[] = { Op1, Op2, Op3 };
10837 return UpdateNodeOperands(N, Ops);
10838}
10839
10842 SDValue Op3, SDValue Op4) {
10843 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
10844 return UpdateNodeOperands(N, Ops);
10845}
10846
10849 SDValue Op3, SDValue Op4, SDValue Op5) {
10850 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
10851 return UpdateNodeOperands(N, Ops);
10852}
10853
10856 unsigned NumOps = Ops.size();
10857 assert(N->getNumOperands() == NumOps &&
10858 "Update with wrong number of operands");
10859
10860 // If no operands changed just return the input node.
10861 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
10862 return N;
10863
10864 // See if the modified node already exists.
10865 void *InsertPos = nullptr;
10866 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
10867 return Existing;
10868
10869 // Nope it doesn't. Remove the node from its current place in the maps.
10870 if (InsertPos)
10871 if (!RemoveNodeFromCSEMaps(N))
10872 InsertPos = nullptr;
10873
10874 // Now we update the operands.
10875 for (unsigned i = 0; i != NumOps; ++i)
10876 if (N->OperandList[i] != Ops[i])
10877 N->OperandList[i].set(Ops[i]);
10878
10880 // If this gets put into a CSE map, add it.
10881 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
10882 return N;
10883}
10884
10885/// DropOperands - Release the operands and set this node to have
10886/// zero operands.
10888 // Unlike the code in MorphNodeTo that does this, we don't need to
10889 // watch for dead nodes here.
10890 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
10891 SDUse &Use = *I++;
10892 Use.set(SDValue());
10893 }
10894}
10895
10897 ArrayRef<MachineMemOperand *> NewMemRefs) {
10898 if (NewMemRefs.empty()) {
10899 N->clearMemRefs();
10900 return;
10901 }
10902
10903 // Check if we can avoid allocating by storing a single reference directly.
10904 if (NewMemRefs.size() == 1) {
10905 N->MemRefs = NewMemRefs[0];
10906 N->NumMemRefs = 1;
10907 return;
10908 }
10909
10910 MachineMemOperand **MemRefsBuffer =
10911 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
10912 llvm::copy(NewMemRefs, MemRefsBuffer);
10913 N->MemRefs = MemRefsBuffer;
10914 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
10915}
10916
10917/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
10918/// machine opcode.
10919///
10921 EVT VT) {
10922 SDVTList VTs = getVTList(VT);
10923 return SelectNodeTo(N, MachineOpc, VTs, {});
10924}
10925
10927 EVT VT, SDValue Op1) {
10928 SDVTList VTs = getVTList(VT);
10929 SDValue Ops[] = { Op1 };
10930 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10931}
10932
10934 EVT VT, SDValue Op1,
10935 SDValue Op2) {
10936 SDVTList VTs = getVTList(VT);
10937 SDValue Ops[] = { Op1, Op2 };
10938 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10939}
10940
10942 EVT VT, SDValue Op1,
10943 SDValue Op2, SDValue Op3) {
10944 SDVTList VTs = getVTList(VT);
10945 SDValue Ops[] = { Op1, Op2, Op3 };
10946 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10947}
10948
10950 EVT VT, ArrayRef<SDValue> Ops) {
10951 SDVTList VTs = getVTList(VT);
10952 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10953}
10954
10956 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
10957 SDVTList VTs = getVTList(VT1, VT2);
10958 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10959}
10960
10962 EVT VT1, EVT VT2) {
10963 SDVTList VTs = getVTList(VT1, VT2);
10964 return SelectNodeTo(N, MachineOpc, VTs, {});
10965}
10966
10968 EVT VT1, EVT VT2, EVT VT3,
10969 ArrayRef<SDValue> Ops) {
10970 SDVTList VTs = getVTList(VT1, VT2, VT3);
10971 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10972}
10973
10975 EVT VT1, EVT VT2,
10976 SDValue Op1, SDValue Op2) {
10977 SDVTList VTs = getVTList(VT1, VT2);
10978 SDValue Ops[] = { Op1, Op2 };
10979 return SelectNodeTo(N, MachineOpc, VTs, Ops);
10980}
10981
10983 SDVTList VTs,ArrayRef<SDValue> Ops) {
10984 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
10985 // Reset the NodeID to -1.
10986 New->setNodeId(-1);
10987 if (New != N) {
10988 ReplaceAllUsesWith(N, New);
10990 }
10991 return New;
10992}
10993
10994/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
10995/// the line number information on the merged node since it is not possible to
10996/// preserve the information that operation is associated with multiple lines.
10997/// This will make the debugger working better at -O0, were there is a higher
10998/// probability having other instructions associated with that line.
10999///
11000/// For IROrder, we keep the smaller of the two
11001SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11002 DebugLoc NLoc = N->getDebugLoc();
11003 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11004 N->setDebugLoc(DebugLoc());
11005 }
11006 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11007 N->setIROrder(Order);
11008 return N;
11009}
11010
11011/// MorphNodeTo - This *mutates* the specified node to have the specified
11012/// return type, opcode, and operands.
11013///
11014/// Note that MorphNodeTo returns the resultant node. If there is already a
11015/// node of the specified opcode and operands, it returns that node instead of
11016/// the current one. Note that the SDLoc need not be the same.
11017///
11018/// Using MorphNodeTo is faster than creating a new node and swapping it in
11019/// with ReplaceAllUsesWith both because it often avoids allocating a new
11020/// node, and because it doesn't require CSE recalculation for any of
11021/// the node's users.
11022///
11023/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11024/// As a consequence it isn't appropriate to use from within the DAG combiner or
11025/// the legalizer which maintain worklists that would need to be updated when
11026/// deleting things.
11028 SDVTList VTs, ArrayRef<SDValue> Ops) {
11029 // If an identical node already exists, use it.
11030 void *IP = nullptr;
11031 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11033 AddNodeIDNode(ID, Opc, VTs, Ops);
11034 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11035 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11036 }
11037
11038 if (!RemoveNodeFromCSEMaps(N))
11039 IP = nullptr;
11040
11041 // Start the morphing.
11042 N->NodeType = Opc;
11043 N->ValueList = VTs.VTs;
11044 N->NumValues = VTs.NumVTs;
11045
11046 // Clear the operands list, updating used nodes to remove this from their
11047 // use list. Keep track of any operands that become dead as a result.
11048 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11049 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11050 SDUse &Use = *I++;
11051 SDNode *Used = Use.getNode();
11052 Use.set(SDValue());
11053 if (Used->use_empty())
11054 DeadNodeSet.insert(Used);
11055 }
11056
11057 // For MachineNode, initialize the memory references information.
11058 if (MachineSDNode *MN = dyn_cast<MachineSDNode>(N))
11059 MN->clearMemRefs();
11060
11061 // Swap for an appropriately sized array from the recycler.
11062 removeOperands(N);
11063 createOperands(N, Ops);
11064
11065 // Delete any nodes that are still dead after adding the uses for the
11066 // new operands.
11067 if (!DeadNodeSet.empty()) {
11068 SmallVector<SDNode *, 16> DeadNodes;
11069 for (SDNode *N : DeadNodeSet)
11070 if (N->use_empty())
11071 DeadNodes.push_back(N);
11072 RemoveDeadNodes(DeadNodes);
11073 }
11074
11075 if (IP)
11076 CSEMap.InsertNode(N, IP); // Memoize the new node.
11077 return N;
11078}
11079
11081 unsigned OrigOpc = Node->getOpcode();
11082 unsigned NewOpc;
11083 switch (OrigOpc) {
11084 default:
11085 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11086#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11087 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11088#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11089 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11090#include "llvm/IR/ConstrainedOps.def"
11091 }
11092
11093 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11094
11095 // We're taking this node out of the chain, so we need to re-link things.
11096 SDValue InputChain = Node->getOperand(0);
11097 SDValue OutputChain = SDValue(Node, 1);
11098 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11099
11101 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11102 Ops.push_back(Node->getOperand(i));
11103
11104 SDVTList VTs = getVTList(Node->getValueType(0));
11105 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11106
11107 // MorphNodeTo can operate in two ways: if an existing node with the
11108 // specified operands exists, it can just return it. Otherwise, it
11109 // updates the node in place to have the requested operands.
11110 if (Res == Node) {
11111 // If we updated the node in place, reset the node ID. To the isel,
11112 // this should be just like a newly allocated machine node.
11113 Res->setNodeId(-1);
11114 } else {
11115 ReplaceAllUsesWith(Node, Res);
11116 RemoveDeadNode(Node);
11117 }
11118
11119 return Res;
11120}
11121
11122/// getMachineNode - These are used for target selectors to create a new node
11123/// with specified return type(s), MachineInstr opcode, and operands.
11124///
11125/// Note that getMachineNode returns the resultant node. If there is already a
11126/// node of the specified opcode and operands, it returns that node instead of
11127/// the current one.
11129 EVT VT) {
11130 SDVTList VTs = getVTList(VT);
11131 return getMachineNode(Opcode, dl, VTs, {});
11132}
11133
11135 EVT VT, SDValue Op1) {
11136 SDVTList VTs = getVTList(VT);
11137 SDValue Ops[] = { Op1 };
11138 return getMachineNode(Opcode, dl, VTs, Ops);
11139}
11140
11142 EVT VT, SDValue Op1, SDValue Op2) {
11143 SDVTList VTs = getVTList(VT);
11144 SDValue Ops[] = { Op1, Op2 };
11145 return getMachineNode(Opcode, dl, VTs, Ops);
11146}
11147
11149 EVT VT, SDValue Op1, SDValue Op2,
11150 SDValue Op3) {
11151 SDVTList VTs = getVTList(VT);
11152 SDValue Ops[] = { Op1, Op2, Op3 };
11153 return getMachineNode(Opcode, dl, VTs, Ops);
11154}
11155
11157 EVT VT, ArrayRef<SDValue> Ops) {
11158 SDVTList VTs = getVTList(VT);
11159 return getMachineNode(Opcode, dl, VTs, Ops);
11160}
11161
11163 EVT VT1, EVT VT2, SDValue Op1,
11164 SDValue Op2) {
11165 SDVTList VTs = getVTList(VT1, VT2);
11166 SDValue Ops[] = { Op1, Op2 };
11167 return getMachineNode(Opcode, dl, VTs, Ops);
11168}
11169
11171 EVT VT1, EVT VT2, SDValue Op1,
11172 SDValue Op2, SDValue Op3) {
11173 SDVTList VTs = getVTList(VT1, VT2);
11174 SDValue Ops[] = { Op1, Op2, Op3 };
11175 return getMachineNode(Opcode, dl, VTs, Ops);
11176}
11177
11179 EVT VT1, EVT VT2,
11180 ArrayRef<SDValue> Ops) {
11181 SDVTList VTs = getVTList(VT1, VT2);
11182 return getMachineNode(Opcode, dl, VTs, Ops);
11183}
11184
11186 EVT VT1, EVT VT2, EVT VT3,
11187 SDValue Op1, SDValue Op2) {
11188 SDVTList VTs = getVTList(VT1, VT2, VT3);
11189 SDValue Ops[] = { Op1, Op2 };
11190 return getMachineNode(Opcode, dl, VTs, Ops);
11191}
11192
11194 EVT VT1, EVT VT2, EVT VT3,
11195 SDValue Op1, SDValue Op2,
11196 SDValue Op3) {
11197 SDVTList VTs = getVTList(VT1, VT2, VT3);
11198 SDValue Ops[] = { Op1, Op2, Op3 };
11199 return getMachineNode(Opcode, dl, VTs, Ops);
11200}
11201
11203 EVT VT1, EVT VT2, EVT VT3,
11204 ArrayRef<SDValue> Ops) {
11205 SDVTList VTs = getVTList(VT1, VT2, VT3);
11206 return getMachineNode(Opcode, dl, VTs, Ops);
11207}
11208
11210 ArrayRef<EVT> ResultTys,
11211 ArrayRef<SDValue> Ops) {
11212 SDVTList VTs = getVTList(ResultTys);
11213 return getMachineNode(Opcode, dl, VTs, Ops);
11214}
11215
11217 SDVTList VTs,
11218 ArrayRef<SDValue> Ops) {
11219 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11221 void *IP = nullptr;
11222
11223 if (DoCSE) {
11225 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11226 IP = nullptr;
11227 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11228 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11229 }
11230 }
11231
11232 // Allocate a new MachineSDNode.
11233 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11234 createOperands(N, Ops);
11235
11236 if (DoCSE)
11237 CSEMap.InsertNode(N, IP);
11238
11239 InsertNode(N);
11240 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11241 return N;
11242}
11243
11244/// getTargetExtractSubreg - A convenience function for creating
11245/// TargetOpcode::EXTRACT_SUBREG nodes.
11247 SDValue Operand) {
11248 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11249 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11250 VT, Operand, SRIdxVal);
11251 return SDValue(Subreg, 0);
11252}
11253
11254/// getTargetInsertSubreg - A convenience function for creating
11255/// TargetOpcode::INSERT_SUBREG nodes.
11257 SDValue Operand, SDValue Subreg) {
11258 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11259 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11260 VT, Operand, Subreg, SRIdxVal);
11261 return SDValue(Result, 0);
11262}
11263
11264/// getNodeIfExists - Get the specified node if it's already available, or
11265/// else return NULL.
11267 ArrayRef<SDValue> Ops) {
11268 SDNodeFlags Flags;
11269 if (Inserter)
11270 Flags = Inserter->getFlags();
11271 return getNodeIfExists(Opcode, VTList, Ops, Flags);
11272}
11273
11276 const SDNodeFlags Flags) {
11277 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11279 AddNodeIDNode(ID, Opcode, VTList, Ops);
11280 void *IP = nullptr;
11281 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP)) {
11282 E->intersectFlagsWith(Flags);
11283 return E;
11284 }
11285 }
11286 return nullptr;
11287}
11288
11289/// doesNodeExist - Check if a node exists without modifying its flags.
11290bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11291 ArrayRef<SDValue> Ops) {
11292 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11294 AddNodeIDNode(ID, Opcode, VTList, Ops);
11295 void *IP = nullptr;
11296 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11297 return true;
11298 }
11299 return false;
11300}
11301
11302/// getDbgValue - Creates a SDDbgValue node.
11303///
11304/// SDNode
11306 SDNode *N, unsigned R, bool IsIndirect,
11307 const DebugLoc &DL, unsigned O) {
11308 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11309 "Expected inlined-at fields to agree");
11310 return new (DbgInfo->getAlloc())
11311 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11312 {}, IsIndirect, DL, O,
11313 /*IsVariadic=*/false);
11314}
11315
11316/// Constant
11318 DIExpression *Expr,
11319 const Value *C,
11320 const DebugLoc &DL, unsigned O) {
11321 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11322 "Expected inlined-at fields to agree");
11323 return new (DbgInfo->getAlloc())
11324 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11325 /*IsIndirect=*/false, DL, O,
11326 /*IsVariadic=*/false);
11327}
11328
11329/// FrameIndex
11331 DIExpression *Expr, unsigned FI,
11332 bool IsIndirect,
11333 const DebugLoc &DL,
11334 unsigned O) {
11335 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11336 "Expected inlined-at fields to agree");
11337 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11338}
11339
11340/// FrameIndex with dependencies
11342 DIExpression *Expr, unsigned FI,
11343 ArrayRef<SDNode *> Dependencies,
11344 bool IsIndirect,
11345 const DebugLoc &DL,
11346 unsigned O) {
11347 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11348 "Expected inlined-at fields to agree");
11349 return new (DbgInfo->getAlloc())
11350 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11351 Dependencies, IsIndirect, DL, O,
11352 /*IsVariadic=*/false);
11353}
11354
11355/// VReg
11357 unsigned VReg, bool IsIndirect,
11358 const DebugLoc &DL, unsigned O) {
11359 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11360 "Expected inlined-at fields to agree");
11361 return new (DbgInfo->getAlloc())
11362 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11363 {}, IsIndirect, DL, O,
11364 /*IsVariadic=*/false);
11365}
11366
11369 ArrayRef<SDNode *> Dependencies,
11370 bool IsIndirect, const DebugLoc &DL,
11371 unsigned O, bool IsVariadic) {
11372 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11373 "Expected inlined-at fields to agree");
11374 return new (DbgInfo->getAlloc())
11375 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11376 DL, O, IsVariadic);
11377}
11378
11380 unsigned OffsetInBits, unsigned SizeInBits,
11381 bool InvalidateDbg) {
11382 SDNode *FromNode = From.getNode();
11383 SDNode *ToNode = To.getNode();
11384 assert(FromNode && ToNode && "Can't modify dbg values");
11385
11386 // PR35338
11387 // TODO: assert(From != To && "Redundant dbg value transfer");
11388 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11389 if (From == To || FromNode == ToNode)
11390 return;
11391
11392 if (!FromNode->getHasDebugValue())
11393 return;
11394
11395 SDDbgOperand FromLocOp =
11396 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11398
11400 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11401 if (Dbg->isInvalidated())
11402 continue;
11403
11404 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11405
11406 // Create a new location ops vector that is equal to the old vector, but
11407 // with each instance of FromLocOp replaced with ToLocOp.
11408 bool Changed = false;
11409 auto NewLocOps = Dbg->copyLocationOps();
11410 std::replace_if(
11411 NewLocOps.begin(), NewLocOps.end(),
11412 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11413 bool Match = Op == FromLocOp;
11414 Changed |= Match;
11415 return Match;
11416 },
11417 ToLocOp);
11418 // Ignore this SDDbgValue if we didn't find a matching location.
11419 if (!Changed)
11420 continue;
11421
11422 DIVariable *Var = Dbg->getVariable();
11423 auto *Expr = Dbg->getExpression();
11424 // If a fragment is requested, update the expression.
11425 if (SizeInBits) {
11426 // When splitting a larger (e.g., sign-extended) value whose
11427 // lower bits are described with an SDDbgValue, do not attempt
11428 // to transfer the SDDbgValue to the upper bits.
11429 if (auto FI = Expr->getFragmentInfo())
11430 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11431 continue;
11432 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11433 SizeInBits);
11434 if (!Fragment)
11435 continue;
11436 Expr = *Fragment;
11437 }
11438
11439 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11440 // Clone the SDDbgValue and move it to To.
11441 SDDbgValue *Clone = getDbgValueList(
11442 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11443 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11444 Dbg->isVariadic());
11445 ClonedDVs.push_back(Clone);
11446
11447 if (InvalidateDbg) {
11448 // Invalidate value and indicate the SDDbgValue should not be emitted.
11449 Dbg->setIsInvalidated();
11450 Dbg->setIsEmitted();
11451 }
11452 }
11453
11454 for (SDDbgValue *Dbg : ClonedDVs) {
11455 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11456 "Transferred DbgValues should depend on the new SDNode");
11457 AddDbgValue(Dbg, false);
11458 }
11459}
11460
11462 if (!N.getHasDebugValue())
11463 return;
11464
11465 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11466 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11467 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11468 return SDDbgOperand::fromNode(Node, ResNo);
11469 };
11470
11472 for (auto *DV : GetDbgValues(&N)) {
11473 if (DV->isInvalidated())
11474 continue;
11475 switch (N.getOpcode()) {
11476 default:
11477 break;
11478 case ISD::ADD: {
11479 SDValue N0 = N.getOperand(0);
11480 SDValue N1 = N.getOperand(1);
11481 if (!isa<ConstantSDNode>(N0)) {
11482 bool RHSConstant = isa<ConstantSDNode>(N1);
11484 if (RHSConstant)
11485 Offset = N.getConstantOperandVal(1);
11486 // We are not allowed to turn indirect debug values variadic, so
11487 // don't salvage those.
11488 if (!RHSConstant && DV->isIndirect())
11489 continue;
11490
11491 // Rewrite an ADD constant node into a DIExpression. Since we are
11492 // performing arithmetic to compute the variable's *value* in the
11493 // DIExpression, we need to mark the expression with a
11494 // DW_OP_stack_value.
11495 auto *DIExpr = DV->getExpression();
11496 auto NewLocOps = DV->copyLocationOps();
11497 bool Changed = false;
11498 size_t OrigLocOpsSize = NewLocOps.size();
11499 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11500 // We're not given a ResNo to compare against because the whole
11501 // node is going away. We know that any ISD::ADD only has one
11502 // result, so we can assume any node match is using the result.
11503 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11504 NewLocOps[i].getSDNode() != &N)
11505 continue;
11506 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
11507 if (RHSConstant) {
11510 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
11511 } else {
11512 // Convert to a variadic expression (if not already).
11513 // convertToVariadicExpression() returns a const pointer, so we use
11514 // a temporary const variable here.
11515 const auto *TmpDIExpr =
11519 ExprOps.push_back(NewLocOps.size());
11520 ExprOps.push_back(dwarf::DW_OP_plus);
11523 NewLocOps.push_back(RHS);
11524 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
11525 }
11526 Changed = true;
11527 }
11528 (void)Changed;
11529 assert(Changed && "Salvage target doesn't use N");
11530
11531 bool IsVariadic =
11532 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
11533
11534 auto AdditionalDependencies = DV->getAdditionalDependencies();
11535 SDDbgValue *Clone = getDbgValueList(
11536 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
11537 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
11538 ClonedDVs.push_back(Clone);
11539 DV->setIsInvalidated();
11540 DV->setIsEmitted();
11541 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
11542 N0.getNode()->dumprFull(this);
11543 dbgs() << " into " << *DIExpr << '\n');
11544 }
11545 break;
11546 }
11547 case ISD::TRUNCATE: {
11548 SDValue N0 = N.getOperand(0);
11549 TypeSize FromSize = N0.getValueSizeInBits();
11550 TypeSize ToSize = N.getValueSizeInBits(0);
11551
11552 DIExpression *DbgExpression = DV->getExpression();
11553 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
11554 auto NewLocOps = DV->copyLocationOps();
11555 bool Changed = false;
11556 for (size_t i = 0; i < NewLocOps.size(); ++i) {
11557 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11558 NewLocOps[i].getSDNode() != &N)
11559 continue;
11560
11561 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
11562 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
11563 Changed = true;
11564 }
11565 assert(Changed && "Salvage target doesn't use N");
11566 (void)Changed;
11567
11568 SDDbgValue *Clone =
11569 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
11570 DV->getAdditionalDependencies(), DV->isIndirect(),
11571 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
11572
11573 ClonedDVs.push_back(Clone);
11574 DV->setIsInvalidated();
11575 DV->setIsEmitted();
11576 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
11577 dbgs() << " into " << *DbgExpression << '\n');
11578 break;
11579 }
11580 }
11581 }
11582
11583 for (SDDbgValue *Dbg : ClonedDVs) {
11584 assert((!Dbg->getSDNodes().empty() ||
11585 llvm::any_of(Dbg->getLocationOps(),
11586 [&](const SDDbgOperand &Op) {
11587 return Op.getKind() == SDDbgOperand::FRAMEIX;
11588 })) &&
11589 "Salvaged DbgValue should depend on a new SDNode");
11590 AddDbgValue(Dbg, false);
11591 }
11592}
11593
11594/// Creates a SDDbgLabel node.
11596 const DebugLoc &DL, unsigned O) {
11597 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
11598 "Expected inlined-at fields to agree");
11599 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
11600}
11601
11602namespace {
11603
11604/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
11605/// pointed to by a use iterator is deleted, increment the use iterator
11606/// so that it doesn't dangle.
11607///
11608class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
11611
11612 void NodeDeleted(SDNode *N, SDNode *E) override {
11613 // Increment the iterator as needed.
11614 while (UI != UE && N == UI->getUser())
11615 ++UI;
11616 }
11617
11618public:
11619 RAUWUpdateListener(SelectionDAG &d,
11622 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
11623};
11624
11625} // end anonymous namespace
11626
11627/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11628/// This can cause recursive merging of nodes in the DAG.
11629///
11630/// This version assumes From has a single result value.
11631///
11633 SDNode *From = FromN.getNode();
11634 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
11635 "Cannot replace with this method!");
11636 assert(From != To.getNode() && "Cannot replace uses of with self");
11637
11638 // Preserve Debug Values
11639 transferDbgValues(FromN, To);
11640 // Preserve extra info.
11641 copyExtraInfo(From, To.getNode());
11642
11643 // Iterate over all the existing uses of From. New uses will be added
11644 // to the beginning of the use list, which we avoid visiting.
11645 // This specifically avoids visiting uses of From that arise while the
11646 // replacement is happening, because any such uses would be the result
11647 // of CSE: If an existing node looks like From after one of its operands
11648 // is replaced by To, we don't want to replace of all its users with To
11649 // too. See PR3018 for more info.
11650 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11651 RAUWUpdateListener Listener(*this, UI, UE);
11652 while (UI != UE) {
11653 SDNode *User = UI->getUser();
11654
11655 // This node is about to morph, remove its old self from the CSE maps.
11656 RemoveNodeFromCSEMaps(User);
11657
11658 // A user can appear in a use list multiple times, and when this
11659 // happens the uses are usually next to each other in the list.
11660 // To help reduce the number of CSE recomputations, process all
11661 // the uses of this user that we can find this way.
11662 do {
11663 SDUse &Use = *UI;
11664 ++UI;
11665 Use.set(To);
11666 if (To->isDivergent() != From->isDivergent())
11668 } while (UI != UE && UI->getUser() == User);
11669 // Now that we have modified User, add it back to the CSE maps. If it
11670 // already exists there, recursively merge the results together.
11671 AddModifiedNodeToCSEMaps(User);
11672 }
11673
11674 // If we just RAUW'd the root, take note.
11675 if (FromN == getRoot())
11676 setRoot(To);
11677}
11678
11679/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11680/// This can cause recursive merging of nodes in the DAG.
11681///
11682/// This version assumes that for each value of From, there is a
11683/// corresponding value in To in the same position with the same type.
11684///
11686#ifndef NDEBUG
11687 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11688 assert((!From->hasAnyUseOfValue(i) ||
11689 From->getValueType(i) == To->getValueType(i)) &&
11690 "Cannot use this version of ReplaceAllUsesWith!");
11691#endif
11692
11693 // Handle the trivial case.
11694 if (From == To)
11695 return;
11696
11697 // Preserve Debug Info. Only do this if there's a use.
11698 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
11699 if (From->hasAnyUseOfValue(i)) {
11700 assert((i < To->getNumValues()) && "Invalid To location");
11702 }
11703 // Preserve extra info.
11704 copyExtraInfo(From, To);
11705
11706 // Iterate over just the existing users of From. See the comments in
11707 // the ReplaceAllUsesWith above.
11708 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11709 RAUWUpdateListener Listener(*this, UI, UE);
11710 while (UI != UE) {
11711 SDNode *User = UI->getUser();
11712
11713 // This node is about to morph, remove its old self from the CSE maps.
11714 RemoveNodeFromCSEMaps(User);
11715
11716 // A user can appear in a use list multiple times, and when this
11717 // happens the uses are usually next to each other in the list.
11718 // To help reduce the number of CSE recomputations, process all
11719 // the uses of this user that we can find this way.
11720 do {
11721 SDUse &Use = *UI;
11722 ++UI;
11723 Use.setNode(To);
11724 if (To->isDivergent() != From->isDivergent())
11726 } while (UI != UE && UI->getUser() == User);
11727
11728 // Now that we have modified User, add it back to the CSE maps. If it
11729 // already exists there, recursively merge the results together.
11730 AddModifiedNodeToCSEMaps(User);
11731 }
11732
11733 // If we just RAUW'd the root, take note.
11734 if (From == getRoot().getNode())
11735 setRoot(SDValue(To, getRoot().getResNo()));
11736}
11737
11738/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
11739/// This can cause recursive merging of nodes in the DAG.
11740///
11741/// This version can replace From with any result values. To must match the
11742/// number and types of values returned by From.
11744 if (From->getNumValues() == 1) // Handle the simple case efficiently.
11745 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
11746
11747 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
11748 // Preserve Debug Info.
11749 transferDbgValues(SDValue(From, i), To[i]);
11750 // Preserve extra info.
11751 copyExtraInfo(From, To[i].getNode());
11752 }
11753
11754 // Iterate over just the existing users of From. See the comments in
11755 // the ReplaceAllUsesWith above.
11756 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
11757 RAUWUpdateListener Listener(*this, UI, UE);
11758 while (UI != UE) {
11759 SDNode *User = UI->getUser();
11760
11761 // This node is about to morph, remove its old self from the CSE maps.
11762 RemoveNodeFromCSEMaps(User);
11763
11764 // A user can appear in a use list multiple times, and when this happens the
11765 // uses are usually next to each other in the list. To help reduce the
11766 // number of CSE and divergence recomputations, process all the uses of this
11767 // user that we can find this way.
11768 bool To_IsDivergent = false;
11769 do {
11770 SDUse &Use = *UI;
11771 const SDValue &ToOp = To[Use.getResNo()];
11772 ++UI;
11773 Use.set(ToOp);
11774 To_IsDivergent |= ToOp->isDivergent();
11775 } while (UI != UE && UI->getUser() == User);
11776
11777 if (To_IsDivergent != From->isDivergent())
11779
11780 // Now that we have modified User, add it back to the CSE maps. If it
11781 // already exists there, recursively merge the results together.
11782 AddModifiedNodeToCSEMaps(User);
11783 }
11784
11785 // If we just RAUW'd the root, take note.
11786 if (From == getRoot().getNode())
11787 setRoot(SDValue(To[getRoot().getResNo()]));
11788}
11789
11790/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
11791/// uses of other values produced by From.getNode() alone. The Deleted
11792/// vector is handled the same way as for ReplaceAllUsesWith.
11794 // Handle the really simple, really trivial case efficiently.
11795 if (From == To) return;
11796
11797 // Handle the simple, trivial, case efficiently.
11798 if (From.getNode()->getNumValues() == 1) {
11800 return;
11801 }
11802
11803 // Preserve Debug Info.
11805 copyExtraInfo(From.getNode(), To.getNode());
11806
11807 // Iterate over just the existing users of From. See the comments in
11808 // the ReplaceAllUsesWith above.
11809 SDNode::use_iterator UI = From.getNode()->use_begin(),
11810 UE = From.getNode()->use_end();
11811 RAUWUpdateListener Listener(*this, UI, UE);
11812 while (UI != UE) {
11813 SDNode *User = UI->getUser();
11814 bool UserRemovedFromCSEMaps = false;
11815
11816 // A user can appear in a use list multiple times, and when this
11817 // happens the uses are usually next to each other in the list.
11818 // To help reduce the number of CSE recomputations, process all
11819 // the uses of this user that we can find this way.
11820 do {
11821 SDUse &Use = *UI;
11822
11823 // Skip uses of different values from the same node.
11824 if (Use.getResNo() != From.getResNo()) {
11825 ++UI;
11826 continue;
11827 }
11828
11829 // If this node hasn't been modified yet, it's still in the CSE maps,
11830 // so remove its old self from the CSE maps.
11831 if (!UserRemovedFromCSEMaps) {
11832 RemoveNodeFromCSEMaps(User);
11833 UserRemovedFromCSEMaps = true;
11834 }
11835
11836 ++UI;
11837 Use.set(To);
11838 if (To->isDivergent() != From->isDivergent())
11840 } while (UI != UE && UI->getUser() == User);
11841 // We are iterating over all uses of the From node, so if a use
11842 // doesn't use the specific value, no changes are made.
11843 if (!UserRemovedFromCSEMaps)
11844 continue;
11845
11846 // Now that we have modified User, add it back to the CSE maps. If it
11847 // already exists there, recursively merge the results together.
11848 AddModifiedNodeToCSEMaps(User);
11849 }
11850
11851 // If we just RAUW'd the root, take note.
11852 if (From == getRoot())
11853 setRoot(To);
11854}
11855
11856namespace {
11857
11858/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
11859/// to record information about a use.
11860struct UseMemo {
11861 SDNode *User;
11862 unsigned Index;
11863 SDUse *Use;
11864};
11865
11866/// operator< - Sort Memos by User.
11867bool operator<(const UseMemo &L, const UseMemo &R) {
11868 return (intptr_t)L.User < (intptr_t)R.User;
11869}
11870
11871/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
11872/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
11873/// the node already has been taken care of recursively.
11874class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
11876
11877 void NodeDeleted(SDNode *N, SDNode *E) override {
11878 for (UseMemo &Memo : Uses)
11879 if (Memo.User == N)
11880 Memo.User = nullptr;
11881 }
11882
11883public:
11884 RAUOVWUpdateListener(SelectionDAG &d, SmallVector<UseMemo, 4> &uses)
11885 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
11886};
11887
11888} // end anonymous namespace
11889
11890/// Return true if a glue output should propagate divergence information.
11892 switch (Node->getOpcode()) {
11893 case ISD::CopyFromReg:
11894 case ISD::CopyToReg:
11895 return false;
11896 default:
11897 return true;
11898 }
11899
11900 llvm_unreachable("covered opcode switch");
11901}
11902
11904 if (TLI->isSDNodeAlwaysUniform(N)) {
11905 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
11906 "Conflicting divergence information!");
11907 return false;
11908 }
11909 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
11910 return true;
11911 for (const auto &Op : N->ops()) {
11912 EVT VT = Op.getValueType();
11913
11914 // Skip Chain. It does not carry divergence.
11915 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
11916 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
11917 return true;
11918 }
11919 return false;
11920}
11921
11923 SmallVector<SDNode *, 16> Worklist(1, N);
11924 do {
11925 N = Worklist.pop_back_val();
11926 bool IsDivergent = calculateDivergence(N);
11927 if (N->SDNodeBits.IsDivergent != IsDivergent) {
11928 N->SDNodeBits.IsDivergent = IsDivergent;
11929 llvm::append_range(Worklist, N->users());
11930 }
11931 } while (!Worklist.empty());
11932}
11933
11934void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
11936 Order.reserve(AllNodes.size());
11937 for (auto &N : allnodes()) {
11938 unsigned NOps = N.getNumOperands();
11939 Degree[&N] = NOps;
11940 if (0 == NOps)
11941 Order.push_back(&N);
11942 }
11943 for (size_t I = 0; I != Order.size(); ++I) {
11944 SDNode *N = Order[I];
11945 for (auto *U : N->users()) {
11946 unsigned &UnsortedOps = Degree[U];
11947 if (0 == --UnsortedOps)
11948 Order.push_back(U);
11949 }
11950 }
11951}
11952
11953#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
11954void SelectionDAG::VerifyDAGDivergence() {
11955 std::vector<SDNode *> TopoOrder;
11956 CreateTopologicalOrder(TopoOrder);
11957 for (auto *N : TopoOrder) {
11958 assert(calculateDivergence(N) == N->isDivergent() &&
11959 "Divergence bit inconsistency detected");
11960 }
11961}
11962#endif
11963
11964/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
11965/// uses of other values produced by From.getNode() alone. The same value
11966/// may appear in both the From and To list. The Deleted vector is
11967/// handled the same way as for ReplaceAllUsesWith.
11969 const SDValue *To,
11970 unsigned Num){
11971 // Handle the simple, trivial case efficiently.
11972 if (Num == 1)
11973 return ReplaceAllUsesOfValueWith(*From, *To);
11974
11975 transferDbgValues(*From, *To);
11976 copyExtraInfo(From->getNode(), To->getNode());
11977
11978 // Read up all the uses and make records of them. This helps
11979 // processing new uses that are introduced during the
11980 // replacement process.
11982 for (unsigned i = 0; i != Num; ++i) {
11983 unsigned FromResNo = From[i].getResNo();
11984 SDNode *FromNode = From[i].getNode();
11985 for (SDUse &Use : FromNode->uses()) {
11986 if (Use.getResNo() == FromResNo) {
11987 UseMemo Memo = {Use.getUser(), i, &Use};
11988 Uses.push_back(Memo);
11989 }
11990 }
11991 }
11992
11993 // Sort the uses, so that all the uses from a given User are together.
11995 RAUOVWUpdateListener Listener(*this, Uses);
11996
11997 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
11998 UseIndex != UseIndexEnd; ) {
11999 // We know that this user uses some value of From. If it is the right
12000 // value, update it.
12001 SDNode *User = Uses[UseIndex].User;
12002 // If the node has been deleted by recursive CSE updates when updating
12003 // another node, then just skip this entry.
12004 if (User == nullptr) {
12005 ++UseIndex;
12006 continue;
12007 }
12008
12009 // This node is about to morph, remove its old self from the CSE maps.
12010 RemoveNodeFromCSEMaps(User);
12011
12012 // The Uses array is sorted, so all the uses for a given User
12013 // are next to each other in the list.
12014 // To help reduce the number of CSE recomputations, process all
12015 // the uses of this user that we can find this way.
12016 do {
12017 unsigned i = Uses[UseIndex].Index;
12018 SDUse &Use = *Uses[UseIndex].Use;
12019 ++UseIndex;
12020
12021 Use.set(To[i]);
12022 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12023
12024 // Now that we have modified User, add it back to the CSE maps. If it
12025 // already exists there, recursively merge the results together.
12026 AddModifiedNodeToCSEMaps(User);
12027 }
12028}
12029
12030/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12031/// based on their topological order. It returns the maximum id and a vector
12032/// of the SDNodes* in assigned order by reference.
12034 unsigned DAGSize = 0;
12035
12036 // SortedPos tracks the progress of the algorithm. Nodes before it are
12037 // sorted, nodes after it are unsorted. When the algorithm completes
12038 // it is at the end of the list.
12039 allnodes_iterator SortedPos = allnodes_begin();
12040
12041 // Visit all the nodes. Move nodes with no operands to the front of
12042 // the list immediately. Annotate nodes that do have operands with their
12043 // operand count. Before we do this, the Node Id fields of the nodes
12044 // may contain arbitrary values. After, the Node Id fields for nodes
12045 // before SortedPos will contain the topological sort index, and the
12046 // Node Id fields for nodes At SortedPos and after will contain the
12047 // count of outstanding operands.
12049 checkForCycles(&N, this);
12050 unsigned Degree = N.getNumOperands();
12051 if (Degree == 0) {
12052 // A node with no uses, add it to the result array immediately.
12053 N.setNodeId(DAGSize++);
12054 allnodes_iterator Q(&N);
12055 if (Q != SortedPos)
12056 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12057 assert(SortedPos != AllNodes.end() && "Overran node list");
12058 ++SortedPos;
12059 } else {
12060 // Temporarily use the Node Id as scratch space for the degree count.
12061 N.setNodeId(Degree);
12062 }
12063 }
12064
12065 // Visit all the nodes. As we iterate, move nodes into sorted order,
12066 // such that by the time the end is reached all nodes will be sorted.
12067 for (SDNode &Node : allnodes()) {
12068 SDNode *N = &Node;
12069 checkForCycles(N, this);
12070 // N is in sorted position, so all its uses have one less operand
12071 // that needs to be sorted.
12072 for (SDNode *P : N->users()) {
12073 unsigned Degree = P->getNodeId();
12074 assert(Degree != 0 && "Invalid node degree");
12075 --Degree;
12076 if (Degree == 0) {
12077 // All of P's operands are sorted, so P may sorted now.
12078 P->setNodeId(DAGSize++);
12079 if (P->getIterator() != SortedPos)
12080 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12081 assert(SortedPos != AllNodes.end() && "Overran node list");
12082 ++SortedPos;
12083 } else {
12084 // Update P's outstanding operand count.
12085 P->setNodeId(Degree);
12086 }
12087 }
12088 if (Node.getIterator() == SortedPos) {
12089#ifndef NDEBUG
12091 SDNode *S = &*++I;
12092 dbgs() << "Overran sorted position:\n";
12093 S->dumprFull(this); dbgs() << "\n";
12094 dbgs() << "Checking if this is due to cycles\n";
12095 checkForCycles(this, true);
12096#endif
12097 llvm_unreachable(nullptr);
12098 }
12099 }
12100
12101 assert(SortedPos == AllNodes.end() &&
12102 "Topological sort incomplete!");
12103 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12104 "First node in topological sort is not the entry token!");
12105 assert(AllNodes.front().getNodeId() == 0 &&
12106 "First node in topological sort has non-zero id!");
12107 assert(AllNodes.front().getNumOperands() == 0 &&
12108 "First node in topological sort has operands!");
12109 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12110 "Last node in topologic sort has unexpected id!");
12111 assert(AllNodes.back().use_empty() &&
12112 "Last node in topologic sort has users!");
12113 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12114 return DAGSize;
12115}
12116
12117/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12118/// value is produced by SD.
12119void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12120 for (SDNode *SD : DB->getSDNodes()) {
12121 if (!SD)
12122 continue;
12123 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12124 SD->setHasDebugValue(true);
12125 }
12126 DbgInfo->add(DB, isParameter);
12127}
12128
12129void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12130
12132 SDValue NewMemOpChain) {
12133 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12134 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12135 // The new memory operation must have the same position as the old load in
12136 // terms of memory dependency. Create a TokenFactor for the old load and new
12137 // memory operation and update uses of the old load's output chain to use that
12138 // TokenFactor.
12139 if (OldChain == NewMemOpChain || OldChain.use_empty())
12140 return NewMemOpChain;
12141
12142 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12143 OldChain, NewMemOpChain);
12144 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12145 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12146 return TokenFactor;
12147}
12148
12150 SDValue NewMemOp) {
12151 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12152 SDValue OldChain = SDValue(OldLoad, 1);
12153 SDValue NewMemOpChain = NewMemOp.getValue(1);
12154 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12155}
12156
12158 Function **OutFunction) {
12159 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12160
12161 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12162 auto *Module = MF->getFunction().getParent();
12163 auto *Function = Module->getFunction(Symbol);
12164
12165 if (OutFunction != nullptr)
12166 *OutFunction = Function;
12167
12168 if (Function != nullptr) {
12169 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12170 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12171 }
12172
12173 std::string ErrorStr;
12174 raw_string_ostream ErrorFormatter(ErrorStr);
12175 ErrorFormatter << "Undefined external symbol ";
12176 ErrorFormatter << '"' << Symbol << '"';
12177 report_fatal_error(Twine(ErrorStr));
12178}
12179
12180//===----------------------------------------------------------------------===//
12181// SDNode Class
12182//===----------------------------------------------------------------------===//
12183
12185 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12186 return Const != nullptr && Const->isZero();
12187}
12188
12190 return V.isUndef() || isNullConstant(V);
12191}
12192
12194 ConstantFPSDNode *Const = dyn_cast<ConstantFPSDNode>(V);
12195 return Const != nullptr && Const->isZero() && !Const->isNegative();
12196}
12197
12199 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12200 return Const != nullptr && Const->isAllOnes();
12201}
12202
12204 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12205 return Const != nullptr && Const->isOne();
12206}
12207
12209 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V);
12210 return Const != nullptr && Const->isMinSignedValue();
12211}
12212
12213bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12214 unsigned OperandNo) {
12215 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12216 // TODO: Target-specific opcodes could be added.
12217 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12218 /*AllowTruncation*/ true)) {
12219 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12220 switch (Opcode) {
12221 case ISD::ADD:
12222 case ISD::OR:
12223 case ISD::XOR:
12224 case ISD::UMAX:
12225 return Const.isZero();
12226 case ISD::MUL:
12227 return Const.isOne();
12228 case ISD::AND:
12229 case ISD::UMIN:
12230 return Const.isAllOnes();
12231 case ISD::SMAX:
12232 return Const.isMinSignedValue();
12233 case ISD::SMIN:
12234 return Const.isMaxSignedValue();
12235 case ISD::SUB:
12236 case ISD::SHL:
12237 case ISD::SRA:
12238 case ISD::SRL:
12239 return OperandNo == 1 && Const.isZero();
12240 case ISD::UDIV:
12241 case ISD::SDIV:
12242 return OperandNo == 1 && Const.isOne();
12243 }
12244 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12245 switch (Opcode) {
12246 case ISD::FADD:
12247 return ConstFP->isZero() &&
12248 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12249 case ISD::FSUB:
12250 return OperandNo == 1 && ConstFP->isZero() &&
12251 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12252 case ISD::FMUL:
12253 return ConstFP->isExactlyValue(1.0);
12254 case ISD::FDIV:
12255 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12256 case ISD::FMINNUM:
12257 case ISD::FMAXNUM: {
12258 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12259 EVT VT = V.getValueType();
12260 const fltSemantics &Semantics = VT.getFltSemantics();
12261 APFloat NeutralAF = !Flags.hasNoNaNs()
12262 ? APFloat::getQNaN(Semantics)
12263 : !Flags.hasNoInfs()
12264 ? APFloat::getInf(Semantics)
12265 : APFloat::getLargest(Semantics);
12266 if (Opcode == ISD::FMAXNUM)
12267 NeutralAF.changeSign();
12268
12269 return ConstFP->isExactlyValue(NeutralAF);
12270 }
12271 }
12272 }
12273 return false;
12274}
12275
12277 while (V.getOpcode() == ISD::BITCAST)
12278 V = V.getOperand(0);
12279 return V;
12280}
12281
12283 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12284 V = V.getOperand(0);
12285 return V;
12286}
12287
12289 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12290 V = V.getOperand(0);
12291 return V;
12292}
12293
12295 while (V.getOpcode() == ISD::TRUNCATE)
12296 V = V.getOperand(0);
12297 return V;
12298}
12299
12300bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12301 if (V.getOpcode() != ISD::XOR)
12302 return false;
12303 V = peekThroughBitcasts(V.getOperand(1));
12304 unsigned NumBits = V.getScalarValueSizeInBits();
12305 ConstantSDNode *C =
12306 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12307 return C && (C->getAPIntValue().countr_one() >= NumBits);
12308}
12309
12311 bool AllowTruncation) {
12312 EVT VT = N.getValueType();
12313 APInt DemandedElts = VT.isFixedLengthVector()
12315 : APInt(1, 1);
12316 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12317}
12318
12320 bool AllowUndefs,
12321 bool AllowTruncation) {
12322 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
12323 return CN;
12324
12325 // SplatVectors can truncate their operands. Ignore that case here unless
12326 // AllowTruncation is set.
12327 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12328 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12329 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12330 EVT CVT = CN->getValueType(0);
12331 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12332 if (AllowTruncation || CVT == VecEltVT)
12333 return CN;
12334 }
12335 }
12336
12337 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12338 BitVector UndefElements;
12339 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12340
12341 // BuildVectors can truncate their operands. Ignore that case here unless
12342 // AllowTruncation is set.
12343 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12344 if (CN && (UndefElements.none() || AllowUndefs)) {
12345 EVT CVT = CN->getValueType(0);
12346 EVT NSVT = N.getValueType().getScalarType();
12347 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12348 if (AllowTruncation || (CVT == NSVT))
12349 return CN;
12350 }
12351 }
12352
12353 return nullptr;
12354}
12355
12357 EVT VT = N.getValueType();
12358 APInt DemandedElts = VT.isFixedLengthVector()
12360 : APInt(1, 1);
12361 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12362}
12363
12365 const APInt &DemandedElts,
12366 bool AllowUndefs) {
12367 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
12368 return CN;
12369
12370 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
12371 BitVector UndefElements;
12372 ConstantFPSDNode *CN =
12373 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12374 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12375 if (CN && (UndefElements.none() || AllowUndefs))
12376 return CN;
12377 }
12378
12379 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12380 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12381 return CN;
12382
12383 return nullptr;
12384}
12385
12386bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12387 // TODO: may want to use peekThroughBitcast() here.
12388 ConstantSDNode *C =
12389 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12390 return C && C->isZero();
12391}
12392
12393bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12394 ConstantSDNode *C =
12395 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12396 return C && C->isOne();
12397}
12398
12399bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12401 unsigned BitWidth = N.getScalarValueSizeInBits();
12402 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12403 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12404}
12405
12407 DropOperands();
12408}
12409
12410MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12411 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12412 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12413 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12414 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12415 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12416 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12417
12418 // We check here that the size of the memory operand fits within the size of
12419 // the MMO. This is because the MMO might indicate only a possible address
12420 // range instead of specifying the affected memory addresses precisely.
12421 assert(
12422 (!MMO->getType().isValid() ||
12424 "Size mismatch!");
12425}
12426
12427/// Profile - Gather unique data for the node.
12428///
12430 AddNodeIDNode(ID, this);
12431}
12432
12433namespace {
12434
12435 struct EVTArray {
12436 std::vector<EVT> VTs;
12437
12438 EVTArray() {
12439 VTs.reserve(MVT::VALUETYPE_SIZE);
12440 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
12441 VTs.push_back(MVT((MVT::SimpleValueType)i));
12442 }
12443 };
12444
12445} // end anonymous namespace
12446
12447/// getValueTypeList - Return a pointer to the specified value type.
12448///
12449const EVT *SDNode::getValueTypeList(MVT VT) {
12450 static EVTArray SimpleVTArray;
12451
12452 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
12453 return &SimpleVTArray.VTs[VT.SimpleTy];
12454}
12455
12456/// hasNUsesOfValue - Return true if there are exactly NUSES uses of the
12457/// indicated value. This method ignores uses of other values defined by this
12458/// operation.
12459bool SDNode::hasNUsesOfValue(unsigned NUses, unsigned Value) const {
12460 assert(Value < getNumValues() && "Bad value!");
12461
12462 // TODO: Only iterate over uses of a given value of the node
12463 for (SDUse &U : uses()) {
12464 if (U.getResNo() == Value) {
12465 if (NUses == 0)
12466 return false;
12467 --NUses;
12468 }
12469 }
12470
12471 // Found exactly the right number of uses?
12472 return NUses == 0;
12473}
12474
12475/// hasAnyUseOfValue - Return true if there are any use of the indicated
12476/// value. This method ignores uses of other values defined by this operation.
12477bool SDNode::hasAnyUseOfValue(unsigned Value) const {
12478 assert(Value < getNumValues() && "Bad value!");
12479
12480 for (SDUse &U : uses())
12481 if (U.getResNo() == Value)
12482 return true;
12483
12484 return false;
12485}
12486
12487/// isOnlyUserOf - Return true if this node is the only use of N.
12488bool SDNode::isOnlyUserOf(const SDNode *N) const {
12489 bool Seen = false;
12490 for (const SDNode *User : N->users()) {
12491 if (User == this)
12492 Seen = true;
12493 else
12494 return false;
12495 }
12496
12497 return Seen;
12498}
12499
12500/// Return true if the only users of N are contained in Nodes.
12502 bool Seen = false;
12503 for (const SDNode *User : N->users()) {
12504 if (llvm::is_contained(Nodes, User))
12505 Seen = true;
12506 else
12507 return false;
12508 }
12509
12510 return Seen;
12511}
12512
12513/// isOperand - Return true if this node is an operand of N.
12514bool SDValue::isOperandOf(const SDNode *N) const {
12515 return is_contained(N->op_values(), *this);
12516}
12517
12518bool SDNode::isOperandOf(const SDNode *N) const {
12519 return any_of(N->op_values(),
12520 [this](SDValue Op) { return this == Op.getNode(); });
12521}
12522
12523/// reachesChainWithoutSideEffects - Return true if this operand (which must
12524/// be a chain) reaches the specified operand without crossing any
12525/// side-effecting instructions on any chain path. In practice, this looks
12526/// through token factors and non-volatile loads. In order to remain efficient,
12527/// this only looks a couple of nodes in, it does not do an exhaustive search.
12528///
12529/// Note that we only need to examine chains when we're searching for
12530/// side-effects; SelectionDAG requires that all side-effects are represented
12531/// by chains, even if another operand would force a specific ordering. This
12532/// constraint is necessary to allow transformations like splitting loads.
12534 unsigned Depth) const {
12535 if (*this == Dest) return true;
12536
12537 // Don't search too deeply, we just want to be able to see through
12538 // TokenFactor's etc.
12539 if (Depth == 0) return false;
12540
12541 // If this is a token factor, all inputs to the TF happen in parallel.
12542 if (getOpcode() == ISD::TokenFactor) {
12543 // First, try a shallow search.
12544 if (is_contained((*this)->ops(), Dest)) {
12545 // We found the chain we want as an operand of this TokenFactor.
12546 // Essentially, we reach the chain without side-effects if we could
12547 // serialize the TokenFactor into a simple chain of operations with
12548 // Dest as the last operation. This is automatically true if the
12549 // chain has one use: there are no other ordering constraints.
12550 // If the chain has more than one use, we give up: some other
12551 // use of Dest might force a side-effect between Dest and the current
12552 // node.
12553 if (Dest.hasOneUse())
12554 return true;
12555 }
12556 // Next, try a deep search: check whether every operand of the TokenFactor
12557 // reaches Dest.
12558 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
12559 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
12560 });
12561 }
12562
12563 // Loads don't have side effects, look through them.
12564 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
12565 if (Ld->isUnordered())
12566 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
12567 }
12568 return false;
12569}
12570
12571bool SDNode::hasPredecessor(const SDNode *N) const {
12574 Worklist.push_back(this);
12575 return hasPredecessorHelper(N, Visited, Worklist);
12576}
12577
12579 this->Flags &= Flags;
12580}
12581
12582SDValue
12584 ArrayRef<ISD::NodeType> CandidateBinOps,
12585 bool AllowPartials) {
12586 // The pattern must end in an extract from index 0.
12587 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
12588 !isNullConstant(Extract->getOperand(1)))
12589 return SDValue();
12590
12591 // Match against one of the candidate binary ops.
12592 SDValue Op = Extract->getOperand(0);
12593 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
12594 return Op.getOpcode() == unsigned(BinOp);
12595 }))
12596 return SDValue();
12597
12598 // Floating-point reductions may require relaxed constraints on the final step
12599 // of the reduction because they may reorder intermediate operations.
12600 unsigned CandidateBinOp = Op.getOpcode();
12601 if (Op.getValueType().isFloatingPoint()) {
12602 SDNodeFlags Flags = Op->getFlags();
12603 switch (CandidateBinOp) {
12604 case ISD::FADD:
12605 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
12606 return SDValue();
12607 break;
12608 default:
12609 llvm_unreachable("Unhandled FP opcode for binop reduction");
12610 }
12611 }
12612
12613 // Matching failed - attempt to see if we did enough stages that a partial
12614 // reduction from a subvector is possible.
12615 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
12616 if (!AllowPartials || !Op)
12617 return SDValue();
12618 EVT OpVT = Op.getValueType();
12619 EVT OpSVT = OpVT.getScalarType();
12620 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
12621 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
12622 return SDValue();
12623 BinOp = (ISD::NodeType)CandidateBinOp;
12624 return getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Op), SubVT, Op,
12626 };
12627
12628 // At each stage, we're looking for something that looks like:
12629 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
12630 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
12631 // i32 undef, i32 undef, i32 undef, i32 undef>
12632 // %a = binop <8 x i32> %op, %s
12633 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
12634 // we expect something like:
12635 // <4,5,6,7,u,u,u,u>
12636 // <2,3,u,u,u,u,u,u>
12637 // <1,u,u,u,u,u,u,u>
12638 // While a partial reduction match would be:
12639 // <2,3,u,u,u,u,u,u>
12640 // <1,u,u,u,u,u,u,u>
12641 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
12642 SDValue PrevOp;
12643 for (unsigned i = 0; i < Stages; ++i) {
12644 unsigned MaskEnd = (1 << i);
12645
12646 if (Op.getOpcode() != CandidateBinOp)
12647 return PartialReduction(PrevOp, MaskEnd);
12648
12649 SDValue Op0 = Op.getOperand(0);
12650 SDValue Op1 = Op.getOperand(1);
12651
12652 ShuffleVectorSDNode *Shuffle = dyn_cast<ShuffleVectorSDNode>(Op0);
12653 if (Shuffle) {
12654 Op = Op1;
12655 } else {
12656 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
12657 Op = Op0;
12658 }
12659
12660 // The first operand of the shuffle should be the same as the other operand
12661 // of the binop.
12662 if (!Shuffle || Shuffle->getOperand(0) != Op)
12663 return PartialReduction(PrevOp, MaskEnd);
12664
12665 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
12666 for (int Index = 0; Index < (int)MaskEnd; ++Index)
12667 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
12668 return PartialReduction(PrevOp, MaskEnd);
12669
12670 PrevOp = Op;
12671 }
12672
12673 // Handle subvector reductions, which tend to appear after the shuffle
12674 // reduction stages.
12675 while (Op.getOpcode() == CandidateBinOp) {
12676 unsigned NumElts = Op.getValueType().getVectorNumElements();
12677 SDValue Op0 = Op.getOperand(0);
12678 SDValue Op1 = Op.getOperand(1);
12679 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
12681 Op0.getOperand(0) != Op1.getOperand(0))
12682 break;
12683 SDValue Src = Op0.getOperand(0);
12684 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
12685 if (NumSrcElts != (2 * NumElts))
12686 break;
12687 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
12688 Op1.getConstantOperandAPInt(1) == NumElts) &&
12689 !(Op1.getConstantOperandAPInt(1) == 0 &&
12690 Op0.getConstantOperandAPInt(1) == NumElts))
12691 break;
12692 Op = Src;
12693 }
12694
12695 BinOp = (ISD::NodeType)CandidateBinOp;
12696 return Op;
12697}
12698
12700 EVT VT = N->getValueType(0);
12701 EVT EltVT = VT.getVectorElementType();
12702 unsigned NE = VT.getVectorNumElements();
12703
12704 SDLoc dl(N);
12705
12706 // If ResNE is 0, fully unroll the vector op.
12707 if (ResNE == 0)
12708 ResNE = NE;
12709 else if (NE > ResNE)
12710 NE = ResNE;
12711
12712 if (N->getNumValues() == 2) {
12713 SmallVector<SDValue, 8> Scalars0, Scalars1;
12714 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12715 EVT VT1 = N->getValueType(1);
12716 EVT EltVT1 = VT1.getVectorElementType();
12717
12718 unsigned i;
12719 for (i = 0; i != NE; ++i) {
12720 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12721 SDValue Operand = N->getOperand(j);
12722 EVT OperandVT = Operand.getValueType();
12723
12724 // A vector operand; extract a single element.
12725 EVT OperandEltVT = OperandVT.getVectorElementType();
12726 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12727 Operand, getVectorIdxConstant(i, dl));
12728 }
12729
12730 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
12731 Scalars0.push_back(EltOp);
12732 Scalars1.push_back(EltOp.getValue(1));
12733 }
12734
12735 for (; i < ResNE; ++i) {
12736 Scalars0.push_back(getUNDEF(EltVT));
12737 Scalars1.push_back(getUNDEF(EltVT1));
12738 }
12739
12740 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12741 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
12742 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
12743 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
12744 return getMergeValues({Vec0, Vec1}, dl);
12745 }
12746
12747 assert(N->getNumValues() == 1 &&
12748 "Can't unroll a vector with multiple results!");
12749
12751 SmallVector<SDValue, 4> Operands(N->getNumOperands());
12752
12753 unsigned i;
12754 for (i= 0; i != NE; ++i) {
12755 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
12756 SDValue Operand = N->getOperand(j);
12757 EVT OperandVT = Operand.getValueType();
12758 if (OperandVT.isVector()) {
12759 // A vector operand; extract a single element.
12760 EVT OperandEltVT = OperandVT.getVectorElementType();
12761 Operands[j] = getNode(ISD::EXTRACT_VECTOR_ELT, dl, OperandEltVT,
12762 Operand, getVectorIdxConstant(i, dl));
12763 } else {
12764 // A scalar operand; just use it as is.
12765 Operands[j] = Operand;
12766 }
12767 }
12768
12769 switch (N->getOpcode()) {
12770 default: {
12771 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
12772 N->getFlags()));
12773 break;
12774 }
12775 case ISD::VSELECT:
12776 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
12777 break;
12778 case ISD::SHL:
12779 case ISD::SRA:
12780 case ISD::SRL:
12781 case ISD::ROTL:
12782 case ISD::ROTR:
12783 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
12785 Operands[1])));
12786 break;
12788 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
12789 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
12790 Operands[0],
12791 getValueType(ExtVT)));
12792 break;
12793 }
12794 case ISD::ADDRSPACECAST: {
12795 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
12796 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
12797 ASC->getSrcAddressSpace(),
12798 ASC->getDestAddressSpace()));
12799 break;
12800 }
12801 }
12802 }
12803
12804 for (; i < ResNE; ++i)
12805 Scalars.push_back(getUNDEF(EltVT));
12806
12807 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
12808 return getBuildVector(VecVT, dl, Scalars);
12809}
12810
12811std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
12812 SDNode *N, unsigned ResNE) {
12813 unsigned Opcode = N->getOpcode();
12814 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
12815 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
12816 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
12817 "Expected an overflow opcode");
12818
12819 EVT ResVT = N->getValueType(0);
12820 EVT OvVT = N->getValueType(1);
12821 EVT ResEltVT = ResVT.getVectorElementType();
12822 EVT OvEltVT = OvVT.getVectorElementType();
12823 SDLoc dl(N);
12824
12825 // If ResNE is 0, fully unroll the vector op.
12826 unsigned NE = ResVT.getVectorNumElements();
12827 if (ResNE == 0)
12828 ResNE = NE;
12829 else if (NE > ResNE)
12830 NE = ResNE;
12831
12832 SmallVector<SDValue, 8> LHSScalars;
12833 SmallVector<SDValue, 8> RHSScalars;
12834 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
12835 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
12836
12837 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
12838 SDVTList VTs = getVTList(ResEltVT, SVT);
12839 SmallVector<SDValue, 8> ResScalars;
12840 SmallVector<SDValue, 8> OvScalars;
12841 for (unsigned i = 0; i < NE; ++i) {
12842 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
12843 SDValue Ov =
12844 getSelect(dl, OvEltVT, Res.getValue(1),
12845 getBoolConstant(true, dl, OvEltVT, ResVT),
12846 getConstant(0, dl, OvEltVT));
12847
12848 ResScalars.push_back(Res);
12849 OvScalars.push_back(Ov);
12850 }
12851
12852 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
12853 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
12854
12855 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
12856 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
12857 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
12858 getBuildVector(NewOvVT, dl, OvScalars));
12859}
12860
12863 unsigned Bytes,
12864 int Dist) const {
12865 if (LD->isVolatile() || Base->isVolatile())
12866 return false;
12867 // TODO: probably too restrictive for atomics, revisit
12868 if (!LD->isSimple())
12869 return false;
12870 if (LD->isIndexed() || Base->isIndexed())
12871 return false;
12872 if (LD->getChain() != Base->getChain())
12873 return false;
12874 EVT VT = LD->getMemoryVT();
12875 if (VT.getSizeInBits() / 8 != Bytes)
12876 return false;
12877
12878 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
12879 auto LocDecomp = BaseIndexOffset::match(LD, *this);
12880
12881 int64_t Offset = 0;
12882 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
12883 return (Dist * (int64_t)Bytes == Offset);
12884 return false;
12885}
12886
12887/// InferPtrAlignment - Infer alignment of a load / store address. Return
12888/// std::nullopt if it cannot be inferred.
12890 // If this is a GlobalAddress + cst, return the alignment.
12891 const GlobalValue *GV = nullptr;
12892 int64_t GVOffset = 0;
12893 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
12894 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
12895 KnownBits Known(PtrWidth);
12897 unsigned AlignBits = Known.countMinTrailingZeros();
12898 if (AlignBits)
12899 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
12900 }
12901
12902 // If this is a direct reference to a stack slot, use information about the
12903 // stack slot's alignment.
12904 int FrameIdx = INT_MIN;
12905 int64_t FrameOffset = 0;
12906 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
12907 FrameIdx = FI->getIndex();
12908 } else if (isBaseWithConstantOffset(Ptr) &&
12909 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
12910 // Handle FI+Cst
12911 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
12912 FrameOffset = Ptr.getConstantOperandVal(1);
12913 }
12914
12915 if (FrameIdx != INT_MIN) {
12917 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
12918 }
12919
12920 return std::nullopt;
12921}
12922
12923/// Split the scalar node with EXTRACT_ELEMENT using the provided
12924/// VTs and return the low/high part.
12925std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
12926 const SDLoc &DL,
12927 const EVT &LoVT,
12928 const EVT &HiVT) {
12929 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
12930 "Split node must be a scalar type");
12931 SDValue Lo =
12933 SDValue Hi =
12935 return std::make_pair(Lo, Hi);
12936}
12937
12938/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
12939/// which is split (or expanded) into two not necessarily identical pieces.
12940std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
12941 // Currently all types are split in half.
12942 EVT LoVT, HiVT;
12943 if (!VT.isVector())
12944 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
12945 else
12946 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
12947
12948 return std::make_pair(LoVT, HiVT);
12949}
12950
12951/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
12952/// type, dependent on an enveloping VT that has been split into two identical
12953/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
12954std::pair<EVT, EVT>
12956 bool *HiIsEmpty) const {
12957 EVT EltTp = VT.getVectorElementType();
12958 // Examples:
12959 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
12960 // custom VL=9 with enveloping VL=8/8 yields 8/1
12961 // custom VL=10 with enveloping VL=8/8 yields 8/2
12962 // etc.
12963 ElementCount VTNumElts = VT.getVectorElementCount();
12964 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
12965 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
12966 "Mixing fixed width and scalable vectors when enveloping a type");
12967 EVT LoVT, HiVT;
12968 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
12969 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12970 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
12971 *HiIsEmpty = false;
12972 } else {
12973 // Flag that hi type has zero storage size, but return split envelop type
12974 // (this would be easier if vector types with zero elements were allowed).
12975 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
12976 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
12977 *HiIsEmpty = true;
12978 }
12979 return std::make_pair(LoVT, HiVT);
12980}
12981
12982/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
12983/// low/high part.
12984std::pair<SDValue, SDValue>
12985SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
12986 const EVT &HiVT) {
12987 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
12988 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
12989 "Splitting vector with an invalid mixture of fixed and scalable "
12990 "vector types");
12992 N.getValueType().getVectorMinNumElements() &&
12993 "More vector elements requested than available!");
12994 SDValue Lo, Hi;
12995 Lo =
12997 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
12998 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
12999 // IDX with the runtime scaling factor of the result vector type. For
13000 // fixed-width result vectors, that runtime scaling factor is 1.
13003 return std::make_pair(Lo, Hi);
13004}
13005
13006std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13007 const SDLoc &DL) {
13008 // Split the vector length parameter.
13009 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13010 EVT VT = N.getValueType();
13012 "Expecting the mask to be an evenly-sized vector");
13013 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13014 SDValue HalfNumElts =
13015 VecVT.isFixedLengthVector()
13016 ? getConstant(HalfMinNumElts, DL, VT)
13017 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13018 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13019 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13020 return std::make_pair(Lo, Hi);
13021}
13022
13023/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13025 EVT VT = N.getValueType();
13028 return getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, getUNDEF(WideVT), N,
13030}
13031
13034 unsigned Start, unsigned Count,
13035 EVT EltVT) {
13036 EVT VT = Op.getValueType();
13037 if (Count == 0)
13038 Count = VT.getVectorNumElements();
13039 if (EltVT == EVT())
13040 EltVT = VT.getVectorElementType();
13041 SDLoc SL(Op);
13042 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13043 Args.push_back(getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, Op,
13044 getVectorIdxConstant(i, SL)));
13045 }
13046}
13047
13048// getAddressSpace - Return the address space this GlobalAddress belongs to.
13050 return getGlobal()->getType()->getAddressSpace();
13051}
13052
13055 return Val.MachineCPVal->getType();
13056 return Val.ConstVal->getType();
13057}
13058
13059bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13060 unsigned &SplatBitSize,
13061 bool &HasAnyUndefs,
13062 unsigned MinSplatBits,
13063 bool IsBigEndian) const {
13064 EVT VT = getValueType(0);
13065 assert(VT.isVector() && "Expected a vector type");
13066 unsigned VecWidth = VT.getSizeInBits();
13067 if (MinSplatBits > VecWidth)
13068 return false;
13069
13070 // FIXME: The widths are based on this node's type, but build vectors can
13071 // truncate their operands.
13072 SplatValue = APInt(VecWidth, 0);
13073 SplatUndef = APInt(VecWidth, 0);
13074
13075 // Get the bits. Bits with undefined values (when the corresponding element
13076 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13077 // in SplatValue. If any of the values are not constant, give up and return
13078 // false.
13079 unsigned int NumOps = getNumOperands();
13080 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13081 unsigned EltWidth = VT.getScalarSizeInBits();
13082
13083 for (unsigned j = 0; j < NumOps; ++j) {
13084 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13085 SDValue OpVal = getOperand(i);
13086 unsigned BitPos = j * EltWidth;
13087
13088 if (OpVal.isUndef())
13089 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13090 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13091 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13092 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13093 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13094 else
13095 return false;
13096 }
13097
13098 // The build_vector is all constants or undefs. Find the smallest element
13099 // size that splats the vector.
13100 HasAnyUndefs = (SplatUndef != 0);
13101
13102 // FIXME: This does not work for vectors with elements less than 8 bits.
13103 while (VecWidth > 8) {
13104 // If we can't split in half, stop here.
13105 if (VecWidth & 1)
13106 break;
13107
13108 unsigned HalfSize = VecWidth / 2;
13109 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13110 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13111 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13112 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13113
13114 // If the two halves do not match (ignoring undef bits), stop here.
13115 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13116 MinSplatBits > HalfSize)
13117 break;
13118
13119 SplatValue = HighValue | LowValue;
13120 SplatUndef = HighUndef & LowUndef;
13121
13122 VecWidth = HalfSize;
13123 }
13124
13125 // FIXME: The loop above only tries to split in halves. But if the input
13126 // vector for example is <3 x i16> it wouldn't be able to detect a
13127 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13128 // optimizations. I guess that back in the days when this helper was created
13129 // vectors normally was power-of-2 sized.
13130
13131 SplatBitSize = VecWidth;
13132 return true;
13133}
13134
13136 BitVector *UndefElements) const {
13137 unsigned NumOps = getNumOperands();
13138 if (UndefElements) {
13139 UndefElements->clear();
13140 UndefElements->resize(NumOps);
13141 }
13142 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13143 if (!DemandedElts)
13144 return SDValue();
13145 SDValue Splatted;
13146 for (unsigned i = 0; i != NumOps; ++i) {
13147 if (!DemandedElts[i])
13148 continue;
13149 SDValue Op = getOperand(i);
13150 if (Op.isUndef()) {
13151 if (UndefElements)
13152 (*UndefElements)[i] = true;
13153 } else if (!Splatted) {
13154 Splatted = Op;
13155 } else if (Splatted != Op) {
13156 return SDValue();
13157 }
13158 }
13159
13160 if (!Splatted) {
13161 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13162 assert(getOperand(FirstDemandedIdx).isUndef() &&
13163 "Can only have a splat without a constant for all undefs.");
13164 return getOperand(FirstDemandedIdx);
13165 }
13166
13167 return Splatted;
13168}
13169
13171 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13172 return getSplatValue(DemandedElts, UndefElements);
13173}
13174
13176 SmallVectorImpl<SDValue> &Sequence,
13177 BitVector *UndefElements) const {
13178 unsigned NumOps = getNumOperands();
13179 Sequence.clear();
13180 if (UndefElements) {
13181 UndefElements->clear();
13182 UndefElements->resize(NumOps);
13183 }
13184 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13185 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13186 return false;
13187
13188 // Set the undefs even if we don't find a sequence (like getSplatValue).
13189 if (UndefElements)
13190 for (unsigned I = 0; I != NumOps; ++I)
13191 if (DemandedElts[I] && getOperand(I).isUndef())
13192 (*UndefElements)[I] = true;
13193
13194 // Iteratively widen the sequence length looking for repetitions.
13195 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13196 Sequence.append(SeqLen, SDValue());
13197 for (unsigned I = 0; I != NumOps; ++I) {
13198 if (!DemandedElts[I])
13199 continue;
13200 SDValue &SeqOp = Sequence[I % SeqLen];
13202 if (Op.isUndef()) {
13203 if (!SeqOp)
13204 SeqOp = Op;
13205 continue;
13206 }
13207 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13208 Sequence.clear();
13209 break;
13210 }
13211 SeqOp = Op;
13212 }
13213 if (!Sequence.empty())
13214 return true;
13215 }
13216
13217 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13218 return false;
13219}
13220
13222 BitVector *UndefElements) const {
13223 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13224 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13225}
13226
13229 BitVector *UndefElements) const {
13230 return dyn_cast_or_null<ConstantSDNode>(
13231 getSplatValue(DemandedElts, UndefElements));
13232}
13233
13236 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13237}
13238
13241 BitVector *UndefElements) const {
13242 return dyn_cast_or_null<ConstantFPSDNode>(
13243 getSplatValue(DemandedElts, UndefElements));
13244}
13245
13248 return dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements));
13249}
13250
13251int32_t
13253 uint32_t BitWidth) const {
13254 if (ConstantFPSDNode *CN =
13255 dyn_cast_or_null<ConstantFPSDNode>(getSplatValue(UndefElements))) {
13256 bool IsExact;
13257 APSInt IntVal(BitWidth);
13258 const APFloat &APF = CN->getValueAPF();
13259 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13260 APFloat::opOK ||
13261 !IsExact)
13262 return -1;
13263
13264 return IntVal.exactLogBase2();
13265 }
13266 return -1;
13267}
13268
13270 bool IsLittleEndian, unsigned DstEltSizeInBits,
13271 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13272 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13273 if (!isConstant())
13274 return false;
13275
13276 unsigned NumSrcOps = getNumOperands();
13277 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13278 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13279 "Invalid bitcast scale");
13280
13281 // Extract raw src bits.
13282 SmallVector<APInt> SrcBitElements(NumSrcOps,
13283 APInt::getZero(SrcEltSizeInBits));
13284 BitVector SrcUndeElements(NumSrcOps, false);
13285
13286 for (unsigned I = 0; I != NumSrcOps; ++I) {
13288 if (Op.isUndef()) {
13289 SrcUndeElements.set(I);
13290 continue;
13291 }
13292 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13293 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13294 assert((CInt || CFP) && "Unknown constant");
13295 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13296 : CFP->getValueAPF().bitcastToAPInt();
13297 }
13298
13299 // Recast to dst width.
13300 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13301 SrcBitElements, UndefElements, SrcUndeElements);
13302 return true;
13303}
13304
13305void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13306 unsigned DstEltSizeInBits,
13307 SmallVectorImpl<APInt> &DstBitElements,
13308 ArrayRef<APInt> SrcBitElements,
13309 BitVector &DstUndefElements,
13310 const BitVector &SrcUndefElements) {
13311 unsigned NumSrcOps = SrcBitElements.size();
13312 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13313 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13314 "Invalid bitcast scale");
13315 assert(NumSrcOps == SrcUndefElements.size() &&
13316 "Vector size mismatch");
13317
13318 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13319 DstUndefElements.clear();
13320 DstUndefElements.resize(NumDstOps, false);
13321 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13322
13323 // Concatenate src elements constant bits together into dst element.
13324 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13325 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13326 for (unsigned I = 0; I != NumDstOps; ++I) {
13327 DstUndefElements.set(I);
13328 APInt &DstBits = DstBitElements[I];
13329 for (unsigned J = 0; J != Scale; ++J) {
13330 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13331 if (SrcUndefElements[Idx])
13332 continue;
13333 DstUndefElements.reset(I);
13334 const APInt &SrcBits = SrcBitElements[Idx];
13335 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13336 "Illegal constant bitwidths");
13337 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13338 }
13339 }
13340 return;
13341 }
13342
13343 // Split src element constant bits into dst elements.
13344 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13345 for (unsigned I = 0; I != NumSrcOps; ++I) {
13346 if (SrcUndefElements[I]) {
13347 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13348 continue;
13349 }
13350 const APInt &SrcBits = SrcBitElements[I];
13351 for (unsigned J = 0; J != Scale; ++J) {
13352 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13353 APInt &DstBits = DstBitElements[Idx];
13354 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13355 }
13356 }
13357}
13358
13360 for (const SDValue &Op : op_values()) {
13361 unsigned Opc = Op.getOpcode();
13362 if (Opc != ISD::UNDEF && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13363 return false;
13364 }
13365 return true;
13366}
13367
13368std::optional<std::pair<APInt, APInt>>
13370 unsigned NumOps = getNumOperands();
13371 if (NumOps < 2)
13372 return std::nullopt;
13373
13374 if (!isa<ConstantSDNode>(getOperand(0)) ||
13375 !isa<ConstantSDNode>(getOperand(1)))
13376 return std::nullopt;
13377
13378 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13379 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13380 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13381
13382 if (Stride.isZero())
13383 return std::nullopt;
13384
13385 for (unsigned i = 2; i < NumOps; ++i) {
13386 if (!isa<ConstantSDNode>(getOperand(i)))
13387 return std::nullopt;
13388
13389 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13390 if (Val != (Start + (Stride * i)))
13391 return std::nullopt;
13392 }
13393
13394 return std::make_pair(Start, Stride);
13395}
13396
13397bool ShuffleVectorSDNode::isSplatMask(const int *Mask, EVT VT) {
13398 // Find the first non-undef value in the shuffle mask.
13399 unsigned i, e;
13400 for (i = 0, e = VT.getVectorNumElements(); i != e && Mask[i] < 0; ++i)
13401 /* search */;
13402
13403 // If all elements are undefined, this shuffle can be considered a splat
13404 // (although it should eventually get simplified away completely).
13405 if (i == e)
13406 return true;
13407
13408 // Make sure all remaining elements are either undef or the same as the first
13409 // non-undef value.
13410 for (int Idx = Mask[i]; i != e; ++i)
13411 if (Mask[i] >= 0 && Mask[i] != Idx)
13412 return false;
13413 return true;
13414}
13415
13416// Returns true if it is a constant integer BuildVector or constant integer,
13417// possibly hidden by a bitcast.
13419 SDValue N, bool AllowOpaques) const {
13421
13422 if (auto *C = dyn_cast<ConstantSDNode>(N))
13423 return AllowOpaques || !C->isOpaque();
13424
13426 return true;
13427
13428 // Treat a GlobalAddress supporting constant offset folding as a
13429 // constant integer.
13430 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
13431 if (GA->getOpcode() == ISD::GlobalAddress &&
13432 TLI->isOffsetFoldingLegal(GA))
13433 return true;
13434
13435 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13436 isa<ConstantSDNode>(N.getOperand(0)))
13437 return true;
13438 return false;
13439}
13440
13441// Returns true if it is a constant float BuildVector or constant float.
13443 if (isa<ConstantFPSDNode>(N))
13444 return true;
13445
13447 return true;
13448
13449 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13450 isa<ConstantFPSDNode>(N.getOperand(0)))
13451 return true;
13452
13453 return false;
13454}
13455
13457 bool AllowTruncation) const {
13458 ConstantSDNode *Const = isConstOrConstSplat(N, false, AllowTruncation);
13459 if (!Const)
13460 return std::nullopt;
13461
13462 const APInt &CVal = Const->getAPIntValue();
13463 switch (TLI->getBooleanContents(N.getValueType())) {
13465 if (CVal.isOne())
13466 return true;
13467 if (CVal.isZero())
13468 return false;
13469 return std::nullopt;
13471 if (CVal.isAllOnes())
13472 return true;
13473 if (CVal.isZero())
13474 return false;
13475 return std::nullopt;
13477 return CVal[0];
13478 }
13479 llvm_unreachable("Unknown BooleanContent enum");
13480}
13481
13482void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
13483 assert(!Node->OperandList && "Node already has operands");
13485 "too many operands to fit into SDNode");
13486 SDUse *Ops = OperandRecycler.allocate(
13487 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
13488
13489 bool IsDivergent = false;
13490 for (unsigned I = 0; I != Vals.size(); ++I) {
13491 Ops[I].setUser(Node);
13492 Ops[I].setInitial(Vals[I]);
13493 EVT VT = Ops[I].getValueType();
13494
13495 // Skip Chain. It does not carry divergence.
13496 if (VT != MVT::Other &&
13497 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
13498 Ops[I].getNode()->isDivergent()) {
13499 IsDivergent = true;
13500 }
13501 }
13502 Node->NumOperands = Vals.size();
13503 Node->OperandList = Ops;
13504 if (!TLI->isSDNodeAlwaysUniform(Node)) {
13505 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
13506 Node->SDNodeBits.IsDivergent = IsDivergent;
13507 }
13509}
13510
13513 size_t Limit = SDNode::getMaxNumOperands();
13514 while (Vals.size() > Limit) {
13515 unsigned SliceIdx = Vals.size() - Limit;
13516 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
13517 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
13518 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
13519 Vals.emplace_back(NewTF);
13520 }
13521 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
13522}
13523
13525 EVT VT, SDNodeFlags Flags) {
13526 switch (Opcode) {
13527 default:
13528 return SDValue();
13529 case ISD::ADD:
13530 case ISD::OR:
13531 case ISD::XOR:
13532 case ISD::UMAX:
13533 return getConstant(0, DL, VT);
13534 case ISD::MUL:
13535 return getConstant(1, DL, VT);
13536 case ISD::AND:
13537 case ISD::UMIN:
13538 return getAllOnesConstant(DL, VT);
13539 case ISD::SMAX:
13541 case ISD::SMIN:
13543 case ISD::FADD:
13544 // If flags allow, prefer positive zero since it's generally cheaper
13545 // to materialize on most targets.
13546 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
13547 case ISD::FMUL:
13548 return getConstantFP(1.0, DL, VT);
13549 case ISD::FMINNUM:
13550 case ISD::FMAXNUM: {
13551 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13552 const fltSemantics &Semantics = VT.getFltSemantics();
13553 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
13554 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
13555 APFloat::getLargest(Semantics);
13556 if (Opcode == ISD::FMAXNUM)
13557 NeutralAF.changeSign();
13558
13559 return getConstantFP(NeutralAF, DL, VT);
13560 }
13561 case ISD::FMINIMUM:
13562 case ISD::FMAXIMUM: {
13563 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
13564 const fltSemantics &Semantics = VT.getFltSemantics();
13565 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
13566 : APFloat::getLargest(Semantics);
13567 if (Opcode == ISD::FMAXIMUM)
13568 NeutralAF.changeSign();
13569
13570 return getConstantFP(NeutralAF, DL, VT);
13571 }
13572
13573 }
13574}
13575
13576/// Helper used to make a call to a library function that has one argument of
13577/// pointer type.
13578///
13579/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
13580/// used to get or set floating-point state. They have one argument of pointer
13581/// type, which points to the memory region containing bits of the
13582/// floating-point state. The value returned by such function is ignored in the
13583/// created call.
13584///
13585/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
13586/// \param Ptr Pointer used to save/load state.
13587/// \param InChain Ingoing token chain.
13588/// \returns Outgoing chain token.
13590 SDValue InChain,
13591 const SDLoc &DLoc) {
13592 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
13595 Entry.Node = Ptr;
13596 Entry.Ty = Ptr.getValueType().getTypeForEVT(*getContext());
13597 Args.push_back(Entry);
13598 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
13599 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
13600 TLI->getPointerTy(getDataLayout()));
13602 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
13603 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
13604 std::move(Args));
13605 return TLI->LowerCallTo(CLI).second;
13606}
13607
13609 assert(From && To && "Invalid SDNode; empty source SDValue?");
13610 auto I = SDEI.find(From);
13611 if (I == SDEI.end())
13612 return;
13613
13614 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
13615 // the iterator, hence the need to make a copy to prevent a use-after-free.
13616 NodeExtraInfo NEI = I->second;
13617 if (LLVM_LIKELY(!NEI.PCSections) && LLVM_LIKELY(!NEI.MMRA)) {
13618 // No deep copy required for the types of extra info set.
13619 //
13620 // FIXME: Investigate if other types of extra info also need deep copy. This
13621 // depends on the types of nodes they can be attached to: if some extra info
13622 // is only ever attached to nodes where a replacement To node is always the
13623 // node where later use and propagation of the extra info has the intended
13624 // semantics, no deep copy is required.
13625 SDEI[To] = std::move(NEI);
13626 return;
13627 }
13628
13629 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
13630 // through the replacement of From with To. Otherwise, replacements of a node
13631 // (From) with more complex nodes (To and its operands) may result in lost
13632 // extra info where the root node (To) is insignificant in further propagating
13633 // and using extra info when further lowering to MIR.
13634 //
13635 // In the first step pre-populate the visited set with the nodes reachable
13636 // from the old From node. This avoids copying NodeExtraInfo to parts of the
13637 // DAG that is not new and should be left untouched.
13638 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
13639 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
13640 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
13641 if (MaxDepth == 0) {
13642 // Remember this node in case we need to increase MaxDepth and continue
13643 // populating FromReach from this node.
13644 Leafs.emplace_back(N);
13645 return;
13646 }
13647 if (!FromReach.insert(N).second)
13648 return;
13649 for (const SDValue &Op : N->op_values())
13650 Self(Self, Op.getNode(), MaxDepth - 1);
13651 };
13652
13653 // Copy extra info to To and all its transitive operands (that are new).
13655 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
13656 if (FromReach.contains(N))
13657 return true;
13658 if (!Visited.insert(N).second)
13659 return true;
13660 if (getEntryNode().getNode() == N)
13661 return false;
13662 for (const SDValue &Op : N->op_values()) {
13663 if (!Self(Self, Op.getNode()))
13664 return false;
13665 }
13666 // Copy only if entry node was not reached.
13667 SDEI[N] = NEI;
13668 return true;
13669 };
13670
13671 // We first try with a lower MaxDepth, assuming that the path to common
13672 // operands between From and To is relatively short. This significantly
13673 // improves performance in the common case. The initial MaxDepth is big
13674 // enough to avoid retry in the common case; the last MaxDepth is large
13675 // enough to avoid having to use the fallback below (and protects from
13676 // potential stack exhaustion from recursion).
13677 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
13678 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
13679 // StartFrom is the previous (or initial) set of leafs reachable at the
13680 // previous maximum depth.
13682 std::swap(StartFrom, Leafs);
13683 for (const SDNode *N : StartFrom)
13684 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
13685 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
13686 return;
13687 // This should happen very rarely (reached the entry node).
13688 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
13689 assert(!Leafs.empty());
13690 }
13691
13692 // This should not happen - but if it did, that means the subgraph reachable
13693 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
13694 // could not visit all reachable common operands. Consequently, we were able
13695 // to reach the entry node.
13696 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
13697 assert(false && "From subgraph too complex - increase max. MaxDepth?");
13698 // Best-effort fallback if assertions disabled.
13699 SDEI[To] = std::move(NEI);
13700}
13701
13702#ifndef NDEBUG
13703static void checkForCyclesHelper(const SDNode *N,
13706 const llvm::SelectionDAG *DAG) {
13707 // If this node has already been checked, don't check it again.
13708 if (Checked.count(N))
13709 return;
13710
13711 // If a node has already been visited on this depth-first walk, reject it as
13712 // a cycle.
13713 if (!Visited.insert(N).second) {
13714 errs() << "Detected cycle in SelectionDAG\n";
13715 dbgs() << "Offending node:\n";
13716 N->dumprFull(DAG); dbgs() << "\n";
13717 abort();
13718 }
13719
13720 for (const SDValue &Op : N->op_values())
13721 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
13722
13723 Checked.insert(N);
13724 Visited.erase(N);
13725}
13726#endif
13727
13729 const llvm::SelectionDAG *DAG,
13730 bool force) {
13731#ifndef NDEBUG
13732 bool check = force;
13733#ifdef EXPENSIVE_CHECKS
13734 check = true;
13735#endif // EXPENSIVE_CHECKS
13736 if (check) {
13737 assert(N && "Checking nonexistent SDNode");
13740 checkForCyclesHelper(N, visited, checked, DAG);
13741 }
13742#endif // !NDEBUG
13743}
13744
13745void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
13746 checkForCycles(DAG->getRoot().getNode(), DAG, force);
13747}
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file implements the BitVector class.
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition: Compiler.h:553
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:319
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(...)
Definition: Debug.h:106
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
uint32_t Index
uint64_t Size
Symbol * Sym
Definition: ELF_riscv.cpp:479
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a hash set that can be used to remove duplication of nodes in a graph.
#define _
iv users
Definition: IVUsers.cpp:48
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:533
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
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue 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 bool canFoldStoreIntoLibCallOutputPointers(StoreSDNode *StoreNode, SDNode *FPNode)
Given a store node StoreNode, return true if it is safe to fold that node into FPNode,...
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void 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 cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
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:1117
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1205
void copySign(const APFloat &RHS)
Definition: APFloat.h:1299
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5465
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1187
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:1423
opStatus add(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1178
bool isFinite() const
Definition: APFloat.h:1445
bool isNaN() const
Definition: APFloat.h:1438
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition: APFloat.h:1329
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition: APFloat.h:1196
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition: APFloat.h:1232
bool isZero() const
Definition: APFloat.h:1436
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1135
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition: APFloat.h:1321
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1095
opStatus mod(const APFloat &RHS)
Definition: APFloat.h:1223
bool isPosZero() const
Definition: APFloat.h:1451
bool isNegZero() const
Definition: APFloat.h:1452
void changeSign()
Definition: APFloat.h:1294
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition: APFloat.h:1106
bool isInfinity() const
Definition: APFloat.h:1437
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1945
APInt usub_sat(const APInt &RHS) const
Definition: APInt.cpp:2029
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1547
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1407
APInt zext(unsigned width) const
Zero extend to a new width.
Definition: APInt.cpp:986
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition: APInt.h:229
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1392
unsigned popcount() const
Count the number of bits set.
Definition: APInt.h:1649
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1386
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition: APInt.cpp:612
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1007
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1492
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:910
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1330
APInt abs() const
Get the absolute value.
Definition: APInt.h:1773
APInt sadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2000
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1640
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1468
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1618
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1397
APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition: APInt.cpp:1128
APInt reverseBits() const
Definition: APInt.cpp:741
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:834
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1166
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition: APInt.h:1618
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1607
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition: APInt.h:1577
static APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition: APInt.cpp:624
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
APInt sshl_sat(const APInt &RHS) const
Definition: APInt.cpp:2060
APInt ushl_sat(const APInt &RHS) const
Definition: APInt.cpp:2074
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1015
APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition: APInt.cpp:1115
void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition: APInt.cpp:370
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition: APInt.h:1417
unsigned logBase2() const
Definition: APInt.h:1739
APInt uadd_sat(const APInt &RHS) const
Definition: APInt.cpp:2010
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:827
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1319
APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition: APInt.cpp:1710
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:959
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition: APInt.h:1367
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
APInt byteSwap() const
Definition: APInt.cpp:719
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition: APInt.h:1257
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition: APInt.h:440
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition: APInt.h:306
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1389
APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition: APInt.cpp:455
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1237
bool isOne() const
Determine if this is a value of 1.
Definition: APInt.h:389
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:858
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
APInt ssub_sat(const APInt &RHS) const
Definition: APInt.cpp:2019
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:157
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
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:198
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:893
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:271
const APFloat & getValue() const
Definition: Constants.h:315
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition: Constants.h:151
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:148
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:1708
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
DWARF expression.
static 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:63
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:197
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:851
Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:843
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:743
Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
Definition: DataLayout.cpp:847
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:278
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 hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition: Function.h:704
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition: Function.h:353
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:369
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:1069
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 TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
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:228
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:310
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:686
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:703
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Definition: PointerUnion.h:142
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
Keeps track of dbg_value information through SDISel.
Definition: SelectionDAG.h:162
BumpPtrAllocator & getAlloc()
Definition: SelectionDAG.h:191
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:197
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.
iterator_range< use_iterator > uses()
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.
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
void DropOperands()
Release the operands and set this node to have zero operands.
Represents a use of a SDNode.
EVT getValueType() const
Convenience function for get().getValueType().
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
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:387
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:228
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:575
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:497
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.
SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=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)
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...
bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
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.
static unsigned getHasPredecessorMaxSteps()
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.
SDValue getRegister(Register Reg, EVT VT)
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:501
bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
Definition: SelectionDAG.h:456
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:395
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:555
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:854
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.
bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
Definition: SelectionDAG.h:495
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 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 getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
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:888
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.
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 getPartialReduceAdd(SDLoc DL, EVT ReducedTy, SDValue Op1, SDValue Op2)
Create the DAG equivalent of vector_partial_reduce where Op1 and Op2 are its operands and ReducedTY i...
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)
SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
Definition: SelectionDAG.h:496
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:567
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:563
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:698
const TargetLibraryInfo & getLibInfo() const
Definition: SelectionDAG.h:502
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:490
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:871
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.
bool expandMultipleResultFPLibCall(RTLIB::Libcall LC, SDNode *Node, SmallVectorImpl< SDValue > &Results, std::optional< unsigned > CallRetResNo={})
Expands a node with multiple results to an FP or vector libcall.
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:508
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:584
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)
std::optional< bool > isBoolConstant(SDValue N, bool AllowTruncation=false) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
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:578
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:904
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:558
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.
size_type size() const
Definition: SmallPtrSet.h:94
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
bool erase(PtrType Ptr)
Remove pointer from the set.
Definition: SmallPtrSet.h:401
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
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:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:704
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
void reserve(size_type N)
Definition: SmallVector.h:663
iterator erase(const_iterator CI)
Definition: SmallVector.h:737
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
const VecDesc * getVectorMappingInfo(StringRef F, const ElementCount &VF, bool Masked) const
CallingConv::ID getLibcallCallingConv(RTLIB::Libcall Call) const
Get the CallingConv that should be used for the specified libcall.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
virtual bool isSExtCheaperThanZExt(EVT FromTy, EVT ToTy) const
Return true if sign-extension from FromTy to ToTy is cheaper than zero-extension.
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 for a comparison of the specified types on this ...
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
bool isOperationLegal(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target.
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
Align getMinStackArgumentAlignment() const
Return the minimum stack alignment of an argument.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
virtual bool hasVectorBlend() const
Return true if the target has a vector blend instruction.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
MVT getFrameIndexTy(const DataLayout &DL) const
Return the type for frame index, which is determined by the alloca address space specified through th...
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
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:568
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:270
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.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void set(Value *Val)
Definition: Value.h:886
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
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
Provides info so a possible vectorization of a function can be computed.
bool isMasked() const
StringRef getVectorFnName() const
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:213
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition: DenseSet.h:193
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
#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:3104
const APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition: APInt.h:2242
APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition: APInt.cpp:3091
APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition: APInt.cpp:3081
const APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition: APInt.h:2237
APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition: APInt.cpp:3096
APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition: APInt.cpp:2982
APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition: APInt.cpp:3076
APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition: APInt.cpp:3086
@ 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:780
@ 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:753
@ 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:1242
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition: ISDOpcodes.h:491
@ ATOMIC_LOAD_FMAX
Definition: ISDOpcodes.h:1347
@ 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:1417
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
Definition: ISDOpcodes.h:1360
@ VECREDUCE_SMIN
Definition: ISDOpcodes.h:1450
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition: ISDOpcodes.h:512
@ 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:1340
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition: ISDOpcodes.h:574
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
Definition: ISDOpcodes.h:1131
@ BSWAP
Byte Swap and Counting operators.
Definition: ISDOpcodes.h:744
@ TargetBlockAddress
Definition: ISDOpcodes.h:176
@ ConstantFP
Definition: ISDOpcodes.h:77
@ ATOMIC_LOAD_MAX
Definition: ISDOpcodes.h:1342
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
Definition: ISDOpcodes.h:1312
@ ATOMIC_LOAD_UMIN
Definition: ISDOpcodes.h:1343
@ 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:502
@ 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:1102
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition: ISDOpcodes.h:814
@ ATOMIC_LOAD_USUB_COND
Definition: ISDOpcodes.h:1351
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition: ISDOpcodes.h:498
@ FATAN2
FATAN2 - atan2, inspired by libm.
Definition: ISDOpcodes.h:999
@ 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:1325
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition: ISDOpcodes.h:841
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition: ISDOpcodes.h:558
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
Definition: ISDOpcodes.h:1435
@ 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:1439
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition: ISDOpcodes.h:717
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition: ISDOpcodes.h:871
@ VECREDUCE_SMAX
Definition: ISDOpcodes.h:1449
@ STRICT_FSETCCS
Definition: ISDOpcodes.h:492
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition: ISDOpcodes.h:964
@ ATOMIC_LOAD_OR
Definition: ISDOpcodes.h:1338
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition: ISDOpcodes.h:954
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition: ISDOpcodes.h:236
@ ATOMIC_LOAD_XOR
Definition: ISDOpcodes.h:1339
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
Definition: ISDOpcodes.h:997
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
Definition: ISDOpcodes.h:1490
@ ATOMIC_LOAD_FADD
Definition: ISDOpcodes.h:1345
@ 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:1238
@ 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:1173
@ ATOMIC_LOAD_USUB_SAT
Definition: ISDOpcodes.h:1352
@ SIGN_EXTEND
Conversion operators.
Definition: ISDOpcodes.h:805
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition: ISDOpcodes.h:685
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition: ISDOpcodes.h:635
@ TargetExternalSymbol
Definition: ISDOpcodes.h:175
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
Definition: ISDOpcodes.h:1432
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition: ISDOpcodes.h:752
@ 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:1292
@ VECREDUCE_FMIN
Definition: ISDOpcodes.h:1436
@ 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:788
@ FNEG
Perform various unary floating-point operations inspired by libm.
Definition: ISDOpcodes.h:981
@ ATOMIC_LOAD_FSUB
Definition: ISDOpcodes.h:1346
@ BR_CC
BR_CC - Conditional branch.
Definition: ISDOpcodes.h:1148
@ SSUBO
Same for subtraction.
Definition: ISDOpcodes.h:334
@ ATOMIC_LOAD_MIN
Definition: ISDOpcodes.h:1341
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition: ISDOpcodes.h:661
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition: ISDOpcodes.h:515
@ 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:757
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
Definition: ISDOpcodes.h:1308
@ UNDEF
UNDEF - An undefined node.
Definition: ISDOpcodes.h:218
@ VECREDUCE_UMAX
Definition: ISDOpcodes.h:1451
@ 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:642
@ 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:1348
@ 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:1444
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition: ISDOpcodes.h:674
@ SHL
Shift and rotation operations.
Definition: ISDOpcodes.h:735
@ ATOMIC_LOAD_CLR
Definition: ISDOpcodes.h:1337
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition: ISDOpcodes.h:615
@ ATOMIC_LOAD_AND
Definition: ISDOpcodes.h:1336
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition: ISDOpcodes.h:588
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
Definition: ISDOpcodes.h:1044
@ 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:550
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition: ISDOpcodes.h:209
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition: ISDOpcodes.h:811
@ TargetConstantFP
Definition: ISDOpcodes.h:165
@ FP_TO_UINT_SAT
Definition: ISDOpcodes.h:907
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition: ISDOpcodes.h:772
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
Definition: ISDOpcodes.h:1407
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
Definition: ISDOpcodes.h:1319
@ ATOMIC_LOAD_UMAX
Definition: ISDOpcodes.h:1344
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
Definition: ISDOpcodes.h:1031
@ 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:860
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition: ISDOpcodes.h:849
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition: ISDOpcodes.h:697
@ LIFETIME_START
This corresponds to the llvm.lifetime.
Definition: ISDOpcodes.h:1377
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition: ISDOpcodes.h:939
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition: ISDOpcodes.h:766
@ 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:1372
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
Definition: ISDOpcodes.h:1262
@ VECREDUCE_UMIN
Definition: ISDOpcodes.h:1452
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
Definition: ISDOpcodes.h:973
@ ATOMIC_LOAD_UDEC_WRAP
Definition: ISDOpcodes.h:1350
@ ATOMIC_LOAD_ADD
Definition: ISDOpcodes.h:1334
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition: ISDOpcodes.h:480
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
Definition: ISDOpcodes.h:1050
@ ATOMIC_LOAD_SUB
Definition: ISDOpcodes.h:1335
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition: ISDOpcodes.h:887
@ 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:485
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition: ISDOpcodes.h:709
@ 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:1078
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
Definition: ISDOpcodes.h:1402
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition: ISDOpcodes.h:705
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition: ISDOpcodes.h:680
@ VECREDUCE_FMUL
Definition: ISDOpcodes.h:1433
@ 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:651
@ 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:539
@ 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:627
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
Definition: ISDOpcodes.h:1333
@ ExternalSymbol
Definition: ISDOpcodes.h:83
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
Definition: ISDOpcodes.h:1004
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition: ISDOpcodes.h:920
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition: ISDOpcodes.h:669
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition: ISDOpcodes.h:882
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition: ISDOpcodes.h:958
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Definition: ISDOpcodes.h:1481
@ 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:906
@ VECREDUCE_FMINIMUM
Definition: ISDOpcodes.h:1440
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition: ISDOpcodes.h:817
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
Definition: ISDOpcodes.h:1217
@ BlockAddress
Definition: ISDOpcodes.h:84
@ VECREDUCE_SEQ_FMUL
Definition: ISDOpcodes.h:1418
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition: ISDOpcodes.h:794
@ 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:1349
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition: ISDOpcodes.h:508
@ 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
@ CALLSEQ_START
CALLSEQ_START/CALLSEQ_END - These operators mark the beginning and end of a call sequence,...
Definition: ISDOpcodes.h:1211
@ SET_FPENV_MEM
Sets the current floating point environment.
Definition: ISDOpcodes.h:1083
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
Definition: ISDOpcodes.h:1055
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition: ISDOpcodes.h:692
@ 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:530
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 isNormalStore(const SDNode *N)
Returns true if the specified node is a non-truncating and unindexed store.
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:1502
bool isExtOpcode(unsigned Opcode)
Definition: ISDOpcodes.h:1684
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:1671
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:1676
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).
std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
Definition: ISDOpcodes.h:1575
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:1562
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:1613
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:1593
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:903
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
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:1739
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:1538
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 tuples (A, B,...
Definition: STLExtras.h:2448
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition: bit.h:307
void * PointerTy
Definition: GenericValue.h:21
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:1599
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
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:657
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:1520
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:1521
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:1746
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:1558
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:1664
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition: APFloat.h:1585
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:1753
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:1544
@ 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:1841
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition: Analysis.cpp:715
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:1903
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:581
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:1572
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition: APFloat.h:1612
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:265
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition: APFloat.h:287
static constexpr roundingMode rmTowardNegative
Definition: APFloat.h:300
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:297
static constexpr roundingMode rmTowardZero
Definition: APFloat.h:301
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:267
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:266
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:263
static constexpr roundingMode rmTowardPositive
Definition: APFloat.h:299
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:264
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:313
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition: ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition: ValueTypes.h:390
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition: ValueTypes.h:137
intptr_t getRawBits() const
Definition: ValueTypes.h:507
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition: ValueTypes.h:74
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition: ValueTypes.h:279
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition: ValueTypes.h:295
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition: ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition: ValueTypes.h:345
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition: ValueTypes.h:368
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition: ValueTypes.h:354
uint64_t getScalarSizeInBits() const
Definition: ValueTypes.h:380
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition: ValueTypes.h:311
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition: ValueTypes.h:65
bool isFixedLengthVector() const
Definition: ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition: ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition: ValueTypes.h:318
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition: ValueTypes.h:287
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition: ValueTypes.h:251
Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
Definition: ValueTypes.cpp:210
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition: ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition: ValueTypes.h:323
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition: ValueTypes.h:142
const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
Definition: ValueTypes.cpp:320
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition: ValueTypes.h:331
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition: ValueTypes.h:303
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition: ValueTypes.h:448
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition: ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:293
KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
Definition: KnownBits.cpp:158
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:909
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:247
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:211
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:100
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:79
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:116
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:774
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:234
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:1049
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:65
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:266
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:111
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:153
KnownBits byteSwap() const
Definition: KnownBits.h:471
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:281
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:85
KnownBits reverseBits() const
Definition: KnownBits.h:475
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition: KnownBits.h:225
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:43
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:187
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:164
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:73
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:313
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:103
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:217
static KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
Definition: KnownBits.cpp:792
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:303
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:172
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:146
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:188
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:137
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:901
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1066
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1009
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:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition: KnownBits.h:106
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:953
static KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
Definition: KnownBits.cpp:788
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:318
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:97
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:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:272
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:211
static KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
Definition: KnownBits.cpp:800
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:804
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:159
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:205
static KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
Definition: KnownBits.cpp:796
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.
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:315
DAGUpdateListener *const Next
Definition: SelectionDAG.h:316
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)