LLVM 22.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84using namespace llvm::SDPatternMatch;
85
86/// makeVTList - Return an instance of the SDVTList struct initialized with the
87/// specified members.
88static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
89 SDVTList Res = {VTs, NumVTs};
90 return Res;
91}
92
93// Default null implementations of the callbacks.
97
98void SelectionDAG::DAGNodeDeletedListener::anchor() {}
99void SelectionDAG::DAGNodeInsertedListener::anchor() {}
100
101#define DEBUG_TYPE "selectiondag"
102
103static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
104 cl::Hidden, cl::init(true),
105 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
106
107static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
108 cl::desc("Number limit for gluing ld/st of memcpy."),
109 cl::Hidden, cl::init(0));
110
112 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
113 cl::desc("DAG combiner limit number of steps when searching DAG "
114 "for predecessor nodes"));
115
117 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
118}
119
121
122//===----------------------------------------------------------------------===//
123// ConstantFPSDNode Class
124//===----------------------------------------------------------------------===//
125
126/// isExactlyValue - We don't rely on operator== working on double values, as
127/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
128/// As such, this method can be used to do an exact bit-for-bit comparison of
129/// two floating point values.
131 return getValueAPF().bitwiseIsEqual(V);
132}
133
135 const APFloat& Val) {
136 assert(VT.isFloatingPoint() && "Can only convert between FP types");
137
138 // convert modifies in place, so make a copy.
139 APFloat Val2 = APFloat(Val);
140 bool losesInfo;
142 &losesInfo);
143 return !losesInfo;
144}
145
146//===----------------------------------------------------------------------===//
147// ISD Namespace
148//===----------------------------------------------------------------------===//
149
150bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
151 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
152 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
153 unsigned EltSize =
154 N->getValueType(0).getVectorElementType().getSizeInBits();
155 SplatVal = OptAPInt->trunc(EltSize);
156 return true;
157 }
158 }
159
160 auto *BV = dyn_cast<BuildVectorSDNode>(N);
161 if (!BV)
162 return false;
163
164 APInt SplatUndef;
165 unsigned SplatBitSize;
166 bool HasUndefs;
167 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
168 // Endianness does not matter here. We are checking for a splat given the
169 // element size of the vector, and if we find such a splat for little endian
170 // layout, then that should be valid also for big endian (as the full vector
171 // size is known to be a multiple of the element size).
172 const bool IsBigEndian = false;
173 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
174 EltSize, IsBigEndian) &&
175 EltSize == SplatBitSize;
176}
177
178// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
179// specializations of the more general isConstantSplatVector()?
180
181bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
182 // Look through a bit convert.
183 while (N->getOpcode() == ISD::BITCAST)
184 N = N->getOperand(0).getNode();
185
186 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
187 APInt SplatVal;
188 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
189 }
190
191 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
192
193 unsigned i = 0, e = N->getNumOperands();
194
195 // Skip over all of the undef values.
196 while (i != e && N->getOperand(i).isUndef())
197 ++i;
198
199 // Do not accept an all-undef vector.
200 if (i == e) return false;
201
202 // Do not accept build_vectors that aren't all constants or which have non-~0
203 // elements. We have to be a bit careful here, as the type of the constant
204 // may not be the same as the type of the vector elements due to type
205 // legalization (the elements are promoted to a legal type for the target and
206 // a vector of a type may be legal when the base element type is not).
207 // We only want to check enough bits to cover the vector elements, because
208 // we care if the resultant vector is all ones, not whether the individual
209 // constants are.
210 SDValue NotZero = N->getOperand(i);
211 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
212 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
213 if (OptAPInt->countr_one() < EltSize)
214 return false;
215 } else
216 return false;
217
218 // Okay, we have at least one ~0 value, check to see if the rest match or are
219 // undefs. Even with the above element type twiddling, this should be OK, as
220 // the same type legalization should have applied to all the elements.
221 for (++i; i != e; ++i)
222 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
223 return false;
224 return true;
225}
226
227bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
228 // Look through a bit convert.
229 while (N->getOpcode() == ISD::BITCAST)
230 N = N->getOperand(0).getNode();
231
232 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
233 APInt SplatVal;
234 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
235 }
236
237 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
238
239 bool IsAllUndef = true;
240 for (const SDValue &Op : N->op_values()) {
241 if (Op.isUndef())
242 continue;
243 IsAllUndef = false;
244 // Do not accept build_vectors that aren't all constants or which have non-0
245 // elements. We have to be a bit careful here, as the type of the constant
246 // may not be the same as the type of the vector elements due to type
247 // legalization (the elements are promoted to a legal type for the target
248 // and a vector of a type may be legal when the base element type is not).
249 // We only want to check enough bits to cover the vector elements, because
250 // we care if the resultant vector is all zeros, not whether the individual
251 // constants are.
252 if (auto OptAPInt = Op->bitcastToAPInt()) {
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (OptAPInt->countr_zero() < EltSize)
255 return false;
256 } else
257 return false;
258 }
259
260 // Do not accept an all-undef vector.
261 if (IsAllUndef)
262 return false;
263 return true;
264}
265
267 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
268}
269
271 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
272}
273
275 if (N->getOpcode() != ISD::BUILD_VECTOR)
276 return false;
277
278 for (const SDValue &Op : N->op_values()) {
279 if (Op.isUndef())
280 continue;
282 return false;
283 }
284 return 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;
295 return false;
296 }
297 return true;
298}
299
300bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
301 bool Signed) {
302 assert(N->getValueType(0).isVector() && "Expected a vector!");
303
304 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
305 if (EltSize <= NewEltSize)
306 return false;
307
308 if (N->getOpcode() == ISD::ZERO_EXTEND) {
309 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
310 NewEltSize) &&
311 !Signed;
312 }
313 if (N->getOpcode() == ISD::SIGN_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 Signed;
317 }
318 if (N->getOpcode() != ISD::BUILD_VECTOR)
319 return false;
320
321 for (const SDValue &Op : N->op_values()) {
322 if (Op.isUndef())
323 continue;
325 return false;
326
327 APInt C = Op->getAsAPIntVal().trunc(EltSize);
328 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
329 return false;
330 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
331 return false;
332 }
333
334 return true;
335}
336
338 // Return false if the node has no operands.
339 // This is "logically inconsistent" with the definition of "all" but
340 // is probably the desired behavior.
341 if (N->getNumOperands() == 0)
342 return false;
343 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
344}
345
347 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
348}
349
350template <typename ConstNodeType>
352 std::function<bool(ConstNodeType *)> Match,
353 bool AllowUndefs, bool AllowTruncation) {
354 // FIXME: Add support for scalar UNDEF cases?
355 if (auto *C = dyn_cast<ConstNodeType>(Op))
356 return Match(C);
357
358 // FIXME: Add support for vector UNDEF cases?
359 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
360 ISD::SPLAT_VECTOR != Op.getOpcode())
361 return false;
362
363 EVT SVT = Op.getValueType().getScalarType();
364 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
365 if (AllowUndefs && Op.getOperand(i).isUndef()) {
366 if (!Match(nullptr))
367 return false;
368 continue;
369 }
370
371 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
372 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
373 !Match(Cst))
374 return false;
375 }
376 return true;
377}
378// Build used template types.
380 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
383
385 SDValue LHS, SDValue RHS,
386 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
387 bool AllowUndefs, bool AllowTypeMismatch) {
388 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
389 return false;
390
391 // TODO: Add support for scalar UNDEF cases?
392 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
393 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
394 return Match(LHSCst, RHSCst);
395
396 // TODO: Add support for vector UNDEF cases?
397 if (LHS.getOpcode() != RHS.getOpcode() ||
398 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
399 LHS.getOpcode() != ISD::SPLAT_VECTOR))
400 return false;
401
402 EVT SVT = LHS.getValueType().getScalarType();
403 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
404 SDValue LHSOp = LHS.getOperand(i);
405 SDValue RHSOp = RHS.getOperand(i);
406 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
407 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
408 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
409 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
410 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
411 return false;
412 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
413 LHSOp.getValueType() != RHSOp.getValueType()))
414 return false;
415 if (!Match(LHSCst, RHSCst))
416 return false;
417 }
418 return true;
419}
420
422 switch (MinMaxOpc) {
423 default:
424 llvm_unreachable("unrecognized opcode");
425 case ISD::UMIN:
426 return ISD::UMAX;
427 case ISD::UMAX:
428 return ISD::UMIN;
429 case ISD::SMIN:
430 return ISD::SMAX;
431 case ISD::SMAX:
432 return ISD::SMIN;
433 }
434}
435
437 switch (VecReduceOpcode) {
438 default:
439 llvm_unreachable("Expected VECREDUCE opcode");
440 case ISD::VECREDUCE_FADD:
441 case ISD::VECREDUCE_SEQ_FADD:
442 case ISD::VP_REDUCE_FADD:
443 case ISD::VP_REDUCE_SEQ_FADD:
444 return ISD::FADD;
445 case ISD::VECREDUCE_FMUL:
446 case ISD::VECREDUCE_SEQ_FMUL:
447 case ISD::VP_REDUCE_FMUL:
448 case ISD::VP_REDUCE_SEQ_FMUL:
449 return ISD::FMUL;
450 case ISD::VECREDUCE_ADD:
451 case ISD::VP_REDUCE_ADD:
452 return ISD::ADD;
453 case ISD::VECREDUCE_MUL:
454 case ISD::VP_REDUCE_MUL:
455 return ISD::MUL;
456 case ISD::VECREDUCE_AND:
457 case ISD::VP_REDUCE_AND:
458 return ISD::AND;
459 case ISD::VECREDUCE_OR:
460 case ISD::VP_REDUCE_OR:
461 return ISD::OR;
462 case ISD::VECREDUCE_XOR:
463 case ISD::VP_REDUCE_XOR:
464 return ISD::XOR;
465 case ISD::VECREDUCE_SMAX:
466 case ISD::VP_REDUCE_SMAX:
467 return ISD::SMAX;
468 case ISD::VECREDUCE_SMIN:
469 case ISD::VP_REDUCE_SMIN:
470 return ISD::SMIN;
471 case ISD::VECREDUCE_UMAX:
472 case ISD::VP_REDUCE_UMAX:
473 return ISD::UMAX;
474 case ISD::VECREDUCE_UMIN:
475 case ISD::VP_REDUCE_UMIN:
476 return ISD::UMIN;
477 case ISD::VECREDUCE_FMAX:
478 case ISD::VP_REDUCE_FMAX:
479 return ISD::FMAXNUM;
480 case ISD::VECREDUCE_FMIN:
481 case ISD::VP_REDUCE_FMIN:
482 return ISD::FMINNUM;
483 case ISD::VECREDUCE_FMAXIMUM:
484 case ISD::VP_REDUCE_FMAXIMUM:
485 return ISD::FMAXIMUM;
486 case ISD::VECREDUCE_FMINIMUM:
487 case ISD::VP_REDUCE_FMINIMUM:
488 return ISD::FMINIMUM;
489 }
490}
491
492bool ISD::isVPOpcode(unsigned Opcode) {
493 switch (Opcode) {
494 default:
495 return false;
496#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
497 case ISD::VPSD: \
498 return true;
499#include "llvm/IR/VPIntrinsics.def"
500 }
501}
502
503bool ISD::isVPBinaryOp(unsigned Opcode) {
504 switch (Opcode) {
505 default:
506 break;
507#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
508#define VP_PROPERTY_BINARYOP return true;
509#define END_REGISTER_VP_SDNODE(VPSD) break;
510#include "llvm/IR/VPIntrinsics.def"
511 }
512 return false;
513}
514
515bool ISD::isVPReduction(unsigned Opcode) {
516 switch (Opcode) {
517 default:
518 return false;
519 case ISD::VP_REDUCE_ADD:
520 case ISD::VP_REDUCE_MUL:
521 case ISD::VP_REDUCE_AND:
522 case ISD::VP_REDUCE_OR:
523 case ISD::VP_REDUCE_XOR:
524 case ISD::VP_REDUCE_SMAX:
525 case ISD::VP_REDUCE_SMIN:
526 case ISD::VP_REDUCE_UMAX:
527 case ISD::VP_REDUCE_UMIN:
528 case ISD::VP_REDUCE_FMAX:
529 case ISD::VP_REDUCE_FMIN:
530 case ISD::VP_REDUCE_FMAXIMUM:
531 case ISD::VP_REDUCE_FMINIMUM:
532 case ISD::VP_REDUCE_FADD:
533 case ISD::VP_REDUCE_FMUL:
534 case ISD::VP_REDUCE_SEQ_FADD:
535 case ISD::VP_REDUCE_SEQ_FMUL:
536 return true;
537 }
538}
539
540/// The operand position of the vector mask.
541std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
542 switch (Opcode) {
543 default:
544 return std::nullopt;
545#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
546 case ISD::VPSD: \
547 return MASKPOS;
548#include "llvm/IR/VPIntrinsics.def"
549 }
550}
551
552/// The operand position of the explicit vector length parameter.
553std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
554 switch (Opcode) {
555 default:
556 return std::nullopt;
557#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
558 case ISD::VPSD: \
559 return EVLPOS;
560#include "llvm/IR/VPIntrinsics.def"
561 }
562}
563
564std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
565 bool hasFPExcept) {
566 // FIXME: Return strict opcodes in case of fp exceptions.
567 switch (VPOpcode) {
568 default:
569 return std::nullopt;
570#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
571#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
572#define END_REGISTER_VP_SDNODE(VPOPC) break;
573#include "llvm/IR/VPIntrinsics.def"
574 }
575 return std::nullopt;
576}
577
578std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
579 switch (Opcode) {
580 default:
581 return std::nullopt;
582#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
583#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
584#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
585#include "llvm/IR/VPIntrinsics.def"
586 }
587}
588
590 switch (ExtType) {
591 case ISD::EXTLOAD:
592 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
593 case ISD::SEXTLOAD:
594 return ISD::SIGN_EXTEND;
595 case ISD::ZEXTLOAD:
596 return ISD::ZERO_EXTEND;
597 default:
598 break;
599 }
600
601 llvm_unreachable("Invalid LoadExtType");
602}
603
605 // To perform this operation, we just need to swap the L and G bits of the
606 // operation.
607 unsigned OldL = (Operation >> 2) & 1;
608 unsigned OldG = (Operation >> 1) & 1;
609 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
610 (OldL << 1) | // New G bit
611 (OldG << 2)); // New L bit.
612}
613
615 unsigned Operation = Op;
616 if (isIntegerLike)
617 Operation ^= 7; // Flip L, G, E bits, but not U.
618 else
619 Operation ^= 15; // Flip all of the condition bits.
620
622 Operation &= ~8; // Don't let N and U bits get set.
623
624 return ISD::CondCode(Operation);
625}
626
630
632 bool isIntegerLike) {
633 return getSetCCInverseImpl(Op, isIntegerLike);
634}
635
636/// For an integer comparison, return 1 if the comparison is a signed operation
637/// and 2 if the result is an unsigned comparison. Return zero if the operation
638/// does not depend on the sign of the input (setne and seteq).
639static int isSignedOp(ISD::CondCode Opcode) {
640 switch (Opcode) {
641 default: llvm_unreachable("Illegal integer setcc operation!");
642 case ISD::SETEQ:
643 case ISD::SETNE: return 0;
644 case ISD::SETLT:
645 case ISD::SETLE:
646 case ISD::SETGT:
647 case ISD::SETGE: return 1;
648 case ISD::SETULT:
649 case ISD::SETULE:
650 case ISD::SETUGT:
651 case ISD::SETUGE: return 2;
652 }
653}
654
656 EVT Type) {
657 bool IsInteger = Type.isInteger();
658 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
659 // Cannot fold a signed integer setcc with an unsigned integer setcc.
660 return ISD::SETCC_INVALID;
661
662 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
663
664 // If the N and U bits get set, then the resultant comparison DOES suddenly
665 // care about orderedness, and it is true when ordered.
666 if (Op > ISD::SETTRUE2)
667 Op &= ~16; // Clear the U bit if the N bit is set.
668
669 // Canonicalize illegal integer setcc's.
670 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
671 Op = ISD::SETNE;
672
673 return ISD::CondCode(Op);
674}
675
677 EVT Type) {
678 bool IsInteger = Type.isInteger();
679 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
680 // Cannot fold a signed setcc with an unsigned setcc.
681 return ISD::SETCC_INVALID;
682
683 // Combine all of the condition bits.
684 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
685
686 // Canonicalize illegal integer setcc's.
687 if (IsInteger) {
688 switch (Result) {
689 default: break;
690 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
691 case ISD::SETOEQ: // SETEQ & SETU[LG]E
692 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
693 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
694 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
695 }
696 }
697
698 return Result;
699}
700
701//===----------------------------------------------------------------------===//
702// SDNode Profile Support
703//===----------------------------------------------------------------------===//
704
705/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
706static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
707 ID.AddInteger(OpC);
708}
709
710/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
711/// solely with their pointer.
713 ID.AddPointer(VTList.VTs);
714}
715
716/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
719 for (const auto &Op : Ops) {
720 ID.AddPointer(Op.getNode());
721 ID.AddInteger(Op.getResNo());
722 }
723}
724
725/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
728 for (const auto &Op : Ops) {
729 ID.AddPointer(Op.getNode());
730 ID.AddInteger(Op.getResNo());
731 }
732}
733
734static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
735 SDVTList VTList, ArrayRef<SDValue> OpList) {
736 AddNodeIDOpcode(ID, OpC);
737 AddNodeIDValueTypes(ID, VTList);
738 AddNodeIDOperands(ID, OpList);
739}
740
741/// If this is an SDNode with special info, add this info to the NodeID data.
743 switch (N->getOpcode()) {
746 case ISD::MCSymbol:
747 llvm_unreachable("Should only be used on nodes with operands");
748 default: break; // Normal nodes don't need extra info.
750 case ISD::Constant: {
752 ID.AddPointer(C->getConstantIntValue());
753 ID.AddBoolean(C->isOpaque());
754 break;
755 }
757 case ISD::ConstantFP:
758 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
759 break;
765 ID.AddPointer(GA->getGlobal());
766 ID.AddInteger(GA->getOffset());
767 ID.AddInteger(GA->getTargetFlags());
768 break;
769 }
770 case ISD::BasicBlock:
772 break;
773 case ISD::Register:
774 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
775 break;
777 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
778 break;
779 case ISD::SRCVALUE:
780 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
781 break;
782 case ISD::FrameIndex:
784 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
785 break;
786 case ISD::PSEUDO_PROBE:
787 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
788 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
789 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
790 break;
791 case ISD::JumpTable:
793 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
794 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
795 break;
799 ID.AddInteger(CP->getAlign().value());
800 ID.AddInteger(CP->getOffset());
801 if (CP->isMachineConstantPoolEntry())
802 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
803 else
804 ID.AddPointer(CP->getConstVal());
805 ID.AddInteger(CP->getTargetFlags());
806 break;
807 }
808 case ISD::TargetIndex: {
810 ID.AddInteger(TI->getIndex());
811 ID.AddInteger(TI->getOffset());
812 ID.AddInteger(TI->getTargetFlags());
813 break;
814 }
815 case ISD::LOAD: {
816 const LoadSDNode *LD = cast<LoadSDNode>(N);
817 ID.AddInteger(LD->getMemoryVT().getRawBits());
818 ID.AddInteger(LD->getRawSubclassData());
819 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
820 ID.AddInteger(LD->getMemOperand()->getFlags());
821 break;
822 }
823 case ISD::STORE: {
824 const StoreSDNode *ST = cast<StoreSDNode>(N);
825 ID.AddInteger(ST->getMemoryVT().getRawBits());
826 ID.AddInteger(ST->getRawSubclassData());
827 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
828 ID.AddInteger(ST->getMemOperand()->getFlags());
829 break;
830 }
831 case ISD::VP_LOAD: {
832 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
833 ID.AddInteger(ELD->getMemoryVT().getRawBits());
834 ID.AddInteger(ELD->getRawSubclassData());
835 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
836 ID.AddInteger(ELD->getMemOperand()->getFlags());
837 break;
838 }
839 case ISD::VP_LOAD_FF: {
840 const auto *LD = cast<VPLoadFFSDNode>(N);
841 ID.AddInteger(LD->getMemoryVT().getRawBits());
842 ID.AddInteger(LD->getRawSubclassData());
843 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
844 ID.AddInteger(LD->getMemOperand()->getFlags());
845 break;
846 }
847 case ISD::VP_STORE: {
848 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
849 ID.AddInteger(EST->getMemoryVT().getRawBits());
850 ID.AddInteger(EST->getRawSubclassData());
851 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
852 ID.AddInteger(EST->getMemOperand()->getFlags());
853 break;
854 }
855 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
857 ID.AddInteger(SLD->getMemoryVT().getRawBits());
858 ID.AddInteger(SLD->getRawSubclassData());
859 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
860 break;
861 }
862 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
864 ID.AddInteger(SST->getMemoryVT().getRawBits());
865 ID.AddInteger(SST->getRawSubclassData());
866 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
867 break;
868 }
869 case ISD::VP_GATHER: {
871 ID.AddInteger(EG->getMemoryVT().getRawBits());
872 ID.AddInteger(EG->getRawSubclassData());
873 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
874 ID.AddInteger(EG->getMemOperand()->getFlags());
875 break;
876 }
877 case ISD::VP_SCATTER: {
879 ID.AddInteger(ES->getMemoryVT().getRawBits());
880 ID.AddInteger(ES->getRawSubclassData());
881 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
882 ID.AddInteger(ES->getMemOperand()->getFlags());
883 break;
884 }
885 case ISD::MLOAD: {
887 ID.AddInteger(MLD->getMemoryVT().getRawBits());
888 ID.AddInteger(MLD->getRawSubclassData());
889 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
890 ID.AddInteger(MLD->getMemOperand()->getFlags());
891 break;
892 }
893 case ISD::MSTORE: {
895 ID.AddInteger(MST->getMemoryVT().getRawBits());
896 ID.AddInteger(MST->getRawSubclassData());
897 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
898 ID.AddInteger(MST->getMemOperand()->getFlags());
899 break;
900 }
901 case ISD::MGATHER: {
903 ID.AddInteger(MG->getMemoryVT().getRawBits());
904 ID.AddInteger(MG->getRawSubclassData());
905 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
906 ID.AddInteger(MG->getMemOperand()->getFlags());
907 break;
908 }
909 case ISD::MSCATTER: {
911 ID.AddInteger(MS->getMemoryVT().getRawBits());
912 ID.AddInteger(MS->getRawSubclassData());
913 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
914 ID.AddInteger(MS->getMemOperand()->getFlags());
915 break;
916 }
917 case ISD::ATOMIC_CMP_SWAP:
918 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
919 case ISD::ATOMIC_SWAP:
920 case ISD::ATOMIC_LOAD_ADD:
921 case ISD::ATOMIC_LOAD_SUB:
922 case ISD::ATOMIC_LOAD_AND:
923 case ISD::ATOMIC_LOAD_CLR:
924 case ISD::ATOMIC_LOAD_OR:
925 case ISD::ATOMIC_LOAD_XOR:
926 case ISD::ATOMIC_LOAD_NAND:
927 case ISD::ATOMIC_LOAD_MIN:
928 case ISD::ATOMIC_LOAD_MAX:
929 case ISD::ATOMIC_LOAD_UMIN:
930 case ISD::ATOMIC_LOAD_UMAX:
931 case ISD::ATOMIC_LOAD:
932 case ISD::ATOMIC_STORE: {
933 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
934 ID.AddInteger(AT->getMemoryVT().getRawBits());
935 ID.AddInteger(AT->getRawSubclassData());
936 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
937 ID.AddInteger(AT->getMemOperand()->getFlags());
938 break;
939 }
940 case ISD::VECTOR_SHUFFLE: {
941 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
942 for (int M : Mask)
943 ID.AddInteger(M);
944 break;
945 }
946 case ISD::ADDRSPACECAST: {
948 ID.AddInteger(ASC->getSrcAddressSpace());
949 ID.AddInteger(ASC->getDestAddressSpace());
950 break;
951 }
953 case ISD::BlockAddress: {
955 ID.AddPointer(BA->getBlockAddress());
956 ID.AddInteger(BA->getOffset());
957 ID.AddInteger(BA->getTargetFlags());
958 break;
959 }
960 case ISD::AssertAlign:
961 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
962 break;
963 case ISD::PREFETCH:
966 // Handled by MemIntrinsicSDNode check after the switch.
967 break;
968 case ISD::MDNODE_SDNODE:
969 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
970 break;
971 } // end switch (N->getOpcode())
972
973 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
974 // to check.
975 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
976 ID.AddInteger(MN->getRawSubclassData());
977 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
978 ID.AddInteger(MN->getMemOperand()->getFlags());
979 ID.AddInteger(MN->getMemoryVT().getRawBits());
980 }
981}
982
983/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
984/// data.
985static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
986 AddNodeIDOpcode(ID, N->getOpcode());
987 // Add the return value info.
988 AddNodeIDValueTypes(ID, N->getVTList());
989 // Add the operand info.
990 AddNodeIDOperands(ID, N->ops());
991
992 // Handle SDNode leafs with special info.
994}
995
996//===----------------------------------------------------------------------===//
997// SelectionDAG Class
998//===----------------------------------------------------------------------===//
999
1000/// doNotCSE - Return true if CSE should not be performed for this node.
1001static bool doNotCSE(SDNode *N) {
1002 if (N->getValueType(0) == MVT::Glue)
1003 return true; // Never CSE anything that produces a glue result.
1004
1005 switch (N->getOpcode()) {
1006 default: break;
1007 case ISD::HANDLENODE:
1008 case ISD::EH_LABEL:
1009 return true; // Never CSE these nodes.
1010 }
1011
1012 // Check that remaining values produced are not flags.
1013 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1014 if (N->getValueType(i) == MVT::Glue)
1015 return true; // Never CSE anything that produces a glue result.
1016
1017 return false;
1018}
1019
1020/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1021/// SelectionDAG.
1023 // Create a dummy node (which is not added to allnodes), that adds a reference
1024 // to the root node, preventing it from being deleted.
1025 HandleSDNode Dummy(getRoot());
1026
1027 SmallVector<SDNode*, 128> DeadNodes;
1028
1029 // Add all obviously-dead nodes to the DeadNodes worklist.
1030 for (SDNode &Node : allnodes())
1031 if (Node.use_empty())
1032 DeadNodes.push_back(&Node);
1033
1034 RemoveDeadNodes(DeadNodes);
1035
1036 // If the root changed (e.g. it was a dead load, update the root).
1037 setRoot(Dummy.getValue());
1038}
1039
1040/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1041/// given list, and any nodes that become unreachable as a result.
1043
1044 // Process the worklist, deleting the nodes and adding their uses to the
1045 // worklist.
1046 while (!DeadNodes.empty()) {
1047 SDNode *N = DeadNodes.pop_back_val();
1048 // Skip to next node if we've already managed to delete the node. This could
1049 // happen if replacing a node causes a node previously added to the node to
1050 // be deleted.
1051 if (N->getOpcode() == ISD::DELETED_NODE)
1052 continue;
1053
1054 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1055 DUL->NodeDeleted(N, nullptr);
1056
1057 // Take the node out of the appropriate CSE map.
1058 RemoveNodeFromCSEMaps(N);
1059
1060 // Next, brutally remove the operand list. This is safe to do, as there are
1061 // no cycles in the graph.
1062 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1063 SDUse &Use = *I++;
1064 SDNode *Operand = Use.getNode();
1065 Use.set(SDValue());
1066
1067 // Now that we removed this operand, see if there are no uses of it left.
1068 if (Operand->use_empty())
1069 DeadNodes.push_back(Operand);
1070 }
1071
1072 DeallocateNode(N);
1073 }
1074}
1075
1077 SmallVector<SDNode*, 16> DeadNodes(1, N);
1078
1079 // Create a dummy node that adds a reference to the root node, preventing
1080 // it from being deleted. (This matters if the root is an operand of the
1081 // dead node.)
1082 HandleSDNode Dummy(getRoot());
1083
1084 RemoveDeadNodes(DeadNodes);
1085}
1086
1088 // First take this out of the appropriate CSE map.
1089 RemoveNodeFromCSEMaps(N);
1090
1091 // Finally, remove uses due to operands of this node, remove from the
1092 // AllNodes list, and delete the node.
1093 DeleteNodeNotInCSEMaps(N);
1094}
1095
1096void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1097 assert(N->getIterator() != AllNodes.begin() &&
1098 "Cannot delete the entry node!");
1099 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1100
1101 // Drop all of the operands and decrement used node's use counts.
1102 N->DropOperands();
1103
1104 DeallocateNode(N);
1105}
1106
1107void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1108 assert(!(V->isVariadic() && isParameter));
1109 if (isParameter)
1110 ByvalParmDbgValues.push_back(V);
1111 else
1112 DbgValues.push_back(V);
1113 for (const SDNode *Node : V->getSDNodes())
1114 if (Node)
1115 DbgValMap[Node].push_back(V);
1116}
1117
1119 DbgValMapType::iterator I = DbgValMap.find(Node);
1120 if (I == DbgValMap.end())
1121 return;
1122 for (auto &Val: I->second)
1123 Val->setIsInvalidated();
1124 DbgValMap.erase(I);
1125}
1126
1127void SelectionDAG::DeallocateNode(SDNode *N) {
1128 // If we have operands, deallocate them.
1130
1131 NodeAllocator.Deallocate(AllNodes.remove(N));
1132
1133 // Set the opcode to DELETED_NODE to help catch bugs when node
1134 // memory is reallocated.
1135 // FIXME: There are places in SDag that have grown a dependency on the opcode
1136 // value in the released node.
1137 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1138 N->NodeType = ISD::DELETED_NODE;
1139
1140 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1141 // them and forget about that node.
1142 DbgInfo->erase(N);
1143
1144 // Invalidate extra info.
1145 SDEI.erase(N);
1146}
1147
1148#ifndef NDEBUG
1149/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1150void SelectionDAG::verifyNode(SDNode *N) const {
1151 switch (N->getOpcode()) {
1152 default:
1153 if (N->isTargetOpcode())
1155 break;
1156 case ISD::BUILD_PAIR: {
1157 EVT VT = N->getValueType(0);
1158 assert(N->getNumValues() == 1 && "Too many results!");
1159 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1160 "Wrong return type!");
1161 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1162 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1163 "Mismatched operand types!");
1164 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1165 "Wrong operand type!");
1166 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1167 "Wrong return type size");
1168 break;
1169 }
1170 case ISD::BUILD_VECTOR: {
1171 assert(N->getNumValues() == 1 && "Too many results!");
1172 assert(N->getValueType(0).isVector() && "Wrong return type!");
1173 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1174 "Wrong number of operands!");
1175 EVT EltVT = N->getValueType(0).getVectorElementType();
1176 for (const SDUse &Op : N->ops()) {
1177 assert((Op.getValueType() == EltVT ||
1178 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1179 EltVT.bitsLE(Op.getValueType()))) &&
1180 "Wrong operand type!");
1181 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1182 "Operands must all have the same type");
1183 }
1184 break;
1185 }
1186 }
1187}
1188#endif // NDEBUG
1189
1190/// Insert a newly allocated node into the DAG.
1191///
1192/// Handles insertion into the all nodes list and CSE map, as well as
1193/// verification and other common operations when a new node is allocated.
1194void SelectionDAG::InsertNode(SDNode *N) {
1195 AllNodes.push_back(N);
1196#ifndef NDEBUG
1197 N->PersistentId = NextPersistentId++;
1198 verifyNode(N);
1199#endif
1200 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1201 DUL->NodeInserted(N);
1202}
1203
1204/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1205/// correspond to it. This is useful when we're about to delete or repurpose
1206/// the node. We don't want future request for structurally identical nodes
1207/// to return N anymore.
1208bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1209 bool Erased = false;
1210 switch (N->getOpcode()) {
1211 case ISD::HANDLENODE: return false; // noop.
1212 case ISD::CONDCODE:
1213 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1214 "Cond code doesn't exist!");
1215 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1216 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1217 break;
1219 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1220 break;
1222 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1223 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1224 ESN->getSymbol(), ESN->getTargetFlags()));
1225 break;
1226 }
1227 case ISD::MCSymbol: {
1228 auto *MCSN = cast<MCSymbolSDNode>(N);
1229 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1230 break;
1231 }
1232 case ISD::VALUETYPE: {
1233 EVT VT = cast<VTSDNode>(N)->getVT();
1234 if (VT.isExtended()) {
1235 Erased = ExtendedValueTypeNodes.erase(VT);
1236 } else {
1237 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1238 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1239 }
1240 break;
1241 }
1242 default:
1243 // Remove it from the CSE Map.
1244 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1245 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1246 Erased = CSEMap.RemoveNode(N);
1247 break;
1248 }
1249#ifndef NDEBUG
1250 // Verify that the node was actually in one of the CSE maps, unless it has a
1251 // glue result (which cannot be CSE'd) or is one of the special cases that are
1252 // not subject to CSE.
1253 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1254 !N->isMachineOpcode() && !doNotCSE(N)) {
1255 N->dump(this);
1256 dbgs() << "\n";
1257 llvm_unreachable("Node is not in map!");
1258 }
1259#endif
1260 return Erased;
1261}
1262
1263/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1264/// maps and modified in place. Add it back to the CSE maps, unless an identical
1265/// node already exists, in which case transfer all its users to the existing
1266/// node. This transfer can potentially trigger recursive merging.
1267void
1268SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1269 // For node types that aren't CSE'd, just act as if no identical node
1270 // already exists.
1271 if (!doNotCSE(N)) {
1272 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1273 if (Existing != N) {
1274 // If there was already an existing matching node, use ReplaceAllUsesWith
1275 // to replace the dead one with the existing one. This can cause
1276 // recursive merging of other unrelated nodes down the line.
1277 Existing->intersectFlagsWith(N->getFlags());
1278 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1279 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1280 ReplaceAllUsesWith(N, Existing);
1281
1282 // N is now dead. Inform the listeners and delete it.
1283 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1284 DUL->NodeDeleted(N, Existing);
1285 DeleteNodeNotInCSEMaps(N);
1286 return;
1287 }
1288 }
1289
1290 // If the node doesn't already exist, we updated it. Inform listeners.
1291 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1292 DUL->NodeUpdated(N);
1293}
1294
1295/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1296/// were replaced with those specified. If this node is never memoized,
1297/// return null, otherwise return a pointer to the slot it would take. If a
1298/// node already exists with these operands, the slot will be non-null.
1299SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1300 void *&InsertPos) {
1301 if (doNotCSE(N))
1302 return nullptr;
1303
1304 SDValue Ops[] = { Op };
1305 FoldingSetNodeID ID;
1306 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1308 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1309 if (Node)
1310 Node->intersectFlagsWith(N->getFlags());
1311 return Node;
1312}
1313
1314/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1315/// were replaced with those specified. If this node is never memoized,
1316/// return null, otherwise return a pointer to the slot it would take. If a
1317/// node already exists with these operands, the slot will be non-null.
1318SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1319 SDValue Op1, SDValue Op2,
1320 void *&InsertPos) {
1321 if (doNotCSE(N))
1322 return nullptr;
1323
1324 SDValue Ops[] = { Op1, Op2 };
1325 FoldingSetNodeID ID;
1326 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1328 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1329 if (Node)
1330 Node->intersectFlagsWith(N->getFlags());
1331 return Node;
1332}
1333
1334/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1335/// were replaced with those specified. If this node is never memoized,
1336/// return null, otherwise return a pointer to the slot it would take. If a
1337/// node already exists with these operands, the slot will be non-null.
1338SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1339 void *&InsertPos) {
1340 if (doNotCSE(N))
1341 return nullptr;
1342
1343 FoldingSetNodeID ID;
1344 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1346 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1347 if (Node)
1348 Node->intersectFlagsWith(N->getFlags());
1349 return Node;
1350}
1351
1353 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1354 : VT.getTypeForEVT(*getContext());
1355
1356 return getDataLayout().getABITypeAlign(Ty);
1357}
1358
1359// EntryNode could meaningfully have debug info if we can find it...
1361 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1362 getVTList(MVT::Other, MVT::Glue)),
1363 Root(getEntryNode()) {
1364 InsertNode(&EntryNode);
1365 DbgInfo = new SDDbgInfo();
1366}
1367
1369 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1370 const TargetLibraryInfo *LibraryInfo,
1371 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1373 FunctionVarLocs const *VarLocs) {
1374 MF = &NewMF;
1375 SDAGISelPass = PassPtr;
1376 ORE = &NewORE;
1379 LibInfo = LibraryInfo;
1380 Context = &MF->getFunction().getContext();
1381 UA = NewUA;
1382 PSI = PSIin;
1383 BFI = BFIin;
1384 MMI = &MMIin;
1385 FnVarLocs = VarLocs;
1386}
1387
1389 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1390 allnodes_clear();
1391 OperandRecycler.clear(OperandAllocator);
1392 delete DbgInfo;
1393}
1394
1396 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1397}
1398
1399void SelectionDAG::allnodes_clear() {
1400 assert(&*AllNodes.begin() == &EntryNode);
1401 AllNodes.remove(AllNodes.begin());
1402 while (!AllNodes.empty())
1403 DeallocateNode(&AllNodes.front());
1404#ifndef NDEBUG
1405 NextPersistentId = 0;
1406#endif
1407}
1408
1409SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1410 void *&InsertPos) {
1411 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1412 if (N) {
1413 switch (N->getOpcode()) {
1414 default: break;
1415 case ISD::Constant:
1416 case ISD::ConstantFP:
1417 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1418 "debug location. Use another overload.");
1419 }
1420 }
1421 return N;
1422}
1423
1424SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1425 const SDLoc &DL, void *&InsertPos) {
1426 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1427 if (N) {
1428 switch (N->getOpcode()) {
1429 case ISD::Constant:
1430 case ISD::ConstantFP:
1431 // Erase debug location from the node if the node is used at several
1432 // different places. Do not propagate one location to all uses as it
1433 // will cause a worse single stepping debugging experience.
1434 if (N->getDebugLoc() != DL.getDebugLoc())
1435 N->setDebugLoc(DebugLoc());
1436 break;
1437 default:
1438 // When the node's point of use is located earlier in the instruction
1439 // sequence than its prior point of use, update its debug info to the
1440 // earlier location.
1441 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1442 N->setDebugLoc(DL.getDebugLoc());
1443 break;
1444 }
1445 }
1446 return N;
1447}
1448
1450 allnodes_clear();
1451 OperandRecycler.clear(OperandAllocator);
1452 OperandAllocator.Reset();
1453 CSEMap.clear();
1454
1455 ExtendedValueTypeNodes.clear();
1456 ExternalSymbols.clear();
1457 TargetExternalSymbols.clear();
1458 MCSymbols.clear();
1459 SDEI.clear();
1460 llvm::fill(CondCodeNodes, nullptr);
1461 llvm::fill(ValueTypeNodes, nullptr);
1462
1463 EntryNode.UseList = nullptr;
1464 InsertNode(&EntryNode);
1465 Root = getEntryNode();
1466 DbgInfo->clear();
1467}
1468
1470 return VT.bitsGT(Op.getValueType())
1471 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1472 : getNode(ISD::FP_ROUND, DL, VT, Op,
1473 getIntPtrConstant(0, DL, /*isTarget=*/true));
1474}
1475
1476std::pair<SDValue, SDValue>
1478 const SDLoc &DL, EVT VT) {
1479 assert(!VT.bitsEq(Op.getValueType()) &&
1480 "Strict no-op FP extend/round not allowed.");
1481 SDValue Res =
1482 VT.bitsGT(Op.getValueType())
1483 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1484 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1485 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1486
1487 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1488}
1489
1491 return VT.bitsGT(Op.getValueType()) ?
1492 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1493 getNode(ISD::TRUNCATE, DL, VT, Op);
1494}
1495
1497 return VT.bitsGT(Op.getValueType()) ?
1498 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1499 getNode(ISD::TRUNCATE, DL, VT, Op);
1500}
1501
1503 return VT.bitsGT(Op.getValueType()) ?
1504 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1505 getNode(ISD::TRUNCATE, DL, VT, Op);
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(EVT::getIntegerVT(*Context, Size), Op);
1517 if (DestOp.getValueType() == VT)
1518 return DestOp;
1519
1520 return getAnyExtOrTrunc(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 getSExtOrTrunc(DestOp, DL, VT);
1536}
1537
1539 EVT VT) {
1540 assert(!VT.isVector());
1541 auto Type = Op.getValueType();
1542 SDValue DestOp;
1543 if (Type == VT)
1544 return Op;
1545 auto Size = Op.getValueSizeInBits();
1546 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1547 if (DestOp.getValueType() == VT)
1548 return DestOp;
1549
1550 return getZExtOrTrunc(DestOp, DL, VT);
1551}
1552
1554 EVT OpVT) {
1555 if (VT.bitsLE(Op.getValueType()))
1556 return getNode(ISD::TRUNCATE, SL, VT, Op);
1557
1558 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1559 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1560}
1561
1563 EVT OpVT = Op.getValueType();
1564 assert(VT.isInteger() && OpVT.isInteger() &&
1565 "Cannot getZeroExtendInReg FP types");
1566 assert(VT.isVector() == OpVT.isVector() &&
1567 "getZeroExtendInReg type should be vector iff the operand "
1568 "type is vector!");
1569 assert((!VT.isVector() ||
1571 "Vector element counts must match in getZeroExtendInReg");
1572 assert(VT.bitsLE(OpVT) && "Not extending!");
1573 if (OpVT == VT)
1574 return Op;
1576 VT.getScalarSizeInBits());
1577 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1578}
1579
1581 SDValue EVL, const SDLoc &DL,
1582 EVT VT) {
1583 EVT OpVT = Op.getValueType();
1584 assert(VT.isInteger() && OpVT.isInteger() &&
1585 "Cannot getVPZeroExtendInReg FP types");
1586 assert(VT.isVector() && OpVT.isVector() &&
1587 "getVPZeroExtendInReg type and operand type should be vector!");
1589 "Vector element counts must match in getZeroExtendInReg");
1590 assert(VT.bitsLE(OpVT) && "Not extending!");
1591 if (OpVT == VT)
1592 return Op;
1594 VT.getScalarSizeInBits());
1595 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1596 EVL);
1597}
1598
1600 // Only unsigned pointer semantics are supported right now. In the future this
1601 // might delegate to TLI to check pointer signedness.
1602 return getZExtOrTrunc(Op, DL, VT);
1603}
1604
1606 // Only unsigned pointer semantics are supported right now. In the future this
1607 // might delegate to TLI to check pointer signedness.
1608 return getZeroExtendInReg(Op, DL, VT);
1609}
1610
1612 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1613}
1614
1615/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1617 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1618}
1619
1621 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1622 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1623}
1624
1626 SDValue Mask, SDValue EVL, EVT VT) {
1627 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1628 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1629}
1630
1632 SDValue Mask, SDValue EVL) {
1633 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1634}
1635
1637 SDValue Mask, SDValue EVL) {
1638 if (VT.bitsGT(Op.getValueType()))
1639 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1640 if (VT.bitsLT(Op.getValueType()))
1641 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1642 return Op;
1643}
1644
1646 EVT OpVT) {
1647 if (!V)
1648 return getConstant(0, DL, VT);
1649
1650 switch (TLI->getBooleanContents(OpVT)) {
1653 return getConstant(1, DL, VT);
1655 return getAllOnesConstant(DL, VT);
1656 }
1657 llvm_unreachable("Unexpected boolean content enum!");
1658}
1659
1661 bool isT, bool isO) {
1662 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1663 DL, VT, isT, isO);
1664}
1665
1667 bool isT, bool isO) {
1668 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1669}
1670
1672 EVT VT, bool isT, bool isO) {
1673 assert(VT.isInteger() && "Cannot create FP integer constant!");
1674
1675 EVT EltVT = VT.getScalarType();
1676 const ConstantInt *Elt = &Val;
1677
1678 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1679 // to-be-splatted scalar ConstantInt.
1680 if (isa<VectorType>(Elt->getType()))
1681 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1682
1683 // In some cases the vector type is legal but the element type is illegal and
1684 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1685 // inserted value (the type does not need to match the vector element type).
1686 // Any extra bits introduced will be truncated away.
1687 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1689 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1690 APInt NewVal;
1691 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1692 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1693 else
1694 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1695 Elt = ConstantInt::get(*getContext(), NewVal);
1696 }
1697 // In other cases the element type is illegal and needs to be expanded, for
1698 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1699 // the value into n parts and use a vector type with n-times the elements.
1700 // Then bitcast to the type requested.
1701 // Legalizing constants too early makes the DAGCombiner's job harder so we
1702 // only legalize if the DAG tells us we must produce legal types.
1703 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1704 TLI->getTypeAction(*getContext(), EltVT) ==
1706 const APInt &NewVal = Elt->getValue();
1707 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1708 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1709
1710 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1711 if (VT.isScalableVector() ||
1712 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1713 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1714 "Can only handle an even split!");
1715 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1716
1717 SmallVector<SDValue, 2> ScalarParts;
1718 for (unsigned i = 0; i != Parts; ++i)
1719 ScalarParts.push_back(getConstant(
1720 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1721 ViaEltVT, isT, isO));
1722
1723 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1724 }
1725
1726 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1727 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1728
1729 // Check the temporary vector is the correct size. If this fails then
1730 // getTypeToTransformTo() probably returned a type whose size (in bits)
1731 // isn't a power-of-2 factor of the requested type size.
1732 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1733
1734 SmallVector<SDValue, 2> EltParts;
1735 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1736 EltParts.push_back(getConstant(
1737 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1738 ViaEltVT, isT, isO));
1739
1740 // EltParts is currently in little endian order. If we actually want
1741 // big-endian order then reverse it now.
1742 if (getDataLayout().isBigEndian())
1743 std::reverse(EltParts.begin(), EltParts.end());
1744
1745 // The elements must be reversed when the element order is different
1746 // to the endianness of the elements (because the BITCAST is itself a
1747 // vector shuffle in this situation). However, we do not need any code to
1748 // perform this reversal because getConstant() is producing a vector
1749 // splat.
1750 // This situation occurs in MIPS MSA.
1751
1753 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1754 llvm::append_range(Ops, EltParts);
1755
1756 SDValue V =
1757 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1758 return V;
1759 }
1760
1761 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1762 "APInt size does not match type size!");
1763 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1764 SDVTList VTs = getVTList(EltVT);
1766 AddNodeIDNode(ID, Opc, VTs, {});
1767 ID.AddPointer(Elt);
1768 ID.AddBoolean(isO);
1769 void *IP = nullptr;
1770 SDNode *N = nullptr;
1771 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1772 if (!VT.isVector())
1773 return SDValue(N, 0);
1774
1775 if (!N) {
1776 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1777 CSEMap.InsertNode(N, IP);
1778 InsertNode(N);
1779 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1780 }
1781
1782 SDValue Result(N, 0);
1783 if (VT.isVector())
1784 Result = getSplat(VT, DL, Result);
1785 return Result;
1786}
1787
1789 bool isT, bool isO) {
1790 unsigned Size = VT.getScalarSizeInBits();
1791 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1792}
1793
1795 bool IsOpaque) {
1797 IsTarget, IsOpaque);
1798}
1799
1801 bool isTarget) {
1802 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1803}
1804
1806 const SDLoc &DL) {
1807 assert(VT.isInteger() && "Shift amount is not an integer type!");
1808 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1809 return getConstant(Val, DL, ShiftVT);
1810}
1811
1813 const SDLoc &DL) {
1814 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1815 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1816}
1817
1819 bool isTarget) {
1820 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1821}
1822
1824 bool isTarget) {
1825 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1826}
1827
1829 EVT VT, bool isTarget) {
1830 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1831
1832 EVT EltVT = VT.getScalarType();
1833 const ConstantFP *Elt = &V;
1834
1835 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1836 // the to-be-splatted scalar ConstantFP.
1837 if (isa<VectorType>(Elt->getType()))
1838 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1839
1840 // Do the map lookup using the actual bit pattern for the floating point
1841 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1842 // we don't have issues with SNANs.
1843 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1844 SDVTList VTs = getVTList(EltVT);
1846 AddNodeIDNode(ID, Opc, VTs, {});
1847 ID.AddPointer(Elt);
1848 void *IP = nullptr;
1849 SDNode *N = nullptr;
1850 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1851 if (!VT.isVector())
1852 return SDValue(N, 0);
1853
1854 if (!N) {
1855 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1856 CSEMap.InsertNode(N, IP);
1857 InsertNode(N);
1858 }
1859
1860 SDValue Result(N, 0);
1861 if (VT.isVector())
1862 Result = getSplat(VT, DL, Result);
1863 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1864 return Result;
1865}
1866
1868 bool isTarget) {
1869 EVT EltVT = VT.getScalarType();
1870 if (EltVT == MVT::f32)
1871 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1872 if (EltVT == MVT::f64)
1873 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1874 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1875 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1876 bool Ignored;
1877 APFloat APF = APFloat(Val);
1879 &Ignored);
1880 return getConstantFP(APF, DL, VT, isTarget);
1881 }
1882 llvm_unreachable("Unsupported type in getConstantFP");
1883}
1884
1886 EVT VT, int64_t Offset, bool isTargetGA,
1887 unsigned TargetFlags) {
1888 assert((TargetFlags == 0 || isTargetGA) &&
1889 "Cannot set target flags on target-independent globals");
1890
1891 // Truncate (with sign-extension) the offset value to the pointer size.
1893 if (BitWidth < 64)
1895
1896 unsigned Opc;
1897 if (GV->isThreadLocal())
1899 else
1901
1902 SDVTList VTs = getVTList(VT);
1904 AddNodeIDNode(ID, Opc, VTs, {});
1905 ID.AddPointer(GV);
1906 ID.AddInteger(Offset);
1907 ID.AddInteger(TargetFlags);
1908 void *IP = nullptr;
1909 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1910 return SDValue(E, 0);
1911
1912 auto *N = newSDNode<GlobalAddressSDNode>(
1913 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1914 CSEMap.InsertNode(N, IP);
1915 InsertNode(N);
1916 return SDValue(N, 0);
1917}
1918
1920 SDVTList VTs = getVTList(MVT::Untyped);
1923 ID.AddPointer(GV);
1924 void *IP = nullptr;
1925 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1926 return SDValue(E, 0);
1927
1928 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1929 CSEMap.InsertNode(N, IP);
1930 InsertNode(N);
1931 return SDValue(N, 0);
1932}
1933
1934SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1935 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1936 SDVTList VTs = getVTList(VT);
1938 AddNodeIDNode(ID, Opc, VTs, {});
1939 ID.AddInteger(FI);
1940 void *IP = nullptr;
1941 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1942 return SDValue(E, 0);
1943
1944 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1945 CSEMap.InsertNode(N, IP);
1946 InsertNode(N);
1947 return SDValue(N, 0);
1948}
1949
1950SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1951 unsigned TargetFlags) {
1952 assert((TargetFlags == 0 || isTarget) &&
1953 "Cannot set target flags on target-independent jump tables");
1954 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1955 SDVTList VTs = getVTList(VT);
1957 AddNodeIDNode(ID, Opc, VTs, {});
1958 ID.AddInteger(JTI);
1959 ID.AddInteger(TargetFlags);
1960 void *IP = nullptr;
1961 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1962 return SDValue(E, 0);
1963
1964 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1965 CSEMap.InsertNode(N, IP);
1966 InsertNode(N);
1967 return SDValue(N, 0);
1968}
1969
1971 const SDLoc &DL) {
1973 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1974 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
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 = shouldOptForSize()
1984 ? getDataLayout().getABITypeAlign(C->getType())
1985 : getDataLayout().getPrefTypeAlign(C->getType());
1986 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1987 SDVTList VTs = getVTList(VT);
1989 AddNodeIDNode(ID, Opc, VTs, {});
1990 ID.AddInteger(Alignment->value());
1991 ID.AddInteger(Offset);
1992 ID.AddPointer(C);
1993 ID.AddInteger(TargetFlags);
1994 void *IP = nullptr;
1995 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1996 return SDValue(E, 0);
1997
1998 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1999 TargetFlags);
2000 CSEMap.InsertNode(N, IP);
2001 InsertNode(N);
2002 SDValue V = SDValue(N, 0);
2003 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2004 return V;
2005}
2006
2008 MaybeAlign Alignment, int Offset,
2009 bool isTarget, unsigned TargetFlags) {
2010 assert((TargetFlags == 0 || isTarget) &&
2011 "Cannot set target flags on target-independent globals");
2012 if (!Alignment)
2013 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2014 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2015 SDVTList VTs = getVTList(VT);
2017 AddNodeIDNode(ID, Opc, VTs, {});
2018 ID.AddInteger(Alignment->value());
2019 ID.AddInteger(Offset);
2020 C->addSelectionDAGCSEId(ID);
2021 ID.AddInteger(TargetFlags);
2022 void *IP = nullptr;
2023 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2024 return SDValue(E, 0);
2025
2026 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2027 TargetFlags);
2028 CSEMap.InsertNode(N, IP);
2029 InsertNode(N);
2030 return SDValue(N, 0);
2031}
2032
2035 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2036 ID.AddPointer(MBB);
2037 void *IP = nullptr;
2038 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2039 return SDValue(E, 0);
2040
2041 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2042 CSEMap.InsertNode(N, IP);
2043 InsertNode(N);
2044 return SDValue(N, 0);
2045}
2046
2048 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2049 ValueTypeNodes.size())
2050 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2051
2052 SDNode *&N = VT.isExtended() ?
2053 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2054
2055 if (N) return SDValue(N, 0);
2056 N = newSDNode<VTSDNode>(VT);
2057 InsertNode(N);
2058 return SDValue(N, 0);
2059}
2060
2062 SDNode *&N = ExternalSymbols[Sym];
2063 if (N) return SDValue(N, 0);
2064 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2065 InsertNode(N);
2066 return SDValue(N, 0);
2067}
2068
2069SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2070 StringRef SymName = TLI->getLibcallImplName(Libcall);
2071 return getExternalSymbol(SymName.data(), VT);
2072}
2073
2075 SDNode *&N = MCSymbols[Sym];
2076 if (N)
2077 return SDValue(N, 0);
2078 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2079 InsertNode(N);
2080 return SDValue(N, 0);
2081}
2082
2084 unsigned TargetFlags) {
2085 SDNode *&N =
2086 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2087 if (N) return SDValue(N, 0);
2088 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2089 InsertNode(N);
2090 return SDValue(N, 0);
2091}
2092
2094 if ((unsigned)Cond >= CondCodeNodes.size())
2095 CondCodeNodes.resize(Cond+1);
2096
2097 if (!CondCodeNodes[Cond]) {
2098 auto *N = newSDNode<CondCodeSDNode>(Cond);
2099 CondCodeNodes[Cond] = N;
2100 InsertNode(N);
2101 }
2102
2103 return SDValue(CondCodeNodes[Cond], 0);
2104}
2105
2107 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2108 "APInt size does not match type size!");
2109
2110 if (MulImm == 0)
2111 return getConstant(0, DL, VT);
2112
2113 const MachineFunction &MF = getMachineFunction();
2114 const Function &F = MF.getFunction();
2115 ConstantRange CR = getVScaleRange(&F, 64);
2116 if (const APInt *C = CR.getSingleElement())
2117 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2118
2119 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2120}
2121
2122/// \returns a value of type \p VT that represents the runtime value of \p
2123/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2124/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2125/// or TypeSize.
2126template <typename Ty>
2128 EVT VT, Ty Quantity) {
2129 if (Quantity.isScalable())
2130 return DAG.getVScale(
2131 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2132
2133 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2134}
2135
2137 ElementCount EC) {
2138 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2139}
2140
2142 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2143}
2144
2146 ElementCount EC) {
2147 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2148 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2149 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2150 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2151}
2152
2154 APInt One(ResVT.getScalarSizeInBits(), 1);
2155 return getStepVector(DL, ResVT, One);
2156}
2157
2159 const APInt &StepVal) {
2160 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2161 if (ResVT.isScalableVector())
2162 return getNode(
2163 ISD::STEP_VECTOR, DL, ResVT,
2164 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2165
2166 SmallVector<SDValue, 16> OpsStepConstants;
2167 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2168 OpsStepConstants.push_back(
2169 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2170 return getBuildVector(ResVT, DL, OpsStepConstants);
2171}
2172
2173/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2174/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2179
2181 SDValue N2, ArrayRef<int> Mask) {
2182 assert(VT.getVectorNumElements() == Mask.size() &&
2183 "Must have the same number of vector elements as mask elements!");
2184 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2185 "Invalid VECTOR_SHUFFLE");
2186
2187 // Canonicalize shuffle undef, undef -> undef
2188 if (N1.isUndef() && N2.isUndef())
2189 return getUNDEF(VT);
2190
2191 // Validate that all indices in Mask are within the range of the elements
2192 // input to the shuffle.
2193 int NElts = Mask.size();
2194 assert(llvm::all_of(Mask,
2195 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2196 "Index out of range");
2197
2198 // Copy the mask so we can do any needed cleanup.
2199 SmallVector<int, 8> MaskVec(Mask);
2200
2201 // Canonicalize shuffle v, v -> v, undef
2202 if (N1 == N2) {
2203 N2 = getUNDEF(VT);
2204 for (int i = 0; i != NElts; ++i)
2205 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2206 }
2207
2208 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2209 if (N1.isUndef())
2210 commuteShuffle(N1, N2, MaskVec);
2211
2212 if (TLI->hasVectorBlend()) {
2213 // If shuffling a splat, try to blend the splat instead. We do this here so
2214 // that even when this arises during lowering we don't have to re-handle it.
2215 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2216 BitVector UndefElements;
2217 SDValue Splat = BV->getSplatValue(&UndefElements);
2218 if (!Splat)
2219 return;
2220
2221 for (int i = 0; i < NElts; ++i) {
2222 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2223 continue;
2224
2225 // If this input comes from undef, mark it as such.
2226 if (UndefElements[MaskVec[i] - Offset]) {
2227 MaskVec[i] = -1;
2228 continue;
2229 }
2230
2231 // If we can blend a non-undef lane, use that instead.
2232 if (!UndefElements[i])
2233 MaskVec[i] = i + Offset;
2234 }
2235 };
2236 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2237 BlendSplat(N1BV, 0);
2238 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2239 BlendSplat(N2BV, NElts);
2240 }
2241
2242 // Canonicalize all index into lhs, -> shuffle lhs, undef
2243 // Canonicalize all index into rhs, -> shuffle rhs, undef
2244 bool AllLHS = true, AllRHS = true;
2245 bool N2Undef = N2.isUndef();
2246 for (int i = 0; i != NElts; ++i) {
2247 if (MaskVec[i] >= NElts) {
2248 if (N2Undef)
2249 MaskVec[i] = -1;
2250 else
2251 AllLHS = false;
2252 } else if (MaskVec[i] >= 0) {
2253 AllRHS = false;
2254 }
2255 }
2256 if (AllLHS && AllRHS)
2257 return getUNDEF(VT);
2258 if (AllLHS && !N2Undef)
2259 N2 = getUNDEF(VT);
2260 if (AllRHS) {
2261 N1 = getUNDEF(VT);
2262 commuteShuffle(N1, N2, MaskVec);
2263 }
2264 // Reset our undef status after accounting for the mask.
2265 N2Undef = N2.isUndef();
2266 // Re-check whether both sides ended up undef.
2267 if (N1.isUndef() && N2Undef)
2268 return getUNDEF(VT);
2269
2270 // If Identity shuffle return that node.
2271 bool Identity = true, AllSame = true;
2272 for (int i = 0; i != NElts; ++i) {
2273 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2274 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2275 }
2276 if (Identity && NElts)
2277 return N1;
2278
2279 // Shuffling a constant splat doesn't change the result.
2280 if (N2Undef) {
2281 SDValue V = N1;
2282
2283 // Look through any bitcasts. We check that these don't change the number
2284 // (and size) of elements and just changes their types.
2285 while (V.getOpcode() == ISD::BITCAST)
2286 V = V->getOperand(0);
2287
2288 // A splat should always show up as a build vector node.
2289 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2290 BitVector UndefElements;
2291 SDValue Splat = BV->getSplatValue(&UndefElements);
2292 // If this is a splat of an undef, shuffling it is also undef.
2293 if (Splat && Splat.isUndef())
2294 return getUNDEF(VT);
2295
2296 bool SameNumElts =
2297 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2298
2299 // We only have a splat which can skip shuffles if there is a splatted
2300 // value and no undef lanes rearranged by the shuffle.
2301 if (Splat && UndefElements.none()) {
2302 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2303 // number of elements match or the value splatted is a zero constant.
2304 if (SameNumElts || isNullConstant(Splat))
2305 return N1;
2306 }
2307
2308 // If the shuffle itself creates a splat, build the vector directly.
2309 if (AllSame && SameNumElts) {
2310 EVT BuildVT = BV->getValueType(0);
2311 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2312 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2313
2314 // We may have jumped through bitcasts, so the type of the
2315 // BUILD_VECTOR may not match the type of the shuffle.
2316 if (BuildVT != VT)
2317 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2318 return NewBV;
2319 }
2320 }
2321 }
2322
2323 SDVTList VTs = getVTList(VT);
2325 SDValue Ops[2] = { N1, N2 };
2327 for (int i = 0; i != NElts; ++i)
2328 ID.AddInteger(MaskVec[i]);
2329
2330 void* IP = nullptr;
2331 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2332 return SDValue(E, 0);
2333
2334 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2335 // SDNode doesn't have access to it. This memory will be "leaked" when
2336 // the node is deallocated, but recovered when the NodeAllocator is released.
2337 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2338 llvm::copy(MaskVec, MaskAlloc);
2339
2340 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2341 dl.getDebugLoc(), MaskAlloc);
2342 createOperands(N, Ops);
2343
2344 CSEMap.InsertNode(N, IP);
2345 InsertNode(N);
2346 SDValue V = SDValue(N, 0);
2347 NewSDValueDbgMsg(V, "Creating new node: ", this);
2348 return V;
2349}
2350
2352 EVT VT = SV.getValueType(0);
2353 SmallVector<int, 8> MaskVec(SV.getMask());
2355
2356 SDValue Op0 = SV.getOperand(0);
2357 SDValue Op1 = SV.getOperand(1);
2358 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2359}
2360
2362 SDVTList VTs = getVTList(VT);
2364 AddNodeIDNode(ID, ISD::Register, VTs, {});
2365 ID.AddInteger(Reg.id());
2366 void *IP = nullptr;
2367 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2368 return SDValue(E, 0);
2369
2370 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2371 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2372 CSEMap.InsertNode(N, IP);
2373 InsertNode(N);
2374 return SDValue(N, 0);
2375}
2376
2379 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2380 ID.AddPointer(RegMask);
2381 void *IP = nullptr;
2382 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2383 return SDValue(E, 0);
2384
2385 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2386 CSEMap.InsertNode(N, IP);
2387 InsertNode(N);
2388 return SDValue(N, 0);
2389}
2390
2392 MCSymbol *Label) {
2393 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2394}
2395
2396SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2397 SDValue Root, MCSymbol *Label) {
2399 SDValue Ops[] = { Root };
2400 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2401 ID.AddPointer(Label);
2402 void *IP = nullptr;
2403 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2404 return SDValue(E, 0);
2405
2406 auto *N =
2407 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2408 createOperands(N, Ops);
2409
2410 CSEMap.InsertNode(N, IP);
2411 InsertNode(N);
2412 return SDValue(N, 0);
2413}
2414
2416 int64_t Offset, bool isTarget,
2417 unsigned TargetFlags) {
2418 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2419 SDVTList VTs = getVTList(VT);
2420
2422 AddNodeIDNode(ID, Opc, VTs, {});
2423 ID.AddPointer(BA);
2424 ID.AddInteger(Offset);
2425 ID.AddInteger(TargetFlags);
2426 void *IP = nullptr;
2427 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2428 return SDValue(E, 0);
2429
2430 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2431 CSEMap.InsertNode(N, IP);
2432 InsertNode(N);
2433 return SDValue(N, 0);
2434}
2435
2438 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2439 ID.AddPointer(V);
2440
2441 void *IP = nullptr;
2442 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2443 return SDValue(E, 0);
2444
2445 auto *N = newSDNode<SrcValueSDNode>(V);
2446 CSEMap.InsertNode(N, IP);
2447 InsertNode(N);
2448 return SDValue(N, 0);
2449}
2450
2453 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2454 ID.AddPointer(MD);
2455
2456 void *IP = nullptr;
2457 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2458 return SDValue(E, 0);
2459
2460 auto *N = newSDNode<MDNodeSDNode>(MD);
2461 CSEMap.InsertNode(N, IP);
2462 InsertNode(N);
2463 return SDValue(N, 0);
2464}
2465
2467 if (VT == V.getValueType())
2468 return V;
2469
2470 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2471}
2472
2474 unsigned SrcAS, unsigned DestAS) {
2475 SDVTList VTs = getVTList(VT);
2476 SDValue Ops[] = {Ptr};
2478 AddNodeIDNode(ID, ISD::ADDRSPACECAST, VTs, Ops);
2479 ID.AddInteger(SrcAS);
2480 ID.AddInteger(DestAS);
2481
2482 void *IP = nullptr;
2483 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2484 return SDValue(E, 0);
2485
2486 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2487 VTs, SrcAS, DestAS);
2488 createOperands(N, Ops);
2489
2490 CSEMap.InsertNode(N, IP);
2491 InsertNode(N);
2492 return SDValue(N, 0);
2493}
2494
2496 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2497}
2498
2499/// getShiftAmountOperand - Return the specified value casted to
2500/// the target's desired shift amount type.
2502 EVT OpTy = Op.getValueType();
2503 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2504 if (OpTy == ShTy || OpTy.isVector()) return Op;
2505
2506 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2507}
2508
2510 SDLoc dl(Node);
2512 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2513 EVT VT = Node->getValueType(0);
2514 SDValue Tmp1 = Node->getOperand(0);
2515 SDValue Tmp2 = Node->getOperand(1);
2516 const MaybeAlign MA(Node->getConstantOperandVal(3));
2517
2518 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2519 Tmp2, MachinePointerInfo(V));
2520 SDValue VAList = VAListLoad;
2521
2522 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2523 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2524 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2525
2526 VAList = getNode(
2527 ISD::AND, dl, VAList.getValueType(), VAList,
2528 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2529 }
2530
2531 // Increment the pointer, VAList, to the next vaarg
2532 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2533 getConstant(getDataLayout().getTypeAllocSize(
2534 VT.getTypeForEVT(*getContext())),
2535 dl, VAList.getValueType()));
2536 // Store the incremented VAList to the legalized pointer
2537 Tmp1 =
2538 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2539 // Load the actual argument out of the pointer VAList
2540 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2541}
2542
2544 SDLoc dl(Node);
2546 // This defaults to loading a pointer from the input and storing it to the
2547 // output, returning the chain.
2548 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2549 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2550 SDValue Tmp1 =
2551 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2552 Node->getOperand(2), MachinePointerInfo(VS));
2553 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2554 MachinePointerInfo(VD));
2555}
2556
2558 const DataLayout &DL = getDataLayout();
2559 Type *Ty = VT.getTypeForEVT(*getContext());
2560 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2561
2562 if (TLI->isTypeLegal(VT) || !VT.isVector())
2563 return RedAlign;
2564
2565 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2566 const Align StackAlign = TFI->getStackAlign();
2567
2568 // See if we can choose a smaller ABI alignment in cases where it's an
2569 // illegal vector type that will get broken down.
2570 if (RedAlign > StackAlign) {
2571 EVT IntermediateVT;
2572 MVT RegisterVT;
2573 unsigned NumIntermediates;
2574 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2575 NumIntermediates, RegisterVT);
2576 Ty = IntermediateVT.getTypeForEVT(*getContext());
2577 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2578 if (RedAlign2 < RedAlign)
2579 RedAlign = RedAlign2;
2580
2581 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2582 // If the stack is not realignable, the alignment should be limited to the
2583 // StackAlignment
2584 RedAlign = std::min(RedAlign, StackAlign);
2585 }
2586
2587 return RedAlign;
2588}
2589
2591 MachineFrameInfo &MFI = MF->getFrameInfo();
2592 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2593 int StackID = 0;
2594 if (Bytes.isScalable())
2595 StackID = TFI->getStackIDForScalableVectors();
2596 // The stack id gives an indication of whether the object is scalable or
2597 // not, so it's safe to pass in the minimum size here.
2598 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2599 false, nullptr, StackID);
2600 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2601}
2602
2604 Type *Ty = VT.getTypeForEVT(*getContext());
2605 Align StackAlign =
2606 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2607 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2608}
2609
2611 TypeSize VT1Size = VT1.getStoreSize();
2612 TypeSize VT2Size = VT2.getStoreSize();
2613 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2614 "Don't know how to choose the maximum size when creating a stack "
2615 "temporary");
2616 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2617 ? VT1Size
2618 : VT2Size;
2619
2620 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2621 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2622 const DataLayout &DL = getDataLayout();
2623 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2624 return CreateStackTemporary(Bytes, Align);
2625}
2626
2628 ISD::CondCode Cond, const SDLoc &dl) {
2629 EVT OpVT = N1.getValueType();
2630
2631 auto GetUndefBooleanConstant = [&]() {
2632 if (VT.getScalarType() == MVT::i1 ||
2633 TLI->getBooleanContents(OpVT) ==
2635 return getUNDEF(VT);
2636 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2637 // so we cannot use getUNDEF(). Return zero instead.
2638 return getConstant(0, dl, VT);
2639 };
2640
2641 // These setcc operations always fold.
2642 switch (Cond) {
2643 default: break;
2644 case ISD::SETFALSE:
2645 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2646 case ISD::SETTRUE:
2647 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2648
2649 case ISD::SETOEQ:
2650 case ISD::SETOGT:
2651 case ISD::SETOGE:
2652 case ISD::SETOLT:
2653 case ISD::SETOLE:
2654 case ISD::SETONE:
2655 case ISD::SETO:
2656 case ISD::SETUO:
2657 case ISD::SETUEQ:
2658 case ISD::SETUNE:
2659 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2660 break;
2661 }
2662
2663 if (OpVT.isInteger()) {
2664 // For EQ and NE, we can always pick a value for the undef to make the
2665 // predicate pass or fail, so we can return undef.
2666 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2667 // icmp eq/ne X, undef -> undef.
2668 if ((N1.isUndef() || N2.isUndef()) &&
2669 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2670 return GetUndefBooleanConstant();
2671
2672 // If both operands are undef, we can return undef for int comparison.
2673 // icmp undef, undef -> undef.
2674 if (N1.isUndef() && N2.isUndef())
2675 return GetUndefBooleanConstant();
2676
2677 // icmp X, X -> true/false
2678 // icmp X, undef -> true/false because undef could be X.
2679 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2680 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2681 }
2682
2684 const APInt &C2 = N2C->getAPIntValue();
2686 const APInt &C1 = N1C->getAPIntValue();
2687
2689 dl, VT, OpVT);
2690 }
2691 }
2692
2693 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2694 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2695
2696 if (N1CFP && N2CFP) {
2697 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2698 switch (Cond) {
2699 default: break;
2700 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2701 return GetUndefBooleanConstant();
2702 [[fallthrough]];
2703 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2704 OpVT);
2705 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2706 return GetUndefBooleanConstant();
2707 [[fallthrough]];
2709 R==APFloat::cmpLessThan, dl, VT,
2710 OpVT);
2711 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2712 return GetUndefBooleanConstant();
2713 [[fallthrough]];
2714 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2715 OpVT);
2716 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2717 return GetUndefBooleanConstant();
2718 [[fallthrough]];
2720 VT, OpVT);
2721 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2722 return GetUndefBooleanConstant();
2723 [[fallthrough]];
2725 R==APFloat::cmpEqual, dl, VT,
2726 OpVT);
2727 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2728 return GetUndefBooleanConstant();
2729 [[fallthrough]];
2731 R==APFloat::cmpEqual, dl, VT, OpVT);
2732 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2733 OpVT);
2734 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2735 OpVT);
2737 R==APFloat::cmpEqual, dl, VT,
2738 OpVT);
2739 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2740 OpVT);
2742 R==APFloat::cmpLessThan, dl, VT,
2743 OpVT);
2745 R==APFloat::cmpUnordered, dl, VT,
2746 OpVT);
2748 VT, OpVT);
2749 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2750 OpVT);
2751 }
2752 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2753 // Ensure that the constant occurs on the RHS.
2755 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2756 return SDValue();
2757 return getSetCC(dl, VT, N2, N1, SwappedCond);
2758 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2759 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2760 // If an operand is known to be a nan (or undef that could be a nan), we can
2761 // fold it.
2762 // Choosing NaN for the undef will always make unordered comparison succeed
2763 // and ordered comparison fails.
2764 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2765 switch (ISD::getUnorderedFlavor(Cond)) {
2766 default:
2767 llvm_unreachable("Unknown flavor!");
2768 case 0: // Known false.
2769 return getBoolConstant(false, dl, VT, OpVT);
2770 case 1: // Known true.
2771 return getBoolConstant(true, dl, VT, OpVT);
2772 case 2: // Undefined.
2773 return GetUndefBooleanConstant();
2774 }
2775 }
2776
2777 // Could not fold it.
2778 return SDValue();
2779}
2780
2781/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2782/// use this predicate to simplify operations downstream.
2784 unsigned BitWidth = Op.getScalarValueSizeInBits();
2786}
2787
2789 if (Depth >= MaxRecursionDepth)
2790 return false; // Limit search depth.
2791
2792 unsigned Opc = Op.getOpcode();
2793 switch (Opc) {
2794 case ISD::FABS:
2795 return true;
2796 case ISD::AssertNoFPClass: {
2797 FPClassTest NoFPClass =
2798 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2799
2800 const FPClassTest TestMask = fcNan | fcNegative;
2801 return (NoFPClass & TestMask) == TestMask;
2802 }
2803 case ISD::ARITH_FENCE:
2804 return SignBitIsZeroFP(Op, Depth + 1);
2805 case ISD::FEXP:
2806 case ISD::FEXP2:
2807 case ISD::FEXP10:
2808 return Op->getFlags().hasNoNaNs();
2809 default:
2810 return false;
2811 }
2812
2813 llvm_unreachable("covered opcode switch");
2814}
2815
2816/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2817/// this predicate to simplify operations downstream. Mask is known to be zero
2818/// for bits that V cannot have.
2820 unsigned Depth) const {
2821 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2822}
2823
2824/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2825/// DemandedElts. We use this predicate to simplify operations downstream.
2826/// Mask is known to be zero for bits that V cannot have.
2828 const APInt &DemandedElts,
2829 unsigned Depth) const {
2830 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2831}
2832
2833/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2834/// DemandedElts. We use this predicate to simplify operations downstream.
2836 unsigned Depth /* = 0 */) const {
2837 return computeKnownBits(V, DemandedElts, Depth).isZero();
2838}
2839
2840/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2842 unsigned Depth) const {
2843 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2844}
2845
2847 const APInt &DemandedElts,
2848 unsigned Depth) const {
2849 EVT VT = Op.getValueType();
2850 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2851
2852 unsigned NumElts = VT.getVectorNumElements();
2853 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2854
2855 APInt KnownZeroElements = APInt::getZero(NumElts);
2856 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2857 if (!DemandedElts[EltIdx])
2858 continue; // Don't query elements that are not demanded.
2859 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2860 if (MaskedVectorIsZero(Op, Mask, Depth))
2861 KnownZeroElements.setBit(EltIdx);
2862 }
2863 return KnownZeroElements;
2864}
2865
2866/// isSplatValue - Return true if the vector V has the same value
2867/// across all DemandedElts. For scalable vectors, we don't know the
2868/// number of lanes at compile time. Instead, we use a 1 bit APInt
2869/// to represent a conservative value for all lanes; that is, that
2870/// one bit value is implicitly splatted across all lanes.
2871bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2872 APInt &UndefElts, unsigned Depth) const {
2873 unsigned Opcode = V.getOpcode();
2874 EVT VT = V.getValueType();
2875 assert(VT.isVector() && "Vector type expected");
2876 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2877 "scalable demanded bits are ignored");
2878
2879 if (!DemandedElts)
2880 return false; // No demanded elts, better to assume we don't know anything.
2881
2882 if (Depth >= MaxRecursionDepth)
2883 return false; // Limit search depth.
2884
2885 // Deal with some common cases here that work for both fixed and scalable
2886 // vector types.
2887 switch (Opcode) {
2888 case ISD::SPLAT_VECTOR:
2889 UndefElts = V.getOperand(0).isUndef()
2890 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2891 : APInt(DemandedElts.getBitWidth(), 0);
2892 return true;
2893 case ISD::ADD:
2894 case ISD::SUB:
2895 case ISD::AND:
2896 case ISD::XOR:
2897 case ISD::OR: {
2898 APInt UndefLHS, UndefRHS;
2899 SDValue LHS = V.getOperand(0);
2900 SDValue RHS = V.getOperand(1);
2901 // Only recognize splats with the same demanded undef elements for both
2902 // operands, otherwise we might fail to handle binop-specific undef
2903 // handling.
2904 // e.g. (and undef, 0) -> 0 etc.
2905 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2906 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2907 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2908 UndefElts = UndefLHS | UndefRHS;
2909 return true;
2910 }
2911 return false;
2912 }
2913 case ISD::ABS:
2914 case ISD::TRUNCATE:
2915 case ISD::SIGN_EXTEND:
2916 case ISD::ZERO_EXTEND:
2917 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2918 default:
2919 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2920 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2921 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2922 Depth);
2923 break;
2924 }
2925
2926 // We don't support other cases than those above for scalable vectors at
2927 // the moment.
2928 if (VT.isScalableVector())
2929 return false;
2930
2931 unsigned NumElts = VT.getVectorNumElements();
2932 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2933 UndefElts = APInt::getZero(NumElts);
2934
2935 switch (Opcode) {
2936 case ISD::BUILD_VECTOR: {
2937 SDValue Scl;
2938 for (unsigned i = 0; i != NumElts; ++i) {
2939 SDValue Op = V.getOperand(i);
2940 if (Op.isUndef()) {
2941 UndefElts.setBit(i);
2942 continue;
2943 }
2944 if (!DemandedElts[i])
2945 continue;
2946 if (Scl && Scl != Op)
2947 return false;
2948 Scl = Op;
2949 }
2950 return true;
2951 }
2952 case ISD::VECTOR_SHUFFLE: {
2953 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2954 APInt DemandedLHS = APInt::getZero(NumElts);
2955 APInt DemandedRHS = APInt::getZero(NumElts);
2956 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2957 for (int i = 0; i != (int)NumElts; ++i) {
2958 int M = Mask[i];
2959 if (M < 0) {
2960 UndefElts.setBit(i);
2961 continue;
2962 }
2963 if (!DemandedElts[i])
2964 continue;
2965 if (M < (int)NumElts)
2966 DemandedLHS.setBit(M);
2967 else
2968 DemandedRHS.setBit(M - NumElts);
2969 }
2970
2971 // If we aren't demanding either op, assume there's no splat.
2972 // If we are demanding both ops, assume there's no splat.
2973 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2974 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2975 return false;
2976
2977 // See if the demanded elts of the source op is a splat or we only demand
2978 // one element, which should always be a splat.
2979 // TODO: Handle source ops splats with undefs.
2980 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2981 APInt SrcUndefs;
2982 return (SrcElts.popcount() == 1) ||
2983 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2984 (SrcElts & SrcUndefs).isZero());
2985 };
2986 if (!DemandedLHS.isZero())
2987 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2988 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2989 }
2991 // Offset the demanded elts by the subvector index.
2992 SDValue Src = V.getOperand(0);
2993 // We don't support scalable vectors at the moment.
2994 if (Src.getValueType().isScalableVector())
2995 return false;
2996 uint64_t Idx = V.getConstantOperandVal(1);
2997 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2998 APInt UndefSrcElts;
2999 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3000 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3001 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3002 return true;
3003 }
3004 break;
3005 }
3009 // Widen the demanded elts by the src element count.
3010 SDValue Src = V.getOperand(0);
3011 // We don't support scalable vectors at the moment.
3012 if (Src.getValueType().isScalableVector())
3013 return false;
3014 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3015 APInt UndefSrcElts;
3016 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3017 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3018 UndefElts = UndefSrcElts.trunc(NumElts);
3019 return true;
3020 }
3021 break;
3022 }
3023 case ISD::BITCAST: {
3024 SDValue Src = V.getOperand(0);
3025 EVT SrcVT = Src.getValueType();
3026 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3027 unsigned BitWidth = VT.getScalarSizeInBits();
3028
3029 // Ignore bitcasts from unsupported types.
3030 // TODO: Add fp support?
3031 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3032 break;
3033
3034 // Bitcast 'small element' vector to 'large element' vector.
3035 if ((BitWidth % SrcBitWidth) == 0) {
3036 // See if each sub element is a splat.
3037 unsigned Scale = BitWidth / SrcBitWidth;
3038 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3039 APInt ScaledDemandedElts =
3040 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3041 for (unsigned I = 0; I != Scale; ++I) {
3042 APInt SubUndefElts;
3043 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3044 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3045 SubDemandedElts &= ScaledDemandedElts;
3046 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3047 return false;
3048 // TODO: Add support for merging sub undef elements.
3049 if (!SubUndefElts.isZero())
3050 return false;
3051 }
3052 return true;
3053 }
3054 break;
3055 }
3056 }
3057
3058 return false;
3059}
3060
3061/// Helper wrapper to main isSplatValue function.
3062bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3063 EVT VT = V.getValueType();
3064 assert(VT.isVector() && "Vector type expected");
3065
3066 APInt UndefElts;
3067 // Since the number of lanes in a scalable vector is unknown at compile time,
3068 // we track one bit which is implicitly broadcast to all lanes. This means
3069 // that all lanes in a scalable vector are considered demanded.
3070 APInt DemandedElts
3072 return isSplatValue(V, DemandedElts, UndefElts) &&
3073 (AllowUndefs || !UndefElts);
3074}
3075
3078
3079 EVT VT = V.getValueType();
3080 unsigned Opcode = V.getOpcode();
3081 switch (Opcode) {
3082 default: {
3083 APInt UndefElts;
3084 // Since the number of lanes in a scalable vector is unknown at compile time,
3085 // we track one bit which is implicitly broadcast to all lanes. This means
3086 // that all lanes in a scalable vector are considered demanded.
3087 APInt DemandedElts
3089
3090 if (isSplatValue(V, DemandedElts, UndefElts)) {
3091 if (VT.isScalableVector()) {
3092 // DemandedElts and UndefElts are ignored for scalable vectors, since
3093 // the only supported cases are SPLAT_VECTOR nodes.
3094 SplatIdx = 0;
3095 } else {
3096 // Handle case where all demanded elements are UNDEF.
3097 if (DemandedElts.isSubsetOf(UndefElts)) {
3098 SplatIdx = 0;
3099 return getUNDEF(VT);
3100 }
3101 SplatIdx = (UndefElts & DemandedElts).countr_one();
3102 }
3103 return V;
3104 }
3105 break;
3106 }
3107 case ISD::SPLAT_VECTOR:
3108 SplatIdx = 0;
3109 return V;
3110 case ISD::VECTOR_SHUFFLE: {
3111 assert(!VT.isScalableVector());
3112 // Check if this is a shuffle node doing a splat.
3113 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3114 // getTargetVShiftNode currently struggles without the splat source.
3115 auto *SVN = cast<ShuffleVectorSDNode>(V);
3116 if (!SVN->isSplat())
3117 break;
3118 int Idx = SVN->getSplatIndex();
3119 int NumElts = V.getValueType().getVectorNumElements();
3120 SplatIdx = Idx % NumElts;
3121 return V.getOperand(Idx / NumElts);
3122 }
3123 }
3124
3125 return SDValue();
3126}
3127
3129 int SplatIdx;
3130 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3131 EVT SVT = SrcVector.getValueType().getScalarType();
3132 EVT LegalSVT = SVT;
3133 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3134 if (!SVT.isInteger())
3135 return SDValue();
3136 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3137 if (LegalSVT.bitsLT(SVT))
3138 return SDValue();
3139 }
3140 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3141 }
3142 return SDValue();
3143}
3144
3145std::optional<ConstantRange>
3147 unsigned Depth) const {
3148 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3149 V.getOpcode() == ISD::SRA) &&
3150 "Unknown shift node");
3151 // Shifting more than the bitwidth is not valid.
3152 unsigned BitWidth = V.getScalarValueSizeInBits();
3153
3154 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3155 const APInt &ShAmt = Cst->getAPIntValue();
3156 if (ShAmt.uge(BitWidth))
3157 return std::nullopt;
3158 return ConstantRange(ShAmt);
3159 }
3160
3161 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3162 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3163 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3164 if (!DemandedElts[i])
3165 continue;
3166 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3167 if (!SA) {
3168 MinAmt = MaxAmt = nullptr;
3169 break;
3170 }
3171 const APInt &ShAmt = SA->getAPIntValue();
3172 if (ShAmt.uge(BitWidth))
3173 return std::nullopt;
3174 if (!MinAmt || MinAmt->ugt(ShAmt))
3175 MinAmt = &ShAmt;
3176 if (!MaxAmt || MaxAmt->ult(ShAmt))
3177 MaxAmt = &ShAmt;
3178 }
3179 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3180 "Failed to find matching min/max shift amounts");
3181 if (MinAmt && MaxAmt)
3182 return ConstantRange(*MinAmt, *MaxAmt + 1);
3183 }
3184
3185 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3186 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3187 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3188 if (KnownAmt.getMaxValue().ult(BitWidth))
3189 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3190
3191 return std::nullopt;
3192}
3193
3194std::optional<unsigned>
3196 unsigned Depth) const {
3197 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3198 V.getOpcode() == ISD::SRA) &&
3199 "Unknown shift node");
3200 if (std::optional<ConstantRange> AmtRange =
3201 getValidShiftAmountRange(V, DemandedElts, Depth))
3202 if (const APInt *ShAmt = AmtRange->getSingleElement())
3203 return ShAmt->getZExtValue();
3204 return std::nullopt;
3205}
3206
3207std::optional<unsigned>
3209 EVT VT = V.getValueType();
3210 APInt DemandedElts = VT.isFixedLengthVector()
3212 : APInt(1, 1);
3213 return getValidShiftAmount(V, DemandedElts, Depth);
3214}
3215
3216std::optional<unsigned>
3218 unsigned Depth) const {
3219 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3220 V.getOpcode() == ISD::SRA) &&
3221 "Unknown shift node");
3222 if (std::optional<ConstantRange> AmtRange =
3223 getValidShiftAmountRange(V, DemandedElts, Depth))
3224 return AmtRange->getUnsignedMin().getZExtValue();
3225 return std::nullopt;
3226}
3227
3228std::optional<unsigned>
3230 EVT VT = V.getValueType();
3231 APInt DemandedElts = VT.isFixedLengthVector()
3233 : APInt(1, 1);
3234 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3235}
3236
3237std::optional<unsigned>
3239 unsigned Depth) const {
3240 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3241 V.getOpcode() == ISD::SRA) &&
3242 "Unknown shift node");
3243 if (std::optional<ConstantRange> AmtRange =
3244 getValidShiftAmountRange(V, DemandedElts, Depth))
3245 return AmtRange->getUnsignedMax().getZExtValue();
3246 return std::nullopt;
3247}
3248
3249std::optional<unsigned>
3251 EVT VT = V.getValueType();
3252 APInt DemandedElts = VT.isFixedLengthVector()
3254 : APInt(1, 1);
3255 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3256}
3257
3258/// Determine which bits of Op are known to be either zero or one and return
3259/// them in Known. For vectors, the known bits are those that are shared by
3260/// every vector element.
3262 EVT VT = Op.getValueType();
3263
3264 // Since the number of lanes in a scalable vector is unknown at compile time,
3265 // we track one bit which is implicitly broadcast to all lanes. This means
3266 // that all lanes in a scalable vector are considered demanded.
3267 APInt DemandedElts = VT.isFixedLengthVector()
3269 : APInt(1, 1);
3270 return computeKnownBits(Op, DemandedElts, Depth);
3271}
3272
3273/// Determine which bits of Op are known to be either zero or one and return
3274/// them in Known. The DemandedElts argument allows us to only collect the known
3275/// bits that are shared by the requested vector elements.
3277 unsigned Depth) const {
3278 unsigned BitWidth = Op.getScalarValueSizeInBits();
3279
3280 KnownBits Known(BitWidth); // Don't know anything.
3281
3282 if (auto OptAPInt = Op->bitcastToAPInt()) {
3283 // We know all of the bits for a constant!
3284 return KnownBits::makeConstant(*std::move(OptAPInt));
3285 }
3286
3287 if (Depth >= MaxRecursionDepth)
3288 return Known; // Limit search depth.
3289
3290 KnownBits Known2;
3291 unsigned NumElts = DemandedElts.getBitWidth();
3292 assert((!Op.getValueType().isFixedLengthVector() ||
3293 NumElts == Op.getValueType().getVectorNumElements()) &&
3294 "Unexpected vector size");
3295
3296 if (!DemandedElts)
3297 return Known; // No demanded elts, better to assume we don't know anything.
3298
3299 unsigned Opcode = Op.getOpcode();
3300 switch (Opcode) {
3301 case ISD::MERGE_VALUES:
3302 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3303 Depth + 1);
3304 case ISD::SPLAT_VECTOR: {
3305 SDValue SrcOp = Op.getOperand(0);
3306 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3307 "Expected SPLAT_VECTOR implicit truncation");
3308 // Implicitly truncate the bits to match the official semantics of
3309 // SPLAT_VECTOR.
3310 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3311 break;
3312 }
3314 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3315 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3316 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3317 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3318 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3319 }
3320 break;
3321 }
3322 case ISD::STEP_VECTOR: {
3323 const APInt &Step = Op.getConstantOperandAPInt(0);
3324
3325 if (Step.isPowerOf2())
3326 Known.Zero.setLowBits(Step.logBase2());
3327
3329
3330 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3331 break;
3332 const APInt MinNumElts =
3333 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3334
3335 bool Overflow;
3336 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3338 .umul_ov(MinNumElts, Overflow);
3339 if (Overflow)
3340 break;
3341
3342 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3343 if (Overflow)
3344 break;
3345
3346 Known.Zero.setHighBits(MaxValue.countl_zero());
3347 break;
3348 }
3349 case ISD::BUILD_VECTOR:
3350 assert(!Op.getValueType().isScalableVector());
3351 // Collect the known bits that are shared by every demanded vector element.
3352 Known.setAllConflict();
3353 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3354 if (!DemandedElts[i])
3355 continue;
3356
3357 SDValue SrcOp = Op.getOperand(i);
3358 Known2 = computeKnownBits(SrcOp, Depth + 1);
3359
3360 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3361 if (SrcOp.getValueSizeInBits() != BitWidth) {
3362 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3363 "Expected BUILD_VECTOR implicit truncation");
3364 Known2 = Known2.trunc(BitWidth);
3365 }
3366
3367 // Known bits are the values that are shared by every demanded element.
3368 Known = Known.intersectWith(Known2);
3369
3370 // If we don't know any bits, early out.
3371 if (Known.isUnknown())
3372 break;
3373 }
3374 break;
3375 case ISD::VECTOR_COMPRESS: {
3376 SDValue Vec = Op.getOperand(0);
3377 SDValue PassThru = Op.getOperand(2);
3378 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3379 // If we don't know any bits, early out.
3380 if (Known.isUnknown())
3381 break;
3382 Known2 = computeKnownBits(Vec, Depth + 1);
3383 Known = Known.intersectWith(Known2);
3384 break;
3385 }
3386 case ISD::VECTOR_SHUFFLE: {
3387 assert(!Op.getValueType().isScalableVector());
3388 // Collect the known bits that are shared by every vector element referenced
3389 // by the shuffle.
3390 APInt DemandedLHS, DemandedRHS;
3392 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3393 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3394 DemandedLHS, DemandedRHS))
3395 break;
3396
3397 // Known bits are the values that are shared by every demanded element.
3398 Known.setAllConflict();
3399 if (!!DemandedLHS) {
3400 SDValue LHS = Op.getOperand(0);
3401 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3402 Known = Known.intersectWith(Known2);
3403 }
3404 // If we don't know any bits, early out.
3405 if (Known.isUnknown())
3406 break;
3407 if (!!DemandedRHS) {
3408 SDValue RHS = Op.getOperand(1);
3409 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3410 Known = Known.intersectWith(Known2);
3411 }
3412 break;
3413 }
3414 case ISD::VSCALE: {
3416 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3417 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3418 break;
3419 }
3420 case ISD::CONCAT_VECTORS: {
3421 if (Op.getValueType().isScalableVector())
3422 break;
3423 // Split DemandedElts and test each of the demanded subvectors.
3424 Known.setAllConflict();
3425 EVT SubVectorVT = Op.getOperand(0).getValueType();
3426 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3427 unsigned NumSubVectors = Op.getNumOperands();
3428 for (unsigned i = 0; i != NumSubVectors; ++i) {
3429 APInt DemandedSub =
3430 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3431 if (!!DemandedSub) {
3432 SDValue Sub = Op.getOperand(i);
3433 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3434 Known = Known.intersectWith(Known2);
3435 }
3436 // If we don't know any bits, early out.
3437 if (Known.isUnknown())
3438 break;
3439 }
3440 break;
3441 }
3442 case ISD::INSERT_SUBVECTOR: {
3443 if (Op.getValueType().isScalableVector())
3444 break;
3445 // Demand any elements from the subvector and the remainder from the src its
3446 // inserted into.
3447 SDValue Src = Op.getOperand(0);
3448 SDValue Sub = Op.getOperand(1);
3449 uint64_t Idx = Op.getConstantOperandVal(2);
3450 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3451 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3452 APInt DemandedSrcElts = DemandedElts;
3453 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3454
3455 Known.setAllConflict();
3456 if (!!DemandedSubElts) {
3457 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3458 if (Known.isUnknown())
3459 break; // early-out.
3460 }
3461 if (!!DemandedSrcElts) {
3462 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3463 Known = Known.intersectWith(Known2);
3464 }
3465 break;
3466 }
3468 // Offset the demanded elts by the subvector index.
3469 SDValue Src = Op.getOperand(0);
3470 // Bail until we can represent demanded elements for scalable vectors.
3471 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3472 break;
3473 uint64_t Idx = Op.getConstantOperandVal(1);
3474 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3475 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3476 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3477 break;
3478 }
3479 case ISD::SCALAR_TO_VECTOR: {
3480 if (Op.getValueType().isScalableVector())
3481 break;
3482 // We know about scalar_to_vector as much as we know about it source,
3483 // which becomes the first element of otherwise unknown vector.
3484 if (DemandedElts != 1)
3485 break;
3486
3487 SDValue N0 = Op.getOperand(0);
3488 Known = computeKnownBits(N0, Depth + 1);
3489 if (N0.getValueSizeInBits() != BitWidth)
3490 Known = Known.trunc(BitWidth);
3491
3492 break;
3493 }
3494 case ISD::BITCAST: {
3495 if (Op.getValueType().isScalableVector())
3496 break;
3497
3498 SDValue N0 = Op.getOperand(0);
3499 EVT SubVT = N0.getValueType();
3500 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3501
3502 // Ignore bitcasts from unsupported types.
3503 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3504 break;
3505
3506 // Fast handling of 'identity' bitcasts.
3507 if (BitWidth == SubBitWidth) {
3508 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3509 break;
3510 }
3511
3512 bool IsLE = getDataLayout().isLittleEndian();
3513
3514 // Bitcast 'small element' vector to 'large element' scalar/vector.
3515 if ((BitWidth % SubBitWidth) == 0) {
3516 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3517
3518 // Collect known bits for the (larger) output by collecting the known
3519 // bits from each set of sub elements and shift these into place.
3520 // We need to separately call computeKnownBits for each set of
3521 // sub elements as the knownbits for each is likely to be different.
3522 unsigned SubScale = BitWidth / SubBitWidth;
3523 APInt SubDemandedElts(NumElts * SubScale, 0);
3524 for (unsigned i = 0; i != NumElts; ++i)
3525 if (DemandedElts[i])
3526 SubDemandedElts.setBit(i * SubScale);
3527
3528 for (unsigned i = 0; i != SubScale; ++i) {
3529 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3530 Depth + 1);
3531 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3532 Known.insertBits(Known2, SubBitWidth * Shifts);
3533 }
3534 }
3535
3536 // Bitcast 'large element' scalar/vector to 'small element' vector.
3537 if ((SubBitWidth % BitWidth) == 0) {
3538 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3539
3540 // Collect known bits for the (smaller) output by collecting the known
3541 // bits from the overlapping larger input elements and extracting the
3542 // sub sections we actually care about.
3543 unsigned SubScale = SubBitWidth / BitWidth;
3544 APInt SubDemandedElts =
3545 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3546 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3547
3548 Known.setAllConflict();
3549 for (unsigned i = 0; i != NumElts; ++i)
3550 if (DemandedElts[i]) {
3551 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3552 unsigned Offset = (Shifts % SubScale) * BitWidth;
3553 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3554 // If we don't know any bits, early out.
3555 if (Known.isUnknown())
3556 break;
3557 }
3558 }
3559 break;
3560 }
3561 case ISD::AND:
3562 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3563 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3564
3565 Known &= Known2;
3566 break;
3567 case ISD::OR:
3568 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3569 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3570
3571 Known |= Known2;
3572 break;
3573 case ISD::XOR:
3574 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3575 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3576
3577 Known ^= Known2;
3578 break;
3579 case ISD::MUL: {
3580 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3581 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3582 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3583 // TODO: SelfMultiply can be poison, but not undef.
3584 if (SelfMultiply)
3585 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3586 Op.getOperand(0), DemandedElts, false, Depth + 1);
3587 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3588
3589 // If the multiplication is known not to overflow, the product of a number
3590 // with itself is non-negative. Only do this if we didn't already computed
3591 // the opposite value for the sign bit.
3592 if (Op->getFlags().hasNoSignedWrap() &&
3593 Op.getOperand(0) == Op.getOperand(1) &&
3594 !Known.isNegative())
3595 Known.makeNonNegative();
3596 break;
3597 }
3598 case ISD::MULHU: {
3599 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3600 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3601 Known = KnownBits::mulhu(Known, Known2);
3602 break;
3603 }
3604 case ISD::MULHS: {
3605 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3606 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3607 Known = KnownBits::mulhs(Known, Known2);
3608 break;
3609 }
3610 case ISD::ABDU: {
3611 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3612 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3613 Known = KnownBits::abdu(Known, Known2);
3614 break;
3615 }
3616 case ISD::ABDS: {
3617 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3618 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3619 Known = KnownBits::abds(Known, Known2);
3620 unsigned SignBits1 =
3621 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3622 if (SignBits1 == 1)
3623 break;
3624 unsigned SignBits0 =
3625 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3626 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3627 break;
3628 }
3629 case ISD::UMUL_LOHI: {
3630 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3631 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3632 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3633 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3634 if (Op.getResNo() == 0)
3635 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3636 else
3637 Known = KnownBits::mulhu(Known, Known2);
3638 break;
3639 }
3640 case ISD::SMUL_LOHI: {
3641 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3642 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3643 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3644 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3645 if (Op.getResNo() == 0)
3646 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3647 else
3648 Known = KnownBits::mulhs(Known, Known2);
3649 break;
3650 }
3651 case ISD::AVGFLOORU: {
3652 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3653 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3654 Known = KnownBits::avgFloorU(Known, Known2);
3655 break;
3656 }
3657 case ISD::AVGCEILU: {
3658 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3659 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3660 Known = KnownBits::avgCeilU(Known, Known2);
3661 break;
3662 }
3663 case ISD::AVGFLOORS: {
3664 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3665 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3666 Known = KnownBits::avgFloorS(Known, Known2);
3667 break;
3668 }
3669 case ISD::AVGCEILS: {
3670 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3671 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3672 Known = KnownBits::avgCeilS(Known, Known2);
3673 break;
3674 }
3675 case ISD::SELECT:
3676 case ISD::VSELECT:
3677 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3678 // If we don't know any bits, early out.
3679 if (Known.isUnknown())
3680 break;
3681 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3682
3683 // Only known if known in both the LHS and RHS.
3684 Known = Known.intersectWith(Known2);
3685 break;
3686 case ISD::SELECT_CC:
3687 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3688 // If we don't know any bits, early out.
3689 if (Known.isUnknown())
3690 break;
3691 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3692
3693 // Only known if known in both the LHS and RHS.
3694 Known = Known.intersectWith(Known2);
3695 break;
3696 case ISD::SMULO:
3697 case ISD::UMULO:
3698 if (Op.getResNo() != 1)
3699 break;
3700 // The boolean result conforms to getBooleanContents.
3701 // If we know the result of a setcc has the top bits zero, use this info.
3702 // We know that we have an integer-based boolean since these operations
3703 // are only available for integer.
3704 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3706 BitWidth > 1)
3707 Known.Zero.setBitsFrom(1);
3708 break;
3709 case ISD::SETCC:
3710 case ISD::SETCCCARRY:
3711 case ISD::STRICT_FSETCC:
3712 case ISD::STRICT_FSETCCS: {
3713 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3714 // If we know the result of a setcc has the top bits zero, use this info.
3715 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3717 BitWidth > 1)
3718 Known.Zero.setBitsFrom(1);
3719 break;
3720 }
3721 case ISD::SHL: {
3722 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3723 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3724
3725 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3726 bool NSW = Op->getFlags().hasNoSignedWrap();
3727
3728 bool ShAmtNonZero = Known2.isNonZero();
3729
3730 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3731
3732 // Minimum shift low bits are known zero.
3733 if (std::optional<unsigned> ShMinAmt =
3734 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3735 Known.Zero.setLowBits(*ShMinAmt);
3736 break;
3737 }
3738 case ISD::SRL:
3739 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3740 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3741 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3742 Op->getFlags().hasExact());
3743
3744 // Minimum shift high bits are known zero.
3745 if (std::optional<unsigned> ShMinAmt =
3746 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3747 Known.Zero.setHighBits(*ShMinAmt);
3748 break;
3749 case ISD::SRA:
3750 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3751 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3752 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3753 Op->getFlags().hasExact());
3754 break;
3755 case ISD::ROTL:
3756 case ISD::ROTR:
3757 if (ConstantSDNode *C =
3758 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3759 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3760
3761 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3762
3763 // Canonicalize to ROTR.
3764 if (Opcode == ISD::ROTL && Amt != 0)
3765 Amt = BitWidth - Amt;
3766
3767 Known.Zero = Known.Zero.rotr(Amt);
3768 Known.One = Known.One.rotr(Amt);
3769 }
3770 break;
3771 case ISD::FSHL:
3772 case ISD::FSHR:
3773 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3774 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3775
3776 // For fshl, 0-shift returns the 1st arg.
3777 // For fshr, 0-shift returns the 2nd arg.
3778 if (Amt == 0) {
3779 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3780 DemandedElts, Depth + 1);
3781 break;
3782 }
3783
3784 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3785 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3786 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3787 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3788 if (Opcode == ISD::FSHL) {
3789 Known <<= Amt;
3790 Known2 >>= BitWidth - Amt;
3791 } else {
3792 Known <<= BitWidth - Amt;
3793 Known2 >>= Amt;
3794 }
3795 Known = Known.unionWith(Known2);
3796 }
3797 break;
3798 case ISD::SHL_PARTS:
3799 case ISD::SRA_PARTS:
3800 case ISD::SRL_PARTS: {
3801 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3802
3803 // Collect lo/hi source values and concatenate.
3804 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3805 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3806 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3807 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3808 Known = Known2.concat(Known);
3809
3810 // Collect shift amount.
3811 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3812
3813 if (Opcode == ISD::SHL_PARTS)
3814 Known = KnownBits::shl(Known, Known2);
3815 else if (Opcode == ISD::SRA_PARTS)
3816 Known = KnownBits::ashr(Known, Known2);
3817 else // if (Opcode == ISD::SRL_PARTS)
3818 Known = KnownBits::lshr(Known, Known2);
3819
3820 // TODO: Minimum shift low/high bits are known zero.
3821
3822 if (Op.getResNo() == 0)
3823 Known = Known.extractBits(LoBits, 0);
3824 else
3825 Known = Known.extractBits(HiBits, LoBits);
3826 break;
3827 }
3829 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3830 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3831 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3832 break;
3833 }
3834 case ISD::CTTZ:
3835 case ISD::CTTZ_ZERO_UNDEF: {
3836 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3837 // If we have a known 1, its position is our upper bound.
3838 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3839 unsigned LowBits = llvm::bit_width(PossibleTZ);
3840 Known.Zero.setBitsFrom(LowBits);
3841 break;
3842 }
3843 case ISD::CTLZ:
3844 case ISD::CTLZ_ZERO_UNDEF: {
3845 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3846 // If we have a known 1, its position is our upper bound.
3847 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3848 unsigned LowBits = llvm::bit_width(PossibleLZ);
3849 Known.Zero.setBitsFrom(LowBits);
3850 break;
3851 }
3852 case ISD::CTPOP: {
3853 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3854 // If we know some of the bits are zero, they can't be one.
3855 unsigned PossibleOnes = Known2.countMaxPopulation();
3856 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3857 break;
3858 }
3859 case ISD::PARITY: {
3860 // Parity returns 0 everywhere but the LSB.
3861 Known.Zero.setBitsFrom(1);
3862 break;
3863 }
3864 case ISD::MGATHER:
3865 case ISD::MLOAD: {
3866 ISD::LoadExtType ETy =
3867 (Opcode == ISD::MGATHER)
3868 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3869 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3870 if (ETy == ISD::ZEXTLOAD) {
3871 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3872 KnownBits Known0(MemVT.getScalarSizeInBits());
3873 return Known0.zext(BitWidth);
3874 }
3875 break;
3876 }
3877 case ISD::LOAD: {
3879 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3880 if (ISD::isNON_EXTLoad(LD) && Cst) {
3881 // Determine any common known bits from the loaded constant pool value.
3882 Type *CstTy = Cst->getType();
3883 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3884 !Op.getValueType().isScalableVector()) {
3885 // If its a vector splat, then we can (quickly) reuse the scalar path.
3886 // NOTE: We assume all elements match and none are UNDEF.
3887 if (CstTy->isVectorTy()) {
3888 if (const Constant *Splat = Cst->getSplatValue()) {
3889 Cst = Splat;
3890 CstTy = Cst->getType();
3891 }
3892 }
3893 // TODO - do we need to handle different bitwidths?
3894 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3895 // Iterate across all vector elements finding common known bits.
3896 Known.setAllConflict();
3897 for (unsigned i = 0; i != NumElts; ++i) {
3898 if (!DemandedElts[i])
3899 continue;
3900 if (Constant *Elt = Cst->getAggregateElement(i)) {
3901 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3902 const APInt &Value = CInt->getValue();
3903 Known.One &= Value;
3904 Known.Zero &= ~Value;
3905 continue;
3906 }
3907 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3908 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3909 Known.One &= Value;
3910 Known.Zero &= ~Value;
3911 continue;
3912 }
3913 }
3914 Known.One.clearAllBits();
3915 Known.Zero.clearAllBits();
3916 break;
3917 }
3918 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3919 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3920 Known = KnownBits::makeConstant(CInt->getValue());
3921 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3922 Known =
3923 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3924 }
3925 }
3926 }
3927 } else if (Op.getResNo() == 0) {
3928 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3929 KnownBits KnownScalarMemory(ScalarMemorySize);
3930 if (const MDNode *MD = LD->getRanges())
3931 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3932
3933 // Extend the Known bits from memory to the size of the scalar result.
3934 if (ISD::isZEXTLoad(Op.getNode()))
3935 Known = KnownScalarMemory.zext(BitWidth);
3936 else if (ISD::isSEXTLoad(Op.getNode()))
3937 Known = KnownScalarMemory.sext(BitWidth);
3938 else if (ISD::isEXTLoad(Op.getNode()))
3939 Known = KnownScalarMemory.anyext(BitWidth);
3940 else
3941 Known = KnownScalarMemory;
3942 assert(Known.getBitWidth() == BitWidth);
3943 return Known;
3944 }
3945 break;
3946 }
3948 if (Op.getValueType().isScalableVector())
3949 break;
3950 EVT InVT = Op.getOperand(0).getValueType();
3951 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3952 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3953 Known = Known.zext(BitWidth);
3954 break;
3955 }
3956 case ISD::ZERO_EXTEND: {
3957 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3958 Known = Known.zext(BitWidth);
3959 break;
3960 }
3962 if (Op.getValueType().isScalableVector())
3963 break;
3964 EVT InVT = Op.getOperand(0).getValueType();
3965 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3966 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3967 // If the sign bit is known to be zero or one, then sext will extend
3968 // it to the top bits, else it will just zext.
3969 Known = Known.sext(BitWidth);
3970 break;
3971 }
3972 case ISD::SIGN_EXTEND: {
3973 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3974 // If the sign bit is known to be zero or one, then sext will extend
3975 // it to the top bits, else it will just zext.
3976 Known = Known.sext(BitWidth);
3977 break;
3978 }
3980 if (Op.getValueType().isScalableVector())
3981 break;
3982 EVT InVT = Op.getOperand(0).getValueType();
3983 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3984 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3985 Known = Known.anyext(BitWidth);
3986 break;
3987 }
3988 case ISD::ANY_EXTEND: {
3989 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3990 Known = Known.anyext(BitWidth);
3991 break;
3992 }
3993 case ISD::TRUNCATE: {
3994 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3995 Known = Known.trunc(BitWidth);
3996 break;
3997 }
3998 case ISD::AssertZext: {
3999 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4001 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4002 Known.Zero |= (~InMask);
4003 Known.One &= (~Known.Zero);
4004 break;
4005 }
4006 case ISD::AssertAlign: {
4007 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4008 assert(LogOfAlign != 0);
4009
4010 // TODO: Should use maximum with source
4011 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4012 // well as clearing one bits.
4013 Known.Zero.setLowBits(LogOfAlign);
4014 Known.One.clearLowBits(LogOfAlign);
4015 break;
4016 }
4017 case ISD::AssertNoFPClass: {
4018 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4019
4020 FPClassTest NoFPClass =
4021 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4022 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4023 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4024 // Cannot be negative.
4025 Known.makeNonNegative();
4026 }
4027
4028 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4029 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4030 // Cannot be positive.
4031 Known.makeNegative();
4032 }
4033
4034 break;
4035 }
4036 case ISD::FGETSIGN:
4037 // All bits are zero except the low bit.
4038 Known.Zero.setBitsFrom(1);
4039 break;
4040 case ISD::ADD:
4041 case ISD::SUB: {
4042 SDNodeFlags Flags = Op.getNode()->getFlags();
4043 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4044 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4046 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4047 Flags.hasNoUnsignedWrap(), Known, Known2);
4048 break;
4049 }
4050 case ISD::USUBO:
4051 case ISD::SSUBO:
4052 case ISD::USUBO_CARRY:
4053 case ISD::SSUBO_CARRY:
4054 if (Op.getResNo() == 1) {
4055 // If we know the result of a setcc has the top bits zero, use this info.
4056 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4058 BitWidth > 1)
4059 Known.Zero.setBitsFrom(1);
4060 break;
4061 }
4062 [[fallthrough]];
4063 case ISD::SUBC: {
4064 assert(Op.getResNo() == 0 &&
4065 "We only compute knownbits for the difference here.");
4066
4067 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4068 KnownBits Borrow(1);
4069 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4070 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4071 // Borrow has bit width 1
4072 Borrow = Borrow.trunc(1);
4073 } else {
4074 Borrow.setAllZero();
4075 }
4076
4077 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4078 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4079 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4080 break;
4081 }
4082 case ISD::UADDO:
4083 case ISD::SADDO:
4084 case ISD::UADDO_CARRY:
4085 case ISD::SADDO_CARRY:
4086 if (Op.getResNo() == 1) {
4087 // If we know the result of a setcc has the top bits zero, use this info.
4088 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4090 BitWidth > 1)
4091 Known.Zero.setBitsFrom(1);
4092 break;
4093 }
4094 [[fallthrough]];
4095 case ISD::ADDC:
4096 case ISD::ADDE: {
4097 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4098
4099 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4100 KnownBits Carry(1);
4101 if (Opcode == ISD::ADDE)
4102 // Can't track carry from glue, set carry to unknown.
4103 Carry.resetAll();
4104 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4105 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4106 // Carry has bit width 1
4107 Carry = Carry.trunc(1);
4108 } else {
4109 Carry.setAllZero();
4110 }
4111
4112 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4113 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4114 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4115 break;
4116 }
4117 case ISD::UDIV: {
4118 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4119 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4120 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4121 break;
4122 }
4123 case ISD::SDIV: {
4124 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4125 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4126 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4127 break;
4128 }
4129 case ISD::SREM: {
4130 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4131 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4132 Known = KnownBits::srem(Known, Known2);
4133 break;
4134 }
4135 case ISD::UREM: {
4136 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4137 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4138 Known = KnownBits::urem(Known, Known2);
4139 break;
4140 }
4141 case ISD::EXTRACT_ELEMENT: {
4142 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4143 const unsigned Index = Op.getConstantOperandVal(1);
4144 const unsigned EltBitWidth = Op.getValueSizeInBits();
4145
4146 // Remove low part of known bits mask
4147 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4148 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4149
4150 // Remove high part of known bit mask
4151 Known = Known.trunc(EltBitWidth);
4152 break;
4153 }
4155 SDValue InVec = Op.getOperand(0);
4156 SDValue EltNo = Op.getOperand(1);
4157 EVT VecVT = InVec.getValueType();
4158 // computeKnownBits not yet implemented for scalable vectors.
4159 if (VecVT.isScalableVector())
4160 break;
4161 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4162 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4163
4164 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4165 // anything about the extended bits.
4166 if (BitWidth > EltBitWidth)
4167 Known = Known.trunc(EltBitWidth);
4168
4169 // If we know the element index, just demand that vector element, else for
4170 // an unknown element index, ignore DemandedElts and demand them all.
4171 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4172 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4173 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4174 DemandedSrcElts =
4175 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4176
4177 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4178 if (BitWidth > EltBitWidth)
4179 Known = Known.anyext(BitWidth);
4180 break;
4181 }
4183 if (Op.getValueType().isScalableVector())
4184 break;
4185
4186 // If we know the element index, split the demand between the
4187 // source vector and the inserted element, otherwise assume we need
4188 // the original demanded vector elements and the value.
4189 SDValue InVec = Op.getOperand(0);
4190 SDValue InVal = Op.getOperand(1);
4191 SDValue EltNo = Op.getOperand(2);
4192 bool DemandedVal = true;
4193 APInt DemandedVecElts = DemandedElts;
4194 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4195 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4196 unsigned EltIdx = CEltNo->getZExtValue();
4197 DemandedVal = !!DemandedElts[EltIdx];
4198 DemandedVecElts.clearBit(EltIdx);
4199 }
4200 Known.setAllConflict();
4201 if (DemandedVal) {
4202 Known2 = computeKnownBits(InVal, Depth + 1);
4203 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4204 }
4205 if (!!DemandedVecElts) {
4206 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4207 Known = Known.intersectWith(Known2);
4208 }
4209 break;
4210 }
4211 case ISD::BITREVERSE: {
4212 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4213 Known = Known2.reverseBits();
4214 break;
4215 }
4216 case ISD::BSWAP: {
4217 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4218 Known = Known2.byteSwap();
4219 break;
4220 }
4221 case ISD::ABS: {
4222 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4223 Known = Known2.abs();
4224 Known.Zero.setHighBits(
4225 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4226 break;
4227 }
4228 case ISD::USUBSAT: {
4229 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4230 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4231 Known = KnownBits::usub_sat(Known, Known2);
4232 break;
4233 }
4234 case ISD::UMIN: {
4235 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4236 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4237 Known = KnownBits::umin(Known, Known2);
4238 break;
4239 }
4240 case ISD::UMAX: {
4241 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4242 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4243 Known = KnownBits::umax(Known, Known2);
4244 break;
4245 }
4246 case ISD::SMIN:
4247 case ISD::SMAX: {
4248 // If we have a clamp pattern, we know that the number of sign bits will be
4249 // the minimum of the clamp min/max range.
4250 bool IsMax = (Opcode == ISD::SMAX);
4251 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4252 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4253 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4254 CstHigh =
4255 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4256 if (CstLow && CstHigh) {
4257 if (!IsMax)
4258 std::swap(CstLow, CstHigh);
4259
4260 const APInt &ValueLow = CstLow->getAPIntValue();
4261 const APInt &ValueHigh = CstHigh->getAPIntValue();
4262 if (ValueLow.sle(ValueHigh)) {
4263 unsigned LowSignBits = ValueLow.getNumSignBits();
4264 unsigned HighSignBits = ValueHigh.getNumSignBits();
4265 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4266 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4267 Known.One.setHighBits(MinSignBits);
4268 break;
4269 }
4270 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4271 Known.Zero.setHighBits(MinSignBits);
4272 break;
4273 }
4274 }
4275 }
4276
4277 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4278 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4279 if (IsMax)
4280 Known = KnownBits::smax(Known, Known2);
4281 else
4282 Known = KnownBits::smin(Known, Known2);
4283
4284 // For SMAX, if CstLow is non-negative we know the result will be
4285 // non-negative and thus all sign bits are 0.
4286 // TODO: There's an equivalent of this for smin with negative constant for
4287 // known ones.
4288 if (IsMax && CstLow) {
4289 const APInt &ValueLow = CstLow->getAPIntValue();
4290 if (ValueLow.isNonNegative()) {
4291 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4292 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4293 }
4294 }
4295
4296 break;
4297 }
4298 case ISD::UINT_TO_FP: {
4299 Known.makeNonNegative();
4300 break;
4301 }
4302 case ISD::SINT_TO_FP: {
4303 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4304 if (Known2.isNonNegative())
4305 Known.makeNonNegative();
4306 else if (Known2.isNegative())
4307 Known.makeNegative();
4308 break;
4309 }
4310 case ISD::FP_TO_UINT_SAT: {
4311 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4312 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4314 break;
4315 }
4316 case ISD::ATOMIC_LOAD: {
4317 // If we are looking at the loaded value.
4318 if (Op.getResNo() == 0) {
4319 auto *AT = cast<AtomicSDNode>(Op);
4320 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4321 KnownBits KnownScalarMemory(ScalarMemorySize);
4322 if (const MDNode *MD = AT->getRanges())
4323 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4324
4325 switch (AT->getExtensionType()) {
4326 case ISD::ZEXTLOAD:
4327 Known = KnownScalarMemory.zext(BitWidth);
4328 break;
4329 case ISD::SEXTLOAD:
4330 Known = KnownScalarMemory.sext(BitWidth);
4331 break;
4332 case ISD::EXTLOAD:
4333 switch (TLI->getExtendForAtomicOps()) {
4334 case ISD::ZERO_EXTEND:
4335 Known = KnownScalarMemory.zext(BitWidth);
4336 break;
4337 case ISD::SIGN_EXTEND:
4338 Known = KnownScalarMemory.sext(BitWidth);
4339 break;
4340 default:
4341 Known = KnownScalarMemory.anyext(BitWidth);
4342 break;
4343 }
4344 break;
4345 case ISD::NON_EXTLOAD:
4346 Known = KnownScalarMemory;
4347 break;
4348 }
4349 assert(Known.getBitWidth() == BitWidth);
4350 }
4351 break;
4352 }
4353 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4354 if (Op.getResNo() == 1) {
4355 // The boolean result conforms to getBooleanContents.
4356 // If we know the result of a setcc has the top bits zero, use this info.
4357 // We know that we have an integer-based boolean since these operations
4358 // are only available for integer.
4359 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4361 BitWidth > 1)
4362 Known.Zero.setBitsFrom(1);
4363 break;
4364 }
4365 [[fallthrough]];
4366 case ISD::ATOMIC_CMP_SWAP:
4367 case ISD::ATOMIC_SWAP:
4368 case ISD::ATOMIC_LOAD_ADD:
4369 case ISD::ATOMIC_LOAD_SUB:
4370 case ISD::ATOMIC_LOAD_AND:
4371 case ISD::ATOMIC_LOAD_CLR:
4372 case ISD::ATOMIC_LOAD_OR:
4373 case ISD::ATOMIC_LOAD_XOR:
4374 case ISD::ATOMIC_LOAD_NAND:
4375 case ISD::ATOMIC_LOAD_MIN:
4376 case ISD::ATOMIC_LOAD_MAX:
4377 case ISD::ATOMIC_LOAD_UMIN:
4378 case ISD::ATOMIC_LOAD_UMAX: {
4379 // If we are looking at the loaded value.
4380 if (Op.getResNo() == 0) {
4381 auto *AT = cast<AtomicSDNode>(Op);
4382 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4383
4384 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4385 Known.Zero.setBitsFrom(MemBits);
4386 }
4387 break;
4388 }
4389 case ISD::FrameIndex:
4391 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4392 Known, getMachineFunction());
4393 break;
4394
4395 default:
4396 if (Opcode < ISD::BUILTIN_OP_END)
4397 break;
4398 [[fallthrough]];
4402 // TODO: Probably okay to remove after audit; here to reduce change size
4403 // in initial enablement patch for scalable vectors
4404 if (Op.getValueType().isScalableVector())
4405 break;
4406
4407 // Allow the target to implement this method for its nodes.
4408 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4409 break;
4410 }
4411
4412 return Known;
4413}
4414
4415/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4428
4431 // X + 0 never overflow
4432 if (isNullConstant(N1))
4433 return OFK_Never;
4434
4435 // If both operands each have at least two sign bits, the addition
4436 // cannot overflow.
4437 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4438 return OFK_Never;
4439
4440 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4441 return OFK_Sometime;
4442}
4443
4446 // X + 0 never overflow
4447 if (isNullConstant(N1))
4448 return OFK_Never;
4449
4450 // mulhi + 1 never overflow
4451 KnownBits N1Known = computeKnownBits(N1);
4452 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4453 N1Known.getMaxValue().ult(2))
4454 return OFK_Never;
4455
4456 KnownBits N0Known = computeKnownBits(N0);
4457 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4458 N0Known.getMaxValue().ult(2))
4459 return OFK_Never;
4460
4461 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4462 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4463 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4464 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4465}
4466
4469 // X - 0 never overflow
4470 if (isNullConstant(N1))
4471 return OFK_Never;
4472
4473 // If both operands each have at least two sign bits, the subtraction
4474 // cannot overflow.
4475 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4476 return OFK_Never;
4477
4478 KnownBits N0Known = computeKnownBits(N0);
4479 KnownBits N1Known = computeKnownBits(N1);
4480 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4481 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4482 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4483}
4484
4487 // X - 0 never overflow
4488 if (isNullConstant(N1))
4489 return OFK_Never;
4490
4491 KnownBits N0Known = computeKnownBits(N0);
4492 KnownBits N1Known = computeKnownBits(N1);
4493 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4494 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4495 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4496}
4497
4500 // X * 0 and X * 1 never overflow.
4501 if (isNullConstant(N1) || isOneConstant(N1))
4502 return OFK_Never;
4503
4504 KnownBits N0Known = computeKnownBits(N0);
4505 KnownBits N1Known = computeKnownBits(N1);
4506 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4507 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4508 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4509}
4510
4513 // X * 0 and X * 1 never overflow.
4514 if (isNullConstant(N1) || isOneConstant(N1))
4515 return OFK_Never;
4516
4517 // Get the size of the result.
4518 unsigned BitWidth = N0.getScalarValueSizeInBits();
4519
4520 // Sum of the sign bits.
4521 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4522
4523 // If we have enough sign bits, then there's no overflow.
4524 if (SignBits > BitWidth + 1)
4525 return OFK_Never;
4526
4527 if (SignBits == BitWidth + 1) {
4528 // The overflow occurs when the true multiplication of the
4529 // the operands is the minimum negative number.
4530 KnownBits N0Known = computeKnownBits(N0);
4531 KnownBits N1Known = computeKnownBits(N1);
4532 // If one of the operands is non-negative, then there's no
4533 // overflow.
4534 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4535 return OFK_Never;
4536 }
4537
4538 return OFK_Sometime;
4539}
4540
4542 if (Depth >= MaxRecursionDepth)
4543 return false; // Limit search depth.
4544
4545 EVT OpVT = Val.getValueType();
4546 unsigned BitWidth = OpVT.getScalarSizeInBits();
4547
4548 // Is the constant a known power of 2?
4550 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4551 }))
4552 return true;
4553
4554 // A left-shift of a constant one will have exactly one bit set because
4555 // shifting the bit off the end is undefined.
4556 if (Val.getOpcode() == ISD::SHL) {
4557 auto *C = isConstOrConstSplat(Val.getOperand(0));
4558 if (C && C->getAPIntValue() == 1)
4559 return true;
4560 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4561 isKnownNeverZero(Val, Depth);
4562 }
4563
4564 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4565 // one bit set.
4566 if (Val.getOpcode() == ISD::SRL) {
4567 auto *C = isConstOrConstSplat(Val.getOperand(0));
4568 if (C && C->getAPIntValue().isSignMask())
4569 return true;
4570 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4571 isKnownNeverZero(Val, Depth);
4572 }
4573
4574 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4575 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4576
4577 // Are all operands of a build vector constant powers of two?
4578 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4579 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4580 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4581 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4582 return false;
4583 }))
4584 return true;
4585
4586 // Is the operand of a splat vector a constant power of two?
4587 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4589 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4590 return true;
4591
4592 // vscale(power-of-two) is a power-of-two for some targets
4593 if (Val.getOpcode() == ISD::VSCALE &&
4594 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4596 return true;
4597
4598 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4599 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4600 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4602
4603 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4604 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4606
4607 // Looking for `x & -x` pattern:
4608 // If x == 0:
4609 // x & -x -> 0
4610 // If x != 0:
4611 // x & -x -> non-zero pow2
4612 // so if we find the pattern return whether we know `x` is non-zero.
4613 SDValue X;
4614 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4615 return isKnownNeverZero(X, Depth);
4616
4617 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4618 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4619
4620 // More could be done here, though the above checks are enough
4621 // to handle some common cases.
4622 return false;
4623}
4624
4626 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4627 return C1->getValueAPF().getExactLog2Abs() >= 0;
4628
4629 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4630 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4631
4632 return false;
4633}
4634
4636 EVT VT = Op.getValueType();
4637
4638 // Since the number of lanes in a scalable vector is unknown at compile time,
4639 // we track one bit which is implicitly broadcast to all lanes. This means
4640 // that all lanes in a scalable vector are considered demanded.
4641 APInt DemandedElts = VT.isFixedLengthVector()
4643 : APInt(1, 1);
4644 return ComputeNumSignBits(Op, DemandedElts, Depth);
4645}
4646
4647unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4648 unsigned Depth) const {
4649 EVT VT = Op.getValueType();
4650 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4651 unsigned VTBits = VT.getScalarSizeInBits();
4652 unsigned NumElts = DemandedElts.getBitWidth();
4653 unsigned Tmp, Tmp2;
4654 unsigned FirstAnswer = 1;
4655
4656 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4657 const APInt &Val = C->getAPIntValue();
4658 return Val.getNumSignBits();
4659 }
4660
4661 if (Depth >= MaxRecursionDepth)
4662 return 1; // Limit search depth.
4663
4664 if (!DemandedElts)
4665 return 1; // No demanded elts, better to assume we don't know anything.
4666
4667 unsigned Opcode = Op.getOpcode();
4668 switch (Opcode) {
4669 default: break;
4670 case ISD::AssertSext:
4671 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4672 return VTBits-Tmp+1;
4673 case ISD::AssertZext:
4674 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4675 return VTBits-Tmp;
4676 case ISD::FREEZE:
4677 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4678 /*PoisonOnly=*/false))
4679 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4680 break;
4681 case ISD::MERGE_VALUES:
4682 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4683 Depth + 1);
4684 case ISD::SPLAT_VECTOR: {
4685 // Check if the sign bits of source go down as far as the truncated value.
4686 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4687 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4688 if (NumSrcSignBits > (NumSrcBits - VTBits))
4689 return NumSrcSignBits - (NumSrcBits - VTBits);
4690 break;
4691 }
4692 case ISD::BUILD_VECTOR:
4693 assert(!VT.isScalableVector());
4694 Tmp = VTBits;
4695 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4696 if (!DemandedElts[i])
4697 continue;
4698
4699 SDValue SrcOp = Op.getOperand(i);
4700 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4701 // for constant nodes to ensure we only look at the sign bits.
4703 APInt T = C->getAPIntValue().trunc(VTBits);
4704 Tmp2 = T.getNumSignBits();
4705 } else {
4706 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4707
4708 if (SrcOp.getValueSizeInBits() != VTBits) {
4709 assert(SrcOp.getValueSizeInBits() > VTBits &&
4710 "Expected BUILD_VECTOR implicit truncation");
4711 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4712 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4713 }
4714 }
4715 Tmp = std::min(Tmp, Tmp2);
4716 }
4717 return Tmp;
4718
4719 case ISD::VECTOR_COMPRESS: {
4720 SDValue Vec = Op.getOperand(0);
4721 SDValue PassThru = Op.getOperand(2);
4722 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4723 if (Tmp == 1)
4724 return 1;
4725 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4726 Tmp = std::min(Tmp, Tmp2);
4727 return Tmp;
4728 }
4729
4730 case ISD::VECTOR_SHUFFLE: {
4731 // Collect the minimum number of sign bits that are shared by every vector
4732 // element referenced by the shuffle.
4733 APInt DemandedLHS, DemandedRHS;
4735 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4736 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4737 DemandedLHS, DemandedRHS))
4738 return 1;
4739
4740 Tmp = std::numeric_limits<unsigned>::max();
4741 if (!!DemandedLHS)
4742 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4743 if (!!DemandedRHS) {
4744 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4745 Tmp = std::min(Tmp, Tmp2);
4746 }
4747 // If we don't know anything, early out and try computeKnownBits fall-back.
4748 if (Tmp == 1)
4749 break;
4750 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4751 return Tmp;
4752 }
4753
4754 case ISD::BITCAST: {
4755 if (VT.isScalableVector())
4756 break;
4757 SDValue N0 = Op.getOperand(0);
4758 EVT SrcVT = N0.getValueType();
4759 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4760
4761 // Ignore bitcasts from unsupported types..
4762 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4763 break;
4764
4765 // Fast handling of 'identity' bitcasts.
4766 if (VTBits == SrcBits)
4767 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4768
4769 bool IsLE = getDataLayout().isLittleEndian();
4770
4771 // Bitcast 'large element' scalar/vector to 'small element' vector.
4772 if ((SrcBits % VTBits) == 0) {
4773 assert(VT.isVector() && "Expected bitcast to vector");
4774
4775 unsigned Scale = SrcBits / VTBits;
4776 APInt SrcDemandedElts =
4777 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4778
4779 // Fast case - sign splat can be simply split across the small elements.
4780 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4781 if (Tmp == SrcBits)
4782 return VTBits;
4783
4784 // Slow case - determine how far the sign extends into each sub-element.
4785 Tmp2 = VTBits;
4786 for (unsigned i = 0; i != NumElts; ++i)
4787 if (DemandedElts[i]) {
4788 unsigned SubOffset = i % Scale;
4789 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4790 SubOffset = SubOffset * VTBits;
4791 if (Tmp <= SubOffset)
4792 return 1;
4793 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4794 }
4795 return Tmp2;
4796 }
4797 break;
4798 }
4799
4801 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4802 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4803 return VTBits - Tmp + 1;
4804 case ISD::SIGN_EXTEND:
4805 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4806 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4808 // Max of the input and what this extends.
4809 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4810 Tmp = VTBits-Tmp+1;
4811 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4812 return std::max(Tmp, Tmp2);
4814 if (VT.isScalableVector())
4815 break;
4816 SDValue Src = Op.getOperand(0);
4817 EVT SrcVT = Src.getValueType();
4818 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4819 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4820 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4821 }
4822 case ISD::SRA:
4823 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4824 // SRA X, C -> adds C sign bits.
4825 if (std::optional<unsigned> ShAmt =
4826 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4827 Tmp = std::min(Tmp + *ShAmt, VTBits);
4828 return Tmp;
4829 case ISD::SHL:
4830 if (std::optional<ConstantRange> ShAmtRange =
4831 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4832 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4833 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4834 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4835 // shifted out, then we can compute the number of sign bits for the
4836 // operand being extended. A future improvement could be to pass along the
4837 // "shifted left by" information in the recursive calls to
4838 // ComputeKnownSignBits. Allowing us to handle this more generically.
4839 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4840 SDValue Ext = Op.getOperand(0);
4841 EVT ExtVT = Ext.getValueType();
4842 SDValue Extendee = Ext.getOperand(0);
4843 EVT ExtendeeVT = Extendee.getValueType();
4844 unsigned SizeDifference =
4845 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4846 if (SizeDifference <= MinShAmt) {
4847 Tmp = SizeDifference +
4848 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4849 if (MaxShAmt < Tmp)
4850 return Tmp - MaxShAmt;
4851 }
4852 }
4853 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4854 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4855 if (MaxShAmt < Tmp)
4856 return Tmp - MaxShAmt;
4857 }
4858 break;
4859 case ISD::AND:
4860 case ISD::OR:
4861 case ISD::XOR: // NOT is handled here.
4862 // Logical binary ops preserve the number of sign bits at the worst.
4863 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4864 if (Tmp != 1) {
4865 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4866 FirstAnswer = std::min(Tmp, Tmp2);
4867 // We computed what we know about the sign bits as our first
4868 // answer. Now proceed to the generic code that uses
4869 // computeKnownBits, and pick whichever answer is better.
4870 }
4871 break;
4872
4873 case ISD::SELECT:
4874 case ISD::VSELECT:
4875 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4876 if (Tmp == 1) return 1; // Early out.
4877 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4878 return std::min(Tmp, Tmp2);
4879 case ISD::SELECT_CC:
4880 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4881 if (Tmp == 1) return 1; // Early out.
4882 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4883 return std::min(Tmp, Tmp2);
4884
4885 case ISD::SMIN:
4886 case ISD::SMAX: {
4887 // If we have a clamp pattern, we know that the number of sign bits will be
4888 // the minimum of the clamp min/max range.
4889 bool IsMax = (Opcode == ISD::SMAX);
4890 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4891 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4892 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4893 CstHigh =
4894 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4895 if (CstLow && CstHigh) {
4896 if (!IsMax)
4897 std::swap(CstLow, CstHigh);
4898 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4899 Tmp = CstLow->getAPIntValue().getNumSignBits();
4900 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4901 return std::min(Tmp, Tmp2);
4902 }
4903 }
4904
4905 // Fallback - just get the minimum number of sign bits of the operands.
4906 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4907 if (Tmp == 1)
4908 return 1; // Early out.
4909 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4910 return std::min(Tmp, Tmp2);
4911 }
4912 case ISD::UMIN:
4913 case ISD::UMAX:
4914 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4915 if (Tmp == 1)
4916 return 1; // Early out.
4917 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4918 return std::min(Tmp, Tmp2);
4919 case ISD::SSUBO_CARRY:
4920 case ISD::USUBO_CARRY:
4921 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4922 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4923 return VTBits;
4924 [[fallthrough]];
4925 case ISD::SADDO:
4926 case ISD::UADDO:
4927 case ISD::SADDO_CARRY:
4928 case ISD::UADDO_CARRY:
4929 case ISD::SSUBO:
4930 case ISD::USUBO:
4931 case ISD::SMULO:
4932 case ISD::UMULO:
4933 if (Op.getResNo() != 1)
4934 break;
4935 // The boolean result conforms to getBooleanContents. Fall through.
4936 // If setcc returns 0/-1, all bits are sign bits.
4937 // We know that we have an integer-based boolean since these operations
4938 // are only available for integer.
4939 if (TLI->getBooleanContents(VT.isVector(), false) ==
4941 return VTBits;
4942 break;
4943 case ISD::SETCC:
4944 case ISD::SETCCCARRY:
4945 case ISD::STRICT_FSETCC:
4946 case ISD::STRICT_FSETCCS: {
4947 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4948 // If setcc returns 0/-1, all bits are sign bits.
4949 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4951 return VTBits;
4952 break;
4953 }
4954 case ISD::ROTL:
4955 case ISD::ROTR:
4956 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4957
4958 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4959 if (Tmp == VTBits)
4960 return VTBits;
4961
4962 if (ConstantSDNode *C =
4963 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4964 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4965
4966 // Handle rotate right by N like a rotate left by 32-N.
4967 if (Opcode == ISD::ROTR)
4968 RotAmt = (VTBits - RotAmt) % VTBits;
4969
4970 // If we aren't rotating out all of the known-in sign bits, return the
4971 // number that are left. This handles rotl(sext(x), 1) for example.
4972 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4973 }
4974 break;
4975 case ISD::ADD:
4976 case ISD::ADDC:
4977 // TODO: Move Operand 1 check before Operand 0 check
4978 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4979 if (Tmp == 1) return 1; // Early out.
4980
4981 // Special case decrementing a value (ADD X, -1):
4982 if (ConstantSDNode *CRHS =
4983 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4984 if (CRHS->isAllOnes()) {
4985 KnownBits Known =
4986 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4987
4988 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4989 // sign bits set.
4990 if ((Known.Zero | 1).isAllOnes())
4991 return VTBits;
4992
4993 // If we are subtracting one from a positive number, there is no carry
4994 // out of the result.
4995 if (Known.isNonNegative())
4996 return Tmp;
4997 }
4998
4999 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5000 if (Tmp2 == 1) return 1; // Early out.
5001
5002 // Add can have at most one carry bit. Thus we know that the output
5003 // is, at worst, one more bit than the inputs.
5004 return std::min(Tmp, Tmp2) - 1;
5005 case ISD::SUB:
5006 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5007 if (Tmp2 == 1) return 1; // Early out.
5008
5009 // Handle NEG.
5010 if (ConstantSDNode *CLHS =
5011 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5012 if (CLHS->isZero()) {
5013 KnownBits Known =
5014 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5015 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5016 // sign bits set.
5017 if ((Known.Zero | 1).isAllOnes())
5018 return VTBits;
5019
5020 // If the input is known to be positive (the sign bit is known clear),
5021 // the output of the NEG has the same number of sign bits as the input.
5022 if (Known.isNonNegative())
5023 return Tmp2;
5024
5025 // Otherwise, we treat this like a SUB.
5026 }
5027
5028 // Sub can have at most one carry bit. Thus we know that the output
5029 // is, at worst, one more bit than the inputs.
5030 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5031 if (Tmp == 1) return 1; // Early out.
5032 return std::min(Tmp, Tmp2) - 1;
5033 case ISD::MUL: {
5034 // The output of the Mul can be at most twice the valid bits in the inputs.
5035 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5036 if (SignBitsOp0 == 1)
5037 break;
5038 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5039 if (SignBitsOp1 == 1)
5040 break;
5041 unsigned OutValidBits =
5042 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5043 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5044 }
5045 case ISD::AVGCEILS:
5046 case ISD::AVGFLOORS:
5047 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5048 if (Tmp == 1)
5049 return 1; // Early out.
5050 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5051 return std::min(Tmp, Tmp2);
5052 case ISD::SREM:
5053 // The sign bit is the LHS's sign bit, except when the result of the
5054 // remainder is zero. The magnitude of the result should be less than or
5055 // equal to the magnitude of the LHS. Therefore, the result should have
5056 // at least as many sign bits as the left hand side.
5057 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5058 case ISD::TRUNCATE: {
5059 // Check if the sign bits of source go down as far as the truncated value.
5060 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5061 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5062 if (NumSrcSignBits > (NumSrcBits - VTBits))
5063 return NumSrcSignBits - (NumSrcBits - VTBits);
5064 break;
5065 }
5066 case ISD::EXTRACT_ELEMENT: {
5067 if (VT.isScalableVector())
5068 break;
5069 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5070 const int BitWidth = Op.getValueSizeInBits();
5071 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5072
5073 // Get reverse index (starting from 1), Op1 value indexes elements from
5074 // little end. Sign starts at big end.
5075 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5076
5077 // If the sign portion ends in our element the subtraction gives correct
5078 // result. Otherwise it gives either negative or > bitwidth result
5079 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5080 }
5082 if (VT.isScalableVector())
5083 break;
5084 // If we know the element index, split the demand between the
5085 // source vector and the inserted element, otherwise assume we need
5086 // the original demanded vector elements and the value.
5087 SDValue InVec = Op.getOperand(0);
5088 SDValue InVal = Op.getOperand(1);
5089 SDValue EltNo = Op.getOperand(2);
5090 bool DemandedVal = true;
5091 APInt DemandedVecElts = DemandedElts;
5092 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5093 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5094 unsigned EltIdx = CEltNo->getZExtValue();
5095 DemandedVal = !!DemandedElts[EltIdx];
5096 DemandedVecElts.clearBit(EltIdx);
5097 }
5098 Tmp = std::numeric_limits<unsigned>::max();
5099 if (DemandedVal) {
5100 // TODO - handle implicit truncation of inserted elements.
5101 if (InVal.getScalarValueSizeInBits() != VTBits)
5102 break;
5103 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5104 Tmp = std::min(Tmp, Tmp2);
5105 }
5106 if (!!DemandedVecElts) {
5107 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5108 Tmp = std::min(Tmp, Tmp2);
5109 }
5110 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5111 return Tmp;
5112 }
5114 assert(!VT.isScalableVector());
5115 SDValue InVec = Op.getOperand(0);
5116 SDValue EltNo = Op.getOperand(1);
5117 EVT VecVT = InVec.getValueType();
5118 // ComputeNumSignBits not yet implemented for scalable vectors.
5119 if (VecVT.isScalableVector())
5120 break;
5121 const unsigned BitWidth = Op.getValueSizeInBits();
5122 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5123 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5124
5125 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5126 // anything about sign bits. But if the sizes match we can derive knowledge
5127 // about sign bits from the vector operand.
5128 if (BitWidth != EltBitWidth)
5129 break;
5130
5131 // If we know the element index, just demand that vector element, else for
5132 // an unknown element index, ignore DemandedElts and demand them all.
5133 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5134 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5135 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5136 DemandedSrcElts =
5137 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5138
5139 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5140 }
5142 // Offset the demanded elts by the subvector index.
5143 SDValue Src = Op.getOperand(0);
5144 // Bail until we can represent demanded elements for scalable vectors.
5145 if (Src.getValueType().isScalableVector())
5146 break;
5147 uint64_t Idx = Op.getConstantOperandVal(1);
5148 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5149 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5150 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5151 }
5152 case ISD::CONCAT_VECTORS: {
5153 if (VT.isScalableVector())
5154 break;
5155 // Determine the minimum number of sign bits across all demanded
5156 // elts of the input vectors. Early out if the result is already 1.
5157 Tmp = std::numeric_limits<unsigned>::max();
5158 EVT SubVectorVT = Op.getOperand(0).getValueType();
5159 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5160 unsigned NumSubVectors = Op.getNumOperands();
5161 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5162 APInt DemandedSub =
5163 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5164 if (!DemandedSub)
5165 continue;
5166 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5167 Tmp = std::min(Tmp, Tmp2);
5168 }
5169 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5170 return Tmp;
5171 }
5172 case ISD::INSERT_SUBVECTOR: {
5173 if (VT.isScalableVector())
5174 break;
5175 // Demand any elements from the subvector and the remainder from the src its
5176 // inserted into.
5177 SDValue Src = Op.getOperand(0);
5178 SDValue Sub = Op.getOperand(1);
5179 uint64_t Idx = Op.getConstantOperandVal(2);
5180 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5181 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5182 APInt DemandedSrcElts = DemandedElts;
5183 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5184
5185 Tmp = std::numeric_limits<unsigned>::max();
5186 if (!!DemandedSubElts) {
5187 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5188 if (Tmp == 1)
5189 return 1; // early-out
5190 }
5191 if (!!DemandedSrcElts) {
5192 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5193 Tmp = std::min(Tmp, Tmp2);
5194 }
5195 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5196 return Tmp;
5197 }
5198 case ISD::LOAD: {
5200 if (const MDNode *Ranges = LD->getRanges()) {
5201 if (DemandedElts != 1)
5202 break;
5203
5205 if (VTBits > CR.getBitWidth()) {
5206 switch (LD->getExtensionType()) {
5207 case ISD::SEXTLOAD:
5208 CR = CR.signExtend(VTBits);
5209 break;
5210 case ISD::ZEXTLOAD:
5211 CR = CR.zeroExtend(VTBits);
5212 break;
5213 default:
5214 break;
5215 }
5216 }
5217
5218 if (VTBits != CR.getBitWidth())
5219 break;
5220 return std::min(CR.getSignedMin().getNumSignBits(),
5222 }
5223
5224 break;
5225 }
5226 case ISD::ATOMIC_CMP_SWAP:
5227 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
5228 case ISD::ATOMIC_SWAP:
5229 case ISD::ATOMIC_LOAD_ADD:
5230 case ISD::ATOMIC_LOAD_SUB:
5231 case ISD::ATOMIC_LOAD_AND:
5232 case ISD::ATOMIC_LOAD_CLR:
5233 case ISD::ATOMIC_LOAD_OR:
5234 case ISD::ATOMIC_LOAD_XOR:
5235 case ISD::ATOMIC_LOAD_NAND:
5236 case ISD::ATOMIC_LOAD_MIN:
5237 case ISD::ATOMIC_LOAD_MAX:
5238 case ISD::ATOMIC_LOAD_UMIN:
5239 case ISD::ATOMIC_LOAD_UMAX:
5240 case ISD::ATOMIC_LOAD: {
5241 auto *AT = cast<AtomicSDNode>(Op);
5242 // If we are looking at the loaded value.
5243 if (Op.getResNo() == 0) {
5244 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5245 if (Tmp == VTBits)
5246 return 1; // early-out
5247
5248 // For atomic_load, prefer to use the extension type.
5249 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5250 switch (AT->getExtensionType()) {
5251 default:
5252 break;
5253 case ISD::SEXTLOAD:
5254 return VTBits - Tmp + 1;
5255 case ISD::ZEXTLOAD:
5256 return VTBits - Tmp;
5257 }
5258 }
5259
5260 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5261 return VTBits - Tmp + 1;
5262 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5263 return VTBits - Tmp;
5264 }
5265 break;
5266 }
5267 }
5268
5269 // If we are looking at the loaded value of the SDNode.
5270 if (Op.getResNo() == 0) {
5271 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5272 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5273 unsigned ExtType = LD->getExtensionType();
5274 switch (ExtType) {
5275 default: break;
5276 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5277 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5278 return VTBits - Tmp + 1;
5279 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5280 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5281 return VTBits - Tmp;
5282 case ISD::NON_EXTLOAD:
5283 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5284 // We only need to handle vectors - computeKnownBits should handle
5285 // scalar cases.
5286 Type *CstTy = Cst->getType();
5287 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5288 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5289 VTBits == CstTy->getScalarSizeInBits()) {
5290 Tmp = VTBits;
5291 for (unsigned i = 0; i != NumElts; ++i) {
5292 if (!DemandedElts[i])
5293 continue;
5294 if (Constant *Elt = Cst->getAggregateElement(i)) {
5295 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5296 const APInt &Value = CInt->getValue();
5297 Tmp = std::min(Tmp, Value.getNumSignBits());
5298 continue;
5299 }
5300 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5301 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5302 Tmp = std::min(Tmp, Value.getNumSignBits());
5303 continue;
5304 }
5305 }
5306 // Unknown type. Conservatively assume no bits match sign bit.
5307 return 1;
5308 }
5309 return Tmp;
5310 }
5311 }
5312 break;
5313 }
5314 }
5315 }
5316
5317 // Allow the target to implement this method for its nodes.
5318 if (Opcode >= ISD::BUILTIN_OP_END ||
5319 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5320 Opcode == ISD::INTRINSIC_W_CHAIN ||
5321 Opcode == ISD::INTRINSIC_VOID) {
5322 // TODO: This can probably be removed once target code is audited. This
5323 // is here purely to reduce patch size and review complexity.
5324 if (!VT.isScalableVector()) {
5325 unsigned NumBits =
5326 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5327 if (NumBits > 1)
5328 FirstAnswer = std::max(FirstAnswer, NumBits);
5329 }
5330 }
5331
5332 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5333 // use this information.
5334 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5335 return std::max(FirstAnswer, Known.countMinSignBits());
5336}
5337
5339 unsigned Depth) const {
5340 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5341 return Op.getScalarValueSizeInBits() - SignBits + 1;
5342}
5343
5345 const APInt &DemandedElts,
5346 unsigned Depth) const {
5347 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5348 return Op.getScalarValueSizeInBits() - SignBits + 1;
5349}
5350
5352 unsigned Depth) const {
5353 // Early out for FREEZE.
5354 if (Op.getOpcode() == ISD::FREEZE)
5355 return true;
5356
5357 EVT VT = Op.getValueType();
5358 APInt DemandedElts = VT.isFixedLengthVector()
5360 : APInt(1, 1);
5361 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5362}
5363
5365 const APInt &DemandedElts,
5366 bool PoisonOnly,
5367 unsigned Depth) const {
5368 unsigned Opcode = Op.getOpcode();
5369
5370 // Early out for FREEZE.
5371 if (Opcode == ISD::FREEZE)
5372 return true;
5373
5374 if (Depth >= MaxRecursionDepth)
5375 return false; // Limit search depth.
5376
5377 if (isIntOrFPConstant(Op))
5378 return true;
5379
5380 switch (Opcode) {
5381 case ISD::CONDCODE:
5382 case ISD::VALUETYPE:
5383 case ISD::FrameIndex:
5385 case ISD::CopyFromReg:
5386 return true;
5387
5388 case ISD::POISON:
5389 return false;
5390
5391 case ISD::UNDEF:
5392 return PoisonOnly;
5393
5394 case ISD::BUILD_VECTOR:
5395 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5396 // this shouldn't affect the result.
5397 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5398 if (!DemandedElts[i])
5399 continue;
5401 Depth + 1))
5402 return false;
5403 }
5404 return true;
5405
5407 SDValue Src = Op.getOperand(0);
5408 if (Src.getValueType().isScalableVector())
5409 break;
5410 uint64_t Idx = Op.getConstantOperandVal(1);
5411 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5412 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5413 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5414 Depth + 1);
5415 }
5416
5417 case ISD::INSERT_SUBVECTOR: {
5418 if (Op.getValueType().isScalableVector())
5419 break;
5420 SDValue Src = Op.getOperand(0);
5421 SDValue Sub = Op.getOperand(1);
5422 uint64_t Idx = Op.getConstantOperandVal(2);
5423 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5424 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5425 APInt DemandedSrcElts = DemandedElts;
5426 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5427
5428 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5429 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5430 return false;
5431 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5432 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5433 return false;
5434 return true;
5435 }
5436
5438 SDValue Src = Op.getOperand(0);
5439 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5440 EVT SrcVT = Src.getValueType();
5441 if (SrcVT.isFixedLengthVector() && IndexC &&
5442 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5443 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5444 IndexC->getZExtValue());
5445 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5446 Depth + 1);
5447 }
5448 break;
5449 }
5450
5452 SDValue InVec = Op.getOperand(0);
5453 SDValue InVal = Op.getOperand(1);
5454 SDValue EltNo = Op.getOperand(2);
5455 EVT VT = InVec.getValueType();
5456 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5457 if (IndexC && VT.isFixedLengthVector() &&
5458 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5459 if (DemandedElts[IndexC->getZExtValue()] &&
5461 return false;
5462 APInt InVecDemandedElts = DemandedElts;
5463 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5464 if (!!InVecDemandedElts &&
5466 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5467 InVecDemandedElts, PoisonOnly, Depth + 1))
5468 return false;
5469 return true;
5470 }
5471 break;
5472 }
5473
5475 // Check upper (known undef) elements.
5476 if (DemandedElts.ugt(1) && !PoisonOnly)
5477 return false;
5478 // Check element zero.
5479 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5480 Op.getOperand(0), PoisonOnly, Depth + 1))
5481 return false;
5482 return true;
5483
5484 case ISD::SPLAT_VECTOR:
5485 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5486 Depth + 1);
5487
5488 case ISD::VECTOR_SHUFFLE: {
5489 APInt DemandedLHS, DemandedRHS;
5490 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5491 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5492 DemandedElts, DemandedLHS, DemandedRHS,
5493 /*AllowUndefElts=*/false))
5494 return false;
5495 if (!DemandedLHS.isZero() &&
5496 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5497 PoisonOnly, Depth + 1))
5498 return false;
5499 if (!DemandedRHS.isZero() &&
5500 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5501 PoisonOnly, Depth + 1))
5502 return false;
5503 return true;
5504 }
5505
5506 case ISD::SHL:
5507 case ISD::SRL:
5508 case ISD::SRA:
5509 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5510 // enough to check operand 0 if Op can't create undef/poison.
5511 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5512 /*ConsiderFlags*/ true, Depth) &&
5513 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5514 PoisonOnly, Depth + 1);
5515
5516 case ISD::BSWAP:
5517 case ISD::CTPOP:
5518 case ISD::BITREVERSE:
5519 case ISD::AND:
5520 case ISD::OR:
5521 case ISD::XOR:
5522 case ISD::ADD:
5523 case ISD::SUB:
5524 case ISD::MUL:
5525 case ISD::SADDSAT:
5526 case ISD::UADDSAT:
5527 case ISD::SSUBSAT:
5528 case ISD::USUBSAT:
5529 case ISD::SSHLSAT:
5530 case ISD::USHLSAT:
5531 case ISD::SMIN:
5532 case ISD::SMAX:
5533 case ISD::UMIN:
5534 case ISD::UMAX:
5535 case ISD::ZERO_EXTEND:
5536 case ISD::SIGN_EXTEND:
5537 case ISD::ANY_EXTEND:
5538 case ISD::TRUNCATE:
5539 case ISD::VSELECT: {
5540 // If Op can't create undef/poison and none of its operands are undef/poison
5541 // then Op is never undef/poison. A difference from the more common check
5542 // below, outside the switch, is that we handle elementwise operations for
5543 // which the DemandedElts mask is valid for all operands here.
5544 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5545 /*ConsiderFlags*/ true, Depth) &&
5546 all_of(Op->ops(), [&](SDValue V) {
5547 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5548 PoisonOnly, Depth + 1);
5549 });
5550 }
5551
5552 // TODO: Search for noundef attributes from library functions.
5553
5554 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5555
5556 default:
5557 // Allow the target to implement this method for its nodes.
5558 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5559 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5560 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5561 Op, DemandedElts, *this, PoisonOnly, Depth);
5562 break;
5563 }
5564
5565 // If Op can't create undef/poison and none of its operands are undef/poison
5566 // then Op is never undef/poison.
5567 // NOTE: TargetNodes can handle this in themselves in
5568 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5569 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5570 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5571 Depth) &&
5572 all_of(Op->ops(), [&](SDValue V) {
5573 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5574 });
5575}
5576
5578 bool ConsiderFlags,
5579 unsigned Depth) const {
5580 EVT VT = Op.getValueType();
5581 APInt DemandedElts = VT.isFixedLengthVector()
5583 : APInt(1, 1);
5584 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5585 Depth);
5586}
5587
5589 bool PoisonOnly, bool ConsiderFlags,
5590 unsigned Depth) const {
5591 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5592 return true;
5593
5594 unsigned Opcode = Op.getOpcode();
5595 switch (Opcode) {
5596 case ISD::AssertSext:
5597 case ISD::AssertZext:
5598 case ISD::AssertAlign:
5600 // Assertion nodes can create poison if the assertion fails.
5601 return true;
5602
5603 case ISD::FREEZE:
5607 case ISD::SADDSAT:
5608 case ISD::UADDSAT:
5609 case ISD::SSUBSAT:
5610 case ISD::USUBSAT:
5611 case ISD::MULHU:
5612 case ISD::MULHS:
5613 case ISD::AVGFLOORS:
5614 case ISD::AVGFLOORU:
5615 case ISD::AVGCEILS:
5616 case ISD::AVGCEILU:
5617 case ISD::ABDU:
5618 case ISD::ABDS:
5619 case ISD::SMIN:
5620 case ISD::SMAX:
5621 case ISD::SCMP:
5622 case ISD::UMIN:
5623 case ISD::UMAX:
5624 case ISD::UCMP:
5625 case ISD::AND:
5626 case ISD::XOR:
5627 case ISD::ROTL:
5628 case ISD::ROTR:
5629 case ISD::FSHL:
5630 case ISD::FSHR:
5631 case ISD::BSWAP:
5632 case ISD::CTTZ:
5633 case ISD::CTLZ:
5634 case ISD::CTPOP:
5635 case ISD::BITREVERSE:
5636 case ISD::PARITY:
5637 case ISD::SIGN_EXTEND:
5638 case ISD::TRUNCATE:
5642 case ISD::BITCAST:
5643 case ISD::BUILD_VECTOR:
5644 case ISD::BUILD_PAIR:
5645 case ISD::SPLAT_VECTOR:
5646 case ISD::FABS:
5647 return false;
5648
5649 case ISD::ABS:
5650 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5651 // Different to Intrinsic::abs.
5652 return false;
5653
5654 case ISD::ADDC:
5655 case ISD::SUBC:
5656 case ISD::ADDE:
5657 case ISD::SUBE:
5658 case ISD::SADDO:
5659 case ISD::SSUBO:
5660 case ISD::SMULO:
5661 case ISD::SADDO_CARRY:
5662 case ISD::SSUBO_CARRY:
5663 case ISD::UADDO:
5664 case ISD::USUBO:
5665 case ISD::UMULO:
5666 case ISD::UADDO_CARRY:
5667 case ISD::USUBO_CARRY:
5668 // No poison on result or overflow flags.
5669 return false;
5670
5671 case ISD::SELECT_CC:
5672 case ISD::SETCC: {
5673 // Integer setcc cannot create undef or poison.
5674 if (Op.getOperand(0).getValueType().isInteger())
5675 return false;
5676
5677 // FP compares are more complicated. They can create poison for nan/infinity
5678 // based on options and flags. The options and flags also cause special
5679 // nonan condition codes to be used. Those condition codes may be preserved
5680 // even if the nonan flag is dropped somewhere.
5681 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5682 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5683 return (unsigned)CCCode & 0x10U;
5684 }
5685
5686 case ISD::OR:
5687 case ISD::ZERO_EXTEND:
5688 case ISD::SELECT:
5689 case ISD::VSELECT:
5690 case ISD::ADD:
5691 case ISD::SUB:
5692 case ISD::MUL:
5693 case ISD::FNEG:
5694 case ISD::FADD:
5695 case ISD::FSUB:
5696 case ISD::FMUL:
5697 case ISD::FDIV:
5698 case ISD::FREM:
5699 case ISD::FCOPYSIGN:
5700 case ISD::FMA:
5701 case ISD::FMAD:
5702 case ISD::FMULADD:
5703 case ISD::FP_EXTEND:
5706 // No poison except from flags (which is handled above)
5707 return false;
5708
5709 case ISD::SHL:
5710 case ISD::SRL:
5711 case ISD::SRA:
5712 // If the max shift amount isn't in range, then the shift can
5713 // create poison.
5714 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5715
5718 // If the amount is zero then the result will be poison.
5719 // TODO: Add isKnownNeverZero DemandedElts handling.
5720 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5721
5723 // Check if we demand any upper (undef) elements.
5724 return !PoisonOnly && DemandedElts.ugt(1);
5725
5728 // Ensure that the element index is in bounds.
5729 EVT VecVT = Op.getOperand(0).getValueType();
5730 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5731 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5732 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5733 }
5734
5735 case ISD::VECTOR_SHUFFLE: {
5736 // Check for any demanded shuffle element that is undef.
5737 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5738 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5739 if (Elt < 0 && DemandedElts[Idx])
5740 return true;
5741 return false;
5742 }
5743
5745 return false;
5746
5747 default:
5748 // Allow the target to implement this method for its nodes.
5749 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5750 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5751 return TLI->canCreateUndefOrPoisonForTargetNode(
5752 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5753 break;
5754 }
5755
5756 // Be conservative and return true.
5757 return true;
5758}
5759
5760bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5761 unsigned Opcode = Op.getOpcode();
5762 if (Opcode == ISD::OR)
5763 return Op->getFlags().hasDisjoint() ||
5764 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5765 if (Opcode == ISD::XOR)
5766 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5767 return false;
5768}
5769
5771 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5772 (Op.isAnyAdd() || isADDLike(Op));
5773}
5774
5776 unsigned Depth) const {
5777 EVT VT = Op.getValueType();
5778
5779 // Since the number of lanes in a scalable vector is unknown at compile time,
5780 // we track one bit which is implicitly broadcast to all lanes. This means
5781 // that all lanes in a scalable vector are considered demanded.
5782 APInt DemandedElts = VT.isFixedLengthVector()
5784 : APInt(1, 1);
5785
5786 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5787}
5788
5790 bool SNaN, unsigned Depth) const {
5791 assert(!DemandedElts.isZero() && "No demanded elements");
5792
5793 // If we're told that NaNs won't happen, assume they won't.
5794 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5795 return true;
5796
5797 if (Depth >= MaxRecursionDepth)
5798 return false; // Limit search depth.
5799
5800 // If the value is a constant, we can obviously see if it is a NaN or not.
5802 return !C->getValueAPF().isNaN() ||
5803 (SNaN && !C->getValueAPF().isSignaling());
5804 }
5805
5806 unsigned Opcode = Op.getOpcode();
5807 switch (Opcode) {
5808 case ISD::FADD:
5809 case ISD::FSUB:
5810 case ISD::FMUL:
5811 case ISD::FDIV:
5812 case ISD::FREM:
5813 case ISD::FSIN:
5814 case ISD::FCOS:
5815 case ISD::FTAN:
5816 case ISD::FASIN:
5817 case ISD::FACOS:
5818 case ISD::FATAN:
5819 case ISD::FATAN2:
5820 case ISD::FSINH:
5821 case ISD::FCOSH:
5822 case ISD::FTANH:
5823 case ISD::FMA:
5824 case ISD::FMULADD:
5825 case ISD::FMAD: {
5826 if (SNaN)
5827 return true;
5828 // TODO: Need isKnownNeverInfinity
5829 return false;
5830 }
5831 case ISD::FCANONICALIZE:
5832 case ISD::FEXP:
5833 case ISD::FEXP2:
5834 case ISD::FEXP10:
5835 case ISD::FTRUNC:
5836 case ISD::FFLOOR:
5837 case ISD::FCEIL:
5838 case ISD::FROUND:
5839 case ISD::FROUNDEVEN:
5840 case ISD::LROUND:
5841 case ISD::LLROUND:
5842 case ISD::FRINT:
5843 case ISD::LRINT:
5844 case ISD::LLRINT:
5845 case ISD::FNEARBYINT:
5846 case ISD::FLDEXP: {
5847 if (SNaN)
5848 return true;
5849 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5850 }
5851 case ISD::FABS:
5852 case ISD::FNEG:
5853 case ISD::FCOPYSIGN: {
5854 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5855 }
5856 case ISD::SELECT:
5857 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5858 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5859 case ISD::FP_EXTEND:
5860 case ISD::FP_ROUND: {
5861 if (SNaN)
5862 return true;
5863 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5864 }
5865 case ISD::SINT_TO_FP:
5866 case ISD::UINT_TO_FP:
5867 return true;
5868 case ISD::FSQRT: // Need is known positive
5869 case ISD::FLOG:
5870 case ISD::FLOG2:
5871 case ISD::FLOG10:
5872 case ISD::FPOWI:
5873 case ISD::FPOW: {
5874 if (SNaN)
5875 return true;
5876 // TODO: Refine on operand
5877 return false;
5878 }
5879 case ISD::FMINNUM:
5880 case ISD::FMAXNUM:
5881 case ISD::FMINIMUMNUM:
5882 case ISD::FMAXIMUMNUM: {
5883 // Only one needs to be known not-nan, since it will be returned if the
5884 // other ends up being one.
5885 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5886 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5887 }
5888 case ISD::FMINNUM_IEEE:
5889 case ISD::FMAXNUM_IEEE: {
5890 if (SNaN)
5891 return true;
5892 // This can return a NaN if either operand is an sNaN, or if both operands
5893 // are NaN.
5894 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5895 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5896 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5897 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5898 }
5899 case ISD::FMINIMUM:
5900 case ISD::FMAXIMUM: {
5901 // TODO: Does this quiet or return the origina NaN as-is?
5902 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5903 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5904 }
5906 SDValue Src = Op.getOperand(0);
5907 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5908 EVT SrcVT = Src.getValueType();
5909 if (SrcVT.isFixedLengthVector() && Idx &&
5910 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5911 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5912 Idx->getZExtValue());
5913 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5914 }
5915 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5916 }
5918 SDValue Src = Op.getOperand(0);
5919 if (Src.getValueType().isFixedLengthVector()) {
5920 unsigned Idx = Op.getConstantOperandVal(1);
5921 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5922 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5923 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5924 }
5925 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5926 }
5927 case ISD::INSERT_SUBVECTOR: {
5928 SDValue BaseVector = Op.getOperand(0);
5929 SDValue SubVector = Op.getOperand(1);
5930 EVT BaseVectorVT = BaseVector.getValueType();
5931 if (BaseVectorVT.isFixedLengthVector()) {
5932 unsigned Idx = Op.getConstantOperandVal(2);
5933 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5934 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5935
5936 // Clear/Extract the bits at the position where the subvector will be
5937 // inserted.
5938 APInt DemandedMask =
5939 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5940 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5941 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5942
5943 bool NeverNaN = true;
5944 if (!DemandedSrcElts.isZero())
5945 NeverNaN &=
5946 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5947 if (NeverNaN && !DemandedSubElts.isZero())
5948 NeverNaN &=
5949 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5950 return NeverNaN;
5951 }
5952 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5953 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5954 }
5955 case ISD::BUILD_VECTOR: {
5956 unsigned NumElts = Op.getNumOperands();
5957 for (unsigned I = 0; I != NumElts; ++I)
5958 if (DemandedElts[I] &&
5959 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
5960 return false;
5961 return true;
5962 }
5963 case ISD::AssertNoFPClass: {
5964 FPClassTest NoFPClass =
5965 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
5966 if ((NoFPClass & fcNan) == fcNan)
5967 return true;
5968 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
5969 return true;
5970 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5971 }
5972 default:
5973 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5974 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
5975 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
5976 Depth);
5977 }
5978
5979 return false;
5980 }
5981}
5982
5984 assert(Op.getValueType().isFloatingPoint() &&
5985 "Floating point type expected");
5986
5987 // If the value is a constant, we can obviously see if it is a zero or not.
5989 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5990}
5991
5993 if (Depth >= MaxRecursionDepth)
5994 return false; // Limit search depth.
5995
5996 assert(!Op.getValueType().isFloatingPoint() &&
5997 "Floating point types unsupported - use isKnownNeverZeroFloat");
5998
5999 // If the value is a constant, we can obviously see if it is a zero or not.
6001 [](ConstantSDNode *C) { return !C->isZero(); }))
6002 return true;
6003
6004 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6005 // some degree.
6006 switch (Op.getOpcode()) {
6007 default:
6008 break;
6009
6010 case ISD::OR:
6011 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6012 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6013
6014 case ISD::VSELECT:
6015 case ISD::SELECT:
6016 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6017 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6018
6019 case ISD::SHL: {
6020 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6021 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6022 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6023 // 1 << X is never zero.
6024 if (ValKnown.One[0])
6025 return true;
6026 // If max shift cnt of known ones is non-zero, result is non-zero.
6027 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6028 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6029 !ValKnown.One.shl(MaxCnt).isZero())
6030 return true;
6031 break;
6032 }
6033 case ISD::UADDSAT:
6034 case ISD::UMAX:
6035 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6036 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6037
6038 // For smin/smax: If either operand is known negative/positive
6039 // respectively we don't need the other to be known at all.
6040 case ISD::SMAX: {
6041 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6042 if (Op1.isStrictlyPositive())
6043 return true;
6044
6045 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6046 if (Op0.isStrictlyPositive())
6047 return true;
6048
6049 if (Op1.isNonZero() && Op0.isNonZero())
6050 return true;
6051
6052 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6053 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6054 }
6055 case ISD::SMIN: {
6056 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6057 if (Op1.isNegative())
6058 return true;
6059
6060 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6061 if (Op0.isNegative())
6062 return true;
6063
6064 if (Op1.isNonZero() && Op0.isNonZero())
6065 return true;
6066
6067 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6068 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6069 }
6070 case ISD::UMIN:
6071 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6072 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6073
6074 case ISD::ROTL:
6075 case ISD::ROTR:
6076 case ISD::BITREVERSE:
6077 case ISD::BSWAP:
6078 case ISD::CTPOP:
6079 case ISD::ABS:
6080 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6081
6082 case ISD::SRA:
6083 case ISD::SRL: {
6084 if (Op->getFlags().hasExact())
6085 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6086 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6087 if (ValKnown.isNegative())
6088 return true;
6089 // If max shift cnt of known ones is non-zero, result is non-zero.
6090 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6091 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6092 !ValKnown.One.lshr(MaxCnt).isZero())
6093 return true;
6094 break;
6095 }
6096 case ISD::UDIV:
6097 case ISD::SDIV:
6098 // div exact can only produce a zero if the dividend is zero.
6099 // TODO: For udiv this is also true if Op1 u<= Op0
6100 if (Op->getFlags().hasExact())
6101 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6102 break;
6103
6104 case ISD::ADD:
6105 if (Op->getFlags().hasNoUnsignedWrap())
6106 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6107 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6108 return true;
6109 // TODO: There are a lot more cases we can prove for add.
6110 break;
6111
6112 case ISD::SUB: {
6113 if (isNullConstant(Op.getOperand(0)))
6114 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6115
6116 std::optional<bool> ne =
6117 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6118 computeKnownBits(Op.getOperand(1), Depth + 1));
6119 return ne && *ne;
6120 }
6121
6122 case ISD::MUL:
6123 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6124 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6125 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6126 return true;
6127 break;
6128
6129 case ISD::ZERO_EXTEND:
6130 case ISD::SIGN_EXTEND:
6131 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6132 case ISD::VSCALE: {
6134 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6135 ConstantRange CR =
6136 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6137 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6138 return true;
6139 break;
6140 }
6141 }
6142
6144}
6145
6147 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6148 return !C1->isNegative();
6149
6150 switch (Op.getOpcode()) {
6151 case ISD::FABS:
6152 case ISD::FEXP:
6153 case ISD::FEXP2:
6154 case ISD::FEXP10:
6155 return true;
6156 default:
6157 return false;
6158 }
6159
6160 llvm_unreachable("covered opcode switch");
6161}
6162
6164 assert(Use.getValueType().isFloatingPoint());
6165 const SDNode *User = Use.getUser();
6166 unsigned OperandNo = Use.getOperandNo();
6167 // Check if this use is insensitive to the sign of zero
6168 switch (User->getOpcode()) {
6169 case ISD::SETCC:
6170 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6171 case ISD::FABS:
6172 // fabs always produces +0.0.
6173 return true;
6174 case ISD::FCOPYSIGN:
6175 // copysign overwrites the sign bit of the first operand.
6176 return OperandNo == 0;
6177 case ISD::FADD:
6178 case ISD::FSUB: {
6179 // Arithmetic with non-zero constants fixes the uncertainty around the
6180 // sign bit.
6181 SDValue Other = User->getOperand(1 - OperandNo);
6183 }
6184 case ISD::FP_TO_SINT:
6185 case ISD::FP_TO_UINT:
6186 // fp-to-int conversions normalize signed zeros.
6187 return true;
6188 default:
6189 return false;
6190 }
6191}
6192
6194 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6195 // regression. Ideally, this should be implemented as a demanded-bits
6196 // optimization that stems from the users.
6197 if (Op->use_size() > 2)
6198 return false;
6199 return all_of(Op->uses(),
6200 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6201}
6202
6204 // Check the obvious case.
6205 if (A == B) return true;
6206
6207 // For negative and positive zero.
6210 if (CA->isZero() && CB->isZero()) return true;
6211
6212 // Otherwise they may not be equal.
6213 return false;
6214}
6215
6216// Only bits set in Mask must be negated, other bits may be arbitrary.
6218 if (isBitwiseNot(V, AllowUndefs))
6219 return V.getOperand(0);
6220
6221 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6222 // bits in the non-extended part.
6223 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6224 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6225 return SDValue();
6226 SDValue ExtArg = V.getOperand(0);
6227 if (ExtArg.getScalarValueSizeInBits() >=
6228 MaskC->getAPIntValue().getActiveBits() &&
6229 isBitwiseNot(ExtArg, AllowUndefs) &&
6230 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6231 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6232 return ExtArg.getOperand(0).getOperand(0);
6233 return SDValue();
6234}
6235
6237 // Match masked merge pattern (X & ~M) op (Y & M)
6238 // Including degenerate case (X & ~M) op M
6239 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6240 SDValue Other) {
6241 if (SDValue NotOperand =
6242 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6243 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6244 NotOperand->getOpcode() == ISD::TRUNCATE)
6245 NotOperand = NotOperand->getOperand(0);
6246
6247 if (Other == NotOperand)
6248 return true;
6249 if (Other->getOpcode() == ISD::AND)
6250 return NotOperand == Other->getOperand(0) ||
6251 NotOperand == Other->getOperand(1);
6252 }
6253 return false;
6254 };
6255
6256 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6257 A = A->getOperand(0);
6258
6259 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6260 B = B->getOperand(0);
6261
6262 if (A->getOpcode() == ISD::AND)
6263 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6264 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6265 return false;
6266}
6267
6268// FIXME: unify with llvm::haveNoCommonBitsSet.
6270 assert(A.getValueType() == B.getValueType() &&
6271 "Values must have the same type");
6274 return true;
6277}
6278
6279static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6280 SelectionDAG &DAG) {
6281 if (cast<ConstantSDNode>(Step)->isZero())
6282 return DAG.getConstant(0, DL, VT);
6283
6284 return SDValue();
6285}
6286
6289 SelectionDAG &DAG) {
6290 int NumOps = Ops.size();
6291 assert(NumOps != 0 && "Can't build an empty vector!");
6292 assert(!VT.isScalableVector() &&
6293 "BUILD_VECTOR cannot be used with scalable types");
6294 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6295 "Incorrect element count in BUILD_VECTOR!");
6296
6297 // BUILD_VECTOR of UNDEFs is UNDEF.
6298 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6299 return DAG.getUNDEF(VT);
6300
6301 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6302 SDValue IdentitySrc;
6303 bool IsIdentity = true;
6304 for (int i = 0; i != NumOps; ++i) {
6306 Ops[i].getOperand(0).getValueType() != VT ||
6307 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6308 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6309 Ops[i].getConstantOperandAPInt(1) != i) {
6310 IsIdentity = false;
6311 break;
6312 }
6313 IdentitySrc = Ops[i].getOperand(0);
6314 }
6315 if (IsIdentity)
6316 return IdentitySrc;
6317
6318 return SDValue();
6319}
6320
6321/// Try to simplify vector concatenation to an input value, undef, or build
6322/// vector.
6325 SelectionDAG &DAG) {
6326 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6328 [Ops](SDValue Op) {
6329 return Ops[0].getValueType() == Op.getValueType();
6330 }) &&
6331 "Concatenation of vectors with inconsistent value types!");
6332 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6333 VT.getVectorElementCount() &&
6334 "Incorrect element count in vector concatenation!");
6335
6336 if (Ops.size() == 1)
6337 return Ops[0];
6338
6339 // Concat of UNDEFs is UNDEF.
6340 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6341 return DAG.getUNDEF(VT);
6342
6343 // Scan the operands and look for extract operations from a single source
6344 // that correspond to insertion at the same location via this concatenation:
6345 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6346 SDValue IdentitySrc;
6347 bool IsIdentity = true;
6348 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6349 SDValue Op = Ops[i];
6350 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6351 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6352 Op.getOperand(0).getValueType() != VT ||
6353 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6354 Op.getConstantOperandVal(1) != IdentityIndex) {
6355 IsIdentity = false;
6356 break;
6357 }
6358 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6359 "Unexpected identity source vector for concat of extracts");
6360 IdentitySrc = Op.getOperand(0);
6361 }
6362 if (IsIdentity) {
6363 assert(IdentitySrc && "Failed to set source vector of extracts");
6364 return IdentitySrc;
6365 }
6366
6367 // The code below this point is only designed to work for fixed width
6368 // vectors, so we bail out for now.
6369 if (VT.isScalableVector())
6370 return SDValue();
6371
6372 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6373 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6374 // BUILD_VECTOR.
6375 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6376 EVT SVT = VT.getScalarType();
6378 for (SDValue Op : Ops) {
6379 EVT OpVT = Op.getValueType();
6380 if (Op.isUndef())
6381 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6382 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6383 Elts.append(Op->op_begin(), Op->op_end());
6384 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6385 OpVT.getVectorNumElements() == 1 &&
6386 isNullConstant(Op.getOperand(2)))
6387 Elts.push_back(Op.getOperand(1));
6388 else
6389 return SDValue();
6390 }
6391
6392 // BUILD_VECTOR requires all inputs to be of the same type, find the
6393 // maximum type and extend them all.
6394 for (SDValue Op : Elts)
6395 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6396
6397 if (SVT.bitsGT(VT.getScalarType())) {
6398 for (SDValue &Op : Elts) {
6399 if (Op.isUndef())
6400 Op = DAG.getUNDEF(SVT);
6401 else
6402 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6403 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6404 : DAG.getSExtOrTrunc(Op, DL, SVT);
6405 }
6406 }
6407
6408 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6409 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6410 return V;
6411}
6412
6413/// Gets or creates the specified node.
6414SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6415 SDVTList VTs = getVTList(VT);
6417 AddNodeIDNode(ID, Opcode, VTs, {});
6418 void *IP = nullptr;
6419 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6420 return SDValue(E, 0);
6421
6422 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6423 CSEMap.InsertNode(N, IP);
6424
6425 InsertNode(N);
6426 SDValue V = SDValue(N, 0);
6427 NewSDValueDbgMsg(V, "Creating new node: ", this);
6428 return V;
6429}
6430
6431SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6432 SDValue N1) {
6433 SDNodeFlags Flags;
6434 if (Inserter)
6435 Flags = Inserter->getFlags();
6436 return getNode(Opcode, DL, VT, N1, Flags);
6437}
6438
6439SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6440 SDValue N1, const SDNodeFlags Flags) {
6441 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6442
6443 // Constant fold unary operations with a vector integer or float operand.
6444 switch (Opcode) {
6445 default:
6446 // FIXME: Entirely reasonable to perform folding of other unary
6447 // operations here as the need arises.
6448 break;
6449 case ISD::FNEG:
6450 case ISD::FABS:
6451 case ISD::FCEIL:
6452 case ISD::FTRUNC:
6453 case ISD::FFLOOR:
6454 case ISD::FP_EXTEND:
6455 case ISD::FP_TO_SINT:
6456 case ISD::FP_TO_UINT:
6457 case ISD::FP_TO_FP16:
6458 case ISD::FP_TO_BF16:
6459 case ISD::TRUNCATE:
6460 case ISD::ANY_EXTEND:
6461 case ISD::ZERO_EXTEND:
6462 case ISD::SIGN_EXTEND:
6463 case ISD::UINT_TO_FP:
6464 case ISD::SINT_TO_FP:
6465 case ISD::FP16_TO_FP:
6466 case ISD::BF16_TO_FP:
6467 case ISD::BITCAST:
6468 case ISD::ABS:
6469 case ISD::BITREVERSE:
6470 case ISD::BSWAP:
6471 case ISD::CTLZ:
6473 case ISD::CTTZ:
6475 case ISD::CTPOP:
6476 case ISD::STEP_VECTOR: {
6477 SDValue Ops = {N1};
6478 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6479 return Fold;
6480 }
6481 }
6482
6483 unsigned OpOpcode = N1.getNode()->getOpcode();
6484 switch (Opcode) {
6485 case ISD::STEP_VECTOR:
6486 assert(VT.isScalableVector() &&
6487 "STEP_VECTOR can only be used with scalable types");
6488 assert(OpOpcode == ISD::TargetConstant &&
6489 VT.getVectorElementType() == N1.getValueType() &&
6490 "Unexpected step operand");
6491 break;
6492 case ISD::FREEZE:
6493 assert(VT == N1.getValueType() && "Unexpected VT!");
6494 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6495 return N1;
6496 break;
6497 case ISD::TokenFactor:
6498 case ISD::MERGE_VALUES:
6500 return N1; // Factor, merge or concat of one node? No need.
6501 case ISD::BUILD_VECTOR: {
6502 // Attempt to simplify BUILD_VECTOR.
6503 SDValue Ops[] = {N1};
6504 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6505 return V;
6506 break;
6507 }
6508 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6509 case ISD::FP_EXTEND:
6511 "Invalid FP cast!");
6512 if (N1.getValueType() == VT) return N1; // noop conversion.
6513 assert((!VT.isVector() || VT.getVectorElementCount() ==
6515 "Vector element count mismatch!");
6516 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6517 if (N1.isUndef())
6518 return getUNDEF(VT);
6519 break;
6520 case ISD::FP_TO_SINT:
6521 case ISD::FP_TO_UINT:
6522 if (N1.isUndef())
6523 return getUNDEF(VT);
6524 break;
6525 case ISD::SINT_TO_FP:
6526 case ISD::UINT_TO_FP:
6527 // [us]itofp(undef) = 0, because the result value is bounded.
6528 if (N1.isUndef())
6529 return getConstantFP(0.0, DL, VT);
6530 break;
6531 case ISD::SIGN_EXTEND:
6532 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6533 "Invalid SIGN_EXTEND!");
6534 assert(VT.isVector() == N1.getValueType().isVector() &&
6535 "SIGN_EXTEND result type type should be vector iff the operand "
6536 "type is vector!");
6537 if (N1.getValueType() == VT) return N1; // noop extension
6538 assert((!VT.isVector() || VT.getVectorElementCount() ==
6540 "Vector element count mismatch!");
6541 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6542 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6543 SDNodeFlags Flags;
6544 if (OpOpcode == ISD::ZERO_EXTEND)
6545 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6546 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6547 transferDbgValues(N1, NewVal);
6548 return NewVal;
6549 }
6550
6551 if (OpOpcode == ISD::POISON)
6552 return getPOISON(VT);
6553
6554 if (N1.isUndef())
6555 // sext(undef) = 0, because the top bits will all be the same.
6556 return getConstant(0, DL, VT);
6557
6558 // Skip unnecessary sext_inreg pattern:
6559 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6560 if (OpOpcode == ISD::TRUNCATE) {
6561 SDValue OpOp = N1.getOperand(0);
6562 if (OpOp.getValueType() == VT) {
6563 unsigned NumSignExtBits =
6565 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6566 transferDbgValues(N1, OpOp);
6567 return OpOp;
6568 }
6569 }
6570 }
6571 break;
6572 case ISD::ZERO_EXTEND:
6573 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6574 "Invalid ZERO_EXTEND!");
6575 assert(VT.isVector() == N1.getValueType().isVector() &&
6576 "ZERO_EXTEND result type type should be vector iff the operand "
6577 "type is vector!");
6578 if (N1.getValueType() == VT) return N1; // noop extension
6579 assert((!VT.isVector() || VT.getVectorElementCount() ==
6581 "Vector element count mismatch!");
6582 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6583 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6584 SDNodeFlags Flags;
6585 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6586 SDValue NewVal =
6587 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6588 transferDbgValues(N1, NewVal);
6589 return NewVal;
6590 }
6591
6592 if (OpOpcode == ISD::POISON)
6593 return getPOISON(VT);
6594
6595 if (N1.isUndef())
6596 // zext(undef) = 0, because the top bits will be zero.
6597 return getConstant(0, DL, VT);
6598
6599 // Skip unnecessary zext_inreg pattern:
6600 // (zext (trunc x)) -> x iff the upper bits are known zero.
6601 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6602 // use to recognise zext_inreg patterns.
6603 if (OpOpcode == ISD::TRUNCATE) {
6604 SDValue OpOp = N1.getOperand(0);
6605 if (OpOp.getValueType() == VT) {
6606 if (OpOp.getOpcode() != ISD::AND) {
6609 if (MaskedValueIsZero(OpOp, HiBits)) {
6610 transferDbgValues(N1, OpOp);
6611 return OpOp;
6612 }
6613 }
6614 }
6615 }
6616 break;
6617 case ISD::ANY_EXTEND:
6618 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6619 "Invalid ANY_EXTEND!");
6620 assert(VT.isVector() == N1.getValueType().isVector() &&
6621 "ANY_EXTEND result type type should be vector iff the operand "
6622 "type is vector!");
6623 if (N1.getValueType() == VT) return N1; // noop extension
6624 assert((!VT.isVector() || VT.getVectorElementCount() ==
6626 "Vector element count mismatch!");
6627 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6628
6629 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6630 OpOpcode == ISD::ANY_EXTEND) {
6631 SDNodeFlags Flags;
6632 if (OpOpcode == ISD::ZERO_EXTEND)
6633 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6634 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6635 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6636 }
6637 if (N1.isUndef())
6638 return getUNDEF(VT);
6639
6640 // (ext (trunc x)) -> x
6641 if (OpOpcode == ISD::TRUNCATE) {
6642 SDValue OpOp = N1.getOperand(0);
6643 if (OpOp.getValueType() == VT) {
6644 transferDbgValues(N1, OpOp);
6645 return OpOp;
6646 }
6647 }
6648 break;
6649 case ISD::TRUNCATE:
6650 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6651 "Invalid TRUNCATE!");
6652 assert(VT.isVector() == N1.getValueType().isVector() &&
6653 "TRUNCATE result type type should be vector iff the operand "
6654 "type is vector!");
6655 if (N1.getValueType() == VT) return N1; // noop truncate
6656 assert((!VT.isVector() || VT.getVectorElementCount() ==
6658 "Vector element count mismatch!");
6659 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6660 if (OpOpcode == ISD::TRUNCATE)
6661 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6662 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6663 OpOpcode == ISD::ANY_EXTEND) {
6664 // If the source is smaller than the dest, we still need an extend.
6666 VT.getScalarType())) {
6667 SDNodeFlags Flags;
6668 if (OpOpcode == ISD::ZERO_EXTEND)
6669 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6670 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6671 }
6672 if (N1.getOperand(0).getValueType().bitsGT(VT))
6673 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6674 return N1.getOperand(0);
6675 }
6676 if (N1.isUndef())
6677 return getUNDEF(VT);
6678 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6679 return getVScale(DL, VT,
6681 break;
6685 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6686 assert(N1.getValueType().bitsLE(VT) &&
6687 "The input must be the same size or smaller than the result.");
6690 "The destination vector type must have fewer lanes than the input.");
6691 break;
6692 case ISD::ABS:
6693 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6694 if (N1.isUndef())
6695 return getConstant(0, DL, VT);
6696 break;
6697 case ISD::BSWAP:
6698 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6699 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6700 "BSWAP types must be a multiple of 16 bits!");
6701 if (N1.isUndef())
6702 return getUNDEF(VT);
6703 // bswap(bswap(X)) -> X.
6704 if (OpOpcode == ISD::BSWAP)
6705 return N1.getOperand(0);
6706 break;
6707 case ISD::BITREVERSE:
6708 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6709 if (N1.isUndef())
6710 return getUNDEF(VT);
6711 break;
6712 case ISD::BITCAST:
6714 "Cannot BITCAST between types of different sizes!");
6715 if (VT == N1.getValueType()) return N1; // noop conversion.
6716 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6717 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6718 if (N1.isUndef())
6719 return getUNDEF(VT);
6720 break;
6722 assert(VT.isVector() && !N1.getValueType().isVector() &&
6723 (VT.getVectorElementType() == N1.getValueType() ||
6725 N1.getValueType().isInteger() &&
6727 "Illegal SCALAR_TO_VECTOR node!");
6728 if (N1.isUndef())
6729 return getUNDEF(VT);
6730 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6731 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6733 N1.getConstantOperandVal(1) == 0 &&
6734 N1.getOperand(0).getValueType() == VT)
6735 return N1.getOperand(0);
6736 break;
6737 case ISD::FNEG:
6738 // Negation of an unknown bag of bits is still completely undefined.
6739 if (N1.isUndef())
6740 return getUNDEF(VT);
6741
6742 if (OpOpcode == ISD::FNEG) // --X -> X
6743 return N1.getOperand(0);
6744 break;
6745 case ISD::FABS:
6746 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6747 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6748 break;
6749 case ISD::VSCALE:
6750 assert(VT == N1.getValueType() && "Unexpected VT!");
6751 break;
6752 case ISD::CTPOP:
6753 if (N1.getValueType().getScalarType() == MVT::i1)
6754 return N1;
6755 break;
6756 case ISD::CTLZ:
6757 case ISD::CTTZ:
6758 if (N1.getValueType().getScalarType() == MVT::i1)
6759 return getNOT(DL, N1, N1.getValueType());
6760 break;
6761 case ISD::VECREDUCE_ADD:
6762 if (N1.getValueType().getScalarType() == MVT::i1)
6763 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6764 break;
6765 case ISD::VECREDUCE_SMIN:
6766 case ISD::VECREDUCE_UMAX:
6767 if (N1.getValueType().getScalarType() == MVT::i1)
6768 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6769 break;
6770 case ISD::VECREDUCE_SMAX:
6771 case ISD::VECREDUCE_UMIN:
6772 if (N1.getValueType().getScalarType() == MVT::i1)
6773 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6774 break;
6775 case ISD::SPLAT_VECTOR:
6776 assert(VT.isVector() && "Wrong return type!");
6777 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6778 // that for now.
6780 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6782 N1.getValueType().isInteger() &&
6784 "Wrong operand type!");
6785 break;
6786 }
6787
6788 SDNode *N;
6789 SDVTList VTs = getVTList(VT);
6790 SDValue Ops[] = {N1};
6791 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6793 AddNodeIDNode(ID, Opcode, VTs, Ops);
6794 void *IP = nullptr;
6795 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6796 E->intersectFlagsWith(Flags);
6797 return SDValue(E, 0);
6798 }
6799
6800 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6801 N->setFlags(Flags);
6802 createOperands(N, Ops);
6803 CSEMap.InsertNode(N, IP);
6804 } else {
6805 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6806 createOperands(N, Ops);
6807 }
6808
6809 InsertNode(N);
6810 SDValue V = SDValue(N, 0);
6811 NewSDValueDbgMsg(V, "Creating new node: ", this);
6812 return V;
6813}
6814
6815static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6816 const APInt &C2) {
6817 switch (Opcode) {
6818 case ISD::ADD: return C1 + C2;
6819 case ISD::SUB: return C1 - C2;
6820 case ISD::MUL: return C1 * C2;
6821 case ISD::AND: return C1 & C2;
6822 case ISD::OR: return C1 | C2;
6823 case ISD::XOR: return C1 ^ C2;
6824 case ISD::SHL: return C1 << C2;
6825 case ISD::SRL: return C1.lshr(C2);
6826 case ISD::SRA: return C1.ashr(C2);
6827 case ISD::ROTL: return C1.rotl(C2);
6828 case ISD::ROTR: return C1.rotr(C2);
6829 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6830 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6831 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6832 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6833 case ISD::SADDSAT: return C1.sadd_sat(C2);
6834 case ISD::UADDSAT: return C1.uadd_sat(C2);
6835 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6836 case ISD::USUBSAT: return C1.usub_sat(C2);
6837 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6838 case ISD::USHLSAT: return C1.ushl_sat(C2);
6839 case ISD::UDIV:
6840 if (!C2.getBoolValue())
6841 break;
6842 return C1.udiv(C2);
6843 case ISD::UREM:
6844 if (!C2.getBoolValue())
6845 break;
6846 return C1.urem(C2);
6847 case ISD::SDIV:
6848 if (!C2.getBoolValue())
6849 break;
6850 return C1.sdiv(C2);
6851 case ISD::SREM:
6852 if (!C2.getBoolValue())
6853 break;
6854 return C1.srem(C2);
6855 case ISD::AVGFLOORS:
6856 return APIntOps::avgFloorS(C1, C2);
6857 case ISD::AVGFLOORU:
6858 return APIntOps::avgFloorU(C1, C2);
6859 case ISD::AVGCEILS:
6860 return APIntOps::avgCeilS(C1, C2);
6861 case ISD::AVGCEILU:
6862 return APIntOps::avgCeilU(C1, C2);
6863 case ISD::ABDS:
6864 return APIntOps::abds(C1, C2);
6865 case ISD::ABDU:
6866 return APIntOps::abdu(C1, C2);
6867 case ISD::MULHS:
6868 return APIntOps::mulhs(C1, C2);
6869 case ISD::MULHU:
6870 return APIntOps::mulhu(C1, C2);
6871 }
6872 return std::nullopt;
6873}
6874// Handle constant folding with UNDEF.
6875// TODO: Handle more cases.
6876static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6877 bool IsUndef1, const APInt &C2,
6878 bool IsUndef2) {
6879 if (!(IsUndef1 || IsUndef2))
6880 return FoldValue(Opcode, C1, C2);
6881
6882 // Fold and(x, undef) -> 0
6883 // Fold mul(x, undef) -> 0
6884 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6885 return APInt::getZero(C1.getBitWidth());
6886
6887 return std::nullopt;
6888}
6889
6891 const GlobalAddressSDNode *GA,
6892 const SDNode *N2) {
6893 if (GA->getOpcode() != ISD::GlobalAddress)
6894 return SDValue();
6895 if (!TLI->isOffsetFoldingLegal(GA))
6896 return SDValue();
6897 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6898 if (!C2)
6899 return SDValue();
6900 int64_t Offset = C2->getSExtValue();
6901 switch (Opcode) {
6902 case ISD::ADD:
6903 case ISD::PTRADD:
6904 break;
6905 case ISD::SUB: Offset = -uint64_t(Offset); break;
6906 default: return SDValue();
6907 }
6908 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6909 GA->getOffset() + uint64_t(Offset));
6910}
6911
6913 switch (Opcode) {
6914 case ISD::SDIV:
6915 case ISD::UDIV:
6916 case ISD::SREM:
6917 case ISD::UREM: {
6918 // If a divisor is zero/undef or any element of a divisor vector is
6919 // zero/undef, the whole op is undef.
6920 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6921 SDValue Divisor = Ops[1];
6922 if (Divisor.isUndef() || isNullConstant(Divisor))
6923 return true;
6924
6925 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6926 llvm::any_of(Divisor->op_values(),
6927 [](SDValue V) { return V.isUndef() ||
6928 isNullConstant(V); });
6929 // TODO: Handle signed overflow.
6930 }
6931 // TODO: Handle oversized shifts.
6932 default:
6933 return false;
6934 }
6935}
6936
6939 SDNodeFlags Flags) {
6940 // If the opcode is a target-specific ISD node, there's nothing we can
6941 // do here and the operand rules may not line up with the below, so
6942 // bail early.
6943 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6944 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6945 // foldCONCAT_VECTORS in getNode before this is called.
6946 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6947 return SDValue();
6948
6949 unsigned NumOps = Ops.size();
6950 if (NumOps == 0)
6951 return SDValue();
6952
6953 if (isUndef(Opcode, Ops))
6954 return getUNDEF(VT);
6955
6956 // Handle unary special cases.
6957 if (NumOps == 1) {
6958 SDValue N1 = Ops[0];
6959
6960 // Constant fold unary operations with an integer constant operand. Even
6961 // opaque constant will be folded, because the folding of unary operations
6962 // doesn't create new constants with different values. Nevertheless, the
6963 // opaque flag is preserved during folding to prevent future folding with
6964 // other constants.
6965 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6966 const APInt &Val = C->getAPIntValue();
6967 switch (Opcode) {
6968 case ISD::SIGN_EXTEND:
6969 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6970 C->isTargetOpcode(), C->isOpaque());
6971 case ISD::TRUNCATE:
6972 if (C->isOpaque())
6973 break;
6974 [[fallthrough]];
6975 case ISD::ZERO_EXTEND:
6976 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6977 C->isTargetOpcode(), C->isOpaque());
6978 case ISD::ANY_EXTEND:
6979 // Some targets like RISCV prefer to sign extend some types.
6980 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6981 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6982 C->isTargetOpcode(), C->isOpaque());
6983 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6984 C->isTargetOpcode(), C->isOpaque());
6985 case ISD::ABS:
6986 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6987 C->isOpaque());
6988 case ISD::BITREVERSE:
6989 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6990 C->isOpaque());
6991 case ISD::BSWAP:
6992 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6993 C->isOpaque());
6994 case ISD::CTPOP:
6995 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6996 C->isOpaque());
6997 case ISD::CTLZ:
6999 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7000 C->isOpaque());
7001 case ISD::CTTZ:
7003 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7004 C->isOpaque());
7005 case ISD::UINT_TO_FP:
7006 case ISD::SINT_TO_FP: {
7008 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7010 return getConstantFP(FPV, DL, VT);
7011 }
7012 case ISD::FP16_TO_FP:
7013 case ISD::BF16_TO_FP: {
7014 bool Ignored;
7015 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7016 : APFloat::BFloat(),
7017 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7018
7019 // This can return overflow, underflow, or inexact; we don't care.
7020 // FIXME need to be more flexible about rounding mode.
7022 &Ignored);
7023 return getConstantFP(FPV, DL, VT);
7024 }
7025 case ISD::STEP_VECTOR:
7026 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7027 return V;
7028 break;
7029 case ISD::BITCAST:
7030 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7031 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7032 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7033 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7034 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7035 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7036 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7037 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7038 break;
7039 }
7040 }
7041
7042 // Constant fold unary operations with a floating point constant operand.
7043 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7044 APFloat V = C->getValueAPF(); // make copy
7045 switch (Opcode) {
7046 case ISD::FNEG:
7047 V.changeSign();
7048 return getConstantFP(V, DL, VT);
7049 case ISD::FABS:
7050 V.clearSign();
7051 return getConstantFP(V, DL, VT);
7052 case ISD::FCEIL: {
7053 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7055 return getConstantFP(V, DL, VT);
7056 return SDValue();
7057 }
7058 case ISD::FTRUNC: {
7059 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7061 return getConstantFP(V, DL, VT);
7062 return SDValue();
7063 }
7064 case ISD::FFLOOR: {
7065 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7067 return getConstantFP(V, DL, VT);
7068 return SDValue();
7069 }
7070 case ISD::FP_EXTEND: {
7071 bool ignored;
7072 // This can return overflow, underflow, or inexact; we don't care.
7073 // FIXME need to be more flexible about rounding mode.
7074 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7075 &ignored);
7076 return getConstantFP(V, DL, VT);
7077 }
7078 case ISD::FP_TO_SINT:
7079 case ISD::FP_TO_UINT: {
7080 bool ignored;
7081 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7082 // FIXME need to be more flexible about rounding mode.
7084 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7085 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7086 break;
7087 return getConstant(IntVal, DL, VT);
7088 }
7089 case ISD::FP_TO_FP16:
7090 case ISD::FP_TO_BF16: {
7091 bool Ignored;
7092 // This can return overflow, underflow, or inexact; we don't care.
7093 // FIXME need to be more flexible about rounding mode.
7094 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7095 : APFloat::BFloat(),
7097 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7098 }
7099 case ISD::BITCAST:
7100 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7101 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7102 VT);
7103 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7104 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7105 VT);
7106 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7107 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7108 VT);
7109 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7110 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7111 break;
7112 }
7113 }
7114
7115 // Early-out if we failed to constant fold a bitcast.
7116 if (Opcode == ISD::BITCAST)
7117 return SDValue();
7118 }
7119
7120 // Handle binops special cases.
7121 if (NumOps == 2) {
7122 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7123 return CFP;
7124
7125 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7126 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7127 if (C1->isOpaque() || C2->isOpaque())
7128 return SDValue();
7129
7130 std::optional<APInt> FoldAttempt =
7131 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7132 if (!FoldAttempt)
7133 return SDValue();
7134
7135 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7136 assert((!Folded || !VT.isVector()) &&
7137 "Can't fold vectors ops with scalar operands");
7138 return Folded;
7139 }
7140 }
7141
7142 // fold (add Sym, c) -> Sym+c
7144 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7145 if (TLI->isCommutativeBinOp(Opcode))
7147 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7148
7149 // fold (sext_in_reg c1) -> c2
7150 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7151 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7152
7153 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7154 unsigned FromBits = EVT.getScalarSizeInBits();
7155 Val <<= Val.getBitWidth() - FromBits;
7156 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7157 return getConstant(Val, DL, ConstantVT);
7158 };
7159
7160 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7161 const APInt &Val = C1->getAPIntValue();
7162 return SignExtendInReg(Val, VT);
7163 }
7164
7166 SmallVector<SDValue, 8> ScalarOps;
7167 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7168 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7169 SDValue Op = Ops[0].getOperand(I);
7170 if (Op.isUndef()) {
7171 ScalarOps.push_back(getUNDEF(OpVT));
7172 continue;
7173 }
7174 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7175 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7176 }
7177 return getBuildVector(VT, DL, ScalarOps);
7178 }
7179
7180 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7181 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7182 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7183 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7184 Ops[0].getOperand(0).getValueType()));
7185 }
7186 }
7187
7188 // Handle fshl/fshr special cases.
7189 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7190 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7191 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7192 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7193
7194 if (C1 && C2 && C3) {
7195 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7196 return SDValue();
7197 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7198 &V3 = C3->getAPIntValue();
7199
7200 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7201 : APIntOps::fshr(V1, V2, V3);
7202 return getConstant(FoldedVal, DL, VT);
7203 }
7204 }
7205
7206 // Handle fma/fmad special cases.
7207 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7208 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7209 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7210 Ops[2].getValueType() == VT && "FMA types must match!");
7214 if (C1 && C2 && C3) {
7215 APFloat V1 = C1->getValueAPF();
7216 const APFloat &V2 = C2->getValueAPF();
7217 const APFloat &V3 = C3->getValueAPF();
7218 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7221 } else
7223 return getConstantFP(V1, DL, VT);
7224 }
7225 }
7226
7227 // This is for vector folding only from here on.
7228 if (!VT.isVector())
7229 return SDValue();
7230
7231 ElementCount NumElts = VT.getVectorElementCount();
7232
7233 // See if we can fold through any bitcasted integer ops.
7234 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7235 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7236 (Ops[0].getOpcode() == ISD::BITCAST ||
7237 Ops[1].getOpcode() == ISD::BITCAST)) {
7240 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7241 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7242 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7243 N2.getValueType().isInteger()) {
7244 bool IsLE = getDataLayout().isLittleEndian();
7245 unsigned EltBits = VT.getScalarSizeInBits();
7246 SmallVector<APInt> RawBits1, RawBits2;
7247 BitVector UndefElts1, UndefElts2;
7248 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7249 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7250 SmallVector<APInt> RawBits;
7251 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7252 std::optional<APInt> Fold = FoldValueWithUndef(
7253 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7254 if (!Fold)
7255 break;
7256 RawBits.push_back(*Fold);
7257 }
7258 if (RawBits.size() == NumElts.getFixedValue()) {
7259 // We have constant folded, but we might need to cast this again back
7260 // to the original (possibly legalized) type.
7261 EVT BVVT, BVEltVT;
7262 if (N1.getValueType() == VT) {
7263 BVVT = N1.getValueType();
7264 BVEltVT = BV1->getOperand(0).getValueType();
7265 } else {
7266 BVVT = N2.getValueType();
7267 BVEltVT = BV2->getOperand(0).getValueType();
7268 }
7269 unsigned BVEltBits = BVEltVT.getSizeInBits();
7270 SmallVector<APInt> DstBits;
7271 BitVector DstUndefs;
7273 DstBits, RawBits, DstUndefs,
7274 BitVector(RawBits.size(), false));
7275 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7276 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7277 if (DstUndefs[I])
7278 continue;
7279 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7280 }
7281 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7282 }
7283 }
7284 }
7285 }
7286
7287 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7288 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7289 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7290 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7291 APInt RHSVal;
7292 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7293 APInt NewStep = Opcode == ISD::MUL
7294 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7295 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7296 return getStepVector(DL, VT, NewStep);
7297 }
7298 }
7299
7300 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7301 return !Op.getValueType().isVector() ||
7302 Op.getValueType().getVectorElementCount() == NumElts;
7303 };
7304
7305 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7306 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7307 Op.getOpcode() == ISD::BUILD_VECTOR ||
7308 Op.getOpcode() == ISD::SPLAT_VECTOR;
7309 };
7310
7311 // All operands must be vector types with the same number of elements as
7312 // the result type and must be either UNDEF or a build/splat vector
7313 // or UNDEF scalars.
7314 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7315 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7316 return SDValue();
7317
7318 // If we are comparing vectors, then the result needs to be a i1 boolean that
7319 // is then extended back to the legal result type depending on how booleans
7320 // are represented.
7321 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7322 ISD::NodeType ExtendCode =
7323 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7324 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7326
7327 // Find legal integer scalar type for constant promotion and
7328 // ensure that its scalar size is at least as large as source.
7329 EVT LegalSVT = VT.getScalarType();
7330 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7331 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7332 if (LegalSVT.bitsLT(VT.getScalarType()))
7333 return SDValue();
7334 }
7335
7336 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7337 // only have one operand to check. For fixed-length vector types we may have
7338 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7339 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7340
7341 // Constant fold each scalar lane separately.
7342 SmallVector<SDValue, 4> ScalarResults;
7343 for (unsigned I = 0; I != NumVectorElts; I++) {
7344 SmallVector<SDValue, 4> ScalarOps;
7345 for (SDValue Op : Ops) {
7346 EVT InSVT = Op.getValueType().getScalarType();
7347 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7348 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7349 if (Op.isUndef())
7350 ScalarOps.push_back(getUNDEF(InSVT));
7351 else
7352 ScalarOps.push_back(Op);
7353 continue;
7354 }
7355
7356 SDValue ScalarOp =
7357 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7358 EVT ScalarVT = ScalarOp.getValueType();
7359
7360 // Build vector (integer) scalar operands may need implicit
7361 // truncation - do this before constant folding.
7362 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7363 // Don't create illegally-typed nodes unless they're constants or undef
7364 // - if we fail to constant fold we can't guarantee the (dead) nodes
7365 // we're creating will be cleaned up before being visited for
7366 // legalization.
7367 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7368 !isa<ConstantSDNode>(ScalarOp) &&
7369 TLI->getTypeAction(*getContext(), InSVT) !=
7371 return SDValue();
7372 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7373 }
7374
7375 ScalarOps.push_back(ScalarOp);
7376 }
7377
7378 // Constant fold the scalar operands.
7379 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7380
7381 // Scalar folding only succeeded if the result is a constant or UNDEF.
7382 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7383 ScalarResult.getOpcode() != ISD::ConstantFP)
7384 return SDValue();
7385
7386 // Legalize the (integer) scalar constant if necessary. We only do
7387 // this once we know the folding succeeded, since otherwise we would
7388 // get a node with illegal type which has a user.
7389 if (LegalSVT != SVT)
7390 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7391
7392 ScalarResults.push_back(ScalarResult);
7393 }
7394
7395 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7396 : getBuildVector(VT, DL, ScalarResults);
7397 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7398 return V;
7399}
7400
7403 // TODO: Add support for unary/ternary fp opcodes.
7404 if (Ops.size() != 2)
7405 return SDValue();
7406
7407 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7408 // should. That will require dealing with a potentially non-default
7409 // rounding mode, checking the "opStatus" return value from the APFloat
7410 // math calculations, and possibly other variations.
7411 SDValue N1 = Ops[0];
7412 SDValue N2 = Ops[1];
7413 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7414 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7415 if (N1CFP && N2CFP) {
7416 APFloat C1 = N1CFP->getValueAPF(); // make copy
7417 const APFloat &C2 = N2CFP->getValueAPF();
7418 switch (Opcode) {
7419 case ISD::FADD:
7421 return getConstantFP(C1, DL, VT);
7422 case ISD::FSUB:
7424 return getConstantFP(C1, DL, VT);
7425 case ISD::FMUL:
7427 return getConstantFP(C1, DL, VT);
7428 case ISD::FDIV:
7430 return getConstantFP(C1, DL, VT);
7431 case ISD::FREM:
7432 C1.mod(C2);
7433 return getConstantFP(C1, DL, VT);
7434 case ISD::FCOPYSIGN:
7435 C1.copySign(C2);
7436 return getConstantFP(C1, DL, VT);
7437 case ISD::FMINNUM:
7438 if (C1.isSignaling() || C2.isSignaling())
7439 return SDValue();
7440 return getConstantFP(minnum(C1, C2), DL, VT);
7441 case ISD::FMAXNUM:
7442 if (C1.isSignaling() || C2.isSignaling())
7443 return SDValue();
7444 return getConstantFP(maxnum(C1, C2), DL, VT);
7445 case ISD::FMINIMUM:
7446 return getConstantFP(minimum(C1, C2), DL, VT);
7447 case ISD::FMAXIMUM:
7448 return getConstantFP(maximum(C1, C2), DL, VT);
7449 case ISD::FMINIMUMNUM:
7450 return getConstantFP(minimumnum(C1, C2), DL, VT);
7451 case ISD::FMAXIMUMNUM:
7452 return getConstantFP(maximumnum(C1, C2), DL, VT);
7453 default: break;
7454 }
7455 }
7456 if (N1CFP && Opcode == ISD::FP_ROUND) {
7457 APFloat C1 = N1CFP->getValueAPF(); // make copy
7458 bool Unused;
7459 // This can return overflow, underflow, or inexact; we don't care.
7460 // FIXME need to be more flexible about rounding mode.
7462 &Unused);
7463 return getConstantFP(C1, DL, VT);
7464 }
7465
7466 switch (Opcode) {
7467 case ISD::FSUB:
7468 // -0.0 - undef --> undef (consistent with "fneg undef")
7469 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7470 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7471 return getUNDEF(VT);
7472 [[fallthrough]];
7473
7474 case ISD::FADD:
7475 case ISD::FMUL:
7476 case ISD::FDIV:
7477 case ISD::FREM:
7478 // If both operands are undef, the result is undef. If 1 operand is undef,
7479 // the result is NaN. This should match the behavior of the IR optimizer.
7480 if (N1.isUndef() && N2.isUndef())
7481 return getUNDEF(VT);
7482 if (N1.isUndef() || N2.isUndef())
7484 }
7485 return SDValue();
7486}
7487
7489 const SDLoc &DL, EVT DstEltVT) {
7490 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7491
7492 // If this is already the right type, we're done.
7493 if (SrcEltVT == DstEltVT)
7494 return SDValue(BV, 0);
7495
7496 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7497 unsigned DstBitSize = DstEltVT.getSizeInBits();
7498
7499 // If this is a conversion of N elements of one type to N elements of another
7500 // type, convert each element. This handles FP<->INT cases.
7501 if (SrcBitSize == DstBitSize) {
7503 for (SDValue Op : BV->op_values()) {
7504 // If the vector element type is not legal, the BUILD_VECTOR operands
7505 // are promoted and implicitly truncated. Make that explicit here.
7506 if (Op.getValueType() != SrcEltVT)
7507 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7508 Ops.push_back(getBitcast(DstEltVT, Op));
7509 }
7510 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7512 return getBuildVector(VT, DL, Ops);
7513 }
7514
7515 // Otherwise, we're growing or shrinking the elements. To avoid having to
7516 // handle annoying details of growing/shrinking FP values, we convert them to
7517 // int first.
7518 if (SrcEltVT.isFloatingPoint()) {
7519 // Convert the input float vector to a int vector where the elements are the
7520 // same sizes.
7521 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7522 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7524 DstEltVT);
7525 return SDValue();
7526 }
7527
7528 // Now we know the input is an integer vector. If the output is a FP type,
7529 // convert to integer first, then to FP of the right size.
7530 if (DstEltVT.isFloatingPoint()) {
7531 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7532 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7534 DstEltVT);
7535 return SDValue();
7536 }
7537
7538 // Okay, we know the src/dst types are both integers of differing types.
7539 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7540
7541 // Extract the constant raw bit data.
7542 BitVector UndefElements;
7543 SmallVector<APInt> RawBits;
7544 bool IsLE = getDataLayout().isLittleEndian();
7545 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7546 return SDValue();
7547
7549 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7550 if (UndefElements[I])
7551 Ops.push_back(getUNDEF(DstEltVT));
7552 else
7553 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7554 }
7555
7556 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7557 return getBuildVector(VT, DL, Ops);
7558}
7559
7561 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7562
7563 // There's no need to assert on a byte-aligned pointer. All pointers are at
7564 // least byte aligned.
7565 if (A == Align(1))
7566 return Val;
7567
7568 SDVTList VTs = getVTList(Val.getValueType());
7570 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7571 ID.AddInteger(A.value());
7572
7573 void *IP = nullptr;
7574 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7575 return SDValue(E, 0);
7576
7577 auto *N =
7578 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7579 createOperands(N, {Val});
7580
7581 CSEMap.InsertNode(N, IP);
7582 InsertNode(N);
7583
7584 SDValue V(N, 0);
7585 NewSDValueDbgMsg(V, "Creating new node: ", this);
7586 return V;
7587}
7588
7589SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7590 SDValue N1, SDValue N2) {
7591 SDNodeFlags Flags;
7592 if (Inserter)
7593 Flags = Inserter->getFlags();
7594 return getNode(Opcode, DL, VT, N1, N2, Flags);
7595}
7596
7598 SDValue &N2) const {
7599 if (!TLI->isCommutativeBinOp(Opcode))
7600 return;
7601
7602 // Canonicalize:
7603 // binop(const, nonconst) -> binop(nonconst, const)
7606 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7607 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7608 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7609 std::swap(N1, N2);
7610
7611 // Canonicalize:
7612 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7613 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7615 std::swap(N1, N2);
7616}
7617
7618SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7619 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7621 N2.getOpcode() != ISD::DELETED_NODE &&
7622 "Operand is DELETED_NODE!");
7623
7624 canonicalizeCommutativeBinop(Opcode, N1, N2);
7625
7626 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7627 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7628
7629 // Don't allow undefs in vector splats - we might be returning N2 when folding
7630 // to zero etc.
7631 ConstantSDNode *N2CV =
7632 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7633
7634 switch (Opcode) {
7635 default: break;
7636 case ISD::TokenFactor:
7637 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7638 N2.getValueType() == MVT::Other && "Invalid token factor!");
7639 // Fold trivial token factors.
7640 if (N1.getOpcode() == ISD::EntryToken) return N2;
7641 if (N2.getOpcode() == ISD::EntryToken) return N1;
7642 if (N1 == N2) return N1;
7643 break;
7644 case ISD::BUILD_VECTOR: {
7645 // Attempt to simplify BUILD_VECTOR.
7646 SDValue Ops[] = {N1, N2};
7647 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7648 return V;
7649 break;
7650 }
7651 case ISD::CONCAT_VECTORS: {
7652 SDValue Ops[] = {N1, N2};
7653 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7654 return V;
7655 break;
7656 }
7657 case ISD::AND:
7658 assert(VT.isInteger() && "This operator does not apply to FP types!");
7659 assert(N1.getValueType() == N2.getValueType() &&
7660 N1.getValueType() == VT && "Binary operator types must match!");
7661 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7662 // worth handling here.
7663 if (N2CV && N2CV->isZero())
7664 return N2;
7665 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7666 return N1;
7667 break;
7668 case ISD::OR:
7669 case ISD::XOR:
7670 case ISD::ADD:
7671 case ISD::PTRADD:
7672 case ISD::SUB:
7673 assert(VT.isInteger() && "This operator does not apply to FP types!");
7674 assert(N1.getValueType() == N2.getValueType() &&
7675 N1.getValueType() == VT && "Binary operator types must match!");
7676 // The equal operand types requirement is unnecessarily strong for PTRADD.
7677 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7678 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7679 // logic everywhere where PTRADDs may be folded or combined to properly
7680 // support them. If/when we introduce pointer types to the SDAG, we will
7681 // need to relax this constraint.
7682
7683 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7684 // it's worth handling here.
7685 if (N2CV && N2CV->isZero())
7686 return N1;
7687 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7688 VT.getScalarType() == MVT::i1)
7689 return getNode(ISD::XOR, DL, VT, N1, N2);
7690 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7691 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7692 N2.getOpcode() == ISD::VSCALE) {
7693 const APInt &C1 = N1->getConstantOperandAPInt(0);
7694 const APInt &C2 = N2->getConstantOperandAPInt(0);
7695 return getVScale(DL, VT, C1 + C2);
7696 }
7697 break;
7698 case ISD::MUL:
7699 assert(VT.isInteger() && "This operator does not apply to FP types!");
7700 assert(N1.getValueType() == N2.getValueType() &&
7701 N1.getValueType() == VT && "Binary operator types must match!");
7702 if (VT.getScalarType() == MVT::i1)
7703 return getNode(ISD::AND, DL, VT, N1, N2);
7704 if (N2CV && N2CV->isZero())
7705 return N2;
7706 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7707 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7708 const APInt &N2CImm = N2C->getAPIntValue();
7709 return getVScale(DL, VT, MulImm * N2CImm);
7710 }
7711 break;
7712 case ISD::UDIV:
7713 case ISD::UREM:
7714 case ISD::MULHU:
7715 case ISD::MULHS:
7716 case ISD::SDIV:
7717 case ISD::SREM:
7718 case ISD::SADDSAT:
7719 case ISD::SSUBSAT:
7720 case ISD::UADDSAT:
7721 case ISD::USUBSAT:
7722 assert(VT.isInteger() && "This operator does not apply to FP types!");
7723 assert(N1.getValueType() == N2.getValueType() &&
7724 N1.getValueType() == VT && "Binary operator types must match!");
7725 if (VT.getScalarType() == MVT::i1) {
7726 // fold (add_sat x, y) -> (or x, y) for bool types.
7727 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7728 return getNode(ISD::OR, DL, VT, N1, N2);
7729 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7730 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7731 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7732 }
7733 break;
7734 case ISD::SCMP:
7735 case ISD::UCMP:
7736 assert(N1.getValueType() == N2.getValueType() &&
7737 "Types of operands of UCMP/SCMP must match");
7738 assert(N1.getValueType().isVector() == VT.isVector() &&
7739 "Operands and return type of must both be scalars or vectors");
7740 if (VT.isVector())
7743 "Result and operands must have the same number of elements");
7744 break;
7745 case ISD::AVGFLOORS:
7746 case ISD::AVGFLOORU:
7747 case ISD::AVGCEILS:
7748 case ISD::AVGCEILU:
7749 assert(VT.isInteger() && "This operator does not apply to FP types!");
7750 assert(N1.getValueType() == N2.getValueType() &&
7751 N1.getValueType() == VT && "Binary operator types must match!");
7752 break;
7753 case ISD::ABDS:
7754 case ISD::ABDU:
7755 assert(VT.isInteger() && "This operator does not apply to FP types!");
7756 assert(N1.getValueType() == N2.getValueType() &&
7757 N1.getValueType() == VT && "Binary operator types must match!");
7758 if (VT.getScalarType() == MVT::i1)
7759 return getNode(ISD::XOR, DL, VT, N1, N2);
7760 break;
7761 case ISD::SMIN:
7762 case ISD::UMAX:
7763 assert(VT.isInteger() && "This operator does not apply to FP types!");
7764 assert(N1.getValueType() == N2.getValueType() &&
7765 N1.getValueType() == VT && "Binary operator types must match!");
7766 if (VT.getScalarType() == MVT::i1)
7767 return getNode(ISD::OR, DL, VT, N1, N2);
7768 break;
7769 case ISD::SMAX:
7770 case ISD::UMIN:
7771 assert(VT.isInteger() && "This operator does not apply to FP types!");
7772 assert(N1.getValueType() == N2.getValueType() &&
7773 N1.getValueType() == VT && "Binary operator types must match!");
7774 if (VT.getScalarType() == MVT::i1)
7775 return getNode(ISD::AND, DL, VT, N1, N2);
7776 break;
7777 case ISD::FADD:
7778 case ISD::FSUB:
7779 case ISD::FMUL:
7780 case ISD::FDIV:
7781 case ISD::FREM:
7782 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7783 assert(N1.getValueType() == N2.getValueType() &&
7784 N1.getValueType() == VT && "Binary operator types must match!");
7785 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7786 return V;
7787 break;
7788 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7789 assert(N1.getValueType() == VT &&
7792 "Invalid FCOPYSIGN!");
7793 break;
7794 case ISD::SHL:
7795 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7796 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7797 const APInt &ShiftImm = N2C->getAPIntValue();
7798 return getVScale(DL, VT, MulImm << ShiftImm);
7799 }
7800 [[fallthrough]];
7801 case ISD::SRA:
7802 case ISD::SRL:
7803 if (SDValue V = simplifyShift(N1, N2))
7804 return V;
7805 [[fallthrough]];
7806 case ISD::ROTL:
7807 case ISD::ROTR:
7808 assert(VT == N1.getValueType() &&
7809 "Shift operators return type must be the same as their first arg");
7810 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7811 "Shifts only work on integers");
7812 assert((!VT.isVector() || VT == N2.getValueType()) &&
7813 "Vector shift amounts must be in the same as their first arg");
7814 // Verify that the shift amount VT is big enough to hold valid shift
7815 // amounts. This catches things like trying to shift an i1024 value by an
7816 // i8, which is easy to fall into in generic code that uses
7817 // TLI.getShiftAmount().
7820 "Invalid use of small shift amount with oversized value!");
7821
7822 // Always fold shifts of i1 values so the code generator doesn't need to
7823 // handle them. Since we know the size of the shift has to be less than the
7824 // size of the value, the shift/rotate count is guaranteed to be zero.
7825 if (VT == MVT::i1)
7826 return N1;
7827 if (N2CV && N2CV->isZero())
7828 return N1;
7829 break;
7830 case ISD::FP_ROUND:
7832 VT.bitsLE(N1.getValueType()) && N2C &&
7833 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7834 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7835 if (N1.getValueType() == VT) return N1; // noop conversion.
7836 break;
7837 case ISD::AssertNoFPClass: {
7839 "AssertNoFPClass is used for a non-floating type");
7840 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7841 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7842 assert(llvm::to_underlying(NoFPClass) <=
7844 "FPClassTest value too large");
7845 (void)NoFPClass;
7846 break;
7847 }
7848 case ISD::AssertSext:
7849 case ISD::AssertZext: {
7850 EVT EVT = cast<VTSDNode>(N2)->getVT();
7851 assert(VT == N1.getValueType() && "Not an inreg extend!");
7852 assert(VT.isInteger() && EVT.isInteger() &&
7853 "Cannot *_EXTEND_INREG FP types");
7854 assert(!EVT.isVector() &&
7855 "AssertSExt/AssertZExt type should be the vector element type "
7856 "rather than the vector type!");
7857 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7858 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7859 break;
7860 }
7862 EVT EVT = cast<VTSDNode>(N2)->getVT();
7863 assert(VT == N1.getValueType() && "Not an inreg extend!");
7864 assert(VT.isInteger() && EVT.isInteger() &&
7865 "Cannot *_EXTEND_INREG FP types");
7866 assert(EVT.isVector() == VT.isVector() &&
7867 "SIGN_EXTEND_INREG type should be vector iff the operand "
7868 "type is vector!");
7869 assert((!EVT.isVector() ||
7871 "Vector element counts must match in SIGN_EXTEND_INREG");
7872 assert(EVT.bitsLE(VT) && "Not extending!");
7873 if (EVT == VT) return N1; // Not actually extending
7874 break;
7875 }
7877 case ISD::FP_TO_UINT_SAT: {
7878 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7879 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7880 assert(N1.getValueType().isVector() == VT.isVector() &&
7881 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7882 "vector!");
7883 assert((!VT.isVector() || VT.getVectorElementCount() ==
7885 "Vector element counts must match in FP_TO_*INT_SAT");
7886 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7887 "Type to saturate to must be a scalar.");
7888 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7889 "Not extending!");
7890 break;
7891 }
7894 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7895 element type of the vector.");
7896
7897 // Extract from an undefined value or using an undefined index is undefined.
7898 if (N1.isUndef() || N2.isUndef())
7899 return getUNDEF(VT);
7900
7901 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7902 // vectors. For scalable vectors we will provide appropriate support for
7903 // dealing with arbitrary indices.
7904 if (N2C && N1.getValueType().isFixedLengthVector() &&
7905 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7906 return getUNDEF(VT);
7907
7908 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7909 // expanding copies of large vectors from registers. This only works for
7910 // fixed length vectors, since we need to know the exact number of
7911 // elements.
7912 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7914 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7915 return getExtractVectorElt(DL, VT,
7916 N1.getOperand(N2C->getZExtValue() / Factor),
7917 N2C->getZExtValue() % Factor);
7918 }
7919
7920 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7921 // lowering is expanding large vector constants.
7922 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7923 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7926 "BUILD_VECTOR used for scalable vectors");
7927 unsigned Index =
7928 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7929 SDValue Elt = N1.getOperand(Index);
7930
7931 if (VT != Elt.getValueType())
7932 // If the vector element type is not legal, the BUILD_VECTOR operands
7933 // are promoted and implicitly truncated, and the result implicitly
7934 // extended. Make that explicit here.
7935 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7936
7937 return Elt;
7938 }
7939
7940 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7941 // operations are lowered to scalars.
7942 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7943 // If the indices are the same, return the inserted element else
7944 // if the indices are known different, extract the element from
7945 // the original vector.
7946 SDValue N1Op2 = N1.getOperand(2);
7948
7949 if (N1Op2C && N2C) {
7950 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7951 if (VT == N1.getOperand(1).getValueType())
7952 return N1.getOperand(1);
7953 if (VT.isFloatingPoint()) {
7955 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7956 }
7957 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7958 }
7959 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7960 }
7961 }
7962
7963 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7964 // when vector types are scalarized and v1iX is legal.
7965 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7966 // Here we are completely ignoring the extract element index (N2),
7967 // which is fine for fixed width vectors, since any index other than 0
7968 // is undefined anyway. However, this cannot be ignored for scalable
7969 // vectors - in theory we could support this, but we don't want to do this
7970 // without a profitability check.
7971 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7973 N1.getValueType().getVectorNumElements() == 1) {
7974 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7975 N1.getOperand(1));
7976 }
7977 break;
7979 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7980 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7981 (N1.getValueType().isInteger() == VT.isInteger()) &&
7982 N1.getValueType() != VT &&
7983 "Wrong types for EXTRACT_ELEMENT!");
7984
7985 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7986 // 64-bit integers into 32-bit parts. Instead of building the extract of
7987 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7988 if (N1.getOpcode() == ISD::BUILD_PAIR)
7989 return N1.getOperand(N2C->getZExtValue());
7990
7991 // EXTRACT_ELEMENT of a constant int is also very common.
7992 if (N1C) {
7993 unsigned ElementSize = VT.getSizeInBits();
7994 unsigned Shift = ElementSize * N2C->getZExtValue();
7995 const APInt &Val = N1C->getAPIntValue();
7996 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7997 }
7998 break;
8000 EVT N1VT = N1.getValueType();
8001 assert(VT.isVector() && N1VT.isVector() &&
8002 "Extract subvector VTs must be vectors!");
8004 "Extract subvector VTs must have the same element type!");
8005 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8006 "Cannot extract a scalable vector from a fixed length vector!");
8007 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8009 "Extract subvector must be from larger vector to smaller vector!");
8010 assert(N2C && "Extract subvector index must be a constant");
8011 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8012 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8013 N1VT.getVectorMinNumElements()) &&
8014 "Extract subvector overflow!");
8015 assert(N2C->getAPIntValue().getBitWidth() ==
8016 TLI->getVectorIdxWidth(getDataLayout()) &&
8017 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8018 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8019 "Extract index is not a multiple of the output vector length");
8020
8021 // Trivial extraction.
8022 if (VT == N1VT)
8023 return N1;
8024
8025 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8026 if (N1.isUndef())
8027 return getUNDEF(VT);
8028
8029 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8030 // the concat have the same type as the extract.
8031 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8032 VT == N1.getOperand(0).getValueType()) {
8033 unsigned Factor = VT.getVectorMinNumElements();
8034 return N1.getOperand(N2C->getZExtValue() / Factor);
8035 }
8036
8037 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8038 // during shuffle legalization.
8039 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8040 VT == N1.getOperand(1).getValueType())
8041 return N1.getOperand(1);
8042 break;
8043 }
8044 }
8045
8046 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8047 switch (Opcode) {
8048 case ISD::XOR:
8049 case ISD::ADD:
8050 case ISD::PTRADD:
8051 case ISD::SUB:
8053 case ISD::UDIV:
8054 case ISD::SDIV:
8055 case ISD::UREM:
8056 case ISD::SREM:
8057 case ISD::MUL:
8058 case ISD::AND:
8059 case ISD::SSUBSAT:
8060 case ISD::USUBSAT:
8061 case ISD::UMIN:
8062 case ISD::OR:
8063 case ISD::SADDSAT:
8064 case ISD::UADDSAT:
8065 case ISD::UMAX:
8066 case ISD::SMAX:
8067 case ISD::SMIN:
8068 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8069 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8070 }
8071 }
8072
8073 // Canonicalize an UNDEF to the RHS, even over a constant.
8074 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8075 if (TLI->isCommutativeBinOp(Opcode)) {
8076 std::swap(N1, N2);
8077 } else {
8078 switch (Opcode) {
8079 case ISD::PTRADD:
8080 case ISD::SUB:
8081 // fold op(undef, non_undef_arg2) -> undef.
8082 return N1;
8084 case ISD::UDIV:
8085 case ISD::SDIV:
8086 case ISD::UREM:
8087 case ISD::SREM:
8088 case ISD::SSUBSAT:
8089 case ISD::USUBSAT:
8090 // fold op(undef, non_undef_arg2) -> 0.
8091 return getConstant(0, DL, VT);
8092 }
8093 }
8094 }
8095
8096 // Fold a bunch of operators when the RHS is undef.
8097 if (N2.getOpcode() == ISD::UNDEF) {
8098 switch (Opcode) {
8099 case ISD::XOR:
8100 if (N1.getOpcode() == ISD::UNDEF)
8101 // Handle undef ^ undef -> 0 special case. This is a common
8102 // idiom (misuse).
8103 return getConstant(0, DL, VT);
8104 [[fallthrough]];
8105 case ISD::ADD:
8106 case ISD::PTRADD:
8107 case ISD::SUB:
8108 // fold op(arg1, undef) -> undef.
8109 return N2;
8110 case ISD::UDIV:
8111 case ISD::SDIV:
8112 case ISD::UREM:
8113 case ISD::SREM:
8114 // fold op(arg1, undef) -> poison.
8115 return getPOISON(VT);
8116 case ISD::MUL:
8117 case ISD::AND:
8118 case ISD::SSUBSAT:
8119 case ISD::USUBSAT:
8120 case ISD::UMIN:
8121 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8122 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8123 case ISD::OR:
8124 case ISD::SADDSAT:
8125 case ISD::UADDSAT:
8126 case ISD::UMAX:
8127 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8128 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8129 case ISD::SMAX:
8130 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8131 return N1.getOpcode() == ISD::UNDEF
8132 ? N2
8133 : getConstant(
8135 VT);
8136 case ISD::SMIN:
8137 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8138 return N1.getOpcode() == ISD::UNDEF
8139 ? N2
8140 : getConstant(
8142 VT);
8143 }
8144 }
8145
8146 // Perform trivial constant folding.
8147 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8148 return SV;
8149
8150 // Memoize this node if possible.
8151 SDNode *N;
8152 SDVTList VTs = getVTList(VT);
8153 SDValue Ops[] = {N1, N2};
8154 if (VT != MVT::Glue) {
8156 AddNodeIDNode(ID, Opcode, VTs, Ops);
8157 void *IP = nullptr;
8158 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8159 E->intersectFlagsWith(Flags);
8160 return SDValue(E, 0);
8161 }
8162
8163 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8164 N->setFlags(Flags);
8165 createOperands(N, Ops);
8166 CSEMap.InsertNode(N, IP);
8167 } else {
8168 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8169 createOperands(N, Ops);
8170 }
8171
8172 InsertNode(N);
8173 SDValue V = SDValue(N, 0);
8174 NewSDValueDbgMsg(V, "Creating new node: ", this);
8175 return V;
8176}
8177
8178SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8179 SDValue N1, SDValue N2, SDValue N3) {
8180 SDNodeFlags Flags;
8181 if (Inserter)
8182 Flags = Inserter->getFlags();
8183 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8184}
8185
8186SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8187 SDValue N1, SDValue N2, SDValue N3,
8188 const SDNodeFlags Flags) {
8190 N2.getOpcode() != ISD::DELETED_NODE &&
8191 N3.getOpcode() != ISD::DELETED_NODE &&
8192 "Operand is DELETED_NODE!");
8193 // Perform various simplifications.
8194 switch (Opcode) {
8195 case ISD::BUILD_VECTOR: {
8196 // Attempt to simplify BUILD_VECTOR.
8197 SDValue Ops[] = {N1, N2, N3};
8198 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8199 return V;
8200 break;
8201 }
8202 case ISD::CONCAT_VECTORS: {
8203 SDValue Ops[] = {N1, N2, N3};
8204 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8205 return V;
8206 break;
8207 }
8208 case ISD::SETCC: {
8209 assert(VT.isInteger() && "SETCC result type must be an integer!");
8210 assert(N1.getValueType() == N2.getValueType() &&
8211 "SETCC operands must have the same type!");
8212 assert(VT.isVector() == N1.getValueType().isVector() &&
8213 "SETCC type should be vector iff the operand type is vector!");
8214 assert((!VT.isVector() || VT.getVectorElementCount() ==
8216 "SETCC vector element counts must match!");
8217 // Use FoldSetCC to simplify SETCC's.
8218 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8219 return V;
8220 break;
8221 }
8222 case ISD::SELECT:
8223 case ISD::VSELECT:
8224 if (SDValue V = simplifySelect(N1, N2, N3))
8225 return V;
8226 break;
8228 llvm_unreachable("should use getVectorShuffle constructor!");
8229 case ISD::VECTOR_SPLICE: {
8230 if (cast<ConstantSDNode>(N3)->isZero())
8231 return N1;
8232 break;
8233 }
8235 assert(VT.isVector() && VT == N1.getValueType() &&
8236 "INSERT_VECTOR_ELT vector type mismatch");
8238 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8239 assert((!VT.isFloatingPoint() ||
8240 VT.getVectorElementType() == N2.getValueType()) &&
8241 "INSERT_VECTOR_ELT fp scalar type mismatch");
8242 assert((!VT.isInteger() ||
8244 "INSERT_VECTOR_ELT int scalar size mismatch");
8245
8246 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8247 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8248 // for scalable vectors where we will generate appropriate code to
8249 // deal with out-of-bounds cases correctly.
8250 if (N3C && VT.isFixedLengthVector() &&
8251 N3C->getZExtValue() >= VT.getVectorNumElements())
8252 return getUNDEF(VT);
8253
8254 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8255 if (N3.isUndef())
8256 return getUNDEF(VT);
8257
8258 // If inserting poison, just use the input vector.
8259 if (N2.getOpcode() == ISD::POISON)
8260 return N1;
8261
8262 // Inserting undef into undef/poison is still undef.
8263 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8264 return getUNDEF(VT);
8265
8266 // If the inserted element is an UNDEF, just use the input vector.
8267 // But not if skipping the insert could make the result more poisonous.
8268 if (N2.isUndef()) {
8269 if (N3C && VT.isFixedLengthVector()) {
8270 APInt EltMask =
8271 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8272 if (isGuaranteedNotToBePoison(N1, EltMask))
8273 return N1;
8274 } else if (isGuaranteedNotToBePoison(N1))
8275 return N1;
8276 }
8277 break;
8278 }
8279 case ISD::INSERT_SUBVECTOR: {
8280 // If inserting poison, just use the input vector,
8281 if (N2.getOpcode() == ISD::POISON)
8282 return N1;
8283
8284 // Inserting undef into undef/poison is still undef.
8285 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8286 return getUNDEF(VT);
8287
8288 EVT N2VT = N2.getValueType();
8289 assert(VT == N1.getValueType() &&
8290 "Dest and insert subvector source types must match!");
8291 assert(VT.isVector() && N2VT.isVector() &&
8292 "Insert subvector VTs must be vectors!");
8294 "Insert subvector VTs must have the same element type!");
8295 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8296 "Cannot insert a scalable vector into a fixed length vector!");
8297 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8299 "Insert subvector must be from smaller vector to larger vector!");
8301 "Insert subvector index must be constant");
8302 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8303 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8305 "Insert subvector overflow!");
8307 TLI->getVectorIdxWidth(getDataLayout()) &&
8308 "Constant index for INSERT_SUBVECTOR has an invalid size");
8309
8310 // Trivial insertion.
8311 if (VT == N2VT)
8312 return N2;
8313
8314 // If this is an insert of an extracted vector into an undef/poison vector,
8315 // we can just use the input to the extract. But not if skipping the
8316 // extract+insert could make the result more poisonous.
8317 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8318 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8319 if (N1.getOpcode() == ISD::POISON)
8320 return N2.getOperand(0);
8321 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8322 unsigned LoBit = N3->getAsZExtVal();
8323 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8324 APInt EltMask =
8325 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8326 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8327 return N2.getOperand(0);
8328 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8329 return N2.getOperand(0);
8330 }
8331
8332 // If the inserted subvector is UNDEF, just use the input vector.
8333 // But not if skipping the insert could make the result more poisonous.
8334 if (N2.isUndef()) {
8335 if (VT.isFixedLengthVector()) {
8336 unsigned LoBit = N3->getAsZExtVal();
8337 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8338 APInt EltMask =
8339 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8340 if (isGuaranteedNotToBePoison(N1, EltMask))
8341 return N1;
8342 } else if (isGuaranteedNotToBePoison(N1))
8343 return N1;
8344 }
8345 break;
8346 }
8347 case ISD::BITCAST:
8348 // Fold bit_convert nodes from a type to themselves.
8349 if (N1.getValueType() == VT)
8350 return N1;
8351 break;
8352 case ISD::VP_TRUNCATE:
8353 case ISD::VP_SIGN_EXTEND:
8354 case ISD::VP_ZERO_EXTEND:
8355 // Don't create noop casts.
8356 if (N1.getValueType() == VT)
8357 return N1;
8358 break;
8359 case ISD::VECTOR_COMPRESS: {
8360 [[maybe_unused]] EVT VecVT = N1.getValueType();
8361 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8362 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8363 assert(VT == VecVT && "Vector and result type don't match.");
8364 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8365 "All inputs must be vectors.");
8366 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8368 "Vector and mask must have same number of elements.");
8369
8370 if (N1.isUndef() || N2.isUndef())
8371 return N3;
8372
8373 break;
8374 }
8375 case ISD::PARTIAL_REDUCE_UMLA:
8376 case ISD::PARTIAL_REDUCE_SMLA:
8377 case ISD::PARTIAL_REDUCE_SUMLA:
8378 case ISD::PARTIAL_REDUCE_FMLA: {
8379 [[maybe_unused]] EVT AccVT = N1.getValueType();
8380 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8381 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8382 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8383 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8384 "node to have the same type!");
8385 assert(VT.isVector() && VT == AccVT &&
8386 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8387 "the same type as its result!");
8389 AccVT.getVectorElementCount()) &&
8390 "Expected the element count of the second and third operands of the "
8391 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8392 "element count of the first operand and the result!");
8394 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8395 "node to have an element type which is the same as or smaller than "
8396 "the element type of the first operand and result!");
8397 break;
8398 }
8399 }
8400
8401 // Perform trivial constant folding for arithmetic operators.
8402 switch (Opcode) {
8403 case ISD::FMA:
8404 case ISD::FMAD:
8405 case ISD::SETCC:
8406 case ISD::FSHL:
8407 case ISD::FSHR:
8408 if (SDValue SV =
8409 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8410 return SV;
8411 break;
8412 }
8413
8414 // Memoize node if it doesn't produce a glue result.
8415 SDNode *N;
8416 SDVTList VTs = getVTList(VT);
8417 SDValue Ops[] = {N1, N2, N3};
8418 if (VT != MVT::Glue) {
8420 AddNodeIDNode(ID, Opcode, VTs, Ops);
8421 void *IP = nullptr;
8422 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8423 E->intersectFlagsWith(Flags);
8424 return SDValue(E, 0);
8425 }
8426
8427 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8428 N->setFlags(Flags);
8429 createOperands(N, Ops);
8430 CSEMap.InsertNode(N, IP);
8431 } else {
8432 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8433 createOperands(N, Ops);
8434 }
8435
8436 InsertNode(N);
8437 SDValue V = SDValue(N, 0);
8438 NewSDValueDbgMsg(V, "Creating new node: ", this);
8439 return V;
8440}
8441
8442SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8443 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8444 const SDNodeFlags Flags) {
8445 SDValue Ops[] = { N1, N2, N3, N4 };
8446 return getNode(Opcode, DL, VT, Ops, Flags);
8447}
8448
8449SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8450 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8451 SDNodeFlags Flags;
8452 if (Inserter)
8453 Flags = Inserter->getFlags();
8454 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8455}
8456
8457SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8458 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8459 SDValue N5, const SDNodeFlags Flags) {
8460 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8461 return getNode(Opcode, DL, VT, Ops, Flags);
8462}
8463
8464SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8465 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8466 SDValue N5) {
8467 SDNodeFlags Flags;
8468 if (Inserter)
8469 Flags = Inserter->getFlags();
8470 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8471}
8472
8473/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8474/// the incoming stack arguments to be loaded from the stack.
8476 SmallVector<SDValue, 8> ArgChains;
8477
8478 // Include the original chain at the beginning of the list. When this is
8479 // used by target LowerCall hooks, this helps legalize find the
8480 // CALLSEQ_BEGIN node.
8481 ArgChains.push_back(Chain);
8482
8483 // Add a chain value for each stack argument.
8484 for (SDNode *U : getEntryNode().getNode()->users())
8485 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8486 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8487 if (FI->getIndex() < 0)
8488 ArgChains.push_back(SDValue(L, 1));
8489
8490 // Build a tokenfactor for all the chains.
8491 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8492}
8493
8494/// getMemsetValue - Vectorized representation of the memset value
8495/// operand.
8497 const SDLoc &dl) {
8498 assert(!Value.isUndef());
8499
8500 unsigned NumBits = VT.getScalarSizeInBits();
8502 assert(C->getAPIntValue().getBitWidth() == 8);
8503 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8504 if (VT.isInteger()) {
8505 bool IsOpaque = VT.getSizeInBits() > 64 ||
8506 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8507 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8508 }
8509 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8510 }
8511
8512 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8513 EVT IntVT = VT.getScalarType();
8514 if (!IntVT.isInteger())
8515 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8516
8517 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8518 if (NumBits > 8) {
8519 // Use a multiplication with 0x010101... to extend the input to the
8520 // required length.
8521 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8522 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8523 DAG.getConstant(Magic, dl, IntVT));
8524 }
8525
8526 if (VT != Value.getValueType() && !VT.isInteger())
8527 Value = DAG.getBitcast(VT.getScalarType(), Value);
8528 if (VT != Value.getValueType())
8529 Value = DAG.getSplatBuildVector(VT, dl, Value);
8530
8531 return Value;
8532}
8533
8534/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8535/// used when a memcpy is turned into a memset when the source is a constant
8536/// string ptr.
8538 const TargetLowering &TLI,
8539 const ConstantDataArraySlice &Slice) {
8540 // Handle vector with all elements zero.
8541 if (Slice.Array == nullptr) {
8542 if (VT.isInteger())
8543 return DAG.getConstant(0, dl, VT);
8544 return DAG.getNode(ISD::BITCAST, dl, VT,
8545 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8546 }
8547
8548 assert(!VT.isVector() && "Can't handle vector type here!");
8549 unsigned NumVTBits = VT.getSizeInBits();
8550 unsigned NumVTBytes = NumVTBits / 8;
8551 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8552
8553 APInt Val(NumVTBits, 0);
8554 if (DAG.getDataLayout().isLittleEndian()) {
8555 for (unsigned i = 0; i != NumBytes; ++i)
8556 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8557 } else {
8558 for (unsigned i = 0; i != NumBytes; ++i)
8559 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8560 }
8561
8562 // If the "cost" of materializing the integer immediate is less than the cost
8563 // of a load, then it is cost effective to turn the load into the immediate.
8564 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8565 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8566 return DAG.getConstant(Val, dl, VT);
8567 return SDValue();
8568}
8569
8571 const SDLoc &DL,
8572 const SDNodeFlags Flags) {
8573 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8574 return getMemBasePlusOffset(Base, Index, DL, Flags);
8575}
8576
8578 const SDLoc &DL,
8579 const SDNodeFlags Flags) {
8580 assert(Offset.getValueType().isInteger());
8581 EVT BasePtrVT = Ptr.getValueType();
8582 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8583 BasePtrVT))
8584 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8585 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8586 SDNodeFlags AddFlags = Flags;
8587 AddFlags.setInBounds(false);
8588 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8589}
8590
8591/// Returns true if memcpy source is constant data.
8593 uint64_t SrcDelta = 0;
8594 GlobalAddressSDNode *G = nullptr;
8595 if (Src.getOpcode() == ISD::GlobalAddress)
8597 else if (Src->isAnyAdd() &&
8598 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8599 Src.getOperand(1).getOpcode() == ISD::Constant) {
8600 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8601 SrcDelta = Src.getConstantOperandVal(1);
8602 }
8603 if (!G)
8604 return false;
8605
8606 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8607 SrcDelta + G->getOffset());
8608}
8609
8611 SelectionDAG &DAG) {
8612 // On Darwin, -Os means optimize for size without hurting performance, so
8613 // only really optimize for size when -Oz (MinSize) is used.
8615 return MF.getFunction().hasMinSize();
8616 return DAG.shouldOptForSize();
8617}
8618
8620 SmallVector<SDValue, 32> &OutChains, unsigned From,
8621 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8622 SmallVector<SDValue, 16> &OutStoreChains) {
8623 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8624 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8625 SmallVector<SDValue, 16> GluedLoadChains;
8626 for (unsigned i = From; i < To; ++i) {
8627 OutChains.push_back(OutLoadChains[i]);
8628 GluedLoadChains.push_back(OutLoadChains[i]);
8629 }
8630
8631 // Chain for all loads.
8632 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8633 GluedLoadChains);
8634
8635 for (unsigned i = From; i < To; ++i) {
8636 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8637 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8638 ST->getBasePtr(), ST->getMemoryVT(),
8639 ST->getMemOperand());
8640 OutChains.push_back(NewStore);
8641 }
8642}
8643
8645 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8646 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8647 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8648 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8649 // Turn a memcpy of undef to nop.
8650 // FIXME: We need to honor volatile even is Src is undef.
8651 if (Src.isUndef())
8652 return Chain;
8653
8654 // Expand memcpy to a series of load and store ops if the size operand falls
8655 // below a certain threshold.
8656 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8657 // rather than maybe a humongous number of loads and stores.
8658 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8659 const DataLayout &DL = DAG.getDataLayout();
8660 LLVMContext &C = *DAG.getContext();
8661 std::vector<EVT> MemOps;
8662 bool DstAlignCanChange = false;
8664 MachineFrameInfo &MFI = MF.getFrameInfo();
8665 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8667 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8668 DstAlignCanChange = true;
8669 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8670 if (!SrcAlign || Alignment > *SrcAlign)
8671 SrcAlign = Alignment;
8672 assert(SrcAlign && "SrcAlign must be set");
8674 // If marked as volatile, perform a copy even when marked as constant.
8675 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8676 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8677 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8678 const MemOp Op = isZeroConstant
8679 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8680 /*IsZeroMemset*/ true, isVol)
8681 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8682 *SrcAlign, isVol, CopyFromConstant);
8683 if (!TLI.findOptimalMemOpLowering(
8684 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8685 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8686 return SDValue();
8687
8688 if (DstAlignCanChange) {
8689 Type *Ty = MemOps[0].getTypeForEVT(C);
8690 Align NewAlign = DL.getABITypeAlign(Ty);
8691
8692 // Don't promote to an alignment that would require dynamic stack
8693 // realignment which may conflict with optimizations such as tail call
8694 // optimization.
8696 if (!TRI->hasStackRealignment(MF))
8697 if (MaybeAlign StackAlign = DL.getStackAlignment())
8698 NewAlign = std::min(NewAlign, *StackAlign);
8699
8700 if (NewAlign > Alignment) {
8701 // Give the stack frame object a larger alignment if needed.
8702 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8703 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8704 Alignment = NewAlign;
8705 }
8706 }
8707
8708 // Prepare AAInfo for loads/stores after lowering this memcpy.
8709 AAMDNodes NewAAInfo = AAInfo;
8710 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8711
8712 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8713 bool isConstant =
8714 BatchAA && SrcVal &&
8715 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8716
8717 MachineMemOperand::Flags MMOFlags =
8719 SmallVector<SDValue, 16> OutLoadChains;
8720 SmallVector<SDValue, 16> OutStoreChains;
8721 SmallVector<SDValue, 32> OutChains;
8722 unsigned NumMemOps = MemOps.size();
8723 uint64_t SrcOff = 0, DstOff = 0;
8724 for (unsigned i = 0; i != NumMemOps; ++i) {
8725 EVT VT = MemOps[i];
8726 unsigned VTSize = VT.getSizeInBits() / 8;
8727 SDValue Value, Store;
8728
8729 if (VTSize > Size) {
8730 // Issuing an unaligned load / store pair that overlaps with the previous
8731 // pair. Adjust the offset accordingly.
8732 assert(i == NumMemOps-1 && i != 0);
8733 SrcOff -= VTSize - Size;
8734 DstOff -= VTSize - Size;
8735 }
8736
8737 if (CopyFromConstant &&
8738 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8739 // It's unlikely a store of a vector immediate can be done in a single
8740 // instruction. It would require a load from a constantpool first.
8741 // We only handle zero vectors here.
8742 // FIXME: Handle other cases where store of vector immediate is done in
8743 // a single instruction.
8744 ConstantDataArraySlice SubSlice;
8745 if (SrcOff < Slice.Length) {
8746 SubSlice = Slice;
8747 SubSlice.move(SrcOff);
8748 } else {
8749 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8750 SubSlice.Array = nullptr;
8751 SubSlice.Offset = 0;
8752 SubSlice.Length = VTSize;
8753 }
8754 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8755 if (Value.getNode()) {
8756 Store = DAG.getStore(
8757 Chain, dl, Value,
8758 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8759 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8760 OutChains.push_back(Store);
8761 }
8762 }
8763
8764 if (!Store.getNode()) {
8765 // The type might not be legal for the target. This should only happen
8766 // if the type is smaller than a legal type, as on PPC, so the right
8767 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8768 // to Load/Store if NVT==VT.
8769 // FIXME does the case above also need this?
8770 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8771 assert(NVT.bitsGE(VT));
8772
8773 bool isDereferenceable =
8774 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8775 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8776 if (isDereferenceable)
8778 if (isConstant)
8779 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8780
8781 Value = DAG.getExtLoad(
8782 ISD::EXTLOAD, dl, NVT, Chain,
8783 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8784 SrcPtrInfo.getWithOffset(SrcOff), VT,
8785 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8786 OutLoadChains.push_back(Value.getValue(1));
8787
8788 Store = DAG.getTruncStore(
8789 Chain, dl, Value,
8790 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8791 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8792 OutStoreChains.push_back(Store);
8793 }
8794 SrcOff += VTSize;
8795 DstOff += VTSize;
8796 Size -= VTSize;
8797 }
8798
8799 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8801 unsigned NumLdStInMemcpy = OutStoreChains.size();
8802
8803 if (NumLdStInMemcpy) {
8804 // It may be that memcpy might be converted to memset if it's memcpy
8805 // of constants. In such a case, we won't have loads and stores, but
8806 // just stores. In the absence of loads, there is nothing to gang up.
8807 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8808 // If target does not care, just leave as it.
8809 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8810 OutChains.push_back(OutLoadChains[i]);
8811 OutChains.push_back(OutStoreChains[i]);
8812 }
8813 } else {
8814 // Ld/St less than/equal limit set by target.
8815 if (NumLdStInMemcpy <= GluedLdStLimit) {
8816 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8817 NumLdStInMemcpy, OutLoadChains,
8818 OutStoreChains);
8819 } else {
8820 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8821 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8822 unsigned GlueIter = 0;
8823
8824 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8825 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8826 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8827
8828 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8829 OutLoadChains, OutStoreChains);
8830 GlueIter += GluedLdStLimit;
8831 }
8832
8833 // Residual ld/st.
8834 if (RemainingLdStInMemcpy) {
8835 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8836 RemainingLdStInMemcpy, OutLoadChains,
8837 OutStoreChains);
8838 }
8839 }
8840 }
8841 }
8842 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8843}
8844
8846 SDValue Chain, SDValue Dst, SDValue Src,
8847 uint64_t Size, Align Alignment,
8848 bool isVol, bool AlwaysInline,
8849 MachinePointerInfo DstPtrInfo,
8850 MachinePointerInfo SrcPtrInfo,
8851 const AAMDNodes &AAInfo) {
8852 // Turn a memmove of undef to nop.
8853 // FIXME: We need to honor volatile even is Src is undef.
8854 if (Src.isUndef())
8855 return Chain;
8856
8857 // Expand memmove to a series of load and store ops if the size operand falls
8858 // below a certain threshold.
8859 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8860 const DataLayout &DL = DAG.getDataLayout();
8861 LLVMContext &C = *DAG.getContext();
8862 std::vector<EVT> MemOps;
8863 bool DstAlignCanChange = false;
8865 MachineFrameInfo &MFI = MF.getFrameInfo();
8866 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8868 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8869 DstAlignCanChange = true;
8870 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8871 if (!SrcAlign || Alignment > *SrcAlign)
8872 SrcAlign = Alignment;
8873 assert(SrcAlign && "SrcAlign must be set");
8874 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8875 if (!TLI.findOptimalMemOpLowering(
8876 C, MemOps, Limit,
8877 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8878 /*IsVolatile*/ true),
8879 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8880 MF.getFunction().getAttributes()))
8881 return SDValue();
8882
8883 if (DstAlignCanChange) {
8884 Type *Ty = MemOps[0].getTypeForEVT(C);
8885 Align NewAlign = DL.getABITypeAlign(Ty);
8886
8887 // Don't promote to an alignment that would require dynamic stack
8888 // realignment which may conflict with optimizations such as tail call
8889 // optimization.
8891 if (!TRI->hasStackRealignment(MF))
8892 if (MaybeAlign StackAlign = DL.getStackAlignment())
8893 NewAlign = std::min(NewAlign, *StackAlign);
8894
8895 if (NewAlign > Alignment) {
8896 // Give the stack frame object a larger alignment if needed.
8897 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8898 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8899 Alignment = NewAlign;
8900 }
8901 }
8902
8903 // Prepare AAInfo for loads/stores after lowering this memmove.
8904 AAMDNodes NewAAInfo = AAInfo;
8905 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8906
8907 MachineMemOperand::Flags MMOFlags =
8909 uint64_t SrcOff = 0, DstOff = 0;
8910 SmallVector<SDValue, 8> LoadValues;
8911 SmallVector<SDValue, 8> LoadChains;
8912 SmallVector<SDValue, 8> OutChains;
8913 unsigned NumMemOps = MemOps.size();
8914 for (unsigned i = 0; i < NumMemOps; i++) {
8915 EVT VT = MemOps[i];
8916 unsigned VTSize = VT.getSizeInBits() / 8;
8917 SDValue Value;
8918
8919 bool isDereferenceable =
8920 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8921 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8922 if (isDereferenceable)
8924
8925 Value = DAG.getLoad(
8926 VT, dl, Chain,
8927 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8928 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8929 LoadValues.push_back(Value);
8930 LoadChains.push_back(Value.getValue(1));
8931 SrcOff += VTSize;
8932 }
8933 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8934 OutChains.clear();
8935 for (unsigned i = 0; i < NumMemOps; i++) {
8936 EVT VT = MemOps[i];
8937 unsigned VTSize = VT.getSizeInBits() / 8;
8938 SDValue Store;
8939
8940 Store = DAG.getStore(
8941 Chain, dl, LoadValues[i],
8942 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8943 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8944 OutChains.push_back(Store);
8945 DstOff += VTSize;
8946 }
8947
8948 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8949}
8950
8951/// Lower the call to 'memset' intrinsic function into a series of store
8952/// operations.
8953///
8954/// \param DAG Selection DAG where lowered code is placed.
8955/// \param dl Link to corresponding IR location.
8956/// \param Chain Control flow dependency.
8957/// \param Dst Pointer to destination memory location.
8958/// \param Src Value of byte to write into the memory.
8959/// \param Size Number of bytes to write.
8960/// \param Alignment Alignment of the destination in bytes.
8961/// \param isVol True if destination is volatile.
8962/// \param AlwaysInline Makes sure no function call is generated.
8963/// \param DstPtrInfo IR information on the memory pointer.
8964/// \returns New head in the control flow, if lowering was successful, empty
8965/// SDValue otherwise.
8966///
8967/// The function tries to replace 'llvm.memset' intrinsic with several store
8968/// operations and value calculation code. This is usually profitable for small
8969/// memory size or when the semantic requires inlining.
8971 SDValue Chain, SDValue Dst, SDValue Src,
8972 uint64_t Size, Align Alignment, bool isVol,
8973 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8974 const AAMDNodes &AAInfo) {
8975 // Turn a memset of undef to nop.
8976 // FIXME: We need to honor volatile even is Src is undef.
8977 if (Src.isUndef())
8978 return Chain;
8979
8980 // Expand memset to a series of load/store ops if the size operand
8981 // falls below a certain threshold.
8982 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8983 std::vector<EVT> MemOps;
8984 bool DstAlignCanChange = false;
8985 LLVMContext &C = *DAG.getContext();
8987 MachineFrameInfo &MFI = MF.getFrameInfo();
8988 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8990 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8991 DstAlignCanChange = true;
8992 bool IsZeroVal = isNullConstant(Src);
8993 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8994
8995 if (!TLI.findOptimalMemOpLowering(
8996 C, MemOps, Limit,
8997 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8998 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8999 return SDValue();
9000
9001 if (DstAlignCanChange) {
9002 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9003 const DataLayout &DL = DAG.getDataLayout();
9004 Align NewAlign = DL.getABITypeAlign(Ty);
9005
9006 // Don't promote to an alignment that would require dynamic stack
9007 // realignment which may conflict with optimizations such as tail call
9008 // optimization.
9010 if (!TRI->hasStackRealignment(MF))
9011 if (MaybeAlign StackAlign = DL.getStackAlignment())
9012 NewAlign = std::min(NewAlign, *StackAlign);
9013
9014 if (NewAlign > Alignment) {
9015 // Give the stack frame object a larger alignment if needed.
9016 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9017 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9018 Alignment = NewAlign;
9019 }
9020 }
9021
9022 SmallVector<SDValue, 8> OutChains;
9023 uint64_t DstOff = 0;
9024 unsigned NumMemOps = MemOps.size();
9025
9026 // Find the largest store and generate the bit pattern for it.
9027 EVT LargestVT = MemOps[0];
9028 for (unsigned i = 1; i < NumMemOps; i++)
9029 if (MemOps[i].bitsGT(LargestVT))
9030 LargestVT = MemOps[i];
9031 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9032
9033 // Prepare AAInfo for loads/stores after lowering this memset.
9034 AAMDNodes NewAAInfo = AAInfo;
9035 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9036
9037 for (unsigned i = 0; i < NumMemOps; i++) {
9038 EVT VT = MemOps[i];
9039 unsigned VTSize = VT.getSizeInBits() / 8;
9040 if (VTSize > Size) {
9041 // Issuing an unaligned load / store pair that overlaps with the previous
9042 // pair. Adjust the offset accordingly.
9043 assert(i == NumMemOps-1 && i != 0);
9044 DstOff -= VTSize - Size;
9045 }
9046
9047 // If this store is smaller than the largest store see whether we can get
9048 // the smaller value for free with a truncate or extract vector element and
9049 // then store.
9050 SDValue Value = MemSetValue;
9051 if (VT.bitsLT(LargestVT)) {
9052 unsigned Index;
9053 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9054 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9055 if (!LargestVT.isVector() && !VT.isVector() &&
9056 TLI.isTruncateFree(LargestVT, VT))
9057 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9058 else if (LargestVT.isVector() && !VT.isVector() &&
9060 LargestVT.getTypeForEVT(*DAG.getContext()),
9061 VT.getSizeInBits(), Index) &&
9062 TLI.isTypeLegal(SVT) &&
9063 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9064 // Target which can combine store(extractelement VectorTy, Idx) can get
9065 // the smaller value for free.
9066 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9067 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9068 } else
9069 Value = getMemsetValue(Src, VT, DAG, dl);
9070 }
9071 assert(Value.getValueType() == VT && "Value with wrong type.");
9072 SDValue Store = DAG.getStore(
9073 Chain, dl, Value,
9074 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9075 DstPtrInfo.getWithOffset(DstOff), Alignment,
9077 NewAAInfo);
9078 OutChains.push_back(Store);
9079 DstOff += VT.getSizeInBits() / 8;
9080 Size -= VTSize;
9081 }
9082
9083 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9084}
9085
9087 unsigned AS) {
9088 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9089 // pointer operands can be losslessly bitcasted to pointers of address space 0
9090 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9091 report_fatal_error("cannot lower memory intrinsic in address space " +
9092 Twine(AS));
9093 }
9094}
9095
9097 const SelectionDAG *SelDAG,
9098 bool AllowReturnsFirstArg) {
9099 if (!CI || !CI->isTailCall())
9100 return false;
9101 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9102 // helper symbol we lower to.
9103 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9104 AllowReturnsFirstArg &&
9106}
9107
9108std::pair<SDValue, SDValue>
9110 SDValue Mem1, SDValue Size, const CallInst *CI) {
9111 RTLIB::LibcallImpl MemcmpImpl = TLI->getLibcallImpl(RTLIB::MEMCMP);
9112 if (MemcmpImpl == RTLIB::Unsupported)
9113 return {};
9114
9117 {Mem0, PT},
9118 {Mem1, PT},
9120
9122 bool IsTailCall =
9123 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9124
9125 StringRef LibCallName = TLI->getLibcallImplName(MemcmpImpl);
9126 CLI.setDebugLoc(dl)
9127 .setChain(Chain)
9128 .setLibCallee(TLI->getLibcallImplCallingConv(MemcmpImpl),
9130 getExternalSymbol(LibCallName.data(),
9131 TLI->getPointerTy(getDataLayout())),
9132 std::move(Args))
9133 .setTailCall(IsTailCall);
9134
9135 return TLI->LowerCallTo(CLI);
9136}
9137
9138std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9139 const SDLoc &dl,
9140 SDValue Src,
9141 const CallInst *CI) {
9142 RTLIB::LibcallImpl StrlenImpl = TLI->getLibcallImpl(RTLIB::STRLEN);
9143 if (StrlenImpl == RTLIB::Unsupported)
9144 return {};
9145
9146 // Emit a library call.
9149
9151 bool IsTailCall =
9152 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9153 StringRef LibcallName = TLI->getLibcallImplName(StrlenImpl);
9154
9155 CLI.setDebugLoc(dl)
9156 .setChain(Chain)
9157 .setLibCallee(
9158 TLI->getLibcallImplCallingConv(StrlenImpl), CI->getType(),
9159 getExternalSymbol(LibcallName.data(),
9160 TLI->getProgramPointerTy(getDataLayout())),
9161 std::move(Args))
9162 .setTailCall(IsTailCall);
9163
9164 return TLI->LowerCallTo(CLI);
9165}
9166
9168 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9169 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9170 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9171 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9172 BatchAAResults *BatchAA) {
9173 // Check to see if we should lower the memcpy to loads and stores first.
9174 // For cases within the target-specified limits, this is the best choice.
9176 if (ConstantSize) {
9177 // Memcpy with size zero? Just return the original chain.
9178 if (ConstantSize->isZero())
9179 return Chain;
9180
9182 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9183 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9184 if (Result.getNode())
9185 return Result;
9186 }
9187
9188 // Then check to see if we should lower the memcpy with target-specific
9189 // code. If the target chooses to do this, this is the next best.
9190 if (TSI) {
9191 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9192 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9193 DstPtrInfo, SrcPtrInfo);
9194 if (Result.getNode())
9195 return Result;
9196 }
9197
9198 // If we really need inline code and the target declined to provide it,
9199 // use a (potentially long) sequence of loads and stores.
9200 if (AlwaysInline) {
9201 assert(ConstantSize && "AlwaysInline requires a constant size!");
9203 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9204 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9205 }
9206
9209
9210 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9211 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9212 // respect volatile, so they may do things like read or write memory
9213 // beyond the given memory regions. But fixing this isn't easy, and most
9214 // people don't care.
9215
9216 // Emit a library call.
9219 Args.emplace_back(Dst, PtrTy);
9220 Args.emplace_back(Src, PtrTy);
9221 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9222 // FIXME: pass in SDLoc
9224 bool IsTailCall = false;
9225 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9226
9227 if (OverrideTailCall.has_value()) {
9228 IsTailCall = *OverrideTailCall;
9229 } else {
9230 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9231 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9232 }
9233
9234 CLI.setDebugLoc(dl)
9235 .setChain(Chain)
9236 .setLibCallee(
9237 TLI->getLibcallImplCallingConv(MemCpyImpl),
9238 Dst.getValueType().getTypeForEVT(*getContext()),
9239 getExternalSymbol(TLI->getLibcallImplName(MemCpyImpl).data(),
9240 TLI->getPointerTy(getDataLayout())),
9241 std::move(Args))
9243 .setTailCall(IsTailCall);
9244
9245 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9246 return CallResult.second;
9247}
9248
9250 SDValue Dst, SDValue Src, SDValue Size,
9251 Type *SizeTy, unsigned ElemSz,
9252 bool isTailCall,
9253 MachinePointerInfo DstPtrInfo,
9254 MachinePointerInfo SrcPtrInfo) {
9255 // Emit a library call.
9258 Args.emplace_back(Dst, ArgTy);
9259 Args.emplace_back(Src, ArgTy);
9260 Args.emplace_back(Size, SizeTy);
9261
9262 RTLIB::Libcall LibraryCall =
9264 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9265 if (LibcallImpl == RTLIB::Unsupported)
9266 report_fatal_error("Unsupported element size");
9267
9269 CLI.setDebugLoc(dl)
9270 .setChain(Chain)
9271 .setLibCallee(
9272 TLI->getLibcallImplCallingConv(LibcallImpl),
9274 getExternalSymbol(TLI->getLibcallImplName(LibcallImpl).data(),
9275 TLI->getPointerTy(getDataLayout())),
9276 std::move(Args))
9278 .setTailCall(isTailCall);
9279
9280 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9281 return CallResult.second;
9282}
9283
9285 SDValue Src, SDValue Size, Align Alignment,
9286 bool isVol, const CallInst *CI,
9287 std::optional<bool> OverrideTailCall,
9288 MachinePointerInfo DstPtrInfo,
9289 MachinePointerInfo SrcPtrInfo,
9290 const AAMDNodes &AAInfo,
9291 BatchAAResults *BatchAA) {
9292 // Check to see if we should lower the memmove to loads and stores first.
9293 // For cases within the target-specified limits, this is the best choice.
9295 if (ConstantSize) {
9296 // Memmove with size zero? Just return the original chain.
9297 if (ConstantSize->isZero())
9298 return Chain;
9299
9301 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9302 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9303 if (Result.getNode())
9304 return Result;
9305 }
9306
9307 // Then check to see if we should lower the memmove with target-specific
9308 // code. If the target chooses to do this, this is the next best.
9309 if (TSI) {
9310 SDValue Result =
9311 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9312 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9313 if (Result.getNode())
9314 return Result;
9315 }
9316
9319
9320 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9321 // not be safe. See memcpy above for more details.
9322
9323 // Emit a library call.
9326 Args.emplace_back(Dst, PtrTy);
9327 Args.emplace_back(Src, PtrTy);
9328 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9329 // FIXME: pass in SDLoc
9331
9332 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9333
9334 bool IsTailCall = false;
9335 if (OverrideTailCall.has_value()) {
9336 IsTailCall = *OverrideTailCall;
9337 } else {
9338 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9339 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9340 }
9341
9342 CLI.setDebugLoc(dl)
9343 .setChain(Chain)
9344 .setLibCallee(
9345 TLI->getLibcallImplCallingConv(MemmoveImpl),
9346 Dst.getValueType().getTypeForEVT(*getContext()),
9347 getExternalSymbol(TLI->getLibcallImplName(MemmoveImpl).data(),
9348 TLI->getPointerTy(getDataLayout())),
9349 std::move(Args))
9351 .setTailCall(IsTailCall);
9352
9353 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9354 return CallResult.second;
9355}
9356
9358 SDValue Dst, SDValue Src, SDValue Size,
9359 Type *SizeTy, unsigned ElemSz,
9360 bool isTailCall,
9361 MachinePointerInfo DstPtrInfo,
9362 MachinePointerInfo SrcPtrInfo) {
9363 // Emit a library call.
9365 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9366 Args.emplace_back(Dst, IntPtrTy);
9367 Args.emplace_back(Src, IntPtrTy);
9368 Args.emplace_back(Size, SizeTy);
9369
9370 RTLIB::Libcall LibraryCall =
9372 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9373 if (LibcallImpl == RTLIB::Unsupported)
9374 report_fatal_error("Unsupported element size");
9375
9377 CLI.setDebugLoc(dl)
9378 .setChain(Chain)
9379 .setLibCallee(
9380 TLI->getLibcallImplCallingConv(LibcallImpl),
9382 getExternalSymbol(TLI->getLibcallImplName(LibcallImpl).data(),
9383 TLI->getPointerTy(getDataLayout())),
9384 std::move(Args))
9386 .setTailCall(isTailCall);
9387
9388 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9389 return CallResult.second;
9390}
9391
9393 SDValue Src, SDValue Size, Align Alignment,
9394 bool isVol, bool AlwaysInline,
9395 const CallInst *CI,
9396 MachinePointerInfo DstPtrInfo,
9397 const AAMDNodes &AAInfo) {
9398 // Check to see if we should lower the memset to stores first.
9399 // For cases within the target-specified limits, this is the best choice.
9401 if (ConstantSize) {
9402 // Memset with size zero? Just return the original chain.
9403 if (ConstantSize->isZero())
9404 return Chain;
9405
9406 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9407 ConstantSize->getZExtValue(), Alignment,
9408 isVol, false, DstPtrInfo, AAInfo);
9409
9410 if (Result.getNode())
9411 return Result;
9412 }
9413
9414 // Then check to see if we should lower the memset with target-specific
9415 // code. If the target chooses to do this, this is the next best.
9416 if (TSI) {
9417 SDValue Result = TSI->EmitTargetCodeForMemset(
9418 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9419 if (Result.getNode())
9420 return Result;
9421 }
9422
9423 // If we really need inline code and the target declined to provide it,
9424 // use a (potentially long) sequence of loads and stores.
9425 if (AlwaysInline) {
9426 assert(ConstantSize && "AlwaysInline requires a constant size!");
9427 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9428 ConstantSize->getZExtValue(), Alignment,
9429 isVol, true, DstPtrInfo, AAInfo);
9430 assert(Result &&
9431 "getMemsetStores must return a valid sequence when AlwaysInline");
9432 return Result;
9433 }
9434
9436
9437 // Emit a library call.
9438 auto &Ctx = *getContext();
9439 const auto& DL = getDataLayout();
9440
9442 // FIXME: pass in SDLoc
9443 CLI.setDebugLoc(dl).setChain(Chain);
9444
9445 RTLIB::LibcallImpl BzeroImpl = TLI->getLibcallImpl(RTLIB::BZERO);
9446 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9447
9448 // If zeroing out and bzero is present, use it.
9449 if (UseBZero) {
9451 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9452 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9453 CLI.setLibCallee(
9454 TLI->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9455 getExternalSymbol(TLI->getLibcallImplName(BzeroImpl).data(),
9456 TLI->getPointerTy(DL)),
9457 std::move(Args));
9458 } else {
9459 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9460
9462 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9463 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9464 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9465 CLI.setLibCallee(
9466 TLI->getLibcallImplCallingConv(MemsetImpl),
9467 Dst.getValueType().getTypeForEVT(Ctx),
9468 getExternalSymbol(TLI->getLibcallImplName(MemsetImpl).data(),
9469 TLI->getPointerTy(DL)),
9470 std::move(Args));
9471 }
9472
9473 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9474 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9475
9476 // If we're going to use bzero, make sure not to tail call unless the
9477 // subsequent return doesn't need a value, as bzero doesn't return the first
9478 // arg unlike memset.
9479 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9480 bool IsTailCall =
9481 CI && CI->isTailCall() &&
9482 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9483 CLI.setDiscardResult().setTailCall(IsTailCall);
9484
9485 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9486 return CallResult.second;
9487}
9488
9491 Type *SizeTy, unsigned ElemSz,
9492 bool isTailCall,
9493 MachinePointerInfo DstPtrInfo) {
9494 // Emit a library call.
9496 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9497 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9498 Args.emplace_back(Size, SizeTy);
9499
9500 RTLIB::Libcall LibraryCall =
9502 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9503 if (LibcallImpl == RTLIB::Unsupported)
9504 report_fatal_error("Unsupported element size");
9505
9507 CLI.setDebugLoc(dl)
9508 .setChain(Chain)
9509 .setLibCallee(
9510 TLI->getLibcallImplCallingConv(LibcallImpl),
9512 getExternalSymbol(TLI->getLibcallImplName(LibcallImpl).data(),
9513 TLI->getPointerTy(getDataLayout())),
9514 std::move(Args))
9516 .setTailCall(isTailCall);
9517
9518 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9519 return CallResult.second;
9520}
9521
9522SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9524 MachineMemOperand *MMO,
9525 ISD::LoadExtType ExtType) {
9527 AddNodeIDNode(ID, Opcode, VTList, Ops);
9528 ID.AddInteger(MemVT.getRawBits());
9529 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9530 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9531 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9532 ID.AddInteger(MMO->getFlags());
9533 void* IP = nullptr;
9534 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9535 E->refineAlignment(MMO);
9536 E->refineRanges(MMO);
9537 return SDValue(E, 0);
9538 }
9539
9540 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9541 VTList, MemVT, MMO, ExtType);
9542 createOperands(N, Ops);
9543
9544 CSEMap.InsertNode(N, IP);
9545 InsertNode(N);
9546 SDValue V(N, 0);
9547 NewSDValueDbgMsg(V, "Creating new node: ", this);
9548 return V;
9549}
9550
9552 EVT MemVT, SDVTList VTs, SDValue Chain,
9553 SDValue Ptr, SDValue Cmp, SDValue Swp,
9554 MachineMemOperand *MMO) {
9555 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9556 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
9557 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9558
9559 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9560 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9561}
9562
9563SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9564 SDValue Chain, SDValue Ptr, SDValue Val,
9565 MachineMemOperand *MMO) {
9566 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9567 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9568 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9569 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9570 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9571 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9572 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9573 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9574 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9575 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9576 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9577 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9578 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9579 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9580 Opcode == ISD::ATOMIC_STORE) &&
9581 "Invalid Atomic Op");
9582
9583 EVT VT = Val.getValueType();
9584
9585 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9586 getVTList(VT, MVT::Other);
9587 SDValue Ops[] = {Chain, Ptr, Val};
9588 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9589}
9590
9592 EVT MemVT, EVT VT, SDValue Chain,
9593 SDValue Ptr, MachineMemOperand *MMO) {
9594 SDVTList VTs = getVTList(VT, MVT::Other);
9595 SDValue Ops[] = {Chain, Ptr};
9596 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9597}
9598
9599/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9601 if (Ops.size() == 1)
9602 return Ops[0];
9603
9605 VTs.reserve(Ops.size());
9606 for (const SDValue &Op : Ops)
9607 VTs.push_back(Op.getValueType());
9608 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9609}
9610
9612 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9613 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9615 const AAMDNodes &AAInfo) {
9616 if (Size.hasValue() && !Size.getValue())
9618
9620 MachineMemOperand *MMO =
9621 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9622
9623 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9624}
9625
9627 SDVTList VTList,
9628 ArrayRef<SDValue> Ops, EVT MemVT,
9629 MachineMemOperand *MMO) {
9630 assert(
9631 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9632 Opcode == ISD::PREFETCH ||
9633 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9634 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9635 "Opcode is not a memory-accessing opcode!");
9636
9637 // Memoize the node unless it returns a glue result.
9639 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9641 AddNodeIDNode(ID, Opcode, VTList, Ops);
9642 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9643 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9644 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9645 ID.AddInteger(MMO->getFlags());
9646 ID.AddInteger(MemVT.getRawBits());
9647 void *IP = nullptr;
9648 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9649 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9650 return SDValue(E, 0);
9651 }
9652
9653 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9654 VTList, MemVT, MMO);
9655 createOperands(N, Ops);
9656
9657 CSEMap.InsertNode(N, IP);
9658 } else {
9659 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9660 VTList, MemVT, MMO);
9661 createOperands(N, Ops);
9662 }
9663 InsertNode(N);
9664 SDValue V(N, 0);
9665 NewSDValueDbgMsg(V, "Creating new node: ", this);
9666 return V;
9667}
9668
9670 SDValue Chain, int FrameIndex) {
9671 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9672 const auto VTs = getVTList(MVT::Other);
9673 SDValue Ops[2] = {
9674 Chain,
9675 getFrameIndex(FrameIndex,
9676 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9677 true)};
9678
9680 AddNodeIDNode(ID, Opcode, VTs, Ops);
9681 ID.AddInteger(FrameIndex);
9682 void *IP = nullptr;
9683 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9684 return SDValue(E, 0);
9685
9686 LifetimeSDNode *N =
9687 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9688 createOperands(N, Ops);
9689 CSEMap.InsertNode(N, IP);
9690 InsertNode(N);
9691 SDValue V(N, 0);
9692 NewSDValueDbgMsg(V, "Creating new node: ", this);
9693 return V;
9694}
9695
9697 uint64_t Guid, uint64_t Index,
9698 uint32_t Attr) {
9699 const unsigned Opcode = ISD::PSEUDO_PROBE;
9700 const auto VTs = getVTList(MVT::Other);
9701 SDValue Ops[] = {Chain};
9703 AddNodeIDNode(ID, Opcode, VTs, Ops);
9704 ID.AddInteger(Guid);
9705 ID.AddInteger(Index);
9706 void *IP = nullptr;
9707 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9708 return SDValue(E, 0);
9709
9710 auto *N = newSDNode<PseudoProbeSDNode>(
9711 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9712 createOperands(N, Ops);
9713 CSEMap.InsertNode(N, IP);
9714 InsertNode(N);
9715 SDValue V(N, 0);
9716 NewSDValueDbgMsg(V, "Creating new node: ", this);
9717 return V;
9718}
9719
9720/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9721/// MachinePointerInfo record from it. This is particularly useful because the
9722/// code generator has many cases where it doesn't bother passing in a
9723/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9725 SelectionDAG &DAG, SDValue Ptr,
9726 int64_t Offset = 0) {
9727 // If this is FI+Offset, we can model it.
9728 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9730 FI->getIndex(), Offset);
9731
9732 // If this is (FI+Offset1)+Offset2, we can model it.
9733 if (Ptr.getOpcode() != ISD::ADD ||
9736 return Info;
9737
9738 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9740 DAG.getMachineFunction(), FI,
9741 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9742}
9743
9744/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9745/// MachinePointerInfo record from it. This is particularly useful because the
9746/// code generator has many cases where it doesn't bother passing in a
9747/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9749 SelectionDAG &DAG, SDValue Ptr,
9750 SDValue OffsetOp) {
9751 // If the 'Offset' value isn't a constant, we can't handle this.
9753 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9754 if (OffsetOp.isUndef())
9755 return InferPointerInfo(Info, DAG, Ptr);
9756 return Info;
9757}
9758
9760 EVT VT, const SDLoc &dl, SDValue Chain,
9761 SDValue Ptr, SDValue Offset,
9762 MachinePointerInfo PtrInfo, EVT MemVT,
9763 Align Alignment,
9764 MachineMemOperand::Flags MMOFlags,
9765 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9766 assert(Chain.getValueType() == MVT::Other &&
9767 "Invalid chain type");
9768
9769 MMOFlags |= MachineMemOperand::MOLoad;
9770 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9771 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9772 // clients.
9773 if (PtrInfo.V.isNull())
9774 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9775
9776 TypeSize Size = MemVT.getStoreSize();
9778 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9779 Alignment, AAInfo, Ranges);
9780 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9781}
9782
9784 EVT VT, const SDLoc &dl, SDValue Chain,
9785 SDValue Ptr, SDValue Offset, EVT MemVT,
9786 MachineMemOperand *MMO) {
9787 if (VT == MemVT) {
9788 ExtType = ISD::NON_EXTLOAD;
9789 } else if (ExtType == ISD::NON_EXTLOAD) {
9790 assert(VT == MemVT && "Non-extending load from different memory type!");
9791 } else {
9792 // Extending load.
9793 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9794 "Should only be an extending load, not truncating!");
9795 assert(VT.isInteger() == MemVT.isInteger() &&
9796 "Cannot convert from FP to Int or Int -> FP!");
9797 assert(VT.isVector() == MemVT.isVector() &&
9798 "Cannot use an ext load to convert to or from a vector!");
9799 assert((!VT.isVector() ||
9801 "Cannot use an ext load to change the number of vector elements!");
9802 }
9803
9804 assert((!MMO->getRanges() ||
9806 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9807 MemVT.isInteger())) &&
9808 "Range metadata and load type must match!");
9809
9810 bool Indexed = AM != ISD::UNINDEXED;
9811 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9812
9813 SDVTList VTs = Indexed ?
9814 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9815 SDValue Ops[] = { Chain, Ptr, Offset };
9817 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9818 ID.AddInteger(MemVT.getRawBits());
9819 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9820 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9821 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9822 ID.AddInteger(MMO->getFlags());
9823 void *IP = nullptr;
9824 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9825 E->refineAlignment(MMO);
9826 E->refineRanges(MMO);
9827 return SDValue(E, 0);
9828 }
9829 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9830 ExtType, MemVT, MMO);
9831 createOperands(N, Ops);
9832
9833 CSEMap.InsertNode(N, IP);
9834 InsertNode(N);
9835 SDValue V(N, 0);
9836 NewSDValueDbgMsg(V, "Creating new node: ", this);
9837 return V;
9838}
9839
9841 SDValue Ptr, MachinePointerInfo PtrInfo,
9842 MaybeAlign Alignment,
9843 MachineMemOperand::Flags MMOFlags,
9844 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9845 SDValue Undef = getUNDEF(Ptr.getValueType());
9846 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9847 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9848}
9849
9851 SDValue Ptr, MachineMemOperand *MMO) {
9852 SDValue Undef = getUNDEF(Ptr.getValueType());
9853 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9854 VT, MMO);
9855}
9856
9858 EVT VT, SDValue Chain, SDValue Ptr,
9859 MachinePointerInfo PtrInfo, EVT MemVT,
9860 MaybeAlign Alignment,
9861 MachineMemOperand::Flags MMOFlags,
9862 const AAMDNodes &AAInfo) {
9863 SDValue Undef = getUNDEF(Ptr.getValueType());
9864 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9865 MemVT, Alignment, MMOFlags, AAInfo);
9866}
9867
9869 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9870 MachineMemOperand *MMO) {
9871 SDValue Undef = getUNDEF(Ptr.getValueType());
9872 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9873 MemVT, MMO);
9874}
9875
9879 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9880 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9881 // Don't propagate the invariant or dereferenceable flags.
9882 auto MMOFlags =
9883 LD->getMemOperand()->getFlags() &
9885 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9886 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9887 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9888}
9889
9891 SDValue Ptr, MachinePointerInfo PtrInfo,
9892 Align Alignment,
9893 MachineMemOperand::Flags MMOFlags,
9894 const AAMDNodes &AAInfo) {
9895 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9896
9897 MMOFlags |= MachineMemOperand::MOStore;
9898 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9899
9900 if (PtrInfo.V.isNull())
9901 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9902
9905 MachineMemOperand *MMO =
9906 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9907 return getStore(Chain, dl, Val, Ptr, MMO);
9908}
9909
9911 SDValue Ptr, MachineMemOperand *MMO) {
9912 SDValue Undef = getUNDEF(Ptr.getValueType());
9913 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9915}
9916
9918 SDValue Ptr, SDValue Offset, EVT SVT,
9920 bool IsTruncating) {
9921 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9922 EVT VT = Val.getValueType();
9923 if (VT == SVT) {
9924 IsTruncating = false;
9925 } else if (!IsTruncating) {
9926 assert(VT == SVT && "No-truncating store from different memory type!");
9927 } else {
9929 "Should only be a truncating store, not extending!");
9930 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9931 assert(VT.isVector() == SVT.isVector() &&
9932 "Cannot use trunc store to convert to or from a vector!");
9933 assert((!VT.isVector() ||
9935 "Cannot use trunc store to change the number of vector elements!");
9936 }
9937
9938 bool Indexed = AM != ISD::UNINDEXED;
9939 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9940 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9941 : getVTList(MVT::Other);
9942 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9944 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9945 ID.AddInteger(SVT.getRawBits());
9946 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9947 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9948 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9949 ID.AddInteger(MMO->getFlags());
9950 void *IP = nullptr;
9951 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9952 cast<StoreSDNode>(E)->refineAlignment(MMO);
9953 return SDValue(E, 0);
9954 }
9955 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9956 IsTruncating, SVT, MMO);
9957 createOperands(N, Ops);
9958
9959 CSEMap.InsertNode(N, IP);
9960 InsertNode(N);
9961 SDValue V(N, 0);
9962 NewSDValueDbgMsg(V, "Creating new node: ", this);
9963 return V;
9964}
9965
9967 SDValue Ptr, MachinePointerInfo PtrInfo,
9968 EVT SVT, Align Alignment,
9969 MachineMemOperand::Flags MMOFlags,
9970 const AAMDNodes &AAInfo) {
9971 assert(Chain.getValueType() == MVT::Other &&
9972 "Invalid chain type");
9973
9974 MMOFlags |= MachineMemOperand::MOStore;
9975 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9976
9977 if (PtrInfo.V.isNull())
9978 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9979
9981 MachineMemOperand *MMO = MF.getMachineMemOperand(
9982 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9983 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9984}
9985
9987 SDValue Ptr, EVT SVT,
9988 MachineMemOperand *MMO) {
9989 SDValue Undef = getUNDEF(Ptr.getValueType());
9990 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9991}
9992
9996 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9997 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9998 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9999 ST->getMemoryVT(), ST->getMemOperand(), AM,
10000 ST->isTruncatingStore());
10001}
10002
10004 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10005 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10006 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10007 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10008 const MDNode *Ranges, bool IsExpanding) {
10009 MMOFlags |= MachineMemOperand::MOLoad;
10010 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10011 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10012 // clients.
10013 if (PtrInfo.V.isNull())
10014 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10015
10016 TypeSize Size = MemVT.getStoreSize();
10018 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10019 Alignment, AAInfo, Ranges);
10020 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10021 MMO, IsExpanding);
10022}
10023
10025 ISD::LoadExtType ExtType, EVT VT,
10026 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10027 SDValue Offset, SDValue Mask, SDValue EVL,
10028 EVT MemVT, MachineMemOperand *MMO,
10029 bool IsExpanding) {
10030 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10031 assert(Mask.getValueType().getVectorElementCount() ==
10032 VT.getVectorElementCount() &&
10033 "Vector width mismatch between mask and data");
10034
10035 bool Indexed = AM != ISD::UNINDEXED;
10036 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10037
10038 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10039 : getVTList(VT, MVT::Other);
10040 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10042 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10043 ID.AddInteger(MemVT.getRawBits());
10044 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10045 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10046 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10047 ID.AddInteger(MMO->getFlags());
10048 void *IP = nullptr;
10049 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10050 E->refineAlignment(MMO);
10051 E->refineRanges(MMO);
10052 return SDValue(E, 0);
10053 }
10054 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10055 ExtType, IsExpanding, MemVT, MMO);
10056 createOperands(N, Ops);
10057
10058 CSEMap.InsertNode(N, IP);
10059 InsertNode(N);
10060 SDValue V(N, 0);
10061 NewSDValueDbgMsg(V, "Creating new node: ", this);
10062 return V;
10063}
10064
10066 SDValue Ptr, SDValue Mask, SDValue EVL,
10067 MachinePointerInfo PtrInfo,
10068 MaybeAlign Alignment,
10069 MachineMemOperand::Flags MMOFlags,
10070 const AAMDNodes &AAInfo, const MDNode *Ranges,
10071 bool IsExpanding) {
10072 SDValue Undef = getUNDEF(Ptr.getValueType());
10073 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10074 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10075 IsExpanding);
10076}
10077
10079 SDValue Ptr, SDValue Mask, SDValue EVL,
10080 MachineMemOperand *MMO, bool IsExpanding) {
10081 SDValue Undef = getUNDEF(Ptr.getValueType());
10082 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10083 Mask, EVL, VT, MMO, IsExpanding);
10084}
10085
10087 EVT VT, SDValue Chain, SDValue Ptr,
10088 SDValue Mask, SDValue EVL,
10089 MachinePointerInfo PtrInfo, EVT MemVT,
10090 MaybeAlign Alignment,
10091 MachineMemOperand::Flags MMOFlags,
10092 const AAMDNodes &AAInfo, bool IsExpanding) {
10093 SDValue Undef = getUNDEF(Ptr.getValueType());
10094 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10095 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10096 IsExpanding);
10097}
10098
10100 EVT VT, SDValue Chain, SDValue Ptr,
10101 SDValue Mask, SDValue EVL, EVT MemVT,
10102 MachineMemOperand *MMO, bool IsExpanding) {
10103 SDValue Undef = getUNDEF(Ptr.getValueType());
10104 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10105 EVL, MemVT, MMO, IsExpanding);
10106}
10107
10111 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10112 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10113 // Don't propagate the invariant or dereferenceable flags.
10114 auto MMOFlags =
10115 LD->getMemOperand()->getFlags() &
10117 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10118 LD->getChain(), Base, Offset, LD->getMask(),
10119 LD->getVectorLength(), LD->getPointerInfo(),
10120 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10121 nullptr, LD->isExpandingLoad());
10122}
10123
10125 SDValue Ptr, SDValue Offset, SDValue Mask,
10126 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10127 ISD::MemIndexedMode AM, bool IsTruncating,
10128 bool IsCompressing) {
10129 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10130 assert(Mask.getValueType().getVectorElementCount() ==
10132 "Vector width mismatch between mask and data");
10133
10134 bool Indexed = AM != ISD::UNINDEXED;
10135 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10136 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10137 : getVTList(MVT::Other);
10138 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10140 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10141 ID.AddInteger(MemVT.getRawBits());
10142 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10143 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10144 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10145 ID.AddInteger(MMO->getFlags());
10146 void *IP = nullptr;
10147 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10148 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10149 return SDValue(E, 0);
10150 }
10151 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10152 IsTruncating, IsCompressing, MemVT, MMO);
10153 createOperands(N, Ops);
10154
10155 CSEMap.InsertNode(N, IP);
10156 InsertNode(N);
10157 SDValue V(N, 0);
10158 NewSDValueDbgMsg(V, "Creating new node: ", this);
10159 return V;
10160}
10161
10163 SDValue Val, SDValue Ptr, SDValue Mask,
10164 SDValue EVL, MachinePointerInfo PtrInfo,
10165 EVT SVT, Align Alignment,
10166 MachineMemOperand::Flags MMOFlags,
10167 const AAMDNodes &AAInfo,
10168 bool IsCompressing) {
10169 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10170
10171 MMOFlags |= MachineMemOperand::MOStore;
10172 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10173
10174 if (PtrInfo.V.isNull())
10175 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10176
10178 MachineMemOperand *MMO = MF.getMachineMemOperand(
10179 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10180 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10181 IsCompressing);
10182}
10183
10185 SDValue Val, SDValue Ptr, SDValue Mask,
10186 SDValue EVL, EVT SVT,
10187 MachineMemOperand *MMO,
10188 bool IsCompressing) {
10189 EVT VT = Val.getValueType();
10190
10191 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10192 if (VT == SVT)
10193 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10194 EVL, VT, MMO, ISD::UNINDEXED,
10195 /*IsTruncating*/ false, IsCompressing);
10196
10198 "Should only be a truncating store, not extending!");
10199 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10200 assert(VT.isVector() == SVT.isVector() &&
10201 "Cannot use trunc store to convert to or from a vector!");
10202 assert((!VT.isVector() ||
10204 "Cannot use trunc store to change the number of vector elements!");
10205
10206 SDVTList VTs = getVTList(MVT::Other);
10207 SDValue Undef = getUNDEF(Ptr.getValueType());
10208 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10210 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10211 ID.AddInteger(SVT.getRawBits());
10212 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10213 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10214 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10215 ID.AddInteger(MMO->getFlags());
10216 void *IP = nullptr;
10217 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10218 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10219 return SDValue(E, 0);
10220 }
10221 auto *N =
10222 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10223 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10224 createOperands(N, Ops);
10225
10226 CSEMap.InsertNode(N, IP);
10227 InsertNode(N);
10228 SDValue V(N, 0);
10229 NewSDValueDbgMsg(V, "Creating new node: ", this);
10230 return V;
10231}
10232
10236 auto *ST = cast<VPStoreSDNode>(OrigStore);
10237 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10238 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10239 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10240 Offset, ST->getMask(), ST->getVectorLength()};
10242 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10243 ID.AddInteger(ST->getMemoryVT().getRawBits());
10244 ID.AddInteger(ST->getRawSubclassData());
10245 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10246 ID.AddInteger(ST->getMemOperand()->getFlags());
10247 void *IP = nullptr;
10248 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10249 return SDValue(E, 0);
10250
10251 auto *N = newSDNode<VPStoreSDNode>(
10252 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10253 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10254 createOperands(N, Ops);
10255
10256 CSEMap.InsertNode(N, IP);
10257 InsertNode(N);
10258 SDValue V(N, 0);
10259 NewSDValueDbgMsg(V, "Creating new node: ", this);
10260 return V;
10261}
10262
10264 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10265 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10266 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10267 bool Indexed = AM != ISD::UNINDEXED;
10268 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10269
10270 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10271 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10272 : getVTList(VT, MVT::Other);
10274 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10275 ID.AddInteger(VT.getRawBits());
10276 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10277 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10278 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10279
10280 void *IP = nullptr;
10281 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10282 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10283 return SDValue(E, 0);
10284 }
10285
10286 auto *N =
10287 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10288 ExtType, IsExpanding, MemVT, MMO);
10289 createOperands(N, Ops);
10290 CSEMap.InsertNode(N, IP);
10291 InsertNode(N);
10292 SDValue V(N, 0);
10293 NewSDValueDbgMsg(V, "Creating new node: ", this);
10294 return V;
10295}
10296
10298 SDValue Ptr, SDValue Stride,
10299 SDValue Mask, SDValue EVL,
10300 MachineMemOperand *MMO,
10301 bool IsExpanding) {
10302 SDValue Undef = getUNDEF(Ptr.getValueType());
10303 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10304 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10305}
10306
10308 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10309 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10310 MachineMemOperand *MMO, bool IsExpanding) {
10311 SDValue Undef = getUNDEF(Ptr.getValueType());
10312 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10313 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10314}
10315
10317 SDValue Val, SDValue Ptr,
10318 SDValue Offset, SDValue Stride,
10319 SDValue Mask, SDValue EVL, EVT MemVT,
10320 MachineMemOperand *MMO,
10322 bool IsTruncating, bool IsCompressing) {
10323 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10324 bool Indexed = AM != ISD::UNINDEXED;
10325 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10326 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10327 : getVTList(MVT::Other);
10328 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10330 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10331 ID.AddInteger(MemVT.getRawBits());
10332 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10333 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10334 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10335 void *IP = nullptr;
10336 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10337 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10338 return SDValue(E, 0);
10339 }
10340 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10341 VTs, AM, IsTruncating,
10342 IsCompressing, MemVT, MMO);
10343 createOperands(N, Ops);
10344
10345 CSEMap.InsertNode(N, IP);
10346 InsertNode(N);
10347 SDValue V(N, 0);
10348 NewSDValueDbgMsg(V, "Creating new node: ", this);
10349 return V;
10350}
10351
10353 SDValue Val, SDValue Ptr,
10354 SDValue Stride, SDValue Mask,
10355 SDValue EVL, EVT SVT,
10356 MachineMemOperand *MMO,
10357 bool IsCompressing) {
10358 EVT VT = Val.getValueType();
10359
10360 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10361 if (VT == SVT)
10362 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10363 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10364 /*IsTruncating*/ false, IsCompressing);
10365
10367 "Should only be a truncating store, not extending!");
10368 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10369 assert(VT.isVector() == SVT.isVector() &&
10370 "Cannot use trunc store to convert to or from a vector!");
10371 assert((!VT.isVector() ||
10373 "Cannot use trunc store to change the number of vector elements!");
10374
10375 SDVTList VTs = getVTList(MVT::Other);
10376 SDValue Undef = getUNDEF(Ptr.getValueType());
10377 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10379 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10380 ID.AddInteger(SVT.getRawBits());
10381 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10382 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10383 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10384 void *IP = nullptr;
10385 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10386 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10387 return SDValue(E, 0);
10388 }
10389 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10390 VTs, ISD::UNINDEXED, true,
10391 IsCompressing, SVT, MMO);
10392 createOperands(N, Ops);
10393
10394 CSEMap.InsertNode(N, IP);
10395 InsertNode(N);
10396 SDValue V(N, 0);
10397 NewSDValueDbgMsg(V, "Creating new node: ", this);
10398 return V;
10399}
10400
10403 ISD::MemIndexType IndexType) {
10404 assert(Ops.size() == 6 && "Incompatible number of operands");
10405
10407 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10408 ID.AddInteger(VT.getRawBits());
10409 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10410 dl.getIROrder(), VTs, VT, MMO, IndexType));
10411 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10412 ID.AddInteger(MMO->getFlags());
10413 void *IP = nullptr;
10414 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10415 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10416 return SDValue(E, 0);
10417 }
10418
10419 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10420 VT, MMO, IndexType);
10421 createOperands(N, Ops);
10422
10423 assert(N->getMask().getValueType().getVectorElementCount() ==
10424 N->getValueType(0).getVectorElementCount() &&
10425 "Vector width mismatch between mask and data");
10426 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10427 N->getValueType(0).getVectorElementCount().isScalable() &&
10428 "Scalable flags of index and data do not match");
10430 N->getIndex().getValueType().getVectorElementCount(),
10431 N->getValueType(0).getVectorElementCount()) &&
10432 "Vector width mismatch between index and data");
10433 assert(isa<ConstantSDNode>(N->getScale()) &&
10434 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10435 "Scale should be a constant power of 2");
10436
10437 CSEMap.InsertNode(N, IP);
10438 InsertNode(N);
10439 SDValue V(N, 0);
10440 NewSDValueDbgMsg(V, "Creating new node: ", this);
10441 return V;
10442}
10443
10446 MachineMemOperand *MMO,
10447 ISD::MemIndexType IndexType) {
10448 assert(Ops.size() == 7 && "Incompatible number of operands");
10449
10451 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10452 ID.AddInteger(VT.getRawBits());
10453 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10454 dl.getIROrder(), VTs, VT, MMO, IndexType));
10455 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10456 ID.AddInteger(MMO->getFlags());
10457 void *IP = nullptr;
10458 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10459 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10460 return SDValue(E, 0);
10461 }
10462 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10463 VT, MMO, IndexType);
10464 createOperands(N, Ops);
10465
10466 assert(N->getMask().getValueType().getVectorElementCount() ==
10467 N->getValue().getValueType().getVectorElementCount() &&
10468 "Vector width mismatch between mask and data");
10469 assert(
10470 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10471 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10472 "Scalable flags of index and data do not match");
10474 N->getIndex().getValueType().getVectorElementCount(),
10475 N->getValue().getValueType().getVectorElementCount()) &&
10476 "Vector width mismatch between index and data");
10477 assert(isa<ConstantSDNode>(N->getScale()) &&
10478 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10479 "Scale should be a constant power of 2");
10480
10481 CSEMap.InsertNode(N, IP);
10482 InsertNode(N);
10483 SDValue V(N, 0);
10484 NewSDValueDbgMsg(V, "Creating new node: ", this);
10485 return V;
10486}
10487
10490 SDValue PassThru, EVT MemVT,
10491 MachineMemOperand *MMO,
10493 ISD::LoadExtType ExtTy, bool isExpanding) {
10494 bool Indexed = AM != ISD::UNINDEXED;
10495 assert((Indexed || Offset.isUndef()) &&
10496 "Unindexed masked load with an offset!");
10497 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10498 : getVTList(VT, MVT::Other);
10499 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10501 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10502 ID.AddInteger(MemVT.getRawBits());
10503 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10504 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10505 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10506 ID.AddInteger(MMO->getFlags());
10507 void *IP = nullptr;
10508 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10509 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10510 return SDValue(E, 0);
10511 }
10512 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10513 AM, ExtTy, isExpanding, MemVT, MMO);
10514 createOperands(N, Ops);
10515
10516 CSEMap.InsertNode(N, IP);
10517 InsertNode(N);
10518 SDValue V(N, 0);
10519 NewSDValueDbgMsg(V, "Creating new node: ", this);
10520 return V;
10521}
10522
10527 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10528 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10529 Offset, LD->getMask(), LD->getPassThru(),
10530 LD->getMemoryVT(), LD->getMemOperand(), AM,
10531 LD->getExtensionType(), LD->isExpandingLoad());
10532}
10533
10536 SDValue Mask, EVT MemVT,
10537 MachineMemOperand *MMO,
10538 ISD::MemIndexedMode AM, bool IsTruncating,
10539 bool IsCompressing) {
10540 assert(Chain.getValueType() == MVT::Other &&
10541 "Invalid chain type");
10542 bool Indexed = AM != ISD::UNINDEXED;
10543 assert((Indexed || Offset.isUndef()) &&
10544 "Unindexed masked store with an offset!");
10545 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10546 : getVTList(MVT::Other);
10547 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10549 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10550 ID.AddInteger(MemVT.getRawBits());
10551 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10552 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10553 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10554 ID.AddInteger(MMO->getFlags());
10555 void *IP = nullptr;
10556 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10557 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10558 return SDValue(E, 0);
10559 }
10560 auto *N =
10561 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10562 IsTruncating, IsCompressing, MemVT, MMO);
10563 createOperands(N, Ops);
10564
10565 CSEMap.InsertNode(N, IP);
10566 InsertNode(N);
10567 SDValue V(N, 0);
10568 NewSDValueDbgMsg(V, "Creating new node: ", this);
10569 return V;
10570}
10571
10576 assert(ST->getOffset().isUndef() &&
10577 "Masked store is already a indexed store!");
10578 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10579 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10580 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10581}
10582
10585 MachineMemOperand *MMO,
10586 ISD::MemIndexType IndexType,
10587 ISD::LoadExtType ExtTy) {
10588 assert(Ops.size() == 6 && "Incompatible number of operands");
10589
10591 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10592 ID.AddInteger(MemVT.getRawBits());
10593 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10594 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10595 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10596 ID.AddInteger(MMO->getFlags());
10597 void *IP = nullptr;
10598 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10599 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10600 return SDValue(E, 0);
10601 }
10602
10603 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10604 VTs, MemVT, MMO, IndexType, ExtTy);
10605 createOperands(N, Ops);
10606
10607 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10608 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10609 assert(N->getMask().getValueType().getVectorElementCount() ==
10610 N->getValueType(0).getVectorElementCount() &&
10611 "Vector width mismatch between mask and data");
10612 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10613 N->getValueType(0).getVectorElementCount().isScalable() &&
10614 "Scalable flags of index and data do not match");
10616 N->getIndex().getValueType().getVectorElementCount(),
10617 N->getValueType(0).getVectorElementCount()) &&
10618 "Vector width mismatch between index and data");
10619 assert(isa<ConstantSDNode>(N->getScale()) &&
10620 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10621 "Scale should be a constant power of 2");
10622
10623 CSEMap.InsertNode(N, IP);
10624 InsertNode(N);
10625 SDValue V(N, 0);
10626 NewSDValueDbgMsg(V, "Creating new node: ", this);
10627 return V;
10628}
10629
10632 MachineMemOperand *MMO,
10633 ISD::MemIndexType IndexType,
10634 bool IsTrunc) {
10635 assert(Ops.size() == 6 && "Incompatible number of operands");
10636
10638 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10639 ID.AddInteger(MemVT.getRawBits());
10640 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10641 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10642 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10643 ID.AddInteger(MMO->getFlags());
10644 void *IP = nullptr;
10645 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10646 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10647 return SDValue(E, 0);
10648 }
10649
10650 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10651 VTs, MemVT, MMO, IndexType, IsTrunc);
10652 createOperands(N, Ops);
10653
10654 assert(N->getMask().getValueType().getVectorElementCount() ==
10655 N->getValue().getValueType().getVectorElementCount() &&
10656 "Vector width mismatch between mask and data");
10657 assert(
10658 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10659 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10660 "Scalable flags of index and data do not match");
10662 N->getIndex().getValueType().getVectorElementCount(),
10663 N->getValue().getValueType().getVectorElementCount()) &&
10664 "Vector width mismatch between index and data");
10665 assert(isa<ConstantSDNode>(N->getScale()) &&
10666 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10667 "Scale should be a constant power of 2");
10668
10669 CSEMap.InsertNode(N, IP);
10670 InsertNode(N);
10671 SDValue V(N, 0);
10672 NewSDValueDbgMsg(V, "Creating new node: ", this);
10673 return V;
10674}
10675
10677 const SDLoc &dl, ArrayRef<SDValue> Ops,
10678 MachineMemOperand *MMO,
10679 ISD::MemIndexType IndexType) {
10680 assert(Ops.size() == 7 && "Incompatible number of operands");
10681
10683 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VECTOR_HISTOGRAM, VTs, Ops);
10684 ID.AddInteger(MemVT.getRawBits());
10685 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10686 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10687 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10688 ID.AddInteger(MMO->getFlags());
10689 void *IP = nullptr;
10690 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10691 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10692 return SDValue(E, 0);
10693 }
10694
10695 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10696 VTs, MemVT, MMO, IndexType);
10697 createOperands(N, Ops);
10698
10699 assert(N->getMask().getValueType().getVectorElementCount() ==
10700 N->getIndex().getValueType().getVectorElementCount() &&
10701 "Vector width mismatch between mask and data");
10702 assert(isa<ConstantSDNode>(N->getScale()) &&
10703 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10704 "Scale should be a constant power of 2");
10705 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10706
10707 CSEMap.InsertNode(N, IP);
10708 InsertNode(N);
10709 SDValue V(N, 0);
10710 NewSDValueDbgMsg(V, "Creating new node: ", this);
10711 return V;
10712}
10713
10715 SDValue Ptr, SDValue Mask, SDValue EVL,
10716 MachineMemOperand *MMO) {
10717 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10718 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10720 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10721 ID.AddInteger(VT.getRawBits());
10722 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10723 VTs, VT, MMO));
10724 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10725 ID.AddInteger(MMO->getFlags());
10726 void *IP = nullptr;
10727 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10728 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10729 return SDValue(E, 0);
10730 }
10731 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10732 VT, MMO);
10733 createOperands(N, Ops);
10734
10735 CSEMap.InsertNode(N, IP);
10736 InsertNode(N);
10737 SDValue V(N, 0);
10738 NewSDValueDbgMsg(V, "Creating new node: ", this);
10739 return V;
10740}
10741
10743 EVT MemVT, MachineMemOperand *MMO) {
10744 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10745 SDVTList VTs = getVTList(MVT::Other);
10746 SDValue Ops[] = {Chain, Ptr};
10748 AddNodeIDNode(ID, ISD::GET_FPENV_MEM, VTs, Ops);
10749 ID.AddInteger(MemVT.getRawBits());
10750 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10751 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10752 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10753 ID.AddInteger(MMO->getFlags());
10754 void *IP = nullptr;
10755 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10756 return SDValue(E, 0);
10757
10758 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10759 dl.getDebugLoc(), VTs, MemVT, MMO);
10760 createOperands(N, Ops);
10761
10762 CSEMap.InsertNode(N, IP);
10763 InsertNode(N);
10764 SDValue V(N, 0);
10765 NewSDValueDbgMsg(V, "Creating new node: ", this);
10766 return V;
10767}
10768
10770 EVT MemVT, MachineMemOperand *MMO) {
10771 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10772 SDVTList VTs = getVTList(MVT::Other);
10773 SDValue Ops[] = {Chain, Ptr};
10775 AddNodeIDNode(ID, ISD::SET_FPENV_MEM, VTs, Ops);
10776 ID.AddInteger(MemVT.getRawBits());
10777 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10778 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10779 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10780 ID.AddInteger(MMO->getFlags());
10781 void *IP = nullptr;
10782 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10783 return SDValue(E, 0);
10784
10785 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10786 dl.getDebugLoc(), VTs, MemVT, MMO);
10787 createOperands(N, Ops);
10788
10789 CSEMap.InsertNode(N, IP);
10790 InsertNode(N);
10791 SDValue V(N, 0);
10792 NewSDValueDbgMsg(V, "Creating new node: ", this);
10793 return V;
10794}
10795
10797 // select undef, T, F --> T (if T is a constant), otherwise F
10798 // select, ?, undef, F --> F
10799 // select, ?, T, undef --> T
10800 if (Cond.isUndef())
10801 return isConstantValueOfAnyType(T) ? T : F;
10802 if (T.isUndef())
10803 return F;
10804 if (F.isUndef())
10805 return T;
10806
10807 // select true, T, F --> T
10808 // select false, T, F --> F
10809 if (auto C = isBoolConstant(Cond))
10810 return *C ? T : F;
10811
10812 // select ?, T, T --> T
10813 if (T == F)
10814 return T;
10815
10816 return SDValue();
10817}
10818
10820 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10821 if (X.isUndef())
10822 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10823 // shift X, undef --> undef (because it may shift by the bitwidth)
10824 if (Y.isUndef())
10825 return getUNDEF(X.getValueType());
10826
10827 // shift 0, Y --> 0
10828 // shift X, 0 --> X
10830 return X;
10831
10832 // shift X, C >= bitwidth(X) --> undef
10833 // All vector elements must be too big (or undef) to avoid partial undefs.
10834 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10835 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10836 };
10837 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10838 return getUNDEF(X.getValueType());
10839
10840 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10841 if (X.getValueType().getScalarType() == MVT::i1)
10842 return X;
10843
10844 return SDValue();
10845}
10846
10848 SDNodeFlags Flags) {
10849 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10850 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10851 // operation is poison. That result can be relaxed to undef.
10852 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10853 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10854 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10855 (YC && YC->getValueAPF().isNaN());
10856 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10857 (YC && YC->getValueAPF().isInfinity());
10858
10859 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10860 return getUNDEF(X.getValueType());
10861
10862 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10863 return getUNDEF(X.getValueType());
10864
10865 if (!YC)
10866 return SDValue();
10867
10868 // X + -0.0 --> X
10869 if (Opcode == ISD::FADD)
10870 if (YC->getValueAPF().isNegZero())
10871 return X;
10872
10873 // X - +0.0 --> X
10874 if (Opcode == ISD::FSUB)
10875 if (YC->getValueAPF().isPosZero())
10876 return X;
10877
10878 // X * 1.0 --> X
10879 // X / 1.0 --> X
10880 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10881 if (YC->getValueAPF().isExactlyValue(1.0))
10882 return X;
10883
10884 // X * 0.0 --> 0.0
10885 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10886 if (YC->getValueAPF().isZero())
10887 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10888
10889 return SDValue();
10890}
10891
10893 SDValue Ptr, SDValue SV, unsigned Align) {
10894 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10895 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10896}
10897
10898SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10900 switch (Ops.size()) {
10901 case 0: return getNode(Opcode, DL, VT);
10902 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10903 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10904 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10905 default: break;
10906 }
10907
10908 // Copy from an SDUse array into an SDValue array for use with
10909 // the regular getNode logic.
10911 return getNode(Opcode, DL, VT, NewOps);
10912}
10913
10914SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10916 SDNodeFlags Flags;
10917 if (Inserter)
10918 Flags = Inserter->getFlags();
10919 return getNode(Opcode, DL, VT, Ops, Flags);
10920}
10921
10922SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10923 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10924 unsigned NumOps = Ops.size();
10925 switch (NumOps) {
10926 case 0: return getNode(Opcode, DL, VT);
10927 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10928 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10929 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10930 default: break;
10931 }
10932
10933#ifndef NDEBUG
10934 for (const auto &Op : Ops)
10935 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10936 "Operand is DELETED_NODE!");
10937#endif
10938
10939 switch (Opcode) {
10940 default: break;
10941 case ISD::BUILD_VECTOR:
10942 // Attempt to simplify BUILD_VECTOR.
10943 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10944 return V;
10945 break;
10947 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10948 return V;
10949 break;
10950 case ISD::SELECT_CC:
10951 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10952 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10953 "LHS and RHS of condition must have same type!");
10954 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10955 "True and False arms of SelectCC must have same type!");
10956 assert(Ops[2].getValueType() == VT &&
10957 "select_cc node must be of same type as true and false value!");
10958 assert((!Ops[0].getValueType().isVector() ||
10959 Ops[0].getValueType().getVectorElementCount() ==
10960 VT.getVectorElementCount()) &&
10961 "Expected select_cc with vector result to have the same sized "
10962 "comparison type!");
10963 break;
10964 case ISD::BR_CC:
10965 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10966 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10967 "LHS/RHS of comparison should match types!");
10968 break;
10969 case ISD::VP_ADD:
10970 case ISD::VP_SUB:
10971 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10972 if (VT.getScalarType() == MVT::i1)
10973 Opcode = ISD::VP_XOR;
10974 break;
10975 case ISD::VP_MUL:
10976 // If it is VP_MUL mask operation then turn it to VP_AND
10977 if (VT.getScalarType() == MVT::i1)
10978 Opcode = ISD::VP_AND;
10979 break;
10980 case ISD::VP_REDUCE_MUL:
10981 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10982 if (VT == MVT::i1)
10983 Opcode = ISD::VP_REDUCE_AND;
10984 break;
10985 case ISD::VP_REDUCE_ADD:
10986 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10987 if (VT == MVT::i1)
10988 Opcode = ISD::VP_REDUCE_XOR;
10989 break;
10990 case ISD::VP_REDUCE_SMAX:
10991 case ISD::VP_REDUCE_UMIN:
10992 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10993 // VP_REDUCE_AND.
10994 if (VT == MVT::i1)
10995 Opcode = ISD::VP_REDUCE_AND;
10996 break;
10997 case ISD::VP_REDUCE_SMIN:
10998 case ISD::VP_REDUCE_UMAX:
10999 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11000 // VP_REDUCE_OR.
11001 if (VT == MVT::i1)
11002 Opcode = ISD::VP_REDUCE_OR;
11003 break;
11004 }
11005
11006 // Memoize nodes.
11007 SDNode *N;
11008 SDVTList VTs = getVTList(VT);
11009
11010 if (VT != MVT::Glue) {
11012 AddNodeIDNode(ID, Opcode, VTs, Ops);
11013 void *IP = nullptr;
11014
11015 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11016 E->intersectFlagsWith(Flags);
11017 return SDValue(E, 0);
11018 }
11019
11020 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11021 createOperands(N, Ops);
11022
11023 CSEMap.InsertNode(N, IP);
11024 } else {
11025 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11026 createOperands(N, Ops);
11027 }
11028
11029 N->setFlags(Flags);
11030 InsertNode(N);
11031 SDValue V(N, 0);
11032 NewSDValueDbgMsg(V, "Creating new node: ", this);
11033 return V;
11034}
11035
11036SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11037 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11038 SDNodeFlags Flags;
11039 if (Inserter)
11040 Flags = Inserter->getFlags();
11041 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11042}
11043
11044SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11046 const SDNodeFlags Flags) {
11047 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11048}
11049
11050SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11052 SDNodeFlags Flags;
11053 if (Inserter)
11054 Flags = Inserter->getFlags();
11055 return getNode(Opcode, DL, VTList, Ops, Flags);
11056}
11057
11058SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11059 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11060 if (VTList.NumVTs == 1)
11061 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11062
11063#ifndef NDEBUG
11064 for (const auto &Op : Ops)
11065 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11066 "Operand is DELETED_NODE!");
11067#endif
11068
11069 switch (Opcode) {
11070 case ISD::SADDO:
11071 case ISD::UADDO:
11072 case ISD::SSUBO:
11073 case ISD::USUBO: {
11074 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11075 "Invalid add/sub overflow op!");
11076 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11077 Ops[0].getValueType() == Ops[1].getValueType() &&
11078 Ops[0].getValueType() == VTList.VTs[0] &&
11079 "Binary operator types must match!");
11080 SDValue N1 = Ops[0], N2 = Ops[1];
11081 canonicalizeCommutativeBinop(Opcode, N1, N2);
11082
11083 // (X +- 0) -> X with zero-overflow.
11084 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11085 /*AllowTruncation*/ true);
11086 if (N2CV && N2CV->isZero()) {
11087 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11088 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11089 }
11090
11091 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11092 VTList.VTs[1].getScalarType() == MVT::i1) {
11093 SDValue F1 = getFreeze(N1);
11094 SDValue F2 = getFreeze(N2);
11095 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11096 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11097 return getNode(ISD::MERGE_VALUES, DL, VTList,
11098 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11099 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11100 Flags);
11101 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11102 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11103 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11104 return getNode(ISD::MERGE_VALUES, DL, VTList,
11105 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11106 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11107 Flags);
11108 }
11109 }
11110 break;
11111 }
11112 case ISD::SADDO_CARRY:
11113 case ISD::UADDO_CARRY:
11114 case ISD::SSUBO_CARRY:
11115 case ISD::USUBO_CARRY:
11116 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11117 "Invalid add/sub overflow op!");
11118 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11119 Ops[0].getValueType() == Ops[1].getValueType() &&
11120 Ops[0].getValueType() == VTList.VTs[0] &&
11121 Ops[2].getValueType() == VTList.VTs[1] &&
11122 "Binary operator types must match!");
11123 break;
11124 case ISD::SMUL_LOHI:
11125 case ISD::UMUL_LOHI: {
11126 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11127 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11128 VTList.VTs[0] == Ops[0].getValueType() &&
11129 VTList.VTs[0] == Ops[1].getValueType() &&
11130 "Binary operator types must match!");
11131 // Constant fold.
11134 if (LHS && RHS) {
11135 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11136 unsigned OutWidth = Width * 2;
11137 APInt Val = LHS->getAPIntValue();
11138 APInt Mul = RHS->getAPIntValue();
11139 if (Opcode == ISD::SMUL_LOHI) {
11140 Val = Val.sext(OutWidth);
11141 Mul = Mul.sext(OutWidth);
11142 } else {
11143 Val = Val.zext(OutWidth);
11144 Mul = Mul.zext(OutWidth);
11145 }
11146 Val *= Mul;
11147
11148 SDValue Hi =
11149 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11150 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11151 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11152 }
11153 break;
11154 }
11155 case ISD::FFREXP: {
11156 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11157 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11158 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11159
11161 int FrexpExp;
11162 APFloat FrexpMant =
11163 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11164 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11165 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11166 DL, VTList.VTs[1]);
11167 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11168 }
11169
11170 break;
11171 }
11173 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11174 "Invalid STRICT_FP_EXTEND!");
11175 assert(VTList.VTs[0].isFloatingPoint() &&
11176 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11177 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11178 "STRICT_FP_EXTEND result type should be vector iff the operand "
11179 "type is vector!");
11180 assert((!VTList.VTs[0].isVector() ||
11181 VTList.VTs[0].getVectorElementCount() ==
11182 Ops[1].getValueType().getVectorElementCount()) &&
11183 "Vector element count mismatch!");
11184 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11185 "Invalid fpext node, dst <= src!");
11186 break;
11188 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11189 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11190 "STRICT_FP_ROUND result type should be vector iff the operand "
11191 "type is vector!");
11192 assert((!VTList.VTs[0].isVector() ||
11193 VTList.VTs[0].getVectorElementCount() ==
11194 Ops[1].getValueType().getVectorElementCount()) &&
11195 "Vector element count mismatch!");
11196 assert(VTList.VTs[0].isFloatingPoint() &&
11197 Ops[1].getValueType().isFloatingPoint() &&
11198 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11199 Ops[2].getOpcode() == ISD::TargetConstant &&
11200 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11201 "Invalid STRICT_FP_ROUND!");
11202 break;
11203 }
11204
11205 // Memoize the node unless it returns a glue result.
11206 SDNode *N;
11207 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11209 AddNodeIDNode(ID, Opcode, VTList, Ops);
11210 void *IP = nullptr;
11211 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11212 E->intersectFlagsWith(Flags);
11213 return SDValue(E, 0);
11214 }
11215
11216 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11217 createOperands(N, Ops);
11218 CSEMap.InsertNode(N, IP);
11219 } else {
11220 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11221 createOperands(N, Ops);
11222 }
11223
11224 N->setFlags(Flags);
11225 InsertNode(N);
11226 SDValue V(N, 0);
11227 NewSDValueDbgMsg(V, "Creating new node: ", this);
11228 return V;
11229}
11230
11231SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11232 SDVTList VTList) {
11233 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11234}
11235
11236SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11237 SDValue N1) {
11238 SDValue Ops[] = { N1 };
11239 return getNode(Opcode, DL, VTList, Ops);
11240}
11241
11242SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11243 SDValue N1, SDValue N2) {
11244 SDValue Ops[] = { N1, N2 };
11245 return getNode(Opcode, DL, VTList, Ops);
11246}
11247
11248SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11249 SDValue N1, SDValue N2, SDValue N3) {
11250 SDValue Ops[] = { N1, N2, N3 };
11251 return getNode(Opcode, DL, VTList, Ops);
11252}
11253
11254SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11255 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11256 SDValue Ops[] = { N1, N2, N3, N4 };
11257 return getNode(Opcode, DL, VTList, Ops);
11258}
11259
11260SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11261 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11262 SDValue N5) {
11263 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11264 return getNode(Opcode, DL, VTList, Ops);
11265}
11266
11268 if (!VT.isExtended())
11269 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11270
11271 return makeVTList(&(*EVTs.insert(VT).first), 1);
11272}
11273
11276 ID.AddInteger(2U);
11277 ID.AddInteger(VT1.getRawBits());
11278 ID.AddInteger(VT2.getRawBits());
11279
11280 void *IP = nullptr;
11281 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11282 if (!Result) {
11283 EVT *Array = Allocator.Allocate<EVT>(2);
11284 Array[0] = VT1;
11285 Array[1] = VT2;
11286 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11287 VTListMap.InsertNode(Result, IP);
11288 }
11289 return Result->getSDVTList();
11290}
11291
11294 ID.AddInteger(3U);
11295 ID.AddInteger(VT1.getRawBits());
11296 ID.AddInteger(VT2.getRawBits());
11297 ID.AddInteger(VT3.getRawBits());
11298
11299 void *IP = nullptr;
11300 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11301 if (!Result) {
11302 EVT *Array = Allocator.Allocate<EVT>(3);
11303 Array[0] = VT1;
11304 Array[1] = VT2;
11305 Array[2] = VT3;
11306 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11307 VTListMap.InsertNode(Result, IP);
11308 }
11309 return Result->getSDVTList();
11310}
11311
11314 ID.AddInteger(4U);
11315 ID.AddInteger(VT1.getRawBits());
11316 ID.AddInteger(VT2.getRawBits());
11317 ID.AddInteger(VT3.getRawBits());
11318 ID.AddInteger(VT4.getRawBits());
11319
11320 void *IP = nullptr;
11321 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11322 if (!Result) {
11323 EVT *Array = Allocator.Allocate<EVT>(4);
11324 Array[0] = VT1;
11325 Array[1] = VT2;
11326 Array[2] = VT3;
11327 Array[3] = VT4;
11328 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11329 VTListMap.InsertNode(Result, IP);
11330 }
11331 return Result->getSDVTList();
11332}
11333
11335 unsigned NumVTs = VTs.size();
11337 ID.AddInteger(NumVTs);
11338 for (unsigned index = 0; index < NumVTs; index++) {
11339 ID.AddInteger(VTs[index].getRawBits());
11340 }
11341
11342 void *IP = nullptr;
11343 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11344 if (!Result) {
11345 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11346 llvm::copy(VTs, Array);
11347 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11348 VTListMap.InsertNode(Result, IP);
11349 }
11350 return Result->getSDVTList();
11351}
11352
11353
11354/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11355/// specified operands. If the resultant node already exists in the DAG,
11356/// this does not modify the specified node, instead it returns the node that
11357/// already exists. If the resultant node does not exist in the DAG, the
11358/// input node is returned. As a degenerate case, if you specify the same
11359/// input operands as the node already has, the input node is returned.
11361 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11362
11363 // Check to see if there is no change.
11364 if (Op == N->getOperand(0)) return N;
11365
11366 // See if the modified node already exists.
11367 void *InsertPos = nullptr;
11368 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11369 return Existing;
11370
11371 // Nope it doesn't. Remove the node from its current place in the maps.
11372 if (InsertPos)
11373 if (!RemoveNodeFromCSEMaps(N))
11374 InsertPos = nullptr;
11375
11376 // Now we update the operands.
11377 N->OperandList[0].set(Op);
11378
11380 // If this gets put into a CSE map, add it.
11381 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11382 return N;
11383}
11384
11386 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11387
11388 // Check to see if there is no change.
11389 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11390 return N; // No operands changed, just return the input node.
11391
11392 // See if the modified node already exists.
11393 void *InsertPos = nullptr;
11394 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11395 return Existing;
11396
11397 // Nope it doesn't. Remove the node from its current place in the maps.
11398 if (InsertPos)
11399 if (!RemoveNodeFromCSEMaps(N))
11400 InsertPos = nullptr;
11401
11402 // Now we update the operands.
11403 if (N->OperandList[0] != Op1)
11404 N->OperandList[0].set(Op1);
11405 if (N->OperandList[1] != Op2)
11406 N->OperandList[1].set(Op2);
11407
11409 // If this gets put into a CSE map, add it.
11410 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11411 return N;
11412}
11413
11416 SDValue Ops[] = { Op1, Op2, Op3 };
11417 return UpdateNodeOperands(N, Ops);
11418}
11419
11422 SDValue Op3, SDValue Op4) {
11423 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11424 return UpdateNodeOperands(N, Ops);
11425}
11426
11429 SDValue Op3, SDValue Op4, SDValue Op5) {
11430 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11431 return UpdateNodeOperands(N, Ops);
11432}
11433
11436 unsigned NumOps = Ops.size();
11437 assert(N->getNumOperands() == NumOps &&
11438 "Update with wrong number of operands");
11439
11440 // If no operands changed just return the input node.
11441 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11442 return N;
11443
11444 // See if the modified node already exists.
11445 void *InsertPos = nullptr;
11446 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11447 return Existing;
11448
11449 // Nope it doesn't. Remove the node from its current place in the maps.
11450 if (InsertPos)
11451 if (!RemoveNodeFromCSEMaps(N))
11452 InsertPos = nullptr;
11453
11454 // Now we update the operands.
11455 for (unsigned i = 0; i != NumOps; ++i)
11456 if (N->OperandList[i] != Ops[i])
11457 N->OperandList[i].set(Ops[i]);
11458
11460 // If this gets put into a CSE map, add it.
11461 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11462 return N;
11463}
11464
11465/// DropOperands - Release the operands and set this node to have
11466/// zero operands.
11468 // Unlike the code in MorphNodeTo that does this, we don't need to
11469 // watch for dead nodes here.
11470 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11471 SDUse &Use = *I++;
11472 Use.set(SDValue());
11473 }
11474}
11475
11477 ArrayRef<MachineMemOperand *> NewMemRefs) {
11478 if (NewMemRefs.empty()) {
11479 N->clearMemRefs();
11480 return;
11481 }
11482
11483 // Check if we can avoid allocating by storing a single reference directly.
11484 if (NewMemRefs.size() == 1) {
11485 N->MemRefs = NewMemRefs[0];
11486 N->NumMemRefs = 1;
11487 return;
11488 }
11489
11490 MachineMemOperand **MemRefsBuffer =
11491 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11492 llvm::copy(NewMemRefs, MemRefsBuffer);
11493 N->MemRefs = MemRefsBuffer;
11494 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11495}
11496
11497/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11498/// machine opcode.
11499///
11501 EVT VT) {
11502 SDVTList VTs = getVTList(VT);
11503 return SelectNodeTo(N, MachineOpc, VTs, {});
11504}
11505
11507 EVT VT, SDValue Op1) {
11508 SDVTList VTs = getVTList(VT);
11509 SDValue Ops[] = { Op1 };
11510 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11511}
11512
11514 EVT VT, SDValue Op1,
11515 SDValue Op2) {
11516 SDVTList VTs = getVTList(VT);
11517 SDValue Ops[] = { Op1, Op2 };
11518 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11519}
11520
11522 EVT VT, SDValue Op1,
11523 SDValue Op2, SDValue Op3) {
11524 SDVTList VTs = getVTList(VT);
11525 SDValue Ops[] = { Op1, Op2, Op3 };
11526 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11527}
11528
11531 SDVTList VTs = getVTList(VT);
11532 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11533}
11534
11536 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11537 SDVTList VTs = getVTList(VT1, VT2);
11538 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11539}
11540
11542 EVT VT1, EVT VT2) {
11543 SDVTList VTs = getVTList(VT1, VT2);
11544 return SelectNodeTo(N, MachineOpc, VTs, {});
11545}
11546
11548 EVT VT1, EVT VT2, EVT VT3,
11550 SDVTList VTs = getVTList(VT1, VT2, VT3);
11551 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11552}
11553
11555 EVT VT1, EVT VT2,
11556 SDValue Op1, SDValue Op2) {
11557 SDVTList VTs = getVTList(VT1, VT2);
11558 SDValue Ops[] = { Op1, Op2 };
11559 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11560}
11561
11564 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11565 // Reset the NodeID to -1.
11566 New->setNodeId(-1);
11567 if (New != N) {
11568 ReplaceAllUsesWith(N, New);
11570 }
11571 return New;
11572}
11573
11574/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11575/// the line number information on the merged node since it is not possible to
11576/// preserve the information that operation is associated with multiple lines.
11577/// This will make the debugger working better at -O0, were there is a higher
11578/// probability having other instructions associated with that line.
11579///
11580/// For IROrder, we keep the smaller of the two
11581SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11582 DebugLoc NLoc = N->getDebugLoc();
11583 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11584 N->setDebugLoc(DebugLoc());
11585 }
11586 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11587 N->setIROrder(Order);
11588 return N;
11589}
11590
11591/// MorphNodeTo - This *mutates* the specified node to have the specified
11592/// return type, opcode, and operands.
11593///
11594/// Note that MorphNodeTo returns the resultant node. If there is already a
11595/// node of the specified opcode and operands, it returns that node instead of
11596/// the current one. Note that the SDLoc need not be the same.
11597///
11598/// Using MorphNodeTo is faster than creating a new node and swapping it in
11599/// with ReplaceAllUsesWith both because it often avoids allocating a new
11600/// node, and because it doesn't require CSE recalculation for any of
11601/// the node's users.
11602///
11603/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11604/// As a consequence it isn't appropriate to use from within the DAG combiner or
11605/// the legalizer which maintain worklists that would need to be updated when
11606/// deleting things.
11609 // If an identical node already exists, use it.
11610 void *IP = nullptr;
11611 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11613 AddNodeIDNode(ID, Opc, VTs, Ops);
11614 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11615 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11616 }
11617
11618 if (!RemoveNodeFromCSEMaps(N))
11619 IP = nullptr;
11620
11621 // Start the morphing.
11622 N->NodeType = Opc;
11623 N->ValueList = VTs.VTs;
11624 N->NumValues = VTs.NumVTs;
11625
11626 // Clear the operands list, updating used nodes to remove this from their
11627 // use list. Keep track of any operands that become dead as a result.
11628 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11629 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11630 SDUse &Use = *I++;
11631 SDNode *Used = Use.getNode();
11632 Use.set(SDValue());
11633 if (Used->use_empty())
11634 DeadNodeSet.insert(Used);
11635 }
11636
11637 // For MachineNode, initialize the memory references information.
11639 MN->clearMemRefs();
11640
11641 // Swap for an appropriately sized array from the recycler.
11642 removeOperands(N);
11643 createOperands(N, Ops);
11644
11645 // Delete any nodes that are still dead after adding the uses for the
11646 // new operands.
11647 if (!DeadNodeSet.empty()) {
11648 SmallVector<SDNode *, 16> DeadNodes;
11649 for (SDNode *N : DeadNodeSet)
11650 if (N->use_empty())
11651 DeadNodes.push_back(N);
11652 RemoveDeadNodes(DeadNodes);
11653 }
11654
11655 if (IP)
11656 CSEMap.InsertNode(N, IP); // Memoize the new node.
11657 return N;
11658}
11659
11661 unsigned OrigOpc = Node->getOpcode();
11662 unsigned NewOpc;
11663 switch (OrigOpc) {
11664 default:
11665 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11666#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11667 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11668#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11669 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11670#include "llvm/IR/ConstrainedOps.def"
11671 }
11672
11673 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11674
11675 // We're taking this node out of the chain, so we need to re-link things.
11676 SDValue InputChain = Node->getOperand(0);
11677 SDValue OutputChain = SDValue(Node, 1);
11678 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11679
11681 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11682 Ops.push_back(Node->getOperand(i));
11683
11684 SDVTList VTs = getVTList(Node->getValueType(0));
11685 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11686
11687 // MorphNodeTo can operate in two ways: if an existing node with the
11688 // specified operands exists, it can just return it. Otherwise, it
11689 // updates the node in place to have the requested operands.
11690 if (Res == Node) {
11691 // If we updated the node in place, reset the node ID. To the isel,
11692 // this should be just like a newly allocated machine node.
11693 Res->setNodeId(-1);
11694 } else {
11697 }
11698
11699 return Res;
11700}
11701
11702/// getMachineNode - These are used for target selectors to create a new node
11703/// with specified return type(s), MachineInstr opcode, and operands.
11704///
11705/// Note that getMachineNode returns the resultant node. If there is already a
11706/// node of the specified opcode and operands, it returns that node instead of
11707/// the current one.
11709 EVT VT) {
11710 SDVTList VTs = getVTList(VT);
11711 return getMachineNode(Opcode, dl, VTs, {});
11712}
11713
11715 EVT VT, SDValue Op1) {
11716 SDVTList VTs = getVTList(VT);
11717 SDValue Ops[] = { Op1 };
11718 return getMachineNode(Opcode, dl, VTs, Ops);
11719}
11720
11722 EVT VT, SDValue Op1, SDValue Op2) {
11723 SDVTList VTs = getVTList(VT);
11724 SDValue Ops[] = { Op1, Op2 };
11725 return getMachineNode(Opcode, dl, VTs, Ops);
11726}
11727
11729 EVT VT, SDValue Op1, SDValue Op2,
11730 SDValue Op3) {
11731 SDVTList VTs = getVTList(VT);
11732 SDValue Ops[] = { Op1, Op2, Op3 };
11733 return getMachineNode(Opcode, dl, VTs, Ops);
11734}
11735
11738 SDVTList VTs = getVTList(VT);
11739 return getMachineNode(Opcode, dl, VTs, Ops);
11740}
11741
11743 EVT VT1, EVT VT2, SDValue Op1,
11744 SDValue Op2) {
11745 SDVTList VTs = getVTList(VT1, VT2);
11746 SDValue Ops[] = { Op1, Op2 };
11747 return getMachineNode(Opcode, dl, VTs, Ops);
11748}
11749
11751 EVT VT1, EVT VT2, SDValue Op1,
11752 SDValue Op2, SDValue Op3) {
11753 SDVTList VTs = getVTList(VT1, VT2);
11754 SDValue Ops[] = { Op1, Op2, Op3 };
11755 return getMachineNode(Opcode, dl, VTs, Ops);
11756}
11757
11759 EVT VT1, EVT VT2,
11761 SDVTList VTs = getVTList(VT1, VT2);
11762 return getMachineNode(Opcode, dl, VTs, Ops);
11763}
11764
11766 EVT VT1, EVT VT2, EVT VT3,
11767 SDValue Op1, SDValue Op2) {
11768 SDVTList VTs = getVTList(VT1, VT2, VT3);
11769 SDValue Ops[] = { Op1, Op2 };
11770 return getMachineNode(Opcode, dl, VTs, Ops);
11771}
11772
11774 EVT VT1, EVT VT2, EVT VT3,
11775 SDValue Op1, SDValue Op2,
11776 SDValue Op3) {
11777 SDVTList VTs = getVTList(VT1, VT2, VT3);
11778 SDValue Ops[] = { Op1, Op2, Op3 };
11779 return getMachineNode(Opcode, dl, VTs, Ops);
11780}
11781
11783 EVT VT1, EVT VT2, EVT VT3,
11785 SDVTList VTs = getVTList(VT1, VT2, VT3);
11786 return getMachineNode(Opcode, dl, VTs, Ops);
11787}
11788
11790 ArrayRef<EVT> ResultTys,
11792 SDVTList VTs = getVTList(ResultTys);
11793 return getMachineNode(Opcode, dl, VTs, Ops);
11794}
11795
11797 SDVTList VTs,
11799 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11801 void *IP = nullptr;
11802
11803 if (DoCSE) {
11805 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11806 IP = nullptr;
11807 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11808 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11809 }
11810 }
11811
11812 // Allocate a new MachineSDNode.
11813 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11814 createOperands(N, Ops);
11815
11816 if (DoCSE)
11817 CSEMap.InsertNode(N, IP);
11818
11819 InsertNode(N);
11820 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11821 return N;
11822}
11823
11824/// getTargetExtractSubreg - A convenience function for creating
11825/// TargetOpcode::EXTRACT_SUBREG nodes.
11827 SDValue Operand) {
11828 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11829 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11830 VT, Operand, SRIdxVal);
11831 return SDValue(Subreg, 0);
11832}
11833
11834/// getTargetInsertSubreg - A convenience function for creating
11835/// TargetOpcode::INSERT_SUBREG nodes.
11837 SDValue Operand, SDValue Subreg) {
11838 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11839 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11840 VT, Operand, Subreg, SRIdxVal);
11841 return SDValue(Result, 0);
11842}
11843
11844/// getNodeIfExists - Get the specified node if it's already available, or
11845/// else return NULL.
11848 bool AllowCommute) {
11849 SDNodeFlags Flags;
11850 if (Inserter)
11851 Flags = Inserter->getFlags();
11852 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11853}
11854
11857 const SDNodeFlags Flags,
11858 bool AllowCommute) {
11859 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11860 return nullptr;
11861
11862 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11864 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11865 void *IP = nullptr;
11866 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11867 E->intersectFlagsWith(Flags);
11868 return E;
11869 }
11870 return nullptr;
11871 };
11872
11873 if (SDNode *Existing = Lookup(Ops))
11874 return Existing;
11875
11876 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11877 return Lookup({Ops[1], Ops[0]});
11878
11879 return nullptr;
11880}
11881
11882/// doesNodeExist - Check if a node exists without modifying its flags.
11883bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11885 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11887 AddNodeIDNode(ID, Opcode, VTList, Ops);
11888 void *IP = nullptr;
11889 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11890 return true;
11891 }
11892 return false;
11893}
11894
11895/// getDbgValue - Creates a SDDbgValue node.
11896///
11897/// SDNode
11899 SDNode *N, unsigned R, bool IsIndirect,
11900 const DebugLoc &DL, unsigned O) {
11901 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11902 "Expected inlined-at fields to agree");
11903 return new (DbgInfo->getAlloc())
11904 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11905 {}, IsIndirect, DL, O,
11906 /*IsVariadic=*/false);
11907}
11908
11909/// Constant
11911 DIExpression *Expr,
11912 const Value *C,
11913 const DebugLoc &DL, unsigned O) {
11914 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11915 "Expected inlined-at fields to agree");
11916 return new (DbgInfo->getAlloc())
11917 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11918 /*IsIndirect=*/false, DL, O,
11919 /*IsVariadic=*/false);
11920}
11921
11922/// FrameIndex
11924 DIExpression *Expr, unsigned FI,
11925 bool IsIndirect,
11926 const DebugLoc &DL,
11927 unsigned O) {
11928 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11929 "Expected inlined-at fields to agree");
11930 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11931}
11932
11933/// FrameIndex with dependencies
11935 DIExpression *Expr, unsigned FI,
11936 ArrayRef<SDNode *> Dependencies,
11937 bool IsIndirect,
11938 const DebugLoc &DL,
11939 unsigned O) {
11940 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11941 "Expected inlined-at fields to agree");
11942 return new (DbgInfo->getAlloc())
11943 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11944 Dependencies, IsIndirect, DL, O,
11945 /*IsVariadic=*/false);
11946}
11947
11948/// VReg
11950 Register VReg, bool IsIndirect,
11951 const DebugLoc &DL, unsigned O) {
11952 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11953 "Expected inlined-at fields to agree");
11954 return new (DbgInfo->getAlloc())
11955 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11956 {}, IsIndirect, DL, O,
11957 /*IsVariadic=*/false);
11958}
11959
11962 ArrayRef<SDNode *> Dependencies,
11963 bool IsIndirect, const DebugLoc &DL,
11964 unsigned O, bool IsVariadic) {
11965 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11966 "Expected inlined-at fields to agree");
11967 return new (DbgInfo->getAlloc())
11968 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11969 DL, O, IsVariadic);
11970}
11971
11973 unsigned OffsetInBits, unsigned SizeInBits,
11974 bool InvalidateDbg) {
11975 SDNode *FromNode = From.getNode();
11976 SDNode *ToNode = To.getNode();
11977 assert(FromNode && ToNode && "Can't modify dbg values");
11978
11979 // PR35338
11980 // TODO: assert(From != To && "Redundant dbg value transfer");
11981 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11982 if (From == To || FromNode == ToNode)
11983 return;
11984
11985 if (!FromNode->getHasDebugValue())
11986 return;
11987
11988 SDDbgOperand FromLocOp =
11989 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11991
11993 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11994 if (Dbg->isInvalidated())
11995 continue;
11996
11997 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11998
11999 // Create a new location ops vector that is equal to the old vector, but
12000 // with each instance of FromLocOp replaced with ToLocOp.
12001 bool Changed = false;
12002 auto NewLocOps = Dbg->copyLocationOps();
12003 std::replace_if(
12004 NewLocOps.begin(), NewLocOps.end(),
12005 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12006 bool Match = Op == FromLocOp;
12007 Changed |= Match;
12008 return Match;
12009 },
12010 ToLocOp);
12011 // Ignore this SDDbgValue if we didn't find a matching location.
12012 if (!Changed)
12013 continue;
12014
12015 DIVariable *Var = Dbg->getVariable();
12016 auto *Expr = Dbg->getExpression();
12017 // If a fragment is requested, update the expression.
12018 if (SizeInBits) {
12019 // When splitting a larger (e.g., sign-extended) value whose
12020 // lower bits are described with an SDDbgValue, do not attempt
12021 // to transfer the SDDbgValue to the upper bits.
12022 if (auto FI = Expr->getFragmentInfo())
12023 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12024 continue;
12025 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12026 SizeInBits);
12027 if (!Fragment)
12028 continue;
12029 Expr = *Fragment;
12030 }
12031
12032 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12033 // Clone the SDDbgValue and move it to To.
12034 SDDbgValue *Clone = getDbgValueList(
12035 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12036 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12037 Dbg->isVariadic());
12038 ClonedDVs.push_back(Clone);
12039
12040 if (InvalidateDbg) {
12041 // Invalidate value and indicate the SDDbgValue should not be emitted.
12042 Dbg->setIsInvalidated();
12043 Dbg->setIsEmitted();
12044 }
12045 }
12046
12047 for (SDDbgValue *Dbg : ClonedDVs) {
12048 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12049 "Transferred DbgValues should depend on the new SDNode");
12050 AddDbgValue(Dbg, false);
12051 }
12052}
12053
12055 if (!N.getHasDebugValue())
12056 return;
12057
12058 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12059 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12060 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12061 return SDDbgOperand::fromNode(Node, ResNo);
12062 };
12063
12065 for (auto *DV : GetDbgValues(&N)) {
12066 if (DV->isInvalidated())
12067 continue;
12068 switch (N.getOpcode()) {
12069 default:
12070 break;
12071 case ISD::ADD: {
12072 SDValue N0 = N.getOperand(0);
12073 SDValue N1 = N.getOperand(1);
12074 if (!isa<ConstantSDNode>(N0)) {
12075 bool RHSConstant = isa<ConstantSDNode>(N1);
12077 if (RHSConstant)
12078 Offset = N.getConstantOperandVal(1);
12079 // We are not allowed to turn indirect debug values variadic, so
12080 // don't salvage those.
12081 if (!RHSConstant && DV->isIndirect())
12082 continue;
12083
12084 // Rewrite an ADD constant node into a DIExpression. Since we are
12085 // performing arithmetic to compute the variable's *value* in the
12086 // DIExpression, we need to mark the expression with a
12087 // DW_OP_stack_value.
12088 auto *DIExpr = DV->getExpression();
12089 auto NewLocOps = DV->copyLocationOps();
12090 bool Changed = false;
12091 size_t OrigLocOpsSize = NewLocOps.size();
12092 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12093 // We're not given a ResNo to compare against because the whole
12094 // node is going away. We know that any ISD::ADD only has one
12095 // result, so we can assume any node match is using the result.
12096 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12097 NewLocOps[i].getSDNode() != &N)
12098 continue;
12099 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12100 if (RHSConstant) {
12103 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12104 } else {
12105 // Convert to a variadic expression (if not already).
12106 // convertToVariadicExpression() returns a const pointer, so we use
12107 // a temporary const variable here.
12108 const auto *TmpDIExpr =
12112 ExprOps.push_back(NewLocOps.size());
12113 ExprOps.push_back(dwarf::DW_OP_plus);
12114 SDDbgOperand RHS =
12116 NewLocOps.push_back(RHS);
12117 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12118 }
12119 Changed = true;
12120 }
12121 (void)Changed;
12122 assert(Changed && "Salvage target doesn't use N");
12123
12124 bool IsVariadic =
12125 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12126
12127 auto AdditionalDependencies = DV->getAdditionalDependencies();
12128 SDDbgValue *Clone = getDbgValueList(
12129 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12130 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12131 ClonedDVs.push_back(Clone);
12132 DV->setIsInvalidated();
12133 DV->setIsEmitted();
12134 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12135 N0.getNode()->dumprFull(this);
12136 dbgs() << " into " << *DIExpr << '\n');
12137 }
12138 break;
12139 }
12140 case ISD::TRUNCATE: {
12141 SDValue N0 = N.getOperand(0);
12142 TypeSize FromSize = N0.getValueSizeInBits();
12143 TypeSize ToSize = N.getValueSizeInBits(0);
12144
12145 DIExpression *DbgExpression = DV->getExpression();
12146 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12147 auto NewLocOps = DV->copyLocationOps();
12148 bool Changed = false;
12149 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12150 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12151 NewLocOps[i].getSDNode() != &N)
12152 continue;
12153
12154 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12155 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12156 Changed = true;
12157 }
12158 assert(Changed && "Salvage target doesn't use N");
12159 (void)Changed;
12160
12161 SDDbgValue *Clone =
12162 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12163 DV->getAdditionalDependencies(), DV->isIndirect(),
12164 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12165
12166 ClonedDVs.push_back(Clone);
12167 DV->setIsInvalidated();
12168 DV->setIsEmitted();
12169 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12170 dbgs() << " into " << *DbgExpression << '\n');
12171 break;
12172 }
12173 }
12174 }
12175
12176 for (SDDbgValue *Dbg : ClonedDVs) {
12177 assert((!Dbg->getSDNodes().empty() ||
12178 llvm::any_of(Dbg->getLocationOps(),
12179 [&](const SDDbgOperand &Op) {
12180 return Op.getKind() == SDDbgOperand::FRAMEIX;
12181 })) &&
12182 "Salvaged DbgValue should depend on a new SDNode");
12183 AddDbgValue(Dbg, false);
12184 }
12185}
12186
12187/// Creates a SDDbgLabel node.
12189 const DebugLoc &DL, unsigned O) {
12190 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12191 "Expected inlined-at fields to agree");
12192 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12193}
12194
12195namespace {
12196
12197/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12198/// pointed to by a use iterator is deleted, increment the use iterator
12199/// so that it doesn't dangle.
12200///
12201class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12204
12205 void NodeDeleted(SDNode *N, SDNode *E) override {
12206 // Increment the iterator as needed.
12207 while (UI != UE && N == UI->getUser())
12208 ++UI;
12209 }
12210
12211public:
12212 RAUWUpdateListener(SelectionDAG &d,
12215 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12216};
12217
12218} // end anonymous namespace
12219
12220/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12221/// This can cause recursive merging of nodes in the DAG.
12222///
12223/// This version assumes From has a single result value.
12224///
12226 SDNode *From = FromN.getNode();
12227 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12228 "Cannot replace with this method!");
12229 assert(From != To.getNode() && "Cannot replace uses of with self");
12230
12231 // Preserve Debug Values
12232 transferDbgValues(FromN, To);
12233 // Preserve extra info.
12234 copyExtraInfo(From, To.getNode());
12235
12236 // Iterate over all the existing uses of From. New uses will be added
12237 // to the beginning of the use list, which we avoid visiting.
12238 // This specifically avoids visiting uses of From that arise while the
12239 // replacement is happening, because any such uses would be the result
12240 // of CSE: If an existing node looks like From after one of its operands
12241 // is replaced by To, we don't want to replace of all its users with To
12242 // too. See PR3018 for more info.
12243 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12244 RAUWUpdateListener Listener(*this, UI, UE);
12245 while (UI != UE) {
12246 SDNode *User = UI->getUser();
12247
12248 // This node is about to morph, remove its old self from the CSE maps.
12249 RemoveNodeFromCSEMaps(User);
12250
12251 // A user can appear in a use list multiple times, and when this
12252 // happens the uses are usually next to each other in the list.
12253 // To help reduce the number of CSE recomputations, process all
12254 // the uses of this user that we can find this way.
12255 do {
12256 SDUse &Use = *UI;
12257 ++UI;
12258 Use.set(To);
12259 if (To->isDivergent() != From->isDivergent())
12261 } while (UI != UE && UI->getUser() == User);
12262 // Now that we have modified User, add it back to the CSE maps. If it
12263 // already exists there, recursively merge the results together.
12264 AddModifiedNodeToCSEMaps(User);
12265 }
12266
12267 // If we just RAUW'd the root, take note.
12268 if (FromN == getRoot())
12269 setRoot(To);
12270}
12271
12272/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12273/// This can cause recursive merging of nodes in the DAG.
12274///
12275/// This version assumes that for each value of From, there is a
12276/// corresponding value in To in the same position with the same type.
12277///
12279#ifndef NDEBUG
12280 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12281 assert((!From->hasAnyUseOfValue(i) ||
12282 From->getValueType(i) == To->getValueType(i)) &&
12283 "Cannot use this version of ReplaceAllUsesWith!");
12284#endif
12285
12286 // Handle the trivial case.
12287 if (From == To)
12288 return;
12289
12290 // Preserve Debug Info. Only do this if there's a use.
12291 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12292 if (From->hasAnyUseOfValue(i)) {
12293 assert((i < To->getNumValues()) && "Invalid To location");
12294 transferDbgValues(SDValue(From, i), SDValue(To, i));
12295 }
12296 // Preserve extra info.
12297 copyExtraInfo(From, To);
12298
12299 // Iterate over just the existing users of From. See the comments in
12300 // the ReplaceAllUsesWith above.
12301 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12302 RAUWUpdateListener Listener(*this, UI, UE);
12303 while (UI != UE) {
12304 SDNode *User = UI->getUser();
12305
12306 // This node is about to morph, remove its old self from the CSE maps.
12307 RemoveNodeFromCSEMaps(User);
12308
12309 // A user can appear in a use list multiple times, and when this
12310 // happens the uses are usually next to each other in the list.
12311 // To help reduce the number of CSE recomputations, process all
12312 // the uses of this user that we can find this way.
12313 do {
12314 SDUse &Use = *UI;
12315 ++UI;
12316 Use.setNode(To);
12317 if (To->isDivergent() != From->isDivergent())
12319 } while (UI != UE && UI->getUser() == User);
12320
12321 // Now that we have modified User, add it back to the CSE maps. If it
12322 // already exists there, recursively merge the results together.
12323 AddModifiedNodeToCSEMaps(User);
12324 }
12325
12326 // If we just RAUW'd the root, take note.
12327 if (From == getRoot().getNode())
12328 setRoot(SDValue(To, getRoot().getResNo()));
12329}
12330
12331/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12332/// This can cause recursive merging of nodes in the DAG.
12333///
12334/// This version can replace From with any result values. To must match the
12335/// number and types of values returned by From.
12337 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12338 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12339
12340 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12341 // Preserve Debug Info.
12342 transferDbgValues(SDValue(From, i), To[i]);
12343 // Preserve extra info.
12344 copyExtraInfo(From, To[i].getNode());
12345 }
12346
12347 // Iterate over just the existing users of From. See the comments in
12348 // the ReplaceAllUsesWith above.
12349 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12350 RAUWUpdateListener Listener(*this, UI, UE);
12351 while (UI != UE) {
12352 SDNode *User = UI->getUser();
12353
12354 // This node is about to morph, remove its old self from the CSE maps.
12355 RemoveNodeFromCSEMaps(User);
12356
12357 // A user can appear in a use list multiple times, and when this happens the
12358 // uses are usually next to each other in the list. To help reduce the
12359 // number of CSE and divergence recomputations, process all the uses of this
12360 // user that we can find this way.
12361 bool To_IsDivergent = false;
12362 do {
12363 SDUse &Use = *UI;
12364 const SDValue &ToOp = To[Use.getResNo()];
12365 ++UI;
12366 Use.set(ToOp);
12367 To_IsDivergent |= ToOp->isDivergent();
12368 } while (UI != UE && UI->getUser() == User);
12369
12370 if (To_IsDivergent != From->isDivergent())
12372
12373 // Now that we have modified User, add it back to the CSE maps. If it
12374 // already exists there, recursively merge the results together.
12375 AddModifiedNodeToCSEMaps(User);
12376 }
12377
12378 // If we just RAUW'd the root, take note.
12379 if (From == getRoot().getNode())
12380 setRoot(SDValue(To[getRoot().getResNo()]));
12381}
12382
12383/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12384/// uses of other values produced by From.getNode() alone. The Deleted
12385/// vector is handled the same way as for ReplaceAllUsesWith.
12387 // Handle the really simple, really trivial case efficiently.
12388 if (From == To) return;
12389
12390 // Handle the simple, trivial, case efficiently.
12391 if (From.getNode()->getNumValues() == 1) {
12392 ReplaceAllUsesWith(From, To);
12393 return;
12394 }
12395
12396 // Preserve Debug Info.
12397 transferDbgValues(From, To);
12398 copyExtraInfo(From.getNode(), To.getNode());
12399
12400 // Iterate over just the existing users of From. See the comments in
12401 // the ReplaceAllUsesWith above.
12402 SDNode::use_iterator UI = From.getNode()->use_begin(),
12403 UE = From.getNode()->use_end();
12404 RAUWUpdateListener Listener(*this, UI, UE);
12405 while (UI != UE) {
12406 SDNode *User = UI->getUser();
12407 bool UserRemovedFromCSEMaps = false;
12408
12409 // A user can appear in a use list multiple times, and when this
12410 // happens the uses are usually next to each other in the list.
12411 // To help reduce the number of CSE recomputations, process all
12412 // the uses of this user that we can find this way.
12413 do {
12414 SDUse &Use = *UI;
12415
12416 // Skip uses of different values from the same node.
12417 if (Use.getResNo() != From.getResNo()) {
12418 ++UI;
12419 continue;
12420 }
12421
12422 // If this node hasn't been modified yet, it's still in the CSE maps,
12423 // so remove its old self from the CSE maps.
12424 if (!UserRemovedFromCSEMaps) {
12425 RemoveNodeFromCSEMaps(User);
12426 UserRemovedFromCSEMaps = true;
12427 }
12428
12429 ++UI;
12430 Use.set(To);
12431 if (To->isDivergent() != From->isDivergent())
12433 } while (UI != UE && UI->getUser() == User);
12434 // We are iterating over all uses of the From node, so if a use
12435 // doesn't use the specific value, no changes are made.
12436 if (!UserRemovedFromCSEMaps)
12437 continue;
12438
12439 // Now that we have modified User, add it back to the CSE maps. If it
12440 // already exists there, recursively merge the results together.
12441 AddModifiedNodeToCSEMaps(User);
12442 }
12443
12444 // If we just RAUW'd the root, take note.
12445 if (From == getRoot())
12446 setRoot(To);
12447}
12448
12449namespace {
12450
12451/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12452/// to record information about a use.
12453struct UseMemo {
12454 SDNode *User;
12455 unsigned Index;
12456 SDUse *Use;
12457};
12458
12459/// operator< - Sort Memos by User.
12460bool operator<(const UseMemo &L, const UseMemo &R) {
12461 return (intptr_t)L.User < (intptr_t)R.User;
12462}
12463
12464/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12465/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12466/// the node already has been taken care of recursively.
12467class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12468 SmallVectorImpl<UseMemo> &Uses;
12469
12470 void NodeDeleted(SDNode *N, SDNode *E) override {
12471 for (UseMemo &Memo : Uses)
12472 if (Memo.User == N)
12473 Memo.User = nullptr;
12474 }
12475
12476public:
12477 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12478 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12479};
12480
12481} // end anonymous namespace
12482
12483/// Return true if a glue output should propagate divergence information.
12485 switch (Node->getOpcode()) {
12486 case ISD::CopyFromReg:
12487 case ISD::CopyToReg:
12488 return false;
12489 default:
12490 return true;
12491 }
12492
12493 llvm_unreachable("covered opcode switch");
12494}
12495
12497 if (TLI->isSDNodeAlwaysUniform(N)) {
12498 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12499 "Conflicting divergence information!");
12500 return false;
12501 }
12502 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12503 return true;
12504 for (const auto &Op : N->ops()) {
12505 EVT VT = Op.getValueType();
12506
12507 // Skip Chain. It does not carry divergence.
12508 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12509 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12510 return true;
12511 }
12512 return false;
12513}
12514
12516 SmallVector<SDNode *, 16> Worklist(1, N);
12517 do {
12518 N = Worklist.pop_back_val();
12519 bool IsDivergent = calculateDivergence(N);
12520 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12521 N->SDNodeBits.IsDivergent = IsDivergent;
12522 llvm::append_range(Worklist, N->users());
12523 }
12524 } while (!Worklist.empty());
12525}
12526
12527void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12529 Order.reserve(AllNodes.size());
12530 for (auto &N : allnodes()) {
12531 unsigned NOps = N.getNumOperands();
12532 Degree[&N] = NOps;
12533 if (0 == NOps)
12534 Order.push_back(&N);
12535 }
12536 for (size_t I = 0; I != Order.size(); ++I) {
12537 SDNode *N = Order[I];
12538 for (auto *U : N->users()) {
12539 unsigned &UnsortedOps = Degree[U];
12540 if (0 == --UnsortedOps)
12541 Order.push_back(U);
12542 }
12543 }
12544}
12545
12546#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12547void SelectionDAG::VerifyDAGDivergence() {
12548 std::vector<SDNode *> TopoOrder;
12549 CreateTopologicalOrder(TopoOrder);
12550 for (auto *N : TopoOrder) {
12551 assert(calculateDivergence(N) == N->isDivergent() &&
12552 "Divergence bit inconsistency detected");
12553 }
12554}
12555#endif
12556
12557/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12558/// uses of other values produced by From.getNode() alone. The same value
12559/// may appear in both the From and To list. The Deleted vector is
12560/// handled the same way as for ReplaceAllUsesWith.
12562 const SDValue *To,
12563 unsigned Num){
12564 // Handle the simple, trivial case efficiently.
12565 if (Num == 1)
12566 return ReplaceAllUsesOfValueWith(*From, *To);
12567
12568 transferDbgValues(*From, *To);
12569 copyExtraInfo(From->getNode(), To->getNode());
12570
12571 // Read up all the uses and make records of them. This helps
12572 // processing new uses that are introduced during the
12573 // replacement process.
12575 for (unsigned i = 0; i != Num; ++i) {
12576 unsigned FromResNo = From[i].getResNo();
12577 SDNode *FromNode = From[i].getNode();
12578 for (SDUse &Use : FromNode->uses()) {
12579 if (Use.getResNo() == FromResNo) {
12580 UseMemo Memo = {Use.getUser(), i, &Use};
12581 Uses.push_back(Memo);
12582 }
12583 }
12584 }
12585
12586 // Sort the uses, so that all the uses from a given User are together.
12588 RAUOVWUpdateListener Listener(*this, Uses);
12589
12590 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12591 UseIndex != UseIndexEnd; ) {
12592 // We know that this user uses some value of From. If it is the right
12593 // value, update it.
12594 SDNode *User = Uses[UseIndex].User;
12595 // If the node has been deleted by recursive CSE updates when updating
12596 // another node, then just skip this entry.
12597 if (User == nullptr) {
12598 ++UseIndex;
12599 continue;
12600 }
12601
12602 // This node is about to morph, remove its old self from the CSE maps.
12603 RemoveNodeFromCSEMaps(User);
12604
12605 // The Uses array is sorted, so all the uses for a given User
12606 // are next to each other in the list.
12607 // To help reduce the number of CSE recomputations, process all
12608 // the uses of this user that we can find this way.
12609 do {
12610 unsigned i = Uses[UseIndex].Index;
12611 SDUse &Use = *Uses[UseIndex].Use;
12612 ++UseIndex;
12613
12614 Use.set(To[i]);
12615 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12616
12617 // Now that we have modified User, add it back to the CSE maps. If it
12618 // already exists there, recursively merge the results together.
12619 AddModifiedNodeToCSEMaps(User);
12620 }
12621}
12622
12623/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12624/// based on their topological order. It returns the maximum id and a vector
12625/// of the SDNodes* in assigned order by reference.
12627 unsigned DAGSize = 0;
12628
12629 // SortedPos tracks the progress of the algorithm. Nodes before it are
12630 // sorted, nodes after it are unsorted. When the algorithm completes
12631 // it is at the end of the list.
12632 allnodes_iterator SortedPos = allnodes_begin();
12633
12634 // Visit all the nodes. Move nodes with no operands to the front of
12635 // the list immediately. Annotate nodes that do have operands with their
12636 // operand count. Before we do this, the Node Id fields of the nodes
12637 // may contain arbitrary values. After, the Node Id fields for nodes
12638 // before SortedPos will contain the topological sort index, and the
12639 // Node Id fields for nodes At SortedPos and after will contain the
12640 // count of outstanding operands.
12642 checkForCycles(&N, this);
12643 unsigned Degree = N.getNumOperands();
12644 if (Degree == 0) {
12645 // A node with no uses, add it to the result array immediately.
12646 N.setNodeId(DAGSize++);
12647 allnodes_iterator Q(&N);
12648 if (Q != SortedPos)
12649 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12650 assert(SortedPos != AllNodes.end() && "Overran node list");
12651 ++SortedPos;
12652 } else {
12653 // Temporarily use the Node Id as scratch space for the degree count.
12654 N.setNodeId(Degree);
12655 }
12656 }
12657
12658 // Visit all the nodes. As we iterate, move nodes into sorted order,
12659 // such that by the time the end is reached all nodes will be sorted.
12660 for (SDNode &Node : allnodes()) {
12661 SDNode *N = &Node;
12662 checkForCycles(N, this);
12663 // N is in sorted position, so all its uses have one less operand
12664 // that needs to be sorted.
12665 for (SDNode *P : N->users()) {
12666 unsigned Degree = P->getNodeId();
12667 assert(Degree != 0 && "Invalid node degree");
12668 --Degree;
12669 if (Degree == 0) {
12670 // All of P's operands are sorted, so P may sorted now.
12671 P->setNodeId(DAGSize++);
12672 if (P->getIterator() != SortedPos)
12673 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12674 assert(SortedPos != AllNodes.end() && "Overran node list");
12675 ++SortedPos;
12676 } else {
12677 // Update P's outstanding operand count.
12678 P->setNodeId(Degree);
12679 }
12680 }
12681 if (Node.getIterator() == SortedPos) {
12682#ifndef NDEBUG
12684 SDNode *S = &*++I;
12685 dbgs() << "Overran sorted position:\n";
12686 S->dumprFull(this); dbgs() << "\n";
12687 dbgs() << "Checking if this is due to cycles\n";
12688 checkForCycles(this, true);
12689#endif
12690 llvm_unreachable(nullptr);
12691 }
12692 }
12693
12694 assert(SortedPos == AllNodes.end() &&
12695 "Topological sort incomplete!");
12696 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12697 "First node in topological sort is not the entry token!");
12698 assert(AllNodes.front().getNodeId() == 0 &&
12699 "First node in topological sort has non-zero id!");
12700 assert(AllNodes.front().getNumOperands() == 0 &&
12701 "First node in topological sort has operands!");
12702 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12703 "Last node in topologic sort has unexpected id!");
12704 assert(AllNodes.back().use_empty() &&
12705 "Last node in topologic sort has users!");
12706 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12707 return DAGSize;
12708}
12709
12711 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12712 SortedNodes.clear();
12713 // Node -> remaining number of outstanding operands.
12714 DenseMap<const SDNode *, unsigned> RemainingOperands;
12715
12716 // Put nodes without any operands into SortedNodes first.
12717 for (const SDNode &N : allnodes()) {
12718 checkForCycles(&N, this);
12719 unsigned NumOperands = N.getNumOperands();
12720 if (NumOperands == 0)
12721 SortedNodes.push_back(&N);
12722 else
12723 // Record their total number of outstanding operands.
12724 RemainingOperands[&N] = NumOperands;
12725 }
12726
12727 // A node is pushed into SortedNodes when all of its operands (predecessors in
12728 // the graph) are also in SortedNodes.
12729 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12730 const SDNode *N = SortedNodes[i];
12731 for (const SDNode *U : N->users()) {
12732 // HandleSDNode is never part of a DAG and therefore has no entry in
12733 // RemainingOperands.
12734 if (U->getOpcode() == ISD::HANDLENODE)
12735 continue;
12736 unsigned &NumRemOperands = RemainingOperands[U];
12737 assert(NumRemOperands && "Invalid number of remaining operands");
12738 --NumRemOperands;
12739 if (!NumRemOperands)
12740 SortedNodes.push_back(U);
12741 }
12742 }
12743
12744 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12745 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12746 "First node in topological sort is not the entry token");
12747 assert(SortedNodes.front()->getNumOperands() == 0 &&
12748 "First node in topological sort has operands");
12749}
12750
12751/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12752/// value is produced by SD.
12753void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12754 for (SDNode *SD : DB->getSDNodes()) {
12755 if (!SD)
12756 continue;
12757 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12758 SD->setHasDebugValue(true);
12759 }
12760 DbgInfo->add(DB, isParameter);
12761}
12762
12763void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12764
12766 SDValue NewMemOpChain) {
12767 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12768 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12769 // The new memory operation must have the same position as the old load in
12770 // terms of memory dependency. Create a TokenFactor for the old load and new
12771 // memory operation and update uses of the old load's output chain to use that
12772 // TokenFactor.
12773 if (OldChain == NewMemOpChain || OldChain.use_empty())
12774 return NewMemOpChain;
12775
12776 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12777 OldChain, NewMemOpChain);
12778 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12779 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12780 return TokenFactor;
12781}
12782
12784 SDValue NewMemOp) {
12785 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12786 SDValue OldChain = SDValue(OldLoad, 1);
12787 SDValue NewMemOpChain = NewMemOp.getValue(1);
12788 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12789}
12790
12792 Function **OutFunction) {
12793 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12794
12795 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12796 auto *Module = MF->getFunction().getParent();
12797 auto *Function = Module->getFunction(Symbol);
12798
12799 if (OutFunction != nullptr)
12800 *OutFunction = Function;
12801
12802 if (Function != nullptr) {
12803 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12804 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12805 }
12806
12807 std::string ErrorStr;
12808 raw_string_ostream ErrorFormatter(ErrorStr);
12809 ErrorFormatter << "Undefined external symbol ";
12810 ErrorFormatter << '"' << Symbol << '"';
12811 report_fatal_error(Twine(ErrorStr));
12812}
12813
12814//===----------------------------------------------------------------------===//
12815// SDNode Class
12816//===----------------------------------------------------------------------===//
12817
12820 return Const != nullptr && Const->isZero();
12821}
12822
12824 return V.isUndef() || isNullConstant(V);
12825}
12826
12829 return Const != nullptr && Const->isZero() && !Const->isNegative();
12830}
12831
12834 return Const != nullptr && Const->isAllOnes();
12835}
12836
12839 return Const != nullptr && Const->isOne();
12840}
12841
12844 return Const != nullptr && Const->isMinSignedValue();
12845}
12846
12847bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12848 unsigned OperandNo) {
12849 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12850 // TODO: Target-specific opcodes could be added.
12851 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12852 /*AllowTruncation*/ true)) {
12853 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12854 switch (Opcode) {
12855 case ISD::ADD:
12856 case ISD::OR:
12857 case ISD::XOR:
12858 case ISD::UMAX:
12859 return Const.isZero();
12860 case ISD::MUL:
12861 return Const.isOne();
12862 case ISD::AND:
12863 case ISD::UMIN:
12864 return Const.isAllOnes();
12865 case ISD::SMAX:
12866 return Const.isMinSignedValue();
12867 case ISD::SMIN:
12868 return Const.isMaxSignedValue();
12869 case ISD::SUB:
12870 case ISD::SHL:
12871 case ISD::SRA:
12872 case ISD::SRL:
12873 return OperandNo == 1 && Const.isZero();
12874 case ISD::UDIV:
12875 case ISD::SDIV:
12876 return OperandNo == 1 && Const.isOne();
12877 }
12878 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12879 switch (Opcode) {
12880 case ISD::FADD:
12881 return ConstFP->isZero() &&
12882 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12883 case ISD::FSUB:
12884 return OperandNo == 1 && ConstFP->isZero() &&
12885 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12886 case ISD::FMUL:
12887 return ConstFP->isExactlyValue(1.0);
12888 case ISD::FDIV:
12889 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12890 case ISD::FMINNUM:
12891 case ISD::FMAXNUM: {
12892 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12893 EVT VT = V.getValueType();
12894 const fltSemantics &Semantics = VT.getFltSemantics();
12895 APFloat NeutralAF = !Flags.hasNoNaNs()
12896 ? APFloat::getQNaN(Semantics)
12897 : !Flags.hasNoInfs()
12898 ? APFloat::getInf(Semantics)
12899 : APFloat::getLargest(Semantics);
12900 if (Opcode == ISD::FMAXNUM)
12901 NeutralAF.changeSign();
12902
12903 return ConstFP->isExactlyValue(NeutralAF);
12904 }
12905 }
12906 }
12907 return false;
12908}
12909
12911 while (V.getOpcode() == ISD::BITCAST)
12912 V = V.getOperand(0);
12913 return V;
12914}
12915
12917 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12918 V = V.getOperand(0);
12919 return V;
12920}
12921
12923 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12924 V = V.getOperand(0);
12925 return V;
12926}
12927
12929 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12930 SDValue InVec = V.getOperand(0);
12931 SDValue EltNo = V.getOperand(2);
12932 EVT VT = InVec.getValueType();
12933 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12934 if (IndexC && VT.isFixedLengthVector() &&
12935 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12936 !DemandedElts[IndexC->getZExtValue()]) {
12937 V = InVec;
12938 continue;
12939 }
12940 break;
12941 }
12942 return V;
12943}
12944
12946 while (V.getOpcode() == ISD::TRUNCATE)
12947 V = V.getOperand(0);
12948 return V;
12949}
12950
12951bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12952 if (V.getOpcode() != ISD::XOR)
12953 return false;
12954 V = peekThroughBitcasts(V.getOperand(1));
12955 unsigned NumBits = V.getScalarValueSizeInBits();
12956 ConstantSDNode *C =
12957 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12958 return C && (C->getAPIntValue().countr_one() >= NumBits);
12959}
12960
12962 bool AllowTruncation) {
12963 EVT VT = N.getValueType();
12964 APInt DemandedElts = VT.isFixedLengthVector()
12966 : APInt(1, 1);
12967 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12968}
12969
12971 bool AllowUndefs,
12972 bool AllowTruncation) {
12974 return CN;
12975
12976 // SplatVectors can truncate their operands. Ignore that case here unless
12977 // AllowTruncation is set.
12978 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12979 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12980 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12981 EVT CVT = CN->getValueType(0);
12982 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12983 if (AllowTruncation || CVT == VecEltVT)
12984 return CN;
12985 }
12986 }
12987
12989 BitVector UndefElements;
12990 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12991
12992 // BuildVectors can truncate their operands. Ignore that case here unless
12993 // AllowTruncation is set.
12994 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12995 if (CN && (UndefElements.none() || AllowUndefs)) {
12996 EVT CVT = CN->getValueType(0);
12997 EVT NSVT = N.getValueType().getScalarType();
12998 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12999 if (AllowTruncation || (CVT == NSVT))
13000 return CN;
13001 }
13002 }
13003
13004 return nullptr;
13005}
13006
13008 EVT VT = N.getValueType();
13009 APInt DemandedElts = VT.isFixedLengthVector()
13011 : APInt(1, 1);
13012 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13013}
13014
13016 const APInt &DemandedElts,
13017 bool AllowUndefs) {
13019 return CN;
13020
13022 BitVector UndefElements;
13023 ConstantFPSDNode *CN =
13024 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13025 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13026 if (CN && (UndefElements.none() || AllowUndefs))
13027 return CN;
13028 }
13029
13030 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13031 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13032 return CN;
13033
13034 return nullptr;
13035}
13036
13037bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13038 // TODO: may want to use peekThroughBitcast() here.
13039 ConstantSDNode *C =
13040 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13041 return C && C->isZero();
13042}
13043
13044bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13045 ConstantSDNode *C =
13046 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13047 return C && C->isOne();
13048}
13049
13050bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13051 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13052 return C && C->isExactlyValue(1.0);
13053}
13054
13055bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13057 unsigned BitWidth = N.getScalarValueSizeInBits();
13058 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13059 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13060}
13061
13062bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13063 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13064 return C && APInt::isSameValue(C->getAPIntValue(),
13065 APInt(C->getAPIntValue().getBitWidth(), 1));
13066}
13067
13068bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13070 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13071 return C && C->isZero();
13072}
13073
13074bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13075 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13076 return C && C->isZero();
13077}
13078
13082
13083MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13084 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13085 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13086 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13087 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13088 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13089 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13090
13091 // We check here that the size of the memory operand fits within the size of
13092 // the MMO. This is because the MMO might indicate only a possible address
13093 // range instead of specifying the affected memory addresses precisely.
13094 assert(
13095 (!MMO->getType().isValid() ||
13096 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13097 "Size mismatch!");
13098}
13099
13100/// Profile - Gather unique data for the node.
13101///
13103 AddNodeIDNode(ID, this);
13104}
13105
13106namespace {
13107
13108 struct EVTArray {
13109 std::vector<EVT> VTs;
13110
13111 EVTArray() {
13112 VTs.reserve(MVT::VALUETYPE_SIZE);
13113 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13114 VTs.push_back(MVT((MVT::SimpleValueType)i));
13115 }
13116 };
13117
13118} // end anonymous namespace
13119
13120/// getValueTypeList - Return a pointer to the specified value type.
13121///
13122const EVT *SDNode::getValueTypeList(MVT VT) {
13123 static EVTArray SimpleVTArray;
13124
13125 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13126 return &SimpleVTArray.VTs[VT.SimpleTy];
13127}
13128
13129/// hasAnyUseOfValue - Return true if there are any use of the indicated
13130/// value. This method ignores uses of other values defined by this operation.
13131bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13132 assert(Value < getNumValues() && "Bad value!");
13133
13134 for (SDUse &U : uses())
13135 if (U.getResNo() == Value)
13136 return true;
13137
13138 return false;
13139}
13140
13141/// isOnlyUserOf - Return true if this node is the only use of N.
13142bool SDNode::isOnlyUserOf(const SDNode *N) const {
13143 bool Seen = false;
13144 for (const SDNode *User : N->users()) {
13145 if (User == this)
13146 Seen = true;
13147 else
13148 return false;
13149 }
13150
13151 return Seen;
13152}
13153
13154/// Return true if the only users of N are contained in Nodes.
13156 bool Seen = false;
13157 for (const SDNode *User : N->users()) {
13158 if (llvm::is_contained(Nodes, User))
13159 Seen = true;
13160 else
13161 return false;
13162 }
13163
13164 return Seen;
13165}
13166
13167/// Return true if the referenced return value is an operand of N.
13168bool SDValue::isOperandOf(const SDNode *N) const {
13169 return is_contained(N->op_values(), *this);
13170}
13171
13172bool SDNode::isOperandOf(const SDNode *N) const {
13173 return any_of(N->op_values(),
13174 [this](SDValue Op) { return this == Op.getNode(); });
13175}
13176
13177/// reachesChainWithoutSideEffects - Return true if this operand (which must
13178/// be a chain) reaches the specified operand without crossing any
13179/// side-effecting instructions on any chain path. In practice, this looks
13180/// through token factors and non-volatile loads. In order to remain efficient,
13181/// this only looks a couple of nodes in, it does not do an exhaustive search.
13182///
13183/// Note that we only need to examine chains when we're searching for
13184/// side-effects; SelectionDAG requires that all side-effects are represented
13185/// by chains, even if another operand would force a specific ordering. This
13186/// constraint is necessary to allow transformations like splitting loads.
13188 unsigned Depth) const {
13189 if (*this == Dest) return true;
13190
13191 // Don't search too deeply, we just want to be able to see through
13192 // TokenFactor's etc.
13193 if (Depth == 0) return false;
13194
13195 // If this is a token factor, all inputs to the TF happen in parallel.
13196 if (getOpcode() == ISD::TokenFactor) {
13197 // First, try a shallow search.
13198 if (is_contained((*this)->ops(), Dest)) {
13199 // We found the chain we want as an operand of this TokenFactor.
13200 // Essentially, we reach the chain without side-effects if we could
13201 // serialize the TokenFactor into a simple chain of operations with
13202 // Dest as the last operation. This is automatically true if the
13203 // chain has one use: there are no other ordering constraints.
13204 // If the chain has more than one use, we give up: some other
13205 // use of Dest might force a side-effect between Dest and the current
13206 // node.
13207 if (Dest.hasOneUse())
13208 return true;
13209 }
13210 // Next, try a deep search: check whether every operand of the TokenFactor
13211 // reaches Dest.
13212 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13213 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13214 });
13215 }
13216
13217 // Loads don't have side effects, look through them.
13218 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13219 if (Ld->isUnordered())
13220 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13221 }
13222 return false;
13223}
13224
13225bool SDNode::hasPredecessor(const SDNode *N) const {
13228 Worklist.push_back(this);
13229 return hasPredecessorHelper(N, Visited, Worklist);
13230}
13231
13233 this->Flags &= Flags;
13234}
13235
13236SDValue
13238 ArrayRef<ISD::NodeType> CandidateBinOps,
13239 bool AllowPartials) {
13240 // The pattern must end in an extract from index 0.
13241 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13242 !isNullConstant(Extract->getOperand(1)))
13243 return SDValue();
13244
13245 // Match against one of the candidate binary ops.
13246 SDValue Op = Extract->getOperand(0);
13247 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13248 return Op.getOpcode() == unsigned(BinOp);
13249 }))
13250 return SDValue();
13251
13252 // Floating-point reductions may require relaxed constraints on the final step
13253 // of the reduction because they may reorder intermediate operations.
13254 unsigned CandidateBinOp = Op.getOpcode();
13255 if (Op.getValueType().isFloatingPoint()) {
13256 SDNodeFlags Flags = Op->getFlags();
13257 switch (CandidateBinOp) {
13258 case ISD::FADD:
13259 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13260 return SDValue();
13261 break;
13262 default:
13263 llvm_unreachable("Unhandled FP opcode for binop reduction");
13264 }
13265 }
13266
13267 // Matching failed - attempt to see if we did enough stages that a partial
13268 // reduction from a subvector is possible.
13269 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13270 if (!AllowPartials || !Op)
13271 return SDValue();
13272 EVT OpVT = Op.getValueType();
13273 EVT OpSVT = OpVT.getScalarType();
13274 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13275 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13276 return SDValue();
13277 BinOp = (ISD::NodeType)CandidateBinOp;
13278 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13279 };
13280
13281 // At each stage, we're looking for something that looks like:
13282 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13283 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13284 // i32 undef, i32 undef, i32 undef, i32 undef>
13285 // %a = binop <8 x i32> %op, %s
13286 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13287 // we expect something like:
13288 // <4,5,6,7,u,u,u,u>
13289 // <2,3,u,u,u,u,u,u>
13290 // <1,u,u,u,u,u,u,u>
13291 // While a partial reduction match would be:
13292 // <2,3,u,u,u,u,u,u>
13293 // <1,u,u,u,u,u,u,u>
13294 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13295 SDValue PrevOp;
13296 for (unsigned i = 0; i < Stages; ++i) {
13297 unsigned MaskEnd = (1 << i);
13298
13299 if (Op.getOpcode() != CandidateBinOp)
13300 return PartialReduction(PrevOp, MaskEnd);
13301
13302 SDValue Op0 = Op.getOperand(0);
13303 SDValue Op1 = Op.getOperand(1);
13304
13306 if (Shuffle) {
13307 Op = Op1;
13308 } else {
13309 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13310 Op = Op0;
13311 }
13312
13313 // The first operand of the shuffle should be the same as the other operand
13314 // of the binop.
13315 if (!Shuffle || Shuffle->getOperand(0) != Op)
13316 return PartialReduction(PrevOp, MaskEnd);
13317
13318 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13319 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13320 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13321 return PartialReduction(PrevOp, MaskEnd);
13322
13323 PrevOp = Op;
13324 }
13325
13326 // Handle subvector reductions, which tend to appear after the shuffle
13327 // reduction stages.
13328 while (Op.getOpcode() == CandidateBinOp) {
13329 unsigned NumElts = Op.getValueType().getVectorNumElements();
13330 SDValue Op0 = Op.getOperand(0);
13331 SDValue Op1 = Op.getOperand(1);
13332 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13334 Op0.getOperand(0) != Op1.getOperand(0))
13335 break;
13336 SDValue Src = Op0.getOperand(0);
13337 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13338 if (NumSrcElts != (2 * NumElts))
13339 break;
13340 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13341 Op1.getConstantOperandAPInt(1) == NumElts) &&
13342 !(Op1.getConstantOperandAPInt(1) == 0 &&
13343 Op0.getConstantOperandAPInt(1) == NumElts))
13344 break;
13345 Op = Src;
13346 }
13347
13348 BinOp = (ISD::NodeType)CandidateBinOp;
13349 return Op;
13350}
13351
13353 EVT VT = N->getValueType(0);
13354 EVT EltVT = VT.getVectorElementType();
13355 unsigned NE = VT.getVectorNumElements();
13356
13357 SDLoc dl(N);
13358
13359 // If ResNE is 0, fully unroll the vector op.
13360 if (ResNE == 0)
13361 ResNE = NE;
13362 else if (NE > ResNE)
13363 NE = ResNE;
13364
13365 if (N->getNumValues() == 2) {
13366 SmallVector<SDValue, 8> Scalars0, Scalars1;
13367 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13368 EVT VT1 = N->getValueType(1);
13369 EVT EltVT1 = VT1.getVectorElementType();
13370
13371 unsigned i;
13372 for (i = 0; i != NE; ++i) {
13373 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13374 SDValue Operand = N->getOperand(j);
13375 EVT OperandVT = Operand.getValueType();
13376
13377 // A vector operand; extract a single element.
13378 EVT OperandEltVT = OperandVT.getVectorElementType();
13379 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13380 }
13381
13382 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13383 Scalars0.push_back(EltOp);
13384 Scalars1.push_back(EltOp.getValue(1));
13385 }
13386
13387 for (; i < ResNE; ++i) {
13388 Scalars0.push_back(getUNDEF(EltVT));
13389 Scalars1.push_back(getUNDEF(EltVT1));
13390 }
13391
13392 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13393 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13394 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13395 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13396 return getMergeValues({Vec0, Vec1}, dl);
13397 }
13398
13399 assert(N->getNumValues() == 1 &&
13400 "Can't unroll a vector with multiple results!");
13401
13403 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13404
13405 unsigned i;
13406 for (i= 0; i != NE; ++i) {
13407 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13408 SDValue Operand = N->getOperand(j);
13409 EVT OperandVT = Operand.getValueType();
13410 if (OperandVT.isVector()) {
13411 // A vector operand; extract a single element.
13412 EVT OperandEltVT = OperandVT.getVectorElementType();
13413 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13414 } else {
13415 // A scalar operand; just use it as is.
13416 Operands[j] = Operand;
13417 }
13418 }
13419
13420 switch (N->getOpcode()) {
13421 default: {
13422 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13423 N->getFlags()));
13424 break;
13425 }
13426 case ISD::VSELECT:
13427 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13428 break;
13429 case ISD::SHL:
13430 case ISD::SRA:
13431 case ISD::SRL:
13432 case ISD::ROTL:
13433 case ISD::ROTR:
13434 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13435 getShiftAmountOperand(Operands[0].getValueType(),
13436 Operands[1])));
13437 break;
13439 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13440 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13441 Operands[0],
13442 getValueType(ExtVT)));
13443 break;
13444 }
13445 case ISD::ADDRSPACECAST: {
13446 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13447 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13448 ASC->getSrcAddressSpace(),
13449 ASC->getDestAddressSpace()));
13450 break;
13451 }
13452 }
13453 }
13454
13455 for (; i < ResNE; ++i)
13456 Scalars.push_back(getUNDEF(EltVT));
13457
13458 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13459 return getBuildVector(VecVT, dl, Scalars);
13460}
13461
13462std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13463 SDNode *N, unsigned ResNE) {
13464 unsigned Opcode = N->getOpcode();
13465 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13466 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13467 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13468 "Expected an overflow opcode");
13469
13470 EVT ResVT = N->getValueType(0);
13471 EVT OvVT = N->getValueType(1);
13472 EVT ResEltVT = ResVT.getVectorElementType();
13473 EVT OvEltVT = OvVT.getVectorElementType();
13474 SDLoc dl(N);
13475
13476 // If ResNE is 0, fully unroll the vector op.
13477 unsigned NE = ResVT.getVectorNumElements();
13478 if (ResNE == 0)
13479 ResNE = NE;
13480 else if (NE > ResNE)
13481 NE = ResNE;
13482
13483 SmallVector<SDValue, 8> LHSScalars;
13484 SmallVector<SDValue, 8> RHSScalars;
13485 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13486 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13487
13488 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13489 SDVTList VTs = getVTList(ResEltVT, SVT);
13490 SmallVector<SDValue, 8> ResScalars;
13491 SmallVector<SDValue, 8> OvScalars;
13492 for (unsigned i = 0; i < NE; ++i) {
13493 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13494 SDValue Ov =
13495 getSelect(dl, OvEltVT, Res.getValue(1),
13496 getBoolConstant(true, dl, OvEltVT, ResVT),
13497 getConstant(0, dl, OvEltVT));
13498
13499 ResScalars.push_back(Res);
13500 OvScalars.push_back(Ov);
13501 }
13502
13503 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13504 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13505
13506 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13507 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13508 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13509 getBuildVector(NewOvVT, dl, OvScalars));
13510}
13511
13514 unsigned Bytes,
13515 int Dist) const {
13516 if (LD->isVolatile() || Base->isVolatile())
13517 return false;
13518 // TODO: probably too restrictive for atomics, revisit
13519 if (!LD->isSimple())
13520 return false;
13521 if (LD->isIndexed() || Base->isIndexed())
13522 return false;
13523 if (LD->getChain() != Base->getChain())
13524 return false;
13525 EVT VT = LD->getMemoryVT();
13526 if (VT.getSizeInBits() / 8 != Bytes)
13527 return false;
13528
13529 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13530 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13531
13532 int64_t Offset = 0;
13533 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13534 return (Dist * (int64_t)Bytes == Offset);
13535 return false;
13536}
13537
13538/// InferPtrAlignment - Infer alignment of a load / store address. Return
13539/// std::nullopt if it cannot be inferred.
13541 // If this is a GlobalAddress + cst, return the alignment.
13542 const GlobalValue *GV = nullptr;
13543 int64_t GVOffset = 0;
13544 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13545 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13546 KnownBits Known(PtrWidth);
13548 unsigned AlignBits = Known.countMinTrailingZeros();
13549 if (AlignBits)
13550 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13551 }
13552
13553 // If this is a direct reference to a stack slot, use information about the
13554 // stack slot's alignment.
13555 int FrameIdx = INT_MIN;
13556 int64_t FrameOffset = 0;
13558 FrameIdx = FI->getIndex();
13559 } else if (isBaseWithConstantOffset(Ptr) &&
13561 // Handle FI+Cst
13562 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13563 FrameOffset = Ptr.getConstantOperandVal(1);
13564 }
13565
13566 if (FrameIdx != INT_MIN) {
13568 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13569 }
13570
13571 return std::nullopt;
13572}
13573
13574/// Split the scalar node with EXTRACT_ELEMENT using the provided
13575/// VTs and return the low/high part.
13576std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13577 const SDLoc &DL,
13578 const EVT &LoVT,
13579 const EVT &HiVT) {
13580 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13581 "Split node must be a scalar type");
13582 SDValue Lo =
13584 SDValue Hi =
13586 return std::make_pair(Lo, Hi);
13587}
13588
13589/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13590/// which is split (or expanded) into two not necessarily identical pieces.
13591std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13592 // Currently all types are split in half.
13593 EVT LoVT, HiVT;
13594 if (!VT.isVector())
13595 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13596 else
13597 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13598
13599 return std::make_pair(LoVT, HiVT);
13600}
13601
13602/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13603/// type, dependent on an enveloping VT that has been split into two identical
13604/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13605std::pair<EVT, EVT>
13607 bool *HiIsEmpty) const {
13608 EVT EltTp = VT.getVectorElementType();
13609 // Examples:
13610 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13611 // custom VL=9 with enveloping VL=8/8 yields 8/1
13612 // custom VL=10 with enveloping VL=8/8 yields 8/2
13613 // etc.
13614 ElementCount VTNumElts = VT.getVectorElementCount();
13615 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13616 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13617 "Mixing fixed width and scalable vectors when enveloping a type");
13618 EVT LoVT, HiVT;
13619 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13620 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13621 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13622 *HiIsEmpty = false;
13623 } else {
13624 // Flag that hi type has zero storage size, but return split envelop type
13625 // (this would be easier if vector types with zero elements were allowed).
13626 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13627 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13628 *HiIsEmpty = true;
13629 }
13630 return std::make_pair(LoVT, HiVT);
13631}
13632
13633/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13634/// low/high part.
13635std::pair<SDValue, SDValue>
13636SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13637 const EVT &HiVT) {
13638 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13639 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13640 "Splitting vector with an invalid mixture of fixed and scalable "
13641 "vector types");
13643 N.getValueType().getVectorMinNumElements() &&
13644 "More vector elements requested than available!");
13645 SDValue Lo, Hi;
13646 Lo = getExtractSubvector(DL, LoVT, N, 0);
13647 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13648 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13649 // IDX with the runtime scaling factor of the result vector type. For
13650 // fixed-width result vectors, that runtime scaling factor is 1.
13653 return std::make_pair(Lo, Hi);
13654}
13655
13656std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13657 const SDLoc &DL) {
13658 // Split the vector length parameter.
13659 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13660 EVT VT = N.getValueType();
13662 "Expecting the mask to be an evenly-sized vector");
13663 SDValue HalfNumElts = getElementCount(
13665 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13666 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13667 return std::make_pair(Lo, Hi);
13668}
13669
13670/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13672 EVT VT = N.getValueType();
13675 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13676}
13677
13680 unsigned Start, unsigned Count,
13681 EVT EltVT) {
13682 EVT VT = Op.getValueType();
13683 if (Count == 0)
13685 if (EltVT == EVT())
13686 EltVT = VT.getVectorElementType();
13687 SDLoc SL(Op);
13688 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13689 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13690 }
13691}
13692
13693// getAddressSpace - Return the address space this GlobalAddress belongs to.
13695 return getGlobal()->getType()->getAddressSpace();
13696}
13697
13700 return Val.MachineCPVal->getType();
13701 return Val.ConstVal->getType();
13702}
13703
13704bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13705 unsigned &SplatBitSize,
13706 bool &HasAnyUndefs,
13707 unsigned MinSplatBits,
13708 bool IsBigEndian) const {
13709 EVT VT = getValueType(0);
13710 assert(VT.isVector() && "Expected a vector type");
13711 unsigned VecWidth = VT.getSizeInBits();
13712 if (MinSplatBits > VecWidth)
13713 return false;
13714
13715 // FIXME: The widths are based on this node's type, but build vectors can
13716 // truncate their operands.
13717 SplatValue = APInt(VecWidth, 0);
13718 SplatUndef = APInt(VecWidth, 0);
13719
13720 // Get the bits. Bits with undefined values (when the corresponding element
13721 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13722 // in SplatValue. If any of the values are not constant, give up and return
13723 // false.
13724 unsigned int NumOps = getNumOperands();
13725 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13726 unsigned EltWidth = VT.getScalarSizeInBits();
13727
13728 for (unsigned j = 0; j < NumOps; ++j) {
13729 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13730 SDValue OpVal = getOperand(i);
13731 unsigned BitPos = j * EltWidth;
13732
13733 if (OpVal.isUndef())
13734 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13735 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13736 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13737 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13738 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13739 else
13740 return false;
13741 }
13742
13743 // The build_vector is all constants or undefs. Find the smallest element
13744 // size that splats the vector.
13745 HasAnyUndefs = (SplatUndef != 0);
13746
13747 // FIXME: This does not work for vectors with elements less than 8 bits.
13748 while (VecWidth > 8) {
13749 // If we can't split in half, stop here.
13750 if (VecWidth & 1)
13751 break;
13752
13753 unsigned HalfSize = VecWidth / 2;
13754 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13755 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13756 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13757 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13758
13759 // If the two halves do not match (ignoring undef bits), stop here.
13760 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13761 MinSplatBits > HalfSize)
13762 break;
13763
13764 SplatValue = HighValue | LowValue;
13765 SplatUndef = HighUndef & LowUndef;
13766
13767 VecWidth = HalfSize;
13768 }
13769
13770 // FIXME: The loop above only tries to split in halves. But if the input
13771 // vector for example is <3 x i16> it wouldn't be able to detect a
13772 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13773 // optimizations. I guess that back in the days when this helper was created
13774 // vectors normally was power-of-2 sized.
13775
13776 SplatBitSize = VecWidth;
13777 return true;
13778}
13779
13781 BitVector *UndefElements) const {
13782 unsigned NumOps = getNumOperands();
13783 if (UndefElements) {
13784 UndefElements->clear();
13785 UndefElements->resize(NumOps);
13786 }
13787 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13788 if (!DemandedElts)
13789 return SDValue();
13790 SDValue Splatted;
13791 for (unsigned i = 0; i != NumOps; ++i) {
13792 if (!DemandedElts[i])
13793 continue;
13794 SDValue Op = getOperand(i);
13795 if (Op.isUndef()) {
13796 if (UndefElements)
13797 (*UndefElements)[i] = true;
13798 } else if (!Splatted) {
13799 Splatted = Op;
13800 } else if (Splatted != Op) {
13801 return SDValue();
13802 }
13803 }
13804
13805 if (!Splatted) {
13806 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13807 assert(getOperand(FirstDemandedIdx).isUndef() &&
13808 "Can only have a splat without a constant for all undefs.");
13809 return getOperand(FirstDemandedIdx);
13810 }
13811
13812 return Splatted;
13813}
13814
13816 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13817 return getSplatValue(DemandedElts, UndefElements);
13818}
13819
13821 SmallVectorImpl<SDValue> &Sequence,
13822 BitVector *UndefElements) const {
13823 unsigned NumOps = getNumOperands();
13824 Sequence.clear();
13825 if (UndefElements) {
13826 UndefElements->clear();
13827 UndefElements->resize(NumOps);
13828 }
13829 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13830 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13831 return false;
13832
13833 // Set the undefs even if we don't find a sequence (like getSplatValue).
13834 if (UndefElements)
13835 for (unsigned I = 0; I != NumOps; ++I)
13836 if (DemandedElts[I] && getOperand(I).isUndef())
13837 (*UndefElements)[I] = true;
13838
13839 // Iteratively widen the sequence length looking for repetitions.
13840 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13841 Sequence.append(SeqLen, SDValue());
13842 for (unsigned I = 0; I != NumOps; ++I) {
13843 if (!DemandedElts[I])
13844 continue;
13845 SDValue &SeqOp = Sequence[I % SeqLen];
13847 if (Op.isUndef()) {
13848 if (!SeqOp)
13849 SeqOp = Op;
13850 continue;
13851 }
13852 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13853 Sequence.clear();
13854 break;
13855 }
13856 SeqOp = Op;
13857 }
13858 if (!Sequence.empty())
13859 return true;
13860 }
13861
13862 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13863 return false;
13864}
13865
13867 BitVector *UndefElements) const {
13868 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13869 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13870}
13871
13874 BitVector *UndefElements) const {
13876 getSplatValue(DemandedElts, UndefElements));
13877}
13878
13881 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13882}
13883
13886 BitVector *UndefElements) const {
13888 getSplatValue(DemandedElts, UndefElements));
13889}
13890
13895
13896int32_t
13898 uint32_t BitWidth) const {
13899 if (ConstantFPSDNode *CN =
13901 bool IsExact;
13902 APSInt IntVal(BitWidth);
13903 const APFloat &APF = CN->getValueAPF();
13904 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13905 APFloat::opOK ||
13906 !IsExact)
13907 return -1;
13908
13909 return IntVal.exactLogBase2();
13910 }
13911 return -1;
13912}
13913
13915 bool IsLittleEndian, unsigned DstEltSizeInBits,
13916 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13917 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13918 if (!isConstant())
13919 return false;
13920
13921 unsigned NumSrcOps = getNumOperands();
13922 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13923 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13924 "Invalid bitcast scale");
13925
13926 // Extract raw src bits.
13927 SmallVector<APInt> SrcBitElements(NumSrcOps,
13928 APInt::getZero(SrcEltSizeInBits));
13929 BitVector SrcUndeElements(NumSrcOps, false);
13930
13931 for (unsigned I = 0; I != NumSrcOps; ++I) {
13933 if (Op.isUndef()) {
13934 SrcUndeElements.set(I);
13935 continue;
13936 }
13937 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13938 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13939 assert((CInt || CFP) && "Unknown constant");
13940 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13941 : CFP->getValueAPF().bitcastToAPInt();
13942 }
13943
13944 // Recast to dst width.
13945 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13946 SrcBitElements, UndefElements, SrcUndeElements);
13947 return true;
13948}
13949
13950void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13951 unsigned DstEltSizeInBits,
13952 SmallVectorImpl<APInt> &DstBitElements,
13953 ArrayRef<APInt> SrcBitElements,
13954 BitVector &DstUndefElements,
13955 const BitVector &SrcUndefElements) {
13956 unsigned NumSrcOps = SrcBitElements.size();
13957 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13958 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13959 "Invalid bitcast scale");
13960 assert(NumSrcOps == SrcUndefElements.size() &&
13961 "Vector size mismatch");
13962
13963 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13964 DstUndefElements.clear();
13965 DstUndefElements.resize(NumDstOps, false);
13966 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13967
13968 // Concatenate src elements constant bits together into dst element.
13969 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13970 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13971 for (unsigned I = 0; I != NumDstOps; ++I) {
13972 DstUndefElements.set(I);
13973 APInt &DstBits = DstBitElements[I];
13974 for (unsigned J = 0; J != Scale; ++J) {
13975 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13976 if (SrcUndefElements[Idx])
13977 continue;
13978 DstUndefElements.reset(I);
13979 const APInt &SrcBits = SrcBitElements[Idx];
13980 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13981 "Illegal constant bitwidths");
13982 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13983 }
13984 }
13985 return;
13986 }
13987
13988 // Split src element constant bits into dst elements.
13989 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13990 for (unsigned I = 0; I != NumSrcOps; ++I) {
13991 if (SrcUndefElements[I]) {
13992 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13993 continue;
13994 }
13995 const APInt &SrcBits = SrcBitElements[I];
13996 for (unsigned J = 0; J != Scale; ++J) {
13997 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13998 APInt &DstBits = DstBitElements[Idx];
13999 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14000 }
14001 }
14002}
14003
14005 for (const SDValue &Op : op_values()) {
14006 unsigned Opc = Op.getOpcode();
14007 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14008 return false;
14009 }
14010 return true;
14011}
14012
14013std::optional<std::pair<APInt, APInt>>
14015 unsigned NumOps = getNumOperands();
14016 if (NumOps < 2)
14017 return std::nullopt;
14018
14021 return std::nullopt;
14022
14023 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14024 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14025 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14026
14027 if (Stride.isZero())
14028 return std::nullopt;
14029
14030 for (unsigned i = 2; i < NumOps; ++i) {
14032 return std::nullopt;
14033
14034 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14035 if (Val != (Start + (Stride * i)))
14036 return std::nullopt;
14037 }
14038
14039 return std::make_pair(Start, Stride);
14040}
14041
14043 // Find the first non-undef value in the shuffle mask.
14044 unsigned i, e;
14045 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14046 /* search */;
14047
14048 // If all elements are undefined, this shuffle can be considered a splat
14049 // (although it should eventually get simplified away completely).
14050 if (i == e)
14051 return true;
14052
14053 // Make sure all remaining elements are either undef or the same as the first
14054 // non-undef value.
14055 for (int Idx = Mask[i]; i != e; ++i)
14056 if (Mask[i] >= 0 && Mask[i] != Idx)
14057 return false;
14058 return true;
14059}
14060
14061// Returns true if it is a constant integer BuildVector or constant integer,
14062// possibly hidden by a bitcast.
14064 SDValue N, bool AllowOpaques) const {
14066
14067 if (auto *C = dyn_cast<ConstantSDNode>(N))
14068 return AllowOpaques || !C->isOpaque();
14069
14071 return true;
14072
14073 // Treat a GlobalAddress supporting constant offset folding as a
14074 // constant integer.
14075 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14076 if (GA->getOpcode() == ISD::GlobalAddress &&
14077 TLI->isOffsetFoldingLegal(GA))
14078 return true;
14079
14080 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14081 isa<ConstantSDNode>(N.getOperand(0)))
14082 return true;
14083 return false;
14084}
14085
14086// Returns true if it is a constant float BuildVector or constant float.
14089 return true;
14090
14092 return true;
14093
14094 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14095 isa<ConstantFPSDNode>(N.getOperand(0)))
14096 return true;
14097
14098 return false;
14099}
14100
14101std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14102 ConstantSDNode *Const =
14103 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14104 if (!Const)
14105 return std::nullopt;
14106
14107 EVT VT = N->getValueType(0);
14108 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14109 switch (TLI->getBooleanContents(N.getValueType())) {
14111 if (CVal.isOne())
14112 return true;
14113 if (CVal.isZero())
14114 return false;
14115 return std::nullopt;
14117 if (CVal.isAllOnes())
14118 return true;
14119 if (CVal.isZero())
14120 return false;
14121 return std::nullopt;
14123 return CVal[0];
14124 }
14125 llvm_unreachable("Unknown BooleanContent enum");
14126}
14127
14128void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14129 assert(!Node->OperandList && "Node already has operands");
14131 "too many operands to fit into SDNode");
14132 SDUse *Ops = OperandRecycler.allocate(
14133 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14134
14135 bool IsDivergent = false;
14136 for (unsigned I = 0; I != Vals.size(); ++I) {
14137 Ops[I].setUser(Node);
14138 Ops[I].setInitial(Vals[I]);
14139 EVT VT = Ops[I].getValueType();
14140
14141 // Skip Chain. It does not carry divergence.
14142 if (VT != MVT::Other &&
14143 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14144 Ops[I].getNode()->isDivergent()) {
14145 IsDivergent = true;
14146 }
14147 }
14148 Node->NumOperands = Vals.size();
14149 Node->OperandList = Ops;
14150 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14151 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14152 Node->SDNodeBits.IsDivergent = IsDivergent;
14153 }
14154 checkForCycles(Node);
14155}
14156
14159 size_t Limit = SDNode::getMaxNumOperands();
14160 while (Vals.size() > Limit) {
14161 unsigned SliceIdx = Vals.size() - Limit;
14162 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14163 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14164 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14165 Vals.emplace_back(NewTF);
14166 }
14167 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14168}
14169
14171 EVT VT, SDNodeFlags Flags) {
14172 switch (Opcode) {
14173 default:
14174 return SDValue();
14175 case ISD::ADD:
14176 case ISD::OR:
14177 case ISD::XOR:
14178 case ISD::UMAX:
14179 return getConstant(0, DL, VT);
14180 case ISD::MUL:
14181 return getConstant(1, DL, VT);
14182 case ISD::AND:
14183 case ISD::UMIN:
14184 return getAllOnesConstant(DL, VT);
14185 case ISD::SMAX:
14187 case ISD::SMIN:
14189 case ISD::FADD:
14190 // If flags allow, prefer positive zero since it's generally cheaper
14191 // to materialize on most targets.
14192 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14193 case ISD::FMUL:
14194 return getConstantFP(1.0, DL, VT);
14195 case ISD::FMINNUM:
14196 case ISD::FMAXNUM: {
14197 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14198 const fltSemantics &Semantics = VT.getFltSemantics();
14199 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14200 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14201 APFloat::getLargest(Semantics);
14202 if (Opcode == ISD::FMAXNUM)
14203 NeutralAF.changeSign();
14204
14205 return getConstantFP(NeutralAF, DL, VT);
14206 }
14207 case ISD::FMINIMUM:
14208 case ISD::FMAXIMUM: {
14209 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14210 const fltSemantics &Semantics = VT.getFltSemantics();
14211 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14212 : APFloat::getLargest(Semantics);
14213 if (Opcode == ISD::FMAXIMUM)
14214 NeutralAF.changeSign();
14215
14216 return getConstantFP(NeutralAF, DL, VT);
14217 }
14218
14219 }
14220}
14221
14222/// Helper used to make a call to a library function that has one argument of
14223/// pointer type.
14224///
14225/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14226/// used to get or set floating-point state. They have one argument of pointer
14227/// type, which points to the memory region containing bits of the
14228/// floating-point state. The value returned by such function is ignored in the
14229/// created call.
14230///
14231/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14232/// \param Ptr Pointer used to save/load state.
14233/// \param InChain Ingoing token chain.
14234/// \returns Outgoing chain token.
14236 SDValue InChain,
14237 const SDLoc &DLoc) {
14238 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14240 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14241 RTLIB::LibcallImpl LibcallImpl =
14242 TLI->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14243 if (LibcallImpl == RTLIB::Unsupported)
14244 reportFatalUsageError("emitting call to unsupported libcall");
14245
14246 SDValue Callee =
14247 getExternalSymbol(TLI->getLibcallImplName(LibcallImpl).data(),
14248 TLI->getPointerTy(getDataLayout()));
14250 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14251 TLI->getLibcallImplCallingConv(LibcallImpl),
14252 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14253 return TLI->LowerCallTo(CLI).second;
14254}
14255
14257 assert(From && To && "Invalid SDNode; empty source SDValue?");
14258 auto I = SDEI.find(From);
14259 if (I == SDEI.end())
14260 return;
14261
14262 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14263 // the iterator, hence the need to make a copy to prevent a use-after-free.
14264 NodeExtraInfo NEI = I->second;
14265 if (LLVM_LIKELY(!NEI.PCSections)) {
14266 // No deep copy required for the types of extra info set.
14267 //
14268 // FIXME: Investigate if other types of extra info also need deep copy. This
14269 // depends on the types of nodes they can be attached to: if some extra info
14270 // is only ever attached to nodes where a replacement To node is always the
14271 // node where later use and propagation of the extra info has the intended
14272 // semantics, no deep copy is required.
14273 SDEI[To] = std::move(NEI);
14274 return;
14275 }
14276
14277 const SDNode *EntrySDN = getEntryNode().getNode();
14278
14279 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14280 // through the replacement of From with To. Otherwise, replacements of a node
14281 // (From) with more complex nodes (To and its operands) may result in lost
14282 // extra info where the root node (To) is insignificant in further propagating
14283 // and using extra info when further lowering to MIR.
14284 //
14285 // In the first step pre-populate the visited set with the nodes reachable
14286 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14287 // DAG that is not new and should be left untouched.
14288 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14289 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14290 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14291 if (MaxDepth == 0) {
14292 // Remember this node in case we need to increase MaxDepth and continue
14293 // populating FromReach from this node.
14294 Leafs.emplace_back(N);
14295 return;
14296 }
14297 if (!FromReach.insert(N).second)
14298 return;
14299 for (const SDValue &Op : N->op_values())
14300 Self(Self, Op.getNode(), MaxDepth - 1);
14301 };
14302
14303 // Copy extra info to To and all its transitive operands (that are new).
14305 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14306 if (FromReach.contains(N))
14307 return true;
14308 if (!Visited.insert(N).second)
14309 return true;
14310 if (EntrySDN == N)
14311 return false;
14312 for (const SDValue &Op : N->op_values()) {
14313 if (N == To && Op.getNode() == EntrySDN) {
14314 // Special case: New node's operand is the entry node; just need to
14315 // copy extra info to new node.
14316 break;
14317 }
14318 if (!Self(Self, Op.getNode()))
14319 return false;
14320 }
14321 // Copy only if entry node was not reached.
14322 SDEI[N] = NEI;
14323 return true;
14324 };
14325
14326 // We first try with a lower MaxDepth, assuming that the path to common
14327 // operands between From and To is relatively short. This significantly
14328 // improves performance in the common case. The initial MaxDepth is big
14329 // enough to avoid retry in the common case; the last MaxDepth is large
14330 // enough to avoid having to use the fallback below (and protects from
14331 // potential stack exhaustion from recursion).
14332 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14333 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14334 // StartFrom is the previous (or initial) set of leafs reachable at the
14335 // previous maximum depth.
14337 std::swap(StartFrom, Leafs);
14338 for (const SDNode *N : StartFrom)
14339 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14340 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14341 return;
14342 // This should happen very rarely (reached the entry node).
14343 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14344 assert(!Leafs.empty());
14345 }
14346
14347 // This should not happen - but if it did, that means the subgraph reachable
14348 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14349 // could not visit all reachable common operands. Consequently, we were able
14350 // to reach the entry node.
14351 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14352 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14353 // Best-effort fallback if assertions disabled.
14354 SDEI[To] = std::move(NEI);
14355}
14356
14357#ifndef NDEBUG
14358static void checkForCyclesHelper(const SDNode *N,
14361 const llvm::SelectionDAG *DAG) {
14362 // If this node has already been checked, don't check it again.
14363 if (Checked.count(N))
14364 return;
14365
14366 // If a node has already been visited on this depth-first walk, reject it as
14367 // a cycle.
14368 if (!Visited.insert(N).second) {
14369 errs() << "Detected cycle in SelectionDAG\n";
14370 dbgs() << "Offending node:\n";
14371 N->dumprFull(DAG); dbgs() << "\n";
14372 abort();
14373 }
14374
14375 for (const SDValue &Op : N->op_values())
14376 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14377
14378 Checked.insert(N);
14379 Visited.erase(N);
14380}
14381#endif
14382
14384 const llvm::SelectionDAG *DAG,
14385 bool force) {
14386#ifndef NDEBUG
14387 bool check = force;
14388#ifdef EXPENSIVE_CHECKS
14389 check = true;
14390#endif // EXPENSIVE_CHECKS
14391 if (check) {
14392 assert(N && "Checking nonexistent SDNode");
14395 checkForCyclesHelper(N, visited, checked, DAG);
14396 }
14397#endif // !NDEBUG
14398}
14399
14400void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14401 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14402}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:569
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue getFixedOrScalableQuantity(SelectionDAG &DAG, const SDLoc &DL, EVT VT, Ty Quantity)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1102
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1190
void copySign(const APFloat &RHS)
Definition APFloat.h:1284
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6053
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1172
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:1414
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1163
bool isFinite() const
Definition APFloat.h:1436
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1329
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1181
bool isSignaling() const
Definition APFloat.h:1433
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1217
bool isZero() const
Definition APFloat.h:1427
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1314
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1208
bool isPosZero() const
Definition APFloat.h:1442
bool isNegZero() const
Definition APFloat.h:1443
void changeSign()
Definition APFloat.h:1279
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1091
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1971
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2055
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1407
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1012
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1541
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:1671
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1386
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1033
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1513
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1331
APInt abs() const
Get the absolute value.
Definition APInt.h:1796
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2026
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1489
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1397
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1154
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:835
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1640
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1629
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1599
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2086
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2100
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1141
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1436
unsigned logBase2() const
Definition APInt.h:1762
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2036
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:985
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1368
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1418
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1389
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2045
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:904
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
const APFloat & getValue() const
Definition Constants.h:326
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:207
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:330
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This SDNode is used for LIFETIME_START/LIFETIME_END values.
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:230
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI void dump(bool Sorted=false) const
Dump the textual format of this DAG.
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:627
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:232
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
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:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3131
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3118
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3108
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3182
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3123
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2269
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3173
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3009
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2274
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3103
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3113
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:807
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ TargetConstantPool
Definition ISDOpcodes.h:184
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:504
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:231
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition ISDOpcodes.h:531
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:270
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:593
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ TargetBlockAddress
Definition ISDOpcodes.h:186
@ DEACTIVATION_SYMBOL
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:289
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:515
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:898
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:521
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:712
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:662
@ TargetExternalSymbol
Definition ISDOpcodes.h:185
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ TargetJumpTable
Definition ISDOpcodes.h:183
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:193
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:815
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:688
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:534
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:228
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:669
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:180
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:701
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:642
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:607
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:569
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:219
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ TargetConstantFP
Definition ISDOpcodes.h:175
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:799
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ TargetFrameIndex
Definition ISDOpcodes.h:182
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:887
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:876
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:323
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:174
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:498
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:732
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:707
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:299
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:678
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:654
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:947
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:696
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:909
@ 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:933
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:821
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:719
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:333
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:181
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1757
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1611
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2484
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
LLVM_ABI bool isOneOrOneSplatFP(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant floating-point value, or a splatted vector of a constant float...
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1625
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2148
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1593
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1537
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1580
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:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1611
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1751
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1561
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:543
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1847
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:723
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1909
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1598
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1638
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:761
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:781
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:778
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
intptr_t getRawBits() const
Definition ValueTypes.h:512
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:142
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:308
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:453
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:301
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:124
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:274
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:119
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:161
KnownBits byteSwap() const
Definition KnownBits.h:514
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:233
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:172
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:321
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:326
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:167
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)