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
1919SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1920 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1921 SDVTList VTs = getVTList(VT);
1923 AddNodeIDNode(ID, Opc, VTs, {});
1924 ID.AddInteger(FI);
1925 void *IP = nullptr;
1926 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1927 return SDValue(E, 0);
1928
1929 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1930 CSEMap.InsertNode(N, IP);
1931 InsertNode(N);
1932 return SDValue(N, 0);
1933}
1934
1935SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1936 unsigned TargetFlags) {
1937 assert((TargetFlags == 0 || isTarget) &&
1938 "Cannot set target flags on target-independent jump tables");
1939 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1940 SDVTList VTs = getVTList(VT);
1942 AddNodeIDNode(ID, Opc, VTs, {});
1943 ID.AddInteger(JTI);
1944 ID.AddInteger(TargetFlags);
1945 void *IP = nullptr;
1946 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1947 return SDValue(E, 0);
1948
1949 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1950 CSEMap.InsertNode(N, IP);
1951 InsertNode(N);
1952 return SDValue(N, 0);
1953}
1954
1956 const SDLoc &DL) {
1958 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1959 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1960}
1961
1963 MaybeAlign Alignment, int Offset,
1964 bool isTarget, unsigned TargetFlags) {
1965 assert((TargetFlags == 0 || isTarget) &&
1966 "Cannot set target flags on target-independent globals");
1967 if (!Alignment)
1968 Alignment = shouldOptForSize()
1969 ? getDataLayout().getABITypeAlign(C->getType())
1970 : getDataLayout().getPrefTypeAlign(C->getType());
1971 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1972 SDVTList VTs = getVTList(VT);
1974 AddNodeIDNode(ID, Opc, VTs, {});
1975 ID.AddInteger(Alignment->value());
1976 ID.AddInteger(Offset);
1977 ID.AddPointer(C);
1978 ID.AddInteger(TargetFlags);
1979 void *IP = nullptr;
1980 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1981 return SDValue(E, 0);
1982
1983 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1984 TargetFlags);
1985 CSEMap.InsertNode(N, IP);
1986 InsertNode(N);
1987 SDValue V = SDValue(N, 0);
1988 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
1989 return V;
1990}
1991
1993 MaybeAlign Alignment, int Offset,
1994 bool isTarget, unsigned TargetFlags) {
1995 assert((TargetFlags == 0 || isTarget) &&
1996 "Cannot set target flags on target-independent globals");
1997 if (!Alignment)
1998 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
1999 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2000 SDVTList VTs = getVTList(VT);
2002 AddNodeIDNode(ID, Opc, VTs, {});
2003 ID.AddInteger(Alignment->value());
2004 ID.AddInteger(Offset);
2005 C->addSelectionDAGCSEId(ID);
2006 ID.AddInteger(TargetFlags);
2007 void *IP = nullptr;
2008 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2009 return SDValue(E, 0);
2010
2011 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2012 TargetFlags);
2013 CSEMap.InsertNode(N, IP);
2014 InsertNode(N);
2015 return SDValue(N, 0);
2016}
2017
2020 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2021 ID.AddPointer(MBB);
2022 void *IP = nullptr;
2023 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2024 return SDValue(E, 0);
2025
2026 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2027 CSEMap.InsertNode(N, IP);
2028 InsertNode(N);
2029 return SDValue(N, 0);
2030}
2031
2033 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2034 ValueTypeNodes.size())
2035 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2036
2037 SDNode *&N = VT.isExtended() ?
2038 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2039
2040 if (N) return SDValue(N, 0);
2041 N = newSDNode<VTSDNode>(VT);
2042 InsertNode(N);
2043 return SDValue(N, 0);
2044}
2045
2047 SDNode *&N = ExternalSymbols[Sym];
2048 if (N) return SDValue(N, 0);
2049 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2050 InsertNode(N);
2051 return SDValue(N, 0);
2052}
2053
2055 SDNode *&N = MCSymbols[Sym];
2056 if (N)
2057 return SDValue(N, 0);
2058 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2059 InsertNode(N);
2060 return SDValue(N, 0);
2061}
2062
2064 unsigned TargetFlags) {
2065 SDNode *&N =
2066 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2067 if (N) return SDValue(N, 0);
2068 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2069 InsertNode(N);
2070 return SDValue(N, 0);
2071}
2072
2074 if ((unsigned)Cond >= CondCodeNodes.size())
2075 CondCodeNodes.resize(Cond+1);
2076
2077 if (!CondCodeNodes[Cond]) {
2078 auto *N = newSDNode<CondCodeSDNode>(Cond);
2079 CondCodeNodes[Cond] = N;
2080 InsertNode(N);
2081 }
2082
2083 return SDValue(CondCodeNodes[Cond], 0);
2084}
2085
2087 bool ConstantFold) {
2088 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2089 "APInt size does not match type size!");
2090
2091 if (MulImm == 0)
2092 return getConstant(0, DL, VT);
2093
2094 if (ConstantFold) {
2095 const MachineFunction &MF = getMachineFunction();
2096 const Function &F = MF.getFunction();
2097 ConstantRange CR = getVScaleRange(&F, 64);
2098 if (const APInt *C = CR.getSingleElement())
2099 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2100 }
2101
2102 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2103}
2104
2106 bool ConstantFold) {
2107 if (EC.isScalable())
2108 return getVScale(DL, VT,
2109 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2110
2111 return getConstant(EC.getKnownMinValue(), DL, VT);
2112}
2113
2115 APInt One(ResVT.getScalarSizeInBits(), 1);
2116 return getStepVector(DL, ResVT, One);
2117}
2118
2120 const APInt &StepVal) {
2121 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2122 if (ResVT.isScalableVector())
2123 return getNode(
2124 ISD::STEP_VECTOR, DL, ResVT,
2125 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2126
2127 SmallVector<SDValue, 16> OpsStepConstants;
2128 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2129 OpsStepConstants.push_back(
2130 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2131 return getBuildVector(ResVT, DL, OpsStepConstants);
2132}
2133
2134/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2135/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2140
2142 SDValue N2, ArrayRef<int> Mask) {
2143 assert(VT.getVectorNumElements() == Mask.size() &&
2144 "Must have the same number of vector elements as mask elements!");
2145 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2146 "Invalid VECTOR_SHUFFLE");
2147
2148 // Canonicalize shuffle undef, undef -> undef
2149 if (N1.isUndef() && N2.isUndef())
2150 return getUNDEF(VT);
2151
2152 // Validate that all indices in Mask are within the range of the elements
2153 // input to the shuffle.
2154 int NElts = Mask.size();
2155 assert(llvm::all_of(Mask,
2156 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2157 "Index out of range");
2158
2159 // Copy the mask so we can do any needed cleanup.
2160 SmallVector<int, 8> MaskVec(Mask);
2161
2162 // Canonicalize shuffle v, v -> v, undef
2163 if (N1 == N2) {
2164 N2 = getUNDEF(VT);
2165 for (int i = 0; i != NElts; ++i)
2166 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2167 }
2168
2169 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2170 if (N1.isUndef())
2171 commuteShuffle(N1, N2, MaskVec);
2172
2173 if (TLI->hasVectorBlend()) {
2174 // If shuffling a splat, try to blend the splat instead. We do this here so
2175 // that even when this arises during lowering we don't have to re-handle it.
2176 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2177 BitVector UndefElements;
2178 SDValue Splat = BV->getSplatValue(&UndefElements);
2179 if (!Splat)
2180 return;
2181
2182 for (int i = 0; i < NElts; ++i) {
2183 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2184 continue;
2185
2186 // If this input comes from undef, mark it as such.
2187 if (UndefElements[MaskVec[i] - Offset]) {
2188 MaskVec[i] = -1;
2189 continue;
2190 }
2191
2192 // If we can blend a non-undef lane, use that instead.
2193 if (!UndefElements[i])
2194 MaskVec[i] = i + Offset;
2195 }
2196 };
2197 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2198 BlendSplat(N1BV, 0);
2199 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2200 BlendSplat(N2BV, NElts);
2201 }
2202
2203 // Canonicalize all index into lhs, -> shuffle lhs, undef
2204 // Canonicalize all index into rhs, -> shuffle rhs, undef
2205 bool AllLHS = true, AllRHS = true;
2206 bool N2Undef = N2.isUndef();
2207 for (int i = 0; i != NElts; ++i) {
2208 if (MaskVec[i] >= NElts) {
2209 if (N2Undef)
2210 MaskVec[i] = -1;
2211 else
2212 AllLHS = false;
2213 } else if (MaskVec[i] >= 0) {
2214 AllRHS = false;
2215 }
2216 }
2217 if (AllLHS && AllRHS)
2218 return getUNDEF(VT);
2219 if (AllLHS && !N2Undef)
2220 N2 = getUNDEF(VT);
2221 if (AllRHS) {
2222 N1 = getUNDEF(VT);
2223 commuteShuffle(N1, N2, MaskVec);
2224 }
2225 // Reset our undef status after accounting for the mask.
2226 N2Undef = N2.isUndef();
2227 // Re-check whether both sides ended up undef.
2228 if (N1.isUndef() && N2Undef)
2229 return getUNDEF(VT);
2230
2231 // If Identity shuffle return that node.
2232 bool Identity = true, AllSame = true;
2233 for (int i = 0; i != NElts; ++i) {
2234 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2235 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2236 }
2237 if (Identity && NElts)
2238 return N1;
2239
2240 // Shuffling a constant splat doesn't change the result.
2241 if (N2Undef) {
2242 SDValue V = N1;
2243
2244 // Look through any bitcasts. We check that these don't change the number
2245 // (and size) of elements and just changes their types.
2246 while (V.getOpcode() == ISD::BITCAST)
2247 V = V->getOperand(0);
2248
2249 // A splat should always show up as a build vector node.
2250 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2251 BitVector UndefElements;
2252 SDValue Splat = BV->getSplatValue(&UndefElements);
2253 // If this is a splat of an undef, shuffling it is also undef.
2254 if (Splat && Splat.isUndef())
2255 return getUNDEF(VT);
2256
2257 bool SameNumElts =
2258 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2259
2260 // We only have a splat which can skip shuffles if there is a splatted
2261 // value and no undef lanes rearranged by the shuffle.
2262 if (Splat && UndefElements.none()) {
2263 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2264 // number of elements match or the value splatted is a zero constant.
2265 if (SameNumElts || isNullConstant(Splat))
2266 return N1;
2267 }
2268
2269 // If the shuffle itself creates a splat, build the vector directly.
2270 if (AllSame && SameNumElts) {
2271 EVT BuildVT = BV->getValueType(0);
2272 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2273 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2274
2275 // We may have jumped through bitcasts, so the type of the
2276 // BUILD_VECTOR may not match the type of the shuffle.
2277 if (BuildVT != VT)
2278 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2279 return NewBV;
2280 }
2281 }
2282 }
2283
2284 SDVTList VTs = getVTList(VT);
2286 SDValue Ops[2] = { N1, N2 };
2288 for (int i = 0; i != NElts; ++i)
2289 ID.AddInteger(MaskVec[i]);
2290
2291 void* IP = nullptr;
2292 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2293 return SDValue(E, 0);
2294
2295 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2296 // SDNode doesn't have access to it. This memory will be "leaked" when
2297 // the node is deallocated, but recovered when the NodeAllocator is released.
2298 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2299 llvm::copy(MaskVec, MaskAlloc);
2300
2301 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2302 dl.getDebugLoc(), MaskAlloc);
2303 createOperands(N, Ops);
2304
2305 CSEMap.InsertNode(N, IP);
2306 InsertNode(N);
2307 SDValue V = SDValue(N, 0);
2308 NewSDValueDbgMsg(V, "Creating new node: ", this);
2309 return V;
2310}
2311
2313 EVT VT = SV.getValueType(0);
2314 SmallVector<int, 8> MaskVec(SV.getMask());
2316
2317 SDValue Op0 = SV.getOperand(0);
2318 SDValue Op1 = SV.getOperand(1);
2319 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2320}
2321
2323 SDVTList VTs = getVTList(VT);
2325 AddNodeIDNode(ID, ISD::Register, VTs, {});
2326 ID.AddInteger(Reg.id());
2327 void *IP = nullptr;
2328 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2329 return SDValue(E, 0);
2330
2331 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2332 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2333 CSEMap.InsertNode(N, IP);
2334 InsertNode(N);
2335 return SDValue(N, 0);
2336}
2337
2340 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2341 ID.AddPointer(RegMask);
2342 void *IP = nullptr;
2343 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2344 return SDValue(E, 0);
2345
2346 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2347 CSEMap.InsertNode(N, IP);
2348 InsertNode(N);
2349 return SDValue(N, 0);
2350}
2351
2353 MCSymbol *Label) {
2354 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2355}
2356
2357SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2358 SDValue Root, MCSymbol *Label) {
2360 SDValue Ops[] = { Root };
2361 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2362 ID.AddPointer(Label);
2363 void *IP = nullptr;
2364 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2365 return SDValue(E, 0);
2366
2367 auto *N =
2368 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2369 createOperands(N, Ops);
2370
2371 CSEMap.InsertNode(N, IP);
2372 InsertNode(N);
2373 return SDValue(N, 0);
2374}
2375
2377 int64_t Offset, bool isTarget,
2378 unsigned TargetFlags) {
2379 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2380 SDVTList VTs = getVTList(VT);
2381
2383 AddNodeIDNode(ID, Opc, VTs, {});
2384 ID.AddPointer(BA);
2385 ID.AddInteger(Offset);
2386 ID.AddInteger(TargetFlags);
2387 void *IP = nullptr;
2388 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2389 return SDValue(E, 0);
2390
2391 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2392 CSEMap.InsertNode(N, IP);
2393 InsertNode(N);
2394 return SDValue(N, 0);
2395}
2396
2399 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2400 ID.AddPointer(V);
2401
2402 void *IP = nullptr;
2403 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2404 return SDValue(E, 0);
2405
2406 auto *N = newSDNode<SrcValueSDNode>(V);
2407 CSEMap.InsertNode(N, IP);
2408 InsertNode(N);
2409 return SDValue(N, 0);
2410}
2411
2414 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2415 ID.AddPointer(MD);
2416
2417 void *IP = nullptr;
2418 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2419 return SDValue(E, 0);
2420
2421 auto *N = newSDNode<MDNodeSDNode>(MD);
2422 CSEMap.InsertNode(N, IP);
2423 InsertNode(N);
2424 return SDValue(N, 0);
2425}
2426
2428 if (VT == V.getValueType())
2429 return V;
2430
2431 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2432}
2433
2435 unsigned SrcAS, unsigned DestAS) {
2436 SDVTList VTs = getVTList(VT);
2437 SDValue Ops[] = {Ptr};
2439 AddNodeIDNode(ID, ISD::ADDRSPACECAST, VTs, Ops);
2440 ID.AddInteger(SrcAS);
2441 ID.AddInteger(DestAS);
2442
2443 void *IP = nullptr;
2444 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2445 return SDValue(E, 0);
2446
2447 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2448 VTs, SrcAS, DestAS);
2449 createOperands(N, Ops);
2450
2451 CSEMap.InsertNode(N, IP);
2452 InsertNode(N);
2453 return SDValue(N, 0);
2454}
2455
2457 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2458}
2459
2460/// getShiftAmountOperand - Return the specified value casted to
2461/// the target's desired shift amount type.
2463 EVT OpTy = Op.getValueType();
2464 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2465 if (OpTy == ShTy || OpTy.isVector()) return Op;
2466
2467 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2468}
2469
2471 SDLoc dl(Node);
2473 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2474 EVT VT = Node->getValueType(0);
2475 SDValue Tmp1 = Node->getOperand(0);
2476 SDValue Tmp2 = Node->getOperand(1);
2477 const MaybeAlign MA(Node->getConstantOperandVal(3));
2478
2479 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2480 Tmp2, MachinePointerInfo(V));
2481 SDValue VAList = VAListLoad;
2482
2483 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2484 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2485 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2486
2487 VAList = getNode(
2488 ISD::AND, dl, VAList.getValueType(), VAList,
2489 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2490 }
2491
2492 // Increment the pointer, VAList, to the next vaarg
2493 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2494 getConstant(getDataLayout().getTypeAllocSize(
2495 VT.getTypeForEVT(*getContext())),
2496 dl, VAList.getValueType()));
2497 // Store the incremented VAList to the legalized pointer
2498 Tmp1 =
2499 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2500 // Load the actual argument out of the pointer VAList
2501 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2502}
2503
2505 SDLoc dl(Node);
2507 // This defaults to loading a pointer from the input and storing it to the
2508 // output, returning the chain.
2509 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2510 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2511 SDValue Tmp1 =
2512 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2513 Node->getOperand(2), MachinePointerInfo(VS));
2514 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2515 MachinePointerInfo(VD));
2516}
2517
2519 const DataLayout &DL = getDataLayout();
2520 Type *Ty = VT.getTypeForEVT(*getContext());
2521 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2522
2523 if (TLI->isTypeLegal(VT) || !VT.isVector())
2524 return RedAlign;
2525
2526 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2527 const Align StackAlign = TFI->getStackAlign();
2528
2529 // See if we can choose a smaller ABI alignment in cases where it's an
2530 // illegal vector type that will get broken down.
2531 if (RedAlign > StackAlign) {
2532 EVT IntermediateVT;
2533 MVT RegisterVT;
2534 unsigned NumIntermediates;
2535 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2536 NumIntermediates, RegisterVT);
2537 Ty = IntermediateVT.getTypeForEVT(*getContext());
2538 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2539 if (RedAlign2 < RedAlign)
2540 RedAlign = RedAlign2;
2541
2542 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2543 // If the stack is not realignable, the alignment should be limited to the
2544 // StackAlignment
2545 RedAlign = std::min(RedAlign, StackAlign);
2546 }
2547
2548 return RedAlign;
2549}
2550
2552 MachineFrameInfo &MFI = MF->getFrameInfo();
2553 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2554 int StackID = 0;
2555 if (Bytes.isScalable())
2556 StackID = TFI->getStackIDForScalableVectors();
2557 // The stack id gives an indication of whether the object is scalable or
2558 // not, so it's safe to pass in the minimum size here.
2559 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2560 false, nullptr, StackID);
2561 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2562}
2563
2565 Type *Ty = VT.getTypeForEVT(*getContext());
2566 Align StackAlign =
2567 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2568 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2569}
2570
2572 TypeSize VT1Size = VT1.getStoreSize();
2573 TypeSize VT2Size = VT2.getStoreSize();
2574 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2575 "Don't know how to choose the maximum size when creating a stack "
2576 "temporary");
2577 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2578 ? VT1Size
2579 : VT2Size;
2580
2581 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2582 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2583 const DataLayout &DL = getDataLayout();
2584 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2585 return CreateStackTemporary(Bytes, Align);
2586}
2587
2589 ISD::CondCode Cond, const SDLoc &dl) {
2590 EVT OpVT = N1.getValueType();
2591
2592 auto GetUndefBooleanConstant = [&]() {
2593 if (VT.getScalarType() == MVT::i1 ||
2594 TLI->getBooleanContents(OpVT) ==
2596 return getUNDEF(VT);
2597 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2598 // so we cannot use getUNDEF(). Return zero instead.
2599 return getConstant(0, dl, VT);
2600 };
2601
2602 // These setcc operations always fold.
2603 switch (Cond) {
2604 default: break;
2605 case ISD::SETFALSE:
2606 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2607 case ISD::SETTRUE:
2608 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2609
2610 case ISD::SETOEQ:
2611 case ISD::SETOGT:
2612 case ISD::SETOGE:
2613 case ISD::SETOLT:
2614 case ISD::SETOLE:
2615 case ISD::SETONE:
2616 case ISD::SETO:
2617 case ISD::SETUO:
2618 case ISD::SETUEQ:
2619 case ISD::SETUNE:
2620 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2621 break;
2622 }
2623
2624 if (OpVT.isInteger()) {
2625 // For EQ and NE, we can always pick a value for the undef to make the
2626 // predicate pass or fail, so we can return undef.
2627 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2628 // icmp eq/ne X, undef -> undef.
2629 if ((N1.isUndef() || N2.isUndef()) &&
2630 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2631 return GetUndefBooleanConstant();
2632
2633 // If both operands are undef, we can return undef for int comparison.
2634 // icmp undef, undef -> undef.
2635 if (N1.isUndef() && N2.isUndef())
2636 return GetUndefBooleanConstant();
2637
2638 // icmp X, X -> true/false
2639 // icmp X, undef -> true/false because undef could be X.
2640 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2641 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2642 }
2643
2645 const APInt &C2 = N2C->getAPIntValue();
2647 const APInt &C1 = N1C->getAPIntValue();
2648
2650 dl, VT, OpVT);
2651 }
2652 }
2653
2654 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2655 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2656
2657 if (N1CFP && N2CFP) {
2658 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2659 switch (Cond) {
2660 default: break;
2661 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2662 return GetUndefBooleanConstant();
2663 [[fallthrough]];
2664 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2665 OpVT);
2666 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2667 return GetUndefBooleanConstant();
2668 [[fallthrough]];
2670 R==APFloat::cmpLessThan, dl, VT,
2671 OpVT);
2672 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2673 return GetUndefBooleanConstant();
2674 [[fallthrough]];
2675 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2676 OpVT);
2677 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2678 return GetUndefBooleanConstant();
2679 [[fallthrough]];
2681 VT, OpVT);
2682 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2683 return GetUndefBooleanConstant();
2684 [[fallthrough]];
2686 R==APFloat::cmpEqual, dl, VT,
2687 OpVT);
2688 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2689 return GetUndefBooleanConstant();
2690 [[fallthrough]];
2692 R==APFloat::cmpEqual, dl, VT, OpVT);
2693 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2694 OpVT);
2695 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2696 OpVT);
2698 R==APFloat::cmpEqual, dl, VT,
2699 OpVT);
2700 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2701 OpVT);
2703 R==APFloat::cmpLessThan, dl, VT,
2704 OpVT);
2706 R==APFloat::cmpUnordered, dl, VT,
2707 OpVT);
2709 VT, OpVT);
2710 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2711 OpVT);
2712 }
2713 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2714 // Ensure that the constant occurs on the RHS.
2716 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2717 return SDValue();
2718 return getSetCC(dl, VT, N2, N1, SwappedCond);
2719 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2720 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2721 // If an operand is known to be a nan (or undef that could be a nan), we can
2722 // fold it.
2723 // Choosing NaN for the undef will always make unordered comparison succeed
2724 // and ordered comparison fails.
2725 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2726 switch (ISD::getUnorderedFlavor(Cond)) {
2727 default:
2728 llvm_unreachable("Unknown flavor!");
2729 case 0: // Known false.
2730 return getBoolConstant(false, dl, VT, OpVT);
2731 case 1: // Known true.
2732 return getBoolConstant(true, dl, VT, OpVT);
2733 case 2: // Undefined.
2734 return GetUndefBooleanConstant();
2735 }
2736 }
2737
2738 // Could not fold it.
2739 return SDValue();
2740}
2741
2742/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2743/// use this predicate to simplify operations downstream.
2745 unsigned BitWidth = Op.getScalarValueSizeInBits();
2747}
2748
2750 if (Depth >= MaxRecursionDepth)
2751 return false; // Limit search depth.
2752
2753 unsigned Opc = Op.getOpcode();
2754 switch (Opc) {
2755 case ISD::FABS:
2756 return true;
2757 case ISD::AssertNoFPClass: {
2758 FPClassTest NoFPClass =
2759 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2760
2761 const FPClassTest TestMask = fcNan | fcNegative;
2762 return (NoFPClass & TestMask) == TestMask;
2763 }
2764 case ISD::ARITH_FENCE:
2765 return SignBitIsZeroFP(Op, Depth + 1);
2766 case ISD::FEXP:
2767 case ISD::FEXP2:
2768 case ISD::FEXP10:
2769 return Op->getFlags().hasNoNaNs();
2770 default:
2771 return false;
2772 }
2773
2774 llvm_unreachable("covered opcode switch");
2775}
2776
2777/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2778/// this predicate to simplify operations downstream. Mask is known to be zero
2779/// for bits that V cannot have.
2781 unsigned Depth) const {
2782 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2783}
2784
2785/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2786/// DemandedElts. We use this predicate to simplify operations downstream.
2787/// Mask is known to be zero for bits that V cannot have.
2789 const APInt &DemandedElts,
2790 unsigned Depth) const {
2791 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2792}
2793
2794/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2795/// DemandedElts. We use this predicate to simplify operations downstream.
2797 unsigned Depth /* = 0 */) const {
2798 return computeKnownBits(V, DemandedElts, Depth).isZero();
2799}
2800
2801/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2803 unsigned Depth) const {
2804 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2805}
2806
2808 const APInt &DemandedElts,
2809 unsigned Depth) const {
2810 EVT VT = Op.getValueType();
2811 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2812
2813 unsigned NumElts = VT.getVectorNumElements();
2814 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2815
2816 APInt KnownZeroElements = APInt::getZero(NumElts);
2817 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2818 if (!DemandedElts[EltIdx])
2819 continue; // Don't query elements that are not demanded.
2820 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2821 if (MaskedVectorIsZero(Op, Mask, Depth))
2822 KnownZeroElements.setBit(EltIdx);
2823 }
2824 return KnownZeroElements;
2825}
2826
2827/// isSplatValue - Return true if the vector V has the same value
2828/// across all DemandedElts. For scalable vectors, we don't know the
2829/// number of lanes at compile time. Instead, we use a 1 bit APInt
2830/// to represent a conservative value for all lanes; that is, that
2831/// one bit value is implicitly splatted across all lanes.
2832bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2833 APInt &UndefElts, unsigned Depth) const {
2834 unsigned Opcode = V.getOpcode();
2835 EVT VT = V.getValueType();
2836 assert(VT.isVector() && "Vector type expected");
2837 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2838 "scalable demanded bits are ignored");
2839
2840 if (!DemandedElts)
2841 return false; // No demanded elts, better to assume we don't know anything.
2842
2843 if (Depth >= MaxRecursionDepth)
2844 return false; // Limit search depth.
2845
2846 // Deal with some common cases here that work for both fixed and scalable
2847 // vector types.
2848 switch (Opcode) {
2849 case ISD::SPLAT_VECTOR:
2850 UndefElts = V.getOperand(0).isUndef()
2851 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2852 : APInt(DemandedElts.getBitWidth(), 0);
2853 return true;
2854 case ISD::ADD:
2855 case ISD::SUB:
2856 case ISD::AND:
2857 case ISD::XOR:
2858 case ISD::OR: {
2859 APInt UndefLHS, UndefRHS;
2860 SDValue LHS = V.getOperand(0);
2861 SDValue RHS = V.getOperand(1);
2862 // Only recognize splats with the same demanded undef elements for both
2863 // operands, otherwise we might fail to handle binop-specific undef
2864 // handling.
2865 // e.g. (and undef, 0) -> 0 etc.
2866 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2867 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2868 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2869 UndefElts = UndefLHS | UndefRHS;
2870 return true;
2871 }
2872 return false;
2873 }
2874 case ISD::ABS:
2875 case ISD::TRUNCATE:
2876 case ISD::SIGN_EXTEND:
2877 case ISD::ZERO_EXTEND:
2878 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2879 default:
2880 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2881 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2882 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2883 Depth);
2884 break;
2885 }
2886
2887 // We don't support other cases than those above for scalable vectors at
2888 // the moment.
2889 if (VT.isScalableVector())
2890 return false;
2891
2892 unsigned NumElts = VT.getVectorNumElements();
2893 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2894 UndefElts = APInt::getZero(NumElts);
2895
2896 switch (Opcode) {
2897 case ISD::BUILD_VECTOR: {
2898 SDValue Scl;
2899 for (unsigned i = 0; i != NumElts; ++i) {
2900 SDValue Op = V.getOperand(i);
2901 if (Op.isUndef()) {
2902 UndefElts.setBit(i);
2903 continue;
2904 }
2905 if (!DemandedElts[i])
2906 continue;
2907 if (Scl && Scl != Op)
2908 return false;
2909 Scl = Op;
2910 }
2911 return true;
2912 }
2913 case ISD::VECTOR_SHUFFLE: {
2914 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2915 APInt DemandedLHS = APInt::getZero(NumElts);
2916 APInt DemandedRHS = APInt::getZero(NumElts);
2917 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2918 for (int i = 0; i != (int)NumElts; ++i) {
2919 int M = Mask[i];
2920 if (M < 0) {
2921 UndefElts.setBit(i);
2922 continue;
2923 }
2924 if (!DemandedElts[i])
2925 continue;
2926 if (M < (int)NumElts)
2927 DemandedLHS.setBit(M);
2928 else
2929 DemandedRHS.setBit(M - NumElts);
2930 }
2931
2932 // If we aren't demanding either op, assume there's no splat.
2933 // If we are demanding both ops, assume there's no splat.
2934 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2935 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2936 return false;
2937
2938 // See if the demanded elts of the source op is a splat or we only demand
2939 // one element, which should always be a splat.
2940 // TODO: Handle source ops splats with undefs.
2941 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2942 APInt SrcUndefs;
2943 return (SrcElts.popcount() == 1) ||
2944 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2945 (SrcElts & SrcUndefs).isZero());
2946 };
2947 if (!DemandedLHS.isZero())
2948 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2949 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2950 }
2952 // Offset the demanded elts by the subvector index.
2953 SDValue Src = V.getOperand(0);
2954 // We don't support scalable vectors at the moment.
2955 if (Src.getValueType().isScalableVector())
2956 return false;
2957 uint64_t Idx = V.getConstantOperandVal(1);
2958 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2959 APInt UndefSrcElts;
2960 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2961 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2962 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2963 return true;
2964 }
2965 break;
2966 }
2970 // Widen the demanded elts by the src element count.
2971 SDValue Src = V.getOperand(0);
2972 // We don't support scalable vectors at the moment.
2973 if (Src.getValueType().isScalableVector())
2974 return false;
2975 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2976 APInt UndefSrcElts;
2977 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2978 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2979 UndefElts = UndefSrcElts.trunc(NumElts);
2980 return true;
2981 }
2982 break;
2983 }
2984 case ISD::BITCAST: {
2985 SDValue Src = V.getOperand(0);
2986 EVT SrcVT = Src.getValueType();
2987 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
2988 unsigned BitWidth = VT.getScalarSizeInBits();
2989
2990 // Ignore bitcasts from unsupported types.
2991 // TODO: Add fp support?
2992 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
2993 break;
2994
2995 // Bitcast 'small element' vector to 'large element' vector.
2996 if ((BitWidth % SrcBitWidth) == 0) {
2997 // See if each sub element is a splat.
2998 unsigned Scale = BitWidth / SrcBitWidth;
2999 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3000 APInt ScaledDemandedElts =
3001 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3002 for (unsigned I = 0; I != Scale; ++I) {
3003 APInt SubUndefElts;
3004 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3005 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3006 SubDemandedElts &= ScaledDemandedElts;
3007 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3008 return false;
3009 // TODO: Add support for merging sub undef elements.
3010 if (!SubUndefElts.isZero())
3011 return false;
3012 }
3013 return true;
3014 }
3015 break;
3016 }
3017 }
3018
3019 return false;
3020}
3021
3022/// Helper wrapper to main isSplatValue function.
3023bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3024 EVT VT = V.getValueType();
3025 assert(VT.isVector() && "Vector type expected");
3026
3027 APInt UndefElts;
3028 // Since the number of lanes in a scalable vector is unknown at compile time,
3029 // we track one bit which is implicitly broadcast to all lanes. This means
3030 // that all lanes in a scalable vector are considered demanded.
3031 APInt DemandedElts
3033 return isSplatValue(V, DemandedElts, UndefElts) &&
3034 (AllowUndefs || !UndefElts);
3035}
3036
3039
3040 EVT VT = V.getValueType();
3041 unsigned Opcode = V.getOpcode();
3042 switch (Opcode) {
3043 default: {
3044 APInt UndefElts;
3045 // Since the number of lanes in a scalable vector is unknown at compile time,
3046 // we track one bit which is implicitly broadcast to all lanes. This means
3047 // that all lanes in a scalable vector are considered demanded.
3048 APInt DemandedElts
3050
3051 if (isSplatValue(V, DemandedElts, UndefElts)) {
3052 if (VT.isScalableVector()) {
3053 // DemandedElts and UndefElts are ignored for scalable vectors, since
3054 // the only supported cases are SPLAT_VECTOR nodes.
3055 SplatIdx = 0;
3056 } else {
3057 // Handle case where all demanded elements are UNDEF.
3058 if (DemandedElts.isSubsetOf(UndefElts)) {
3059 SplatIdx = 0;
3060 return getUNDEF(VT);
3061 }
3062 SplatIdx = (UndefElts & DemandedElts).countr_one();
3063 }
3064 return V;
3065 }
3066 break;
3067 }
3068 case ISD::SPLAT_VECTOR:
3069 SplatIdx = 0;
3070 return V;
3071 case ISD::VECTOR_SHUFFLE: {
3072 assert(!VT.isScalableVector());
3073 // Check if this is a shuffle node doing a splat.
3074 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3075 // getTargetVShiftNode currently struggles without the splat source.
3076 auto *SVN = cast<ShuffleVectorSDNode>(V);
3077 if (!SVN->isSplat())
3078 break;
3079 int Idx = SVN->getSplatIndex();
3080 int NumElts = V.getValueType().getVectorNumElements();
3081 SplatIdx = Idx % NumElts;
3082 return V.getOperand(Idx / NumElts);
3083 }
3084 }
3085
3086 return SDValue();
3087}
3088
3090 int SplatIdx;
3091 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3092 EVT SVT = SrcVector.getValueType().getScalarType();
3093 EVT LegalSVT = SVT;
3094 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3095 if (!SVT.isInteger())
3096 return SDValue();
3097 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3098 if (LegalSVT.bitsLT(SVT))
3099 return SDValue();
3100 }
3101 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3102 }
3103 return SDValue();
3104}
3105
3106std::optional<ConstantRange>
3108 unsigned Depth) const {
3109 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3110 V.getOpcode() == ISD::SRA) &&
3111 "Unknown shift node");
3112 // Shifting more than the bitwidth is not valid.
3113 unsigned BitWidth = V.getScalarValueSizeInBits();
3114
3115 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3116 const APInt &ShAmt = Cst->getAPIntValue();
3117 if (ShAmt.uge(BitWidth))
3118 return std::nullopt;
3119 return ConstantRange(ShAmt);
3120 }
3121
3122 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3123 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3124 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3125 if (!DemandedElts[i])
3126 continue;
3127 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3128 if (!SA) {
3129 MinAmt = MaxAmt = nullptr;
3130 break;
3131 }
3132 const APInt &ShAmt = SA->getAPIntValue();
3133 if (ShAmt.uge(BitWidth))
3134 return std::nullopt;
3135 if (!MinAmt || MinAmt->ugt(ShAmt))
3136 MinAmt = &ShAmt;
3137 if (!MaxAmt || MaxAmt->ult(ShAmt))
3138 MaxAmt = &ShAmt;
3139 }
3140 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3141 "Failed to find matching min/max shift amounts");
3142 if (MinAmt && MaxAmt)
3143 return ConstantRange(*MinAmt, *MaxAmt + 1);
3144 }
3145
3146 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3147 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3148 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3149 if (KnownAmt.getMaxValue().ult(BitWidth))
3150 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3151
3152 return std::nullopt;
3153}
3154
3155std::optional<unsigned>
3157 unsigned Depth) const {
3158 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3159 V.getOpcode() == ISD::SRA) &&
3160 "Unknown shift node");
3161 if (std::optional<ConstantRange> AmtRange =
3162 getValidShiftAmountRange(V, DemandedElts, Depth))
3163 if (const APInt *ShAmt = AmtRange->getSingleElement())
3164 return ShAmt->getZExtValue();
3165 return std::nullopt;
3166}
3167
3168std::optional<unsigned>
3170 EVT VT = V.getValueType();
3171 APInt DemandedElts = VT.isFixedLengthVector()
3173 : APInt(1, 1);
3174 return getValidShiftAmount(V, DemandedElts, Depth);
3175}
3176
3177std::optional<unsigned>
3179 unsigned Depth) const {
3180 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3181 V.getOpcode() == ISD::SRA) &&
3182 "Unknown shift node");
3183 if (std::optional<ConstantRange> AmtRange =
3184 getValidShiftAmountRange(V, DemandedElts, Depth))
3185 return AmtRange->getUnsignedMin().getZExtValue();
3186 return std::nullopt;
3187}
3188
3189std::optional<unsigned>
3191 EVT VT = V.getValueType();
3192 APInt DemandedElts = VT.isFixedLengthVector()
3194 : APInt(1, 1);
3195 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3196}
3197
3198std::optional<unsigned>
3200 unsigned Depth) const {
3201 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3202 V.getOpcode() == ISD::SRA) &&
3203 "Unknown shift node");
3204 if (std::optional<ConstantRange> AmtRange =
3205 getValidShiftAmountRange(V, DemandedElts, Depth))
3206 return AmtRange->getUnsignedMax().getZExtValue();
3207 return std::nullopt;
3208}
3209
3210std::optional<unsigned>
3212 EVT VT = V.getValueType();
3213 APInt DemandedElts = VT.isFixedLengthVector()
3215 : APInt(1, 1);
3216 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3217}
3218
3219/// Determine which bits of Op are known to be either zero or one and return
3220/// them in Known. For vectors, the known bits are those that are shared by
3221/// every vector element.
3223 EVT VT = Op.getValueType();
3224
3225 // Since the number of lanes in a scalable vector is unknown at compile time,
3226 // we track one bit which is implicitly broadcast to all lanes. This means
3227 // that all lanes in a scalable vector are considered demanded.
3228 APInt DemandedElts = VT.isFixedLengthVector()
3230 : APInt(1, 1);
3231 return computeKnownBits(Op, DemandedElts, Depth);
3232}
3233
3234/// Determine which bits of Op are known to be either zero or one and return
3235/// them in Known. The DemandedElts argument allows us to only collect the known
3236/// bits that are shared by the requested vector elements.
3238 unsigned Depth) const {
3239 unsigned BitWidth = Op.getScalarValueSizeInBits();
3240
3241 KnownBits Known(BitWidth); // Don't know anything.
3242
3243 if (auto OptAPInt = Op->bitcastToAPInt()) {
3244 // We know all of the bits for a constant!
3245 return KnownBits::makeConstant(*std::move(OptAPInt));
3246 }
3247
3248 if (Depth >= MaxRecursionDepth)
3249 return Known; // Limit search depth.
3250
3251 KnownBits Known2;
3252 unsigned NumElts = DemandedElts.getBitWidth();
3253 assert((!Op.getValueType().isFixedLengthVector() ||
3254 NumElts == Op.getValueType().getVectorNumElements()) &&
3255 "Unexpected vector size");
3256
3257 if (!DemandedElts)
3258 return Known; // No demanded elts, better to assume we don't know anything.
3259
3260 unsigned Opcode = Op.getOpcode();
3261 switch (Opcode) {
3262 case ISD::MERGE_VALUES:
3263 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3264 Depth + 1);
3265 case ISD::SPLAT_VECTOR: {
3266 SDValue SrcOp = Op.getOperand(0);
3267 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3268 "Expected SPLAT_VECTOR implicit truncation");
3269 // Implicitly truncate the bits to match the official semantics of
3270 // SPLAT_VECTOR.
3271 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3272 break;
3273 }
3275 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3276 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3277 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3278 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3279 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3280 }
3281 break;
3282 }
3283 case ISD::STEP_VECTOR: {
3284 const APInt &Step = Op.getConstantOperandAPInt(0);
3285
3286 if (Step.isPowerOf2())
3287 Known.Zero.setLowBits(Step.logBase2());
3288
3290
3291 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3292 break;
3293 const APInt MinNumElts =
3294 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3295
3296 bool Overflow;
3297 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3299 .umul_ov(MinNumElts, Overflow);
3300 if (Overflow)
3301 break;
3302
3303 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3304 if (Overflow)
3305 break;
3306
3307 Known.Zero.setHighBits(MaxValue.countl_zero());
3308 break;
3309 }
3310 case ISD::BUILD_VECTOR:
3311 assert(!Op.getValueType().isScalableVector());
3312 // Collect the known bits that are shared by every demanded vector element.
3313 Known.setAllConflict();
3314 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3315 if (!DemandedElts[i])
3316 continue;
3317
3318 SDValue SrcOp = Op.getOperand(i);
3319 Known2 = computeKnownBits(SrcOp, Depth + 1);
3320
3321 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3322 if (SrcOp.getValueSizeInBits() != BitWidth) {
3323 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3324 "Expected BUILD_VECTOR implicit truncation");
3325 Known2 = Known2.trunc(BitWidth);
3326 }
3327
3328 // Known bits are the values that are shared by every demanded element.
3329 Known = Known.intersectWith(Known2);
3330
3331 // If we don't know any bits, early out.
3332 if (Known.isUnknown())
3333 break;
3334 }
3335 break;
3336 case ISD::VECTOR_COMPRESS: {
3337 SDValue Vec = Op.getOperand(0);
3338 SDValue PassThru = Op.getOperand(2);
3339 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3340 // If we don't know any bits, early out.
3341 if (Known.isUnknown())
3342 break;
3343 Known2 = computeKnownBits(Vec, Depth + 1);
3344 Known = Known.intersectWith(Known2);
3345 break;
3346 }
3347 case ISD::VECTOR_SHUFFLE: {
3348 assert(!Op.getValueType().isScalableVector());
3349 // Collect the known bits that are shared by every vector element referenced
3350 // by the shuffle.
3351 APInt DemandedLHS, DemandedRHS;
3353 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3354 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3355 DemandedLHS, DemandedRHS))
3356 break;
3357
3358 // Known bits are the values that are shared by every demanded element.
3359 Known.setAllConflict();
3360 if (!!DemandedLHS) {
3361 SDValue LHS = Op.getOperand(0);
3362 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3363 Known = Known.intersectWith(Known2);
3364 }
3365 // If we don't know any bits, early out.
3366 if (Known.isUnknown())
3367 break;
3368 if (!!DemandedRHS) {
3369 SDValue RHS = Op.getOperand(1);
3370 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3371 Known = Known.intersectWith(Known2);
3372 }
3373 break;
3374 }
3375 case ISD::VSCALE: {
3377 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3378 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3379 break;
3380 }
3381 case ISD::CONCAT_VECTORS: {
3382 if (Op.getValueType().isScalableVector())
3383 break;
3384 // Split DemandedElts and test each of the demanded subvectors.
3385 Known.setAllConflict();
3386 EVT SubVectorVT = Op.getOperand(0).getValueType();
3387 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3388 unsigned NumSubVectors = Op.getNumOperands();
3389 for (unsigned i = 0; i != NumSubVectors; ++i) {
3390 APInt DemandedSub =
3391 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3392 if (!!DemandedSub) {
3393 SDValue Sub = Op.getOperand(i);
3394 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3395 Known = Known.intersectWith(Known2);
3396 }
3397 // If we don't know any bits, early out.
3398 if (Known.isUnknown())
3399 break;
3400 }
3401 break;
3402 }
3403 case ISD::INSERT_SUBVECTOR: {
3404 if (Op.getValueType().isScalableVector())
3405 break;
3406 // Demand any elements from the subvector and the remainder from the src its
3407 // inserted into.
3408 SDValue Src = Op.getOperand(0);
3409 SDValue Sub = Op.getOperand(1);
3410 uint64_t Idx = Op.getConstantOperandVal(2);
3411 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3412 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3413 APInt DemandedSrcElts = DemandedElts;
3414 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3415
3416 Known.setAllConflict();
3417 if (!!DemandedSubElts) {
3418 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3419 if (Known.isUnknown())
3420 break; // early-out.
3421 }
3422 if (!!DemandedSrcElts) {
3423 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3424 Known = Known.intersectWith(Known2);
3425 }
3426 break;
3427 }
3429 // Offset the demanded elts by the subvector index.
3430 SDValue Src = Op.getOperand(0);
3431 // Bail until we can represent demanded elements for scalable vectors.
3432 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3433 break;
3434 uint64_t Idx = Op.getConstantOperandVal(1);
3435 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3436 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3437 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3438 break;
3439 }
3440 case ISD::SCALAR_TO_VECTOR: {
3441 if (Op.getValueType().isScalableVector())
3442 break;
3443 // We know about scalar_to_vector as much as we know about it source,
3444 // which becomes the first element of otherwise unknown vector.
3445 if (DemandedElts != 1)
3446 break;
3447
3448 SDValue N0 = Op.getOperand(0);
3449 Known = computeKnownBits(N0, Depth + 1);
3450 if (N0.getValueSizeInBits() != BitWidth)
3451 Known = Known.trunc(BitWidth);
3452
3453 break;
3454 }
3455 case ISD::BITCAST: {
3456 if (Op.getValueType().isScalableVector())
3457 break;
3458
3459 SDValue N0 = Op.getOperand(0);
3460 EVT SubVT = N0.getValueType();
3461 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3462
3463 // Ignore bitcasts from unsupported types.
3464 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3465 break;
3466
3467 // Fast handling of 'identity' bitcasts.
3468 if (BitWidth == SubBitWidth) {
3469 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3470 break;
3471 }
3472
3473 bool IsLE = getDataLayout().isLittleEndian();
3474
3475 // Bitcast 'small element' vector to 'large element' scalar/vector.
3476 if ((BitWidth % SubBitWidth) == 0) {
3477 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3478
3479 // Collect known bits for the (larger) output by collecting the known
3480 // bits from each set of sub elements and shift these into place.
3481 // We need to separately call computeKnownBits for each set of
3482 // sub elements as the knownbits for each is likely to be different.
3483 unsigned SubScale = BitWidth / SubBitWidth;
3484 APInt SubDemandedElts(NumElts * SubScale, 0);
3485 for (unsigned i = 0; i != NumElts; ++i)
3486 if (DemandedElts[i])
3487 SubDemandedElts.setBit(i * SubScale);
3488
3489 for (unsigned i = 0; i != SubScale; ++i) {
3490 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3491 Depth + 1);
3492 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3493 Known.insertBits(Known2, SubBitWidth * Shifts);
3494 }
3495 }
3496
3497 // Bitcast 'large element' scalar/vector to 'small element' vector.
3498 if ((SubBitWidth % BitWidth) == 0) {
3499 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3500
3501 // Collect known bits for the (smaller) output by collecting the known
3502 // bits from the overlapping larger input elements and extracting the
3503 // sub sections we actually care about.
3504 unsigned SubScale = SubBitWidth / BitWidth;
3505 APInt SubDemandedElts =
3506 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3507 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3508
3509 Known.setAllConflict();
3510 for (unsigned i = 0; i != NumElts; ++i)
3511 if (DemandedElts[i]) {
3512 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3513 unsigned Offset = (Shifts % SubScale) * BitWidth;
3514 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3515 // If we don't know any bits, early out.
3516 if (Known.isUnknown())
3517 break;
3518 }
3519 }
3520 break;
3521 }
3522 case ISD::AND:
3523 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3524 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3525
3526 Known &= Known2;
3527 break;
3528 case ISD::OR:
3529 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3530 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3531
3532 Known |= Known2;
3533 break;
3534 case ISD::XOR:
3535 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3536 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3537
3538 Known ^= Known2;
3539 break;
3540 case ISD::MUL: {
3541 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3542 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3543 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3544 // TODO: SelfMultiply can be poison, but not undef.
3545 if (SelfMultiply)
3546 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3547 Op.getOperand(0), DemandedElts, false, Depth + 1);
3548 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3549
3550 // If the multiplication is known not to overflow, the product of a number
3551 // with itself is non-negative. Only do this if we didn't already computed
3552 // the opposite value for the sign bit.
3553 if (Op->getFlags().hasNoSignedWrap() &&
3554 Op.getOperand(0) == Op.getOperand(1) &&
3555 !Known.isNegative())
3556 Known.makeNonNegative();
3557 break;
3558 }
3559 case ISD::MULHU: {
3560 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3561 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3562 Known = KnownBits::mulhu(Known, Known2);
3563 break;
3564 }
3565 case ISD::MULHS: {
3566 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3567 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3568 Known = KnownBits::mulhs(Known, Known2);
3569 break;
3570 }
3571 case ISD::ABDU: {
3572 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3573 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3574 Known = KnownBits::abdu(Known, Known2);
3575 break;
3576 }
3577 case ISD::ABDS: {
3578 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3579 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3580 Known = KnownBits::abds(Known, Known2);
3581 unsigned SignBits1 =
3582 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3583 if (SignBits1 == 1)
3584 break;
3585 unsigned SignBits0 =
3586 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3587 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3588 break;
3589 }
3590 case ISD::UMUL_LOHI: {
3591 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3592 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3593 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3594 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3595 if (Op.getResNo() == 0)
3596 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3597 else
3598 Known = KnownBits::mulhu(Known, Known2);
3599 break;
3600 }
3601 case ISD::SMUL_LOHI: {
3602 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3603 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3604 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3605 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3606 if (Op.getResNo() == 0)
3607 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3608 else
3609 Known = KnownBits::mulhs(Known, Known2);
3610 break;
3611 }
3612 case ISD::AVGFLOORU: {
3613 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3614 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3615 Known = KnownBits::avgFloorU(Known, Known2);
3616 break;
3617 }
3618 case ISD::AVGCEILU: {
3619 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3620 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3621 Known = KnownBits::avgCeilU(Known, Known2);
3622 break;
3623 }
3624 case ISD::AVGFLOORS: {
3625 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3626 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3627 Known = KnownBits::avgFloorS(Known, Known2);
3628 break;
3629 }
3630 case ISD::AVGCEILS: {
3631 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3632 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3633 Known = KnownBits::avgCeilS(Known, Known2);
3634 break;
3635 }
3636 case ISD::SELECT:
3637 case ISD::VSELECT:
3638 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3639 // If we don't know any bits, early out.
3640 if (Known.isUnknown())
3641 break;
3642 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3643
3644 // Only known if known in both the LHS and RHS.
3645 Known = Known.intersectWith(Known2);
3646 break;
3647 case ISD::SELECT_CC:
3648 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3649 // If we don't know any bits, early out.
3650 if (Known.isUnknown())
3651 break;
3652 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3653
3654 // Only known if known in both the LHS and RHS.
3655 Known = Known.intersectWith(Known2);
3656 break;
3657 case ISD::SMULO:
3658 case ISD::UMULO:
3659 if (Op.getResNo() != 1)
3660 break;
3661 // The boolean result conforms to getBooleanContents.
3662 // If we know the result of a setcc has the top bits zero, use this info.
3663 // We know that we have an integer-based boolean since these operations
3664 // are only available for integer.
3665 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3667 BitWidth > 1)
3668 Known.Zero.setBitsFrom(1);
3669 break;
3670 case ISD::SETCC:
3671 case ISD::SETCCCARRY:
3672 case ISD::STRICT_FSETCC:
3673 case ISD::STRICT_FSETCCS: {
3674 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3675 // If we know the result of a setcc has the top bits zero, use this info.
3676 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3678 BitWidth > 1)
3679 Known.Zero.setBitsFrom(1);
3680 break;
3681 }
3682 case ISD::SHL: {
3683 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3684 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3685
3686 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3687 bool NSW = Op->getFlags().hasNoSignedWrap();
3688
3689 bool ShAmtNonZero = Known2.isNonZero();
3690
3691 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3692
3693 // Minimum shift low bits are known zero.
3694 if (std::optional<unsigned> ShMinAmt =
3695 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3696 Known.Zero.setLowBits(*ShMinAmt);
3697 break;
3698 }
3699 case ISD::SRL:
3700 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3701 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3702 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3703 Op->getFlags().hasExact());
3704
3705 // Minimum shift high bits are known zero.
3706 if (std::optional<unsigned> ShMinAmt =
3707 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3708 Known.Zero.setHighBits(*ShMinAmt);
3709 break;
3710 case ISD::SRA:
3711 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3712 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3713 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3714 Op->getFlags().hasExact());
3715 break;
3716 case ISD::ROTL:
3717 case ISD::ROTR:
3718 if (ConstantSDNode *C =
3719 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3720 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3721
3722 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3723
3724 // Canonicalize to ROTR.
3725 if (Opcode == ISD::ROTL && Amt != 0)
3726 Amt = BitWidth - Amt;
3727
3728 Known.Zero = Known.Zero.rotr(Amt);
3729 Known.One = Known.One.rotr(Amt);
3730 }
3731 break;
3732 case ISD::FSHL:
3733 case ISD::FSHR:
3734 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3735 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3736
3737 // For fshl, 0-shift returns the 1st arg.
3738 // For fshr, 0-shift returns the 2nd arg.
3739 if (Amt == 0) {
3740 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3741 DemandedElts, Depth + 1);
3742 break;
3743 }
3744
3745 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3746 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3747 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3748 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3749 if (Opcode == ISD::FSHL) {
3750 Known <<= Amt;
3751 Known2 >>= BitWidth - Amt;
3752 } else {
3753 Known <<= BitWidth - Amt;
3754 Known2 >>= Amt;
3755 }
3756 Known = Known.unionWith(Known2);
3757 }
3758 break;
3759 case ISD::SHL_PARTS:
3760 case ISD::SRA_PARTS:
3761 case ISD::SRL_PARTS: {
3762 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3763
3764 // Collect lo/hi source values and concatenate.
3765 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3766 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3767 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3768 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3769 Known = Known2.concat(Known);
3770
3771 // Collect shift amount.
3772 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3773
3774 if (Opcode == ISD::SHL_PARTS)
3775 Known = KnownBits::shl(Known, Known2);
3776 else if (Opcode == ISD::SRA_PARTS)
3777 Known = KnownBits::ashr(Known, Known2);
3778 else // if (Opcode == ISD::SRL_PARTS)
3779 Known = KnownBits::lshr(Known, Known2);
3780
3781 // TODO: Minimum shift low/high bits are known zero.
3782
3783 if (Op.getResNo() == 0)
3784 Known = Known.extractBits(LoBits, 0);
3785 else
3786 Known = Known.extractBits(HiBits, LoBits);
3787 break;
3788 }
3790 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3791 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3792 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3793 break;
3794 }
3795 case ISD::CTTZ:
3796 case ISD::CTTZ_ZERO_UNDEF: {
3797 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3798 // If we have a known 1, its position is our upper bound.
3799 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3800 unsigned LowBits = llvm::bit_width(PossibleTZ);
3801 Known.Zero.setBitsFrom(LowBits);
3802 break;
3803 }
3804 case ISD::CTLZ:
3805 case ISD::CTLZ_ZERO_UNDEF: {
3806 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3807 // If we have a known 1, its position is our upper bound.
3808 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3809 unsigned LowBits = llvm::bit_width(PossibleLZ);
3810 Known.Zero.setBitsFrom(LowBits);
3811 break;
3812 }
3813 case ISD::CTPOP: {
3814 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3815 // If we know some of the bits are zero, they can't be one.
3816 unsigned PossibleOnes = Known2.countMaxPopulation();
3817 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3818 break;
3819 }
3820 case ISD::PARITY: {
3821 // Parity returns 0 everywhere but the LSB.
3822 Known.Zero.setBitsFrom(1);
3823 break;
3824 }
3825 case ISD::MGATHER:
3826 case ISD::MLOAD: {
3827 ISD::LoadExtType ETy =
3828 (Opcode == ISD::MGATHER)
3829 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3830 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3831 if (ETy == ISD::ZEXTLOAD) {
3832 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3833 KnownBits Known0(MemVT.getScalarSizeInBits());
3834 return Known0.zext(BitWidth);
3835 }
3836 break;
3837 }
3838 case ISD::LOAD: {
3840 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3841 if (ISD::isNON_EXTLoad(LD) && Cst) {
3842 // Determine any common known bits from the loaded constant pool value.
3843 Type *CstTy = Cst->getType();
3844 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3845 !Op.getValueType().isScalableVector()) {
3846 // If its a vector splat, then we can (quickly) reuse the scalar path.
3847 // NOTE: We assume all elements match and none are UNDEF.
3848 if (CstTy->isVectorTy()) {
3849 if (const Constant *Splat = Cst->getSplatValue()) {
3850 Cst = Splat;
3851 CstTy = Cst->getType();
3852 }
3853 }
3854 // TODO - do we need to handle different bitwidths?
3855 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3856 // Iterate across all vector elements finding common known bits.
3857 Known.setAllConflict();
3858 for (unsigned i = 0; i != NumElts; ++i) {
3859 if (!DemandedElts[i])
3860 continue;
3861 if (Constant *Elt = Cst->getAggregateElement(i)) {
3862 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3863 const APInt &Value = CInt->getValue();
3864 Known.One &= Value;
3865 Known.Zero &= ~Value;
3866 continue;
3867 }
3868 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3869 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3870 Known.One &= Value;
3871 Known.Zero &= ~Value;
3872 continue;
3873 }
3874 }
3875 Known.One.clearAllBits();
3876 Known.Zero.clearAllBits();
3877 break;
3878 }
3879 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3880 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3881 Known = KnownBits::makeConstant(CInt->getValue());
3882 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3883 Known =
3884 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3885 }
3886 }
3887 }
3888 } else if (Op.getResNo() == 0) {
3889 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3890 KnownBits KnownScalarMemory(ScalarMemorySize);
3891 if (const MDNode *MD = LD->getRanges())
3892 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3893
3894 // Extend the Known bits from memory to the size of the scalar result.
3895 if (ISD::isZEXTLoad(Op.getNode()))
3896 Known = KnownScalarMemory.zext(BitWidth);
3897 else if (ISD::isSEXTLoad(Op.getNode()))
3898 Known = KnownScalarMemory.sext(BitWidth);
3899 else if (ISD::isEXTLoad(Op.getNode()))
3900 Known = KnownScalarMemory.anyext(BitWidth);
3901 else
3902 Known = KnownScalarMemory;
3903 assert(Known.getBitWidth() == BitWidth);
3904 return Known;
3905 }
3906 break;
3907 }
3909 if (Op.getValueType().isScalableVector())
3910 break;
3911 EVT InVT = Op.getOperand(0).getValueType();
3912 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3913 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3914 Known = Known.zext(BitWidth);
3915 break;
3916 }
3917 case ISD::ZERO_EXTEND: {
3918 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3919 Known = Known.zext(BitWidth);
3920 break;
3921 }
3923 if (Op.getValueType().isScalableVector())
3924 break;
3925 EVT InVT = Op.getOperand(0).getValueType();
3926 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3927 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3928 // If the sign bit is known to be zero or one, then sext will extend
3929 // it to the top bits, else it will just zext.
3930 Known = Known.sext(BitWidth);
3931 break;
3932 }
3933 case ISD::SIGN_EXTEND: {
3934 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3935 // If the sign bit is known to be zero or one, then sext will extend
3936 // it to the top bits, else it will just zext.
3937 Known = Known.sext(BitWidth);
3938 break;
3939 }
3941 if (Op.getValueType().isScalableVector())
3942 break;
3943 EVT InVT = Op.getOperand(0).getValueType();
3944 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3945 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3946 Known = Known.anyext(BitWidth);
3947 break;
3948 }
3949 case ISD::ANY_EXTEND: {
3950 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3951 Known = Known.anyext(BitWidth);
3952 break;
3953 }
3954 case ISD::TRUNCATE: {
3955 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3956 Known = Known.trunc(BitWidth);
3957 break;
3958 }
3959 case ISD::AssertZext: {
3960 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3962 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3963 Known.Zero |= (~InMask);
3964 Known.One &= (~Known.Zero);
3965 break;
3966 }
3967 case ISD::AssertAlign: {
3968 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3969 assert(LogOfAlign != 0);
3970
3971 // TODO: Should use maximum with source
3972 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3973 // well as clearing one bits.
3974 Known.Zero.setLowBits(LogOfAlign);
3975 Known.One.clearLowBits(LogOfAlign);
3976 break;
3977 }
3978 case ISD::AssertNoFPClass: {
3979 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3980
3981 FPClassTest NoFPClass =
3982 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
3983 const FPClassTest NegativeTestMask = fcNan | fcNegative;
3984 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
3985 // Cannot be negative.
3986 Known.makeNonNegative();
3987 }
3988
3989 const FPClassTest PositiveTestMask = fcNan | fcPositive;
3990 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
3991 // Cannot be positive.
3992 Known.makeNegative();
3993 }
3994
3995 break;
3996 }
3997 case ISD::FGETSIGN:
3998 // All bits are zero except the low bit.
3999 Known.Zero.setBitsFrom(1);
4000 break;
4001 case ISD::ADD:
4002 case ISD::SUB: {
4003 SDNodeFlags Flags = Op.getNode()->getFlags();
4004 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4005 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4007 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4008 Flags.hasNoUnsignedWrap(), Known, Known2);
4009 break;
4010 }
4011 case ISD::USUBO:
4012 case ISD::SSUBO:
4013 case ISD::USUBO_CARRY:
4014 case ISD::SSUBO_CARRY:
4015 if (Op.getResNo() == 1) {
4016 // If we know the result of a setcc has the top bits zero, use this info.
4017 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4019 BitWidth > 1)
4020 Known.Zero.setBitsFrom(1);
4021 break;
4022 }
4023 [[fallthrough]];
4024 case ISD::SUBC: {
4025 assert(Op.getResNo() == 0 &&
4026 "We only compute knownbits for the difference here.");
4027
4028 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4029 KnownBits Borrow(1);
4030 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4031 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4032 // Borrow has bit width 1
4033 Borrow = Borrow.trunc(1);
4034 } else {
4035 Borrow.setAllZero();
4036 }
4037
4038 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4039 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4040 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4041 break;
4042 }
4043 case ISD::UADDO:
4044 case ISD::SADDO:
4045 case ISD::UADDO_CARRY:
4046 case ISD::SADDO_CARRY:
4047 if (Op.getResNo() == 1) {
4048 // If we know the result of a setcc has the top bits zero, use this info.
4049 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4051 BitWidth > 1)
4052 Known.Zero.setBitsFrom(1);
4053 break;
4054 }
4055 [[fallthrough]];
4056 case ISD::ADDC:
4057 case ISD::ADDE: {
4058 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4059
4060 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4061 KnownBits Carry(1);
4062 if (Opcode == ISD::ADDE)
4063 // Can't track carry from glue, set carry to unknown.
4064 Carry.resetAll();
4065 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4066 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4067 // Carry has bit width 1
4068 Carry = Carry.trunc(1);
4069 } else {
4070 Carry.setAllZero();
4071 }
4072
4073 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4074 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4075 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4076 break;
4077 }
4078 case ISD::UDIV: {
4079 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4080 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4081 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4082 break;
4083 }
4084 case ISD::SDIV: {
4085 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4086 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4087 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4088 break;
4089 }
4090 case ISD::SREM: {
4091 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4092 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4093 Known = KnownBits::srem(Known, Known2);
4094 break;
4095 }
4096 case ISD::UREM: {
4097 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4098 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4099 Known = KnownBits::urem(Known, Known2);
4100 break;
4101 }
4102 case ISD::EXTRACT_ELEMENT: {
4103 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4104 const unsigned Index = Op.getConstantOperandVal(1);
4105 const unsigned EltBitWidth = Op.getValueSizeInBits();
4106
4107 // Remove low part of known bits mask
4108 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4109 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4110
4111 // Remove high part of known bit mask
4112 Known = Known.trunc(EltBitWidth);
4113 break;
4114 }
4116 SDValue InVec = Op.getOperand(0);
4117 SDValue EltNo = Op.getOperand(1);
4118 EVT VecVT = InVec.getValueType();
4119 // computeKnownBits not yet implemented for scalable vectors.
4120 if (VecVT.isScalableVector())
4121 break;
4122 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4123 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4124
4125 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4126 // anything about the extended bits.
4127 if (BitWidth > EltBitWidth)
4128 Known = Known.trunc(EltBitWidth);
4129
4130 // If we know the element index, just demand that vector element, else for
4131 // an unknown element index, ignore DemandedElts and demand them all.
4132 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4133 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4134 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4135 DemandedSrcElts =
4136 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4137
4138 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4139 if (BitWidth > EltBitWidth)
4140 Known = Known.anyext(BitWidth);
4141 break;
4142 }
4144 if (Op.getValueType().isScalableVector())
4145 break;
4146
4147 // If we know the element index, split the demand between the
4148 // source vector and the inserted element, otherwise assume we need
4149 // the original demanded vector elements and the value.
4150 SDValue InVec = Op.getOperand(0);
4151 SDValue InVal = Op.getOperand(1);
4152 SDValue EltNo = Op.getOperand(2);
4153 bool DemandedVal = true;
4154 APInt DemandedVecElts = DemandedElts;
4155 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4156 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4157 unsigned EltIdx = CEltNo->getZExtValue();
4158 DemandedVal = !!DemandedElts[EltIdx];
4159 DemandedVecElts.clearBit(EltIdx);
4160 }
4161 Known.setAllConflict();
4162 if (DemandedVal) {
4163 Known2 = computeKnownBits(InVal, Depth + 1);
4164 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4165 }
4166 if (!!DemandedVecElts) {
4167 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4168 Known = Known.intersectWith(Known2);
4169 }
4170 break;
4171 }
4172 case ISD::BITREVERSE: {
4173 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4174 Known = Known2.reverseBits();
4175 break;
4176 }
4177 case ISD::BSWAP: {
4178 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4179 Known = Known2.byteSwap();
4180 break;
4181 }
4182 case ISD::ABS: {
4183 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4184 Known = Known2.abs();
4185 Known.Zero.setHighBits(
4186 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4187 break;
4188 }
4189 case ISD::USUBSAT: {
4190 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4191 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4192 Known = KnownBits::usub_sat(Known, Known2);
4193 break;
4194 }
4195 case ISD::UMIN: {
4196 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4197 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4198 Known = KnownBits::umin(Known, Known2);
4199 break;
4200 }
4201 case ISD::UMAX: {
4202 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4203 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4204 Known = KnownBits::umax(Known, Known2);
4205 break;
4206 }
4207 case ISD::SMIN:
4208 case ISD::SMAX: {
4209 // If we have a clamp pattern, we know that the number of sign bits will be
4210 // the minimum of the clamp min/max range.
4211 bool IsMax = (Opcode == ISD::SMAX);
4212 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4213 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4214 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4215 CstHigh =
4216 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4217 if (CstLow && CstHigh) {
4218 if (!IsMax)
4219 std::swap(CstLow, CstHigh);
4220
4221 const APInt &ValueLow = CstLow->getAPIntValue();
4222 const APInt &ValueHigh = CstHigh->getAPIntValue();
4223 if (ValueLow.sle(ValueHigh)) {
4224 unsigned LowSignBits = ValueLow.getNumSignBits();
4225 unsigned HighSignBits = ValueHigh.getNumSignBits();
4226 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4227 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4228 Known.One.setHighBits(MinSignBits);
4229 break;
4230 }
4231 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4232 Known.Zero.setHighBits(MinSignBits);
4233 break;
4234 }
4235 }
4236 }
4237
4238 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4239 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4240 if (IsMax)
4241 Known = KnownBits::smax(Known, Known2);
4242 else
4243 Known = KnownBits::smin(Known, Known2);
4244
4245 // For SMAX, if CstLow is non-negative we know the result will be
4246 // non-negative and thus all sign bits are 0.
4247 // TODO: There's an equivalent of this for smin with negative constant for
4248 // known ones.
4249 if (IsMax && CstLow) {
4250 const APInt &ValueLow = CstLow->getAPIntValue();
4251 if (ValueLow.isNonNegative()) {
4252 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4253 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4254 }
4255 }
4256
4257 break;
4258 }
4259 case ISD::UINT_TO_FP: {
4260 Known.makeNonNegative();
4261 break;
4262 }
4263 case ISD::SINT_TO_FP: {
4264 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4265 if (Known2.isNonNegative())
4266 Known.makeNonNegative();
4267 else if (Known2.isNegative())
4268 Known.makeNegative();
4269 break;
4270 }
4271 case ISD::FP_TO_UINT_SAT: {
4272 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4273 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4275 break;
4276 }
4277 case ISD::ATOMIC_LOAD: {
4278 // If we are looking at the loaded value.
4279 if (Op.getResNo() == 0) {
4280 auto *AT = cast<AtomicSDNode>(Op);
4281 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4282 KnownBits KnownScalarMemory(ScalarMemorySize);
4283 if (const MDNode *MD = AT->getRanges())
4284 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4285
4286 switch (AT->getExtensionType()) {
4287 case ISD::ZEXTLOAD:
4288 Known = KnownScalarMemory.zext(BitWidth);
4289 break;
4290 case ISD::SEXTLOAD:
4291 Known = KnownScalarMemory.sext(BitWidth);
4292 break;
4293 case ISD::EXTLOAD:
4294 switch (TLI->getExtendForAtomicOps()) {
4295 case ISD::ZERO_EXTEND:
4296 Known = KnownScalarMemory.zext(BitWidth);
4297 break;
4298 case ISD::SIGN_EXTEND:
4299 Known = KnownScalarMemory.sext(BitWidth);
4300 break;
4301 default:
4302 Known = KnownScalarMemory.anyext(BitWidth);
4303 break;
4304 }
4305 break;
4306 case ISD::NON_EXTLOAD:
4307 Known = KnownScalarMemory;
4308 break;
4309 }
4310 assert(Known.getBitWidth() == BitWidth);
4311 }
4312 break;
4313 }
4314 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4315 if (Op.getResNo() == 1) {
4316 // The boolean result conforms to getBooleanContents.
4317 // If we know the result of a setcc has the top bits zero, use this info.
4318 // We know that we have an integer-based boolean since these operations
4319 // are only available for integer.
4320 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4322 BitWidth > 1)
4323 Known.Zero.setBitsFrom(1);
4324 break;
4325 }
4326 [[fallthrough]];
4327 case ISD::ATOMIC_CMP_SWAP:
4328 case ISD::ATOMIC_SWAP:
4329 case ISD::ATOMIC_LOAD_ADD:
4330 case ISD::ATOMIC_LOAD_SUB:
4331 case ISD::ATOMIC_LOAD_AND:
4332 case ISD::ATOMIC_LOAD_CLR:
4333 case ISD::ATOMIC_LOAD_OR:
4334 case ISD::ATOMIC_LOAD_XOR:
4335 case ISD::ATOMIC_LOAD_NAND:
4336 case ISD::ATOMIC_LOAD_MIN:
4337 case ISD::ATOMIC_LOAD_MAX:
4338 case ISD::ATOMIC_LOAD_UMIN:
4339 case ISD::ATOMIC_LOAD_UMAX: {
4340 // If we are looking at the loaded value.
4341 if (Op.getResNo() == 0) {
4342 auto *AT = cast<AtomicSDNode>(Op);
4343 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4344
4345 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4346 Known.Zero.setBitsFrom(MemBits);
4347 }
4348 break;
4349 }
4350 case ISD::FrameIndex:
4352 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4353 Known, getMachineFunction());
4354 break;
4355
4356 default:
4357 if (Opcode < ISD::BUILTIN_OP_END)
4358 break;
4359 [[fallthrough]];
4363 // TODO: Probably okay to remove after audit; here to reduce change size
4364 // in initial enablement patch for scalable vectors
4365 if (Op.getValueType().isScalableVector())
4366 break;
4367
4368 // Allow the target to implement this method for its nodes.
4369 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4370 break;
4371 }
4372
4373 return Known;
4374}
4375
4376/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4389
4392 // X + 0 never overflow
4393 if (isNullConstant(N1))
4394 return OFK_Never;
4395
4396 // If both operands each have at least two sign bits, the addition
4397 // cannot overflow.
4398 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4399 return OFK_Never;
4400
4401 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4402 return OFK_Sometime;
4403}
4404
4407 // X + 0 never overflow
4408 if (isNullConstant(N1))
4409 return OFK_Never;
4410
4411 // mulhi + 1 never overflow
4412 KnownBits N1Known = computeKnownBits(N1);
4413 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4414 N1Known.getMaxValue().ult(2))
4415 return OFK_Never;
4416
4417 KnownBits N0Known = computeKnownBits(N0);
4418 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4419 N0Known.getMaxValue().ult(2))
4420 return OFK_Never;
4421
4422 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4423 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4424 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4425 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4426}
4427
4430 // X - 0 never overflow
4431 if (isNullConstant(N1))
4432 return OFK_Never;
4433
4434 // If both operands each have at least two sign bits, the subtraction
4435 // cannot overflow.
4436 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4437 return OFK_Never;
4438
4439 KnownBits N0Known = computeKnownBits(N0);
4440 KnownBits N1Known = computeKnownBits(N1);
4441 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4442 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4443 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4444}
4445
4448 // X - 0 never overflow
4449 if (isNullConstant(N1))
4450 return OFK_Never;
4451
4452 KnownBits N0Known = computeKnownBits(N0);
4453 KnownBits N1Known = computeKnownBits(N1);
4454 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4455 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4456 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4457}
4458
4461 // X * 0 and X * 1 never overflow.
4462 if (isNullConstant(N1) || isOneConstant(N1))
4463 return OFK_Never;
4464
4465 KnownBits N0Known = computeKnownBits(N0);
4466 KnownBits N1Known = computeKnownBits(N1);
4467 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4468 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4469 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4470}
4471
4474 // X * 0 and X * 1 never overflow.
4475 if (isNullConstant(N1) || isOneConstant(N1))
4476 return OFK_Never;
4477
4478 // Get the size of the result.
4479 unsigned BitWidth = N0.getScalarValueSizeInBits();
4480
4481 // Sum of the sign bits.
4482 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4483
4484 // If we have enough sign bits, then there's no overflow.
4485 if (SignBits > BitWidth + 1)
4486 return OFK_Never;
4487
4488 if (SignBits == BitWidth + 1) {
4489 // The overflow occurs when the true multiplication of the
4490 // the operands is the minimum negative number.
4491 KnownBits N0Known = computeKnownBits(N0);
4492 KnownBits N1Known = computeKnownBits(N1);
4493 // If one of the operands is non-negative, then there's no
4494 // overflow.
4495 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4496 return OFK_Never;
4497 }
4498
4499 return OFK_Sometime;
4500}
4501
4503 if (Depth >= MaxRecursionDepth)
4504 return false; // Limit search depth.
4505
4506 EVT OpVT = Val.getValueType();
4507 unsigned BitWidth = OpVT.getScalarSizeInBits();
4508
4509 // Is the constant a known power of 2?
4511 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4512 }))
4513 return true;
4514
4515 // A left-shift of a constant one will have exactly one bit set because
4516 // shifting the bit off the end is undefined.
4517 if (Val.getOpcode() == ISD::SHL) {
4518 auto *C = isConstOrConstSplat(Val.getOperand(0));
4519 if (C && C->getAPIntValue() == 1)
4520 return true;
4521 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4522 isKnownNeverZero(Val, Depth);
4523 }
4524
4525 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4526 // one bit set.
4527 if (Val.getOpcode() == ISD::SRL) {
4528 auto *C = isConstOrConstSplat(Val.getOperand(0));
4529 if (C && C->getAPIntValue().isSignMask())
4530 return true;
4531 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4532 isKnownNeverZero(Val, Depth);
4533 }
4534
4535 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4536 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4537
4538 // Are all operands of a build vector constant powers of two?
4539 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4540 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4541 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4542 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4543 return false;
4544 }))
4545 return true;
4546
4547 // Is the operand of a splat vector a constant power of two?
4548 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4550 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4551 return true;
4552
4553 // vscale(power-of-two) is a power-of-two for some targets
4554 if (Val.getOpcode() == ISD::VSCALE &&
4555 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4557 return true;
4558
4559 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4560 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4561 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4563
4564 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4565 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4567
4568 // Looking for `x & -x` pattern:
4569 // If x == 0:
4570 // x & -x -> 0
4571 // If x != 0:
4572 // x & -x -> non-zero pow2
4573 // so if we find the pattern return whether we know `x` is non-zero.
4574 SDValue X;
4575 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4576 return isKnownNeverZero(X, Depth);
4577
4578 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4579 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4580
4581 // More could be done here, though the above checks are enough
4582 // to handle some common cases.
4583 return false;
4584}
4585
4587 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4588 return C1->getValueAPF().getExactLog2Abs() >= 0;
4589
4590 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4591 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4592
4593 return false;
4594}
4595
4597 EVT VT = Op.getValueType();
4598
4599 // Since the number of lanes in a scalable vector is unknown at compile time,
4600 // we track one bit which is implicitly broadcast to all lanes. This means
4601 // that all lanes in a scalable vector are considered demanded.
4602 APInt DemandedElts = VT.isFixedLengthVector()
4604 : APInt(1, 1);
4605 return ComputeNumSignBits(Op, DemandedElts, Depth);
4606}
4607
4608unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4609 unsigned Depth) const {
4610 EVT VT = Op.getValueType();
4611 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4612 unsigned VTBits = VT.getScalarSizeInBits();
4613 unsigned NumElts = DemandedElts.getBitWidth();
4614 unsigned Tmp, Tmp2;
4615 unsigned FirstAnswer = 1;
4616
4617 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4618 const APInt &Val = C->getAPIntValue();
4619 return Val.getNumSignBits();
4620 }
4621
4622 if (Depth >= MaxRecursionDepth)
4623 return 1; // Limit search depth.
4624
4625 if (!DemandedElts)
4626 return 1; // No demanded elts, better to assume we don't know anything.
4627
4628 unsigned Opcode = Op.getOpcode();
4629 switch (Opcode) {
4630 default: break;
4631 case ISD::AssertSext:
4632 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4633 return VTBits-Tmp+1;
4634 case ISD::AssertZext:
4635 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4636 return VTBits-Tmp;
4637 case ISD::FREEZE:
4638 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4639 /*PoisonOnly=*/false))
4640 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4641 break;
4642 case ISD::MERGE_VALUES:
4643 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4644 Depth + 1);
4645 case ISD::SPLAT_VECTOR: {
4646 // Check if the sign bits of source go down as far as the truncated value.
4647 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4648 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4649 if (NumSrcSignBits > (NumSrcBits - VTBits))
4650 return NumSrcSignBits - (NumSrcBits - VTBits);
4651 break;
4652 }
4653 case ISD::BUILD_VECTOR:
4654 assert(!VT.isScalableVector());
4655 Tmp = VTBits;
4656 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4657 if (!DemandedElts[i])
4658 continue;
4659
4660 SDValue SrcOp = Op.getOperand(i);
4661 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4662 // for constant nodes to ensure we only look at the sign bits.
4664 APInt T = C->getAPIntValue().trunc(VTBits);
4665 Tmp2 = T.getNumSignBits();
4666 } else {
4667 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4668
4669 if (SrcOp.getValueSizeInBits() != VTBits) {
4670 assert(SrcOp.getValueSizeInBits() > VTBits &&
4671 "Expected BUILD_VECTOR implicit truncation");
4672 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4673 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4674 }
4675 }
4676 Tmp = std::min(Tmp, Tmp2);
4677 }
4678 return Tmp;
4679
4680 case ISD::VECTOR_COMPRESS: {
4681 SDValue Vec = Op.getOperand(0);
4682 SDValue PassThru = Op.getOperand(2);
4683 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4684 if (Tmp == 1)
4685 return 1;
4686 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4687 Tmp = std::min(Tmp, Tmp2);
4688 return Tmp;
4689 }
4690
4691 case ISD::VECTOR_SHUFFLE: {
4692 // Collect the minimum number of sign bits that are shared by every vector
4693 // element referenced by the shuffle.
4694 APInt DemandedLHS, DemandedRHS;
4696 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4697 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4698 DemandedLHS, DemandedRHS))
4699 return 1;
4700
4701 Tmp = std::numeric_limits<unsigned>::max();
4702 if (!!DemandedLHS)
4703 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4704 if (!!DemandedRHS) {
4705 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4706 Tmp = std::min(Tmp, Tmp2);
4707 }
4708 // If we don't know anything, early out and try computeKnownBits fall-back.
4709 if (Tmp == 1)
4710 break;
4711 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4712 return Tmp;
4713 }
4714
4715 case ISD::BITCAST: {
4716 if (VT.isScalableVector())
4717 break;
4718 SDValue N0 = Op.getOperand(0);
4719 EVT SrcVT = N0.getValueType();
4720 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4721
4722 // Ignore bitcasts from unsupported types..
4723 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4724 break;
4725
4726 // Fast handling of 'identity' bitcasts.
4727 if (VTBits == SrcBits)
4728 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4729
4730 bool IsLE = getDataLayout().isLittleEndian();
4731
4732 // Bitcast 'large element' scalar/vector to 'small element' vector.
4733 if ((SrcBits % VTBits) == 0) {
4734 assert(VT.isVector() && "Expected bitcast to vector");
4735
4736 unsigned Scale = SrcBits / VTBits;
4737 APInt SrcDemandedElts =
4738 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4739
4740 // Fast case - sign splat can be simply split across the small elements.
4741 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4742 if (Tmp == SrcBits)
4743 return VTBits;
4744
4745 // Slow case - determine how far the sign extends into each sub-element.
4746 Tmp2 = VTBits;
4747 for (unsigned i = 0; i != NumElts; ++i)
4748 if (DemandedElts[i]) {
4749 unsigned SubOffset = i % Scale;
4750 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4751 SubOffset = SubOffset * VTBits;
4752 if (Tmp <= SubOffset)
4753 return 1;
4754 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4755 }
4756 return Tmp2;
4757 }
4758 break;
4759 }
4760
4762 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4763 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4764 return VTBits - Tmp + 1;
4765 case ISD::SIGN_EXTEND:
4766 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4767 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4769 // Max of the input and what this extends.
4770 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4771 Tmp = VTBits-Tmp+1;
4772 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4773 return std::max(Tmp, Tmp2);
4775 if (VT.isScalableVector())
4776 break;
4777 SDValue Src = Op.getOperand(0);
4778 EVT SrcVT = Src.getValueType();
4779 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4780 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4781 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4782 }
4783 case ISD::SRA:
4784 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4785 // SRA X, C -> adds C sign bits.
4786 if (std::optional<unsigned> ShAmt =
4787 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4788 Tmp = std::min(Tmp + *ShAmt, VTBits);
4789 return Tmp;
4790 case ISD::SHL:
4791 if (std::optional<ConstantRange> ShAmtRange =
4792 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4793 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4794 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4795 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4796 // shifted out, then we can compute the number of sign bits for the
4797 // operand being extended. A future improvement could be to pass along the
4798 // "shifted left by" information in the recursive calls to
4799 // ComputeKnownSignBits. Allowing us to handle this more generically.
4800 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4801 SDValue Ext = Op.getOperand(0);
4802 EVT ExtVT = Ext.getValueType();
4803 SDValue Extendee = Ext.getOperand(0);
4804 EVT ExtendeeVT = Extendee.getValueType();
4805 unsigned SizeDifference =
4806 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4807 if (SizeDifference <= MinShAmt) {
4808 Tmp = SizeDifference +
4809 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4810 if (MaxShAmt < Tmp)
4811 return Tmp - MaxShAmt;
4812 }
4813 }
4814 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4815 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4816 if (MaxShAmt < Tmp)
4817 return Tmp - MaxShAmt;
4818 }
4819 break;
4820 case ISD::AND:
4821 case ISD::OR:
4822 case ISD::XOR: // NOT is handled here.
4823 // Logical binary ops preserve the number of sign bits at the worst.
4824 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4825 if (Tmp != 1) {
4826 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4827 FirstAnswer = std::min(Tmp, Tmp2);
4828 // We computed what we know about the sign bits as our first
4829 // answer. Now proceed to the generic code that uses
4830 // computeKnownBits, and pick whichever answer is better.
4831 }
4832 break;
4833
4834 case ISD::SELECT:
4835 case ISD::VSELECT:
4836 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4837 if (Tmp == 1) return 1; // Early out.
4838 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4839 return std::min(Tmp, Tmp2);
4840 case ISD::SELECT_CC:
4841 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4842 if (Tmp == 1) return 1; // Early out.
4843 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4844 return std::min(Tmp, Tmp2);
4845
4846 case ISD::SMIN:
4847 case ISD::SMAX: {
4848 // If we have a clamp pattern, we know that the number of sign bits will be
4849 // the minimum of the clamp min/max range.
4850 bool IsMax = (Opcode == ISD::SMAX);
4851 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4852 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4853 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4854 CstHigh =
4855 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4856 if (CstLow && CstHigh) {
4857 if (!IsMax)
4858 std::swap(CstLow, CstHigh);
4859 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4860 Tmp = CstLow->getAPIntValue().getNumSignBits();
4861 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4862 return std::min(Tmp, Tmp2);
4863 }
4864 }
4865
4866 // Fallback - just get the minimum number of sign bits of the operands.
4867 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4868 if (Tmp == 1)
4869 return 1; // Early out.
4870 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4871 return std::min(Tmp, Tmp2);
4872 }
4873 case ISD::UMIN:
4874 case ISD::UMAX:
4875 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4876 if (Tmp == 1)
4877 return 1; // Early out.
4878 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4879 return std::min(Tmp, Tmp2);
4880 case ISD::SSUBO_CARRY:
4881 case ISD::USUBO_CARRY:
4882 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4883 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4884 return VTBits;
4885 [[fallthrough]];
4886 case ISD::SADDO:
4887 case ISD::UADDO:
4888 case ISD::SADDO_CARRY:
4889 case ISD::UADDO_CARRY:
4890 case ISD::SSUBO:
4891 case ISD::USUBO:
4892 case ISD::SMULO:
4893 case ISD::UMULO:
4894 if (Op.getResNo() != 1)
4895 break;
4896 // The boolean result conforms to getBooleanContents. Fall through.
4897 // If setcc returns 0/-1, all bits are sign bits.
4898 // We know that we have an integer-based boolean since these operations
4899 // are only available for integer.
4900 if (TLI->getBooleanContents(VT.isVector(), false) ==
4902 return VTBits;
4903 break;
4904 case ISD::SETCC:
4905 case ISD::SETCCCARRY:
4906 case ISD::STRICT_FSETCC:
4907 case ISD::STRICT_FSETCCS: {
4908 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4909 // If setcc returns 0/-1, all bits are sign bits.
4910 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4912 return VTBits;
4913 break;
4914 }
4915 case ISD::ROTL:
4916 case ISD::ROTR:
4917 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4918
4919 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4920 if (Tmp == VTBits)
4921 return VTBits;
4922
4923 if (ConstantSDNode *C =
4924 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4925 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4926
4927 // Handle rotate right by N like a rotate left by 32-N.
4928 if (Opcode == ISD::ROTR)
4929 RotAmt = (VTBits - RotAmt) % VTBits;
4930
4931 // If we aren't rotating out all of the known-in sign bits, return the
4932 // number that are left. This handles rotl(sext(x), 1) for example.
4933 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4934 }
4935 break;
4936 case ISD::ADD:
4937 case ISD::ADDC:
4938 // TODO: Move Operand 1 check before Operand 0 check
4939 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4940 if (Tmp == 1) return 1; // Early out.
4941
4942 // Special case decrementing a value (ADD X, -1):
4943 if (ConstantSDNode *CRHS =
4944 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4945 if (CRHS->isAllOnes()) {
4946 KnownBits Known =
4947 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4948
4949 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4950 // sign bits set.
4951 if ((Known.Zero | 1).isAllOnes())
4952 return VTBits;
4953
4954 // If we are subtracting one from a positive number, there is no carry
4955 // out of the result.
4956 if (Known.isNonNegative())
4957 return Tmp;
4958 }
4959
4960 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4961 if (Tmp2 == 1) return 1; // Early out.
4962
4963 // Add can have at most one carry bit. Thus we know that the output
4964 // is, at worst, one more bit than the inputs.
4965 return std::min(Tmp, Tmp2) - 1;
4966 case ISD::SUB:
4967 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4968 if (Tmp2 == 1) return 1; // Early out.
4969
4970 // Handle NEG.
4971 if (ConstantSDNode *CLHS =
4972 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4973 if (CLHS->isZero()) {
4974 KnownBits Known =
4975 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4976 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4977 // sign bits set.
4978 if ((Known.Zero | 1).isAllOnes())
4979 return VTBits;
4980
4981 // If the input is known to be positive (the sign bit is known clear),
4982 // the output of the NEG has the same number of sign bits as the input.
4983 if (Known.isNonNegative())
4984 return Tmp2;
4985
4986 // Otherwise, we treat this like a SUB.
4987 }
4988
4989 // Sub can have at most one carry bit. Thus we know that the output
4990 // is, at worst, one more bit than the inputs.
4991 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4992 if (Tmp == 1) return 1; // Early out.
4993 return std::min(Tmp, Tmp2) - 1;
4994 case ISD::MUL: {
4995 // The output of the Mul can be at most twice the valid bits in the inputs.
4996 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4997 if (SignBitsOp0 == 1)
4998 break;
4999 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5000 if (SignBitsOp1 == 1)
5001 break;
5002 unsigned OutValidBits =
5003 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5004 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5005 }
5006 case ISD::AVGCEILS:
5007 case ISD::AVGFLOORS:
5008 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5009 if (Tmp == 1)
5010 return 1; // Early out.
5011 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5012 return std::min(Tmp, Tmp2);
5013 case ISD::SREM:
5014 // The sign bit is the LHS's sign bit, except when the result of the
5015 // remainder is zero. The magnitude of the result should be less than or
5016 // equal to the magnitude of the LHS. Therefore, the result should have
5017 // at least as many sign bits as the left hand side.
5018 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5019 case ISD::TRUNCATE: {
5020 // Check if the sign bits of source go down as far as the truncated value.
5021 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5022 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5023 if (NumSrcSignBits > (NumSrcBits - VTBits))
5024 return NumSrcSignBits - (NumSrcBits - VTBits);
5025 break;
5026 }
5027 case ISD::EXTRACT_ELEMENT: {
5028 if (VT.isScalableVector())
5029 break;
5030 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5031 const int BitWidth = Op.getValueSizeInBits();
5032 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5033
5034 // Get reverse index (starting from 1), Op1 value indexes elements from
5035 // little end. Sign starts at big end.
5036 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5037
5038 // If the sign portion ends in our element the subtraction gives correct
5039 // result. Otherwise it gives either negative or > bitwidth result
5040 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5041 }
5043 if (VT.isScalableVector())
5044 break;
5045 // If we know the element index, split the demand between the
5046 // source vector and the inserted element, otherwise assume we need
5047 // the original demanded vector elements and the value.
5048 SDValue InVec = Op.getOperand(0);
5049 SDValue InVal = Op.getOperand(1);
5050 SDValue EltNo = Op.getOperand(2);
5051 bool DemandedVal = true;
5052 APInt DemandedVecElts = DemandedElts;
5053 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5054 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5055 unsigned EltIdx = CEltNo->getZExtValue();
5056 DemandedVal = !!DemandedElts[EltIdx];
5057 DemandedVecElts.clearBit(EltIdx);
5058 }
5059 Tmp = std::numeric_limits<unsigned>::max();
5060 if (DemandedVal) {
5061 // TODO - handle implicit truncation of inserted elements.
5062 if (InVal.getScalarValueSizeInBits() != VTBits)
5063 break;
5064 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5065 Tmp = std::min(Tmp, Tmp2);
5066 }
5067 if (!!DemandedVecElts) {
5068 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5069 Tmp = std::min(Tmp, Tmp2);
5070 }
5071 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5072 return Tmp;
5073 }
5075 assert(!VT.isScalableVector());
5076 SDValue InVec = Op.getOperand(0);
5077 SDValue EltNo = Op.getOperand(1);
5078 EVT VecVT = InVec.getValueType();
5079 // ComputeNumSignBits not yet implemented for scalable vectors.
5080 if (VecVT.isScalableVector())
5081 break;
5082 const unsigned BitWidth = Op.getValueSizeInBits();
5083 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5084 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5085
5086 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5087 // anything about sign bits. But if the sizes match we can derive knowledge
5088 // about sign bits from the vector operand.
5089 if (BitWidth != EltBitWidth)
5090 break;
5091
5092 // If we know the element index, just demand that vector element, else for
5093 // an unknown element index, ignore DemandedElts and demand them all.
5094 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5095 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5096 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5097 DemandedSrcElts =
5098 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5099
5100 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5101 }
5103 // Offset the demanded elts by the subvector index.
5104 SDValue Src = Op.getOperand(0);
5105 // Bail until we can represent demanded elements for scalable vectors.
5106 if (Src.getValueType().isScalableVector())
5107 break;
5108 uint64_t Idx = Op.getConstantOperandVal(1);
5109 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5110 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5111 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5112 }
5113 case ISD::CONCAT_VECTORS: {
5114 if (VT.isScalableVector())
5115 break;
5116 // Determine the minimum number of sign bits across all demanded
5117 // elts of the input vectors. Early out if the result is already 1.
5118 Tmp = std::numeric_limits<unsigned>::max();
5119 EVT SubVectorVT = Op.getOperand(0).getValueType();
5120 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5121 unsigned NumSubVectors = Op.getNumOperands();
5122 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5123 APInt DemandedSub =
5124 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5125 if (!DemandedSub)
5126 continue;
5127 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5128 Tmp = std::min(Tmp, Tmp2);
5129 }
5130 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5131 return Tmp;
5132 }
5133 case ISD::INSERT_SUBVECTOR: {
5134 if (VT.isScalableVector())
5135 break;
5136 // Demand any elements from the subvector and the remainder from the src its
5137 // inserted into.
5138 SDValue Src = Op.getOperand(0);
5139 SDValue Sub = Op.getOperand(1);
5140 uint64_t Idx = Op.getConstantOperandVal(2);
5141 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5142 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5143 APInt DemandedSrcElts = DemandedElts;
5144 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5145
5146 Tmp = std::numeric_limits<unsigned>::max();
5147 if (!!DemandedSubElts) {
5148 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5149 if (Tmp == 1)
5150 return 1; // early-out
5151 }
5152 if (!!DemandedSrcElts) {
5153 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5154 Tmp = std::min(Tmp, Tmp2);
5155 }
5156 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5157 return Tmp;
5158 }
5159 case ISD::LOAD: {
5161 if (const MDNode *Ranges = LD->getRanges()) {
5162 if (DemandedElts != 1)
5163 break;
5164
5166 if (VTBits > CR.getBitWidth()) {
5167 switch (LD->getExtensionType()) {
5168 case ISD::SEXTLOAD:
5169 CR = CR.signExtend(VTBits);
5170 break;
5171 case ISD::ZEXTLOAD:
5172 CR = CR.zeroExtend(VTBits);
5173 break;
5174 default:
5175 break;
5176 }
5177 }
5178
5179 if (VTBits != CR.getBitWidth())
5180 break;
5181 return std::min(CR.getSignedMin().getNumSignBits(),
5183 }
5184
5185 break;
5186 }
5187 case ISD::ATOMIC_CMP_SWAP:
5188 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
5189 case ISD::ATOMIC_SWAP:
5190 case ISD::ATOMIC_LOAD_ADD:
5191 case ISD::ATOMIC_LOAD_SUB:
5192 case ISD::ATOMIC_LOAD_AND:
5193 case ISD::ATOMIC_LOAD_CLR:
5194 case ISD::ATOMIC_LOAD_OR:
5195 case ISD::ATOMIC_LOAD_XOR:
5196 case ISD::ATOMIC_LOAD_NAND:
5197 case ISD::ATOMIC_LOAD_MIN:
5198 case ISD::ATOMIC_LOAD_MAX:
5199 case ISD::ATOMIC_LOAD_UMIN:
5200 case ISD::ATOMIC_LOAD_UMAX:
5201 case ISD::ATOMIC_LOAD: {
5202 auto *AT = cast<AtomicSDNode>(Op);
5203 // If we are looking at the loaded value.
5204 if (Op.getResNo() == 0) {
5205 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5206 if (Tmp == VTBits)
5207 return 1; // early-out
5208
5209 // For atomic_load, prefer to use the extension type.
5210 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5211 switch (AT->getExtensionType()) {
5212 default:
5213 break;
5214 case ISD::SEXTLOAD:
5215 return VTBits - Tmp + 1;
5216 case ISD::ZEXTLOAD:
5217 return VTBits - Tmp;
5218 }
5219 }
5220
5221 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5222 return VTBits - Tmp + 1;
5223 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5224 return VTBits - Tmp;
5225 }
5226 break;
5227 }
5228 }
5229
5230 // If we are looking at the loaded value of the SDNode.
5231 if (Op.getResNo() == 0) {
5232 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5233 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5234 unsigned ExtType = LD->getExtensionType();
5235 switch (ExtType) {
5236 default: break;
5237 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5238 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5239 return VTBits - Tmp + 1;
5240 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5241 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5242 return VTBits - Tmp;
5243 case ISD::NON_EXTLOAD:
5244 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5245 // We only need to handle vectors - computeKnownBits should handle
5246 // scalar cases.
5247 Type *CstTy = Cst->getType();
5248 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5249 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5250 VTBits == CstTy->getScalarSizeInBits()) {
5251 Tmp = VTBits;
5252 for (unsigned i = 0; i != NumElts; ++i) {
5253 if (!DemandedElts[i])
5254 continue;
5255 if (Constant *Elt = Cst->getAggregateElement(i)) {
5256 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5257 const APInt &Value = CInt->getValue();
5258 Tmp = std::min(Tmp, Value.getNumSignBits());
5259 continue;
5260 }
5261 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5262 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5263 Tmp = std::min(Tmp, Value.getNumSignBits());
5264 continue;
5265 }
5266 }
5267 // Unknown type. Conservatively assume no bits match sign bit.
5268 return 1;
5269 }
5270 return Tmp;
5271 }
5272 }
5273 break;
5274 }
5275 }
5276 }
5277
5278 // Allow the target to implement this method for its nodes.
5279 if (Opcode >= ISD::BUILTIN_OP_END ||
5280 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5281 Opcode == ISD::INTRINSIC_W_CHAIN ||
5282 Opcode == ISD::INTRINSIC_VOID) {
5283 // TODO: This can probably be removed once target code is audited. This
5284 // is here purely to reduce patch size and review complexity.
5285 if (!VT.isScalableVector()) {
5286 unsigned NumBits =
5287 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5288 if (NumBits > 1)
5289 FirstAnswer = std::max(FirstAnswer, NumBits);
5290 }
5291 }
5292
5293 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5294 // use this information.
5295 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5296 return std::max(FirstAnswer, Known.countMinSignBits());
5297}
5298
5300 unsigned Depth) const {
5301 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5302 return Op.getScalarValueSizeInBits() - SignBits + 1;
5303}
5304
5306 const APInt &DemandedElts,
5307 unsigned Depth) const {
5308 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5309 return Op.getScalarValueSizeInBits() - SignBits + 1;
5310}
5311
5313 unsigned Depth) const {
5314 // Early out for FREEZE.
5315 if (Op.getOpcode() == ISD::FREEZE)
5316 return true;
5317
5318 EVT VT = Op.getValueType();
5319 APInt DemandedElts = VT.isFixedLengthVector()
5321 : APInt(1, 1);
5322 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5323}
5324
5326 const APInt &DemandedElts,
5327 bool PoisonOnly,
5328 unsigned Depth) const {
5329 unsigned Opcode = Op.getOpcode();
5330
5331 // Early out for FREEZE.
5332 if (Opcode == ISD::FREEZE)
5333 return true;
5334
5335 if (Depth >= MaxRecursionDepth)
5336 return false; // Limit search depth.
5337
5338 if (isIntOrFPConstant(Op))
5339 return true;
5340
5341 switch (Opcode) {
5342 case ISD::CONDCODE:
5343 case ISD::VALUETYPE:
5344 case ISD::FrameIndex:
5346 case ISD::CopyFromReg:
5347 return true;
5348
5349 case ISD::POISON:
5350 return false;
5351
5352 case ISD::UNDEF:
5353 return PoisonOnly;
5354
5355 case ISD::BUILD_VECTOR:
5356 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5357 // this shouldn't affect the result.
5358 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5359 if (!DemandedElts[i])
5360 continue;
5362 Depth + 1))
5363 return false;
5364 }
5365 return true;
5366
5368 SDValue Src = Op.getOperand(0);
5369 if (Src.getValueType().isScalableVector())
5370 break;
5371 uint64_t Idx = Op.getConstantOperandVal(1);
5372 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5373 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5374 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5375 Depth + 1);
5376 }
5377
5378 case ISD::INSERT_SUBVECTOR: {
5379 if (Op.getValueType().isScalableVector())
5380 break;
5381 SDValue Src = Op.getOperand(0);
5382 SDValue Sub = Op.getOperand(1);
5383 uint64_t Idx = Op.getConstantOperandVal(2);
5384 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5385 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5386 APInt DemandedSrcElts = DemandedElts;
5387 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5388
5389 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5390 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5391 return false;
5392 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5393 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5394 return false;
5395 return true;
5396 }
5397
5399 SDValue Src = Op.getOperand(0);
5400 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5401 EVT SrcVT = Src.getValueType();
5402 if (SrcVT.isFixedLengthVector() && IndexC &&
5403 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5404 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5405 IndexC->getZExtValue());
5406 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5407 Depth + 1);
5408 }
5409 break;
5410 }
5411
5413 SDValue InVec = Op.getOperand(0);
5414 SDValue InVal = Op.getOperand(1);
5415 SDValue EltNo = Op.getOperand(2);
5416 EVT VT = InVec.getValueType();
5417 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5418 if (IndexC && VT.isFixedLengthVector() &&
5419 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5420 if (DemandedElts[IndexC->getZExtValue()] &&
5422 return false;
5423 APInt InVecDemandedElts = DemandedElts;
5424 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5425 if (!!InVecDemandedElts &&
5427 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5428 InVecDemandedElts, PoisonOnly, Depth + 1))
5429 return false;
5430 return true;
5431 }
5432 break;
5433 }
5434
5436 // Check upper (known undef) elements.
5437 if (DemandedElts.ugt(1) && !PoisonOnly)
5438 return false;
5439 // Check element zero.
5440 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5441 Op.getOperand(0), PoisonOnly, Depth + 1))
5442 return false;
5443 return true;
5444
5445 case ISD::SPLAT_VECTOR:
5446 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5447 Depth + 1);
5448
5449 case ISD::VECTOR_SHUFFLE: {
5450 APInt DemandedLHS, DemandedRHS;
5451 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5452 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5453 DemandedElts, DemandedLHS, DemandedRHS,
5454 /*AllowUndefElts=*/false))
5455 return false;
5456 if (!DemandedLHS.isZero() &&
5457 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5458 PoisonOnly, Depth + 1))
5459 return false;
5460 if (!DemandedRHS.isZero() &&
5461 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5462 PoisonOnly, Depth + 1))
5463 return false;
5464 return true;
5465 }
5466
5467 case ISD::SHL:
5468 case ISD::SRL:
5469 case ISD::SRA:
5470 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5471 // enough to check operand 0 if Op can't create undef/poison.
5472 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5473 /*ConsiderFlags*/ true, Depth) &&
5474 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5475 PoisonOnly, Depth + 1);
5476
5477 case ISD::BSWAP:
5478 case ISD::CTPOP:
5479 case ISD::BITREVERSE:
5480 case ISD::AND:
5481 case ISD::OR:
5482 case ISD::XOR:
5483 case ISD::ADD:
5484 case ISD::SUB:
5485 case ISD::MUL:
5486 case ISD::SADDSAT:
5487 case ISD::UADDSAT:
5488 case ISD::SSUBSAT:
5489 case ISD::USUBSAT:
5490 case ISD::SSHLSAT:
5491 case ISD::USHLSAT:
5492 case ISD::SMIN:
5493 case ISD::SMAX:
5494 case ISD::UMIN:
5495 case ISD::UMAX:
5496 case ISD::ZERO_EXTEND:
5497 case ISD::SIGN_EXTEND:
5498 case ISD::ANY_EXTEND:
5499 case ISD::TRUNCATE:
5500 case ISD::VSELECT: {
5501 // If Op can't create undef/poison and none of its operands are undef/poison
5502 // then Op is never undef/poison. A difference from the more common check
5503 // below, outside the switch, is that we handle elementwise operations for
5504 // which the DemandedElts mask is valid for all operands here.
5505 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5506 /*ConsiderFlags*/ true, Depth) &&
5507 all_of(Op->ops(), [&](SDValue V) {
5508 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5509 PoisonOnly, Depth + 1);
5510 });
5511 }
5512
5513 // TODO: Search for noundef attributes from library functions.
5514
5515 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5516
5517 default:
5518 // Allow the target to implement this method for its nodes.
5519 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5520 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5521 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5522 Op, DemandedElts, *this, PoisonOnly, Depth);
5523 break;
5524 }
5525
5526 // If Op can't create undef/poison and none of its operands are undef/poison
5527 // then Op is never undef/poison.
5528 // NOTE: TargetNodes can handle this in themselves in
5529 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5530 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5531 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5532 Depth) &&
5533 all_of(Op->ops(), [&](SDValue V) {
5534 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5535 });
5536}
5537
5539 bool ConsiderFlags,
5540 unsigned Depth) const {
5541 EVT VT = Op.getValueType();
5542 APInt DemandedElts = VT.isFixedLengthVector()
5544 : APInt(1, 1);
5545 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5546 Depth);
5547}
5548
5550 bool PoisonOnly, bool ConsiderFlags,
5551 unsigned Depth) const {
5552 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5553 return true;
5554
5555 unsigned Opcode = Op.getOpcode();
5556 switch (Opcode) {
5557 case ISD::AssertSext:
5558 case ISD::AssertZext:
5559 case ISD::AssertAlign:
5561 // Assertion nodes can create poison if the assertion fails.
5562 return true;
5563
5564 case ISD::FREEZE:
5568 case ISD::SADDSAT:
5569 case ISD::UADDSAT:
5570 case ISD::SSUBSAT:
5571 case ISD::USUBSAT:
5572 case ISD::MULHU:
5573 case ISD::MULHS:
5574 case ISD::AVGFLOORS:
5575 case ISD::AVGFLOORU:
5576 case ISD::AVGCEILS:
5577 case ISD::AVGCEILU:
5578 case ISD::ABDU:
5579 case ISD::ABDS:
5580 case ISD::SMIN:
5581 case ISD::SMAX:
5582 case ISD::SCMP:
5583 case ISD::UMIN:
5584 case ISD::UMAX:
5585 case ISD::UCMP:
5586 case ISD::AND:
5587 case ISD::XOR:
5588 case ISD::ROTL:
5589 case ISD::ROTR:
5590 case ISD::FSHL:
5591 case ISD::FSHR:
5592 case ISD::BSWAP:
5593 case ISD::CTTZ:
5594 case ISD::CTLZ:
5595 case ISD::CTPOP:
5596 case ISD::BITREVERSE:
5597 case ISD::PARITY:
5598 case ISD::SIGN_EXTEND:
5599 case ISD::TRUNCATE:
5603 case ISD::BITCAST:
5604 case ISD::BUILD_VECTOR:
5605 case ISD::BUILD_PAIR:
5606 case ISD::SPLAT_VECTOR:
5607 case ISD::FABS:
5608 return false;
5609
5610 case ISD::ABS:
5611 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5612 // Different to Intrinsic::abs.
5613 return false;
5614
5615 case ISD::ADDC:
5616 case ISD::SUBC:
5617 case ISD::ADDE:
5618 case ISD::SUBE:
5619 case ISD::SADDO:
5620 case ISD::SSUBO:
5621 case ISD::SMULO:
5622 case ISD::SADDO_CARRY:
5623 case ISD::SSUBO_CARRY:
5624 case ISD::UADDO:
5625 case ISD::USUBO:
5626 case ISD::UMULO:
5627 case ISD::UADDO_CARRY:
5628 case ISD::USUBO_CARRY:
5629 // No poison on result or overflow flags.
5630 return false;
5631
5632 case ISD::SELECT_CC:
5633 case ISD::SETCC: {
5634 // Integer setcc cannot create undef or poison.
5635 if (Op.getOperand(0).getValueType().isInteger())
5636 return false;
5637
5638 // FP compares are more complicated. They can create poison for nan/infinity
5639 // based on options and flags. The options and flags also cause special
5640 // nonan condition codes to be used. Those condition codes may be preserved
5641 // even if the nonan flag is dropped somewhere.
5642 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5643 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5644 return (unsigned)CCCode & 0x10U;
5645 }
5646
5647 case ISD::OR:
5648 case ISD::ZERO_EXTEND:
5649 case ISD::SELECT:
5650 case ISD::VSELECT:
5651 case ISD::ADD:
5652 case ISD::SUB:
5653 case ISD::MUL:
5654 case ISD::FNEG:
5655 case ISD::FADD:
5656 case ISD::FSUB:
5657 case ISD::FMUL:
5658 case ISD::FDIV:
5659 case ISD::FREM:
5660 case ISD::FCOPYSIGN:
5661 case ISD::FMA:
5662 case ISD::FMAD:
5663 case ISD::FMULADD:
5664 case ISD::FP_EXTEND:
5667 // No poison except from flags (which is handled above)
5668 return false;
5669
5670 case ISD::SHL:
5671 case ISD::SRL:
5672 case ISD::SRA:
5673 // If the max shift amount isn't in range, then the shift can
5674 // create poison.
5675 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5676
5679 // If the amount is zero then the result will be poison.
5680 // TODO: Add isKnownNeverZero DemandedElts handling.
5681 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5682
5684 // Check if we demand any upper (undef) elements.
5685 return !PoisonOnly && DemandedElts.ugt(1);
5686
5689 // Ensure that the element index is in bounds.
5690 EVT VecVT = Op.getOperand(0).getValueType();
5691 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5692 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5693 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5694 }
5695
5696 case ISD::VECTOR_SHUFFLE: {
5697 // Check for any demanded shuffle element that is undef.
5698 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5699 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5700 if (Elt < 0 && DemandedElts[Idx])
5701 return true;
5702 return false;
5703 }
5704
5706 return false;
5707
5708 default:
5709 // Allow the target to implement this method for its nodes.
5710 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5711 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5712 return TLI->canCreateUndefOrPoisonForTargetNode(
5713 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5714 break;
5715 }
5716
5717 // Be conservative and return true.
5718 return true;
5719}
5720
5721bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5722 unsigned Opcode = Op.getOpcode();
5723 if (Opcode == ISD::OR)
5724 return Op->getFlags().hasDisjoint() ||
5725 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5726 if (Opcode == ISD::XOR)
5727 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5728 return false;
5729}
5730
5732 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5733 (Op.isAnyAdd() || isADDLike(Op));
5734}
5735
5737 unsigned Depth) const {
5738 EVT VT = Op.getValueType();
5739
5740 // Since the number of lanes in a scalable vector is unknown at compile time,
5741 // we track one bit which is implicitly broadcast to all lanes. This means
5742 // that all lanes in a scalable vector are considered demanded.
5743 APInt DemandedElts = VT.isFixedLengthVector()
5745 : APInt(1, 1);
5746
5747 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5748}
5749
5751 bool SNaN, unsigned Depth) const {
5752 assert(!DemandedElts.isZero() && "No demanded elements");
5753
5754 // If we're told that NaNs won't happen, assume they won't.
5755 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5756 return true;
5757
5758 if (Depth >= MaxRecursionDepth)
5759 return false; // Limit search depth.
5760
5761 // If the value is a constant, we can obviously see if it is a NaN or not.
5763 return !C->getValueAPF().isNaN() ||
5764 (SNaN && !C->getValueAPF().isSignaling());
5765 }
5766
5767 unsigned Opcode = Op.getOpcode();
5768 switch (Opcode) {
5769 case ISD::FADD:
5770 case ISD::FSUB:
5771 case ISD::FMUL:
5772 case ISD::FDIV:
5773 case ISD::FREM:
5774 case ISD::FSIN:
5775 case ISD::FCOS:
5776 case ISD::FTAN:
5777 case ISD::FASIN:
5778 case ISD::FACOS:
5779 case ISD::FATAN:
5780 case ISD::FATAN2:
5781 case ISD::FSINH:
5782 case ISD::FCOSH:
5783 case ISD::FTANH:
5784 case ISD::FMA:
5785 case ISD::FMULADD:
5786 case ISD::FMAD: {
5787 if (SNaN)
5788 return true;
5789 // TODO: Need isKnownNeverInfinity
5790 return false;
5791 }
5792 case ISD::FCANONICALIZE:
5793 case ISD::FEXP:
5794 case ISD::FEXP2:
5795 case ISD::FEXP10:
5796 case ISD::FTRUNC:
5797 case ISD::FFLOOR:
5798 case ISD::FCEIL:
5799 case ISD::FROUND:
5800 case ISD::FROUNDEVEN:
5801 case ISD::LROUND:
5802 case ISD::LLROUND:
5803 case ISD::FRINT:
5804 case ISD::LRINT:
5805 case ISD::LLRINT:
5806 case ISD::FNEARBYINT:
5807 case ISD::FLDEXP: {
5808 if (SNaN)
5809 return true;
5810 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5811 }
5812 case ISD::FABS:
5813 case ISD::FNEG:
5814 case ISD::FCOPYSIGN: {
5815 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5816 }
5817 case ISD::SELECT:
5818 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5819 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5820 case ISD::FP_EXTEND:
5821 case ISD::FP_ROUND: {
5822 if (SNaN)
5823 return true;
5824 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5825 }
5826 case ISD::SINT_TO_FP:
5827 case ISD::UINT_TO_FP:
5828 return true;
5829 case ISD::FSQRT: // Need is known positive
5830 case ISD::FLOG:
5831 case ISD::FLOG2:
5832 case ISD::FLOG10:
5833 case ISD::FPOWI:
5834 case ISD::FPOW: {
5835 if (SNaN)
5836 return true;
5837 // TODO: Refine on operand
5838 return false;
5839 }
5840 case ISD::FMINNUM:
5841 case ISD::FMAXNUM:
5842 case ISD::FMINIMUMNUM:
5843 case ISD::FMAXIMUMNUM: {
5844 // Only one needs to be known not-nan, since it will be returned if the
5845 // other ends up being one.
5846 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5847 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5848 }
5849 case ISD::FMINNUM_IEEE:
5850 case ISD::FMAXNUM_IEEE: {
5851 if (SNaN)
5852 return true;
5853 // This can return a NaN if either operand is an sNaN, or if both operands
5854 // are NaN.
5855 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5856 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5857 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5858 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5859 }
5860 case ISD::FMINIMUM:
5861 case ISD::FMAXIMUM: {
5862 // TODO: Does this quiet or return the origina NaN as-is?
5863 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5864 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5865 }
5867 SDValue Src = Op.getOperand(0);
5868 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5869 EVT SrcVT = Src.getValueType();
5870 if (SrcVT.isFixedLengthVector() && Idx &&
5871 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5872 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5873 Idx->getZExtValue());
5874 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5875 }
5876 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5877 }
5879 SDValue Src = Op.getOperand(0);
5880 if (Src.getValueType().isFixedLengthVector()) {
5881 unsigned Idx = Op.getConstantOperandVal(1);
5882 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5883 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5884 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5885 }
5886 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5887 }
5888 case ISD::INSERT_SUBVECTOR: {
5889 SDValue BaseVector = Op.getOperand(0);
5890 SDValue SubVector = Op.getOperand(1);
5891 EVT BaseVectorVT = BaseVector.getValueType();
5892 if (BaseVectorVT.isFixedLengthVector()) {
5893 unsigned Idx = Op.getConstantOperandVal(2);
5894 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5895 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5896
5897 // Clear/Extract the bits at the position where the subvector will be
5898 // inserted.
5899 APInt DemandedMask =
5900 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5901 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5902 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5903
5904 bool NeverNaN = true;
5905 if (!DemandedSrcElts.isZero())
5906 NeverNaN &=
5907 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5908 if (NeverNaN && !DemandedSubElts.isZero())
5909 NeverNaN &=
5910 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5911 return NeverNaN;
5912 }
5913 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5914 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5915 }
5916 case ISD::BUILD_VECTOR: {
5917 unsigned NumElts = Op.getNumOperands();
5918 for (unsigned I = 0; I != NumElts; ++I)
5919 if (DemandedElts[I] &&
5920 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
5921 return false;
5922 return true;
5923 }
5924 case ISD::AssertNoFPClass: {
5925 FPClassTest NoFPClass =
5926 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
5927 if ((NoFPClass & fcNan) == fcNan)
5928 return true;
5929 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
5930 return true;
5931 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5932 }
5933 default:
5934 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5935 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
5936 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
5937 Depth);
5938 }
5939
5940 return false;
5941 }
5942}
5943
5945 assert(Op.getValueType().isFloatingPoint() &&
5946 "Floating point type expected");
5947
5948 // If the value is a constant, we can obviously see if it is a zero or not.
5950 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5951}
5952
5954 if (Depth >= MaxRecursionDepth)
5955 return false; // Limit search depth.
5956
5957 assert(!Op.getValueType().isFloatingPoint() &&
5958 "Floating point types unsupported - use isKnownNeverZeroFloat");
5959
5960 // If the value is a constant, we can obviously see if it is a zero or not.
5962 [](ConstantSDNode *C) { return !C->isZero(); }))
5963 return true;
5964
5965 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5966 // some degree.
5967 switch (Op.getOpcode()) {
5968 default:
5969 break;
5970
5971 case ISD::OR:
5972 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5973 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5974
5975 case ISD::VSELECT:
5976 case ISD::SELECT:
5977 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5978 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5979
5980 case ISD::SHL: {
5981 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5982 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5983 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5984 // 1 << X is never zero.
5985 if (ValKnown.One[0])
5986 return true;
5987 // If max shift cnt of known ones is non-zero, result is non-zero.
5988 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5989 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5990 !ValKnown.One.shl(MaxCnt).isZero())
5991 return true;
5992 break;
5993 }
5994 case ISD::UADDSAT:
5995 case ISD::UMAX:
5996 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5997 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5998
5999 // For smin/smax: If either operand is known negative/positive
6000 // respectively we don't need the other to be known at all.
6001 case ISD::SMAX: {
6002 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6003 if (Op1.isStrictlyPositive())
6004 return true;
6005
6006 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6007 if (Op0.isStrictlyPositive())
6008 return true;
6009
6010 if (Op1.isNonZero() && Op0.isNonZero())
6011 return true;
6012
6013 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6014 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6015 }
6016 case ISD::SMIN: {
6017 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6018 if (Op1.isNegative())
6019 return true;
6020
6021 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6022 if (Op0.isNegative())
6023 return true;
6024
6025 if (Op1.isNonZero() && Op0.isNonZero())
6026 return true;
6027
6028 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6029 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6030 }
6031 case ISD::UMIN:
6032 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6033 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6034
6035 case ISD::ROTL:
6036 case ISD::ROTR:
6037 case ISD::BITREVERSE:
6038 case ISD::BSWAP:
6039 case ISD::CTPOP:
6040 case ISD::ABS:
6041 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6042
6043 case ISD::SRA:
6044 case ISD::SRL: {
6045 if (Op->getFlags().hasExact())
6046 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6047 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6048 if (ValKnown.isNegative())
6049 return true;
6050 // If max shift cnt of known ones is non-zero, result is non-zero.
6051 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6052 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6053 !ValKnown.One.lshr(MaxCnt).isZero())
6054 return true;
6055 break;
6056 }
6057 case ISD::UDIV:
6058 case ISD::SDIV:
6059 // div exact can only produce a zero if the dividend is zero.
6060 // TODO: For udiv this is also true if Op1 u<= Op0
6061 if (Op->getFlags().hasExact())
6062 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6063 break;
6064
6065 case ISD::ADD:
6066 if (Op->getFlags().hasNoUnsignedWrap())
6067 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6068 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6069 return true;
6070 // TODO: There are a lot more cases we can prove for add.
6071 break;
6072
6073 case ISD::SUB: {
6074 if (isNullConstant(Op.getOperand(0)))
6075 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6076
6077 std::optional<bool> ne =
6078 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6079 computeKnownBits(Op.getOperand(1), Depth + 1));
6080 return ne && *ne;
6081 }
6082
6083 case ISD::MUL:
6084 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6085 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6086 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6087 return true;
6088 break;
6089
6090 case ISD::ZERO_EXTEND:
6091 case ISD::SIGN_EXTEND:
6092 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6093 case ISD::VSCALE: {
6095 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6096 ConstantRange CR =
6097 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6098 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6099 return true;
6100 break;
6101 }
6102 }
6103
6105}
6106
6108 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6109 return !C1->isNegative();
6110
6111 switch (Op.getOpcode()) {
6112 case ISD::FABS:
6113 case ISD::FEXP:
6114 case ISD::FEXP2:
6115 case ISD::FEXP10:
6116 return true;
6117 default:
6118 return false;
6119 }
6120
6121 llvm_unreachable("covered opcode switch");
6122}
6123
6125 // Check the obvious case.
6126 if (A == B) return true;
6127
6128 // For negative and positive zero.
6131 if (CA->isZero() && CB->isZero()) return true;
6132
6133 // Otherwise they may not be equal.
6134 return false;
6135}
6136
6137// Only bits set in Mask must be negated, other bits may be arbitrary.
6139 if (isBitwiseNot(V, AllowUndefs))
6140 return V.getOperand(0);
6141
6142 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6143 // bits in the non-extended part.
6144 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6145 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6146 return SDValue();
6147 SDValue ExtArg = V.getOperand(0);
6148 if (ExtArg.getScalarValueSizeInBits() >=
6149 MaskC->getAPIntValue().getActiveBits() &&
6150 isBitwiseNot(ExtArg, AllowUndefs) &&
6151 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6152 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6153 return ExtArg.getOperand(0).getOperand(0);
6154 return SDValue();
6155}
6156
6158 // Match masked merge pattern (X & ~M) op (Y & M)
6159 // Including degenerate case (X & ~M) op M
6160 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6161 SDValue Other) {
6162 if (SDValue NotOperand =
6163 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6164 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6165 NotOperand->getOpcode() == ISD::TRUNCATE)
6166 NotOperand = NotOperand->getOperand(0);
6167
6168 if (Other == NotOperand)
6169 return true;
6170 if (Other->getOpcode() == ISD::AND)
6171 return NotOperand == Other->getOperand(0) ||
6172 NotOperand == Other->getOperand(1);
6173 }
6174 return false;
6175 };
6176
6177 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6178 A = A->getOperand(0);
6179
6180 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6181 B = B->getOperand(0);
6182
6183 if (A->getOpcode() == ISD::AND)
6184 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6185 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6186 return false;
6187}
6188
6189// FIXME: unify with llvm::haveNoCommonBitsSet.
6191 assert(A.getValueType() == B.getValueType() &&
6192 "Values must have the same type");
6195 return true;
6198}
6199
6200static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6201 SelectionDAG &DAG) {
6202 if (cast<ConstantSDNode>(Step)->isZero())
6203 return DAG.getConstant(0, DL, VT);
6204
6205 return SDValue();
6206}
6207
6210 SelectionDAG &DAG) {
6211 int NumOps = Ops.size();
6212 assert(NumOps != 0 && "Can't build an empty vector!");
6213 assert(!VT.isScalableVector() &&
6214 "BUILD_VECTOR cannot be used with scalable types");
6215 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6216 "Incorrect element count in BUILD_VECTOR!");
6217
6218 // BUILD_VECTOR of UNDEFs is UNDEF.
6219 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6220 return DAG.getUNDEF(VT);
6221
6222 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6223 SDValue IdentitySrc;
6224 bool IsIdentity = true;
6225 for (int i = 0; i != NumOps; ++i) {
6227 Ops[i].getOperand(0).getValueType() != VT ||
6228 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6229 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6230 Ops[i].getConstantOperandAPInt(1) != i) {
6231 IsIdentity = false;
6232 break;
6233 }
6234 IdentitySrc = Ops[i].getOperand(0);
6235 }
6236 if (IsIdentity)
6237 return IdentitySrc;
6238
6239 return SDValue();
6240}
6241
6242/// Try to simplify vector concatenation to an input value, undef, or build
6243/// vector.
6246 SelectionDAG &DAG) {
6247 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6249 [Ops](SDValue Op) {
6250 return Ops[0].getValueType() == Op.getValueType();
6251 }) &&
6252 "Concatenation of vectors with inconsistent value types!");
6253 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6254 VT.getVectorElementCount() &&
6255 "Incorrect element count in vector concatenation!");
6256
6257 if (Ops.size() == 1)
6258 return Ops[0];
6259
6260 // Concat of UNDEFs is UNDEF.
6261 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6262 return DAG.getUNDEF(VT);
6263
6264 // Scan the operands and look for extract operations from a single source
6265 // that correspond to insertion at the same location via this concatenation:
6266 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6267 SDValue IdentitySrc;
6268 bool IsIdentity = true;
6269 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6270 SDValue Op = Ops[i];
6271 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6272 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6273 Op.getOperand(0).getValueType() != VT ||
6274 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6275 Op.getConstantOperandVal(1) != IdentityIndex) {
6276 IsIdentity = false;
6277 break;
6278 }
6279 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6280 "Unexpected identity source vector for concat of extracts");
6281 IdentitySrc = Op.getOperand(0);
6282 }
6283 if (IsIdentity) {
6284 assert(IdentitySrc && "Failed to set source vector of extracts");
6285 return IdentitySrc;
6286 }
6287
6288 // The code below this point is only designed to work for fixed width
6289 // vectors, so we bail out for now.
6290 if (VT.isScalableVector())
6291 return SDValue();
6292
6293 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6294 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6295 // BUILD_VECTOR.
6296 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6297 EVT SVT = VT.getScalarType();
6299 for (SDValue Op : Ops) {
6300 EVT OpVT = Op.getValueType();
6301 if (Op.isUndef())
6302 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6303 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6304 Elts.append(Op->op_begin(), Op->op_end());
6305 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6306 OpVT.getVectorNumElements() == 1 &&
6307 isNullConstant(Op.getOperand(2)))
6308 Elts.push_back(Op.getOperand(1));
6309 else
6310 return SDValue();
6311 }
6312
6313 // BUILD_VECTOR requires all inputs to be of the same type, find the
6314 // maximum type and extend them all.
6315 for (SDValue Op : Elts)
6316 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6317
6318 if (SVT.bitsGT(VT.getScalarType())) {
6319 for (SDValue &Op : Elts) {
6320 if (Op.isUndef())
6321 Op = DAG.getUNDEF(SVT);
6322 else
6323 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6324 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6325 : DAG.getSExtOrTrunc(Op, DL, SVT);
6326 }
6327 }
6328
6329 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6330 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6331 return V;
6332}
6333
6334/// Gets or creates the specified node.
6335SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6336 SDVTList VTs = getVTList(VT);
6338 AddNodeIDNode(ID, Opcode, VTs, {});
6339 void *IP = nullptr;
6340 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6341 return SDValue(E, 0);
6342
6343 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6344 CSEMap.InsertNode(N, IP);
6345
6346 InsertNode(N);
6347 SDValue V = SDValue(N, 0);
6348 NewSDValueDbgMsg(V, "Creating new node: ", this);
6349 return V;
6350}
6351
6352SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6353 SDValue N1) {
6354 SDNodeFlags Flags;
6355 if (Inserter)
6356 Flags = Inserter->getFlags();
6357 return getNode(Opcode, DL, VT, N1, Flags);
6358}
6359
6360SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6361 SDValue N1, const SDNodeFlags Flags) {
6362 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6363
6364 // Constant fold unary operations with a vector integer or float operand.
6365 switch (Opcode) {
6366 default:
6367 // FIXME: Entirely reasonable to perform folding of other unary
6368 // operations here as the need arises.
6369 break;
6370 case ISD::FNEG:
6371 case ISD::FABS:
6372 case ISD::FCEIL:
6373 case ISD::FTRUNC:
6374 case ISD::FFLOOR:
6375 case ISD::FP_EXTEND:
6376 case ISD::FP_TO_SINT:
6377 case ISD::FP_TO_UINT:
6378 case ISD::FP_TO_FP16:
6379 case ISD::FP_TO_BF16:
6380 case ISD::TRUNCATE:
6381 case ISD::ANY_EXTEND:
6382 case ISD::ZERO_EXTEND:
6383 case ISD::SIGN_EXTEND:
6384 case ISD::UINT_TO_FP:
6385 case ISD::SINT_TO_FP:
6386 case ISD::FP16_TO_FP:
6387 case ISD::BF16_TO_FP:
6388 case ISD::BITCAST:
6389 case ISD::ABS:
6390 case ISD::BITREVERSE:
6391 case ISD::BSWAP:
6392 case ISD::CTLZ:
6394 case ISD::CTTZ:
6396 case ISD::CTPOP:
6397 case ISD::STEP_VECTOR: {
6398 SDValue Ops = {N1};
6399 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6400 return Fold;
6401 }
6402 }
6403
6404 unsigned OpOpcode = N1.getNode()->getOpcode();
6405 switch (Opcode) {
6406 case ISD::STEP_VECTOR:
6407 assert(VT.isScalableVector() &&
6408 "STEP_VECTOR can only be used with scalable types");
6409 assert(OpOpcode == ISD::TargetConstant &&
6410 VT.getVectorElementType() == N1.getValueType() &&
6411 "Unexpected step operand");
6412 break;
6413 case ISD::FREEZE:
6414 assert(VT == N1.getValueType() && "Unexpected VT!");
6415 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6416 return N1;
6417 break;
6418 case ISD::TokenFactor:
6419 case ISD::MERGE_VALUES:
6421 return N1; // Factor, merge or concat of one node? No need.
6422 case ISD::BUILD_VECTOR: {
6423 // Attempt to simplify BUILD_VECTOR.
6424 SDValue Ops[] = {N1};
6425 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6426 return V;
6427 break;
6428 }
6429 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6430 case ISD::FP_EXTEND:
6432 "Invalid FP cast!");
6433 if (N1.getValueType() == VT) return N1; // noop conversion.
6434 assert((!VT.isVector() || VT.getVectorElementCount() ==
6436 "Vector element count mismatch!");
6437 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6438 if (N1.isUndef())
6439 return getUNDEF(VT);
6440 break;
6441 case ISD::FP_TO_SINT:
6442 case ISD::FP_TO_UINT:
6443 if (N1.isUndef())
6444 return getUNDEF(VT);
6445 break;
6446 case ISD::SINT_TO_FP:
6447 case ISD::UINT_TO_FP:
6448 // [us]itofp(undef) = 0, because the result value is bounded.
6449 if (N1.isUndef())
6450 return getConstantFP(0.0, DL, VT);
6451 break;
6452 case ISD::SIGN_EXTEND:
6453 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6454 "Invalid SIGN_EXTEND!");
6455 assert(VT.isVector() == N1.getValueType().isVector() &&
6456 "SIGN_EXTEND result type type should be vector iff the operand "
6457 "type is vector!");
6458 if (N1.getValueType() == VT) return N1; // noop extension
6459 assert((!VT.isVector() || VT.getVectorElementCount() ==
6461 "Vector element count mismatch!");
6462 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6463 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6464 SDNodeFlags Flags;
6465 if (OpOpcode == ISD::ZERO_EXTEND)
6466 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6467 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6468 transferDbgValues(N1, NewVal);
6469 return NewVal;
6470 }
6471
6472 if (OpOpcode == ISD::POISON)
6473 return getPOISON(VT);
6474
6475 if (N1.isUndef())
6476 // sext(undef) = 0, because the top bits will all be the same.
6477 return getConstant(0, DL, VT);
6478
6479 // Skip unnecessary sext_inreg pattern:
6480 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6481 if (OpOpcode == ISD::TRUNCATE) {
6482 SDValue OpOp = N1.getOperand(0);
6483 if (OpOp.getValueType() == VT) {
6484 unsigned NumSignExtBits =
6486 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6487 transferDbgValues(N1, OpOp);
6488 return OpOp;
6489 }
6490 }
6491 }
6492 break;
6493 case ISD::ZERO_EXTEND:
6494 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6495 "Invalid ZERO_EXTEND!");
6496 assert(VT.isVector() == N1.getValueType().isVector() &&
6497 "ZERO_EXTEND result type type should be vector iff the operand "
6498 "type is vector!");
6499 if (N1.getValueType() == VT) return N1; // noop extension
6500 assert((!VT.isVector() || VT.getVectorElementCount() ==
6502 "Vector element count mismatch!");
6503 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6504 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6505 SDNodeFlags Flags;
6506 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6507 SDValue NewVal =
6508 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6509 transferDbgValues(N1, NewVal);
6510 return NewVal;
6511 }
6512
6513 if (OpOpcode == ISD::POISON)
6514 return getPOISON(VT);
6515
6516 if (N1.isUndef())
6517 // zext(undef) = 0, because the top bits will be zero.
6518 return getConstant(0, DL, VT);
6519
6520 // Skip unnecessary zext_inreg pattern:
6521 // (zext (trunc x)) -> x iff the upper bits are known zero.
6522 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6523 // use to recognise zext_inreg patterns.
6524 if (OpOpcode == ISD::TRUNCATE) {
6525 SDValue OpOp = N1.getOperand(0);
6526 if (OpOp.getValueType() == VT) {
6527 if (OpOp.getOpcode() != ISD::AND) {
6530 if (MaskedValueIsZero(OpOp, HiBits)) {
6531 transferDbgValues(N1, OpOp);
6532 return OpOp;
6533 }
6534 }
6535 }
6536 }
6537 break;
6538 case ISD::ANY_EXTEND:
6539 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6540 "Invalid ANY_EXTEND!");
6541 assert(VT.isVector() == N1.getValueType().isVector() &&
6542 "ANY_EXTEND result type type should be vector iff the operand "
6543 "type is vector!");
6544 if (N1.getValueType() == VT) return N1; // noop extension
6545 assert((!VT.isVector() || VT.getVectorElementCount() ==
6547 "Vector element count mismatch!");
6548 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6549
6550 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6551 OpOpcode == ISD::ANY_EXTEND) {
6552 SDNodeFlags Flags;
6553 if (OpOpcode == ISD::ZERO_EXTEND)
6554 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6555 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6556 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6557 }
6558 if (N1.isUndef())
6559 return getUNDEF(VT);
6560
6561 // (ext (trunc x)) -> x
6562 if (OpOpcode == ISD::TRUNCATE) {
6563 SDValue OpOp = N1.getOperand(0);
6564 if (OpOp.getValueType() == VT) {
6565 transferDbgValues(N1, OpOp);
6566 return OpOp;
6567 }
6568 }
6569 break;
6570 case ISD::TRUNCATE:
6571 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6572 "Invalid TRUNCATE!");
6573 assert(VT.isVector() == N1.getValueType().isVector() &&
6574 "TRUNCATE result type type should be vector iff the operand "
6575 "type is vector!");
6576 if (N1.getValueType() == VT) return N1; // noop truncate
6577 assert((!VT.isVector() || VT.getVectorElementCount() ==
6579 "Vector element count mismatch!");
6580 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6581 if (OpOpcode == ISD::TRUNCATE)
6582 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6583 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6584 OpOpcode == ISD::ANY_EXTEND) {
6585 // If the source is smaller than the dest, we still need an extend.
6587 VT.getScalarType())) {
6588 SDNodeFlags Flags;
6589 if (OpOpcode == ISD::ZERO_EXTEND)
6590 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6591 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6592 }
6593 if (N1.getOperand(0).getValueType().bitsGT(VT))
6594 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6595 return N1.getOperand(0);
6596 }
6597 if (N1.isUndef())
6598 return getUNDEF(VT);
6599 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6600 return getVScale(DL, VT,
6602 break;
6606 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6607 assert(N1.getValueType().bitsLE(VT) &&
6608 "The input must be the same size or smaller than the result.");
6611 "The destination vector type must have fewer lanes than the input.");
6612 break;
6613 case ISD::ABS:
6614 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6615 if (N1.isUndef())
6616 return getConstant(0, DL, VT);
6617 break;
6618 case ISD::BSWAP:
6619 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6620 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6621 "BSWAP types must be a multiple of 16 bits!");
6622 if (N1.isUndef())
6623 return getUNDEF(VT);
6624 // bswap(bswap(X)) -> X.
6625 if (OpOpcode == ISD::BSWAP)
6626 return N1.getOperand(0);
6627 break;
6628 case ISD::BITREVERSE:
6629 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6630 if (N1.isUndef())
6631 return getUNDEF(VT);
6632 break;
6633 case ISD::BITCAST:
6635 "Cannot BITCAST between types of different sizes!");
6636 if (VT == N1.getValueType()) return N1; // noop conversion.
6637 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6638 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6639 if (N1.isUndef())
6640 return getUNDEF(VT);
6641 break;
6643 assert(VT.isVector() && !N1.getValueType().isVector() &&
6644 (VT.getVectorElementType() == N1.getValueType() ||
6646 N1.getValueType().isInteger() &&
6648 "Illegal SCALAR_TO_VECTOR node!");
6649 if (N1.isUndef())
6650 return getUNDEF(VT);
6651 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6652 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6654 N1.getConstantOperandVal(1) == 0 &&
6655 N1.getOperand(0).getValueType() == VT)
6656 return N1.getOperand(0);
6657 break;
6658 case ISD::FNEG:
6659 // Negation of an unknown bag of bits is still completely undefined.
6660 if (N1.isUndef())
6661 return getUNDEF(VT);
6662
6663 if (OpOpcode == ISD::FNEG) // --X -> X
6664 return N1.getOperand(0);
6665 break;
6666 case ISD::FABS:
6667 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6668 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6669 break;
6670 case ISD::VSCALE:
6671 assert(VT == N1.getValueType() && "Unexpected VT!");
6672 break;
6673 case ISD::CTPOP:
6674 if (N1.getValueType().getScalarType() == MVT::i1)
6675 return N1;
6676 break;
6677 case ISD::CTLZ:
6678 case ISD::CTTZ:
6679 if (N1.getValueType().getScalarType() == MVT::i1)
6680 return getNOT(DL, N1, N1.getValueType());
6681 break;
6682 case ISD::VECREDUCE_ADD:
6683 if (N1.getValueType().getScalarType() == MVT::i1)
6684 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6685 break;
6686 case ISD::VECREDUCE_SMIN:
6687 case ISD::VECREDUCE_UMAX:
6688 if (N1.getValueType().getScalarType() == MVT::i1)
6689 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6690 break;
6691 case ISD::VECREDUCE_SMAX:
6692 case ISD::VECREDUCE_UMIN:
6693 if (N1.getValueType().getScalarType() == MVT::i1)
6694 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6695 break;
6696 case ISD::SPLAT_VECTOR:
6697 assert(VT.isVector() && "Wrong return type!");
6698 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6699 // that for now.
6701 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6703 N1.getValueType().isInteger() &&
6705 "Wrong operand type!");
6706 break;
6707 }
6708
6709 SDNode *N;
6710 SDVTList VTs = getVTList(VT);
6711 SDValue Ops[] = {N1};
6712 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6714 AddNodeIDNode(ID, Opcode, VTs, Ops);
6715 void *IP = nullptr;
6716 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6717 E->intersectFlagsWith(Flags);
6718 return SDValue(E, 0);
6719 }
6720
6721 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6722 N->setFlags(Flags);
6723 createOperands(N, Ops);
6724 CSEMap.InsertNode(N, IP);
6725 } else {
6726 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6727 createOperands(N, Ops);
6728 }
6729
6730 InsertNode(N);
6731 SDValue V = SDValue(N, 0);
6732 NewSDValueDbgMsg(V, "Creating new node: ", this);
6733 return V;
6734}
6735
6736static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6737 const APInt &C2) {
6738 switch (Opcode) {
6739 case ISD::ADD: return C1 + C2;
6740 case ISD::SUB: return C1 - C2;
6741 case ISD::MUL: return C1 * C2;
6742 case ISD::AND: return C1 & C2;
6743 case ISD::OR: return C1 | C2;
6744 case ISD::XOR: return C1 ^ C2;
6745 case ISD::SHL: return C1 << C2;
6746 case ISD::SRL: return C1.lshr(C2);
6747 case ISD::SRA: return C1.ashr(C2);
6748 case ISD::ROTL: return C1.rotl(C2);
6749 case ISD::ROTR: return C1.rotr(C2);
6750 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6751 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6752 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6753 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6754 case ISD::SADDSAT: return C1.sadd_sat(C2);
6755 case ISD::UADDSAT: return C1.uadd_sat(C2);
6756 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6757 case ISD::USUBSAT: return C1.usub_sat(C2);
6758 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6759 case ISD::USHLSAT: return C1.ushl_sat(C2);
6760 case ISD::UDIV:
6761 if (!C2.getBoolValue())
6762 break;
6763 return C1.udiv(C2);
6764 case ISD::UREM:
6765 if (!C2.getBoolValue())
6766 break;
6767 return C1.urem(C2);
6768 case ISD::SDIV:
6769 if (!C2.getBoolValue())
6770 break;
6771 return C1.sdiv(C2);
6772 case ISD::SREM:
6773 if (!C2.getBoolValue())
6774 break;
6775 return C1.srem(C2);
6776 case ISD::AVGFLOORS:
6777 return APIntOps::avgFloorS(C1, C2);
6778 case ISD::AVGFLOORU:
6779 return APIntOps::avgFloorU(C1, C2);
6780 case ISD::AVGCEILS:
6781 return APIntOps::avgCeilS(C1, C2);
6782 case ISD::AVGCEILU:
6783 return APIntOps::avgCeilU(C1, C2);
6784 case ISD::ABDS:
6785 return APIntOps::abds(C1, C2);
6786 case ISD::ABDU:
6787 return APIntOps::abdu(C1, C2);
6788 case ISD::MULHS:
6789 return APIntOps::mulhs(C1, C2);
6790 case ISD::MULHU:
6791 return APIntOps::mulhu(C1, C2);
6792 }
6793 return std::nullopt;
6794}
6795// Handle constant folding with UNDEF.
6796// TODO: Handle more cases.
6797static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6798 bool IsUndef1, const APInt &C2,
6799 bool IsUndef2) {
6800 if (!(IsUndef1 || IsUndef2))
6801 return FoldValue(Opcode, C1, C2);
6802
6803 // Fold and(x, undef) -> 0
6804 // Fold mul(x, undef) -> 0
6805 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6806 return APInt::getZero(C1.getBitWidth());
6807
6808 return std::nullopt;
6809}
6810
6812 const GlobalAddressSDNode *GA,
6813 const SDNode *N2) {
6814 if (GA->getOpcode() != ISD::GlobalAddress)
6815 return SDValue();
6816 if (!TLI->isOffsetFoldingLegal(GA))
6817 return SDValue();
6818 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6819 if (!C2)
6820 return SDValue();
6821 int64_t Offset = C2->getSExtValue();
6822 switch (Opcode) {
6823 case ISD::ADD:
6824 case ISD::PTRADD:
6825 break;
6826 case ISD::SUB: Offset = -uint64_t(Offset); break;
6827 default: return SDValue();
6828 }
6829 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6830 GA->getOffset() + uint64_t(Offset));
6831}
6832
6834 switch (Opcode) {
6835 case ISD::SDIV:
6836 case ISD::UDIV:
6837 case ISD::SREM:
6838 case ISD::UREM: {
6839 // If a divisor is zero/undef or any element of a divisor vector is
6840 // zero/undef, the whole op is undef.
6841 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6842 SDValue Divisor = Ops[1];
6843 if (Divisor.isUndef() || isNullConstant(Divisor))
6844 return true;
6845
6846 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6847 llvm::any_of(Divisor->op_values(),
6848 [](SDValue V) { return V.isUndef() ||
6849 isNullConstant(V); });
6850 // TODO: Handle signed overflow.
6851 }
6852 // TODO: Handle oversized shifts.
6853 default:
6854 return false;
6855 }
6856}
6857
6860 SDNodeFlags Flags) {
6861 // If the opcode is a target-specific ISD node, there's nothing we can
6862 // do here and the operand rules may not line up with the below, so
6863 // bail early.
6864 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6865 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6866 // foldCONCAT_VECTORS in getNode before this is called.
6867 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6868 return SDValue();
6869
6870 unsigned NumOps = Ops.size();
6871 if (NumOps == 0)
6872 return SDValue();
6873
6874 if (isUndef(Opcode, Ops))
6875 return getUNDEF(VT);
6876
6877 // Handle unary special cases.
6878 if (NumOps == 1) {
6879 SDValue N1 = Ops[0];
6880
6881 // Constant fold unary operations with an integer constant operand. Even
6882 // opaque constant will be folded, because the folding of unary operations
6883 // doesn't create new constants with different values. Nevertheless, the
6884 // opaque flag is preserved during folding to prevent future folding with
6885 // other constants.
6886 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6887 const APInt &Val = C->getAPIntValue();
6888 switch (Opcode) {
6889 case ISD::SIGN_EXTEND:
6890 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6891 C->isTargetOpcode(), C->isOpaque());
6892 case ISD::TRUNCATE:
6893 if (C->isOpaque())
6894 break;
6895 [[fallthrough]];
6896 case ISD::ZERO_EXTEND:
6897 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6898 C->isTargetOpcode(), C->isOpaque());
6899 case ISD::ANY_EXTEND:
6900 // Some targets like RISCV prefer to sign extend some types.
6901 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6902 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6903 C->isTargetOpcode(), C->isOpaque());
6904 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6905 C->isTargetOpcode(), C->isOpaque());
6906 case ISD::ABS:
6907 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6908 C->isOpaque());
6909 case ISD::BITREVERSE:
6910 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6911 C->isOpaque());
6912 case ISD::BSWAP:
6913 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6914 C->isOpaque());
6915 case ISD::CTPOP:
6916 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6917 C->isOpaque());
6918 case ISD::CTLZ:
6920 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6921 C->isOpaque());
6922 case ISD::CTTZ:
6924 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6925 C->isOpaque());
6926 case ISD::UINT_TO_FP:
6927 case ISD::SINT_TO_FP: {
6929 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6931 return getConstantFP(FPV, DL, VT);
6932 }
6933 case ISD::FP16_TO_FP:
6934 case ISD::BF16_TO_FP: {
6935 bool Ignored;
6936 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6937 : APFloat::BFloat(),
6938 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6939
6940 // This can return overflow, underflow, or inexact; we don't care.
6941 // FIXME need to be more flexible about rounding mode.
6943 &Ignored);
6944 return getConstantFP(FPV, DL, VT);
6945 }
6946 case ISD::STEP_VECTOR:
6947 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6948 return V;
6949 break;
6950 case ISD::BITCAST:
6951 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6952 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6953 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6954 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6955 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6956 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6957 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6958 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6959 break;
6960 }
6961 }
6962
6963 // Constant fold unary operations with a floating point constant operand.
6964 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6965 APFloat V = C->getValueAPF(); // make copy
6966 switch (Opcode) {
6967 case ISD::FNEG:
6968 V.changeSign();
6969 return getConstantFP(V, DL, VT);
6970 case ISD::FABS:
6971 V.clearSign();
6972 return getConstantFP(V, DL, VT);
6973 case ISD::FCEIL: {
6974 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6976 return getConstantFP(V, DL, VT);
6977 return SDValue();
6978 }
6979 case ISD::FTRUNC: {
6980 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6982 return getConstantFP(V, DL, VT);
6983 return SDValue();
6984 }
6985 case ISD::FFLOOR: {
6986 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6988 return getConstantFP(V, DL, VT);
6989 return SDValue();
6990 }
6991 case ISD::FP_EXTEND: {
6992 bool ignored;
6993 // This can return overflow, underflow, or inexact; we don't care.
6994 // FIXME need to be more flexible about rounding mode.
6995 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
6996 &ignored);
6997 return getConstantFP(V, DL, VT);
6998 }
6999 case ISD::FP_TO_SINT:
7000 case ISD::FP_TO_UINT: {
7001 bool ignored;
7002 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7003 // FIXME need to be more flexible about rounding mode.
7005 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7006 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7007 break;
7008 return getConstant(IntVal, DL, VT);
7009 }
7010 case ISD::FP_TO_FP16:
7011 case ISD::FP_TO_BF16: {
7012 bool Ignored;
7013 // This can return overflow, underflow, or inexact; we don't care.
7014 // FIXME need to be more flexible about rounding mode.
7015 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7016 : APFloat::BFloat(),
7018 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7019 }
7020 case ISD::BITCAST:
7021 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7022 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7023 VT);
7024 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7025 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7026 VT);
7027 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7028 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7029 VT);
7030 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7031 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7032 break;
7033 }
7034 }
7035
7036 // Early-out if we failed to constant fold a bitcast.
7037 if (Opcode == ISD::BITCAST)
7038 return SDValue();
7039 }
7040
7041 // Handle binops special cases.
7042 if (NumOps == 2) {
7043 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7044 return CFP;
7045
7046 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7047 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7048 if (C1->isOpaque() || C2->isOpaque())
7049 return SDValue();
7050
7051 std::optional<APInt> FoldAttempt =
7052 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7053 if (!FoldAttempt)
7054 return SDValue();
7055
7056 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7057 assert((!Folded || !VT.isVector()) &&
7058 "Can't fold vectors ops with scalar operands");
7059 return Folded;
7060 }
7061 }
7062
7063 // fold (add Sym, c) -> Sym+c
7065 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7066 if (TLI->isCommutativeBinOp(Opcode))
7068 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7069
7070 // fold (sext_in_reg c1) -> c2
7071 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7072 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7073
7074 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7075 unsigned FromBits = EVT.getScalarSizeInBits();
7076 Val <<= Val.getBitWidth() - FromBits;
7077 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7078 return getConstant(Val, DL, ConstantVT);
7079 };
7080
7081 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7082 const APInt &Val = C1->getAPIntValue();
7083 return SignExtendInReg(Val, VT);
7084 }
7085
7087 SmallVector<SDValue, 8> ScalarOps;
7088 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7089 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7090 SDValue Op = Ops[0].getOperand(I);
7091 if (Op.isUndef()) {
7092 ScalarOps.push_back(getUNDEF(OpVT));
7093 continue;
7094 }
7095 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7096 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7097 }
7098 return getBuildVector(VT, DL, ScalarOps);
7099 }
7100
7101 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7102 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7103 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7104 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7105 Ops[0].getOperand(0).getValueType()));
7106 }
7107 }
7108
7109 // Handle fshl/fshr special cases.
7110 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7111 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7112 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7113 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7114
7115 if (C1 && C2 && C3) {
7116 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7117 return SDValue();
7118 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7119 &V3 = C3->getAPIntValue();
7120
7121 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7122 : APIntOps::fshr(V1, V2, V3);
7123 return getConstant(FoldedVal, DL, VT);
7124 }
7125 }
7126
7127 // Handle fma/fmad special cases.
7128 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7129 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7130 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7131 Ops[2].getValueType() == VT && "FMA types must match!");
7135 if (C1 && C2 && C3) {
7136 APFloat V1 = C1->getValueAPF();
7137 const APFloat &V2 = C2->getValueAPF();
7138 const APFloat &V3 = C3->getValueAPF();
7139 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7142 } else
7144 return getConstantFP(V1, DL, VT);
7145 }
7146 }
7147
7148 // This is for vector folding only from here on.
7149 if (!VT.isVector())
7150 return SDValue();
7151
7152 ElementCount NumElts = VT.getVectorElementCount();
7153
7154 // See if we can fold through any bitcasted integer ops.
7155 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7156 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7157 (Ops[0].getOpcode() == ISD::BITCAST ||
7158 Ops[1].getOpcode() == ISD::BITCAST)) {
7161 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7162 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7163 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7164 N2.getValueType().isInteger()) {
7165 bool IsLE = getDataLayout().isLittleEndian();
7166 unsigned EltBits = VT.getScalarSizeInBits();
7167 SmallVector<APInt> RawBits1, RawBits2;
7168 BitVector UndefElts1, UndefElts2;
7169 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7170 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7171 SmallVector<APInt> RawBits;
7172 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7173 std::optional<APInt> Fold = FoldValueWithUndef(
7174 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7175 if (!Fold)
7176 break;
7177 RawBits.push_back(*Fold);
7178 }
7179 if (RawBits.size() == NumElts.getFixedValue()) {
7180 // We have constant folded, but we might need to cast this again back
7181 // to the original (possibly legalized) type.
7182 EVT BVVT, BVEltVT;
7183 if (N1.getValueType() == VT) {
7184 BVVT = N1.getValueType();
7185 BVEltVT = BV1->getOperand(0).getValueType();
7186 } else {
7187 BVVT = N2.getValueType();
7188 BVEltVT = BV2->getOperand(0).getValueType();
7189 }
7190 unsigned BVEltBits = BVEltVT.getSizeInBits();
7191 SmallVector<APInt> DstBits;
7192 BitVector DstUndefs;
7194 DstBits, RawBits, DstUndefs,
7195 BitVector(RawBits.size(), false));
7196 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7197 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7198 if (DstUndefs[I])
7199 continue;
7200 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7201 }
7202 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7203 }
7204 }
7205 }
7206 }
7207
7208 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7209 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7210 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7211 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7212 APInt RHSVal;
7213 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7214 APInt NewStep = Opcode == ISD::MUL
7215 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7216 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7217 return getStepVector(DL, VT, NewStep);
7218 }
7219 }
7220
7221 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7222 return !Op.getValueType().isVector() ||
7223 Op.getValueType().getVectorElementCount() == NumElts;
7224 };
7225
7226 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7227 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7228 Op.getOpcode() == ISD::BUILD_VECTOR ||
7229 Op.getOpcode() == ISD::SPLAT_VECTOR;
7230 };
7231
7232 // All operands must be vector types with the same number of elements as
7233 // the result type and must be either UNDEF or a build/splat vector
7234 // or UNDEF scalars.
7235 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7236 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7237 return SDValue();
7238
7239 // If we are comparing vectors, then the result needs to be a i1 boolean that
7240 // is then extended back to the legal result type depending on how booleans
7241 // are represented.
7242 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7243 ISD::NodeType ExtendCode =
7244 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7245 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7247
7248 // Find legal integer scalar type for constant promotion and
7249 // ensure that its scalar size is at least as large as source.
7250 EVT LegalSVT = VT.getScalarType();
7251 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7252 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7253 if (LegalSVT.bitsLT(VT.getScalarType()))
7254 return SDValue();
7255 }
7256
7257 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7258 // only have one operand to check. For fixed-length vector types we may have
7259 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7260 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7261
7262 // Constant fold each scalar lane separately.
7263 SmallVector<SDValue, 4> ScalarResults;
7264 for (unsigned I = 0; I != NumVectorElts; I++) {
7265 SmallVector<SDValue, 4> ScalarOps;
7266 for (SDValue Op : Ops) {
7267 EVT InSVT = Op.getValueType().getScalarType();
7268 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7269 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7270 if (Op.isUndef())
7271 ScalarOps.push_back(getUNDEF(InSVT));
7272 else
7273 ScalarOps.push_back(Op);
7274 continue;
7275 }
7276
7277 SDValue ScalarOp =
7278 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7279 EVT ScalarVT = ScalarOp.getValueType();
7280
7281 // Build vector (integer) scalar operands may need implicit
7282 // truncation - do this before constant folding.
7283 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7284 // Don't create illegally-typed nodes unless they're constants or undef
7285 // - if we fail to constant fold we can't guarantee the (dead) nodes
7286 // we're creating will be cleaned up before being visited for
7287 // legalization.
7288 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7289 !isa<ConstantSDNode>(ScalarOp) &&
7290 TLI->getTypeAction(*getContext(), InSVT) !=
7292 return SDValue();
7293 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7294 }
7295
7296 ScalarOps.push_back(ScalarOp);
7297 }
7298
7299 // Constant fold the scalar operands.
7300 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7301
7302 // Scalar folding only succeeded if the result is a constant or UNDEF.
7303 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7304 ScalarResult.getOpcode() != ISD::ConstantFP)
7305 return SDValue();
7306
7307 // Legalize the (integer) scalar constant if necessary. We only do
7308 // this once we know the folding succeeded, since otherwise we would
7309 // get a node with illegal type which has a user.
7310 if (LegalSVT != SVT)
7311 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7312
7313 ScalarResults.push_back(ScalarResult);
7314 }
7315
7316 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7317 : getBuildVector(VT, DL, ScalarResults);
7318 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7319 return V;
7320}
7321
7324 // TODO: Add support for unary/ternary fp opcodes.
7325 if (Ops.size() != 2)
7326 return SDValue();
7327
7328 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7329 // should. That will require dealing with a potentially non-default
7330 // rounding mode, checking the "opStatus" return value from the APFloat
7331 // math calculations, and possibly other variations.
7332 SDValue N1 = Ops[0];
7333 SDValue N2 = Ops[1];
7334 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7335 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7336 if (N1CFP && N2CFP) {
7337 APFloat C1 = N1CFP->getValueAPF(); // make copy
7338 const APFloat &C2 = N2CFP->getValueAPF();
7339 switch (Opcode) {
7340 case ISD::FADD:
7342 return getConstantFP(C1, DL, VT);
7343 case ISD::FSUB:
7345 return getConstantFP(C1, DL, VT);
7346 case ISD::FMUL:
7348 return getConstantFP(C1, DL, VT);
7349 case ISD::FDIV:
7351 return getConstantFP(C1, DL, VT);
7352 case ISD::FREM:
7353 C1.mod(C2);
7354 return getConstantFP(C1, DL, VT);
7355 case ISD::FCOPYSIGN:
7356 C1.copySign(C2);
7357 return getConstantFP(C1, DL, VT);
7358 case ISD::FMINNUM:
7359 return getConstantFP(minnum(C1, C2), DL, VT);
7360 case ISD::FMAXNUM:
7361 return getConstantFP(maxnum(C1, C2), DL, VT);
7362 case ISD::FMINIMUM:
7363 return getConstantFP(minimum(C1, C2), DL, VT);
7364 case ISD::FMAXIMUM:
7365 return getConstantFP(maximum(C1, C2), DL, VT);
7366 case ISD::FMINIMUMNUM:
7367 return getConstantFP(minimumnum(C1, C2), DL, VT);
7368 case ISD::FMAXIMUMNUM:
7369 return getConstantFP(maximumnum(C1, C2), DL, VT);
7370 default: break;
7371 }
7372 }
7373 if (N1CFP && Opcode == ISD::FP_ROUND) {
7374 APFloat C1 = N1CFP->getValueAPF(); // make copy
7375 bool Unused;
7376 // This can return overflow, underflow, or inexact; we don't care.
7377 // FIXME need to be more flexible about rounding mode.
7379 &Unused);
7380 return getConstantFP(C1, DL, VT);
7381 }
7382
7383 switch (Opcode) {
7384 case ISD::FSUB:
7385 // -0.0 - undef --> undef (consistent with "fneg undef")
7386 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7387 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7388 return getUNDEF(VT);
7389 [[fallthrough]];
7390
7391 case ISD::FADD:
7392 case ISD::FMUL:
7393 case ISD::FDIV:
7394 case ISD::FREM:
7395 // If both operands are undef, the result is undef. If 1 operand is undef,
7396 // the result is NaN. This should match the behavior of the IR optimizer.
7397 if (N1.isUndef() && N2.isUndef())
7398 return getUNDEF(VT);
7399 if (N1.isUndef() || N2.isUndef())
7401 }
7402 return SDValue();
7403}
7404
7406 const SDLoc &DL, EVT DstEltVT) {
7407 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7408
7409 // If this is already the right type, we're done.
7410 if (SrcEltVT == DstEltVT)
7411 return SDValue(BV, 0);
7412
7413 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7414 unsigned DstBitSize = DstEltVT.getSizeInBits();
7415
7416 // If this is a conversion of N elements of one type to N elements of another
7417 // type, convert each element. This handles FP<->INT cases.
7418 if (SrcBitSize == DstBitSize) {
7420 for (SDValue Op : BV->op_values()) {
7421 // If the vector element type is not legal, the BUILD_VECTOR operands
7422 // are promoted and implicitly truncated. Make that explicit here.
7423 if (Op.getValueType() != SrcEltVT)
7424 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7425 Ops.push_back(getBitcast(DstEltVT, Op));
7426 }
7427 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7429 return getBuildVector(VT, DL, Ops);
7430 }
7431
7432 // Otherwise, we're growing or shrinking the elements. To avoid having to
7433 // handle annoying details of growing/shrinking FP values, we convert them to
7434 // int first.
7435 if (SrcEltVT.isFloatingPoint()) {
7436 // Convert the input float vector to a int vector where the elements are the
7437 // same sizes.
7438 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7439 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7441 DstEltVT);
7442 return SDValue();
7443 }
7444
7445 // Now we know the input is an integer vector. If the output is a FP type,
7446 // convert to integer first, then to FP of the right size.
7447 if (DstEltVT.isFloatingPoint()) {
7448 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7449 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7451 DstEltVT);
7452 return SDValue();
7453 }
7454
7455 // Okay, we know the src/dst types are both integers of differing types.
7456 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7457
7458 // Extract the constant raw bit data.
7459 BitVector UndefElements;
7460 SmallVector<APInt> RawBits;
7461 bool IsLE = getDataLayout().isLittleEndian();
7462 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7463 return SDValue();
7464
7466 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7467 if (UndefElements[I])
7468 Ops.push_back(getUNDEF(DstEltVT));
7469 else
7470 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7471 }
7472
7473 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7474 return getBuildVector(VT, DL, Ops);
7475}
7476
7478 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7479
7480 // There's no need to assert on a byte-aligned pointer. All pointers are at
7481 // least byte aligned.
7482 if (A == Align(1))
7483 return Val;
7484
7485 SDVTList VTs = getVTList(Val.getValueType());
7487 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7488 ID.AddInteger(A.value());
7489
7490 void *IP = nullptr;
7491 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7492 return SDValue(E, 0);
7493
7494 auto *N =
7495 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7496 createOperands(N, {Val});
7497
7498 CSEMap.InsertNode(N, IP);
7499 InsertNode(N);
7500
7501 SDValue V(N, 0);
7502 NewSDValueDbgMsg(V, "Creating new node: ", this);
7503 return V;
7504}
7505
7506SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7507 SDValue N1, SDValue N2) {
7508 SDNodeFlags Flags;
7509 if (Inserter)
7510 Flags = Inserter->getFlags();
7511 return getNode(Opcode, DL, VT, N1, N2, Flags);
7512}
7513
7515 SDValue &N2) const {
7516 if (!TLI->isCommutativeBinOp(Opcode))
7517 return;
7518
7519 // Canonicalize:
7520 // binop(const, nonconst) -> binop(nonconst, const)
7523 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7524 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7525 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7526 std::swap(N1, N2);
7527
7528 // Canonicalize:
7529 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7530 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7532 std::swap(N1, N2);
7533}
7534
7535SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7536 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7538 N2.getOpcode() != ISD::DELETED_NODE &&
7539 "Operand is DELETED_NODE!");
7540
7541 canonicalizeCommutativeBinop(Opcode, N1, N2);
7542
7543 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7544 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7545
7546 // Don't allow undefs in vector splats - we might be returning N2 when folding
7547 // to zero etc.
7548 ConstantSDNode *N2CV =
7549 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7550
7551 switch (Opcode) {
7552 default: break;
7553 case ISD::TokenFactor:
7554 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7555 N2.getValueType() == MVT::Other && "Invalid token factor!");
7556 // Fold trivial token factors.
7557 if (N1.getOpcode() == ISD::EntryToken) return N2;
7558 if (N2.getOpcode() == ISD::EntryToken) return N1;
7559 if (N1 == N2) return N1;
7560 break;
7561 case ISD::BUILD_VECTOR: {
7562 // Attempt to simplify BUILD_VECTOR.
7563 SDValue Ops[] = {N1, N2};
7564 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7565 return V;
7566 break;
7567 }
7568 case ISD::CONCAT_VECTORS: {
7569 SDValue Ops[] = {N1, N2};
7570 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7571 return V;
7572 break;
7573 }
7574 case ISD::AND:
7575 assert(VT.isInteger() && "This operator does not apply to FP types!");
7576 assert(N1.getValueType() == N2.getValueType() &&
7577 N1.getValueType() == VT && "Binary operator types must match!");
7578 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7579 // worth handling here.
7580 if (N2CV && N2CV->isZero())
7581 return N2;
7582 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7583 return N1;
7584 break;
7585 case ISD::OR:
7586 case ISD::XOR:
7587 case ISD::ADD:
7588 case ISD::PTRADD:
7589 case ISD::SUB:
7590 assert(VT.isInteger() && "This operator does not apply to FP types!");
7591 assert(N1.getValueType() == N2.getValueType() &&
7592 N1.getValueType() == VT && "Binary operator types must match!");
7593 // The equal operand types requirement is unnecessarily strong for PTRADD.
7594 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7595 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7596 // logic everywhere where PTRADDs may be folded or combined to properly
7597 // support them. If/when we introduce pointer types to the SDAG, we will
7598 // need to relax this constraint.
7599
7600 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7601 // it's worth handling here.
7602 if (N2CV && N2CV->isZero())
7603 return N1;
7604 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7605 VT.getScalarType() == MVT::i1)
7606 return getNode(ISD::XOR, DL, VT, N1, N2);
7607 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7608 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7609 N2.getOpcode() == ISD::VSCALE) {
7610 const APInt &C1 = N1->getConstantOperandAPInt(0);
7611 const APInt &C2 = N2->getConstantOperandAPInt(0);
7612 return getVScale(DL, VT, C1 + C2);
7613 }
7614 break;
7615 case ISD::MUL:
7616 assert(VT.isInteger() && "This operator does not apply to FP types!");
7617 assert(N1.getValueType() == N2.getValueType() &&
7618 N1.getValueType() == VT && "Binary operator types must match!");
7619 if (VT.getScalarType() == MVT::i1)
7620 return getNode(ISD::AND, DL, VT, N1, N2);
7621 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7622 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7623 const APInt &N2CImm = N2C->getAPIntValue();
7624 return getVScale(DL, VT, MulImm * N2CImm);
7625 }
7626 break;
7627 case ISD::UDIV:
7628 case ISD::UREM:
7629 case ISD::MULHU:
7630 case ISD::MULHS:
7631 case ISD::SDIV:
7632 case ISD::SREM:
7633 case ISD::SADDSAT:
7634 case ISD::SSUBSAT:
7635 case ISD::UADDSAT:
7636 case ISD::USUBSAT:
7637 assert(VT.isInteger() && "This operator does not apply to FP types!");
7638 assert(N1.getValueType() == N2.getValueType() &&
7639 N1.getValueType() == VT && "Binary operator types must match!");
7640 if (VT.getScalarType() == MVT::i1) {
7641 // fold (add_sat x, y) -> (or x, y) for bool types.
7642 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7643 return getNode(ISD::OR, DL, VT, N1, N2);
7644 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7645 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7646 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7647 }
7648 break;
7649 case ISD::SCMP:
7650 case ISD::UCMP:
7651 assert(N1.getValueType() == N2.getValueType() &&
7652 "Types of operands of UCMP/SCMP must match");
7653 assert(N1.getValueType().isVector() == VT.isVector() &&
7654 "Operands and return type of must both be scalars or vectors");
7655 if (VT.isVector())
7658 "Result and operands must have the same number of elements");
7659 break;
7660 case ISD::AVGFLOORS:
7661 case ISD::AVGFLOORU:
7662 case ISD::AVGCEILS:
7663 case ISD::AVGCEILU:
7664 assert(VT.isInteger() && "This operator does not apply to FP types!");
7665 assert(N1.getValueType() == N2.getValueType() &&
7666 N1.getValueType() == VT && "Binary operator types must match!");
7667 break;
7668 case ISD::ABDS:
7669 case ISD::ABDU:
7670 assert(VT.isInteger() && "This operator does not apply to FP types!");
7671 assert(N1.getValueType() == N2.getValueType() &&
7672 N1.getValueType() == VT && "Binary operator types must match!");
7673 if (VT.getScalarType() == MVT::i1)
7674 return getNode(ISD::XOR, DL, VT, N1, N2);
7675 break;
7676 case ISD::SMIN:
7677 case ISD::UMAX:
7678 assert(VT.isInteger() && "This operator does not apply to FP types!");
7679 assert(N1.getValueType() == N2.getValueType() &&
7680 N1.getValueType() == VT && "Binary operator types must match!");
7681 if (VT.getScalarType() == MVT::i1)
7682 return getNode(ISD::OR, DL, VT, N1, N2);
7683 break;
7684 case ISD::SMAX:
7685 case ISD::UMIN:
7686 assert(VT.isInteger() && "This operator does not apply to FP types!");
7687 assert(N1.getValueType() == N2.getValueType() &&
7688 N1.getValueType() == VT && "Binary operator types must match!");
7689 if (VT.getScalarType() == MVT::i1)
7690 return getNode(ISD::AND, DL, VT, N1, N2);
7691 break;
7692 case ISD::FADD:
7693 case ISD::FSUB:
7694 case ISD::FMUL:
7695 case ISD::FDIV:
7696 case ISD::FREM:
7697 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7698 assert(N1.getValueType() == N2.getValueType() &&
7699 N1.getValueType() == VT && "Binary operator types must match!");
7700 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7701 return V;
7702 break;
7703 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7704 assert(N1.getValueType() == VT &&
7707 "Invalid FCOPYSIGN!");
7708 break;
7709 case ISD::SHL:
7710 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7711 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7712 const APInt &ShiftImm = N2C->getAPIntValue();
7713 return getVScale(DL, VT, MulImm << ShiftImm);
7714 }
7715 [[fallthrough]];
7716 case ISD::SRA:
7717 case ISD::SRL:
7718 if (SDValue V = simplifyShift(N1, N2))
7719 return V;
7720 [[fallthrough]];
7721 case ISD::ROTL:
7722 case ISD::ROTR:
7723 assert(VT == N1.getValueType() &&
7724 "Shift operators return type must be the same as their first arg");
7725 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7726 "Shifts only work on integers");
7727 assert((!VT.isVector() || VT == N2.getValueType()) &&
7728 "Vector shift amounts must be in the same as their first arg");
7729 // Verify that the shift amount VT is big enough to hold valid shift
7730 // amounts. This catches things like trying to shift an i1024 value by an
7731 // i8, which is easy to fall into in generic code that uses
7732 // TLI.getShiftAmount().
7735 "Invalid use of small shift amount with oversized value!");
7736
7737 // Always fold shifts of i1 values so the code generator doesn't need to
7738 // handle them. Since we know the size of the shift has to be less than the
7739 // size of the value, the shift/rotate count is guaranteed to be zero.
7740 if (VT == MVT::i1)
7741 return N1;
7742 if (N2CV && N2CV->isZero())
7743 return N1;
7744 break;
7745 case ISD::FP_ROUND:
7747 VT.bitsLE(N1.getValueType()) && N2C &&
7748 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7749 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7750 if (N1.getValueType() == VT) return N1; // noop conversion.
7751 break;
7752 case ISD::AssertNoFPClass: {
7754 "AssertNoFPClass is used for a non-floating type");
7755 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7756 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7757 assert(llvm::to_underlying(NoFPClass) <=
7759 "FPClassTest value too large");
7760 (void)NoFPClass;
7761 break;
7762 }
7763 case ISD::AssertSext:
7764 case ISD::AssertZext: {
7765 EVT EVT = cast<VTSDNode>(N2)->getVT();
7766 assert(VT == N1.getValueType() && "Not an inreg extend!");
7767 assert(VT.isInteger() && EVT.isInteger() &&
7768 "Cannot *_EXTEND_INREG FP types");
7769 assert(!EVT.isVector() &&
7770 "AssertSExt/AssertZExt type should be the vector element type "
7771 "rather than the vector type!");
7772 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7773 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7774 break;
7775 }
7777 EVT EVT = cast<VTSDNode>(N2)->getVT();
7778 assert(VT == N1.getValueType() && "Not an inreg extend!");
7779 assert(VT.isInteger() && EVT.isInteger() &&
7780 "Cannot *_EXTEND_INREG FP types");
7781 assert(EVT.isVector() == VT.isVector() &&
7782 "SIGN_EXTEND_INREG type should be vector iff the operand "
7783 "type is vector!");
7784 assert((!EVT.isVector() ||
7786 "Vector element counts must match in SIGN_EXTEND_INREG");
7787 assert(EVT.bitsLE(VT) && "Not extending!");
7788 if (EVT == VT) return N1; // Not actually extending
7789 break;
7790 }
7792 case ISD::FP_TO_UINT_SAT: {
7793 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7794 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7795 assert(N1.getValueType().isVector() == VT.isVector() &&
7796 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7797 "vector!");
7798 assert((!VT.isVector() || VT.getVectorElementCount() ==
7800 "Vector element counts must match in FP_TO_*INT_SAT");
7801 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7802 "Type to saturate to must be a scalar.");
7803 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7804 "Not extending!");
7805 break;
7806 }
7809 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7810 element type of the vector.");
7811
7812 // Extract from an undefined value or using an undefined index is undefined.
7813 if (N1.isUndef() || N2.isUndef())
7814 return getUNDEF(VT);
7815
7816 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7817 // vectors. For scalable vectors we will provide appropriate support for
7818 // dealing with arbitrary indices.
7819 if (N2C && N1.getValueType().isFixedLengthVector() &&
7820 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7821 return getUNDEF(VT);
7822
7823 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7824 // expanding copies of large vectors from registers. This only works for
7825 // fixed length vectors, since we need to know the exact number of
7826 // elements.
7827 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7829 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7830 return getExtractVectorElt(DL, VT,
7831 N1.getOperand(N2C->getZExtValue() / Factor),
7832 N2C->getZExtValue() % Factor);
7833 }
7834
7835 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7836 // lowering is expanding large vector constants.
7837 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7838 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7841 "BUILD_VECTOR used for scalable vectors");
7842 unsigned Index =
7843 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7844 SDValue Elt = N1.getOperand(Index);
7845
7846 if (VT != Elt.getValueType())
7847 // If the vector element type is not legal, the BUILD_VECTOR operands
7848 // are promoted and implicitly truncated, and the result implicitly
7849 // extended. Make that explicit here.
7850 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7851
7852 return Elt;
7853 }
7854
7855 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7856 // operations are lowered to scalars.
7857 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7858 // If the indices are the same, return the inserted element else
7859 // if the indices are known different, extract the element from
7860 // the original vector.
7861 SDValue N1Op2 = N1.getOperand(2);
7863
7864 if (N1Op2C && N2C) {
7865 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7866 if (VT == N1.getOperand(1).getValueType())
7867 return N1.getOperand(1);
7868 if (VT.isFloatingPoint()) {
7870 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7871 }
7872 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7873 }
7874 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7875 }
7876 }
7877
7878 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7879 // when vector types are scalarized and v1iX is legal.
7880 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7881 // Here we are completely ignoring the extract element index (N2),
7882 // which is fine for fixed width vectors, since any index other than 0
7883 // is undefined anyway. However, this cannot be ignored for scalable
7884 // vectors - in theory we could support this, but we don't want to do this
7885 // without a profitability check.
7886 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7888 N1.getValueType().getVectorNumElements() == 1) {
7889 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7890 N1.getOperand(1));
7891 }
7892 break;
7894 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7895 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7896 (N1.getValueType().isInteger() == VT.isInteger()) &&
7897 N1.getValueType() != VT &&
7898 "Wrong types for EXTRACT_ELEMENT!");
7899
7900 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7901 // 64-bit integers into 32-bit parts. Instead of building the extract of
7902 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7903 if (N1.getOpcode() == ISD::BUILD_PAIR)
7904 return N1.getOperand(N2C->getZExtValue());
7905
7906 // EXTRACT_ELEMENT of a constant int is also very common.
7907 if (N1C) {
7908 unsigned ElementSize = VT.getSizeInBits();
7909 unsigned Shift = ElementSize * N2C->getZExtValue();
7910 const APInt &Val = N1C->getAPIntValue();
7911 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7912 }
7913 break;
7915 EVT N1VT = N1.getValueType();
7916 assert(VT.isVector() && N1VT.isVector() &&
7917 "Extract subvector VTs must be vectors!");
7919 "Extract subvector VTs must have the same element type!");
7920 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7921 "Cannot extract a scalable vector from a fixed length vector!");
7922 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7924 "Extract subvector must be from larger vector to smaller vector!");
7925 assert(N2C && "Extract subvector index must be a constant");
7926 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7927 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7928 N1VT.getVectorMinNumElements()) &&
7929 "Extract subvector overflow!");
7930 assert(N2C->getAPIntValue().getBitWidth() ==
7931 TLI->getVectorIdxWidth(getDataLayout()) &&
7932 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7933 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
7934 "Extract index is not a multiple of the output vector length");
7935
7936 // Trivial extraction.
7937 if (VT == N1VT)
7938 return N1;
7939
7940 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7941 if (N1.isUndef())
7942 return getUNDEF(VT);
7943
7944 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7945 // the concat have the same type as the extract.
7946 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7947 VT == N1.getOperand(0).getValueType()) {
7948 unsigned Factor = VT.getVectorMinNumElements();
7949 return N1.getOperand(N2C->getZExtValue() / Factor);
7950 }
7951
7952 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7953 // during shuffle legalization.
7954 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7955 VT == N1.getOperand(1).getValueType())
7956 return N1.getOperand(1);
7957 break;
7958 }
7959 }
7960
7961 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
7962 switch (Opcode) {
7963 case ISD::XOR:
7964 case ISD::ADD:
7965 case ISD::PTRADD:
7966 case ISD::SUB:
7968 case ISD::UDIV:
7969 case ISD::SDIV:
7970 case ISD::UREM:
7971 case ISD::SREM:
7972 case ISD::MUL:
7973 case ISD::AND:
7974 case ISD::SSUBSAT:
7975 case ISD::USUBSAT:
7976 case ISD::UMIN:
7977 case ISD::OR:
7978 case ISD::SADDSAT:
7979 case ISD::UADDSAT:
7980 case ISD::UMAX:
7981 case ISD::SMAX:
7982 case ISD::SMIN:
7983 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
7984 return N2.getOpcode() == ISD::POISON ? N2 : N1;
7985 }
7986 }
7987
7988 // Canonicalize an UNDEF to the RHS, even over a constant.
7989 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
7990 if (TLI->isCommutativeBinOp(Opcode)) {
7991 std::swap(N1, N2);
7992 } else {
7993 switch (Opcode) {
7994 case ISD::PTRADD:
7995 case ISD::SUB:
7996 // fold op(undef, non_undef_arg2) -> undef.
7997 return N1;
7999 case ISD::UDIV:
8000 case ISD::SDIV:
8001 case ISD::UREM:
8002 case ISD::SREM:
8003 case ISD::SSUBSAT:
8004 case ISD::USUBSAT:
8005 // fold op(undef, non_undef_arg2) -> 0.
8006 return getConstant(0, DL, VT);
8007 }
8008 }
8009 }
8010
8011 // Fold a bunch of operators when the RHS is undef.
8012 if (N2.getOpcode() == ISD::UNDEF) {
8013 switch (Opcode) {
8014 case ISD::XOR:
8015 if (N1.getOpcode() == ISD::UNDEF)
8016 // Handle undef ^ undef -> 0 special case. This is a common
8017 // idiom (misuse).
8018 return getConstant(0, DL, VT);
8019 [[fallthrough]];
8020 case ISD::ADD:
8021 case ISD::PTRADD:
8022 case ISD::SUB:
8023 // fold op(arg1, undef) -> undef.
8024 return N2;
8025 case ISD::UDIV:
8026 case ISD::SDIV:
8027 case ISD::UREM:
8028 case ISD::SREM:
8029 // fold op(arg1, undef) -> poison.
8030 return getPOISON(VT);
8031 case ISD::MUL:
8032 case ISD::AND:
8033 case ISD::SSUBSAT:
8034 case ISD::USUBSAT:
8035 case ISD::UMIN:
8036 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8037 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8038 case ISD::OR:
8039 case ISD::SADDSAT:
8040 case ISD::UADDSAT:
8041 case ISD::UMAX:
8042 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8043 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8044 case ISD::SMAX:
8045 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8046 return N1.getOpcode() == ISD::UNDEF
8047 ? N2
8048 : getConstant(
8050 VT);
8051 case ISD::SMIN:
8052 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8053 return N1.getOpcode() == ISD::UNDEF
8054 ? N2
8055 : getConstant(
8057 VT);
8058 }
8059 }
8060
8061 // Perform trivial constant folding.
8062 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8063 return SV;
8064
8065 // Memoize this node if possible.
8066 SDNode *N;
8067 SDVTList VTs = getVTList(VT);
8068 SDValue Ops[] = {N1, N2};
8069 if (VT != MVT::Glue) {
8071 AddNodeIDNode(ID, Opcode, VTs, Ops);
8072 void *IP = nullptr;
8073 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8074 E->intersectFlagsWith(Flags);
8075 return SDValue(E, 0);
8076 }
8077
8078 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8079 N->setFlags(Flags);
8080 createOperands(N, Ops);
8081 CSEMap.InsertNode(N, IP);
8082 } else {
8083 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8084 createOperands(N, Ops);
8085 }
8086
8087 InsertNode(N);
8088 SDValue V = SDValue(N, 0);
8089 NewSDValueDbgMsg(V, "Creating new node: ", this);
8090 return V;
8091}
8092
8093SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8094 SDValue N1, SDValue N2, SDValue N3) {
8095 SDNodeFlags Flags;
8096 if (Inserter)
8097 Flags = Inserter->getFlags();
8098 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8099}
8100
8101SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8102 SDValue N1, SDValue N2, SDValue N3,
8103 const SDNodeFlags Flags) {
8105 N2.getOpcode() != ISD::DELETED_NODE &&
8106 N3.getOpcode() != ISD::DELETED_NODE &&
8107 "Operand is DELETED_NODE!");
8108 // Perform various simplifications.
8109 switch (Opcode) {
8110 case ISD::BUILD_VECTOR: {
8111 // Attempt to simplify BUILD_VECTOR.
8112 SDValue Ops[] = {N1, N2, N3};
8113 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8114 return V;
8115 break;
8116 }
8117 case ISD::CONCAT_VECTORS: {
8118 SDValue Ops[] = {N1, N2, N3};
8119 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8120 return V;
8121 break;
8122 }
8123 case ISD::SETCC: {
8124 assert(VT.isInteger() && "SETCC result type must be an integer!");
8125 assert(N1.getValueType() == N2.getValueType() &&
8126 "SETCC operands must have the same type!");
8127 assert(VT.isVector() == N1.getValueType().isVector() &&
8128 "SETCC type should be vector iff the operand type is vector!");
8129 assert((!VT.isVector() || VT.getVectorElementCount() ==
8131 "SETCC vector element counts must match!");
8132 // Use FoldSetCC to simplify SETCC's.
8133 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8134 return V;
8135 break;
8136 }
8137 case ISD::SELECT:
8138 case ISD::VSELECT:
8139 if (SDValue V = simplifySelect(N1, N2, N3))
8140 return V;
8141 break;
8143 llvm_unreachable("should use getVectorShuffle constructor!");
8144 case ISD::VECTOR_SPLICE: {
8145 if (cast<ConstantSDNode>(N3)->isZero())
8146 return N1;
8147 break;
8148 }
8150 assert(VT.isVector() && VT == N1.getValueType() &&
8151 "INSERT_VECTOR_ELT vector type mismatch");
8153 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8154 assert((!VT.isFloatingPoint() ||
8155 VT.getVectorElementType() == N2.getValueType()) &&
8156 "INSERT_VECTOR_ELT fp scalar type mismatch");
8157 assert((!VT.isInteger() ||
8159 "INSERT_VECTOR_ELT int scalar size mismatch");
8160
8161 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8162 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8163 // for scalable vectors where we will generate appropriate code to
8164 // deal with out-of-bounds cases correctly.
8165 if (N3C && VT.isFixedLengthVector() &&
8166 N3C->getZExtValue() >= VT.getVectorNumElements())
8167 return getUNDEF(VT);
8168
8169 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8170 if (N3.isUndef())
8171 return getUNDEF(VT);
8172
8173 // If inserting poison, just use the input vector.
8174 if (N2.getOpcode() == ISD::POISON)
8175 return N1;
8176
8177 // Inserting undef into undef/poison is still undef.
8178 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8179 return getUNDEF(VT);
8180
8181 // If the inserted element is an UNDEF, just use the input vector.
8182 // But not if skipping the insert could make the result more poisonous.
8183 if (N2.isUndef()) {
8184 if (N3C && VT.isFixedLengthVector()) {
8185 APInt EltMask =
8186 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8187 if (isGuaranteedNotToBePoison(N1, EltMask))
8188 return N1;
8189 } else if (isGuaranteedNotToBePoison(N1))
8190 return N1;
8191 }
8192 break;
8193 }
8194 case ISD::INSERT_SUBVECTOR: {
8195 // If inserting poison, just use the input vector,
8196 if (N2.getOpcode() == ISD::POISON)
8197 return N1;
8198
8199 // Inserting undef into undef/poison is still undef.
8200 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8201 return getUNDEF(VT);
8202
8203 EVT N2VT = N2.getValueType();
8204 assert(VT == N1.getValueType() &&
8205 "Dest and insert subvector source types must match!");
8206 assert(VT.isVector() && N2VT.isVector() &&
8207 "Insert subvector VTs must be vectors!");
8209 "Insert subvector VTs must have the same element type!");
8210 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8211 "Cannot insert a scalable vector into a fixed length vector!");
8212 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8214 "Insert subvector must be from smaller vector to larger vector!");
8216 "Insert subvector index must be constant");
8217 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8218 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8220 "Insert subvector overflow!");
8222 TLI->getVectorIdxWidth(getDataLayout()) &&
8223 "Constant index for INSERT_SUBVECTOR has an invalid size");
8224
8225 // Trivial insertion.
8226 if (VT == N2VT)
8227 return N2;
8228
8229 // If this is an insert of an extracted vector into an undef/poison vector,
8230 // we can just use the input to the extract. But not if skipping the
8231 // extract+insert could make the result more poisonous.
8232 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8233 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8234 if (N1.getOpcode() == ISD::POISON)
8235 return N2.getOperand(0);
8236 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8237 unsigned LoBit = N3->getAsZExtVal();
8238 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8239 APInt EltMask =
8240 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8241 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8242 return N2.getOperand(0);
8243 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8244 return N2.getOperand(0);
8245 }
8246
8247 // If the inserted subvector is UNDEF, just use the input vector.
8248 // But not if skipping the insert could make the result more poisonous.
8249 if (N2.isUndef()) {
8250 if (VT.isFixedLengthVector()) {
8251 unsigned LoBit = N3->getAsZExtVal();
8252 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8253 APInt EltMask =
8254 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8255 if (isGuaranteedNotToBePoison(N1, EltMask))
8256 return N1;
8257 } else if (isGuaranteedNotToBePoison(N1))
8258 return N1;
8259 }
8260 break;
8261 }
8262 case ISD::BITCAST:
8263 // Fold bit_convert nodes from a type to themselves.
8264 if (N1.getValueType() == VT)
8265 return N1;
8266 break;
8267 case ISD::VP_TRUNCATE:
8268 case ISD::VP_SIGN_EXTEND:
8269 case ISD::VP_ZERO_EXTEND:
8270 // Don't create noop casts.
8271 if (N1.getValueType() == VT)
8272 return N1;
8273 break;
8274 case ISD::VECTOR_COMPRESS: {
8275 [[maybe_unused]] EVT VecVT = N1.getValueType();
8276 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8277 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8278 assert(VT == VecVT && "Vector and result type don't match.");
8279 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8280 "All inputs must be vectors.");
8281 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8283 "Vector and mask must have same number of elements.");
8284
8285 if (N1.isUndef() || N2.isUndef())
8286 return N3;
8287
8288 break;
8289 }
8290 case ISD::PARTIAL_REDUCE_UMLA:
8291 case ISD::PARTIAL_REDUCE_SMLA:
8292 case ISD::PARTIAL_REDUCE_SUMLA:
8293 case ISD::PARTIAL_REDUCE_FMLA: {
8294 [[maybe_unused]] EVT AccVT = N1.getValueType();
8295 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8296 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8297 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8298 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8299 "node to have the same type!");
8300 assert(VT.isVector() && VT == AccVT &&
8301 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8302 "the same type as its result!");
8304 AccVT.getVectorElementCount()) &&
8305 "Expected the element count of the second and third operands of the "
8306 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8307 "element count of the first operand and the result!");
8309 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8310 "node to have an element type which is the same as or smaller than "
8311 "the element type of the first operand and result!");
8312 break;
8313 }
8314 }
8315
8316 // Perform trivial constant folding for arithmetic operators.
8317 switch (Opcode) {
8318 case ISD::FMA:
8319 case ISD::FMAD:
8320 case ISD::SETCC:
8321 case ISD::FSHL:
8322 case ISD::FSHR:
8323 if (SDValue SV =
8324 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8325 return SV;
8326 break;
8327 }
8328
8329 // Memoize node if it doesn't produce a glue result.
8330 SDNode *N;
8331 SDVTList VTs = getVTList(VT);
8332 SDValue Ops[] = {N1, N2, N3};
8333 if (VT != MVT::Glue) {
8335 AddNodeIDNode(ID, Opcode, VTs, Ops);
8336 void *IP = nullptr;
8337 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8338 E->intersectFlagsWith(Flags);
8339 return SDValue(E, 0);
8340 }
8341
8342 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8343 N->setFlags(Flags);
8344 createOperands(N, Ops);
8345 CSEMap.InsertNode(N, IP);
8346 } else {
8347 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8348 createOperands(N, Ops);
8349 }
8350
8351 InsertNode(N);
8352 SDValue V = SDValue(N, 0);
8353 NewSDValueDbgMsg(V, "Creating new node: ", this);
8354 return V;
8355}
8356
8357SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8358 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8359 const SDNodeFlags Flags) {
8360 SDValue Ops[] = { N1, N2, N3, N4 };
8361 return getNode(Opcode, DL, VT, Ops, Flags);
8362}
8363
8364SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8365 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8366 SDNodeFlags Flags;
8367 if (Inserter)
8368 Flags = Inserter->getFlags();
8369 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8370}
8371
8372SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8373 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8374 SDValue N5, const SDNodeFlags Flags) {
8375 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8376 return getNode(Opcode, DL, VT, Ops, Flags);
8377}
8378
8379SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8380 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8381 SDValue N5) {
8382 SDNodeFlags Flags;
8383 if (Inserter)
8384 Flags = Inserter->getFlags();
8385 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8386}
8387
8388/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8389/// the incoming stack arguments to be loaded from the stack.
8391 SmallVector<SDValue, 8> ArgChains;
8392
8393 // Include the original chain at the beginning of the list. When this is
8394 // used by target LowerCall hooks, this helps legalize find the
8395 // CALLSEQ_BEGIN node.
8396 ArgChains.push_back(Chain);
8397
8398 // Add a chain value for each stack argument.
8399 for (SDNode *U : getEntryNode().getNode()->users())
8400 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8401 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8402 if (FI->getIndex() < 0)
8403 ArgChains.push_back(SDValue(L, 1));
8404
8405 // Build a tokenfactor for all the chains.
8406 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8407}
8408
8409/// getMemsetValue - Vectorized representation of the memset value
8410/// operand.
8412 const SDLoc &dl) {
8413 assert(!Value.isUndef());
8414
8415 unsigned NumBits = VT.getScalarSizeInBits();
8417 assert(C->getAPIntValue().getBitWidth() == 8);
8418 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8419 if (VT.isInteger()) {
8420 bool IsOpaque = VT.getSizeInBits() > 64 ||
8421 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8422 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8423 }
8424 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8425 }
8426
8427 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8428 EVT IntVT = VT.getScalarType();
8429 if (!IntVT.isInteger())
8430 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8431
8432 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8433 if (NumBits > 8) {
8434 // Use a multiplication with 0x010101... to extend the input to the
8435 // required length.
8436 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8437 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8438 DAG.getConstant(Magic, dl, IntVT));
8439 }
8440
8441 if (VT != Value.getValueType() && !VT.isInteger())
8442 Value = DAG.getBitcast(VT.getScalarType(), Value);
8443 if (VT != Value.getValueType())
8444 Value = DAG.getSplatBuildVector(VT, dl, Value);
8445
8446 return Value;
8447}
8448
8449/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8450/// used when a memcpy is turned into a memset when the source is a constant
8451/// string ptr.
8453 const TargetLowering &TLI,
8454 const ConstantDataArraySlice &Slice) {
8455 // Handle vector with all elements zero.
8456 if (Slice.Array == nullptr) {
8457 if (VT.isInteger())
8458 return DAG.getConstant(0, dl, VT);
8459 return DAG.getNode(ISD::BITCAST, dl, VT,
8460 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8461 }
8462
8463 assert(!VT.isVector() && "Can't handle vector type here!");
8464 unsigned NumVTBits = VT.getSizeInBits();
8465 unsigned NumVTBytes = NumVTBits / 8;
8466 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8467
8468 APInt Val(NumVTBits, 0);
8469 if (DAG.getDataLayout().isLittleEndian()) {
8470 for (unsigned i = 0; i != NumBytes; ++i)
8471 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8472 } else {
8473 for (unsigned i = 0; i != NumBytes; ++i)
8474 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8475 }
8476
8477 // If the "cost" of materializing the integer immediate is less than the cost
8478 // of a load, then it is cost effective to turn the load into the immediate.
8479 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8480 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8481 return DAG.getConstant(Val, dl, VT);
8482 return SDValue();
8483}
8484
8486 const SDLoc &DL,
8487 const SDNodeFlags Flags) {
8488 EVT VT = Base.getValueType();
8489 SDValue Index;
8490
8491 if (Offset.isScalable())
8492 Index = getVScale(DL, Base.getValueType(),
8493 APInt(Base.getValueSizeInBits().getFixedValue(),
8494 Offset.getKnownMinValue()));
8495 else
8496 Index = getConstant(Offset.getFixedValue(), DL, VT);
8497
8498 return getMemBasePlusOffset(Base, Index, DL, Flags);
8499}
8500
8502 const SDLoc &DL,
8503 const SDNodeFlags Flags) {
8504 assert(Offset.getValueType().isInteger());
8505 EVT BasePtrVT = Ptr.getValueType();
8506 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8507 BasePtrVT))
8508 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8509 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8510 SDNodeFlags AddFlags = Flags;
8511 AddFlags.setInBounds(false);
8512 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8513}
8514
8515/// Returns true if memcpy source is constant data.
8517 uint64_t SrcDelta = 0;
8518 GlobalAddressSDNode *G = nullptr;
8519 if (Src.getOpcode() == ISD::GlobalAddress)
8521 else if (Src->isAnyAdd() &&
8522 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8523 Src.getOperand(1).getOpcode() == ISD::Constant) {
8524 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8525 SrcDelta = Src.getConstantOperandVal(1);
8526 }
8527 if (!G)
8528 return false;
8529
8530 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8531 SrcDelta + G->getOffset());
8532}
8533
8535 SelectionDAG &DAG) {
8536 // On Darwin, -Os means optimize for size without hurting performance, so
8537 // only really optimize for size when -Oz (MinSize) is used.
8539 return MF.getFunction().hasMinSize();
8540 return DAG.shouldOptForSize();
8541}
8542
8544 SmallVector<SDValue, 32> &OutChains, unsigned From,
8545 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8546 SmallVector<SDValue, 16> &OutStoreChains) {
8547 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8548 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8549 SmallVector<SDValue, 16> GluedLoadChains;
8550 for (unsigned i = From; i < To; ++i) {
8551 OutChains.push_back(OutLoadChains[i]);
8552 GluedLoadChains.push_back(OutLoadChains[i]);
8553 }
8554
8555 // Chain for all loads.
8556 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8557 GluedLoadChains);
8558
8559 for (unsigned i = From; i < To; ++i) {
8560 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8561 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8562 ST->getBasePtr(), ST->getMemoryVT(),
8563 ST->getMemOperand());
8564 OutChains.push_back(NewStore);
8565 }
8566}
8567
8569 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8570 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8571 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8572 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8573 // Turn a memcpy of undef to nop.
8574 // FIXME: We need to honor volatile even is Src is undef.
8575 if (Src.isUndef())
8576 return Chain;
8577
8578 // Expand memcpy to a series of load and store ops if the size operand falls
8579 // below a certain threshold.
8580 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8581 // rather than maybe a humongous number of loads and stores.
8582 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8583 const DataLayout &DL = DAG.getDataLayout();
8584 LLVMContext &C = *DAG.getContext();
8585 std::vector<EVT> MemOps;
8586 bool DstAlignCanChange = false;
8588 MachineFrameInfo &MFI = MF.getFrameInfo();
8589 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8591 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8592 DstAlignCanChange = true;
8593 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8594 if (!SrcAlign || Alignment > *SrcAlign)
8595 SrcAlign = Alignment;
8596 assert(SrcAlign && "SrcAlign must be set");
8598 // If marked as volatile, perform a copy even when marked as constant.
8599 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8600 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8601 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8602 const MemOp Op = isZeroConstant
8603 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8604 /*IsZeroMemset*/ true, isVol)
8605 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8606 *SrcAlign, isVol, CopyFromConstant);
8607 if (!TLI.findOptimalMemOpLowering(
8608 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8609 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8610 return SDValue();
8611
8612 if (DstAlignCanChange) {
8613 Type *Ty = MemOps[0].getTypeForEVT(C);
8614 Align NewAlign = DL.getABITypeAlign(Ty);
8615
8616 // Don't promote to an alignment that would require dynamic stack
8617 // realignment which may conflict with optimizations such as tail call
8618 // optimization.
8620 if (!TRI->hasStackRealignment(MF))
8621 if (MaybeAlign StackAlign = DL.getStackAlignment())
8622 NewAlign = std::min(NewAlign, *StackAlign);
8623
8624 if (NewAlign > Alignment) {
8625 // Give the stack frame object a larger alignment if needed.
8626 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8627 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8628 Alignment = NewAlign;
8629 }
8630 }
8631
8632 // Prepare AAInfo for loads/stores after lowering this memcpy.
8633 AAMDNodes NewAAInfo = AAInfo;
8634 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8635
8636 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8637 bool isConstant =
8638 BatchAA && SrcVal &&
8639 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8640
8641 MachineMemOperand::Flags MMOFlags =
8643 SmallVector<SDValue, 16> OutLoadChains;
8644 SmallVector<SDValue, 16> OutStoreChains;
8645 SmallVector<SDValue, 32> OutChains;
8646 unsigned NumMemOps = MemOps.size();
8647 uint64_t SrcOff = 0, DstOff = 0;
8648 for (unsigned i = 0; i != NumMemOps; ++i) {
8649 EVT VT = MemOps[i];
8650 unsigned VTSize = VT.getSizeInBits() / 8;
8651 SDValue Value, Store;
8652
8653 if (VTSize > Size) {
8654 // Issuing an unaligned load / store pair that overlaps with the previous
8655 // pair. Adjust the offset accordingly.
8656 assert(i == NumMemOps-1 && i != 0);
8657 SrcOff -= VTSize - Size;
8658 DstOff -= VTSize - Size;
8659 }
8660
8661 if (CopyFromConstant &&
8662 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8663 // It's unlikely a store of a vector immediate can be done in a single
8664 // instruction. It would require a load from a constantpool first.
8665 // We only handle zero vectors here.
8666 // FIXME: Handle other cases where store of vector immediate is done in
8667 // a single instruction.
8668 ConstantDataArraySlice SubSlice;
8669 if (SrcOff < Slice.Length) {
8670 SubSlice = Slice;
8671 SubSlice.move(SrcOff);
8672 } else {
8673 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8674 SubSlice.Array = nullptr;
8675 SubSlice.Offset = 0;
8676 SubSlice.Length = VTSize;
8677 }
8678 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8679 if (Value.getNode()) {
8680 Store = DAG.getStore(
8681 Chain, dl, Value,
8682 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8683 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8684 OutChains.push_back(Store);
8685 }
8686 }
8687
8688 if (!Store.getNode()) {
8689 // The type might not be legal for the target. This should only happen
8690 // if the type is smaller than a legal type, as on PPC, so the right
8691 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8692 // to Load/Store if NVT==VT.
8693 // FIXME does the case above also need this?
8694 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8695 assert(NVT.bitsGE(VT));
8696
8697 bool isDereferenceable =
8698 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8699 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8700 if (isDereferenceable)
8702 if (isConstant)
8703 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8704
8705 Value = DAG.getExtLoad(
8706 ISD::EXTLOAD, dl, NVT, Chain,
8707 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8708 SrcPtrInfo.getWithOffset(SrcOff), VT,
8709 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8710 OutLoadChains.push_back(Value.getValue(1));
8711
8712 Store = DAG.getTruncStore(
8713 Chain, dl, Value,
8714 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8715 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8716 OutStoreChains.push_back(Store);
8717 }
8718 SrcOff += VTSize;
8719 DstOff += VTSize;
8720 Size -= VTSize;
8721 }
8722
8723 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8725 unsigned NumLdStInMemcpy = OutStoreChains.size();
8726
8727 if (NumLdStInMemcpy) {
8728 // It may be that memcpy might be converted to memset if it's memcpy
8729 // of constants. In such a case, we won't have loads and stores, but
8730 // just stores. In the absence of loads, there is nothing to gang up.
8731 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8732 // If target does not care, just leave as it.
8733 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8734 OutChains.push_back(OutLoadChains[i]);
8735 OutChains.push_back(OutStoreChains[i]);
8736 }
8737 } else {
8738 // Ld/St less than/equal limit set by target.
8739 if (NumLdStInMemcpy <= GluedLdStLimit) {
8740 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8741 NumLdStInMemcpy, OutLoadChains,
8742 OutStoreChains);
8743 } else {
8744 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8745 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8746 unsigned GlueIter = 0;
8747
8748 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8749 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8750 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8751
8752 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8753 OutLoadChains, OutStoreChains);
8754 GlueIter += GluedLdStLimit;
8755 }
8756
8757 // Residual ld/st.
8758 if (RemainingLdStInMemcpy) {
8759 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8760 RemainingLdStInMemcpy, OutLoadChains,
8761 OutStoreChains);
8762 }
8763 }
8764 }
8765 }
8766 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8767}
8768
8770 SDValue Chain, SDValue Dst, SDValue Src,
8771 uint64_t Size, Align Alignment,
8772 bool isVol, bool AlwaysInline,
8773 MachinePointerInfo DstPtrInfo,
8774 MachinePointerInfo SrcPtrInfo,
8775 const AAMDNodes &AAInfo) {
8776 // Turn a memmove of undef to nop.
8777 // FIXME: We need to honor volatile even is Src is undef.
8778 if (Src.isUndef())
8779 return Chain;
8780
8781 // Expand memmove to a series of load and store ops if the size operand falls
8782 // below a certain threshold.
8783 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8784 const DataLayout &DL = DAG.getDataLayout();
8785 LLVMContext &C = *DAG.getContext();
8786 std::vector<EVT> MemOps;
8787 bool DstAlignCanChange = false;
8789 MachineFrameInfo &MFI = MF.getFrameInfo();
8790 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8792 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8793 DstAlignCanChange = true;
8794 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8795 if (!SrcAlign || Alignment > *SrcAlign)
8796 SrcAlign = Alignment;
8797 assert(SrcAlign && "SrcAlign must be set");
8798 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8799 if (!TLI.findOptimalMemOpLowering(
8800 C, MemOps, Limit,
8801 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8802 /*IsVolatile*/ true),
8803 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8804 MF.getFunction().getAttributes()))
8805 return SDValue();
8806
8807 if (DstAlignCanChange) {
8808 Type *Ty = MemOps[0].getTypeForEVT(C);
8809 Align NewAlign = DL.getABITypeAlign(Ty);
8810
8811 // Don't promote to an alignment that would require dynamic stack
8812 // realignment which may conflict with optimizations such as tail call
8813 // optimization.
8815 if (!TRI->hasStackRealignment(MF))
8816 if (MaybeAlign StackAlign = DL.getStackAlignment())
8817 NewAlign = std::min(NewAlign, *StackAlign);
8818
8819 if (NewAlign > Alignment) {
8820 // Give the stack frame object a larger alignment if needed.
8821 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8822 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8823 Alignment = NewAlign;
8824 }
8825 }
8826
8827 // Prepare AAInfo for loads/stores after lowering this memmove.
8828 AAMDNodes NewAAInfo = AAInfo;
8829 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8830
8831 MachineMemOperand::Flags MMOFlags =
8833 uint64_t SrcOff = 0, DstOff = 0;
8834 SmallVector<SDValue, 8> LoadValues;
8835 SmallVector<SDValue, 8> LoadChains;
8836 SmallVector<SDValue, 8> OutChains;
8837 unsigned NumMemOps = MemOps.size();
8838 for (unsigned i = 0; i < NumMemOps; i++) {
8839 EVT VT = MemOps[i];
8840 unsigned VTSize = VT.getSizeInBits() / 8;
8841 SDValue Value;
8842
8843 bool isDereferenceable =
8844 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8845 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8846 if (isDereferenceable)
8848
8849 Value = DAG.getLoad(
8850 VT, dl, Chain,
8851 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8852 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8853 LoadValues.push_back(Value);
8854 LoadChains.push_back(Value.getValue(1));
8855 SrcOff += VTSize;
8856 }
8857 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8858 OutChains.clear();
8859 for (unsigned i = 0; i < NumMemOps; i++) {
8860 EVT VT = MemOps[i];
8861 unsigned VTSize = VT.getSizeInBits() / 8;
8862 SDValue Store;
8863
8864 Store = DAG.getStore(
8865 Chain, dl, LoadValues[i],
8866 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8867 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8868 OutChains.push_back(Store);
8869 DstOff += VTSize;
8870 }
8871
8872 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8873}
8874
8875/// Lower the call to 'memset' intrinsic function into a series of store
8876/// operations.
8877///
8878/// \param DAG Selection DAG where lowered code is placed.
8879/// \param dl Link to corresponding IR location.
8880/// \param Chain Control flow dependency.
8881/// \param Dst Pointer to destination memory location.
8882/// \param Src Value of byte to write into the memory.
8883/// \param Size Number of bytes to write.
8884/// \param Alignment Alignment of the destination in bytes.
8885/// \param isVol True if destination is volatile.
8886/// \param AlwaysInline Makes sure no function call is generated.
8887/// \param DstPtrInfo IR information on the memory pointer.
8888/// \returns New head in the control flow, if lowering was successful, empty
8889/// SDValue otherwise.
8890///
8891/// The function tries to replace 'llvm.memset' intrinsic with several store
8892/// operations and value calculation code. This is usually profitable for small
8893/// memory size or when the semantic requires inlining.
8895 SDValue Chain, SDValue Dst, SDValue Src,
8896 uint64_t Size, Align Alignment, bool isVol,
8897 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8898 const AAMDNodes &AAInfo) {
8899 // Turn a memset of undef to nop.
8900 // FIXME: We need to honor volatile even is Src is undef.
8901 if (Src.isUndef())
8902 return Chain;
8903
8904 // Expand memset to a series of load/store ops if the size operand
8905 // falls below a certain threshold.
8906 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8907 std::vector<EVT> MemOps;
8908 bool DstAlignCanChange = false;
8909 LLVMContext &C = *DAG.getContext();
8911 MachineFrameInfo &MFI = MF.getFrameInfo();
8912 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8914 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8915 DstAlignCanChange = true;
8916 bool IsZeroVal = isNullConstant(Src);
8917 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8918
8919 if (!TLI.findOptimalMemOpLowering(
8920 C, MemOps, Limit,
8921 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8922 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8923 return SDValue();
8924
8925 if (DstAlignCanChange) {
8926 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8927 const DataLayout &DL = DAG.getDataLayout();
8928 Align NewAlign = DL.getABITypeAlign(Ty);
8929
8930 // Don't promote to an alignment that would require dynamic stack
8931 // realignment which may conflict with optimizations such as tail call
8932 // optimization.
8934 if (!TRI->hasStackRealignment(MF))
8935 if (MaybeAlign StackAlign = DL.getStackAlignment())
8936 NewAlign = std::min(NewAlign, *StackAlign);
8937
8938 if (NewAlign > Alignment) {
8939 // Give the stack frame object a larger alignment if needed.
8940 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8941 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8942 Alignment = NewAlign;
8943 }
8944 }
8945
8946 SmallVector<SDValue, 8> OutChains;
8947 uint64_t DstOff = 0;
8948 unsigned NumMemOps = MemOps.size();
8949
8950 // Find the largest store and generate the bit pattern for it.
8951 EVT LargestVT = MemOps[0];
8952 for (unsigned i = 1; i < NumMemOps; i++)
8953 if (MemOps[i].bitsGT(LargestVT))
8954 LargestVT = MemOps[i];
8955 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8956
8957 // Prepare AAInfo for loads/stores after lowering this memset.
8958 AAMDNodes NewAAInfo = AAInfo;
8959 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8960
8961 for (unsigned i = 0; i < NumMemOps; i++) {
8962 EVT VT = MemOps[i];
8963 unsigned VTSize = VT.getSizeInBits() / 8;
8964 if (VTSize > Size) {
8965 // Issuing an unaligned load / store pair that overlaps with the previous
8966 // pair. Adjust the offset accordingly.
8967 assert(i == NumMemOps-1 && i != 0);
8968 DstOff -= VTSize - Size;
8969 }
8970
8971 // If this store is smaller than the largest store see whether we can get
8972 // the smaller value for free with a truncate or extract vector element and
8973 // then store.
8974 SDValue Value = MemSetValue;
8975 if (VT.bitsLT(LargestVT)) {
8976 unsigned Index;
8977 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8978 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8979 if (!LargestVT.isVector() && !VT.isVector() &&
8980 TLI.isTruncateFree(LargestVT, VT))
8981 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8982 else if (LargestVT.isVector() && !VT.isVector() &&
8984 LargestVT.getTypeForEVT(*DAG.getContext()),
8985 VT.getSizeInBits(), Index) &&
8986 TLI.isTypeLegal(SVT) &&
8987 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8988 // Target which can combine store(extractelement VectorTy, Idx) can get
8989 // the smaller value for free.
8990 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8991 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
8992 } else
8993 Value = getMemsetValue(Src, VT, DAG, dl);
8994 }
8995 assert(Value.getValueType() == VT && "Value with wrong type.");
8996 SDValue Store = DAG.getStore(
8997 Chain, dl, Value,
8998 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8999 DstPtrInfo.getWithOffset(DstOff), Alignment,
9001 NewAAInfo);
9002 OutChains.push_back(Store);
9003 DstOff += VT.getSizeInBits() / 8;
9004 Size -= VTSize;
9005 }
9006
9007 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9008}
9009
9011 unsigned AS) {
9012 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9013 // pointer operands can be losslessly bitcasted to pointers of address space 0
9014 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9015 report_fatal_error("cannot lower memory intrinsic in address space " +
9016 Twine(AS));
9017 }
9018}
9019
9021 const SelectionDAG *SelDAG,
9022 bool AllowReturnsFirstArg) {
9023 if (!CI || !CI->isTailCall())
9024 return false;
9025 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9026 // helper symbol we lower to.
9027 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9028 AllowReturnsFirstArg &&
9030}
9031
9032std::pair<SDValue, SDValue>
9034 SDValue Mem1, SDValue Size, const CallInst *CI) {
9035 const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
9036 if (!LibCallName)
9037 return {};
9038
9041 {Mem0, PT},
9042 {Mem1, PT},
9044
9046 bool IsTailCall =
9047 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9048
9049 CLI.setDebugLoc(dl)
9050 .setChain(Chain)
9051 .setLibCallee(
9052 TLI->getLibcallCallingConv(RTLIB::MEMCMP),
9054 getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
9055 std::move(Args))
9056 .setTailCall(IsTailCall);
9057
9058 return TLI->LowerCallTo(CLI);
9059}
9060
9061std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9062 const SDLoc &dl,
9063 SDValue Src,
9064 const CallInst *CI) {
9065 const char *LibCallName = TLI->getLibcallName(RTLIB::STRLEN);
9066 if (!LibCallName)
9067 return {};
9068
9069 // Emit a library call.
9072
9074 bool IsTailCall =
9075 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9076
9077 CLI.setDebugLoc(dl)
9078 .setChain(Chain)
9079 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::STRLEN), CI->getType(),
9081 LibCallName, TLI->getProgramPointerTy(getDataLayout())),
9082 std::move(Args))
9083 .setTailCall(IsTailCall);
9084
9085 return TLI->LowerCallTo(CLI);
9086}
9087
9089 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9090 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9091 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9092 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9093 BatchAAResults *BatchAA) {
9094 // Check to see if we should lower the memcpy to loads and stores first.
9095 // For cases within the target-specified limits, this is the best choice.
9097 if (ConstantSize) {
9098 // Memcpy with size zero? Just return the original chain.
9099 if (ConstantSize->isZero())
9100 return Chain;
9101
9103 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9104 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9105 if (Result.getNode())
9106 return Result;
9107 }
9108
9109 // Then check to see if we should lower the memcpy with target-specific
9110 // code. If the target chooses to do this, this is the next best.
9111 if (TSI) {
9112 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9113 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9114 DstPtrInfo, SrcPtrInfo);
9115 if (Result.getNode())
9116 return Result;
9117 }
9118
9119 // If we really need inline code and the target declined to provide it,
9120 // use a (potentially long) sequence of loads and stores.
9121 if (AlwaysInline) {
9122 assert(ConstantSize && "AlwaysInline requires a constant size!");
9124 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9125 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9126 }
9127
9130
9131 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9132 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9133 // respect volatile, so they may do things like read or write memory
9134 // beyond the given memory regions. But fixing this isn't easy, and most
9135 // people don't care.
9136
9137 // Emit a library call.
9140 Args.emplace_back(Dst, PtrTy);
9141 Args.emplace_back(Src, PtrTy);
9142 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9143 // FIXME: pass in SDLoc
9145 bool IsTailCall = false;
9146 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9147
9148 if (OverrideTailCall.has_value()) {
9149 IsTailCall = *OverrideTailCall;
9150 } else {
9151 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9152 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9153 }
9154
9155 CLI.setDebugLoc(dl)
9156 .setChain(Chain)
9157 .setLibCallee(
9158 TLI->getLibcallImplCallingConv(MemCpyImpl),
9159 Dst.getValueType().getTypeForEVT(*getContext()),
9160 getExternalSymbol(TLI->getLibcallImplName(MemCpyImpl).data(),
9161 TLI->getPointerTy(getDataLayout())),
9162 std::move(Args))
9164 .setTailCall(IsTailCall);
9165
9166 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9167 return CallResult.second;
9168}
9169
9171 SDValue Dst, SDValue Src, SDValue Size,
9172 Type *SizeTy, unsigned ElemSz,
9173 bool isTailCall,
9174 MachinePointerInfo DstPtrInfo,
9175 MachinePointerInfo SrcPtrInfo) {
9176 // Emit a library call.
9179 Args.emplace_back(Dst, ArgTy);
9180 Args.emplace_back(Src, ArgTy);
9181 Args.emplace_back(Size, SizeTy);
9182
9183 RTLIB::Libcall LibraryCall =
9185 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9186 report_fatal_error("Unsupported element size");
9187
9189 CLI.setDebugLoc(dl)
9190 .setChain(Chain)
9191 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9193 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9194 TLI->getPointerTy(getDataLayout())),
9195 std::move(Args))
9197 .setTailCall(isTailCall);
9198
9199 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9200 return CallResult.second;
9201}
9202
9204 SDValue Src, SDValue Size, Align Alignment,
9205 bool isVol, const CallInst *CI,
9206 std::optional<bool> OverrideTailCall,
9207 MachinePointerInfo DstPtrInfo,
9208 MachinePointerInfo SrcPtrInfo,
9209 const AAMDNodes &AAInfo,
9210 BatchAAResults *BatchAA) {
9211 // Check to see if we should lower the memmove to loads and stores first.
9212 // For cases within the target-specified limits, this is the best choice.
9214 if (ConstantSize) {
9215 // Memmove with size zero? Just return the original chain.
9216 if (ConstantSize->isZero())
9217 return Chain;
9218
9220 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9221 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9222 if (Result.getNode())
9223 return Result;
9224 }
9225
9226 // Then check to see if we should lower the memmove with target-specific
9227 // code. If the target chooses to do this, this is the next best.
9228 if (TSI) {
9229 SDValue Result =
9230 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9231 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9232 if (Result.getNode())
9233 return Result;
9234 }
9235
9238
9239 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9240 // not be safe. See memcpy above for more details.
9241
9242 // Emit a library call.
9245 Args.emplace_back(Dst, PtrTy);
9246 Args.emplace_back(Src, PtrTy);
9247 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9248 // FIXME: pass in SDLoc
9250
9251 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9252
9253 bool IsTailCall = false;
9254 if (OverrideTailCall.has_value()) {
9255 IsTailCall = *OverrideTailCall;
9256 } else {
9257 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9258 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9259 }
9260
9261 CLI.setDebugLoc(dl)
9262 .setChain(Chain)
9263 .setLibCallee(
9264 TLI->getLibcallImplCallingConv(MemmoveImpl),
9265 Dst.getValueType().getTypeForEVT(*getContext()),
9266 getExternalSymbol(TLI->getLibcallImplName(MemmoveImpl).data(),
9267 TLI->getPointerTy(getDataLayout())),
9268 std::move(Args))
9270 .setTailCall(IsTailCall);
9271
9272 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9273 return CallResult.second;
9274}
9275
9277 SDValue Dst, SDValue Src, SDValue Size,
9278 Type *SizeTy, unsigned ElemSz,
9279 bool isTailCall,
9280 MachinePointerInfo DstPtrInfo,
9281 MachinePointerInfo SrcPtrInfo) {
9282 // Emit a library call.
9284 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9285 Args.emplace_back(Dst, IntPtrTy);
9286 Args.emplace_back(Src, IntPtrTy);
9287 Args.emplace_back(Size, SizeTy);
9288
9289 RTLIB::Libcall LibraryCall =
9291 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9292 report_fatal_error("Unsupported element size");
9293
9295 CLI.setDebugLoc(dl)
9296 .setChain(Chain)
9297 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9299 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9300 TLI->getPointerTy(getDataLayout())),
9301 std::move(Args))
9303 .setTailCall(isTailCall);
9304
9305 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9306 return CallResult.second;
9307}
9308
9310 SDValue Src, SDValue Size, Align Alignment,
9311 bool isVol, bool AlwaysInline,
9312 const CallInst *CI,
9313 MachinePointerInfo DstPtrInfo,
9314 const AAMDNodes &AAInfo) {
9315 // Check to see if we should lower the memset to stores first.
9316 // For cases within the target-specified limits, this is the best choice.
9318 if (ConstantSize) {
9319 // Memset with size zero? Just return the original chain.
9320 if (ConstantSize->isZero())
9321 return Chain;
9322
9323 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9324 ConstantSize->getZExtValue(), Alignment,
9325 isVol, false, DstPtrInfo, AAInfo);
9326
9327 if (Result.getNode())
9328 return Result;
9329 }
9330
9331 // Then check to see if we should lower the memset with target-specific
9332 // code. If the target chooses to do this, this is the next best.
9333 if (TSI) {
9334 SDValue Result = TSI->EmitTargetCodeForMemset(
9335 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9336 if (Result.getNode())
9337 return Result;
9338 }
9339
9340 // If we really need inline code and the target declined to provide it,
9341 // use a (potentially long) sequence of loads and stores.
9342 if (AlwaysInline) {
9343 assert(ConstantSize && "AlwaysInline requires a constant size!");
9344 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9345 ConstantSize->getZExtValue(), Alignment,
9346 isVol, true, DstPtrInfo, AAInfo);
9347 assert(Result &&
9348 "getMemsetStores must return a valid sequence when AlwaysInline");
9349 return Result;
9350 }
9351
9353
9354 // Emit a library call.
9355 auto &Ctx = *getContext();
9356 const auto& DL = getDataLayout();
9357
9359 // FIXME: pass in SDLoc
9360 CLI.setDebugLoc(dl).setChain(Chain);
9361
9362 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
9363
9364 bool UseBZero = isNullConstant(Src) && BzeroName;
9365 // If zeroing out and bzero is present, use it.
9366 if (UseBZero) {
9368 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9369 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9370 CLI.setLibCallee(
9371 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
9372 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
9373 } else {
9375 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9376 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9377 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9378 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
9379 Dst.getValueType().getTypeForEVT(Ctx),
9380 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
9381 TLI->getPointerTy(DL)),
9382 std::move(Args));
9383 }
9384
9385 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9386 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9387
9388 // If we're going to use bzero, make sure not to tail call unless the
9389 // subsequent return doesn't need a value, as bzero doesn't return the first
9390 // arg unlike memset.
9391 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9392 bool IsTailCall =
9393 CI && CI->isTailCall() &&
9394 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9395 CLI.setDiscardResult().setTailCall(IsTailCall);
9396
9397 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9398 return CallResult.second;
9399}
9400
9403 Type *SizeTy, unsigned ElemSz,
9404 bool isTailCall,
9405 MachinePointerInfo DstPtrInfo) {
9406 // Emit a library call.
9408 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9409 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9410 Args.emplace_back(Size, SizeTy);
9411
9412 RTLIB::Libcall LibraryCall =
9414 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9415 report_fatal_error("Unsupported element size");
9416
9418 CLI.setDebugLoc(dl)
9419 .setChain(Chain)
9420 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9422 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9423 TLI->getPointerTy(getDataLayout())),
9424 std::move(Args))
9426 .setTailCall(isTailCall);
9427
9428 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9429 return CallResult.second;
9430}
9431
9432SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9434 MachineMemOperand *MMO,
9435 ISD::LoadExtType ExtType) {
9437 AddNodeIDNode(ID, Opcode, VTList, Ops);
9438 ID.AddInteger(MemVT.getRawBits());
9439 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9440 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9441 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9442 ID.AddInteger(MMO->getFlags());
9443 void* IP = nullptr;
9444 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9445 E->refineAlignment(MMO);
9446 E->refineRanges(MMO);
9447 return SDValue(E, 0);
9448 }
9449
9450 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9451 VTList, MemVT, MMO, ExtType);
9452 createOperands(N, Ops);
9453
9454 CSEMap.InsertNode(N, IP);
9455 InsertNode(N);
9456 SDValue V(N, 0);
9457 NewSDValueDbgMsg(V, "Creating new node: ", this);
9458 return V;
9459}
9460
9462 EVT MemVT, SDVTList VTs, SDValue Chain,
9463 SDValue Ptr, SDValue Cmp, SDValue Swp,
9464 MachineMemOperand *MMO) {
9465 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9466 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
9467 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9468
9469 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9470 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9471}
9472
9473SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9474 SDValue Chain, SDValue Ptr, SDValue Val,
9475 MachineMemOperand *MMO) {
9476 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9477 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9478 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9479 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9480 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9481 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9482 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9483 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9484 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9485 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9486 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9487 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9488 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9489 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9490 Opcode == ISD::ATOMIC_STORE) &&
9491 "Invalid Atomic Op");
9492
9493 EVT VT = Val.getValueType();
9494
9495 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9496 getVTList(VT, MVT::Other);
9497 SDValue Ops[] = {Chain, Ptr, Val};
9498 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9499}
9500
9502 EVT MemVT, EVT VT, SDValue Chain,
9503 SDValue Ptr, MachineMemOperand *MMO) {
9504 SDVTList VTs = getVTList(VT, MVT::Other);
9505 SDValue Ops[] = {Chain, Ptr};
9506 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9507}
9508
9509/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9511 if (Ops.size() == 1)
9512 return Ops[0];
9513
9515 VTs.reserve(Ops.size());
9516 for (const SDValue &Op : Ops)
9517 VTs.push_back(Op.getValueType());
9518 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9519}
9520
9522 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9523 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9525 const AAMDNodes &AAInfo) {
9526 if (Size.hasValue() && !Size.getValue())
9528
9530 MachineMemOperand *MMO =
9531 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9532
9533 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9534}
9535
9537 SDVTList VTList,
9538 ArrayRef<SDValue> Ops, EVT MemVT,
9539 MachineMemOperand *MMO) {
9540 assert(
9541 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9542 Opcode == ISD::PREFETCH ||
9543 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9544 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9545 "Opcode is not a memory-accessing opcode!");
9546
9547 // Memoize the node unless it returns a glue result.
9549 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9551 AddNodeIDNode(ID, Opcode, VTList, Ops);
9552 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9553 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9554 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9555 ID.AddInteger(MMO->getFlags());
9556 ID.AddInteger(MemVT.getRawBits());
9557 void *IP = nullptr;
9558 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9559 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9560 return SDValue(E, 0);
9561 }
9562
9563 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9564 VTList, MemVT, MMO);
9565 createOperands(N, Ops);
9566
9567 CSEMap.InsertNode(N, IP);
9568 } else {
9569 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9570 VTList, MemVT, MMO);
9571 createOperands(N, Ops);
9572 }
9573 InsertNode(N);
9574 SDValue V(N, 0);
9575 NewSDValueDbgMsg(V, "Creating new node: ", this);
9576 return V;
9577}
9578
9580 SDValue Chain, int FrameIndex) {
9581 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9582 const auto VTs = getVTList(MVT::Other);
9583 SDValue Ops[2] = {
9584 Chain,
9585 getFrameIndex(FrameIndex,
9586 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9587 true)};
9588
9590 AddNodeIDNode(ID, Opcode, VTs, Ops);
9591 ID.AddInteger(FrameIndex);
9592 void *IP = nullptr;
9593 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9594 return SDValue(E, 0);
9595
9596 LifetimeSDNode *N =
9597 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9598 createOperands(N, Ops);
9599 CSEMap.InsertNode(N, IP);
9600 InsertNode(N);
9601 SDValue V(N, 0);
9602 NewSDValueDbgMsg(V, "Creating new node: ", this);
9603 return V;
9604}
9605
9607 uint64_t Guid, uint64_t Index,
9608 uint32_t Attr) {
9609 const unsigned Opcode = ISD::PSEUDO_PROBE;
9610 const auto VTs = getVTList(MVT::Other);
9611 SDValue Ops[] = {Chain};
9613 AddNodeIDNode(ID, Opcode, VTs, Ops);
9614 ID.AddInteger(Guid);
9615 ID.AddInteger(Index);
9616 void *IP = nullptr;
9617 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9618 return SDValue(E, 0);
9619
9620 auto *N = newSDNode<PseudoProbeSDNode>(
9621 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9622 createOperands(N, Ops);
9623 CSEMap.InsertNode(N, IP);
9624 InsertNode(N);
9625 SDValue V(N, 0);
9626 NewSDValueDbgMsg(V, "Creating new node: ", this);
9627 return V;
9628}
9629
9630/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9631/// MachinePointerInfo record from it. This is particularly useful because the
9632/// code generator has many cases where it doesn't bother passing in a
9633/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9635 SelectionDAG &DAG, SDValue Ptr,
9636 int64_t Offset = 0) {
9637 // If this is FI+Offset, we can model it.
9638 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9640 FI->getIndex(), Offset);
9641
9642 // If this is (FI+Offset1)+Offset2, we can model it.
9643 if (Ptr.getOpcode() != ISD::ADD ||
9646 return Info;
9647
9648 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9650 DAG.getMachineFunction(), FI,
9651 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9652}
9653
9654/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9655/// MachinePointerInfo record from it. This is particularly useful because the
9656/// code generator has many cases where it doesn't bother passing in a
9657/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9659 SelectionDAG &DAG, SDValue Ptr,
9660 SDValue OffsetOp) {
9661 // If the 'Offset' value isn't a constant, we can't handle this.
9663 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9664 if (OffsetOp.isUndef())
9665 return InferPointerInfo(Info, DAG, Ptr);
9666 return Info;
9667}
9668
9670 EVT VT, const SDLoc &dl, SDValue Chain,
9671 SDValue Ptr, SDValue Offset,
9672 MachinePointerInfo PtrInfo, EVT MemVT,
9673 Align Alignment,
9674 MachineMemOperand::Flags MMOFlags,
9675 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9676 assert(Chain.getValueType() == MVT::Other &&
9677 "Invalid chain type");
9678
9679 MMOFlags |= MachineMemOperand::MOLoad;
9680 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9681 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9682 // clients.
9683 if (PtrInfo.V.isNull())
9684 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9685
9686 TypeSize Size = MemVT.getStoreSize();
9688 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9689 Alignment, AAInfo, Ranges);
9690 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9691}
9692
9694 EVT VT, const SDLoc &dl, SDValue Chain,
9695 SDValue Ptr, SDValue Offset, EVT MemVT,
9696 MachineMemOperand *MMO) {
9697 if (VT == MemVT) {
9698 ExtType = ISD::NON_EXTLOAD;
9699 } else if (ExtType == ISD::NON_EXTLOAD) {
9700 assert(VT == MemVT && "Non-extending load from different memory type!");
9701 } else {
9702 // Extending load.
9703 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9704 "Should only be an extending load, not truncating!");
9705 assert(VT.isInteger() == MemVT.isInteger() &&
9706 "Cannot convert from FP to Int or Int -> FP!");
9707 assert(VT.isVector() == MemVT.isVector() &&
9708 "Cannot use an ext load to convert to or from a vector!");
9709 assert((!VT.isVector() ||
9711 "Cannot use an ext load to change the number of vector elements!");
9712 }
9713
9714 assert((!MMO->getRanges() ||
9716 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9717 MemVT.isInteger())) &&
9718 "Range metadata and load type must match!");
9719
9720 bool Indexed = AM != ISD::UNINDEXED;
9721 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9722
9723 SDVTList VTs = Indexed ?
9724 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9725 SDValue Ops[] = { Chain, Ptr, Offset };
9727 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9728 ID.AddInteger(MemVT.getRawBits());
9729 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9730 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9731 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9732 ID.AddInteger(MMO->getFlags());
9733 void *IP = nullptr;
9734 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9735 E->refineAlignment(MMO);
9736 E->refineRanges(MMO);
9737 return SDValue(E, 0);
9738 }
9739 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9740 ExtType, MemVT, MMO);
9741 createOperands(N, Ops);
9742
9743 CSEMap.InsertNode(N, IP);
9744 InsertNode(N);
9745 SDValue V(N, 0);
9746 NewSDValueDbgMsg(V, "Creating new node: ", this);
9747 return V;
9748}
9749
9751 SDValue Ptr, MachinePointerInfo PtrInfo,
9752 MaybeAlign Alignment,
9753 MachineMemOperand::Flags MMOFlags,
9754 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9755 SDValue Undef = getUNDEF(Ptr.getValueType());
9756 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9757 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9758}
9759
9761 SDValue Ptr, MachineMemOperand *MMO) {
9762 SDValue Undef = getUNDEF(Ptr.getValueType());
9763 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9764 VT, MMO);
9765}
9766
9768 EVT VT, SDValue Chain, SDValue Ptr,
9769 MachinePointerInfo PtrInfo, EVT MemVT,
9770 MaybeAlign Alignment,
9771 MachineMemOperand::Flags MMOFlags,
9772 const AAMDNodes &AAInfo) {
9773 SDValue Undef = getUNDEF(Ptr.getValueType());
9774 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9775 MemVT, Alignment, MMOFlags, AAInfo);
9776}
9777
9779 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9780 MachineMemOperand *MMO) {
9781 SDValue Undef = getUNDEF(Ptr.getValueType());
9782 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9783 MemVT, MMO);
9784}
9785
9789 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9790 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9791 // Don't propagate the invariant or dereferenceable flags.
9792 auto MMOFlags =
9793 LD->getMemOperand()->getFlags() &
9795 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9796 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9797 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9798}
9799
9801 SDValue Ptr, MachinePointerInfo PtrInfo,
9802 Align Alignment,
9803 MachineMemOperand::Flags MMOFlags,
9804 const AAMDNodes &AAInfo) {
9805 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9806
9807 MMOFlags |= MachineMemOperand::MOStore;
9808 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9809
9810 if (PtrInfo.V.isNull())
9811 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9812
9815 MachineMemOperand *MMO =
9816 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9817 return getStore(Chain, dl, Val, Ptr, MMO);
9818}
9819
9821 SDValue Ptr, MachineMemOperand *MMO) {
9822 SDValue Undef = getUNDEF(Ptr.getValueType());
9823 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9825}
9826
9828 SDValue Ptr, SDValue Offset, EVT SVT,
9830 bool IsTruncating) {
9831 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9832 EVT VT = Val.getValueType();
9833 if (VT == SVT) {
9834 IsTruncating = false;
9835 } else if (!IsTruncating) {
9836 assert(VT == SVT && "No-truncating store from different memory type!");
9837 } else {
9839 "Should only be a truncating store, not extending!");
9840 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9841 assert(VT.isVector() == SVT.isVector() &&
9842 "Cannot use trunc store to convert to or from a vector!");
9843 assert((!VT.isVector() ||
9845 "Cannot use trunc store to change the number of vector elements!");
9846 }
9847
9848 bool Indexed = AM != ISD::UNINDEXED;
9849 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9850 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9851 : getVTList(MVT::Other);
9852 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9854 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9855 ID.AddInteger(SVT.getRawBits());
9856 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9857 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9858 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9859 ID.AddInteger(MMO->getFlags());
9860 void *IP = nullptr;
9861 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9862 cast<StoreSDNode>(E)->refineAlignment(MMO);
9863 return SDValue(E, 0);
9864 }
9865 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9866 IsTruncating, SVT, MMO);
9867 createOperands(N, Ops);
9868
9869 CSEMap.InsertNode(N, IP);
9870 InsertNode(N);
9871 SDValue V(N, 0);
9872 NewSDValueDbgMsg(V, "Creating new node: ", this);
9873 return V;
9874}
9875
9877 SDValue Ptr, MachinePointerInfo PtrInfo,
9878 EVT SVT, Align Alignment,
9879 MachineMemOperand::Flags MMOFlags,
9880 const AAMDNodes &AAInfo) {
9881 assert(Chain.getValueType() == MVT::Other &&
9882 "Invalid chain type");
9883
9884 MMOFlags |= MachineMemOperand::MOStore;
9885 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9886
9887 if (PtrInfo.V.isNull())
9888 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9889
9891 MachineMemOperand *MMO = MF.getMachineMemOperand(
9892 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9893 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9894}
9895
9897 SDValue Ptr, EVT SVT,
9898 MachineMemOperand *MMO) {
9899 SDValue Undef = getUNDEF(Ptr.getValueType());
9900 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9901}
9902
9906 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9907 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9908 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9909 ST->getMemoryVT(), ST->getMemOperand(), AM,
9910 ST->isTruncatingStore());
9911}
9912
9914 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9915 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9916 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9917 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9918 const MDNode *Ranges, bool IsExpanding) {
9919 MMOFlags |= MachineMemOperand::MOLoad;
9920 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9921 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9922 // clients.
9923 if (PtrInfo.V.isNull())
9924 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9925
9926 TypeSize Size = MemVT.getStoreSize();
9928 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9929 Alignment, AAInfo, Ranges);
9930 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9931 MMO, IsExpanding);
9932}
9933
9935 ISD::LoadExtType ExtType, EVT VT,
9936 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9937 SDValue Offset, SDValue Mask, SDValue EVL,
9938 EVT MemVT, MachineMemOperand *MMO,
9939 bool IsExpanding) {
9940 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9941 assert(Mask.getValueType().getVectorElementCount() ==
9942 VT.getVectorElementCount() &&
9943 "Vector width mismatch between mask and data");
9944
9945 bool Indexed = AM != ISD::UNINDEXED;
9946 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9947
9948 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9949 : getVTList(VT, MVT::Other);
9950 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9952 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9953 ID.AddInteger(MemVT.getRawBits());
9954 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9955 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9956 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9957 ID.AddInteger(MMO->getFlags());
9958 void *IP = nullptr;
9959 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9960 E->refineAlignment(MMO);
9961 E->refineRanges(MMO);
9962 return SDValue(E, 0);
9963 }
9964 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9965 ExtType, IsExpanding, MemVT, MMO);
9966 createOperands(N, Ops);
9967
9968 CSEMap.InsertNode(N, IP);
9969 InsertNode(N);
9970 SDValue V(N, 0);
9971 NewSDValueDbgMsg(V, "Creating new node: ", this);
9972 return V;
9973}
9974
9976 SDValue Ptr, SDValue Mask, SDValue EVL,
9977 MachinePointerInfo PtrInfo,
9978 MaybeAlign Alignment,
9979 MachineMemOperand::Flags MMOFlags,
9980 const AAMDNodes &AAInfo, const MDNode *Ranges,
9981 bool IsExpanding) {
9982 SDValue Undef = getUNDEF(Ptr.getValueType());
9983 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9984 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9985 IsExpanding);
9986}
9987
9989 SDValue Ptr, SDValue Mask, SDValue EVL,
9990 MachineMemOperand *MMO, bool IsExpanding) {
9991 SDValue Undef = getUNDEF(Ptr.getValueType());
9992 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9993 Mask, EVL, VT, MMO, IsExpanding);
9994}
9995
9997 EVT VT, SDValue Chain, SDValue Ptr,
9998 SDValue Mask, SDValue EVL,
9999 MachinePointerInfo PtrInfo, EVT MemVT,
10000 MaybeAlign Alignment,
10001 MachineMemOperand::Flags MMOFlags,
10002 const AAMDNodes &AAInfo, bool IsExpanding) {
10003 SDValue Undef = getUNDEF(Ptr.getValueType());
10004 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10005 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10006 IsExpanding);
10007}
10008
10010 EVT VT, SDValue Chain, SDValue Ptr,
10011 SDValue Mask, SDValue EVL, EVT MemVT,
10012 MachineMemOperand *MMO, bool IsExpanding) {
10013 SDValue Undef = getUNDEF(Ptr.getValueType());
10014 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10015 EVL, MemVT, MMO, IsExpanding);
10016}
10017
10021 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10022 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10023 // Don't propagate the invariant or dereferenceable flags.
10024 auto MMOFlags =
10025 LD->getMemOperand()->getFlags() &
10027 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10028 LD->getChain(), Base, Offset, LD->getMask(),
10029 LD->getVectorLength(), LD->getPointerInfo(),
10030 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10031 nullptr, LD->isExpandingLoad());
10032}
10033
10035 SDValue Ptr, SDValue Offset, SDValue Mask,
10036 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10037 ISD::MemIndexedMode AM, bool IsTruncating,
10038 bool IsCompressing) {
10039 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10040 assert(Mask.getValueType().getVectorElementCount() ==
10042 "Vector width mismatch between mask and data");
10043
10044 bool Indexed = AM != ISD::UNINDEXED;
10045 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10046 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10047 : getVTList(MVT::Other);
10048 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10050 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10051 ID.AddInteger(MemVT.getRawBits());
10052 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10053 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10054 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10055 ID.AddInteger(MMO->getFlags());
10056 void *IP = nullptr;
10057 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10058 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10059 return SDValue(E, 0);
10060 }
10061 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10062 IsTruncating, IsCompressing, MemVT, MMO);
10063 createOperands(N, Ops);
10064
10065 CSEMap.InsertNode(N, IP);
10066 InsertNode(N);
10067 SDValue V(N, 0);
10068 NewSDValueDbgMsg(V, "Creating new node: ", this);
10069 return V;
10070}
10071
10073 SDValue Val, SDValue Ptr, SDValue Mask,
10074 SDValue EVL, MachinePointerInfo PtrInfo,
10075 EVT SVT, Align Alignment,
10076 MachineMemOperand::Flags MMOFlags,
10077 const AAMDNodes &AAInfo,
10078 bool IsCompressing) {
10079 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10080
10081 MMOFlags |= MachineMemOperand::MOStore;
10082 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10083
10084 if (PtrInfo.V.isNull())
10085 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10086
10088 MachineMemOperand *MMO = MF.getMachineMemOperand(
10089 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10090 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10091 IsCompressing);
10092}
10093
10095 SDValue Val, SDValue Ptr, SDValue Mask,
10096 SDValue EVL, EVT SVT,
10097 MachineMemOperand *MMO,
10098 bool IsCompressing) {
10099 EVT VT = Val.getValueType();
10100
10101 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10102 if (VT == SVT)
10103 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10104 EVL, VT, MMO, ISD::UNINDEXED,
10105 /*IsTruncating*/ false, IsCompressing);
10106
10108 "Should only be a truncating store, not extending!");
10109 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10110 assert(VT.isVector() == SVT.isVector() &&
10111 "Cannot use trunc store to convert to or from a vector!");
10112 assert((!VT.isVector() ||
10114 "Cannot use trunc store to change the number of vector elements!");
10115
10116 SDVTList VTs = getVTList(MVT::Other);
10117 SDValue Undef = getUNDEF(Ptr.getValueType());
10118 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10120 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10121 ID.AddInteger(SVT.getRawBits());
10122 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10123 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10124 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10125 ID.AddInteger(MMO->getFlags());
10126 void *IP = nullptr;
10127 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10128 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10129 return SDValue(E, 0);
10130 }
10131 auto *N =
10132 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10133 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10134 createOperands(N, Ops);
10135
10136 CSEMap.InsertNode(N, IP);
10137 InsertNode(N);
10138 SDValue V(N, 0);
10139 NewSDValueDbgMsg(V, "Creating new node: ", this);
10140 return V;
10141}
10142
10146 auto *ST = cast<VPStoreSDNode>(OrigStore);
10147 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10148 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10149 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10150 Offset, ST->getMask(), ST->getVectorLength()};
10152 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10153 ID.AddInteger(ST->getMemoryVT().getRawBits());
10154 ID.AddInteger(ST->getRawSubclassData());
10155 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10156 ID.AddInteger(ST->getMemOperand()->getFlags());
10157 void *IP = nullptr;
10158 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10159 return SDValue(E, 0);
10160
10161 auto *N = newSDNode<VPStoreSDNode>(
10162 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10163 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10164 createOperands(N, Ops);
10165
10166 CSEMap.InsertNode(N, IP);
10167 InsertNode(N);
10168 SDValue V(N, 0);
10169 NewSDValueDbgMsg(V, "Creating new node: ", this);
10170 return V;
10171}
10172
10174 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10175 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10176 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10177 bool Indexed = AM != ISD::UNINDEXED;
10178 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10179
10180 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10181 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10182 : getVTList(VT, MVT::Other);
10184 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10185 ID.AddInteger(VT.getRawBits());
10186 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10187 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10188 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10189
10190 void *IP = nullptr;
10191 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10192 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10193 return SDValue(E, 0);
10194 }
10195
10196 auto *N =
10197 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10198 ExtType, IsExpanding, MemVT, MMO);
10199 createOperands(N, Ops);
10200 CSEMap.InsertNode(N, IP);
10201 InsertNode(N);
10202 SDValue V(N, 0);
10203 NewSDValueDbgMsg(V, "Creating new node: ", this);
10204 return V;
10205}
10206
10208 SDValue Ptr, SDValue Stride,
10209 SDValue Mask, SDValue EVL,
10210 MachineMemOperand *MMO,
10211 bool IsExpanding) {
10212 SDValue Undef = getUNDEF(Ptr.getValueType());
10213 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10214 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10215}
10216
10218 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10219 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10220 MachineMemOperand *MMO, bool IsExpanding) {
10221 SDValue Undef = getUNDEF(Ptr.getValueType());
10222 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10223 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10224}
10225
10227 SDValue Val, SDValue Ptr,
10228 SDValue Offset, SDValue Stride,
10229 SDValue Mask, SDValue EVL, EVT MemVT,
10230 MachineMemOperand *MMO,
10232 bool IsTruncating, bool IsCompressing) {
10233 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10234 bool Indexed = AM != ISD::UNINDEXED;
10235 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10236 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10237 : getVTList(MVT::Other);
10238 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10240 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10241 ID.AddInteger(MemVT.getRawBits());
10242 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10243 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10244 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10245 void *IP = nullptr;
10246 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10247 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10248 return SDValue(E, 0);
10249 }
10250 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10251 VTs, AM, IsTruncating,
10252 IsCompressing, MemVT, MMO);
10253 createOperands(N, Ops);
10254
10255 CSEMap.InsertNode(N, IP);
10256 InsertNode(N);
10257 SDValue V(N, 0);
10258 NewSDValueDbgMsg(V, "Creating new node: ", this);
10259 return V;
10260}
10261
10263 SDValue Val, SDValue Ptr,
10264 SDValue Stride, SDValue Mask,
10265 SDValue EVL, EVT SVT,
10266 MachineMemOperand *MMO,
10267 bool IsCompressing) {
10268 EVT VT = Val.getValueType();
10269
10270 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10271 if (VT == SVT)
10272 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10273 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10274 /*IsTruncating*/ false, IsCompressing);
10275
10277 "Should only be a truncating store, not extending!");
10278 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10279 assert(VT.isVector() == SVT.isVector() &&
10280 "Cannot use trunc store to convert to or from a vector!");
10281 assert((!VT.isVector() ||
10283 "Cannot use trunc store to change the number of vector elements!");
10284
10285 SDVTList VTs = getVTList(MVT::Other);
10286 SDValue Undef = getUNDEF(Ptr.getValueType());
10287 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10289 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10290 ID.AddInteger(SVT.getRawBits());
10291 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10292 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10293 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10294 void *IP = nullptr;
10295 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10296 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10297 return SDValue(E, 0);
10298 }
10299 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10300 VTs, ISD::UNINDEXED, true,
10301 IsCompressing, SVT, MMO);
10302 createOperands(N, Ops);
10303
10304 CSEMap.InsertNode(N, IP);
10305 InsertNode(N);
10306 SDValue V(N, 0);
10307 NewSDValueDbgMsg(V, "Creating new node: ", this);
10308 return V;
10309}
10310
10313 ISD::MemIndexType IndexType) {
10314 assert(Ops.size() == 6 && "Incompatible number of operands");
10315
10317 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10318 ID.AddInteger(VT.getRawBits());
10319 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10320 dl.getIROrder(), VTs, VT, MMO, IndexType));
10321 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10322 ID.AddInteger(MMO->getFlags());
10323 void *IP = nullptr;
10324 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10325 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10326 return SDValue(E, 0);
10327 }
10328
10329 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10330 VT, MMO, IndexType);
10331 createOperands(N, Ops);
10332
10333 assert(N->getMask().getValueType().getVectorElementCount() ==
10334 N->getValueType(0).getVectorElementCount() &&
10335 "Vector width mismatch between mask and data");
10336 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10337 N->getValueType(0).getVectorElementCount().isScalable() &&
10338 "Scalable flags of index and data do not match");
10340 N->getIndex().getValueType().getVectorElementCount(),
10341 N->getValueType(0).getVectorElementCount()) &&
10342 "Vector width mismatch between index and data");
10343 assert(isa<ConstantSDNode>(N->getScale()) &&
10344 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10345 "Scale should be a constant power of 2");
10346
10347 CSEMap.InsertNode(N, IP);
10348 InsertNode(N);
10349 SDValue V(N, 0);
10350 NewSDValueDbgMsg(V, "Creating new node: ", this);
10351 return V;
10352}
10353
10356 MachineMemOperand *MMO,
10357 ISD::MemIndexType IndexType) {
10358 assert(Ops.size() == 7 && "Incompatible number of operands");
10359
10361 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10362 ID.AddInteger(VT.getRawBits());
10363 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10364 dl.getIROrder(), VTs, VT, MMO, IndexType));
10365 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10366 ID.AddInteger(MMO->getFlags());
10367 void *IP = nullptr;
10368 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10369 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10370 return SDValue(E, 0);
10371 }
10372 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10373 VT, MMO, IndexType);
10374 createOperands(N, Ops);
10375
10376 assert(N->getMask().getValueType().getVectorElementCount() ==
10377 N->getValue().getValueType().getVectorElementCount() &&
10378 "Vector width mismatch between mask and data");
10379 assert(
10380 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10381 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10382 "Scalable flags of index and data do not match");
10384 N->getIndex().getValueType().getVectorElementCount(),
10385 N->getValue().getValueType().getVectorElementCount()) &&
10386 "Vector width mismatch between index and data");
10387 assert(isa<ConstantSDNode>(N->getScale()) &&
10388 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10389 "Scale should be a constant power of 2");
10390
10391 CSEMap.InsertNode(N, IP);
10392 InsertNode(N);
10393 SDValue V(N, 0);
10394 NewSDValueDbgMsg(V, "Creating new node: ", this);
10395 return V;
10396}
10397
10400 SDValue PassThru, EVT MemVT,
10401 MachineMemOperand *MMO,
10403 ISD::LoadExtType ExtTy, bool isExpanding) {
10404 bool Indexed = AM != ISD::UNINDEXED;
10405 assert((Indexed || Offset.isUndef()) &&
10406 "Unindexed masked load with an offset!");
10407 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10408 : getVTList(VT, MVT::Other);
10409 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10411 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10412 ID.AddInteger(MemVT.getRawBits());
10413 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10414 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10415 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10416 ID.AddInteger(MMO->getFlags());
10417 void *IP = nullptr;
10418 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10419 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10420 return SDValue(E, 0);
10421 }
10422 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10423 AM, ExtTy, isExpanding, MemVT, MMO);
10424 createOperands(N, Ops);
10425
10426 CSEMap.InsertNode(N, IP);
10427 InsertNode(N);
10428 SDValue V(N, 0);
10429 NewSDValueDbgMsg(V, "Creating new node: ", this);
10430 return V;
10431}
10432
10437 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10438 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10439 Offset, LD->getMask(), LD->getPassThru(),
10440 LD->getMemoryVT(), LD->getMemOperand(), AM,
10441 LD->getExtensionType(), LD->isExpandingLoad());
10442}
10443
10446 SDValue Mask, EVT MemVT,
10447 MachineMemOperand *MMO,
10448 ISD::MemIndexedMode AM, bool IsTruncating,
10449 bool IsCompressing) {
10450 assert(Chain.getValueType() == MVT::Other &&
10451 "Invalid chain type");
10452 bool Indexed = AM != ISD::UNINDEXED;
10453 assert((Indexed || Offset.isUndef()) &&
10454 "Unindexed masked store with an offset!");
10455 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10456 : getVTList(MVT::Other);
10457 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10459 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10460 ID.AddInteger(MemVT.getRawBits());
10461 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10462 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10463 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10464 ID.AddInteger(MMO->getFlags());
10465 void *IP = nullptr;
10466 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10467 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10468 return SDValue(E, 0);
10469 }
10470 auto *N =
10471 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10472 IsTruncating, IsCompressing, MemVT, MMO);
10473 createOperands(N, Ops);
10474
10475 CSEMap.InsertNode(N, IP);
10476 InsertNode(N);
10477 SDValue V(N, 0);
10478 NewSDValueDbgMsg(V, "Creating new node: ", this);
10479 return V;
10480}
10481
10486 assert(ST->getOffset().isUndef() &&
10487 "Masked store is already a indexed store!");
10488 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10489 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10490 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10491}
10492
10495 MachineMemOperand *MMO,
10496 ISD::MemIndexType IndexType,
10497 ISD::LoadExtType ExtTy) {
10498 assert(Ops.size() == 6 && "Incompatible number of operands");
10499
10501 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10502 ID.AddInteger(MemVT.getRawBits());
10503 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10504 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
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<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10510 return SDValue(E, 0);
10511 }
10512
10513 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10514 VTs, MemVT, MMO, IndexType, ExtTy);
10515 createOperands(N, Ops);
10516
10517 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10518 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10519 assert(N->getMask().getValueType().getVectorElementCount() ==
10520 N->getValueType(0).getVectorElementCount() &&
10521 "Vector width mismatch between mask and data");
10522 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10523 N->getValueType(0).getVectorElementCount().isScalable() &&
10524 "Scalable flags of index and data do not match");
10526 N->getIndex().getValueType().getVectorElementCount(),
10527 N->getValueType(0).getVectorElementCount()) &&
10528 "Vector width mismatch between index and data");
10529 assert(isa<ConstantSDNode>(N->getScale()) &&
10530 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10531 "Scale should be a constant power of 2");
10532
10533 CSEMap.InsertNode(N, IP);
10534 InsertNode(N);
10535 SDValue V(N, 0);
10536 NewSDValueDbgMsg(V, "Creating new node: ", this);
10537 return V;
10538}
10539
10542 MachineMemOperand *MMO,
10543 ISD::MemIndexType IndexType,
10544 bool IsTrunc) {
10545 assert(Ops.size() == 6 && "Incompatible number of operands");
10546
10548 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10549 ID.AddInteger(MemVT.getRawBits());
10550 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10551 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10552 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10553 ID.AddInteger(MMO->getFlags());
10554 void *IP = nullptr;
10555 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10556 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10557 return SDValue(E, 0);
10558 }
10559
10560 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10561 VTs, MemVT, MMO, IndexType, IsTrunc);
10562 createOperands(N, Ops);
10563
10564 assert(N->getMask().getValueType().getVectorElementCount() ==
10565 N->getValue().getValueType().getVectorElementCount() &&
10566 "Vector width mismatch between mask and data");
10567 assert(
10568 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10569 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10570 "Scalable flags of index and data do not match");
10572 N->getIndex().getValueType().getVectorElementCount(),
10573 N->getValue().getValueType().getVectorElementCount()) &&
10574 "Vector width mismatch between index and data");
10575 assert(isa<ConstantSDNode>(N->getScale()) &&
10576 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10577 "Scale should be a constant power of 2");
10578
10579 CSEMap.InsertNode(N, IP);
10580 InsertNode(N);
10581 SDValue V(N, 0);
10582 NewSDValueDbgMsg(V, "Creating new node: ", this);
10583 return V;
10584}
10585
10587 const SDLoc &dl, ArrayRef<SDValue> Ops,
10588 MachineMemOperand *MMO,
10589 ISD::MemIndexType IndexType) {
10590 assert(Ops.size() == 7 && "Incompatible number of operands");
10591
10593 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VECTOR_HISTOGRAM, VTs, Ops);
10594 ID.AddInteger(MemVT.getRawBits());
10595 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10596 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10597 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10598 ID.AddInteger(MMO->getFlags());
10599 void *IP = nullptr;
10600 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10601 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10602 return SDValue(E, 0);
10603 }
10604
10605 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10606 VTs, MemVT, MMO, IndexType);
10607 createOperands(N, Ops);
10608
10609 assert(N->getMask().getValueType().getVectorElementCount() ==
10610 N->getIndex().getValueType().getVectorElementCount() &&
10611 "Vector width mismatch between mask and data");
10612 assert(isa<ConstantSDNode>(N->getScale()) &&
10613 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10614 "Scale should be a constant power of 2");
10615 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10616
10617 CSEMap.InsertNode(N, IP);
10618 InsertNode(N);
10619 SDValue V(N, 0);
10620 NewSDValueDbgMsg(V, "Creating new node: ", this);
10621 return V;
10622}
10623
10625 SDValue Ptr, SDValue Mask, SDValue EVL,
10626 MachineMemOperand *MMO) {
10627 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10628 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10630 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10631 ID.AddInteger(VT.getRawBits());
10632 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10633 VTs, VT, MMO));
10634 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10635 ID.AddInteger(MMO->getFlags());
10636 void *IP = nullptr;
10637 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10638 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10639 return SDValue(E, 0);
10640 }
10641 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10642 VT, MMO);
10643 createOperands(N, Ops);
10644
10645 CSEMap.InsertNode(N, IP);
10646 InsertNode(N);
10647 SDValue V(N, 0);
10648 NewSDValueDbgMsg(V, "Creating new node: ", this);
10649 return V;
10650}
10651
10653 EVT MemVT, MachineMemOperand *MMO) {
10654 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10655 SDVTList VTs = getVTList(MVT::Other);
10656 SDValue Ops[] = {Chain, Ptr};
10658 AddNodeIDNode(ID, ISD::GET_FPENV_MEM, VTs, Ops);
10659 ID.AddInteger(MemVT.getRawBits());
10660 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10661 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10662 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10663 ID.AddInteger(MMO->getFlags());
10664 void *IP = nullptr;
10665 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10666 return SDValue(E, 0);
10667
10668 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10669 dl.getDebugLoc(), VTs, MemVT, MMO);
10670 createOperands(N, Ops);
10671
10672 CSEMap.InsertNode(N, IP);
10673 InsertNode(N);
10674 SDValue V(N, 0);
10675 NewSDValueDbgMsg(V, "Creating new node: ", this);
10676 return V;
10677}
10678
10680 EVT MemVT, MachineMemOperand *MMO) {
10681 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10682 SDVTList VTs = getVTList(MVT::Other);
10683 SDValue Ops[] = {Chain, Ptr};
10685 AddNodeIDNode(ID, ISD::SET_FPENV_MEM, VTs, Ops);
10686 ID.AddInteger(MemVT.getRawBits());
10687 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10688 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10689 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10690 ID.AddInteger(MMO->getFlags());
10691 void *IP = nullptr;
10692 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10693 return SDValue(E, 0);
10694
10695 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10696 dl.getDebugLoc(), VTs, MemVT, MMO);
10697 createOperands(N, Ops);
10698
10699 CSEMap.InsertNode(N, IP);
10700 InsertNode(N);
10701 SDValue V(N, 0);
10702 NewSDValueDbgMsg(V, "Creating new node: ", this);
10703 return V;
10704}
10705
10707 // select undef, T, F --> T (if T is a constant), otherwise F
10708 // select, ?, undef, F --> F
10709 // select, ?, T, undef --> T
10710 if (Cond.isUndef())
10711 return isConstantValueOfAnyType(T) ? T : F;
10712 if (T.isUndef())
10713 return F;
10714 if (F.isUndef())
10715 return T;
10716
10717 // select true, T, F --> T
10718 // select false, T, F --> F
10719 if (auto C = isBoolConstant(Cond))
10720 return *C ? T : F;
10721
10722 // select ?, T, T --> T
10723 if (T == F)
10724 return T;
10725
10726 return SDValue();
10727}
10728
10730 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10731 if (X.isUndef())
10732 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10733 // shift X, undef --> undef (because it may shift by the bitwidth)
10734 if (Y.isUndef())
10735 return getUNDEF(X.getValueType());
10736
10737 // shift 0, Y --> 0
10738 // shift X, 0 --> X
10740 return X;
10741
10742 // shift X, C >= bitwidth(X) --> undef
10743 // All vector elements must be too big (or undef) to avoid partial undefs.
10744 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10745 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10746 };
10747 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10748 return getUNDEF(X.getValueType());
10749
10750 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10751 if (X.getValueType().getScalarType() == MVT::i1)
10752 return X;
10753
10754 return SDValue();
10755}
10756
10758 SDNodeFlags Flags) {
10759 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10760 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10761 // operation is poison. That result can be relaxed to undef.
10762 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10763 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10764 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10765 (YC && YC->getValueAPF().isNaN());
10766 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10767 (YC && YC->getValueAPF().isInfinity());
10768
10769 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10770 return getUNDEF(X.getValueType());
10771
10772 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10773 return getUNDEF(X.getValueType());
10774
10775 if (!YC)
10776 return SDValue();
10777
10778 // X + -0.0 --> X
10779 if (Opcode == ISD::FADD)
10780 if (YC->getValueAPF().isNegZero())
10781 return X;
10782
10783 // X - +0.0 --> X
10784 if (Opcode == ISD::FSUB)
10785 if (YC->getValueAPF().isPosZero())
10786 return X;
10787
10788 // X * 1.0 --> X
10789 // X / 1.0 --> X
10790 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10791 if (YC->getValueAPF().isExactlyValue(1.0))
10792 return X;
10793
10794 // X * 0.0 --> 0.0
10795 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10796 if (YC->getValueAPF().isZero())
10797 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10798
10799 return SDValue();
10800}
10801
10803 SDValue Ptr, SDValue SV, unsigned Align) {
10804 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10805 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10806}
10807
10808SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10810 switch (Ops.size()) {
10811 case 0: return getNode(Opcode, DL, VT);
10812 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10813 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10814 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10815 default: break;
10816 }
10817
10818 // Copy from an SDUse array into an SDValue array for use with
10819 // the regular getNode logic.
10821 return getNode(Opcode, DL, VT, NewOps);
10822}
10823
10824SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10826 SDNodeFlags Flags;
10827 if (Inserter)
10828 Flags = Inserter->getFlags();
10829 return getNode(Opcode, DL, VT, Ops, Flags);
10830}
10831
10832SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10833 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10834 unsigned NumOps = Ops.size();
10835 switch (NumOps) {
10836 case 0: return getNode(Opcode, DL, VT);
10837 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10838 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10839 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10840 default: break;
10841 }
10842
10843#ifndef NDEBUG
10844 for (const auto &Op : Ops)
10845 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10846 "Operand is DELETED_NODE!");
10847#endif
10848
10849 switch (Opcode) {
10850 default: break;
10851 case ISD::BUILD_VECTOR:
10852 // Attempt to simplify BUILD_VECTOR.
10853 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10854 return V;
10855 break;
10857 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10858 return V;
10859 break;
10860 case ISD::SELECT_CC:
10861 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10862 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10863 "LHS and RHS of condition must have same type!");
10864 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10865 "True and False arms of SelectCC must have same type!");
10866 assert(Ops[2].getValueType() == VT &&
10867 "select_cc node must be of same type as true and false value!");
10868 assert((!Ops[0].getValueType().isVector() ||
10869 Ops[0].getValueType().getVectorElementCount() ==
10870 VT.getVectorElementCount()) &&
10871 "Expected select_cc with vector result to have the same sized "
10872 "comparison type!");
10873 break;
10874 case ISD::BR_CC:
10875 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10876 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10877 "LHS/RHS of comparison should match types!");
10878 break;
10879 case ISD::VP_ADD:
10880 case ISD::VP_SUB:
10881 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10882 if (VT.getScalarType() == MVT::i1)
10883 Opcode = ISD::VP_XOR;
10884 break;
10885 case ISD::VP_MUL:
10886 // If it is VP_MUL mask operation then turn it to VP_AND
10887 if (VT.getScalarType() == MVT::i1)
10888 Opcode = ISD::VP_AND;
10889 break;
10890 case ISD::VP_REDUCE_MUL:
10891 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10892 if (VT == MVT::i1)
10893 Opcode = ISD::VP_REDUCE_AND;
10894 break;
10895 case ISD::VP_REDUCE_ADD:
10896 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10897 if (VT == MVT::i1)
10898 Opcode = ISD::VP_REDUCE_XOR;
10899 break;
10900 case ISD::VP_REDUCE_SMAX:
10901 case ISD::VP_REDUCE_UMIN:
10902 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10903 // VP_REDUCE_AND.
10904 if (VT == MVT::i1)
10905 Opcode = ISD::VP_REDUCE_AND;
10906 break;
10907 case ISD::VP_REDUCE_SMIN:
10908 case ISD::VP_REDUCE_UMAX:
10909 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10910 // VP_REDUCE_OR.
10911 if (VT == MVT::i1)
10912 Opcode = ISD::VP_REDUCE_OR;
10913 break;
10914 }
10915
10916 // Memoize nodes.
10917 SDNode *N;
10918 SDVTList VTs = getVTList(VT);
10919
10920 if (VT != MVT::Glue) {
10922 AddNodeIDNode(ID, Opcode, VTs, Ops);
10923 void *IP = nullptr;
10924
10925 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10926 E->intersectFlagsWith(Flags);
10927 return SDValue(E, 0);
10928 }
10929
10930 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10931 createOperands(N, Ops);
10932
10933 CSEMap.InsertNode(N, IP);
10934 } else {
10935 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10936 createOperands(N, Ops);
10937 }
10938
10939 N->setFlags(Flags);
10940 InsertNode(N);
10941 SDValue V(N, 0);
10942 NewSDValueDbgMsg(V, "Creating new node: ", this);
10943 return V;
10944}
10945
10946SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10947 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10948 SDNodeFlags Flags;
10949 if (Inserter)
10950 Flags = Inserter->getFlags();
10951 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10952}
10953
10954SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10956 const SDNodeFlags Flags) {
10957 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10958}
10959
10960SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10962 SDNodeFlags Flags;
10963 if (Inserter)
10964 Flags = Inserter->getFlags();
10965 return getNode(Opcode, DL, VTList, Ops, Flags);
10966}
10967
10968SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10969 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10970 if (VTList.NumVTs == 1)
10971 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10972
10973#ifndef NDEBUG
10974 for (const auto &Op : Ops)
10975 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10976 "Operand is DELETED_NODE!");
10977#endif
10978
10979 switch (Opcode) {
10980 case ISD::SADDO:
10981 case ISD::UADDO:
10982 case ISD::SSUBO:
10983 case ISD::USUBO: {
10984 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10985 "Invalid add/sub overflow op!");
10986 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10987 Ops[0].getValueType() == Ops[1].getValueType() &&
10988 Ops[0].getValueType() == VTList.VTs[0] &&
10989 "Binary operator types must match!");
10990 SDValue N1 = Ops[0], N2 = Ops[1];
10991 canonicalizeCommutativeBinop(Opcode, N1, N2);
10992
10993 // (X +- 0) -> X with zero-overflow.
10994 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10995 /*AllowTruncation*/ true);
10996 if (N2CV && N2CV->isZero()) {
10997 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10998 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10999 }
11000
11001 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11002 VTList.VTs[1].getScalarType() == MVT::i1) {
11003 SDValue F1 = getFreeze(N1);
11004 SDValue F2 = getFreeze(N2);
11005 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11006 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11007 return getNode(ISD::MERGE_VALUES, DL, VTList,
11008 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11009 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11010 Flags);
11011 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11012 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11013 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11014 return getNode(ISD::MERGE_VALUES, DL, VTList,
11015 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11016 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11017 Flags);
11018 }
11019 }
11020 break;
11021 }
11022 case ISD::SADDO_CARRY:
11023 case ISD::UADDO_CARRY:
11024 case ISD::SSUBO_CARRY:
11025 case ISD::USUBO_CARRY:
11026 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11027 "Invalid add/sub overflow op!");
11028 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11029 Ops[0].getValueType() == Ops[1].getValueType() &&
11030 Ops[0].getValueType() == VTList.VTs[0] &&
11031 Ops[2].getValueType() == VTList.VTs[1] &&
11032 "Binary operator types must match!");
11033 break;
11034 case ISD::SMUL_LOHI:
11035 case ISD::UMUL_LOHI: {
11036 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11037 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11038 VTList.VTs[0] == Ops[0].getValueType() &&
11039 VTList.VTs[0] == Ops[1].getValueType() &&
11040 "Binary operator types must match!");
11041 // Constant fold.
11044 if (LHS && RHS) {
11045 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11046 unsigned OutWidth = Width * 2;
11047 APInt Val = LHS->getAPIntValue();
11048 APInt Mul = RHS->getAPIntValue();
11049 if (Opcode == ISD::SMUL_LOHI) {
11050 Val = Val.sext(OutWidth);
11051 Mul = Mul.sext(OutWidth);
11052 } else {
11053 Val = Val.zext(OutWidth);
11054 Mul = Mul.zext(OutWidth);
11055 }
11056 Val *= Mul;
11057
11058 SDValue Hi =
11059 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11060 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11061 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11062 }
11063 break;
11064 }
11065 case ISD::FFREXP: {
11066 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11067 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11068 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11069
11071 int FrexpExp;
11072 APFloat FrexpMant =
11073 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11074 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11075 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11076 DL, VTList.VTs[1]);
11077 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11078 }
11079
11080 break;
11081 }
11083 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11084 "Invalid STRICT_FP_EXTEND!");
11085 assert(VTList.VTs[0].isFloatingPoint() &&
11086 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11087 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11088 "STRICT_FP_EXTEND result type should be vector iff the operand "
11089 "type is vector!");
11090 assert((!VTList.VTs[0].isVector() ||
11091 VTList.VTs[0].getVectorElementCount() ==
11092 Ops[1].getValueType().getVectorElementCount()) &&
11093 "Vector element count mismatch!");
11094 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11095 "Invalid fpext node, dst <= src!");
11096 break;
11098 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11099 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11100 "STRICT_FP_ROUND result type should be vector iff the operand "
11101 "type is vector!");
11102 assert((!VTList.VTs[0].isVector() ||
11103 VTList.VTs[0].getVectorElementCount() ==
11104 Ops[1].getValueType().getVectorElementCount()) &&
11105 "Vector element count mismatch!");
11106 assert(VTList.VTs[0].isFloatingPoint() &&
11107 Ops[1].getValueType().isFloatingPoint() &&
11108 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11109 Ops[2].getOpcode() == ISD::TargetConstant &&
11110 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11111 "Invalid STRICT_FP_ROUND!");
11112 break;
11113 }
11114
11115 // Memoize the node unless it returns a glue result.
11116 SDNode *N;
11117 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11119 AddNodeIDNode(ID, Opcode, VTList, Ops);
11120 void *IP = nullptr;
11121 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11122 E->intersectFlagsWith(Flags);
11123 return SDValue(E, 0);
11124 }
11125
11126 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11127 createOperands(N, Ops);
11128 CSEMap.InsertNode(N, IP);
11129 } else {
11130 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11131 createOperands(N, Ops);
11132 }
11133
11134 N->setFlags(Flags);
11135 InsertNode(N);
11136 SDValue V(N, 0);
11137 NewSDValueDbgMsg(V, "Creating new node: ", this);
11138 return V;
11139}
11140
11141SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11142 SDVTList VTList) {
11143 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11144}
11145
11146SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11147 SDValue N1) {
11148 SDValue Ops[] = { N1 };
11149 return getNode(Opcode, DL, VTList, Ops);
11150}
11151
11152SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11153 SDValue N1, SDValue N2) {
11154 SDValue Ops[] = { N1, N2 };
11155 return getNode(Opcode, DL, VTList, Ops);
11156}
11157
11158SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11159 SDValue N1, SDValue N2, SDValue N3) {
11160 SDValue Ops[] = { N1, N2, N3 };
11161 return getNode(Opcode, DL, VTList, Ops);
11162}
11163
11164SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11165 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11166 SDValue Ops[] = { N1, N2, N3, N4 };
11167 return getNode(Opcode, DL, VTList, Ops);
11168}
11169
11170SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11171 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11172 SDValue N5) {
11173 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11174 return getNode(Opcode, DL, VTList, Ops);
11175}
11176
11178 if (!VT.isExtended())
11179 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11180
11181 return makeVTList(&(*EVTs.insert(VT).first), 1);
11182}
11183
11186 ID.AddInteger(2U);
11187 ID.AddInteger(VT1.getRawBits());
11188 ID.AddInteger(VT2.getRawBits());
11189
11190 void *IP = nullptr;
11191 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11192 if (!Result) {
11193 EVT *Array = Allocator.Allocate<EVT>(2);
11194 Array[0] = VT1;
11195 Array[1] = VT2;
11196 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11197 VTListMap.InsertNode(Result, IP);
11198 }
11199 return Result->getSDVTList();
11200}
11201
11204 ID.AddInteger(3U);
11205 ID.AddInteger(VT1.getRawBits());
11206 ID.AddInteger(VT2.getRawBits());
11207 ID.AddInteger(VT3.getRawBits());
11208
11209 void *IP = nullptr;
11210 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11211 if (!Result) {
11212 EVT *Array = Allocator.Allocate<EVT>(3);
11213 Array[0] = VT1;
11214 Array[1] = VT2;
11215 Array[2] = VT3;
11216 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11217 VTListMap.InsertNode(Result, IP);
11218 }
11219 return Result->getSDVTList();
11220}
11221
11224 ID.AddInteger(4U);
11225 ID.AddInteger(VT1.getRawBits());
11226 ID.AddInteger(VT2.getRawBits());
11227 ID.AddInteger(VT3.getRawBits());
11228 ID.AddInteger(VT4.getRawBits());
11229
11230 void *IP = nullptr;
11231 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11232 if (!Result) {
11233 EVT *Array = Allocator.Allocate<EVT>(4);
11234 Array[0] = VT1;
11235 Array[1] = VT2;
11236 Array[2] = VT3;
11237 Array[3] = VT4;
11238 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11239 VTListMap.InsertNode(Result, IP);
11240 }
11241 return Result->getSDVTList();
11242}
11243
11245 unsigned NumVTs = VTs.size();
11247 ID.AddInteger(NumVTs);
11248 for (unsigned index = 0; index < NumVTs; index++) {
11249 ID.AddInteger(VTs[index].getRawBits());
11250 }
11251
11252 void *IP = nullptr;
11253 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11254 if (!Result) {
11255 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11256 llvm::copy(VTs, Array);
11257 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11258 VTListMap.InsertNode(Result, IP);
11259 }
11260 return Result->getSDVTList();
11261}
11262
11263
11264/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11265/// specified operands. If the resultant node already exists in the DAG,
11266/// this does not modify the specified node, instead it returns the node that
11267/// already exists. If the resultant node does not exist in the DAG, the
11268/// input node is returned. As a degenerate case, if you specify the same
11269/// input operands as the node already has, the input node is returned.
11271 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11272
11273 // Check to see if there is no change.
11274 if (Op == N->getOperand(0)) return N;
11275
11276 // See if the modified node already exists.
11277 void *InsertPos = nullptr;
11278 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11279 return Existing;
11280
11281 // Nope it doesn't. Remove the node from its current place in the maps.
11282 if (InsertPos)
11283 if (!RemoveNodeFromCSEMaps(N))
11284 InsertPos = nullptr;
11285
11286 // Now we update the operands.
11287 N->OperandList[0].set(Op);
11288
11290 // If this gets put into a CSE map, add it.
11291 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11292 return N;
11293}
11294
11296 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11297
11298 // Check to see if there is no change.
11299 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11300 return N; // No operands changed, just return the input node.
11301
11302 // See if the modified node already exists.
11303 void *InsertPos = nullptr;
11304 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11305 return Existing;
11306
11307 // Nope it doesn't. Remove the node from its current place in the maps.
11308 if (InsertPos)
11309 if (!RemoveNodeFromCSEMaps(N))
11310 InsertPos = nullptr;
11311
11312 // Now we update the operands.
11313 if (N->OperandList[0] != Op1)
11314 N->OperandList[0].set(Op1);
11315 if (N->OperandList[1] != Op2)
11316 N->OperandList[1].set(Op2);
11317
11319 // If this gets put into a CSE map, add it.
11320 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11321 return N;
11322}
11323
11326 SDValue Ops[] = { Op1, Op2, Op3 };
11327 return UpdateNodeOperands(N, Ops);
11328}
11329
11332 SDValue Op3, SDValue Op4) {
11333 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11334 return UpdateNodeOperands(N, Ops);
11335}
11336
11339 SDValue Op3, SDValue Op4, SDValue Op5) {
11340 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11341 return UpdateNodeOperands(N, Ops);
11342}
11343
11346 unsigned NumOps = Ops.size();
11347 assert(N->getNumOperands() == NumOps &&
11348 "Update with wrong number of operands");
11349
11350 // If no operands changed just return the input node.
11351 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11352 return N;
11353
11354 // See if the modified node already exists.
11355 void *InsertPos = nullptr;
11356 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11357 return Existing;
11358
11359 // Nope it doesn't. Remove the node from its current place in the maps.
11360 if (InsertPos)
11361 if (!RemoveNodeFromCSEMaps(N))
11362 InsertPos = nullptr;
11363
11364 // Now we update the operands.
11365 for (unsigned i = 0; i != NumOps; ++i)
11366 if (N->OperandList[i] != Ops[i])
11367 N->OperandList[i].set(Ops[i]);
11368
11370 // If this gets put into a CSE map, add it.
11371 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11372 return N;
11373}
11374
11375/// DropOperands - Release the operands and set this node to have
11376/// zero operands.
11378 // Unlike the code in MorphNodeTo that does this, we don't need to
11379 // watch for dead nodes here.
11380 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11381 SDUse &Use = *I++;
11382 Use.set(SDValue());
11383 }
11384}
11385
11387 ArrayRef<MachineMemOperand *> NewMemRefs) {
11388 if (NewMemRefs.empty()) {
11389 N->clearMemRefs();
11390 return;
11391 }
11392
11393 // Check if we can avoid allocating by storing a single reference directly.
11394 if (NewMemRefs.size() == 1) {
11395 N->MemRefs = NewMemRefs[0];
11396 N->NumMemRefs = 1;
11397 return;
11398 }
11399
11400 MachineMemOperand **MemRefsBuffer =
11401 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11402 llvm::copy(NewMemRefs, MemRefsBuffer);
11403 N->MemRefs = MemRefsBuffer;
11404 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11405}
11406
11407/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11408/// machine opcode.
11409///
11411 EVT VT) {
11412 SDVTList VTs = getVTList(VT);
11413 return SelectNodeTo(N, MachineOpc, VTs, {});
11414}
11415
11417 EVT VT, SDValue Op1) {
11418 SDVTList VTs = getVTList(VT);
11419 SDValue Ops[] = { Op1 };
11420 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11421}
11422
11424 EVT VT, SDValue Op1,
11425 SDValue Op2) {
11426 SDVTList VTs = getVTList(VT);
11427 SDValue Ops[] = { Op1, Op2 };
11428 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11429}
11430
11432 EVT VT, SDValue Op1,
11433 SDValue Op2, SDValue Op3) {
11434 SDVTList VTs = getVTList(VT);
11435 SDValue Ops[] = { Op1, Op2, Op3 };
11436 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11437}
11438
11441 SDVTList VTs = getVTList(VT);
11442 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11443}
11444
11446 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11447 SDVTList VTs = getVTList(VT1, VT2);
11448 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11449}
11450
11452 EVT VT1, EVT VT2) {
11453 SDVTList VTs = getVTList(VT1, VT2);
11454 return SelectNodeTo(N, MachineOpc, VTs, {});
11455}
11456
11458 EVT VT1, EVT VT2, EVT VT3,
11460 SDVTList VTs = getVTList(VT1, VT2, VT3);
11461 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11462}
11463
11465 EVT VT1, EVT VT2,
11466 SDValue Op1, SDValue Op2) {
11467 SDVTList VTs = getVTList(VT1, VT2);
11468 SDValue Ops[] = { Op1, Op2 };
11469 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11470}
11471
11474 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11475 // Reset the NodeID to -1.
11476 New->setNodeId(-1);
11477 if (New != N) {
11478 ReplaceAllUsesWith(N, New);
11480 }
11481 return New;
11482}
11483
11484/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11485/// the line number information on the merged node since it is not possible to
11486/// preserve the information that operation is associated with multiple lines.
11487/// This will make the debugger working better at -O0, were there is a higher
11488/// probability having other instructions associated with that line.
11489///
11490/// For IROrder, we keep the smaller of the two
11491SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11492 DebugLoc NLoc = N->getDebugLoc();
11493 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11494 N->setDebugLoc(DebugLoc());
11495 }
11496 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11497 N->setIROrder(Order);
11498 return N;
11499}
11500
11501/// MorphNodeTo - This *mutates* the specified node to have the specified
11502/// return type, opcode, and operands.
11503///
11504/// Note that MorphNodeTo returns the resultant node. If there is already a
11505/// node of the specified opcode and operands, it returns that node instead of
11506/// the current one. Note that the SDLoc need not be the same.
11507///
11508/// Using MorphNodeTo is faster than creating a new node and swapping it in
11509/// with ReplaceAllUsesWith both because it often avoids allocating a new
11510/// node, and because it doesn't require CSE recalculation for any of
11511/// the node's users.
11512///
11513/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11514/// As a consequence it isn't appropriate to use from within the DAG combiner or
11515/// the legalizer which maintain worklists that would need to be updated when
11516/// deleting things.
11519 // If an identical node already exists, use it.
11520 void *IP = nullptr;
11521 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11523 AddNodeIDNode(ID, Opc, VTs, Ops);
11524 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11525 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11526 }
11527
11528 if (!RemoveNodeFromCSEMaps(N))
11529 IP = nullptr;
11530
11531 // Start the morphing.
11532 N->NodeType = Opc;
11533 N->ValueList = VTs.VTs;
11534 N->NumValues = VTs.NumVTs;
11535
11536 // Clear the operands list, updating used nodes to remove this from their
11537 // use list. Keep track of any operands that become dead as a result.
11538 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11539 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11540 SDUse &Use = *I++;
11541 SDNode *Used = Use.getNode();
11542 Use.set(SDValue());
11543 if (Used->use_empty())
11544 DeadNodeSet.insert(Used);
11545 }
11546
11547 // For MachineNode, initialize the memory references information.
11549 MN->clearMemRefs();
11550
11551 // Swap for an appropriately sized array from the recycler.
11552 removeOperands(N);
11553 createOperands(N, Ops);
11554
11555 // Delete any nodes that are still dead after adding the uses for the
11556 // new operands.
11557 if (!DeadNodeSet.empty()) {
11558 SmallVector<SDNode *, 16> DeadNodes;
11559 for (SDNode *N : DeadNodeSet)
11560 if (N->use_empty())
11561 DeadNodes.push_back(N);
11562 RemoveDeadNodes(DeadNodes);
11563 }
11564
11565 if (IP)
11566 CSEMap.InsertNode(N, IP); // Memoize the new node.
11567 return N;
11568}
11569
11571 unsigned OrigOpc = Node->getOpcode();
11572 unsigned NewOpc;
11573 switch (OrigOpc) {
11574 default:
11575 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11576#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11577 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11578#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11579 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11580#include "llvm/IR/ConstrainedOps.def"
11581 }
11582
11583 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11584
11585 // We're taking this node out of the chain, so we need to re-link things.
11586 SDValue InputChain = Node->getOperand(0);
11587 SDValue OutputChain = SDValue(Node, 1);
11588 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11589
11591 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11592 Ops.push_back(Node->getOperand(i));
11593
11594 SDVTList VTs = getVTList(Node->getValueType(0));
11595 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11596
11597 // MorphNodeTo can operate in two ways: if an existing node with the
11598 // specified operands exists, it can just return it. Otherwise, it
11599 // updates the node in place to have the requested operands.
11600 if (Res == Node) {
11601 // If we updated the node in place, reset the node ID. To the isel,
11602 // this should be just like a newly allocated machine node.
11603 Res->setNodeId(-1);
11604 } else {
11607 }
11608
11609 return Res;
11610}
11611
11612/// getMachineNode - These are used for target selectors to create a new node
11613/// with specified return type(s), MachineInstr opcode, and operands.
11614///
11615/// Note that getMachineNode returns the resultant node. If there is already a
11616/// node of the specified opcode and operands, it returns that node instead of
11617/// the current one.
11619 EVT VT) {
11620 SDVTList VTs = getVTList(VT);
11621 return getMachineNode(Opcode, dl, VTs, {});
11622}
11623
11625 EVT VT, SDValue Op1) {
11626 SDVTList VTs = getVTList(VT);
11627 SDValue Ops[] = { Op1 };
11628 return getMachineNode(Opcode, dl, VTs, Ops);
11629}
11630
11632 EVT VT, SDValue Op1, SDValue Op2) {
11633 SDVTList VTs = getVTList(VT);
11634 SDValue Ops[] = { Op1, Op2 };
11635 return getMachineNode(Opcode, dl, VTs, Ops);
11636}
11637
11639 EVT VT, SDValue Op1, SDValue Op2,
11640 SDValue Op3) {
11641 SDVTList VTs = getVTList(VT);
11642 SDValue Ops[] = { Op1, Op2, Op3 };
11643 return getMachineNode(Opcode, dl, VTs, Ops);
11644}
11645
11648 SDVTList VTs = getVTList(VT);
11649 return getMachineNode(Opcode, dl, VTs, Ops);
11650}
11651
11653 EVT VT1, EVT VT2, SDValue Op1,
11654 SDValue Op2) {
11655 SDVTList VTs = getVTList(VT1, VT2);
11656 SDValue Ops[] = { Op1, Op2 };
11657 return getMachineNode(Opcode, dl, VTs, Ops);
11658}
11659
11661 EVT VT1, EVT VT2, SDValue Op1,
11662 SDValue Op2, SDValue Op3) {
11663 SDVTList VTs = getVTList(VT1, VT2);
11664 SDValue Ops[] = { Op1, Op2, Op3 };
11665 return getMachineNode(Opcode, dl, VTs, Ops);
11666}
11667
11669 EVT VT1, EVT VT2,
11671 SDVTList VTs = getVTList(VT1, VT2);
11672 return getMachineNode(Opcode, dl, VTs, Ops);
11673}
11674
11676 EVT VT1, EVT VT2, EVT VT3,
11677 SDValue Op1, SDValue Op2) {
11678 SDVTList VTs = getVTList(VT1, VT2, VT3);
11679 SDValue Ops[] = { Op1, Op2 };
11680 return getMachineNode(Opcode, dl, VTs, Ops);
11681}
11682
11684 EVT VT1, EVT VT2, EVT VT3,
11685 SDValue Op1, SDValue Op2,
11686 SDValue Op3) {
11687 SDVTList VTs = getVTList(VT1, VT2, VT3);
11688 SDValue Ops[] = { Op1, Op2, Op3 };
11689 return getMachineNode(Opcode, dl, VTs, Ops);
11690}
11691
11693 EVT VT1, EVT VT2, EVT VT3,
11695 SDVTList VTs = getVTList(VT1, VT2, VT3);
11696 return getMachineNode(Opcode, dl, VTs, Ops);
11697}
11698
11700 ArrayRef<EVT> ResultTys,
11702 SDVTList VTs = getVTList(ResultTys);
11703 return getMachineNode(Opcode, dl, VTs, Ops);
11704}
11705
11707 SDVTList VTs,
11709 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11711 void *IP = nullptr;
11712
11713 if (DoCSE) {
11715 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11716 IP = nullptr;
11717 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11718 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11719 }
11720 }
11721
11722 // Allocate a new MachineSDNode.
11723 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11724 createOperands(N, Ops);
11725
11726 if (DoCSE)
11727 CSEMap.InsertNode(N, IP);
11728
11729 InsertNode(N);
11730 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11731 return N;
11732}
11733
11734/// getTargetExtractSubreg - A convenience function for creating
11735/// TargetOpcode::EXTRACT_SUBREG nodes.
11737 SDValue Operand) {
11738 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11739 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11740 VT, Operand, SRIdxVal);
11741 return SDValue(Subreg, 0);
11742}
11743
11744/// getTargetInsertSubreg - A convenience function for creating
11745/// TargetOpcode::INSERT_SUBREG nodes.
11747 SDValue Operand, SDValue Subreg) {
11748 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11749 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11750 VT, Operand, Subreg, SRIdxVal);
11751 return SDValue(Result, 0);
11752}
11753
11754/// getNodeIfExists - Get the specified node if it's already available, or
11755/// else return NULL.
11758 bool AllowCommute) {
11759 SDNodeFlags Flags;
11760 if (Inserter)
11761 Flags = Inserter->getFlags();
11762 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11763}
11764
11767 const SDNodeFlags Flags,
11768 bool AllowCommute) {
11769 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11770 return nullptr;
11771
11772 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11774 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11775 void *IP = nullptr;
11776 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11777 E->intersectFlagsWith(Flags);
11778 return E;
11779 }
11780 return nullptr;
11781 };
11782
11783 if (SDNode *Existing = Lookup(Ops))
11784 return Existing;
11785
11786 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11787 return Lookup({Ops[1], Ops[0]});
11788
11789 return nullptr;
11790}
11791
11792/// doesNodeExist - Check if a node exists without modifying its flags.
11793bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11795 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11797 AddNodeIDNode(ID, Opcode, VTList, Ops);
11798 void *IP = nullptr;
11799 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11800 return true;
11801 }
11802 return false;
11803}
11804
11805/// getDbgValue - Creates a SDDbgValue node.
11806///
11807/// SDNode
11809 SDNode *N, unsigned R, bool IsIndirect,
11810 const DebugLoc &DL, unsigned O) {
11811 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11812 "Expected inlined-at fields to agree");
11813 return new (DbgInfo->getAlloc())
11814 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11815 {}, IsIndirect, DL, O,
11816 /*IsVariadic=*/false);
11817}
11818
11819/// Constant
11821 DIExpression *Expr,
11822 const Value *C,
11823 const DebugLoc &DL, unsigned O) {
11824 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11825 "Expected inlined-at fields to agree");
11826 return new (DbgInfo->getAlloc())
11827 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11828 /*IsIndirect=*/false, DL, O,
11829 /*IsVariadic=*/false);
11830}
11831
11832/// FrameIndex
11834 DIExpression *Expr, unsigned FI,
11835 bool IsIndirect,
11836 const DebugLoc &DL,
11837 unsigned O) {
11838 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11839 "Expected inlined-at fields to agree");
11840 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11841}
11842
11843/// FrameIndex with dependencies
11845 DIExpression *Expr, unsigned FI,
11846 ArrayRef<SDNode *> Dependencies,
11847 bool IsIndirect,
11848 const DebugLoc &DL,
11849 unsigned O) {
11850 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11851 "Expected inlined-at fields to agree");
11852 return new (DbgInfo->getAlloc())
11853 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11854 Dependencies, IsIndirect, DL, O,
11855 /*IsVariadic=*/false);
11856}
11857
11858/// VReg
11860 Register VReg, bool IsIndirect,
11861 const DebugLoc &DL, unsigned O) {
11862 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11863 "Expected inlined-at fields to agree");
11864 return new (DbgInfo->getAlloc())
11865 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11866 {}, IsIndirect, DL, O,
11867 /*IsVariadic=*/false);
11868}
11869
11872 ArrayRef<SDNode *> Dependencies,
11873 bool IsIndirect, const DebugLoc &DL,
11874 unsigned O, bool IsVariadic) {
11875 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11876 "Expected inlined-at fields to agree");
11877 return new (DbgInfo->getAlloc())
11878 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11879 DL, O, IsVariadic);
11880}
11881
11883 unsigned OffsetInBits, unsigned SizeInBits,
11884 bool InvalidateDbg) {
11885 SDNode *FromNode = From.getNode();
11886 SDNode *ToNode = To.getNode();
11887 assert(FromNode && ToNode && "Can't modify dbg values");
11888
11889 // PR35338
11890 // TODO: assert(From != To && "Redundant dbg value transfer");
11891 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11892 if (From == To || FromNode == ToNode)
11893 return;
11894
11895 if (!FromNode->getHasDebugValue())
11896 return;
11897
11898 SDDbgOperand FromLocOp =
11899 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11901
11903 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11904 if (Dbg->isInvalidated())
11905 continue;
11906
11907 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11908
11909 // Create a new location ops vector that is equal to the old vector, but
11910 // with each instance of FromLocOp replaced with ToLocOp.
11911 bool Changed = false;
11912 auto NewLocOps = Dbg->copyLocationOps();
11913 std::replace_if(
11914 NewLocOps.begin(), NewLocOps.end(),
11915 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11916 bool Match = Op == FromLocOp;
11917 Changed |= Match;
11918 return Match;
11919 },
11920 ToLocOp);
11921 // Ignore this SDDbgValue if we didn't find a matching location.
11922 if (!Changed)
11923 continue;
11924
11925 DIVariable *Var = Dbg->getVariable();
11926 auto *Expr = Dbg->getExpression();
11927 // If a fragment is requested, update the expression.
11928 if (SizeInBits) {
11929 // When splitting a larger (e.g., sign-extended) value whose
11930 // lower bits are described with an SDDbgValue, do not attempt
11931 // to transfer the SDDbgValue to the upper bits.
11932 if (auto FI = Expr->getFragmentInfo())
11933 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11934 continue;
11935 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11936 SizeInBits);
11937 if (!Fragment)
11938 continue;
11939 Expr = *Fragment;
11940 }
11941
11942 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11943 // Clone the SDDbgValue and move it to To.
11944 SDDbgValue *Clone = getDbgValueList(
11945 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11946 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11947 Dbg->isVariadic());
11948 ClonedDVs.push_back(Clone);
11949
11950 if (InvalidateDbg) {
11951 // Invalidate value and indicate the SDDbgValue should not be emitted.
11952 Dbg->setIsInvalidated();
11953 Dbg->setIsEmitted();
11954 }
11955 }
11956
11957 for (SDDbgValue *Dbg : ClonedDVs) {
11958 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11959 "Transferred DbgValues should depend on the new SDNode");
11960 AddDbgValue(Dbg, false);
11961 }
11962}
11963
11965 if (!N.getHasDebugValue())
11966 return;
11967
11968 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11969 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11970 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11971 return SDDbgOperand::fromNode(Node, ResNo);
11972 };
11973
11975 for (auto *DV : GetDbgValues(&N)) {
11976 if (DV->isInvalidated())
11977 continue;
11978 switch (N.getOpcode()) {
11979 default:
11980 break;
11981 case ISD::ADD: {
11982 SDValue N0 = N.getOperand(0);
11983 SDValue N1 = N.getOperand(1);
11984 if (!isa<ConstantSDNode>(N0)) {
11985 bool RHSConstant = isa<ConstantSDNode>(N1);
11987 if (RHSConstant)
11988 Offset = N.getConstantOperandVal(1);
11989 // We are not allowed to turn indirect debug values variadic, so
11990 // don't salvage those.
11991 if (!RHSConstant && DV->isIndirect())
11992 continue;
11993
11994 // Rewrite an ADD constant node into a DIExpression. Since we are
11995 // performing arithmetic to compute the variable's *value* in the
11996 // DIExpression, we need to mark the expression with a
11997 // DW_OP_stack_value.
11998 auto *DIExpr = DV->getExpression();
11999 auto NewLocOps = DV->copyLocationOps();
12000 bool Changed = false;
12001 size_t OrigLocOpsSize = NewLocOps.size();
12002 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12003 // We're not given a ResNo to compare against because the whole
12004 // node is going away. We know that any ISD::ADD only has one
12005 // result, so we can assume any node match is using the result.
12006 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12007 NewLocOps[i].getSDNode() != &N)
12008 continue;
12009 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12010 if (RHSConstant) {
12013 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12014 } else {
12015 // Convert to a variadic expression (if not already).
12016 // convertToVariadicExpression() returns a const pointer, so we use
12017 // a temporary const variable here.
12018 const auto *TmpDIExpr =
12022 ExprOps.push_back(NewLocOps.size());
12023 ExprOps.push_back(dwarf::DW_OP_plus);
12024 SDDbgOperand RHS =
12026 NewLocOps.push_back(RHS);
12027 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12028 }
12029 Changed = true;
12030 }
12031 (void)Changed;
12032 assert(Changed && "Salvage target doesn't use N");
12033
12034 bool IsVariadic =
12035 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12036
12037 auto AdditionalDependencies = DV->getAdditionalDependencies();
12038 SDDbgValue *Clone = getDbgValueList(
12039 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12040 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12041 ClonedDVs.push_back(Clone);
12042 DV->setIsInvalidated();
12043 DV->setIsEmitted();
12044 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12045 N0.getNode()->dumprFull(this);
12046 dbgs() << " into " << *DIExpr << '\n');
12047 }
12048 break;
12049 }
12050 case ISD::TRUNCATE: {
12051 SDValue N0 = N.getOperand(0);
12052 TypeSize FromSize = N0.getValueSizeInBits();
12053 TypeSize ToSize = N.getValueSizeInBits(0);
12054
12055 DIExpression *DbgExpression = DV->getExpression();
12056 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12057 auto NewLocOps = DV->copyLocationOps();
12058 bool Changed = false;
12059 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12060 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12061 NewLocOps[i].getSDNode() != &N)
12062 continue;
12063
12064 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12065 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12066 Changed = true;
12067 }
12068 assert(Changed && "Salvage target doesn't use N");
12069 (void)Changed;
12070
12071 SDDbgValue *Clone =
12072 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12073 DV->getAdditionalDependencies(), DV->isIndirect(),
12074 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12075
12076 ClonedDVs.push_back(Clone);
12077 DV->setIsInvalidated();
12078 DV->setIsEmitted();
12079 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12080 dbgs() << " into " << *DbgExpression << '\n');
12081 break;
12082 }
12083 }
12084 }
12085
12086 for (SDDbgValue *Dbg : ClonedDVs) {
12087 assert((!Dbg->getSDNodes().empty() ||
12088 llvm::any_of(Dbg->getLocationOps(),
12089 [&](const SDDbgOperand &Op) {
12090 return Op.getKind() == SDDbgOperand::FRAMEIX;
12091 })) &&
12092 "Salvaged DbgValue should depend on a new SDNode");
12093 AddDbgValue(Dbg, false);
12094 }
12095}
12096
12097/// Creates a SDDbgLabel node.
12099 const DebugLoc &DL, unsigned O) {
12100 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12101 "Expected inlined-at fields to agree");
12102 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12103}
12104
12105namespace {
12106
12107/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12108/// pointed to by a use iterator is deleted, increment the use iterator
12109/// so that it doesn't dangle.
12110///
12111class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12114
12115 void NodeDeleted(SDNode *N, SDNode *E) override {
12116 // Increment the iterator as needed.
12117 while (UI != UE && N == UI->getUser())
12118 ++UI;
12119 }
12120
12121public:
12122 RAUWUpdateListener(SelectionDAG &d,
12125 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12126};
12127
12128} // end anonymous namespace
12129
12130/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12131/// This can cause recursive merging of nodes in the DAG.
12132///
12133/// This version assumes From has a single result value.
12134///
12136 SDNode *From = FromN.getNode();
12137 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12138 "Cannot replace with this method!");
12139 assert(From != To.getNode() && "Cannot replace uses of with self");
12140
12141 // Preserve Debug Values
12142 transferDbgValues(FromN, To);
12143 // Preserve extra info.
12144 copyExtraInfo(From, To.getNode());
12145
12146 // Iterate over all the existing uses of From. New uses will be added
12147 // to the beginning of the use list, which we avoid visiting.
12148 // This specifically avoids visiting uses of From that arise while the
12149 // replacement is happening, because any such uses would be the result
12150 // of CSE: If an existing node looks like From after one of its operands
12151 // is replaced by To, we don't want to replace of all its users with To
12152 // too. See PR3018 for more info.
12153 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12154 RAUWUpdateListener Listener(*this, UI, UE);
12155 while (UI != UE) {
12156 SDNode *User = UI->getUser();
12157
12158 // This node is about to morph, remove its old self from the CSE maps.
12159 RemoveNodeFromCSEMaps(User);
12160
12161 // A user can appear in a use list multiple times, and when this
12162 // happens the uses are usually next to each other in the list.
12163 // To help reduce the number of CSE recomputations, process all
12164 // the uses of this user that we can find this way.
12165 do {
12166 SDUse &Use = *UI;
12167 ++UI;
12168 Use.set(To);
12169 if (To->isDivergent() != From->isDivergent())
12171 } while (UI != UE && UI->getUser() == User);
12172 // Now that we have modified User, add it back to the CSE maps. If it
12173 // already exists there, recursively merge the results together.
12174 AddModifiedNodeToCSEMaps(User);
12175 }
12176
12177 // If we just RAUW'd the root, take note.
12178 if (FromN == getRoot())
12179 setRoot(To);
12180}
12181
12182/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12183/// This can cause recursive merging of nodes in the DAG.
12184///
12185/// This version assumes that for each value of From, there is a
12186/// corresponding value in To in the same position with the same type.
12187///
12189#ifndef NDEBUG
12190 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12191 assert((!From->hasAnyUseOfValue(i) ||
12192 From->getValueType(i) == To->getValueType(i)) &&
12193 "Cannot use this version of ReplaceAllUsesWith!");
12194#endif
12195
12196 // Handle the trivial case.
12197 if (From == To)
12198 return;
12199
12200 // Preserve Debug Info. Only do this if there's a use.
12201 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12202 if (From->hasAnyUseOfValue(i)) {
12203 assert((i < To->getNumValues()) && "Invalid To location");
12204 transferDbgValues(SDValue(From, i), SDValue(To, i));
12205 }
12206 // Preserve extra info.
12207 copyExtraInfo(From, To);
12208
12209 // Iterate over just the existing users of From. See the comments in
12210 // the ReplaceAllUsesWith above.
12211 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12212 RAUWUpdateListener Listener(*this, UI, UE);
12213 while (UI != UE) {
12214 SDNode *User = UI->getUser();
12215
12216 // This node is about to morph, remove its old self from the CSE maps.
12217 RemoveNodeFromCSEMaps(User);
12218
12219 // A user can appear in a use list multiple times, and when this
12220 // happens the uses are usually next to each other in the list.
12221 // To help reduce the number of CSE recomputations, process all
12222 // the uses of this user that we can find this way.
12223 do {
12224 SDUse &Use = *UI;
12225 ++UI;
12226 Use.setNode(To);
12227 if (To->isDivergent() != From->isDivergent())
12229 } while (UI != UE && UI->getUser() == User);
12230
12231 // Now that we have modified User, add it back to the CSE maps. If it
12232 // already exists there, recursively merge the results together.
12233 AddModifiedNodeToCSEMaps(User);
12234 }
12235
12236 // If we just RAUW'd the root, take note.
12237 if (From == getRoot().getNode())
12238 setRoot(SDValue(To, getRoot().getResNo()));
12239}
12240
12241/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12242/// This can cause recursive merging of nodes in the DAG.
12243///
12244/// This version can replace From with any result values. To must match the
12245/// number and types of values returned by From.
12247 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12248 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12249
12250 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12251 // Preserve Debug Info.
12252 transferDbgValues(SDValue(From, i), To[i]);
12253 // Preserve extra info.
12254 copyExtraInfo(From, To[i].getNode());
12255 }
12256
12257 // Iterate over just the existing users of From. See the comments in
12258 // the ReplaceAllUsesWith above.
12259 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12260 RAUWUpdateListener Listener(*this, UI, UE);
12261 while (UI != UE) {
12262 SDNode *User = UI->getUser();
12263
12264 // This node is about to morph, remove its old self from the CSE maps.
12265 RemoveNodeFromCSEMaps(User);
12266
12267 // A user can appear in a use list multiple times, and when this happens the
12268 // uses are usually next to each other in the list. To help reduce the
12269 // number of CSE and divergence recomputations, process all the uses of this
12270 // user that we can find this way.
12271 bool To_IsDivergent = false;
12272 do {
12273 SDUse &Use = *UI;
12274 const SDValue &ToOp = To[Use.getResNo()];
12275 ++UI;
12276 Use.set(ToOp);
12277 To_IsDivergent |= ToOp->isDivergent();
12278 } while (UI != UE && UI->getUser() == User);
12279
12280 if (To_IsDivergent != From->isDivergent())
12282
12283 // Now that we have modified User, add it back to the CSE maps. If it
12284 // already exists there, recursively merge the results together.
12285 AddModifiedNodeToCSEMaps(User);
12286 }
12287
12288 // If we just RAUW'd the root, take note.
12289 if (From == getRoot().getNode())
12290 setRoot(SDValue(To[getRoot().getResNo()]));
12291}
12292
12293/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12294/// uses of other values produced by From.getNode() alone. The Deleted
12295/// vector is handled the same way as for ReplaceAllUsesWith.
12297 // Handle the really simple, really trivial case efficiently.
12298 if (From == To) return;
12299
12300 // Handle the simple, trivial, case efficiently.
12301 if (From.getNode()->getNumValues() == 1) {
12302 ReplaceAllUsesWith(From, To);
12303 return;
12304 }
12305
12306 // Preserve Debug Info.
12307 transferDbgValues(From, To);
12308 copyExtraInfo(From.getNode(), To.getNode());
12309
12310 // Iterate over just the existing users of From. See the comments in
12311 // the ReplaceAllUsesWith above.
12312 SDNode::use_iterator UI = From.getNode()->use_begin(),
12313 UE = From.getNode()->use_end();
12314 RAUWUpdateListener Listener(*this, UI, UE);
12315 while (UI != UE) {
12316 SDNode *User = UI->getUser();
12317 bool UserRemovedFromCSEMaps = false;
12318
12319 // A user can appear in a use list multiple times, and when this
12320 // happens the uses are usually next to each other in the list.
12321 // To help reduce the number of CSE recomputations, process all
12322 // the uses of this user that we can find this way.
12323 do {
12324 SDUse &Use = *UI;
12325
12326 // Skip uses of different values from the same node.
12327 if (Use.getResNo() != From.getResNo()) {
12328 ++UI;
12329 continue;
12330 }
12331
12332 // If this node hasn't been modified yet, it's still in the CSE maps,
12333 // so remove its old self from the CSE maps.
12334 if (!UserRemovedFromCSEMaps) {
12335 RemoveNodeFromCSEMaps(User);
12336 UserRemovedFromCSEMaps = true;
12337 }
12338
12339 ++UI;
12340 Use.set(To);
12341 if (To->isDivergent() != From->isDivergent())
12343 } while (UI != UE && UI->getUser() == User);
12344 // We are iterating over all uses of the From node, so if a use
12345 // doesn't use the specific value, no changes are made.
12346 if (!UserRemovedFromCSEMaps)
12347 continue;
12348
12349 // Now that we have modified User, add it back to the CSE maps. If it
12350 // already exists there, recursively merge the results together.
12351 AddModifiedNodeToCSEMaps(User);
12352 }
12353
12354 // If we just RAUW'd the root, take note.
12355 if (From == getRoot())
12356 setRoot(To);
12357}
12358
12359namespace {
12360
12361/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12362/// to record information about a use.
12363struct UseMemo {
12364 SDNode *User;
12365 unsigned Index;
12366 SDUse *Use;
12367};
12368
12369/// operator< - Sort Memos by User.
12370bool operator<(const UseMemo &L, const UseMemo &R) {
12371 return (intptr_t)L.User < (intptr_t)R.User;
12372}
12373
12374/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12375/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12376/// the node already has been taken care of recursively.
12377class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12378 SmallVectorImpl<UseMemo> &Uses;
12379
12380 void NodeDeleted(SDNode *N, SDNode *E) override {
12381 for (UseMemo &Memo : Uses)
12382 if (Memo.User == N)
12383 Memo.User = nullptr;
12384 }
12385
12386public:
12387 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12388 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12389};
12390
12391} // end anonymous namespace
12392
12393/// Return true if a glue output should propagate divergence information.
12395 switch (Node->getOpcode()) {
12396 case ISD::CopyFromReg:
12397 case ISD::CopyToReg:
12398 return false;
12399 default:
12400 return true;
12401 }
12402
12403 llvm_unreachable("covered opcode switch");
12404}
12405
12407 if (TLI->isSDNodeAlwaysUniform(N)) {
12408 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12409 "Conflicting divergence information!");
12410 return false;
12411 }
12412 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12413 return true;
12414 for (const auto &Op : N->ops()) {
12415 EVT VT = Op.getValueType();
12416
12417 // Skip Chain. It does not carry divergence.
12418 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12419 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12420 return true;
12421 }
12422 return false;
12423}
12424
12426 SmallVector<SDNode *, 16> Worklist(1, N);
12427 do {
12428 N = Worklist.pop_back_val();
12429 bool IsDivergent = calculateDivergence(N);
12430 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12431 N->SDNodeBits.IsDivergent = IsDivergent;
12432 llvm::append_range(Worklist, N->users());
12433 }
12434 } while (!Worklist.empty());
12435}
12436
12437void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12439 Order.reserve(AllNodes.size());
12440 for (auto &N : allnodes()) {
12441 unsigned NOps = N.getNumOperands();
12442 Degree[&N] = NOps;
12443 if (0 == NOps)
12444 Order.push_back(&N);
12445 }
12446 for (size_t I = 0; I != Order.size(); ++I) {
12447 SDNode *N = Order[I];
12448 for (auto *U : N->users()) {
12449 unsigned &UnsortedOps = Degree[U];
12450 if (0 == --UnsortedOps)
12451 Order.push_back(U);
12452 }
12453 }
12454}
12455
12456#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12457void SelectionDAG::VerifyDAGDivergence() {
12458 std::vector<SDNode *> TopoOrder;
12459 CreateTopologicalOrder(TopoOrder);
12460 for (auto *N : TopoOrder) {
12461 assert(calculateDivergence(N) == N->isDivergent() &&
12462 "Divergence bit inconsistency detected");
12463 }
12464}
12465#endif
12466
12467/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12468/// uses of other values produced by From.getNode() alone. The same value
12469/// may appear in both the From and To list. The Deleted vector is
12470/// handled the same way as for ReplaceAllUsesWith.
12472 const SDValue *To,
12473 unsigned Num){
12474 // Handle the simple, trivial case efficiently.
12475 if (Num == 1)
12476 return ReplaceAllUsesOfValueWith(*From, *To);
12477
12478 transferDbgValues(*From, *To);
12479 copyExtraInfo(From->getNode(), To->getNode());
12480
12481 // Read up all the uses and make records of them. This helps
12482 // processing new uses that are introduced during the
12483 // replacement process.
12485 for (unsigned i = 0; i != Num; ++i) {
12486 unsigned FromResNo = From[i].getResNo();
12487 SDNode *FromNode = From[i].getNode();
12488 for (SDUse &Use : FromNode->uses()) {
12489 if (Use.getResNo() == FromResNo) {
12490 UseMemo Memo = {Use.getUser(), i, &Use};
12491 Uses.push_back(Memo);
12492 }
12493 }
12494 }
12495
12496 // Sort the uses, so that all the uses from a given User are together.
12498 RAUOVWUpdateListener Listener(*this, Uses);
12499
12500 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12501 UseIndex != UseIndexEnd; ) {
12502 // We know that this user uses some value of From. If it is the right
12503 // value, update it.
12504 SDNode *User = Uses[UseIndex].User;
12505 // If the node has been deleted by recursive CSE updates when updating
12506 // another node, then just skip this entry.
12507 if (User == nullptr) {
12508 ++UseIndex;
12509 continue;
12510 }
12511
12512 // This node is about to morph, remove its old self from the CSE maps.
12513 RemoveNodeFromCSEMaps(User);
12514
12515 // The Uses array is sorted, so all the uses for a given User
12516 // are next to each other in the list.
12517 // To help reduce the number of CSE recomputations, process all
12518 // the uses of this user that we can find this way.
12519 do {
12520 unsigned i = Uses[UseIndex].Index;
12521 SDUse &Use = *Uses[UseIndex].Use;
12522 ++UseIndex;
12523
12524 Use.set(To[i]);
12525 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12526
12527 // Now that we have modified User, add it back to the CSE maps. If it
12528 // already exists there, recursively merge the results together.
12529 AddModifiedNodeToCSEMaps(User);
12530 }
12531}
12532
12533/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12534/// based on their topological order. It returns the maximum id and a vector
12535/// of the SDNodes* in assigned order by reference.
12537 unsigned DAGSize = 0;
12538
12539 // SortedPos tracks the progress of the algorithm. Nodes before it are
12540 // sorted, nodes after it are unsorted. When the algorithm completes
12541 // it is at the end of the list.
12542 allnodes_iterator SortedPos = allnodes_begin();
12543
12544 // Visit all the nodes. Move nodes with no operands to the front of
12545 // the list immediately. Annotate nodes that do have operands with their
12546 // operand count. Before we do this, the Node Id fields of the nodes
12547 // may contain arbitrary values. After, the Node Id fields for nodes
12548 // before SortedPos will contain the topological sort index, and the
12549 // Node Id fields for nodes At SortedPos and after will contain the
12550 // count of outstanding operands.
12552 checkForCycles(&N, this);
12553 unsigned Degree = N.getNumOperands();
12554 if (Degree == 0) {
12555 // A node with no uses, add it to the result array immediately.
12556 N.setNodeId(DAGSize++);
12557 allnodes_iterator Q(&N);
12558 if (Q != SortedPos)
12559 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12560 assert(SortedPos != AllNodes.end() && "Overran node list");
12561 ++SortedPos;
12562 } else {
12563 // Temporarily use the Node Id as scratch space for the degree count.
12564 N.setNodeId(Degree);
12565 }
12566 }
12567
12568 // Visit all the nodes. As we iterate, move nodes into sorted order,
12569 // such that by the time the end is reached all nodes will be sorted.
12570 for (SDNode &Node : allnodes()) {
12571 SDNode *N = &Node;
12572 checkForCycles(N, this);
12573 // N is in sorted position, so all its uses have one less operand
12574 // that needs to be sorted.
12575 for (SDNode *P : N->users()) {
12576 unsigned Degree = P->getNodeId();
12577 assert(Degree != 0 && "Invalid node degree");
12578 --Degree;
12579 if (Degree == 0) {
12580 // All of P's operands are sorted, so P may sorted now.
12581 P->setNodeId(DAGSize++);
12582 if (P->getIterator() != SortedPos)
12583 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12584 assert(SortedPos != AllNodes.end() && "Overran node list");
12585 ++SortedPos;
12586 } else {
12587 // Update P's outstanding operand count.
12588 P->setNodeId(Degree);
12589 }
12590 }
12591 if (Node.getIterator() == SortedPos) {
12592#ifndef NDEBUG
12594 SDNode *S = &*++I;
12595 dbgs() << "Overran sorted position:\n";
12596 S->dumprFull(this); dbgs() << "\n";
12597 dbgs() << "Checking if this is due to cycles\n";
12598 checkForCycles(this, true);
12599#endif
12600 llvm_unreachable(nullptr);
12601 }
12602 }
12603
12604 assert(SortedPos == AllNodes.end() &&
12605 "Topological sort incomplete!");
12606 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12607 "First node in topological sort is not the entry token!");
12608 assert(AllNodes.front().getNodeId() == 0 &&
12609 "First node in topological sort has non-zero id!");
12610 assert(AllNodes.front().getNumOperands() == 0 &&
12611 "First node in topological sort has operands!");
12612 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12613 "Last node in topologic sort has unexpected id!");
12614 assert(AllNodes.back().use_empty() &&
12615 "Last node in topologic sort has users!");
12616 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12617 return DAGSize;
12618}
12619
12621 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12622 SortedNodes.clear();
12623 // Node -> remaining number of outstanding operands.
12624 DenseMap<const SDNode *, unsigned> RemainingOperands;
12625
12626 // Put nodes without any operands into SortedNodes first.
12627 for (const SDNode &N : allnodes()) {
12628 checkForCycles(&N, this);
12629 unsigned NumOperands = N.getNumOperands();
12630 if (NumOperands == 0)
12631 SortedNodes.push_back(&N);
12632 else
12633 // Record their total number of outstanding operands.
12634 RemainingOperands[&N] = NumOperands;
12635 }
12636
12637 // A node is pushed into SortedNodes when all of its operands (predecessors in
12638 // the graph) are also in SortedNodes.
12639 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12640 const SDNode *N = SortedNodes[i];
12641 for (const SDNode *U : N->users()) {
12642 // HandleSDNode is never part of a DAG and therefore has no entry in
12643 // RemainingOperands.
12644 if (U->getOpcode() == ISD::HANDLENODE)
12645 continue;
12646 unsigned &NumRemOperands = RemainingOperands[U];
12647 assert(NumRemOperands && "Invalid number of remaining operands");
12648 --NumRemOperands;
12649 if (!NumRemOperands)
12650 SortedNodes.push_back(U);
12651 }
12652 }
12653
12654 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12655 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12656 "First node in topological sort is not the entry token");
12657 assert(SortedNodes.front()->getNumOperands() == 0 &&
12658 "First node in topological sort has operands");
12659}
12660
12661/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12662/// value is produced by SD.
12663void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12664 for (SDNode *SD : DB->getSDNodes()) {
12665 if (!SD)
12666 continue;
12667 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12668 SD->setHasDebugValue(true);
12669 }
12670 DbgInfo->add(DB, isParameter);
12671}
12672
12673void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12674
12676 SDValue NewMemOpChain) {
12677 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12678 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12679 // The new memory operation must have the same position as the old load in
12680 // terms of memory dependency. Create a TokenFactor for the old load and new
12681 // memory operation and update uses of the old load's output chain to use that
12682 // TokenFactor.
12683 if (OldChain == NewMemOpChain || OldChain.use_empty())
12684 return NewMemOpChain;
12685
12686 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12687 OldChain, NewMemOpChain);
12688 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12689 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12690 return TokenFactor;
12691}
12692
12694 SDValue NewMemOp) {
12695 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12696 SDValue OldChain = SDValue(OldLoad, 1);
12697 SDValue NewMemOpChain = NewMemOp.getValue(1);
12698 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12699}
12700
12702 Function **OutFunction) {
12703 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12704
12705 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12706 auto *Module = MF->getFunction().getParent();
12707 auto *Function = Module->getFunction(Symbol);
12708
12709 if (OutFunction != nullptr)
12710 *OutFunction = Function;
12711
12712 if (Function != nullptr) {
12713 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12714 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12715 }
12716
12717 std::string ErrorStr;
12718 raw_string_ostream ErrorFormatter(ErrorStr);
12719 ErrorFormatter << "Undefined external symbol ";
12720 ErrorFormatter << '"' << Symbol << '"';
12721 report_fatal_error(Twine(ErrorStr));
12722}
12723
12724//===----------------------------------------------------------------------===//
12725// SDNode Class
12726//===----------------------------------------------------------------------===//
12727
12730 return Const != nullptr && Const->isZero();
12731}
12732
12734 return V.isUndef() || isNullConstant(V);
12735}
12736
12739 return Const != nullptr && Const->isZero() && !Const->isNegative();
12740}
12741
12744 return Const != nullptr && Const->isAllOnes();
12745}
12746
12749 return Const != nullptr && Const->isOne();
12750}
12751
12754 return Const != nullptr && Const->isMinSignedValue();
12755}
12756
12757bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12758 unsigned OperandNo) {
12759 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12760 // TODO: Target-specific opcodes could be added.
12761 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12762 /*AllowTruncation*/ true)) {
12763 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12764 switch (Opcode) {
12765 case ISD::ADD:
12766 case ISD::OR:
12767 case ISD::XOR:
12768 case ISD::UMAX:
12769 return Const.isZero();
12770 case ISD::MUL:
12771 return Const.isOne();
12772 case ISD::AND:
12773 case ISD::UMIN:
12774 return Const.isAllOnes();
12775 case ISD::SMAX:
12776 return Const.isMinSignedValue();
12777 case ISD::SMIN:
12778 return Const.isMaxSignedValue();
12779 case ISD::SUB:
12780 case ISD::SHL:
12781 case ISD::SRA:
12782 case ISD::SRL:
12783 return OperandNo == 1 && Const.isZero();
12784 case ISD::UDIV:
12785 case ISD::SDIV:
12786 return OperandNo == 1 && Const.isOne();
12787 }
12788 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12789 switch (Opcode) {
12790 case ISD::FADD:
12791 return ConstFP->isZero() &&
12792 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12793 case ISD::FSUB:
12794 return OperandNo == 1 && ConstFP->isZero() &&
12795 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12796 case ISD::FMUL:
12797 return ConstFP->isExactlyValue(1.0);
12798 case ISD::FDIV:
12799 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12800 case ISD::FMINNUM:
12801 case ISD::FMAXNUM: {
12802 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12803 EVT VT = V.getValueType();
12804 const fltSemantics &Semantics = VT.getFltSemantics();
12805 APFloat NeutralAF = !Flags.hasNoNaNs()
12806 ? APFloat::getQNaN(Semantics)
12807 : !Flags.hasNoInfs()
12808 ? APFloat::getInf(Semantics)
12809 : APFloat::getLargest(Semantics);
12810 if (Opcode == ISD::FMAXNUM)
12811 NeutralAF.changeSign();
12812
12813 return ConstFP->isExactlyValue(NeutralAF);
12814 }
12815 }
12816 }
12817 return false;
12818}
12819
12821 while (V.getOpcode() == ISD::BITCAST)
12822 V = V.getOperand(0);
12823 return V;
12824}
12825
12827 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12828 V = V.getOperand(0);
12829 return V;
12830}
12831
12833 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12834 V = V.getOperand(0);
12835 return V;
12836}
12837
12839 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12840 SDValue InVec = V.getOperand(0);
12841 SDValue EltNo = V.getOperand(2);
12842 EVT VT = InVec.getValueType();
12843 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12844 if (IndexC && VT.isFixedLengthVector() &&
12845 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12846 !DemandedElts[IndexC->getZExtValue()]) {
12847 V = InVec;
12848 continue;
12849 }
12850 break;
12851 }
12852 return V;
12853}
12854
12856 while (V.getOpcode() == ISD::TRUNCATE)
12857 V = V.getOperand(0);
12858 return V;
12859}
12860
12861bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12862 if (V.getOpcode() != ISD::XOR)
12863 return false;
12864 V = peekThroughBitcasts(V.getOperand(1));
12865 unsigned NumBits = V.getScalarValueSizeInBits();
12866 ConstantSDNode *C =
12867 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12868 return C && (C->getAPIntValue().countr_one() >= NumBits);
12869}
12870
12872 bool AllowTruncation) {
12873 EVT VT = N.getValueType();
12874 APInt DemandedElts = VT.isFixedLengthVector()
12876 : APInt(1, 1);
12877 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12878}
12879
12881 bool AllowUndefs,
12882 bool AllowTruncation) {
12884 return CN;
12885
12886 // SplatVectors can truncate their operands. Ignore that case here unless
12887 // AllowTruncation is set.
12888 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12889 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12890 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12891 EVT CVT = CN->getValueType(0);
12892 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12893 if (AllowTruncation || CVT == VecEltVT)
12894 return CN;
12895 }
12896 }
12897
12899 BitVector UndefElements;
12900 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12901
12902 // BuildVectors can truncate their operands. Ignore that case here unless
12903 // AllowTruncation is set.
12904 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12905 if (CN && (UndefElements.none() || AllowUndefs)) {
12906 EVT CVT = CN->getValueType(0);
12907 EVT NSVT = N.getValueType().getScalarType();
12908 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12909 if (AllowTruncation || (CVT == NSVT))
12910 return CN;
12911 }
12912 }
12913
12914 return nullptr;
12915}
12916
12918 EVT VT = N.getValueType();
12919 APInt DemandedElts = VT.isFixedLengthVector()
12921 : APInt(1, 1);
12922 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12923}
12924
12926 const APInt &DemandedElts,
12927 bool AllowUndefs) {
12929 return CN;
12930
12932 BitVector UndefElements;
12933 ConstantFPSDNode *CN =
12934 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12935 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12936 if (CN && (UndefElements.none() || AllowUndefs))
12937 return CN;
12938 }
12939
12940 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12941 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12942 return CN;
12943
12944 return nullptr;
12945}
12946
12947bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12948 // TODO: may want to use peekThroughBitcast() here.
12949 ConstantSDNode *C =
12950 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12951 return C && C->isZero();
12952}
12953
12954bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12955 ConstantSDNode *C =
12956 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12957 return C && C->isOne();
12958}
12959
12960bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
12961 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
12962 return C && C->isExactlyValue(1.0);
12963}
12964
12965bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12967 unsigned BitWidth = N.getScalarValueSizeInBits();
12968 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12969 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12970}
12971
12972bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
12973 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12974 return C && APInt::isSameValue(C->getAPIntValue(),
12975 APInt(C->getAPIntValue().getBitWidth(), 1));
12976}
12977
12978bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
12980 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
12981 return C && C->isZero();
12982}
12983
12984bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
12985 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
12986 return C && C->isZero();
12987}
12988
12992
12993MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12994 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12995 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12996 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12997 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12998 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12999 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13000
13001 // We check here that the size of the memory operand fits within the size of
13002 // the MMO. This is because the MMO might indicate only a possible address
13003 // range instead of specifying the affected memory addresses precisely.
13004 assert(
13005 (!MMO->getType().isValid() ||
13006 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13007 "Size mismatch!");
13008}
13009
13010/// Profile - Gather unique data for the node.
13011///
13013 AddNodeIDNode(ID, this);
13014}
13015
13016namespace {
13017
13018 struct EVTArray {
13019 std::vector<EVT> VTs;
13020
13021 EVTArray() {
13022 VTs.reserve(MVT::VALUETYPE_SIZE);
13023 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13024 VTs.push_back(MVT((MVT::SimpleValueType)i));
13025 }
13026 };
13027
13028} // end anonymous namespace
13029
13030/// getValueTypeList - Return a pointer to the specified value type.
13031///
13032const EVT *SDNode::getValueTypeList(MVT VT) {
13033 static EVTArray SimpleVTArray;
13034
13035 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13036 return &SimpleVTArray.VTs[VT.SimpleTy];
13037}
13038
13039/// hasAnyUseOfValue - Return true if there are any use of the indicated
13040/// value. This method ignores uses of other values defined by this operation.
13041bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13042 assert(Value < getNumValues() && "Bad value!");
13043
13044 for (SDUse &U : uses())
13045 if (U.getResNo() == Value)
13046 return true;
13047
13048 return false;
13049}
13050
13051/// isOnlyUserOf - Return true if this node is the only use of N.
13052bool SDNode::isOnlyUserOf(const SDNode *N) const {
13053 bool Seen = false;
13054 for (const SDNode *User : N->users()) {
13055 if (User == this)
13056 Seen = true;
13057 else
13058 return false;
13059 }
13060
13061 return Seen;
13062}
13063
13064/// Return true if the only users of N are contained in Nodes.
13066 bool Seen = false;
13067 for (const SDNode *User : N->users()) {
13068 if (llvm::is_contained(Nodes, User))
13069 Seen = true;
13070 else
13071 return false;
13072 }
13073
13074 return Seen;
13075}
13076
13077/// Return true if the referenced return value is an operand of N.
13078bool SDValue::isOperandOf(const SDNode *N) const {
13079 return is_contained(N->op_values(), *this);
13080}
13081
13082bool SDNode::isOperandOf(const SDNode *N) const {
13083 return any_of(N->op_values(),
13084 [this](SDValue Op) { return this == Op.getNode(); });
13085}
13086
13087/// reachesChainWithoutSideEffects - Return true if this operand (which must
13088/// be a chain) reaches the specified operand without crossing any
13089/// side-effecting instructions on any chain path. In practice, this looks
13090/// through token factors and non-volatile loads. In order to remain efficient,
13091/// this only looks a couple of nodes in, it does not do an exhaustive search.
13092///
13093/// Note that we only need to examine chains when we're searching for
13094/// side-effects; SelectionDAG requires that all side-effects are represented
13095/// by chains, even if another operand would force a specific ordering. This
13096/// constraint is necessary to allow transformations like splitting loads.
13098 unsigned Depth) const {
13099 if (*this == Dest) return true;
13100
13101 // Don't search too deeply, we just want to be able to see through
13102 // TokenFactor's etc.
13103 if (Depth == 0) return false;
13104
13105 // If this is a token factor, all inputs to the TF happen in parallel.
13106 if (getOpcode() == ISD::TokenFactor) {
13107 // First, try a shallow search.
13108 if (is_contained((*this)->ops(), Dest)) {
13109 // We found the chain we want as an operand of this TokenFactor.
13110 // Essentially, we reach the chain without side-effects if we could
13111 // serialize the TokenFactor into a simple chain of operations with
13112 // Dest as the last operation. This is automatically true if the
13113 // chain has one use: there are no other ordering constraints.
13114 // If the chain has more than one use, we give up: some other
13115 // use of Dest might force a side-effect between Dest and the current
13116 // node.
13117 if (Dest.hasOneUse())
13118 return true;
13119 }
13120 // Next, try a deep search: check whether every operand of the TokenFactor
13121 // reaches Dest.
13122 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13123 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13124 });
13125 }
13126
13127 // Loads don't have side effects, look through them.
13128 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13129 if (Ld->isUnordered())
13130 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13131 }
13132 return false;
13133}
13134
13135bool SDNode::hasPredecessor(const SDNode *N) const {
13138 Worklist.push_back(this);
13139 return hasPredecessorHelper(N, Visited, Worklist);
13140}
13141
13143 this->Flags &= Flags;
13144}
13145
13146SDValue
13148 ArrayRef<ISD::NodeType> CandidateBinOps,
13149 bool AllowPartials) {
13150 // The pattern must end in an extract from index 0.
13151 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13152 !isNullConstant(Extract->getOperand(1)))
13153 return SDValue();
13154
13155 // Match against one of the candidate binary ops.
13156 SDValue Op = Extract->getOperand(0);
13157 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13158 return Op.getOpcode() == unsigned(BinOp);
13159 }))
13160 return SDValue();
13161
13162 // Floating-point reductions may require relaxed constraints on the final step
13163 // of the reduction because they may reorder intermediate operations.
13164 unsigned CandidateBinOp = Op.getOpcode();
13165 if (Op.getValueType().isFloatingPoint()) {
13166 SDNodeFlags Flags = Op->getFlags();
13167 switch (CandidateBinOp) {
13168 case ISD::FADD:
13169 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13170 return SDValue();
13171 break;
13172 default:
13173 llvm_unreachable("Unhandled FP opcode for binop reduction");
13174 }
13175 }
13176
13177 // Matching failed - attempt to see if we did enough stages that a partial
13178 // reduction from a subvector is possible.
13179 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13180 if (!AllowPartials || !Op)
13181 return SDValue();
13182 EVT OpVT = Op.getValueType();
13183 EVT OpSVT = OpVT.getScalarType();
13184 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13185 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13186 return SDValue();
13187 BinOp = (ISD::NodeType)CandidateBinOp;
13188 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13189 };
13190
13191 // At each stage, we're looking for something that looks like:
13192 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13193 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13194 // i32 undef, i32 undef, i32 undef, i32 undef>
13195 // %a = binop <8 x i32> %op, %s
13196 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13197 // we expect something like:
13198 // <4,5,6,7,u,u,u,u>
13199 // <2,3,u,u,u,u,u,u>
13200 // <1,u,u,u,u,u,u,u>
13201 // While a partial reduction match would be:
13202 // <2,3,u,u,u,u,u,u>
13203 // <1,u,u,u,u,u,u,u>
13204 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13205 SDValue PrevOp;
13206 for (unsigned i = 0; i < Stages; ++i) {
13207 unsigned MaskEnd = (1 << i);
13208
13209 if (Op.getOpcode() != CandidateBinOp)
13210 return PartialReduction(PrevOp, MaskEnd);
13211
13212 SDValue Op0 = Op.getOperand(0);
13213 SDValue Op1 = Op.getOperand(1);
13214
13216 if (Shuffle) {
13217 Op = Op1;
13218 } else {
13219 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13220 Op = Op0;
13221 }
13222
13223 // The first operand of the shuffle should be the same as the other operand
13224 // of the binop.
13225 if (!Shuffle || Shuffle->getOperand(0) != Op)
13226 return PartialReduction(PrevOp, MaskEnd);
13227
13228 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13229 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13230 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13231 return PartialReduction(PrevOp, MaskEnd);
13232
13233 PrevOp = Op;
13234 }
13235
13236 // Handle subvector reductions, which tend to appear after the shuffle
13237 // reduction stages.
13238 while (Op.getOpcode() == CandidateBinOp) {
13239 unsigned NumElts = Op.getValueType().getVectorNumElements();
13240 SDValue Op0 = Op.getOperand(0);
13241 SDValue Op1 = Op.getOperand(1);
13242 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13244 Op0.getOperand(0) != Op1.getOperand(0))
13245 break;
13246 SDValue Src = Op0.getOperand(0);
13247 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13248 if (NumSrcElts != (2 * NumElts))
13249 break;
13250 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13251 Op1.getConstantOperandAPInt(1) == NumElts) &&
13252 !(Op1.getConstantOperandAPInt(1) == 0 &&
13253 Op0.getConstantOperandAPInt(1) == NumElts))
13254 break;
13255 Op = Src;
13256 }
13257
13258 BinOp = (ISD::NodeType)CandidateBinOp;
13259 return Op;
13260}
13261
13263 EVT VT = N->getValueType(0);
13264 EVT EltVT = VT.getVectorElementType();
13265 unsigned NE = VT.getVectorNumElements();
13266
13267 SDLoc dl(N);
13268
13269 // If ResNE is 0, fully unroll the vector op.
13270 if (ResNE == 0)
13271 ResNE = NE;
13272 else if (NE > ResNE)
13273 NE = ResNE;
13274
13275 if (N->getNumValues() == 2) {
13276 SmallVector<SDValue, 8> Scalars0, Scalars1;
13277 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13278 EVT VT1 = N->getValueType(1);
13279 EVT EltVT1 = VT1.getVectorElementType();
13280
13281 unsigned i;
13282 for (i = 0; i != NE; ++i) {
13283 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13284 SDValue Operand = N->getOperand(j);
13285 EVT OperandVT = Operand.getValueType();
13286
13287 // A vector operand; extract a single element.
13288 EVT OperandEltVT = OperandVT.getVectorElementType();
13289 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13290 }
13291
13292 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13293 Scalars0.push_back(EltOp);
13294 Scalars1.push_back(EltOp.getValue(1));
13295 }
13296
13297 for (; i < ResNE; ++i) {
13298 Scalars0.push_back(getUNDEF(EltVT));
13299 Scalars1.push_back(getUNDEF(EltVT1));
13300 }
13301
13302 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13303 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13304 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13305 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13306 return getMergeValues({Vec0, Vec1}, dl);
13307 }
13308
13309 assert(N->getNumValues() == 1 &&
13310 "Can't unroll a vector with multiple results!");
13311
13313 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13314
13315 unsigned i;
13316 for (i= 0; i != NE; ++i) {
13317 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13318 SDValue Operand = N->getOperand(j);
13319 EVT OperandVT = Operand.getValueType();
13320 if (OperandVT.isVector()) {
13321 // A vector operand; extract a single element.
13322 EVT OperandEltVT = OperandVT.getVectorElementType();
13323 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13324 } else {
13325 // A scalar operand; just use it as is.
13326 Operands[j] = Operand;
13327 }
13328 }
13329
13330 switch (N->getOpcode()) {
13331 default: {
13332 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13333 N->getFlags()));
13334 break;
13335 }
13336 case ISD::VSELECT:
13337 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13338 break;
13339 case ISD::SHL:
13340 case ISD::SRA:
13341 case ISD::SRL:
13342 case ISD::ROTL:
13343 case ISD::ROTR:
13344 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13345 getShiftAmountOperand(Operands[0].getValueType(),
13346 Operands[1])));
13347 break;
13349 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13350 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13351 Operands[0],
13352 getValueType(ExtVT)));
13353 break;
13354 }
13355 case ISD::ADDRSPACECAST: {
13356 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13357 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13358 ASC->getSrcAddressSpace(),
13359 ASC->getDestAddressSpace()));
13360 break;
13361 }
13362 }
13363 }
13364
13365 for (; i < ResNE; ++i)
13366 Scalars.push_back(getUNDEF(EltVT));
13367
13368 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13369 return getBuildVector(VecVT, dl, Scalars);
13370}
13371
13372std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13373 SDNode *N, unsigned ResNE) {
13374 unsigned Opcode = N->getOpcode();
13375 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13376 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13377 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13378 "Expected an overflow opcode");
13379
13380 EVT ResVT = N->getValueType(0);
13381 EVT OvVT = N->getValueType(1);
13382 EVT ResEltVT = ResVT.getVectorElementType();
13383 EVT OvEltVT = OvVT.getVectorElementType();
13384 SDLoc dl(N);
13385
13386 // If ResNE is 0, fully unroll the vector op.
13387 unsigned NE = ResVT.getVectorNumElements();
13388 if (ResNE == 0)
13389 ResNE = NE;
13390 else if (NE > ResNE)
13391 NE = ResNE;
13392
13393 SmallVector<SDValue, 8> LHSScalars;
13394 SmallVector<SDValue, 8> RHSScalars;
13395 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13396 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13397
13398 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13399 SDVTList VTs = getVTList(ResEltVT, SVT);
13400 SmallVector<SDValue, 8> ResScalars;
13401 SmallVector<SDValue, 8> OvScalars;
13402 for (unsigned i = 0; i < NE; ++i) {
13403 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13404 SDValue Ov =
13405 getSelect(dl, OvEltVT, Res.getValue(1),
13406 getBoolConstant(true, dl, OvEltVT, ResVT),
13407 getConstant(0, dl, OvEltVT));
13408
13409 ResScalars.push_back(Res);
13410 OvScalars.push_back(Ov);
13411 }
13412
13413 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13414 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13415
13416 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13417 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13418 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13419 getBuildVector(NewOvVT, dl, OvScalars));
13420}
13421
13424 unsigned Bytes,
13425 int Dist) const {
13426 if (LD->isVolatile() || Base->isVolatile())
13427 return false;
13428 // TODO: probably too restrictive for atomics, revisit
13429 if (!LD->isSimple())
13430 return false;
13431 if (LD->isIndexed() || Base->isIndexed())
13432 return false;
13433 if (LD->getChain() != Base->getChain())
13434 return false;
13435 EVT VT = LD->getMemoryVT();
13436 if (VT.getSizeInBits() / 8 != Bytes)
13437 return false;
13438
13439 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13440 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13441
13442 int64_t Offset = 0;
13443 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13444 return (Dist * (int64_t)Bytes == Offset);
13445 return false;
13446}
13447
13448/// InferPtrAlignment - Infer alignment of a load / store address. Return
13449/// std::nullopt if it cannot be inferred.
13451 // If this is a GlobalAddress + cst, return the alignment.
13452 const GlobalValue *GV = nullptr;
13453 int64_t GVOffset = 0;
13454 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13455 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13456 KnownBits Known(PtrWidth);
13458 unsigned AlignBits = Known.countMinTrailingZeros();
13459 if (AlignBits)
13460 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13461 }
13462
13463 // If this is a direct reference to a stack slot, use information about the
13464 // stack slot's alignment.
13465 int FrameIdx = INT_MIN;
13466 int64_t FrameOffset = 0;
13468 FrameIdx = FI->getIndex();
13469 } else if (isBaseWithConstantOffset(Ptr) &&
13471 // Handle FI+Cst
13472 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13473 FrameOffset = Ptr.getConstantOperandVal(1);
13474 }
13475
13476 if (FrameIdx != INT_MIN) {
13478 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13479 }
13480
13481 return std::nullopt;
13482}
13483
13484/// Split the scalar node with EXTRACT_ELEMENT using the provided
13485/// VTs and return the low/high part.
13486std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13487 const SDLoc &DL,
13488 const EVT &LoVT,
13489 const EVT &HiVT) {
13490 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13491 "Split node must be a scalar type");
13492 SDValue Lo =
13494 SDValue Hi =
13496 return std::make_pair(Lo, Hi);
13497}
13498
13499/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13500/// which is split (or expanded) into two not necessarily identical pieces.
13501std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13502 // Currently all types are split in half.
13503 EVT LoVT, HiVT;
13504 if (!VT.isVector())
13505 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13506 else
13507 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13508
13509 return std::make_pair(LoVT, HiVT);
13510}
13511
13512/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13513/// type, dependent on an enveloping VT that has been split into two identical
13514/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13515std::pair<EVT, EVT>
13517 bool *HiIsEmpty) const {
13518 EVT EltTp = VT.getVectorElementType();
13519 // Examples:
13520 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13521 // custom VL=9 with enveloping VL=8/8 yields 8/1
13522 // custom VL=10 with enveloping VL=8/8 yields 8/2
13523 // etc.
13524 ElementCount VTNumElts = VT.getVectorElementCount();
13525 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13526 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13527 "Mixing fixed width and scalable vectors when enveloping a type");
13528 EVT LoVT, HiVT;
13529 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13530 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13531 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13532 *HiIsEmpty = false;
13533 } else {
13534 // Flag that hi type has zero storage size, but return split envelop type
13535 // (this would be easier if vector types with zero elements were allowed).
13536 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13537 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13538 *HiIsEmpty = true;
13539 }
13540 return std::make_pair(LoVT, HiVT);
13541}
13542
13543/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13544/// low/high part.
13545std::pair<SDValue, SDValue>
13546SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13547 const EVT &HiVT) {
13548 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13549 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13550 "Splitting vector with an invalid mixture of fixed and scalable "
13551 "vector types");
13553 N.getValueType().getVectorMinNumElements() &&
13554 "More vector elements requested than available!");
13555 SDValue Lo, Hi;
13556 Lo = getExtractSubvector(DL, LoVT, N, 0);
13557 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13558 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13559 // IDX with the runtime scaling factor of the result vector type. For
13560 // fixed-width result vectors, that runtime scaling factor is 1.
13563 return std::make_pair(Lo, Hi);
13564}
13565
13566std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13567 const SDLoc &DL) {
13568 // Split the vector length parameter.
13569 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13570 EVT VT = N.getValueType();
13572 "Expecting the mask to be an evenly-sized vector");
13573 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13574 SDValue HalfNumElts =
13575 VecVT.isFixedLengthVector()
13576 ? getConstant(HalfMinNumElts, DL, VT)
13577 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13578 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13579 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13580 return std::make_pair(Lo, Hi);
13581}
13582
13583/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13585 EVT VT = N.getValueType();
13588 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13589}
13590
13593 unsigned Start, unsigned Count,
13594 EVT EltVT) {
13595 EVT VT = Op.getValueType();
13596 if (Count == 0)
13598 if (EltVT == EVT())
13599 EltVT = VT.getVectorElementType();
13600 SDLoc SL(Op);
13601 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13602 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13603 }
13604}
13605
13606// getAddressSpace - Return the address space this GlobalAddress belongs to.
13608 return getGlobal()->getType()->getAddressSpace();
13609}
13610
13613 return Val.MachineCPVal->getType();
13614 return Val.ConstVal->getType();
13615}
13616
13617bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13618 unsigned &SplatBitSize,
13619 bool &HasAnyUndefs,
13620 unsigned MinSplatBits,
13621 bool IsBigEndian) const {
13622 EVT VT = getValueType(0);
13623 assert(VT.isVector() && "Expected a vector type");
13624 unsigned VecWidth = VT.getSizeInBits();
13625 if (MinSplatBits > VecWidth)
13626 return false;
13627
13628 // FIXME: The widths are based on this node's type, but build vectors can
13629 // truncate their operands.
13630 SplatValue = APInt(VecWidth, 0);
13631 SplatUndef = APInt(VecWidth, 0);
13632
13633 // Get the bits. Bits with undefined values (when the corresponding element
13634 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13635 // in SplatValue. If any of the values are not constant, give up and return
13636 // false.
13637 unsigned int NumOps = getNumOperands();
13638 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13639 unsigned EltWidth = VT.getScalarSizeInBits();
13640
13641 for (unsigned j = 0; j < NumOps; ++j) {
13642 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13643 SDValue OpVal = getOperand(i);
13644 unsigned BitPos = j * EltWidth;
13645
13646 if (OpVal.isUndef())
13647 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13648 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13649 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13650 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13651 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13652 else
13653 return false;
13654 }
13655
13656 // The build_vector is all constants or undefs. Find the smallest element
13657 // size that splats the vector.
13658 HasAnyUndefs = (SplatUndef != 0);
13659
13660 // FIXME: This does not work for vectors with elements less than 8 bits.
13661 while (VecWidth > 8) {
13662 // If we can't split in half, stop here.
13663 if (VecWidth & 1)
13664 break;
13665
13666 unsigned HalfSize = VecWidth / 2;
13667 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13668 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13669 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13670 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13671
13672 // If the two halves do not match (ignoring undef bits), stop here.
13673 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13674 MinSplatBits > HalfSize)
13675 break;
13676
13677 SplatValue = HighValue | LowValue;
13678 SplatUndef = HighUndef & LowUndef;
13679
13680 VecWidth = HalfSize;
13681 }
13682
13683 // FIXME: The loop above only tries to split in halves. But if the input
13684 // vector for example is <3 x i16> it wouldn't be able to detect a
13685 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13686 // optimizations. I guess that back in the days when this helper was created
13687 // vectors normally was power-of-2 sized.
13688
13689 SplatBitSize = VecWidth;
13690 return true;
13691}
13692
13694 BitVector *UndefElements) const {
13695 unsigned NumOps = getNumOperands();
13696 if (UndefElements) {
13697 UndefElements->clear();
13698 UndefElements->resize(NumOps);
13699 }
13700 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13701 if (!DemandedElts)
13702 return SDValue();
13703 SDValue Splatted;
13704 for (unsigned i = 0; i != NumOps; ++i) {
13705 if (!DemandedElts[i])
13706 continue;
13707 SDValue Op = getOperand(i);
13708 if (Op.isUndef()) {
13709 if (UndefElements)
13710 (*UndefElements)[i] = true;
13711 } else if (!Splatted) {
13712 Splatted = Op;
13713 } else if (Splatted != Op) {
13714 return SDValue();
13715 }
13716 }
13717
13718 if (!Splatted) {
13719 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13720 assert(getOperand(FirstDemandedIdx).isUndef() &&
13721 "Can only have a splat without a constant for all undefs.");
13722 return getOperand(FirstDemandedIdx);
13723 }
13724
13725 return Splatted;
13726}
13727
13729 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13730 return getSplatValue(DemandedElts, UndefElements);
13731}
13732
13734 SmallVectorImpl<SDValue> &Sequence,
13735 BitVector *UndefElements) const {
13736 unsigned NumOps = getNumOperands();
13737 Sequence.clear();
13738 if (UndefElements) {
13739 UndefElements->clear();
13740 UndefElements->resize(NumOps);
13741 }
13742 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13743 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13744 return false;
13745
13746 // Set the undefs even if we don't find a sequence (like getSplatValue).
13747 if (UndefElements)
13748 for (unsigned I = 0; I != NumOps; ++I)
13749 if (DemandedElts[I] && getOperand(I).isUndef())
13750 (*UndefElements)[I] = true;
13751
13752 // Iteratively widen the sequence length looking for repetitions.
13753 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13754 Sequence.append(SeqLen, SDValue());
13755 for (unsigned I = 0; I != NumOps; ++I) {
13756 if (!DemandedElts[I])
13757 continue;
13758 SDValue &SeqOp = Sequence[I % SeqLen];
13760 if (Op.isUndef()) {
13761 if (!SeqOp)
13762 SeqOp = Op;
13763 continue;
13764 }
13765 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13766 Sequence.clear();
13767 break;
13768 }
13769 SeqOp = Op;
13770 }
13771 if (!Sequence.empty())
13772 return true;
13773 }
13774
13775 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13776 return false;
13777}
13778
13780 BitVector *UndefElements) const {
13781 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13782 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13783}
13784
13787 BitVector *UndefElements) const {
13789 getSplatValue(DemandedElts, UndefElements));
13790}
13791
13794 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13795}
13796
13799 BitVector *UndefElements) const {
13801 getSplatValue(DemandedElts, UndefElements));
13802}
13803
13808
13809int32_t
13811 uint32_t BitWidth) const {
13812 if (ConstantFPSDNode *CN =
13814 bool IsExact;
13815 APSInt IntVal(BitWidth);
13816 const APFloat &APF = CN->getValueAPF();
13817 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13818 APFloat::opOK ||
13819 !IsExact)
13820 return -1;
13821
13822 return IntVal.exactLogBase2();
13823 }
13824 return -1;
13825}
13826
13828 bool IsLittleEndian, unsigned DstEltSizeInBits,
13829 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13830 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13831 if (!isConstant())
13832 return false;
13833
13834 unsigned NumSrcOps = getNumOperands();
13835 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13836 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13837 "Invalid bitcast scale");
13838
13839 // Extract raw src bits.
13840 SmallVector<APInt> SrcBitElements(NumSrcOps,
13841 APInt::getZero(SrcEltSizeInBits));
13842 BitVector SrcUndeElements(NumSrcOps, false);
13843
13844 for (unsigned I = 0; I != NumSrcOps; ++I) {
13846 if (Op.isUndef()) {
13847 SrcUndeElements.set(I);
13848 continue;
13849 }
13850 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13851 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13852 assert((CInt || CFP) && "Unknown constant");
13853 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13854 : CFP->getValueAPF().bitcastToAPInt();
13855 }
13856
13857 // Recast to dst width.
13858 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13859 SrcBitElements, UndefElements, SrcUndeElements);
13860 return true;
13861}
13862
13863void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13864 unsigned DstEltSizeInBits,
13865 SmallVectorImpl<APInt> &DstBitElements,
13866 ArrayRef<APInt> SrcBitElements,
13867 BitVector &DstUndefElements,
13868 const BitVector &SrcUndefElements) {
13869 unsigned NumSrcOps = SrcBitElements.size();
13870 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13871 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13872 "Invalid bitcast scale");
13873 assert(NumSrcOps == SrcUndefElements.size() &&
13874 "Vector size mismatch");
13875
13876 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13877 DstUndefElements.clear();
13878 DstUndefElements.resize(NumDstOps, false);
13879 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13880
13881 // Concatenate src elements constant bits together into dst element.
13882 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13883 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13884 for (unsigned I = 0; I != NumDstOps; ++I) {
13885 DstUndefElements.set(I);
13886 APInt &DstBits = DstBitElements[I];
13887 for (unsigned J = 0; J != Scale; ++J) {
13888 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13889 if (SrcUndefElements[Idx])
13890 continue;
13891 DstUndefElements.reset(I);
13892 const APInt &SrcBits = SrcBitElements[Idx];
13893 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13894 "Illegal constant bitwidths");
13895 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13896 }
13897 }
13898 return;
13899 }
13900
13901 // Split src element constant bits into dst elements.
13902 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13903 for (unsigned I = 0; I != NumSrcOps; ++I) {
13904 if (SrcUndefElements[I]) {
13905 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13906 continue;
13907 }
13908 const APInt &SrcBits = SrcBitElements[I];
13909 for (unsigned J = 0; J != Scale; ++J) {
13910 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13911 APInt &DstBits = DstBitElements[Idx];
13912 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13913 }
13914 }
13915}
13916
13918 for (const SDValue &Op : op_values()) {
13919 unsigned Opc = Op.getOpcode();
13920 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13921 return false;
13922 }
13923 return true;
13924}
13925
13926std::optional<std::pair<APInt, APInt>>
13928 unsigned NumOps = getNumOperands();
13929 if (NumOps < 2)
13930 return std::nullopt;
13931
13934 return std::nullopt;
13935
13936 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13937 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13938 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13939
13940 if (Stride.isZero())
13941 return std::nullopt;
13942
13943 for (unsigned i = 2; i < NumOps; ++i) {
13945 return std::nullopt;
13946
13947 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13948 if (Val != (Start + (Stride * i)))
13949 return std::nullopt;
13950 }
13951
13952 return std::make_pair(Start, Stride);
13953}
13954
13956 // Find the first non-undef value in the shuffle mask.
13957 unsigned i, e;
13958 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
13959 /* search */;
13960
13961 // If all elements are undefined, this shuffle can be considered a splat
13962 // (although it should eventually get simplified away completely).
13963 if (i == e)
13964 return true;
13965
13966 // Make sure all remaining elements are either undef or the same as the first
13967 // non-undef value.
13968 for (int Idx = Mask[i]; i != e; ++i)
13969 if (Mask[i] >= 0 && Mask[i] != Idx)
13970 return false;
13971 return true;
13972}
13973
13974// Returns true if it is a constant integer BuildVector or constant integer,
13975// possibly hidden by a bitcast.
13977 SDValue N, bool AllowOpaques) const {
13979
13980 if (auto *C = dyn_cast<ConstantSDNode>(N))
13981 return AllowOpaques || !C->isOpaque();
13982
13984 return true;
13985
13986 // Treat a GlobalAddress supporting constant offset folding as a
13987 // constant integer.
13988 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
13989 if (GA->getOpcode() == ISD::GlobalAddress &&
13990 TLI->isOffsetFoldingLegal(GA))
13991 return true;
13992
13993 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13994 isa<ConstantSDNode>(N.getOperand(0)))
13995 return true;
13996 return false;
13997}
13998
13999// Returns true if it is a constant float BuildVector or constant float.
14002 return true;
14003
14005 return true;
14006
14007 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14008 isa<ConstantFPSDNode>(N.getOperand(0)))
14009 return true;
14010
14011 return false;
14012}
14013
14014std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14015 ConstantSDNode *Const =
14016 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14017 if (!Const)
14018 return std::nullopt;
14019
14020 EVT VT = N->getValueType(0);
14021 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14022 switch (TLI->getBooleanContents(N.getValueType())) {
14024 if (CVal.isOne())
14025 return true;
14026 if (CVal.isZero())
14027 return false;
14028 return std::nullopt;
14030 if (CVal.isAllOnes())
14031 return true;
14032 if (CVal.isZero())
14033 return false;
14034 return std::nullopt;
14036 return CVal[0];
14037 }
14038 llvm_unreachable("Unknown BooleanContent enum");
14039}
14040
14041void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14042 assert(!Node->OperandList && "Node already has operands");
14044 "too many operands to fit into SDNode");
14045 SDUse *Ops = OperandRecycler.allocate(
14046 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14047
14048 bool IsDivergent = false;
14049 for (unsigned I = 0; I != Vals.size(); ++I) {
14050 Ops[I].setUser(Node);
14051 Ops[I].setInitial(Vals[I]);
14052 EVT VT = Ops[I].getValueType();
14053
14054 // Skip Chain. It does not carry divergence.
14055 if (VT != MVT::Other &&
14056 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14057 Ops[I].getNode()->isDivergent()) {
14058 IsDivergent = true;
14059 }
14060 }
14061 Node->NumOperands = Vals.size();
14062 Node->OperandList = Ops;
14063 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14064 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14065 Node->SDNodeBits.IsDivergent = IsDivergent;
14066 }
14067 checkForCycles(Node);
14068}
14069
14072 size_t Limit = SDNode::getMaxNumOperands();
14073 while (Vals.size() > Limit) {
14074 unsigned SliceIdx = Vals.size() - Limit;
14075 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14076 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14077 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14078 Vals.emplace_back(NewTF);
14079 }
14080 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14081}
14082
14084 EVT VT, SDNodeFlags Flags) {
14085 switch (Opcode) {
14086 default:
14087 return SDValue();
14088 case ISD::ADD:
14089 case ISD::OR:
14090 case ISD::XOR:
14091 case ISD::UMAX:
14092 return getConstant(0, DL, VT);
14093 case ISD::MUL:
14094 return getConstant(1, DL, VT);
14095 case ISD::AND:
14096 case ISD::UMIN:
14097 return getAllOnesConstant(DL, VT);
14098 case ISD::SMAX:
14100 case ISD::SMIN:
14102 case ISD::FADD:
14103 // If flags allow, prefer positive zero since it's generally cheaper
14104 // to materialize on most targets.
14105 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14106 case ISD::FMUL:
14107 return getConstantFP(1.0, DL, VT);
14108 case ISD::FMINNUM:
14109 case ISD::FMAXNUM: {
14110 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14111 const fltSemantics &Semantics = VT.getFltSemantics();
14112 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14113 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14114 APFloat::getLargest(Semantics);
14115 if (Opcode == ISD::FMAXNUM)
14116 NeutralAF.changeSign();
14117
14118 return getConstantFP(NeutralAF, DL, VT);
14119 }
14120 case ISD::FMINIMUM:
14121 case ISD::FMAXIMUM: {
14122 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14123 const fltSemantics &Semantics = VT.getFltSemantics();
14124 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14125 : APFloat::getLargest(Semantics);
14126 if (Opcode == ISD::FMAXIMUM)
14127 NeutralAF.changeSign();
14128
14129 return getConstantFP(NeutralAF, DL, VT);
14130 }
14131
14132 }
14133}
14134
14135/// Helper used to make a call to a library function that has one argument of
14136/// pointer type.
14137///
14138/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14139/// used to get or set floating-point state. They have one argument of pointer
14140/// type, which points to the memory region containing bits of the
14141/// floating-point state. The value returned by such function is ignored in the
14142/// created call.
14143///
14144/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14145/// \param Ptr Pointer used to save/load state.
14146/// \param InChain Ingoing token chain.
14147/// \returns Outgoing chain token.
14149 SDValue InChain,
14150 const SDLoc &DLoc) {
14151 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14153 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14154 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
14155 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
14156 TLI->getPointerTy(getDataLayout()));
14158 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14159 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
14160 std::move(Args));
14161 return TLI->LowerCallTo(CLI).second;
14162}
14163
14165 assert(From && To && "Invalid SDNode; empty source SDValue?");
14166 auto I = SDEI.find(From);
14167 if (I == SDEI.end())
14168 return;
14169
14170 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14171 // the iterator, hence the need to make a copy to prevent a use-after-free.
14172 NodeExtraInfo NEI = I->second;
14173 if (LLVM_LIKELY(!NEI.PCSections)) {
14174 // No deep copy required for the types of extra info set.
14175 //
14176 // FIXME: Investigate if other types of extra info also need deep copy. This
14177 // depends on the types of nodes they can be attached to: if some extra info
14178 // is only ever attached to nodes where a replacement To node is always the
14179 // node where later use and propagation of the extra info has the intended
14180 // semantics, no deep copy is required.
14181 SDEI[To] = std::move(NEI);
14182 return;
14183 }
14184
14185 const SDNode *EntrySDN = getEntryNode().getNode();
14186
14187 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14188 // through the replacement of From with To. Otherwise, replacements of a node
14189 // (From) with more complex nodes (To and its operands) may result in lost
14190 // extra info where the root node (To) is insignificant in further propagating
14191 // and using extra info when further lowering to MIR.
14192 //
14193 // In the first step pre-populate the visited set with the nodes reachable
14194 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14195 // DAG that is not new and should be left untouched.
14196 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14197 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14198 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14199 if (MaxDepth == 0) {
14200 // Remember this node in case we need to increase MaxDepth and continue
14201 // populating FromReach from this node.
14202 Leafs.emplace_back(N);
14203 return;
14204 }
14205 if (!FromReach.insert(N).second)
14206 return;
14207 for (const SDValue &Op : N->op_values())
14208 Self(Self, Op.getNode(), MaxDepth - 1);
14209 };
14210
14211 // Copy extra info to To and all its transitive operands (that are new).
14213 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14214 if (FromReach.contains(N))
14215 return true;
14216 if (!Visited.insert(N).second)
14217 return true;
14218 if (EntrySDN == N)
14219 return false;
14220 for (const SDValue &Op : N->op_values()) {
14221 if (N == To && Op.getNode() == EntrySDN) {
14222 // Special case: New node's operand is the entry node; just need to
14223 // copy extra info to new node.
14224 break;
14225 }
14226 if (!Self(Self, Op.getNode()))
14227 return false;
14228 }
14229 // Copy only if entry node was not reached.
14230 SDEI[N] = NEI;
14231 return true;
14232 };
14233
14234 // We first try with a lower MaxDepth, assuming that the path to common
14235 // operands between From and To is relatively short. This significantly
14236 // improves performance in the common case. The initial MaxDepth is big
14237 // enough to avoid retry in the common case; the last MaxDepth is large
14238 // enough to avoid having to use the fallback below (and protects from
14239 // potential stack exhaustion from recursion).
14240 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14241 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14242 // StartFrom is the previous (or initial) set of leafs reachable at the
14243 // previous maximum depth.
14245 std::swap(StartFrom, Leafs);
14246 for (const SDNode *N : StartFrom)
14247 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14248 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14249 return;
14250 // This should happen very rarely (reached the entry node).
14251 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14252 assert(!Leafs.empty());
14253 }
14254
14255 // This should not happen - but if it did, that means the subgraph reachable
14256 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14257 // could not visit all reachable common operands. Consequently, we were able
14258 // to reach the entry node.
14259 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14260 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14261 // Best-effort fallback if assertions disabled.
14262 SDEI[To] = std::move(NEI);
14263}
14264
14265#ifndef NDEBUG
14266static void checkForCyclesHelper(const SDNode *N,
14269 const llvm::SelectionDAG *DAG) {
14270 // If this node has already been checked, don't check it again.
14271 if (Checked.count(N))
14272 return;
14273
14274 // If a node has already been visited on this depth-first walk, reject it as
14275 // a cycle.
14276 if (!Visited.insert(N).second) {
14277 errs() << "Detected cycle in SelectionDAG\n";
14278 dbgs() << "Offending node:\n";
14279 N->dumprFull(DAG); dbgs() << "\n";
14280 abort();
14281 }
14282
14283 for (const SDValue &Op : N->op_values())
14284 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14285
14286 Checked.insert(N);
14287 Visited.erase(N);
14288}
14289#endif
14290
14292 const llvm::SelectionDAG *DAG,
14293 bool force) {
14294#ifndef NDEBUG
14295 bool check = force;
14296#ifdef EXPENSIVE_CHECKS
14297 check = true;
14298#endif // EXPENSIVE_CHECKS
14299 if (check) {
14300 assert(N && "Checking nonexistent SDNode");
14303 checkForCyclesHelper(N, visited, checked, DAG);
14304 }
14305#endif // !NDEBUG
14306}
14307
14308void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14309 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14310}
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 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
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:899
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
const APFloat & getValue() const
Definition Constants.h:321
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:157
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:154
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h: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:124
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 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 getVScale(const SDLoc &DL, EVT VT, APInt MulImm, bool ConstantFold=true)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC, bool ConstantFold=true)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI 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 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 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 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
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
const char * getLibcallName(RTLIB::Libcall Call) const
Get the libcall routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
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 void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
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
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
@ 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:1745
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:1725
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:1606
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:2472
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:2136
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:1588
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:1732
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:1622
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:1739
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:1835
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:1897
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
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#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)