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
5705 default:
5706 // Allow the target to implement this method for its nodes.
5707 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5708 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5709 return TLI->canCreateUndefOrPoisonForTargetNode(
5710 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5711 break;
5712 }
5713
5714 // Be conservative and return true.
5715 return true;
5716}
5717
5718bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5719 unsigned Opcode = Op.getOpcode();
5720 if (Opcode == ISD::OR)
5721 return Op->getFlags().hasDisjoint() ||
5722 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5723 if (Opcode == ISD::XOR)
5724 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5725 return false;
5726}
5727
5729 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5730 (Op.isAnyAdd() || isADDLike(Op));
5731}
5732
5734 unsigned Depth) const {
5735 EVT VT = Op.getValueType();
5736
5737 // Since the number of lanes in a scalable vector is unknown at compile time,
5738 // we track one bit which is implicitly broadcast to all lanes. This means
5739 // that all lanes in a scalable vector are considered demanded.
5740 APInt DemandedElts = VT.isFixedLengthVector()
5742 : APInt(1, 1);
5743
5744 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5745}
5746
5748 bool SNaN, unsigned Depth) const {
5749 assert(!DemandedElts.isZero() && "No demanded elements");
5750
5751 // If we're told that NaNs won't happen, assume they won't.
5752 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5753 return true;
5754
5755 if (Depth >= MaxRecursionDepth)
5756 return false; // Limit search depth.
5757
5758 // If the value is a constant, we can obviously see if it is a NaN or not.
5760 return !C->getValueAPF().isNaN() ||
5761 (SNaN && !C->getValueAPF().isSignaling());
5762 }
5763
5764 unsigned Opcode = Op.getOpcode();
5765 switch (Opcode) {
5766 case ISD::FADD:
5767 case ISD::FSUB:
5768 case ISD::FMUL:
5769 case ISD::FDIV:
5770 case ISD::FREM:
5771 case ISD::FSIN:
5772 case ISD::FCOS:
5773 case ISD::FTAN:
5774 case ISD::FASIN:
5775 case ISD::FACOS:
5776 case ISD::FATAN:
5777 case ISD::FATAN2:
5778 case ISD::FSINH:
5779 case ISD::FCOSH:
5780 case ISD::FTANH:
5781 case ISD::FMA:
5782 case ISD::FMULADD:
5783 case ISD::FMAD: {
5784 if (SNaN)
5785 return true;
5786 // TODO: Need isKnownNeverInfinity
5787 return false;
5788 }
5789 case ISD::FCANONICALIZE:
5790 case ISD::FEXP:
5791 case ISD::FEXP2:
5792 case ISD::FEXP10:
5793 case ISD::FTRUNC:
5794 case ISD::FFLOOR:
5795 case ISD::FCEIL:
5796 case ISD::FROUND:
5797 case ISD::FROUNDEVEN:
5798 case ISD::LROUND:
5799 case ISD::LLROUND:
5800 case ISD::FRINT:
5801 case ISD::LRINT:
5802 case ISD::LLRINT:
5803 case ISD::FNEARBYINT:
5804 case ISD::FLDEXP: {
5805 if (SNaN)
5806 return true;
5807 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5808 }
5809 case ISD::FABS:
5810 case ISD::FNEG:
5811 case ISD::FCOPYSIGN: {
5812 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5813 }
5814 case ISD::SELECT:
5815 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5816 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5817 case ISD::FP_EXTEND:
5818 case ISD::FP_ROUND: {
5819 if (SNaN)
5820 return true;
5821 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5822 }
5823 case ISD::SINT_TO_FP:
5824 case ISD::UINT_TO_FP:
5825 return true;
5826 case ISD::FSQRT: // Need is known positive
5827 case ISD::FLOG:
5828 case ISD::FLOG2:
5829 case ISD::FLOG10:
5830 case ISD::FPOWI:
5831 case ISD::FPOW: {
5832 if (SNaN)
5833 return true;
5834 // TODO: Refine on operand
5835 return false;
5836 }
5837 case ISD::FMINNUM:
5838 case ISD::FMAXNUM:
5839 case ISD::FMINIMUMNUM:
5840 case ISD::FMAXIMUMNUM: {
5841 // Only one needs to be known not-nan, since it will be returned if the
5842 // other ends up being one.
5843 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5844 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5845 }
5846 case ISD::FMINNUM_IEEE:
5847 case ISD::FMAXNUM_IEEE: {
5848 if (SNaN)
5849 return true;
5850 // This can return a NaN if either operand is an sNaN, or if both operands
5851 // are NaN.
5852 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5853 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5854 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5855 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5856 }
5857 case ISD::FMINIMUM:
5858 case ISD::FMAXIMUM: {
5859 // TODO: Does this quiet or return the origina NaN as-is?
5860 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5861 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5862 }
5864 SDValue Src = Op.getOperand(0);
5865 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5866 EVT SrcVT = Src.getValueType();
5867 if (SrcVT.isFixedLengthVector() && Idx &&
5868 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5869 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5870 Idx->getZExtValue());
5871 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5872 }
5873 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5874 }
5876 SDValue Src = Op.getOperand(0);
5877 if (Src.getValueType().isFixedLengthVector()) {
5878 unsigned Idx = Op.getConstantOperandVal(1);
5879 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5880 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5881 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5882 }
5883 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5884 }
5885 case ISD::INSERT_SUBVECTOR: {
5886 SDValue BaseVector = Op.getOperand(0);
5887 SDValue SubVector = Op.getOperand(1);
5888 EVT BaseVectorVT = BaseVector.getValueType();
5889 if (BaseVectorVT.isFixedLengthVector()) {
5890 unsigned Idx = Op.getConstantOperandVal(2);
5891 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5892 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5893
5894 // Clear/Extract the bits at the position where the subvector will be
5895 // inserted.
5896 APInt DemandedMask =
5897 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5898 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5899 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5900
5901 bool NeverNaN = true;
5902 if (!DemandedSrcElts.isZero())
5903 NeverNaN &=
5904 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5905 if (NeverNaN && !DemandedSubElts.isZero())
5906 NeverNaN &=
5907 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5908 return NeverNaN;
5909 }
5910 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5911 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5912 }
5913 case ISD::BUILD_VECTOR: {
5914 unsigned NumElts = Op.getNumOperands();
5915 for (unsigned I = 0; I != NumElts; ++I)
5916 if (DemandedElts[I] &&
5917 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
5918 return false;
5919 return true;
5920 }
5921 case ISD::AssertNoFPClass: {
5922 FPClassTest NoFPClass =
5923 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
5924 if ((NoFPClass & fcNan) == fcNan)
5925 return true;
5926 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
5927 return true;
5928 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5929 }
5930 default:
5931 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5932 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
5933 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
5934 Depth);
5935 }
5936
5937 return false;
5938 }
5939}
5940
5942 assert(Op.getValueType().isFloatingPoint() &&
5943 "Floating point type expected");
5944
5945 // If the value is a constant, we can obviously see if it is a zero or not.
5947 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5948}
5949
5951 if (Depth >= MaxRecursionDepth)
5952 return false; // Limit search depth.
5953
5954 assert(!Op.getValueType().isFloatingPoint() &&
5955 "Floating point types unsupported - use isKnownNeverZeroFloat");
5956
5957 // If the value is a constant, we can obviously see if it is a zero or not.
5959 [](ConstantSDNode *C) { return !C->isZero(); }))
5960 return true;
5961
5962 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5963 // some degree.
5964 switch (Op.getOpcode()) {
5965 default:
5966 break;
5967
5968 case ISD::OR:
5969 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5970 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5971
5972 case ISD::VSELECT:
5973 case ISD::SELECT:
5974 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5975 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5976
5977 case ISD::SHL: {
5978 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5979 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5980 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5981 // 1 << X is never zero.
5982 if (ValKnown.One[0])
5983 return true;
5984 // If max shift cnt of known ones is non-zero, result is non-zero.
5985 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
5986 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
5987 !ValKnown.One.shl(MaxCnt).isZero())
5988 return true;
5989 break;
5990 }
5991 case ISD::UADDSAT:
5992 case ISD::UMAX:
5993 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5994 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5995
5996 // For smin/smax: If either operand is known negative/positive
5997 // respectively we don't need the other to be known at all.
5998 case ISD::SMAX: {
5999 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6000 if (Op1.isStrictlyPositive())
6001 return true;
6002
6003 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6004 if (Op0.isStrictlyPositive())
6005 return true;
6006
6007 if (Op1.isNonZero() && Op0.isNonZero())
6008 return true;
6009
6010 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6011 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6012 }
6013 case ISD::SMIN: {
6014 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6015 if (Op1.isNegative())
6016 return true;
6017
6018 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6019 if (Op0.isNegative())
6020 return true;
6021
6022 if (Op1.isNonZero() && Op0.isNonZero())
6023 return true;
6024
6025 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6026 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6027 }
6028 case ISD::UMIN:
6029 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6030 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6031
6032 case ISD::ROTL:
6033 case ISD::ROTR:
6034 case ISD::BITREVERSE:
6035 case ISD::BSWAP:
6036 case ISD::CTPOP:
6037 case ISD::ABS:
6038 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6039
6040 case ISD::SRA:
6041 case ISD::SRL: {
6042 if (Op->getFlags().hasExact())
6043 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6044 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6045 if (ValKnown.isNegative())
6046 return true;
6047 // If max shift cnt of known ones is non-zero, result is non-zero.
6048 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6049 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6050 !ValKnown.One.lshr(MaxCnt).isZero())
6051 return true;
6052 break;
6053 }
6054 case ISD::UDIV:
6055 case ISD::SDIV:
6056 // div exact can only produce a zero if the dividend is zero.
6057 // TODO: For udiv this is also true if Op1 u<= Op0
6058 if (Op->getFlags().hasExact())
6059 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6060 break;
6061
6062 case ISD::ADD:
6063 if (Op->getFlags().hasNoUnsignedWrap())
6064 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6065 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6066 return true;
6067 // TODO: There are a lot more cases we can prove for add.
6068 break;
6069
6070 case ISD::SUB: {
6071 if (isNullConstant(Op.getOperand(0)))
6072 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6073
6074 std::optional<bool> ne =
6075 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6076 computeKnownBits(Op.getOperand(1), Depth + 1));
6077 return ne && *ne;
6078 }
6079
6080 case ISD::MUL:
6081 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6082 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6083 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6084 return true;
6085 break;
6086
6087 case ISD::ZERO_EXTEND:
6088 case ISD::SIGN_EXTEND:
6089 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6090 case ISD::VSCALE: {
6092 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6093 ConstantRange CR =
6094 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6095 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6096 return true;
6097 break;
6098 }
6099 }
6100
6102}
6103
6105 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6106 return !C1->isNegative();
6107
6108 switch (Op.getOpcode()) {
6109 case ISD::FABS:
6110 case ISD::FEXP:
6111 case ISD::FEXP2:
6112 case ISD::FEXP10:
6113 return true;
6114 default:
6115 return false;
6116 }
6117
6118 llvm_unreachable("covered opcode switch");
6119}
6120
6122 // Check the obvious case.
6123 if (A == B) return true;
6124
6125 // For negative and positive zero.
6128 if (CA->isZero() && CB->isZero()) return true;
6129
6130 // Otherwise they may not be equal.
6131 return false;
6132}
6133
6134// Only bits set in Mask must be negated, other bits may be arbitrary.
6136 if (isBitwiseNot(V, AllowUndefs))
6137 return V.getOperand(0);
6138
6139 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6140 // bits in the non-extended part.
6141 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6142 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6143 return SDValue();
6144 SDValue ExtArg = V.getOperand(0);
6145 if (ExtArg.getScalarValueSizeInBits() >=
6146 MaskC->getAPIntValue().getActiveBits() &&
6147 isBitwiseNot(ExtArg, AllowUndefs) &&
6148 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6149 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6150 return ExtArg.getOperand(0).getOperand(0);
6151 return SDValue();
6152}
6153
6155 // Match masked merge pattern (X & ~M) op (Y & M)
6156 // Including degenerate case (X & ~M) op M
6157 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6158 SDValue Other) {
6159 if (SDValue NotOperand =
6160 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6161 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6162 NotOperand->getOpcode() == ISD::TRUNCATE)
6163 NotOperand = NotOperand->getOperand(0);
6164
6165 if (Other == NotOperand)
6166 return true;
6167 if (Other->getOpcode() == ISD::AND)
6168 return NotOperand == Other->getOperand(0) ||
6169 NotOperand == Other->getOperand(1);
6170 }
6171 return false;
6172 };
6173
6174 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6175 A = A->getOperand(0);
6176
6177 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6178 B = B->getOperand(0);
6179
6180 if (A->getOpcode() == ISD::AND)
6181 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6182 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6183 return false;
6184}
6185
6186// FIXME: unify with llvm::haveNoCommonBitsSet.
6188 assert(A.getValueType() == B.getValueType() &&
6189 "Values must have the same type");
6192 return true;
6195}
6196
6197static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6198 SelectionDAG &DAG) {
6199 if (cast<ConstantSDNode>(Step)->isZero())
6200 return DAG.getConstant(0, DL, VT);
6201
6202 return SDValue();
6203}
6204
6207 SelectionDAG &DAG) {
6208 int NumOps = Ops.size();
6209 assert(NumOps != 0 && "Can't build an empty vector!");
6210 assert(!VT.isScalableVector() &&
6211 "BUILD_VECTOR cannot be used with scalable types");
6212 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6213 "Incorrect element count in BUILD_VECTOR!");
6214
6215 // BUILD_VECTOR of UNDEFs is UNDEF.
6216 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6217 return DAG.getUNDEF(VT);
6218
6219 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6220 SDValue IdentitySrc;
6221 bool IsIdentity = true;
6222 for (int i = 0; i != NumOps; ++i) {
6224 Ops[i].getOperand(0).getValueType() != VT ||
6225 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6226 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6227 Ops[i].getConstantOperandAPInt(1) != i) {
6228 IsIdentity = false;
6229 break;
6230 }
6231 IdentitySrc = Ops[i].getOperand(0);
6232 }
6233 if (IsIdentity)
6234 return IdentitySrc;
6235
6236 return SDValue();
6237}
6238
6239/// Try to simplify vector concatenation to an input value, undef, or build
6240/// vector.
6243 SelectionDAG &DAG) {
6244 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6246 [Ops](SDValue Op) {
6247 return Ops[0].getValueType() == Op.getValueType();
6248 }) &&
6249 "Concatenation of vectors with inconsistent value types!");
6250 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6251 VT.getVectorElementCount() &&
6252 "Incorrect element count in vector concatenation!");
6253
6254 if (Ops.size() == 1)
6255 return Ops[0];
6256
6257 // Concat of UNDEFs is UNDEF.
6258 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6259 return DAG.getUNDEF(VT);
6260
6261 // Scan the operands and look for extract operations from a single source
6262 // that correspond to insertion at the same location via this concatenation:
6263 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6264 SDValue IdentitySrc;
6265 bool IsIdentity = true;
6266 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6267 SDValue Op = Ops[i];
6268 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6269 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6270 Op.getOperand(0).getValueType() != VT ||
6271 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6272 Op.getConstantOperandVal(1) != IdentityIndex) {
6273 IsIdentity = false;
6274 break;
6275 }
6276 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6277 "Unexpected identity source vector for concat of extracts");
6278 IdentitySrc = Op.getOperand(0);
6279 }
6280 if (IsIdentity) {
6281 assert(IdentitySrc && "Failed to set source vector of extracts");
6282 return IdentitySrc;
6283 }
6284
6285 // The code below this point is only designed to work for fixed width
6286 // vectors, so we bail out for now.
6287 if (VT.isScalableVector())
6288 return SDValue();
6289
6290 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6291 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6292 // BUILD_VECTOR.
6293 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6294 EVT SVT = VT.getScalarType();
6296 for (SDValue Op : Ops) {
6297 EVT OpVT = Op.getValueType();
6298 if (Op.isUndef())
6299 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6300 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6301 Elts.append(Op->op_begin(), Op->op_end());
6302 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6303 OpVT.getVectorNumElements() == 1 &&
6304 isNullConstant(Op.getOperand(2)))
6305 Elts.push_back(Op.getOperand(1));
6306 else
6307 return SDValue();
6308 }
6309
6310 // BUILD_VECTOR requires all inputs to be of the same type, find the
6311 // maximum type and extend them all.
6312 for (SDValue Op : Elts)
6313 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6314
6315 if (SVT.bitsGT(VT.getScalarType())) {
6316 for (SDValue &Op : Elts) {
6317 if (Op.isUndef())
6318 Op = DAG.getUNDEF(SVT);
6319 else
6320 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6321 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6322 : DAG.getSExtOrTrunc(Op, DL, SVT);
6323 }
6324 }
6325
6326 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6327 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6328 return V;
6329}
6330
6331/// Gets or creates the specified node.
6332SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6333 SDVTList VTs = getVTList(VT);
6335 AddNodeIDNode(ID, Opcode, VTs, {});
6336 void *IP = nullptr;
6337 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6338 return SDValue(E, 0);
6339
6340 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6341 CSEMap.InsertNode(N, IP);
6342
6343 InsertNode(N);
6344 SDValue V = SDValue(N, 0);
6345 NewSDValueDbgMsg(V, "Creating new node: ", this);
6346 return V;
6347}
6348
6349SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6350 SDValue N1) {
6351 SDNodeFlags Flags;
6352 if (Inserter)
6353 Flags = Inserter->getFlags();
6354 return getNode(Opcode, DL, VT, N1, Flags);
6355}
6356
6357SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6358 SDValue N1, const SDNodeFlags Flags) {
6359 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6360
6361 // Constant fold unary operations with a vector integer or float operand.
6362 switch (Opcode) {
6363 default:
6364 // FIXME: Entirely reasonable to perform folding of other unary
6365 // operations here as the need arises.
6366 break;
6367 case ISD::FNEG:
6368 case ISD::FABS:
6369 case ISD::FCEIL:
6370 case ISD::FTRUNC:
6371 case ISD::FFLOOR:
6372 case ISD::FP_EXTEND:
6373 case ISD::FP_TO_SINT:
6374 case ISD::FP_TO_UINT:
6375 case ISD::FP_TO_FP16:
6376 case ISD::FP_TO_BF16:
6377 case ISD::TRUNCATE:
6378 case ISD::ANY_EXTEND:
6379 case ISD::ZERO_EXTEND:
6380 case ISD::SIGN_EXTEND:
6381 case ISD::UINT_TO_FP:
6382 case ISD::SINT_TO_FP:
6383 case ISD::FP16_TO_FP:
6384 case ISD::BF16_TO_FP:
6385 case ISD::BITCAST:
6386 case ISD::ABS:
6387 case ISD::BITREVERSE:
6388 case ISD::BSWAP:
6389 case ISD::CTLZ:
6391 case ISD::CTTZ:
6393 case ISD::CTPOP:
6394 case ISD::STEP_VECTOR: {
6395 SDValue Ops = {N1};
6396 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6397 return Fold;
6398 }
6399 }
6400
6401 unsigned OpOpcode = N1.getNode()->getOpcode();
6402 switch (Opcode) {
6403 case ISD::STEP_VECTOR:
6404 assert(VT.isScalableVector() &&
6405 "STEP_VECTOR can only be used with scalable types");
6406 assert(OpOpcode == ISD::TargetConstant &&
6407 VT.getVectorElementType() == N1.getValueType() &&
6408 "Unexpected step operand");
6409 break;
6410 case ISD::FREEZE:
6411 assert(VT == N1.getValueType() && "Unexpected VT!");
6412 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6413 return N1;
6414 break;
6415 case ISD::TokenFactor:
6416 case ISD::MERGE_VALUES:
6418 return N1; // Factor, merge or concat of one node? No need.
6419 case ISD::BUILD_VECTOR: {
6420 // Attempt to simplify BUILD_VECTOR.
6421 SDValue Ops[] = {N1};
6422 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6423 return V;
6424 break;
6425 }
6426 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6427 case ISD::FP_EXTEND:
6429 "Invalid FP cast!");
6430 if (N1.getValueType() == VT) return N1; // noop conversion.
6431 assert((!VT.isVector() || VT.getVectorElementCount() ==
6433 "Vector element count mismatch!");
6434 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6435 if (N1.isUndef())
6436 return getUNDEF(VT);
6437 break;
6438 case ISD::FP_TO_SINT:
6439 case ISD::FP_TO_UINT:
6440 if (N1.isUndef())
6441 return getUNDEF(VT);
6442 break;
6443 case ISD::SINT_TO_FP:
6444 case ISD::UINT_TO_FP:
6445 // [us]itofp(undef) = 0, because the result value is bounded.
6446 if (N1.isUndef())
6447 return getConstantFP(0.0, DL, VT);
6448 break;
6449 case ISD::SIGN_EXTEND:
6450 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6451 "Invalid SIGN_EXTEND!");
6452 assert(VT.isVector() == N1.getValueType().isVector() &&
6453 "SIGN_EXTEND result type type should be vector iff the operand "
6454 "type is vector!");
6455 if (N1.getValueType() == VT) return N1; // noop extension
6456 assert((!VT.isVector() || VT.getVectorElementCount() ==
6458 "Vector element count mismatch!");
6459 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6460 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6461 SDNodeFlags Flags;
6462 if (OpOpcode == ISD::ZERO_EXTEND)
6463 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6464 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6465 transferDbgValues(N1, NewVal);
6466 return NewVal;
6467 }
6468
6469 if (OpOpcode == ISD::POISON)
6470 return getPOISON(VT);
6471
6472 if (N1.isUndef())
6473 // sext(undef) = 0, because the top bits will all be the same.
6474 return getConstant(0, DL, VT);
6475
6476 // Skip unnecessary sext_inreg pattern:
6477 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6478 if (OpOpcode == ISD::TRUNCATE) {
6479 SDValue OpOp = N1.getOperand(0);
6480 if (OpOp.getValueType() == VT) {
6481 unsigned NumSignExtBits =
6483 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6484 transferDbgValues(N1, OpOp);
6485 return OpOp;
6486 }
6487 }
6488 }
6489 break;
6490 case ISD::ZERO_EXTEND:
6491 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6492 "Invalid ZERO_EXTEND!");
6493 assert(VT.isVector() == N1.getValueType().isVector() &&
6494 "ZERO_EXTEND result type type should be vector iff the operand "
6495 "type is vector!");
6496 if (N1.getValueType() == VT) return N1; // noop extension
6497 assert((!VT.isVector() || VT.getVectorElementCount() ==
6499 "Vector element count mismatch!");
6500 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6501 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6502 SDNodeFlags Flags;
6503 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6504 SDValue NewVal =
6505 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6506 transferDbgValues(N1, NewVal);
6507 return NewVal;
6508 }
6509
6510 if (OpOpcode == ISD::POISON)
6511 return getPOISON(VT);
6512
6513 if (N1.isUndef())
6514 // zext(undef) = 0, because the top bits will be zero.
6515 return getConstant(0, DL, VT);
6516
6517 // Skip unnecessary zext_inreg pattern:
6518 // (zext (trunc x)) -> x iff the upper bits are known zero.
6519 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6520 // use to recognise zext_inreg patterns.
6521 if (OpOpcode == ISD::TRUNCATE) {
6522 SDValue OpOp = N1.getOperand(0);
6523 if (OpOp.getValueType() == VT) {
6524 if (OpOp.getOpcode() != ISD::AND) {
6527 if (MaskedValueIsZero(OpOp, HiBits)) {
6528 transferDbgValues(N1, OpOp);
6529 return OpOp;
6530 }
6531 }
6532 }
6533 }
6534 break;
6535 case ISD::ANY_EXTEND:
6536 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6537 "Invalid ANY_EXTEND!");
6538 assert(VT.isVector() == N1.getValueType().isVector() &&
6539 "ANY_EXTEND result type type should be vector iff the operand "
6540 "type is vector!");
6541 if (N1.getValueType() == VT) return N1; // noop extension
6542 assert((!VT.isVector() || VT.getVectorElementCount() ==
6544 "Vector element count mismatch!");
6545 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6546
6547 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6548 OpOpcode == ISD::ANY_EXTEND) {
6549 SDNodeFlags Flags;
6550 if (OpOpcode == ISD::ZERO_EXTEND)
6551 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6552 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6553 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6554 }
6555 if (N1.isUndef())
6556 return getUNDEF(VT);
6557
6558 // (ext (trunc x)) -> x
6559 if (OpOpcode == ISD::TRUNCATE) {
6560 SDValue OpOp = N1.getOperand(0);
6561 if (OpOp.getValueType() == VT) {
6562 transferDbgValues(N1, OpOp);
6563 return OpOp;
6564 }
6565 }
6566 break;
6567 case ISD::TRUNCATE:
6568 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6569 "Invalid TRUNCATE!");
6570 assert(VT.isVector() == N1.getValueType().isVector() &&
6571 "TRUNCATE result type type should be vector iff the operand "
6572 "type is vector!");
6573 if (N1.getValueType() == VT) return N1; // noop truncate
6574 assert((!VT.isVector() || VT.getVectorElementCount() ==
6576 "Vector element count mismatch!");
6577 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6578 if (OpOpcode == ISD::TRUNCATE)
6579 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6580 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6581 OpOpcode == ISD::ANY_EXTEND) {
6582 // If the source is smaller than the dest, we still need an extend.
6584 VT.getScalarType())) {
6585 SDNodeFlags Flags;
6586 if (OpOpcode == ISD::ZERO_EXTEND)
6587 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6588 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6589 }
6590 if (N1.getOperand(0).getValueType().bitsGT(VT))
6591 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6592 return N1.getOperand(0);
6593 }
6594 if (N1.isUndef())
6595 return getUNDEF(VT);
6596 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6597 return getVScale(DL, VT,
6599 break;
6603 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6604 assert(N1.getValueType().bitsLE(VT) &&
6605 "The input must be the same size or smaller than the result.");
6608 "The destination vector type must have fewer lanes than the input.");
6609 break;
6610 case ISD::ABS:
6611 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6612 if (N1.isUndef())
6613 return getConstant(0, DL, VT);
6614 break;
6615 case ISD::BSWAP:
6616 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6617 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6618 "BSWAP types must be a multiple of 16 bits!");
6619 if (N1.isUndef())
6620 return getUNDEF(VT);
6621 // bswap(bswap(X)) -> X.
6622 if (OpOpcode == ISD::BSWAP)
6623 return N1.getOperand(0);
6624 break;
6625 case ISD::BITREVERSE:
6626 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6627 if (N1.isUndef())
6628 return getUNDEF(VT);
6629 break;
6630 case ISD::BITCAST:
6632 "Cannot BITCAST between types of different sizes!");
6633 if (VT == N1.getValueType()) return N1; // noop conversion.
6634 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6635 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6636 if (N1.isUndef())
6637 return getUNDEF(VT);
6638 break;
6640 assert(VT.isVector() && !N1.getValueType().isVector() &&
6641 (VT.getVectorElementType() == N1.getValueType() ||
6643 N1.getValueType().isInteger() &&
6645 "Illegal SCALAR_TO_VECTOR node!");
6646 if (N1.isUndef())
6647 return getUNDEF(VT);
6648 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6649 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6651 N1.getConstantOperandVal(1) == 0 &&
6652 N1.getOperand(0).getValueType() == VT)
6653 return N1.getOperand(0);
6654 break;
6655 case ISD::FNEG:
6656 // Negation of an unknown bag of bits is still completely undefined.
6657 if (N1.isUndef())
6658 return getUNDEF(VT);
6659
6660 if (OpOpcode == ISD::FNEG) // --X -> X
6661 return N1.getOperand(0);
6662 break;
6663 case ISD::FABS:
6664 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6665 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6666 break;
6667 case ISD::VSCALE:
6668 assert(VT == N1.getValueType() && "Unexpected VT!");
6669 break;
6670 case ISD::CTPOP:
6671 if (N1.getValueType().getScalarType() == MVT::i1)
6672 return N1;
6673 break;
6674 case ISD::CTLZ:
6675 case ISD::CTTZ:
6676 if (N1.getValueType().getScalarType() == MVT::i1)
6677 return getNOT(DL, N1, N1.getValueType());
6678 break;
6679 case ISD::VECREDUCE_ADD:
6680 if (N1.getValueType().getScalarType() == MVT::i1)
6681 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6682 break;
6683 case ISD::VECREDUCE_SMIN:
6684 case ISD::VECREDUCE_UMAX:
6685 if (N1.getValueType().getScalarType() == MVT::i1)
6686 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6687 break;
6688 case ISD::VECREDUCE_SMAX:
6689 case ISD::VECREDUCE_UMIN:
6690 if (N1.getValueType().getScalarType() == MVT::i1)
6691 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6692 break;
6693 case ISD::SPLAT_VECTOR:
6694 assert(VT.isVector() && "Wrong return type!");
6695 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6696 // that for now.
6698 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6700 N1.getValueType().isInteger() &&
6702 "Wrong operand type!");
6703 break;
6704 }
6705
6706 SDNode *N;
6707 SDVTList VTs = getVTList(VT);
6708 SDValue Ops[] = {N1};
6709 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6711 AddNodeIDNode(ID, Opcode, VTs, Ops);
6712 void *IP = nullptr;
6713 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6714 E->intersectFlagsWith(Flags);
6715 return SDValue(E, 0);
6716 }
6717
6718 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6719 N->setFlags(Flags);
6720 createOperands(N, Ops);
6721 CSEMap.InsertNode(N, IP);
6722 } else {
6723 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6724 createOperands(N, Ops);
6725 }
6726
6727 InsertNode(N);
6728 SDValue V = SDValue(N, 0);
6729 NewSDValueDbgMsg(V, "Creating new node: ", this);
6730 return V;
6731}
6732
6733static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6734 const APInt &C2) {
6735 switch (Opcode) {
6736 case ISD::ADD: return C1 + C2;
6737 case ISD::SUB: return C1 - C2;
6738 case ISD::MUL: return C1 * C2;
6739 case ISD::AND: return C1 & C2;
6740 case ISD::OR: return C1 | C2;
6741 case ISD::XOR: return C1 ^ C2;
6742 case ISD::SHL: return C1 << C2;
6743 case ISD::SRL: return C1.lshr(C2);
6744 case ISD::SRA: return C1.ashr(C2);
6745 case ISD::ROTL: return C1.rotl(C2);
6746 case ISD::ROTR: return C1.rotr(C2);
6747 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6748 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6749 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6750 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6751 case ISD::SADDSAT: return C1.sadd_sat(C2);
6752 case ISD::UADDSAT: return C1.uadd_sat(C2);
6753 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6754 case ISD::USUBSAT: return C1.usub_sat(C2);
6755 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6756 case ISD::USHLSAT: return C1.ushl_sat(C2);
6757 case ISD::UDIV:
6758 if (!C2.getBoolValue())
6759 break;
6760 return C1.udiv(C2);
6761 case ISD::UREM:
6762 if (!C2.getBoolValue())
6763 break;
6764 return C1.urem(C2);
6765 case ISD::SDIV:
6766 if (!C2.getBoolValue())
6767 break;
6768 return C1.sdiv(C2);
6769 case ISD::SREM:
6770 if (!C2.getBoolValue())
6771 break;
6772 return C1.srem(C2);
6773 case ISD::AVGFLOORS:
6774 return APIntOps::avgFloorS(C1, C2);
6775 case ISD::AVGFLOORU:
6776 return APIntOps::avgFloorU(C1, C2);
6777 case ISD::AVGCEILS:
6778 return APIntOps::avgCeilS(C1, C2);
6779 case ISD::AVGCEILU:
6780 return APIntOps::avgCeilU(C1, C2);
6781 case ISD::ABDS:
6782 return APIntOps::abds(C1, C2);
6783 case ISD::ABDU:
6784 return APIntOps::abdu(C1, C2);
6785 case ISD::MULHS:
6786 return APIntOps::mulhs(C1, C2);
6787 case ISD::MULHU:
6788 return APIntOps::mulhu(C1, C2);
6789 }
6790 return std::nullopt;
6791}
6792// Handle constant folding with UNDEF.
6793// TODO: Handle more cases.
6794static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6795 bool IsUndef1, const APInt &C2,
6796 bool IsUndef2) {
6797 if (!(IsUndef1 || IsUndef2))
6798 return FoldValue(Opcode, C1, C2);
6799
6800 // Fold and(x, undef) -> 0
6801 // Fold mul(x, undef) -> 0
6802 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6803 return APInt::getZero(C1.getBitWidth());
6804
6805 return std::nullopt;
6806}
6807
6809 const GlobalAddressSDNode *GA,
6810 const SDNode *N2) {
6811 if (GA->getOpcode() != ISD::GlobalAddress)
6812 return SDValue();
6813 if (!TLI->isOffsetFoldingLegal(GA))
6814 return SDValue();
6815 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6816 if (!C2)
6817 return SDValue();
6818 int64_t Offset = C2->getSExtValue();
6819 switch (Opcode) {
6820 case ISD::ADD:
6821 case ISD::PTRADD:
6822 break;
6823 case ISD::SUB: Offset = -uint64_t(Offset); break;
6824 default: return SDValue();
6825 }
6826 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6827 GA->getOffset() + uint64_t(Offset));
6828}
6829
6831 switch (Opcode) {
6832 case ISD::SDIV:
6833 case ISD::UDIV:
6834 case ISD::SREM:
6835 case ISD::UREM: {
6836 // If a divisor is zero/undef or any element of a divisor vector is
6837 // zero/undef, the whole op is undef.
6838 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6839 SDValue Divisor = Ops[1];
6840 if (Divisor.isUndef() || isNullConstant(Divisor))
6841 return true;
6842
6843 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6844 llvm::any_of(Divisor->op_values(),
6845 [](SDValue V) { return V.isUndef() ||
6846 isNullConstant(V); });
6847 // TODO: Handle signed overflow.
6848 }
6849 // TODO: Handle oversized shifts.
6850 default:
6851 return false;
6852 }
6853}
6854
6857 SDNodeFlags Flags) {
6858 // If the opcode is a target-specific ISD node, there's nothing we can
6859 // do here and the operand rules may not line up with the below, so
6860 // bail early.
6861 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6862 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6863 // foldCONCAT_VECTORS in getNode before this is called.
6864 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6865 return SDValue();
6866
6867 unsigned NumOps = Ops.size();
6868 if (NumOps == 0)
6869 return SDValue();
6870
6871 if (isUndef(Opcode, Ops))
6872 return getUNDEF(VT);
6873
6874 // Handle unary special cases.
6875 if (NumOps == 1) {
6876 SDValue N1 = Ops[0];
6877
6878 // Constant fold unary operations with an integer constant operand. Even
6879 // opaque constant will be folded, because the folding of unary operations
6880 // doesn't create new constants with different values. Nevertheless, the
6881 // opaque flag is preserved during folding to prevent future folding with
6882 // other constants.
6883 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6884 const APInt &Val = C->getAPIntValue();
6885 switch (Opcode) {
6886 case ISD::SIGN_EXTEND:
6887 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6888 C->isTargetOpcode(), C->isOpaque());
6889 case ISD::TRUNCATE:
6890 if (C->isOpaque())
6891 break;
6892 [[fallthrough]];
6893 case ISD::ZERO_EXTEND:
6894 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6895 C->isTargetOpcode(), C->isOpaque());
6896 case ISD::ANY_EXTEND:
6897 // Some targets like RISCV prefer to sign extend some types.
6898 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6899 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6900 C->isTargetOpcode(), C->isOpaque());
6901 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6902 C->isTargetOpcode(), C->isOpaque());
6903 case ISD::ABS:
6904 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6905 C->isOpaque());
6906 case ISD::BITREVERSE:
6907 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6908 C->isOpaque());
6909 case ISD::BSWAP:
6910 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6911 C->isOpaque());
6912 case ISD::CTPOP:
6913 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6914 C->isOpaque());
6915 case ISD::CTLZ:
6917 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6918 C->isOpaque());
6919 case ISD::CTTZ:
6921 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6922 C->isOpaque());
6923 case ISD::UINT_TO_FP:
6924 case ISD::SINT_TO_FP: {
6926 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6928 return getConstantFP(FPV, DL, VT);
6929 }
6930 case ISD::FP16_TO_FP:
6931 case ISD::BF16_TO_FP: {
6932 bool Ignored;
6933 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6934 : APFloat::BFloat(),
6935 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6936
6937 // This can return overflow, underflow, or inexact; we don't care.
6938 // FIXME need to be more flexible about rounding mode.
6940 &Ignored);
6941 return getConstantFP(FPV, DL, VT);
6942 }
6943 case ISD::STEP_VECTOR:
6944 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6945 return V;
6946 break;
6947 case ISD::BITCAST:
6948 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6949 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6950 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6951 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6952 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6953 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6954 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6955 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6956 break;
6957 }
6958 }
6959
6960 // Constant fold unary operations with a floating point constant operand.
6961 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6962 APFloat V = C->getValueAPF(); // make copy
6963 switch (Opcode) {
6964 case ISD::FNEG:
6965 V.changeSign();
6966 return getConstantFP(V, DL, VT);
6967 case ISD::FABS:
6968 V.clearSign();
6969 return getConstantFP(V, DL, VT);
6970 case ISD::FCEIL: {
6971 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6973 return getConstantFP(V, DL, VT);
6974 return SDValue();
6975 }
6976 case ISD::FTRUNC: {
6977 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6979 return getConstantFP(V, DL, VT);
6980 return SDValue();
6981 }
6982 case ISD::FFLOOR: {
6983 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
6985 return getConstantFP(V, DL, VT);
6986 return SDValue();
6987 }
6988 case ISD::FP_EXTEND: {
6989 bool ignored;
6990 // This can return overflow, underflow, or inexact; we don't care.
6991 // FIXME need to be more flexible about rounding mode.
6992 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
6993 &ignored);
6994 return getConstantFP(V, DL, VT);
6995 }
6996 case ISD::FP_TO_SINT:
6997 case ISD::FP_TO_UINT: {
6998 bool ignored;
6999 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7000 // FIXME need to be more flexible about rounding mode.
7002 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7003 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7004 break;
7005 return getConstant(IntVal, DL, VT);
7006 }
7007 case ISD::FP_TO_FP16:
7008 case ISD::FP_TO_BF16: {
7009 bool Ignored;
7010 // This can return overflow, underflow, or inexact; we don't care.
7011 // FIXME need to be more flexible about rounding mode.
7012 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7013 : APFloat::BFloat(),
7015 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7016 }
7017 case ISD::BITCAST:
7018 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7019 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7020 VT);
7021 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7022 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7023 VT);
7024 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7025 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7026 VT);
7027 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7028 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7029 break;
7030 }
7031 }
7032
7033 // Early-out if we failed to constant fold a bitcast.
7034 if (Opcode == ISD::BITCAST)
7035 return SDValue();
7036 }
7037
7038 // Handle binops special cases.
7039 if (NumOps == 2) {
7040 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7041 return CFP;
7042
7043 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7044 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7045 if (C1->isOpaque() || C2->isOpaque())
7046 return SDValue();
7047
7048 std::optional<APInt> FoldAttempt =
7049 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7050 if (!FoldAttempt)
7051 return SDValue();
7052
7053 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7054 assert((!Folded || !VT.isVector()) &&
7055 "Can't fold vectors ops with scalar operands");
7056 return Folded;
7057 }
7058 }
7059
7060 // fold (add Sym, c) -> Sym+c
7062 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7063 if (TLI->isCommutativeBinOp(Opcode))
7065 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7066
7067 // fold (sext_in_reg c1) -> c2
7068 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7069 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7070
7071 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7072 unsigned FromBits = EVT.getScalarSizeInBits();
7073 Val <<= Val.getBitWidth() - FromBits;
7074 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7075 return getConstant(Val, DL, ConstantVT);
7076 };
7077
7078 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7079 const APInt &Val = C1->getAPIntValue();
7080 return SignExtendInReg(Val, VT);
7081 }
7082
7084 SmallVector<SDValue, 8> ScalarOps;
7085 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7086 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7087 SDValue Op = Ops[0].getOperand(I);
7088 if (Op.isUndef()) {
7089 ScalarOps.push_back(getUNDEF(OpVT));
7090 continue;
7091 }
7092 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7093 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7094 }
7095 return getBuildVector(VT, DL, ScalarOps);
7096 }
7097
7098 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7099 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7100 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7101 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7102 Ops[0].getOperand(0).getValueType()));
7103 }
7104 }
7105
7106 // Handle fshl/fshr special cases.
7107 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7108 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7109 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7110 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7111
7112 if (C1 && C2 && C3) {
7113 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7114 return SDValue();
7115 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7116 &V3 = C3->getAPIntValue();
7117
7118 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7119 : APIntOps::fshr(V1, V2, V3);
7120 return getConstant(FoldedVal, DL, VT);
7121 }
7122 }
7123
7124 // Handle fma/fmad special cases.
7125 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7126 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7127 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7128 Ops[2].getValueType() == VT && "FMA types must match!");
7132 if (C1 && C2 && C3) {
7133 APFloat V1 = C1->getValueAPF();
7134 const APFloat &V2 = C2->getValueAPF();
7135 const APFloat &V3 = C3->getValueAPF();
7136 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7139 } else
7141 return getConstantFP(V1, DL, VT);
7142 }
7143 }
7144
7145 // This is for vector folding only from here on.
7146 if (!VT.isVector())
7147 return SDValue();
7148
7149 ElementCount NumElts = VT.getVectorElementCount();
7150
7151 // See if we can fold through any bitcasted integer ops.
7152 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7153 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7154 (Ops[0].getOpcode() == ISD::BITCAST ||
7155 Ops[1].getOpcode() == ISD::BITCAST)) {
7158 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7159 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7160 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7161 N2.getValueType().isInteger()) {
7162 bool IsLE = getDataLayout().isLittleEndian();
7163 unsigned EltBits = VT.getScalarSizeInBits();
7164 SmallVector<APInt> RawBits1, RawBits2;
7165 BitVector UndefElts1, UndefElts2;
7166 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7167 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7168 SmallVector<APInt> RawBits;
7169 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7170 std::optional<APInt> Fold = FoldValueWithUndef(
7171 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7172 if (!Fold)
7173 break;
7174 RawBits.push_back(*Fold);
7175 }
7176 if (RawBits.size() == NumElts.getFixedValue()) {
7177 // We have constant folded, but we might need to cast this again back
7178 // to the original (possibly legalized) type.
7179 EVT BVVT, BVEltVT;
7180 if (N1.getValueType() == VT) {
7181 BVVT = N1.getValueType();
7182 BVEltVT = BV1->getOperand(0).getValueType();
7183 } else {
7184 BVVT = N2.getValueType();
7185 BVEltVT = BV2->getOperand(0).getValueType();
7186 }
7187 unsigned BVEltBits = BVEltVT.getSizeInBits();
7188 SmallVector<APInt> DstBits;
7189 BitVector DstUndefs;
7191 DstBits, RawBits, DstUndefs,
7192 BitVector(RawBits.size(), false));
7193 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7194 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7195 if (DstUndefs[I])
7196 continue;
7197 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7198 }
7199 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7200 }
7201 }
7202 }
7203 }
7204
7205 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7206 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7207 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7208 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7209 APInt RHSVal;
7210 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7211 APInt NewStep = Opcode == ISD::MUL
7212 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7213 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7214 return getStepVector(DL, VT, NewStep);
7215 }
7216 }
7217
7218 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7219 return !Op.getValueType().isVector() ||
7220 Op.getValueType().getVectorElementCount() == NumElts;
7221 };
7222
7223 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7224 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7225 Op.getOpcode() == ISD::BUILD_VECTOR ||
7226 Op.getOpcode() == ISD::SPLAT_VECTOR;
7227 };
7228
7229 // All operands must be vector types with the same number of elements as
7230 // the result type and must be either UNDEF or a build/splat vector
7231 // or UNDEF scalars.
7232 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7233 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7234 return SDValue();
7235
7236 // If we are comparing vectors, then the result needs to be a i1 boolean that
7237 // is then extended back to the legal result type depending on how booleans
7238 // are represented.
7239 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7240 ISD::NodeType ExtendCode =
7241 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7242 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7244
7245 // Find legal integer scalar type for constant promotion and
7246 // ensure that its scalar size is at least as large as source.
7247 EVT LegalSVT = VT.getScalarType();
7248 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7249 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7250 if (LegalSVT.bitsLT(VT.getScalarType()))
7251 return SDValue();
7252 }
7253
7254 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7255 // only have one operand to check. For fixed-length vector types we may have
7256 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7257 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7258
7259 // Constant fold each scalar lane separately.
7260 SmallVector<SDValue, 4> ScalarResults;
7261 for (unsigned I = 0; I != NumVectorElts; I++) {
7262 SmallVector<SDValue, 4> ScalarOps;
7263 for (SDValue Op : Ops) {
7264 EVT InSVT = Op.getValueType().getScalarType();
7265 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7266 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7267 if (Op.isUndef())
7268 ScalarOps.push_back(getUNDEF(InSVT));
7269 else
7270 ScalarOps.push_back(Op);
7271 continue;
7272 }
7273
7274 SDValue ScalarOp =
7275 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7276 EVT ScalarVT = ScalarOp.getValueType();
7277
7278 // Build vector (integer) scalar operands may need implicit
7279 // truncation - do this before constant folding.
7280 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7281 // Don't create illegally-typed nodes unless they're constants or undef
7282 // - if we fail to constant fold we can't guarantee the (dead) nodes
7283 // we're creating will be cleaned up before being visited for
7284 // legalization.
7285 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7286 !isa<ConstantSDNode>(ScalarOp) &&
7287 TLI->getTypeAction(*getContext(), InSVT) !=
7289 return SDValue();
7290 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7291 }
7292
7293 ScalarOps.push_back(ScalarOp);
7294 }
7295
7296 // Constant fold the scalar operands.
7297 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7298
7299 // Scalar folding only succeeded if the result is a constant or UNDEF.
7300 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7301 ScalarResult.getOpcode() != ISD::ConstantFP)
7302 return SDValue();
7303
7304 // Legalize the (integer) scalar constant if necessary. We only do
7305 // this once we know the folding succeeded, since otherwise we would
7306 // get a node with illegal type which has a user.
7307 if (LegalSVT != SVT)
7308 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7309
7310 ScalarResults.push_back(ScalarResult);
7311 }
7312
7313 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7314 : getBuildVector(VT, DL, ScalarResults);
7315 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7316 return V;
7317}
7318
7321 // TODO: Add support for unary/ternary fp opcodes.
7322 if (Ops.size() != 2)
7323 return SDValue();
7324
7325 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7326 // should. That will require dealing with a potentially non-default
7327 // rounding mode, checking the "opStatus" return value from the APFloat
7328 // math calculations, and possibly other variations.
7329 SDValue N1 = Ops[0];
7330 SDValue N2 = Ops[1];
7331 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7332 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7333 if (N1CFP && N2CFP) {
7334 APFloat C1 = N1CFP->getValueAPF(); // make copy
7335 const APFloat &C2 = N2CFP->getValueAPF();
7336 switch (Opcode) {
7337 case ISD::FADD:
7339 return getConstantFP(C1, DL, VT);
7340 case ISD::FSUB:
7342 return getConstantFP(C1, DL, VT);
7343 case ISD::FMUL:
7345 return getConstantFP(C1, DL, VT);
7346 case ISD::FDIV:
7348 return getConstantFP(C1, DL, VT);
7349 case ISD::FREM:
7350 C1.mod(C2);
7351 return getConstantFP(C1, DL, VT);
7352 case ISD::FCOPYSIGN:
7353 C1.copySign(C2);
7354 return getConstantFP(C1, DL, VT);
7355 case ISD::FMINNUM:
7356 return getConstantFP(minnum(C1, C2), DL, VT);
7357 case ISD::FMAXNUM:
7358 return getConstantFP(maxnum(C1, C2), DL, VT);
7359 case ISD::FMINIMUM:
7360 return getConstantFP(minimum(C1, C2), DL, VT);
7361 case ISD::FMAXIMUM:
7362 return getConstantFP(maximum(C1, C2), DL, VT);
7363 case ISD::FMINIMUMNUM:
7364 return getConstantFP(minimumnum(C1, C2), DL, VT);
7365 case ISD::FMAXIMUMNUM:
7366 return getConstantFP(maximumnum(C1, C2), DL, VT);
7367 default: break;
7368 }
7369 }
7370 if (N1CFP && Opcode == ISD::FP_ROUND) {
7371 APFloat C1 = N1CFP->getValueAPF(); // make copy
7372 bool Unused;
7373 // This can return overflow, underflow, or inexact; we don't care.
7374 // FIXME need to be more flexible about rounding mode.
7376 &Unused);
7377 return getConstantFP(C1, DL, VT);
7378 }
7379
7380 switch (Opcode) {
7381 case ISD::FSUB:
7382 // -0.0 - undef --> undef (consistent with "fneg undef")
7383 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7384 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7385 return getUNDEF(VT);
7386 [[fallthrough]];
7387
7388 case ISD::FADD:
7389 case ISD::FMUL:
7390 case ISD::FDIV:
7391 case ISD::FREM:
7392 // If both operands are undef, the result is undef. If 1 operand is undef,
7393 // the result is NaN. This should match the behavior of the IR optimizer.
7394 if (N1.isUndef() && N2.isUndef())
7395 return getUNDEF(VT);
7396 if (N1.isUndef() || N2.isUndef())
7398 }
7399 return SDValue();
7400}
7401
7403 const SDLoc &DL, EVT DstEltVT) {
7404 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7405
7406 // If this is already the right type, we're done.
7407 if (SrcEltVT == DstEltVT)
7408 return SDValue(BV, 0);
7409
7410 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7411 unsigned DstBitSize = DstEltVT.getSizeInBits();
7412
7413 // If this is a conversion of N elements of one type to N elements of another
7414 // type, convert each element. This handles FP<->INT cases.
7415 if (SrcBitSize == DstBitSize) {
7417 for (SDValue Op : BV->op_values()) {
7418 // If the vector element type is not legal, the BUILD_VECTOR operands
7419 // are promoted and implicitly truncated. Make that explicit here.
7420 if (Op.getValueType() != SrcEltVT)
7421 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7422 Ops.push_back(getBitcast(DstEltVT, Op));
7423 }
7424 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7426 return getBuildVector(VT, DL, Ops);
7427 }
7428
7429 // Otherwise, we're growing or shrinking the elements. To avoid having to
7430 // handle annoying details of growing/shrinking FP values, we convert them to
7431 // int first.
7432 if (SrcEltVT.isFloatingPoint()) {
7433 // Convert the input float vector to a int vector where the elements are the
7434 // same sizes.
7435 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7436 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7438 DstEltVT);
7439 return SDValue();
7440 }
7441
7442 // Now we know the input is an integer vector. If the output is a FP type,
7443 // convert to integer first, then to FP of the right size.
7444 if (DstEltVT.isFloatingPoint()) {
7445 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7446 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7448 DstEltVT);
7449 return SDValue();
7450 }
7451
7452 // Okay, we know the src/dst types are both integers of differing types.
7453 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7454
7455 // Extract the constant raw bit data.
7456 BitVector UndefElements;
7457 SmallVector<APInt> RawBits;
7458 bool IsLE = getDataLayout().isLittleEndian();
7459 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7460 return SDValue();
7461
7463 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7464 if (UndefElements[I])
7465 Ops.push_back(getUNDEF(DstEltVT));
7466 else
7467 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7468 }
7469
7470 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7471 return getBuildVector(VT, DL, Ops);
7472}
7473
7475 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7476
7477 // There's no need to assert on a byte-aligned pointer. All pointers are at
7478 // least byte aligned.
7479 if (A == Align(1))
7480 return Val;
7481
7482 SDVTList VTs = getVTList(Val.getValueType());
7484 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7485 ID.AddInteger(A.value());
7486
7487 void *IP = nullptr;
7488 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7489 return SDValue(E, 0);
7490
7491 auto *N =
7492 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7493 createOperands(N, {Val});
7494
7495 CSEMap.InsertNode(N, IP);
7496 InsertNode(N);
7497
7498 SDValue V(N, 0);
7499 NewSDValueDbgMsg(V, "Creating new node: ", this);
7500 return V;
7501}
7502
7503SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7504 SDValue N1, SDValue N2) {
7505 SDNodeFlags Flags;
7506 if (Inserter)
7507 Flags = Inserter->getFlags();
7508 return getNode(Opcode, DL, VT, N1, N2, Flags);
7509}
7510
7512 SDValue &N2) const {
7513 if (!TLI->isCommutativeBinOp(Opcode))
7514 return;
7515
7516 // Canonicalize:
7517 // binop(const, nonconst) -> binop(nonconst, const)
7520 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7521 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7522 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7523 std::swap(N1, N2);
7524
7525 // Canonicalize:
7526 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7527 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7529 std::swap(N1, N2);
7530}
7531
7532SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7533 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7535 N2.getOpcode() != ISD::DELETED_NODE &&
7536 "Operand is DELETED_NODE!");
7537
7538 canonicalizeCommutativeBinop(Opcode, N1, N2);
7539
7540 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7541 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7542
7543 // Don't allow undefs in vector splats - we might be returning N2 when folding
7544 // to zero etc.
7545 ConstantSDNode *N2CV =
7546 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7547
7548 switch (Opcode) {
7549 default: break;
7550 case ISD::TokenFactor:
7551 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7552 N2.getValueType() == MVT::Other && "Invalid token factor!");
7553 // Fold trivial token factors.
7554 if (N1.getOpcode() == ISD::EntryToken) return N2;
7555 if (N2.getOpcode() == ISD::EntryToken) return N1;
7556 if (N1 == N2) return N1;
7557 break;
7558 case ISD::BUILD_VECTOR: {
7559 // Attempt to simplify BUILD_VECTOR.
7560 SDValue Ops[] = {N1, N2};
7561 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7562 return V;
7563 break;
7564 }
7565 case ISD::CONCAT_VECTORS: {
7566 SDValue Ops[] = {N1, N2};
7567 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7568 return V;
7569 break;
7570 }
7571 case ISD::AND:
7572 assert(VT.isInteger() && "This operator does not apply to FP types!");
7573 assert(N1.getValueType() == N2.getValueType() &&
7574 N1.getValueType() == VT && "Binary operator types must match!");
7575 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7576 // worth handling here.
7577 if (N2CV && N2CV->isZero())
7578 return N2;
7579 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7580 return N1;
7581 break;
7582 case ISD::OR:
7583 case ISD::XOR:
7584 case ISD::ADD:
7585 case ISD::PTRADD:
7586 case ISD::SUB:
7587 assert(VT.isInteger() && "This operator does not apply to FP types!");
7588 assert(N1.getValueType() == N2.getValueType() &&
7589 N1.getValueType() == VT && "Binary operator types must match!");
7590 // The equal operand types requirement is unnecessarily strong for PTRADD.
7591 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7592 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7593 // logic everywhere where PTRADDs may be folded or combined to properly
7594 // support them. If/when we introduce pointer types to the SDAG, we will
7595 // need to relax this constraint.
7596
7597 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7598 // it's worth handling here.
7599 if (N2CV && N2CV->isZero())
7600 return N1;
7601 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7602 VT.getScalarType() == MVT::i1)
7603 return getNode(ISD::XOR, DL, VT, N1, N2);
7604 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7605 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7606 N2.getOpcode() == ISD::VSCALE) {
7607 const APInt &C1 = N1->getConstantOperandAPInt(0);
7608 const APInt &C2 = N2->getConstantOperandAPInt(0);
7609 return getVScale(DL, VT, C1 + C2);
7610 }
7611 break;
7612 case ISD::MUL:
7613 assert(VT.isInteger() && "This operator does not apply to FP types!");
7614 assert(N1.getValueType() == N2.getValueType() &&
7615 N1.getValueType() == VT && "Binary operator types must match!");
7616 if (VT.getScalarType() == MVT::i1)
7617 return getNode(ISD::AND, DL, VT, N1, N2);
7618 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7619 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7620 const APInt &N2CImm = N2C->getAPIntValue();
7621 return getVScale(DL, VT, MulImm * N2CImm);
7622 }
7623 break;
7624 case ISD::UDIV:
7625 case ISD::UREM:
7626 case ISD::MULHU:
7627 case ISD::MULHS:
7628 case ISD::SDIV:
7629 case ISD::SREM:
7630 case ISD::SADDSAT:
7631 case ISD::SSUBSAT:
7632 case ISD::UADDSAT:
7633 case ISD::USUBSAT:
7634 assert(VT.isInteger() && "This operator does not apply to FP types!");
7635 assert(N1.getValueType() == N2.getValueType() &&
7636 N1.getValueType() == VT && "Binary operator types must match!");
7637 if (VT.getScalarType() == MVT::i1) {
7638 // fold (add_sat x, y) -> (or x, y) for bool types.
7639 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7640 return getNode(ISD::OR, DL, VT, N1, N2);
7641 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7642 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7643 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7644 }
7645 break;
7646 case ISD::SCMP:
7647 case ISD::UCMP:
7648 assert(N1.getValueType() == N2.getValueType() &&
7649 "Types of operands of UCMP/SCMP must match");
7650 assert(N1.getValueType().isVector() == VT.isVector() &&
7651 "Operands and return type of must both be scalars or vectors");
7652 if (VT.isVector())
7655 "Result and operands must have the same number of elements");
7656 break;
7657 case ISD::AVGFLOORS:
7658 case ISD::AVGFLOORU:
7659 case ISD::AVGCEILS:
7660 case ISD::AVGCEILU:
7661 assert(VT.isInteger() && "This operator does not apply to FP types!");
7662 assert(N1.getValueType() == N2.getValueType() &&
7663 N1.getValueType() == VT && "Binary operator types must match!");
7664 break;
7665 case ISD::ABDS:
7666 case ISD::ABDU:
7667 assert(VT.isInteger() && "This operator does not apply to FP types!");
7668 assert(N1.getValueType() == N2.getValueType() &&
7669 N1.getValueType() == VT && "Binary operator types must match!");
7670 if (VT.getScalarType() == MVT::i1)
7671 return getNode(ISD::XOR, DL, VT, N1, N2);
7672 break;
7673 case ISD::SMIN:
7674 case ISD::UMAX:
7675 assert(VT.isInteger() && "This operator does not apply to FP types!");
7676 assert(N1.getValueType() == N2.getValueType() &&
7677 N1.getValueType() == VT && "Binary operator types must match!");
7678 if (VT.getScalarType() == MVT::i1)
7679 return getNode(ISD::OR, DL, VT, N1, N2);
7680 break;
7681 case ISD::SMAX:
7682 case ISD::UMIN:
7683 assert(VT.isInteger() && "This operator does not apply to FP types!");
7684 assert(N1.getValueType() == N2.getValueType() &&
7685 N1.getValueType() == VT && "Binary operator types must match!");
7686 if (VT.getScalarType() == MVT::i1)
7687 return getNode(ISD::AND, DL, VT, N1, N2);
7688 break;
7689 case ISD::FADD:
7690 case ISD::FSUB:
7691 case ISD::FMUL:
7692 case ISD::FDIV:
7693 case ISD::FREM:
7694 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7695 assert(N1.getValueType() == N2.getValueType() &&
7696 N1.getValueType() == VT && "Binary operator types must match!");
7697 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7698 return V;
7699 break;
7700 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7701 assert(N1.getValueType() == VT &&
7704 "Invalid FCOPYSIGN!");
7705 break;
7706 case ISD::SHL:
7707 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7708 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7709 const APInt &ShiftImm = N2C->getAPIntValue();
7710 return getVScale(DL, VT, MulImm << ShiftImm);
7711 }
7712 [[fallthrough]];
7713 case ISD::SRA:
7714 case ISD::SRL:
7715 if (SDValue V = simplifyShift(N1, N2))
7716 return V;
7717 [[fallthrough]];
7718 case ISD::ROTL:
7719 case ISD::ROTR:
7720 assert(VT == N1.getValueType() &&
7721 "Shift operators return type must be the same as their first arg");
7722 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7723 "Shifts only work on integers");
7724 assert((!VT.isVector() || VT == N2.getValueType()) &&
7725 "Vector shift amounts must be in the same as their first arg");
7726 // Verify that the shift amount VT is big enough to hold valid shift
7727 // amounts. This catches things like trying to shift an i1024 value by an
7728 // i8, which is easy to fall into in generic code that uses
7729 // TLI.getShiftAmount().
7732 "Invalid use of small shift amount with oversized value!");
7733
7734 // Always fold shifts of i1 values so the code generator doesn't need to
7735 // handle them. Since we know the size of the shift has to be less than the
7736 // size of the value, the shift/rotate count is guaranteed to be zero.
7737 if (VT == MVT::i1)
7738 return N1;
7739 if (N2CV && N2CV->isZero())
7740 return N1;
7741 break;
7742 case ISD::FP_ROUND:
7744 VT.bitsLE(N1.getValueType()) && N2C &&
7745 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7746 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7747 if (N1.getValueType() == VT) return N1; // noop conversion.
7748 break;
7749 case ISD::AssertNoFPClass: {
7751 "AssertNoFPClass is used for a non-floating type");
7752 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7753 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7754 assert(llvm::to_underlying(NoFPClass) <=
7756 "FPClassTest value too large");
7757 (void)NoFPClass;
7758 break;
7759 }
7760 case ISD::AssertSext:
7761 case ISD::AssertZext: {
7762 EVT EVT = cast<VTSDNode>(N2)->getVT();
7763 assert(VT == N1.getValueType() && "Not an inreg extend!");
7764 assert(VT.isInteger() && EVT.isInteger() &&
7765 "Cannot *_EXTEND_INREG FP types");
7766 assert(!EVT.isVector() &&
7767 "AssertSExt/AssertZExt type should be the vector element type "
7768 "rather than the vector type!");
7769 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7770 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7771 break;
7772 }
7774 EVT EVT = cast<VTSDNode>(N2)->getVT();
7775 assert(VT == N1.getValueType() && "Not an inreg extend!");
7776 assert(VT.isInteger() && EVT.isInteger() &&
7777 "Cannot *_EXTEND_INREG FP types");
7778 assert(EVT.isVector() == VT.isVector() &&
7779 "SIGN_EXTEND_INREG type should be vector iff the operand "
7780 "type is vector!");
7781 assert((!EVT.isVector() ||
7783 "Vector element counts must match in SIGN_EXTEND_INREG");
7784 assert(EVT.bitsLE(VT) && "Not extending!");
7785 if (EVT == VT) return N1; // Not actually extending
7786 break;
7787 }
7789 case ISD::FP_TO_UINT_SAT: {
7790 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7791 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7792 assert(N1.getValueType().isVector() == VT.isVector() &&
7793 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7794 "vector!");
7795 assert((!VT.isVector() || VT.getVectorElementCount() ==
7797 "Vector element counts must match in FP_TO_*INT_SAT");
7798 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7799 "Type to saturate to must be a scalar.");
7800 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7801 "Not extending!");
7802 break;
7803 }
7806 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7807 element type of the vector.");
7808
7809 // Extract from an undefined value or using an undefined index is undefined.
7810 if (N1.isUndef() || N2.isUndef())
7811 return getUNDEF(VT);
7812
7813 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7814 // vectors. For scalable vectors we will provide appropriate support for
7815 // dealing with arbitrary indices.
7816 if (N2C && N1.getValueType().isFixedLengthVector() &&
7817 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7818 return getUNDEF(VT);
7819
7820 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7821 // expanding copies of large vectors from registers. This only works for
7822 // fixed length vectors, since we need to know the exact number of
7823 // elements.
7824 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7826 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7827 return getExtractVectorElt(DL, VT,
7828 N1.getOperand(N2C->getZExtValue() / Factor),
7829 N2C->getZExtValue() % Factor);
7830 }
7831
7832 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7833 // lowering is expanding large vector constants.
7834 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7835 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7838 "BUILD_VECTOR used for scalable vectors");
7839 unsigned Index =
7840 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7841 SDValue Elt = N1.getOperand(Index);
7842
7843 if (VT != Elt.getValueType())
7844 // If the vector element type is not legal, the BUILD_VECTOR operands
7845 // are promoted and implicitly truncated, and the result implicitly
7846 // extended. Make that explicit here.
7847 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7848
7849 return Elt;
7850 }
7851
7852 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7853 // operations are lowered to scalars.
7854 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7855 // If the indices are the same, return the inserted element else
7856 // if the indices are known different, extract the element from
7857 // the original vector.
7858 SDValue N1Op2 = N1.getOperand(2);
7860
7861 if (N1Op2C && N2C) {
7862 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7863 if (VT == N1.getOperand(1).getValueType())
7864 return N1.getOperand(1);
7865 if (VT.isFloatingPoint()) {
7867 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7868 }
7869 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7870 }
7871 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7872 }
7873 }
7874
7875 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7876 // when vector types are scalarized and v1iX is legal.
7877 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7878 // Here we are completely ignoring the extract element index (N2),
7879 // which is fine for fixed width vectors, since any index other than 0
7880 // is undefined anyway. However, this cannot be ignored for scalable
7881 // vectors - in theory we could support this, but we don't want to do this
7882 // without a profitability check.
7883 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7885 N1.getValueType().getVectorNumElements() == 1) {
7886 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7887 N1.getOperand(1));
7888 }
7889 break;
7891 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7892 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7893 (N1.getValueType().isInteger() == VT.isInteger()) &&
7894 N1.getValueType() != VT &&
7895 "Wrong types for EXTRACT_ELEMENT!");
7896
7897 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7898 // 64-bit integers into 32-bit parts. Instead of building the extract of
7899 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7900 if (N1.getOpcode() == ISD::BUILD_PAIR)
7901 return N1.getOperand(N2C->getZExtValue());
7902
7903 // EXTRACT_ELEMENT of a constant int is also very common.
7904 if (N1C) {
7905 unsigned ElementSize = VT.getSizeInBits();
7906 unsigned Shift = ElementSize * N2C->getZExtValue();
7907 const APInt &Val = N1C->getAPIntValue();
7908 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7909 }
7910 break;
7912 EVT N1VT = N1.getValueType();
7913 assert(VT.isVector() && N1VT.isVector() &&
7914 "Extract subvector VTs must be vectors!");
7916 "Extract subvector VTs must have the same element type!");
7917 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7918 "Cannot extract a scalable vector from a fixed length vector!");
7919 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7921 "Extract subvector must be from larger vector to smaller vector!");
7922 assert(N2C && "Extract subvector index must be a constant");
7923 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7924 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7925 N1VT.getVectorMinNumElements()) &&
7926 "Extract subvector overflow!");
7927 assert(N2C->getAPIntValue().getBitWidth() ==
7928 TLI->getVectorIdxWidth(getDataLayout()) &&
7929 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7930 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
7931 "Extract index is not a multiple of the output vector length");
7932
7933 // Trivial extraction.
7934 if (VT == N1VT)
7935 return N1;
7936
7937 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7938 if (N1.isUndef())
7939 return getUNDEF(VT);
7940
7941 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7942 // the concat have the same type as the extract.
7943 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7944 VT == N1.getOperand(0).getValueType()) {
7945 unsigned Factor = VT.getVectorMinNumElements();
7946 return N1.getOperand(N2C->getZExtValue() / Factor);
7947 }
7948
7949 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7950 // during shuffle legalization.
7951 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7952 VT == N1.getOperand(1).getValueType())
7953 return N1.getOperand(1);
7954 break;
7955 }
7956 }
7957
7958 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
7959 switch (Opcode) {
7960 case ISD::XOR:
7961 case ISD::ADD:
7962 case ISD::PTRADD:
7963 case ISD::SUB:
7965 case ISD::UDIV:
7966 case ISD::SDIV:
7967 case ISD::UREM:
7968 case ISD::SREM:
7969 case ISD::MUL:
7970 case ISD::AND:
7971 case ISD::SSUBSAT:
7972 case ISD::USUBSAT:
7973 case ISD::UMIN:
7974 case ISD::OR:
7975 case ISD::SADDSAT:
7976 case ISD::UADDSAT:
7977 case ISD::UMAX:
7978 case ISD::SMAX:
7979 case ISD::SMIN:
7980 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
7981 return N2.getOpcode() == ISD::POISON ? N2 : N1;
7982 }
7983 }
7984
7985 // Canonicalize an UNDEF to the RHS, even over a constant.
7986 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
7987 if (TLI->isCommutativeBinOp(Opcode)) {
7988 std::swap(N1, N2);
7989 } else {
7990 switch (Opcode) {
7991 case ISD::PTRADD:
7992 case ISD::SUB:
7993 // fold op(undef, non_undef_arg2) -> undef.
7994 return N1;
7996 case ISD::UDIV:
7997 case ISD::SDIV:
7998 case ISD::UREM:
7999 case ISD::SREM:
8000 case ISD::SSUBSAT:
8001 case ISD::USUBSAT:
8002 // fold op(undef, non_undef_arg2) -> 0.
8003 return getConstant(0, DL, VT);
8004 }
8005 }
8006 }
8007
8008 // Fold a bunch of operators when the RHS is undef.
8009 if (N2.getOpcode() == ISD::UNDEF) {
8010 switch (Opcode) {
8011 case ISD::XOR:
8012 if (N1.getOpcode() == ISD::UNDEF)
8013 // Handle undef ^ undef -> 0 special case. This is a common
8014 // idiom (misuse).
8015 return getConstant(0, DL, VT);
8016 [[fallthrough]];
8017 case ISD::ADD:
8018 case ISD::PTRADD:
8019 case ISD::SUB:
8020 // fold op(arg1, undef) -> undef.
8021 return N2;
8022 case ISD::UDIV:
8023 case ISD::SDIV:
8024 case ISD::UREM:
8025 case ISD::SREM:
8026 // fold op(arg1, undef) -> poison.
8027 return getPOISON(VT);
8028 case ISD::MUL:
8029 case ISD::AND:
8030 case ISD::SSUBSAT:
8031 case ISD::USUBSAT:
8032 case ISD::UMIN:
8033 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8034 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8035 case ISD::OR:
8036 case ISD::SADDSAT:
8037 case ISD::UADDSAT:
8038 case ISD::UMAX:
8039 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8040 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8041 case ISD::SMAX:
8042 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8043 return N1.getOpcode() == ISD::UNDEF
8044 ? N2
8045 : getConstant(
8047 VT);
8048 case ISD::SMIN:
8049 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8050 return N1.getOpcode() == ISD::UNDEF
8051 ? N2
8052 : getConstant(
8054 VT);
8055 }
8056 }
8057
8058 // Perform trivial constant folding.
8059 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8060 return SV;
8061
8062 // Memoize this node if possible.
8063 SDNode *N;
8064 SDVTList VTs = getVTList(VT);
8065 SDValue Ops[] = {N1, N2};
8066 if (VT != MVT::Glue) {
8068 AddNodeIDNode(ID, Opcode, VTs, Ops);
8069 void *IP = nullptr;
8070 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8071 E->intersectFlagsWith(Flags);
8072 return SDValue(E, 0);
8073 }
8074
8075 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8076 N->setFlags(Flags);
8077 createOperands(N, Ops);
8078 CSEMap.InsertNode(N, IP);
8079 } else {
8080 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8081 createOperands(N, Ops);
8082 }
8083
8084 InsertNode(N);
8085 SDValue V = SDValue(N, 0);
8086 NewSDValueDbgMsg(V, "Creating new node: ", this);
8087 return V;
8088}
8089
8090SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8091 SDValue N1, SDValue N2, SDValue N3) {
8092 SDNodeFlags Flags;
8093 if (Inserter)
8094 Flags = Inserter->getFlags();
8095 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8096}
8097
8098SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8099 SDValue N1, SDValue N2, SDValue N3,
8100 const SDNodeFlags Flags) {
8102 N2.getOpcode() != ISD::DELETED_NODE &&
8103 N3.getOpcode() != ISD::DELETED_NODE &&
8104 "Operand is DELETED_NODE!");
8105 // Perform various simplifications.
8106 switch (Opcode) {
8107 case ISD::BUILD_VECTOR: {
8108 // Attempt to simplify BUILD_VECTOR.
8109 SDValue Ops[] = {N1, N2, N3};
8110 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8111 return V;
8112 break;
8113 }
8114 case ISD::CONCAT_VECTORS: {
8115 SDValue Ops[] = {N1, N2, N3};
8116 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8117 return V;
8118 break;
8119 }
8120 case ISD::SETCC: {
8121 assert(VT.isInteger() && "SETCC result type must be an integer!");
8122 assert(N1.getValueType() == N2.getValueType() &&
8123 "SETCC operands must have the same type!");
8124 assert(VT.isVector() == N1.getValueType().isVector() &&
8125 "SETCC type should be vector iff the operand type is vector!");
8126 assert((!VT.isVector() || VT.getVectorElementCount() ==
8128 "SETCC vector element counts must match!");
8129 // Use FoldSetCC to simplify SETCC's.
8130 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8131 return V;
8132 break;
8133 }
8134 case ISD::SELECT:
8135 case ISD::VSELECT:
8136 if (SDValue V = simplifySelect(N1, N2, N3))
8137 return V;
8138 break;
8140 llvm_unreachable("should use getVectorShuffle constructor!");
8141 case ISD::VECTOR_SPLICE: {
8142 if (cast<ConstantSDNode>(N3)->isZero())
8143 return N1;
8144 break;
8145 }
8147 assert(VT.isVector() && VT == N1.getValueType() &&
8148 "INSERT_VECTOR_ELT vector type mismatch");
8150 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8151 assert((!VT.isFloatingPoint() ||
8152 VT.getVectorElementType() == N2.getValueType()) &&
8153 "INSERT_VECTOR_ELT fp scalar type mismatch");
8154 assert((!VT.isInteger() ||
8156 "INSERT_VECTOR_ELT int scalar size mismatch");
8157
8158 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8159 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8160 // for scalable vectors where we will generate appropriate code to
8161 // deal with out-of-bounds cases correctly.
8162 if (N3C && VT.isFixedLengthVector() &&
8163 N3C->getZExtValue() >= VT.getVectorNumElements())
8164 return getUNDEF(VT);
8165
8166 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8167 if (N3.isUndef())
8168 return getUNDEF(VT);
8169
8170 // If inserting poison, just use the input vector.
8171 if (N2.getOpcode() == ISD::POISON)
8172 return N1;
8173
8174 // Inserting undef into undef/poison is still undef.
8175 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8176 return getUNDEF(VT);
8177
8178 // If the inserted element is an UNDEF, just use the input vector.
8179 // But not if skipping the insert could make the result more poisonous.
8180 if (N2.isUndef()) {
8181 if (N3C && VT.isFixedLengthVector()) {
8182 APInt EltMask =
8183 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8184 if (isGuaranteedNotToBePoison(N1, EltMask))
8185 return N1;
8186 } else if (isGuaranteedNotToBePoison(N1))
8187 return N1;
8188 }
8189 break;
8190 }
8191 case ISD::INSERT_SUBVECTOR: {
8192 // If inserting poison, just use the input vector,
8193 if (N2.getOpcode() == ISD::POISON)
8194 return N1;
8195
8196 // Inserting undef into undef/poison is still undef.
8197 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8198 return getUNDEF(VT);
8199
8200 EVT N2VT = N2.getValueType();
8201 assert(VT == N1.getValueType() &&
8202 "Dest and insert subvector source types must match!");
8203 assert(VT.isVector() && N2VT.isVector() &&
8204 "Insert subvector VTs must be vectors!");
8206 "Insert subvector VTs must have the same element type!");
8207 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8208 "Cannot insert a scalable vector into a fixed length vector!");
8209 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8211 "Insert subvector must be from smaller vector to larger vector!");
8213 "Insert subvector index must be constant");
8214 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8215 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8217 "Insert subvector overflow!");
8219 TLI->getVectorIdxWidth(getDataLayout()) &&
8220 "Constant index for INSERT_SUBVECTOR has an invalid size");
8221
8222 // Trivial insertion.
8223 if (VT == N2VT)
8224 return N2;
8225
8226 // If this is an insert of an extracted vector into an undef/poison vector,
8227 // we can just use the input to the extract. But not if skipping the
8228 // extract+insert could make the result more poisonous.
8229 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8230 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8231 if (N1.getOpcode() == ISD::POISON)
8232 return N2.getOperand(0);
8233 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8234 unsigned LoBit = N3->getAsZExtVal();
8235 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8236 APInt EltMask =
8237 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8238 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8239 return N2.getOperand(0);
8240 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8241 return N2.getOperand(0);
8242 }
8243
8244 // If the inserted subvector is UNDEF, just use the input vector.
8245 // But not if skipping the insert could make the result more poisonous.
8246 if (N2.isUndef()) {
8247 if (VT.isFixedLengthVector()) {
8248 unsigned LoBit = N3->getAsZExtVal();
8249 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8250 APInt EltMask =
8251 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8252 if (isGuaranteedNotToBePoison(N1, EltMask))
8253 return N1;
8254 } else if (isGuaranteedNotToBePoison(N1))
8255 return N1;
8256 }
8257 break;
8258 }
8259 case ISD::BITCAST:
8260 // Fold bit_convert nodes from a type to themselves.
8261 if (N1.getValueType() == VT)
8262 return N1;
8263 break;
8264 case ISD::VP_TRUNCATE:
8265 case ISD::VP_SIGN_EXTEND:
8266 case ISD::VP_ZERO_EXTEND:
8267 // Don't create noop casts.
8268 if (N1.getValueType() == VT)
8269 return N1;
8270 break;
8271 case ISD::VECTOR_COMPRESS: {
8272 [[maybe_unused]] EVT VecVT = N1.getValueType();
8273 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8274 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8275 assert(VT == VecVT && "Vector and result type don't match.");
8276 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8277 "All inputs must be vectors.");
8278 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8280 "Vector and mask must have same number of elements.");
8281
8282 if (N1.isUndef() || N2.isUndef())
8283 return N3;
8284
8285 break;
8286 }
8287 case ISD::PARTIAL_REDUCE_UMLA:
8288 case ISD::PARTIAL_REDUCE_SMLA:
8289 case ISD::PARTIAL_REDUCE_SUMLA:
8290 case ISD::PARTIAL_REDUCE_FMLA: {
8291 [[maybe_unused]] EVT AccVT = N1.getValueType();
8292 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8293 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8294 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8295 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8296 "node to have the same type!");
8297 assert(VT.isVector() && VT == AccVT &&
8298 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8299 "the same type as its result!");
8301 AccVT.getVectorElementCount()) &&
8302 "Expected the element count of the second and third operands of the "
8303 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8304 "element count of the first operand and the result!");
8306 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8307 "node to have an element type which is the same as or smaller than "
8308 "the element type of the first operand and result!");
8309 break;
8310 }
8311 }
8312
8313 // Perform trivial constant folding for arithmetic operators.
8314 switch (Opcode) {
8315 case ISD::FMA:
8316 case ISD::FMAD:
8317 case ISD::SETCC:
8318 case ISD::FSHL:
8319 case ISD::FSHR:
8320 if (SDValue SV =
8321 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8322 return SV;
8323 break;
8324 }
8325
8326 // Memoize node if it doesn't produce a glue result.
8327 SDNode *N;
8328 SDVTList VTs = getVTList(VT);
8329 SDValue Ops[] = {N1, N2, N3};
8330 if (VT != MVT::Glue) {
8332 AddNodeIDNode(ID, Opcode, VTs, Ops);
8333 void *IP = nullptr;
8334 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8335 E->intersectFlagsWith(Flags);
8336 return SDValue(E, 0);
8337 }
8338
8339 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8340 N->setFlags(Flags);
8341 createOperands(N, Ops);
8342 CSEMap.InsertNode(N, IP);
8343 } else {
8344 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8345 createOperands(N, Ops);
8346 }
8347
8348 InsertNode(N);
8349 SDValue V = SDValue(N, 0);
8350 NewSDValueDbgMsg(V, "Creating new node: ", this);
8351 return V;
8352}
8353
8354SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8355 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8356 const SDNodeFlags Flags) {
8357 SDValue Ops[] = { N1, N2, N3, N4 };
8358 return getNode(Opcode, DL, VT, Ops, Flags);
8359}
8360
8361SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8362 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8363 SDNodeFlags Flags;
8364 if (Inserter)
8365 Flags = Inserter->getFlags();
8366 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8367}
8368
8369SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8370 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8371 SDValue N5, const SDNodeFlags Flags) {
8372 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8373 return getNode(Opcode, DL, VT, Ops, Flags);
8374}
8375
8376SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8377 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8378 SDValue N5) {
8379 SDNodeFlags Flags;
8380 if (Inserter)
8381 Flags = Inserter->getFlags();
8382 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8383}
8384
8385/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8386/// the incoming stack arguments to be loaded from the stack.
8388 SmallVector<SDValue, 8> ArgChains;
8389
8390 // Include the original chain at the beginning of the list. When this is
8391 // used by target LowerCall hooks, this helps legalize find the
8392 // CALLSEQ_BEGIN node.
8393 ArgChains.push_back(Chain);
8394
8395 // Add a chain value for each stack argument.
8396 for (SDNode *U : getEntryNode().getNode()->users())
8397 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8398 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8399 if (FI->getIndex() < 0)
8400 ArgChains.push_back(SDValue(L, 1));
8401
8402 // Build a tokenfactor for all the chains.
8403 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8404}
8405
8406/// getMemsetValue - Vectorized representation of the memset value
8407/// operand.
8409 const SDLoc &dl) {
8410 assert(!Value.isUndef());
8411
8412 unsigned NumBits = VT.getScalarSizeInBits();
8414 assert(C->getAPIntValue().getBitWidth() == 8);
8415 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8416 if (VT.isInteger()) {
8417 bool IsOpaque = VT.getSizeInBits() > 64 ||
8418 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8419 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8420 }
8421 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8422 }
8423
8424 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8425 EVT IntVT = VT.getScalarType();
8426 if (!IntVT.isInteger())
8427 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8428
8429 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8430 if (NumBits > 8) {
8431 // Use a multiplication with 0x010101... to extend the input to the
8432 // required length.
8433 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8434 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8435 DAG.getConstant(Magic, dl, IntVT));
8436 }
8437
8438 if (VT != Value.getValueType() && !VT.isInteger())
8439 Value = DAG.getBitcast(VT.getScalarType(), Value);
8440 if (VT != Value.getValueType())
8441 Value = DAG.getSplatBuildVector(VT, dl, Value);
8442
8443 return Value;
8444}
8445
8446/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8447/// used when a memcpy is turned into a memset when the source is a constant
8448/// string ptr.
8450 const TargetLowering &TLI,
8451 const ConstantDataArraySlice &Slice) {
8452 // Handle vector with all elements zero.
8453 if (Slice.Array == nullptr) {
8454 if (VT.isInteger())
8455 return DAG.getConstant(0, dl, VT);
8456 return DAG.getNode(ISD::BITCAST, dl, VT,
8457 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8458 }
8459
8460 assert(!VT.isVector() && "Can't handle vector type here!");
8461 unsigned NumVTBits = VT.getSizeInBits();
8462 unsigned NumVTBytes = NumVTBits / 8;
8463 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8464
8465 APInt Val(NumVTBits, 0);
8466 if (DAG.getDataLayout().isLittleEndian()) {
8467 for (unsigned i = 0; i != NumBytes; ++i)
8468 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8469 } else {
8470 for (unsigned i = 0; i != NumBytes; ++i)
8471 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8472 }
8473
8474 // If the "cost" of materializing the integer immediate is less than the cost
8475 // of a load, then it is cost effective to turn the load into the immediate.
8476 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8477 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8478 return DAG.getConstant(Val, dl, VT);
8479 return SDValue();
8480}
8481
8483 const SDLoc &DL,
8484 const SDNodeFlags Flags) {
8485 EVT VT = Base.getValueType();
8486 SDValue Index;
8487
8488 if (Offset.isScalable())
8489 Index = getVScale(DL, Base.getValueType(),
8490 APInt(Base.getValueSizeInBits().getFixedValue(),
8491 Offset.getKnownMinValue()));
8492 else
8493 Index = getConstant(Offset.getFixedValue(), DL, VT);
8494
8495 return getMemBasePlusOffset(Base, Index, DL, Flags);
8496}
8497
8499 const SDLoc &DL,
8500 const SDNodeFlags Flags) {
8501 assert(Offset.getValueType().isInteger());
8502 EVT BasePtrVT = Ptr.getValueType();
8503 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8504 BasePtrVT))
8505 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8506 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8507 SDNodeFlags AddFlags = Flags;
8508 AddFlags.setInBounds(false);
8509 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8510}
8511
8512/// Returns true if memcpy source is constant data.
8514 uint64_t SrcDelta = 0;
8515 GlobalAddressSDNode *G = nullptr;
8516 if (Src.getOpcode() == ISD::GlobalAddress)
8518 else if (Src->isAnyAdd() &&
8519 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8520 Src.getOperand(1).getOpcode() == ISD::Constant) {
8521 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8522 SrcDelta = Src.getConstantOperandVal(1);
8523 }
8524 if (!G)
8525 return false;
8526
8527 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8528 SrcDelta + G->getOffset());
8529}
8530
8532 SelectionDAG &DAG) {
8533 // On Darwin, -Os means optimize for size without hurting performance, so
8534 // only really optimize for size when -Oz (MinSize) is used.
8536 return MF.getFunction().hasMinSize();
8537 return DAG.shouldOptForSize();
8538}
8539
8541 SmallVector<SDValue, 32> &OutChains, unsigned From,
8542 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8543 SmallVector<SDValue, 16> &OutStoreChains) {
8544 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8545 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8546 SmallVector<SDValue, 16> GluedLoadChains;
8547 for (unsigned i = From; i < To; ++i) {
8548 OutChains.push_back(OutLoadChains[i]);
8549 GluedLoadChains.push_back(OutLoadChains[i]);
8550 }
8551
8552 // Chain for all loads.
8553 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8554 GluedLoadChains);
8555
8556 for (unsigned i = From; i < To; ++i) {
8557 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8558 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8559 ST->getBasePtr(), ST->getMemoryVT(),
8560 ST->getMemOperand());
8561 OutChains.push_back(NewStore);
8562 }
8563}
8564
8566 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8567 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8568 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8569 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8570 // Turn a memcpy of undef to nop.
8571 // FIXME: We need to honor volatile even is Src is undef.
8572 if (Src.isUndef())
8573 return Chain;
8574
8575 // Expand memcpy to a series of load and store ops if the size operand falls
8576 // below a certain threshold.
8577 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8578 // rather than maybe a humongous number of loads and stores.
8579 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8580 const DataLayout &DL = DAG.getDataLayout();
8581 LLVMContext &C = *DAG.getContext();
8582 std::vector<EVT> MemOps;
8583 bool DstAlignCanChange = false;
8585 MachineFrameInfo &MFI = MF.getFrameInfo();
8586 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8588 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8589 DstAlignCanChange = true;
8590 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8591 if (!SrcAlign || Alignment > *SrcAlign)
8592 SrcAlign = Alignment;
8593 assert(SrcAlign && "SrcAlign must be set");
8595 // If marked as volatile, perform a copy even when marked as constant.
8596 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8597 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8598 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8599 const MemOp Op = isZeroConstant
8600 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8601 /*IsZeroMemset*/ true, isVol)
8602 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8603 *SrcAlign, isVol, CopyFromConstant);
8604 if (!TLI.findOptimalMemOpLowering(
8605 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8606 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8607 return SDValue();
8608
8609 if (DstAlignCanChange) {
8610 Type *Ty = MemOps[0].getTypeForEVT(C);
8611 Align NewAlign = DL.getABITypeAlign(Ty);
8612
8613 // Don't promote to an alignment that would require dynamic stack
8614 // realignment which may conflict with optimizations such as tail call
8615 // optimization.
8617 if (!TRI->hasStackRealignment(MF))
8618 if (MaybeAlign StackAlign = DL.getStackAlignment())
8619 NewAlign = std::min(NewAlign, *StackAlign);
8620
8621 if (NewAlign > Alignment) {
8622 // Give the stack frame object a larger alignment if needed.
8623 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8624 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8625 Alignment = NewAlign;
8626 }
8627 }
8628
8629 // Prepare AAInfo for loads/stores after lowering this memcpy.
8630 AAMDNodes NewAAInfo = AAInfo;
8631 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8632
8633 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8634 bool isConstant =
8635 BatchAA && SrcVal &&
8636 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8637
8638 MachineMemOperand::Flags MMOFlags =
8640 SmallVector<SDValue, 16> OutLoadChains;
8641 SmallVector<SDValue, 16> OutStoreChains;
8642 SmallVector<SDValue, 32> OutChains;
8643 unsigned NumMemOps = MemOps.size();
8644 uint64_t SrcOff = 0, DstOff = 0;
8645 for (unsigned i = 0; i != NumMemOps; ++i) {
8646 EVT VT = MemOps[i];
8647 unsigned VTSize = VT.getSizeInBits() / 8;
8648 SDValue Value, Store;
8649
8650 if (VTSize > Size) {
8651 // Issuing an unaligned load / store pair that overlaps with the previous
8652 // pair. Adjust the offset accordingly.
8653 assert(i == NumMemOps-1 && i != 0);
8654 SrcOff -= VTSize - Size;
8655 DstOff -= VTSize - Size;
8656 }
8657
8658 if (CopyFromConstant &&
8659 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8660 // It's unlikely a store of a vector immediate can be done in a single
8661 // instruction. It would require a load from a constantpool first.
8662 // We only handle zero vectors here.
8663 // FIXME: Handle other cases where store of vector immediate is done in
8664 // a single instruction.
8665 ConstantDataArraySlice SubSlice;
8666 if (SrcOff < Slice.Length) {
8667 SubSlice = Slice;
8668 SubSlice.move(SrcOff);
8669 } else {
8670 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8671 SubSlice.Array = nullptr;
8672 SubSlice.Offset = 0;
8673 SubSlice.Length = VTSize;
8674 }
8675 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8676 if (Value.getNode()) {
8677 Store = DAG.getStore(
8678 Chain, dl, Value,
8679 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8680 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8681 OutChains.push_back(Store);
8682 }
8683 }
8684
8685 if (!Store.getNode()) {
8686 // The type might not be legal for the target. This should only happen
8687 // if the type is smaller than a legal type, as on PPC, so the right
8688 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8689 // to Load/Store if NVT==VT.
8690 // FIXME does the case above also need this?
8691 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8692 assert(NVT.bitsGE(VT));
8693
8694 bool isDereferenceable =
8695 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8696 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8697 if (isDereferenceable)
8699 if (isConstant)
8700 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8701
8702 Value = DAG.getExtLoad(
8703 ISD::EXTLOAD, dl, NVT, Chain,
8704 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8705 SrcPtrInfo.getWithOffset(SrcOff), VT,
8706 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8707 OutLoadChains.push_back(Value.getValue(1));
8708
8709 Store = DAG.getTruncStore(
8710 Chain, dl, Value,
8711 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8712 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8713 OutStoreChains.push_back(Store);
8714 }
8715 SrcOff += VTSize;
8716 DstOff += VTSize;
8717 Size -= VTSize;
8718 }
8719
8720 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8722 unsigned NumLdStInMemcpy = OutStoreChains.size();
8723
8724 if (NumLdStInMemcpy) {
8725 // It may be that memcpy might be converted to memset if it's memcpy
8726 // of constants. In such a case, we won't have loads and stores, but
8727 // just stores. In the absence of loads, there is nothing to gang up.
8728 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8729 // If target does not care, just leave as it.
8730 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8731 OutChains.push_back(OutLoadChains[i]);
8732 OutChains.push_back(OutStoreChains[i]);
8733 }
8734 } else {
8735 // Ld/St less than/equal limit set by target.
8736 if (NumLdStInMemcpy <= GluedLdStLimit) {
8737 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8738 NumLdStInMemcpy, OutLoadChains,
8739 OutStoreChains);
8740 } else {
8741 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8742 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8743 unsigned GlueIter = 0;
8744
8745 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8746 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8747 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8748
8749 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8750 OutLoadChains, OutStoreChains);
8751 GlueIter += GluedLdStLimit;
8752 }
8753
8754 // Residual ld/st.
8755 if (RemainingLdStInMemcpy) {
8756 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8757 RemainingLdStInMemcpy, OutLoadChains,
8758 OutStoreChains);
8759 }
8760 }
8761 }
8762 }
8763 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8764}
8765
8767 SDValue Chain, SDValue Dst, SDValue Src,
8768 uint64_t Size, Align Alignment,
8769 bool isVol, bool AlwaysInline,
8770 MachinePointerInfo DstPtrInfo,
8771 MachinePointerInfo SrcPtrInfo,
8772 const AAMDNodes &AAInfo) {
8773 // Turn a memmove of undef to nop.
8774 // FIXME: We need to honor volatile even is Src is undef.
8775 if (Src.isUndef())
8776 return Chain;
8777
8778 // Expand memmove to a series of load and store ops if the size operand falls
8779 // below a certain threshold.
8780 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8781 const DataLayout &DL = DAG.getDataLayout();
8782 LLVMContext &C = *DAG.getContext();
8783 std::vector<EVT> MemOps;
8784 bool DstAlignCanChange = false;
8786 MachineFrameInfo &MFI = MF.getFrameInfo();
8787 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8789 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8790 DstAlignCanChange = true;
8791 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8792 if (!SrcAlign || Alignment > *SrcAlign)
8793 SrcAlign = Alignment;
8794 assert(SrcAlign && "SrcAlign must be set");
8795 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8796 if (!TLI.findOptimalMemOpLowering(
8797 C, MemOps, Limit,
8798 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8799 /*IsVolatile*/ true),
8800 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8801 MF.getFunction().getAttributes()))
8802 return SDValue();
8803
8804 if (DstAlignCanChange) {
8805 Type *Ty = MemOps[0].getTypeForEVT(C);
8806 Align NewAlign = DL.getABITypeAlign(Ty);
8807
8808 // Don't promote to an alignment that would require dynamic stack
8809 // realignment which may conflict with optimizations such as tail call
8810 // optimization.
8812 if (!TRI->hasStackRealignment(MF))
8813 if (MaybeAlign StackAlign = DL.getStackAlignment())
8814 NewAlign = std::min(NewAlign, *StackAlign);
8815
8816 if (NewAlign > Alignment) {
8817 // Give the stack frame object a larger alignment if needed.
8818 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8819 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8820 Alignment = NewAlign;
8821 }
8822 }
8823
8824 // Prepare AAInfo for loads/stores after lowering this memmove.
8825 AAMDNodes NewAAInfo = AAInfo;
8826 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8827
8828 MachineMemOperand::Flags MMOFlags =
8830 uint64_t SrcOff = 0, DstOff = 0;
8831 SmallVector<SDValue, 8> LoadValues;
8832 SmallVector<SDValue, 8> LoadChains;
8833 SmallVector<SDValue, 8> OutChains;
8834 unsigned NumMemOps = MemOps.size();
8835 for (unsigned i = 0; i < NumMemOps; i++) {
8836 EVT VT = MemOps[i];
8837 unsigned VTSize = VT.getSizeInBits() / 8;
8838 SDValue Value;
8839
8840 bool isDereferenceable =
8841 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8842 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8843 if (isDereferenceable)
8845
8846 Value = DAG.getLoad(
8847 VT, dl, Chain,
8848 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8849 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8850 LoadValues.push_back(Value);
8851 LoadChains.push_back(Value.getValue(1));
8852 SrcOff += VTSize;
8853 }
8854 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8855 OutChains.clear();
8856 for (unsigned i = 0; i < NumMemOps; i++) {
8857 EVT VT = MemOps[i];
8858 unsigned VTSize = VT.getSizeInBits() / 8;
8859 SDValue Store;
8860
8861 Store = DAG.getStore(
8862 Chain, dl, LoadValues[i],
8863 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8864 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8865 OutChains.push_back(Store);
8866 DstOff += VTSize;
8867 }
8868
8869 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8870}
8871
8872/// Lower the call to 'memset' intrinsic function into a series of store
8873/// operations.
8874///
8875/// \param DAG Selection DAG where lowered code is placed.
8876/// \param dl Link to corresponding IR location.
8877/// \param Chain Control flow dependency.
8878/// \param Dst Pointer to destination memory location.
8879/// \param Src Value of byte to write into the memory.
8880/// \param Size Number of bytes to write.
8881/// \param Alignment Alignment of the destination in bytes.
8882/// \param isVol True if destination is volatile.
8883/// \param AlwaysInline Makes sure no function call is generated.
8884/// \param DstPtrInfo IR information on the memory pointer.
8885/// \returns New head in the control flow, if lowering was successful, empty
8886/// SDValue otherwise.
8887///
8888/// The function tries to replace 'llvm.memset' intrinsic with several store
8889/// operations and value calculation code. This is usually profitable for small
8890/// memory size or when the semantic requires inlining.
8892 SDValue Chain, SDValue Dst, SDValue Src,
8893 uint64_t Size, Align Alignment, bool isVol,
8894 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8895 const AAMDNodes &AAInfo) {
8896 // Turn a memset of undef to nop.
8897 // FIXME: We need to honor volatile even is Src is undef.
8898 if (Src.isUndef())
8899 return Chain;
8900
8901 // Expand memset to a series of load/store ops if the size operand
8902 // falls below a certain threshold.
8903 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8904 std::vector<EVT> MemOps;
8905 bool DstAlignCanChange = false;
8906 LLVMContext &C = *DAG.getContext();
8908 MachineFrameInfo &MFI = MF.getFrameInfo();
8909 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8911 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8912 DstAlignCanChange = true;
8913 bool IsZeroVal = isNullConstant(Src);
8914 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8915
8916 if (!TLI.findOptimalMemOpLowering(
8917 C, MemOps, Limit,
8918 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8919 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8920 return SDValue();
8921
8922 if (DstAlignCanChange) {
8923 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8924 const DataLayout &DL = DAG.getDataLayout();
8925 Align NewAlign = DL.getABITypeAlign(Ty);
8926
8927 // Don't promote to an alignment that would require dynamic stack
8928 // realignment which may conflict with optimizations such as tail call
8929 // optimization.
8931 if (!TRI->hasStackRealignment(MF))
8932 if (MaybeAlign StackAlign = DL.getStackAlignment())
8933 NewAlign = std::min(NewAlign, *StackAlign);
8934
8935 if (NewAlign > Alignment) {
8936 // Give the stack frame object a larger alignment if needed.
8937 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8938 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8939 Alignment = NewAlign;
8940 }
8941 }
8942
8943 SmallVector<SDValue, 8> OutChains;
8944 uint64_t DstOff = 0;
8945 unsigned NumMemOps = MemOps.size();
8946
8947 // Find the largest store and generate the bit pattern for it.
8948 EVT LargestVT = MemOps[0];
8949 for (unsigned i = 1; i < NumMemOps; i++)
8950 if (MemOps[i].bitsGT(LargestVT))
8951 LargestVT = MemOps[i];
8952 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8953
8954 // Prepare AAInfo for loads/stores after lowering this memset.
8955 AAMDNodes NewAAInfo = AAInfo;
8956 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8957
8958 for (unsigned i = 0; i < NumMemOps; i++) {
8959 EVT VT = MemOps[i];
8960 unsigned VTSize = VT.getSizeInBits() / 8;
8961 if (VTSize > Size) {
8962 // Issuing an unaligned load / store pair that overlaps with the previous
8963 // pair. Adjust the offset accordingly.
8964 assert(i == NumMemOps-1 && i != 0);
8965 DstOff -= VTSize - Size;
8966 }
8967
8968 // If this store is smaller than the largest store see whether we can get
8969 // the smaller value for free with a truncate or extract vector element and
8970 // then store.
8971 SDValue Value = MemSetValue;
8972 if (VT.bitsLT(LargestVT)) {
8973 unsigned Index;
8974 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8975 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8976 if (!LargestVT.isVector() && !VT.isVector() &&
8977 TLI.isTruncateFree(LargestVT, VT))
8978 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8979 else if (LargestVT.isVector() && !VT.isVector() &&
8981 LargestVT.getTypeForEVT(*DAG.getContext()),
8982 VT.getSizeInBits(), Index) &&
8983 TLI.isTypeLegal(SVT) &&
8984 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
8985 // Target which can combine store(extractelement VectorTy, Idx) can get
8986 // the smaller value for free.
8987 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
8988 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
8989 } else
8990 Value = getMemsetValue(Src, VT, DAG, dl);
8991 }
8992 assert(Value.getValueType() == VT && "Value with wrong type.");
8993 SDValue Store = DAG.getStore(
8994 Chain, dl, Value,
8995 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8996 DstPtrInfo.getWithOffset(DstOff), Alignment,
8998 NewAAInfo);
8999 OutChains.push_back(Store);
9000 DstOff += VT.getSizeInBits() / 8;
9001 Size -= VTSize;
9002 }
9003
9004 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9005}
9006
9008 unsigned AS) {
9009 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9010 // pointer operands can be losslessly bitcasted to pointers of address space 0
9011 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9012 report_fatal_error("cannot lower memory intrinsic in address space " +
9013 Twine(AS));
9014 }
9015}
9016
9018 const SelectionDAG *SelDAG,
9019 bool AllowReturnsFirstArg) {
9020 if (!CI || !CI->isTailCall())
9021 return false;
9022 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9023 // helper symbol we lower to.
9024 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9025 AllowReturnsFirstArg &&
9027}
9028
9029std::pair<SDValue, SDValue>
9031 SDValue Mem1, SDValue Size, const CallInst *CI) {
9032 const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
9033 if (!LibCallName)
9034 return {};
9035
9038 {Mem0, PT},
9039 {Mem1, PT},
9041
9043 bool IsTailCall =
9044 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9045
9046 CLI.setDebugLoc(dl)
9047 .setChain(Chain)
9048 .setLibCallee(
9049 TLI->getLibcallCallingConv(RTLIB::MEMCMP),
9051 getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
9052 std::move(Args))
9053 .setTailCall(IsTailCall);
9054
9055 return TLI->LowerCallTo(CLI);
9056}
9057
9058std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9059 const SDLoc &dl,
9060 SDValue Src,
9061 const CallInst *CI) {
9062 const char *LibCallName = TLI->getLibcallName(RTLIB::STRLEN);
9063 if (!LibCallName)
9064 return {};
9065
9066 // Emit a library call.
9069
9071 bool IsTailCall =
9072 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9073
9074 CLI.setDebugLoc(dl)
9075 .setChain(Chain)
9076 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::STRLEN), CI->getType(),
9078 LibCallName, TLI->getProgramPointerTy(getDataLayout())),
9079 std::move(Args))
9080 .setTailCall(IsTailCall);
9081
9082 return TLI->LowerCallTo(CLI);
9083}
9084
9086 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9087 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9088 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9089 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9090 BatchAAResults *BatchAA) {
9091 // Check to see if we should lower the memcpy to loads and stores first.
9092 // For cases within the target-specified limits, this is the best choice.
9094 if (ConstantSize) {
9095 // Memcpy with size zero? Just return the original chain.
9096 if (ConstantSize->isZero())
9097 return Chain;
9098
9100 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9101 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9102 if (Result.getNode())
9103 return Result;
9104 }
9105
9106 // Then check to see if we should lower the memcpy with target-specific
9107 // code. If the target chooses to do this, this is the next best.
9108 if (TSI) {
9109 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9110 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9111 DstPtrInfo, SrcPtrInfo);
9112 if (Result.getNode())
9113 return Result;
9114 }
9115
9116 // If we really need inline code and the target declined to provide it,
9117 // use a (potentially long) sequence of loads and stores.
9118 if (AlwaysInline) {
9119 assert(ConstantSize && "AlwaysInline requires a constant size!");
9121 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9122 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9123 }
9124
9127
9128 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9129 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9130 // respect volatile, so they may do things like read or write memory
9131 // beyond the given memory regions. But fixing this isn't easy, and most
9132 // people don't care.
9133
9134 // Emit a library call.
9137 Args.emplace_back(Dst, PtrTy);
9138 Args.emplace_back(Src, PtrTy);
9139 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9140 // FIXME: pass in SDLoc
9142 bool IsTailCall = false;
9143 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9144
9145 if (OverrideTailCall.has_value()) {
9146 IsTailCall = *OverrideTailCall;
9147 } else {
9148 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9149 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9150 }
9151
9152 CLI.setDebugLoc(dl)
9153 .setChain(Chain)
9154 .setLibCallee(
9155 TLI->getLibcallImplCallingConv(MemCpyImpl),
9156 Dst.getValueType().getTypeForEVT(*getContext()),
9157 getExternalSymbol(TLI->getLibcallImplName(MemCpyImpl).data(),
9158 TLI->getPointerTy(getDataLayout())),
9159 std::move(Args))
9161 .setTailCall(IsTailCall);
9162
9163 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9164 return CallResult.second;
9165}
9166
9168 SDValue Dst, SDValue Src, SDValue Size,
9169 Type *SizeTy, unsigned ElemSz,
9170 bool isTailCall,
9171 MachinePointerInfo DstPtrInfo,
9172 MachinePointerInfo SrcPtrInfo) {
9173 // Emit a library call.
9176 Args.emplace_back(Dst, ArgTy);
9177 Args.emplace_back(Src, ArgTy);
9178 Args.emplace_back(Size, SizeTy);
9179
9180 RTLIB::Libcall LibraryCall =
9182 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9183 report_fatal_error("Unsupported element size");
9184
9186 CLI.setDebugLoc(dl)
9187 .setChain(Chain)
9188 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9190 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9191 TLI->getPointerTy(getDataLayout())),
9192 std::move(Args))
9194 .setTailCall(isTailCall);
9195
9196 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9197 return CallResult.second;
9198}
9199
9201 SDValue Src, SDValue Size, Align Alignment,
9202 bool isVol, const CallInst *CI,
9203 std::optional<bool> OverrideTailCall,
9204 MachinePointerInfo DstPtrInfo,
9205 MachinePointerInfo SrcPtrInfo,
9206 const AAMDNodes &AAInfo,
9207 BatchAAResults *BatchAA) {
9208 // Check to see if we should lower the memmove to loads and stores first.
9209 // For cases within the target-specified limits, this is the best choice.
9211 if (ConstantSize) {
9212 // Memmove with size zero? Just return the original chain.
9213 if (ConstantSize->isZero())
9214 return Chain;
9215
9217 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9218 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9219 if (Result.getNode())
9220 return Result;
9221 }
9222
9223 // Then check to see if we should lower the memmove with target-specific
9224 // code. If the target chooses to do this, this is the next best.
9225 if (TSI) {
9226 SDValue Result =
9227 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9228 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9229 if (Result.getNode())
9230 return Result;
9231 }
9232
9235
9236 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9237 // not be safe. See memcpy above for more details.
9238
9239 // Emit a library call.
9242 Args.emplace_back(Dst, PtrTy);
9243 Args.emplace_back(Src, PtrTy);
9244 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9245 // FIXME: pass in SDLoc
9247
9248 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9249
9250 bool IsTailCall = false;
9251 if (OverrideTailCall.has_value()) {
9252 IsTailCall = *OverrideTailCall;
9253 } else {
9254 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9255 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9256 }
9257
9258 CLI.setDebugLoc(dl)
9259 .setChain(Chain)
9260 .setLibCallee(
9261 TLI->getLibcallImplCallingConv(MemmoveImpl),
9262 Dst.getValueType().getTypeForEVT(*getContext()),
9263 getExternalSymbol(TLI->getLibcallImplName(MemmoveImpl).data(),
9264 TLI->getPointerTy(getDataLayout())),
9265 std::move(Args))
9267 .setTailCall(IsTailCall);
9268
9269 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9270 return CallResult.second;
9271}
9272
9274 SDValue Dst, SDValue Src, SDValue Size,
9275 Type *SizeTy, unsigned ElemSz,
9276 bool isTailCall,
9277 MachinePointerInfo DstPtrInfo,
9278 MachinePointerInfo SrcPtrInfo) {
9279 // Emit a library call.
9281 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9282 Args.emplace_back(Dst, IntPtrTy);
9283 Args.emplace_back(Src, IntPtrTy);
9284 Args.emplace_back(Size, SizeTy);
9285
9286 RTLIB::Libcall LibraryCall =
9288 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9289 report_fatal_error("Unsupported element size");
9290
9292 CLI.setDebugLoc(dl)
9293 .setChain(Chain)
9294 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9296 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9297 TLI->getPointerTy(getDataLayout())),
9298 std::move(Args))
9300 .setTailCall(isTailCall);
9301
9302 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9303 return CallResult.second;
9304}
9305
9307 SDValue Src, SDValue Size, Align Alignment,
9308 bool isVol, bool AlwaysInline,
9309 const CallInst *CI,
9310 MachinePointerInfo DstPtrInfo,
9311 const AAMDNodes &AAInfo) {
9312 // Check to see if we should lower the memset to stores first.
9313 // For cases within the target-specified limits, this is the best choice.
9315 if (ConstantSize) {
9316 // Memset with size zero? Just return the original chain.
9317 if (ConstantSize->isZero())
9318 return Chain;
9319
9320 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9321 ConstantSize->getZExtValue(), Alignment,
9322 isVol, false, DstPtrInfo, AAInfo);
9323
9324 if (Result.getNode())
9325 return Result;
9326 }
9327
9328 // Then check to see if we should lower the memset with target-specific
9329 // code. If the target chooses to do this, this is the next best.
9330 if (TSI) {
9331 SDValue Result = TSI->EmitTargetCodeForMemset(
9332 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9333 if (Result.getNode())
9334 return Result;
9335 }
9336
9337 // If we really need inline code and the target declined to provide it,
9338 // use a (potentially long) sequence of loads and stores.
9339 if (AlwaysInline) {
9340 assert(ConstantSize && "AlwaysInline requires a constant size!");
9341 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9342 ConstantSize->getZExtValue(), Alignment,
9343 isVol, true, DstPtrInfo, AAInfo);
9344 assert(Result &&
9345 "getMemsetStores must return a valid sequence when AlwaysInline");
9346 return Result;
9347 }
9348
9350
9351 // Emit a library call.
9352 auto &Ctx = *getContext();
9353 const auto& DL = getDataLayout();
9354
9356 // FIXME: pass in SDLoc
9357 CLI.setDebugLoc(dl).setChain(Chain);
9358
9359 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
9360
9361 bool UseBZero = isNullConstant(Src) && BzeroName;
9362 // If zeroing out and bzero is present, use it.
9363 if (UseBZero) {
9365 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9366 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9367 CLI.setLibCallee(
9368 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
9369 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
9370 } else {
9372 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9373 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9374 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9375 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
9376 Dst.getValueType().getTypeForEVT(Ctx),
9377 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
9378 TLI->getPointerTy(DL)),
9379 std::move(Args));
9380 }
9381
9382 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9383 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9384
9385 // If we're going to use bzero, make sure not to tail call unless the
9386 // subsequent return doesn't need a value, as bzero doesn't return the first
9387 // arg unlike memset.
9388 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9389 bool IsTailCall =
9390 CI && CI->isTailCall() &&
9391 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9392 CLI.setDiscardResult().setTailCall(IsTailCall);
9393
9394 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9395 return CallResult.second;
9396}
9397
9400 Type *SizeTy, unsigned ElemSz,
9401 bool isTailCall,
9402 MachinePointerInfo DstPtrInfo) {
9403 // Emit a library call.
9405 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9406 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9407 Args.emplace_back(Size, SizeTy);
9408
9409 RTLIB::Libcall LibraryCall =
9411 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9412 report_fatal_error("Unsupported element size");
9413
9415 CLI.setDebugLoc(dl)
9416 .setChain(Chain)
9417 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9419 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9420 TLI->getPointerTy(getDataLayout())),
9421 std::move(Args))
9423 .setTailCall(isTailCall);
9424
9425 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9426 return CallResult.second;
9427}
9428
9429SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9431 MachineMemOperand *MMO,
9432 ISD::LoadExtType ExtType) {
9434 AddNodeIDNode(ID, Opcode, VTList, Ops);
9435 ID.AddInteger(MemVT.getRawBits());
9436 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9437 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9438 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9439 ID.AddInteger(MMO->getFlags());
9440 void* IP = nullptr;
9441 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9442 E->refineAlignment(MMO);
9443 E->refineRanges(MMO);
9444 return SDValue(E, 0);
9445 }
9446
9447 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9448 VTList, MemVT, MMO, ExtType);
9449 createOperands(N, Ops);
9450
9451 CSEMap.InsertNode(N, IP);
9452 InsertNode(N);
9453 SDValue V(N, 0);
9454 NewSDValueDbgMsg(V, "Creating new node: ", this);
9455 return V;
9456}
9457
9459 EVT MemVT, SDVTList VTs, SDValue Chain,
9460 SDValue Ptr, SDValue Cmp, SDValue Swp,
9461 MachineMemOperand *MMO) {
9462 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9463 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
9464 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9465
9466 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9467 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9468}
9469
9470SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9471 SDValue Chain, SDValue Ptr, SDValue Val,
9472 MachineMemOperand *MMO) {
9473 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9474 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9475 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9476 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9477 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9478 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9479 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9480 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9481 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9482 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9483 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9484 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9485 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9486 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9487 Opcode == ISD::ATOMIC_STORE) &&
9488 "Invalid Atomic Op");
9489
9490 EVT VT = Val.getValueType();
9491
9492 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9493 getVTList(VT, MVT::Other);
9494 SDValue Ops[] = {Chain, Ptr, Val};
9495 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9496}
9497
9499 EVT MemVT, EVT VT, SDValue Chain,
9501 SDVTList VTs = getVTList(VT, MVT::Other);
9502 SDValue Ops[] = {Chain, Ptr};
9503 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9504}
9505
9506/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9508 if (Ops.size() == 1)
9509 return Ops[0];
9510
9512 VTs.reserve(Ops.size());
9513 for (const SDValue &Op : Ops)
9514 VTs.push_back(Op.getValueType());
9515 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9516}
9517
9519 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9520 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9522 const AAMDNodes &AAInfo) {
9523 if (Size.hasValue() && !Size.getValue())
9525
9527 MachineMemOperand *MMO =
9528 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9529
9530 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9531}
9532
9534 SDVTList VTList,
9535 ArrayRef<SDValue> Ops, EVT MemVT,
9536 MachineMemOperand *MMO) {
9537 assert(
9538 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9539 Opcode == ISD::PREFETCH ||
9540 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9541 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9542 "Opcode is not a memory-accessing opcode!");
9543
9544 // Memoize the node unless it returns a glue result.
9546 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9548 AddNodeIDNode(ID, Opcode, VTList, Ops);
9549 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9550 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9551 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9552 ID.AddInteger(MMO->getFlags());
9553 ID.AddInteger(MemVT.getRawBits());
9554 void *IP = nullptr;
9555 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9556 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9557 return SDValue(E, 0);
9558 }
9559
9560 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9561 VTList, MemVT, MMO);
9562 createOperands(N, Ops);
9563
9564 CSEMap.InsertNode(N, IP);
9565 } else {
9566 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9567 VTList, MemVT, MMO);
9568 createOperands(N, Ops);
9569 }
9570 InsertNode(N);
9571 SDValue V(N, 0);
9572 NewSDValueDbgMsg(V, "Creating new node: ", this);
9573 return V;
9574}
9575
9577 SDValue Chain, int FrameIndex) {
9578 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9579 const auto VTs = getVTList(MVT::Other);
9580 SDValue Ops[2] = {
9581 Chain,
9582 getFrameIndex(FrameIndex,
9583 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9584 true)};
9585
9587 AddNodeIDNode(ID, Opcode, VTs, Ops);
9588 ID.AddInteger(FrameIndex);
9589 void *IP = nullptr;
9590 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9591 return SDValue(E, 0);
9592
9593 LifetimeSDNode *N =
9594 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9595 createOperands(N, Ops);
9596 CSEMap.InsertNode(N, IP);
9597 InsertNode(N);
9598 SDValue V(N, 0);
9599 NewSDValueDbgMsg(V, "Creating new node: ", this);
9600 return V;
9601}
9602
9604 uint64_t Guid, uint64_t Index,
9605 uint32_t Attr) {
9606 const unsigned Opcode = ISD::PSEUDO_PROBE;
9607 const auto VTs = getVTList(MVT::Other);
9608 SDValue Ops[] = {Chain};
9610 AddNodeIDNode(ID, Opcode, VTs, Ops);
9611 ID.AddInteger(Guid);
9612 ID.AddInteger(Index);
9613 void *IP = nullptr;
9614 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9615 return SDValue(E, 0);
9616
9617 auto *N = newSDNode<PseudoProbeSDNode>(
9618 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9619 createOperands(N, Ops);
9620 CSEMap.InsertNode(N, IP);
9621 InsertNode(N);
9622 SDValue V(N, 0);
9623 NewSDValueDbgMsg(V, "Creating new node: ", this);
9624 return V;
9625}
9626
9627/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9628/// MachinePointerInfo record from it. This is particularly useful because the
9629/// code generator has many cases where it doesn't bother passing in a
9630/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9632 SelectionDAG &DAG, SDValue Ptr,
9633 int64_t Offset = 0) {
9634 // If this is FI+Offset, we can model it.
9637 FI->getIndex(), Offset);
9638
9639 // If this is (FI+Offset1)+Offset2, we can model it.
9640 if (Ptr.getOpcode() != ISD::ADD ||
9641 !isa<ConstantSDNode>(Ptr.getOperand(1)) ||
9642 !isa<FrameIndexSDNode>(Ptr.getOperand(0)))
9643 return Info;
9644
9645 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9647 DAG.getMachineFunction(), FI,
9648 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9649}
9650
9651/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9652/// MachinePointerInfo record from it. This is particularly useful because the
9653/// code generator has many cases where it doesn't bother passing in a
9654/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9656 SelectionDAG &DAG, SDValue Ptr,
9657 SDValue OffsetOp) {
9658 // If the 'Offset' value isn't a constant, we can't handle this.
9660 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9661 if (OffsetOp.isUndef())
9662 return InferPointerInfo(Info, DAG, Ptr);
9663 return Info;
9664}
9665
9667 EVT VT, const SDLoc &dl, SDValue Chain,
9669 MachinePointerInfo PtrInfo, EVT MemVT,
9670 Align Alignment,
9671 MachineMemOperand::Flags MMOFlags,
9672 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9673 assert(Chain.getValueType() == MVT::Other &&
9674 "Invalid chain type");
9675
9676 MMOFlags |= MachineMemOperand::MOLoad;
9677 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9678 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9679 // clients.
9680 if (PtrInfo.V.isNull())
9681 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9682
9683 TypeSize Size = MemVT.getStoreSize();
9685 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9686 Alignment, AAInfo, Ranges);
9687 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9688}
9689
9691 EVT VT, const SDLoc &dl, SDValue Chain,
9692 SDValue Ptr, SDValue Offset, EVT MemVT,
9693 MachineMemOperand *MMO) {
9694 if (VT == MemVT) {
9695 ExtType = ISD::NON_EXTLOAD;
9696 } else if (ExtType == ISD::NON_EXTLOAD) {
9697 assert(VT == MemVT && "Non-extending load from different memory type!");
9698 } else {
9699 // Extending load.
9700 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9701 "Should only be an extending load, not truncating!");
9702 assert(VT.isInteger() == MemVT.isInteger() &&
9703 "Cannot convert from FP to Int or Int -> FP!");
9704 assert(VT.isVector() == MemVT.isVector() &&
9705 "Cannot use an ext load to convert to or from a vector!");
9706 assert((!VT.isVector() ||
9708 "Cannot use an ext load to change the number of vector elements!");
9709 }
9710
9711 assert((!MMO->getRanges() ||
9713 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9714 MemVT.isInteger())) &&
9715 "Range metadata and load type must match!");
9716
9717 bool Indexed = AM != ISD::UNINDEXED;
9718 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9719
9720 SDVTList VTs = Indexed ?
9721 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9722 SDValue Ops[] = { Chain, Ptr, Offset };
9724 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9725 ID.AddInteger(MemVT.getRawBits());
9726 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9727 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9728 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9729 ID.AddInteger(MMO->getFlags());
9730 void *IP = nullptr;
9731 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9732 E->refineAlignment(MMO);
9733 E->refineRanges(MMO);
9734 return SDValue(E, 0);
9735 }
9736 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9737 ExtType, MemVT, MMO);
9738 createOperands(N, Ops);
9739
9740 CSEMap.InsertNode(N, IP);
9741 InsertNode(N);
9742 SDValue V(N, 0);
9743 NewSDValueDbgMsg(V, "Creating new node: ", this);
9744 return V;
9745}
9746
9749 MaybeAlign Alignment,
9750 MachineMemOperand::Flags MMOFlags,
9751 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9752 SDValue Undef = getUNDEF(Ptr.getValueType());
9753 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9754 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9755}
9756
9759 SDValue Undef = getUNDEF(Ptr.getValueType());
9760 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9761 VT, MMO);
9762}
9763
9765 EVT VT, SDValue Chain, SDValue Ptr,
9766 MachinePointerInfo PtrInfo, EVT MemVT,
9767 MaybeAlign Alignment,
9768 MachineMemOperand::Flags MMOFlags,
9769 const AAMDNodes &AAInfo) {
9770 SDValue Undef = getUNDEF(Ptr.getValueType());
9771 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9772 MemVT, Alignment, MMOFlags, AAInfo);
9773}
9774
9776 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9777 MachineMemOperand *MMO) {
9778 SDValue Undef = getUNDEF(Ptr.getValueType());
9779 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9780 MemVT, MMO);
9781}
9782
9786 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9787 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9788 // Don't propagate the invariant or dereferenceable flags.
9789 auto MMOFlags =
9790 LD->getMemOperand()->getFlags() &
9792 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9793 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9794 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9795}
9796
9799 Align Alignment,
9800 MachineMemOperand::Flags MMOFlags,
9801 const AAMDNodes &AAInfo) {
9802 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9803
9804 MMOFlags |= MachineMemOperand::MOStore;
9805 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9806
9807 if (PtrInfo.V.isNull())
9808 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9809
9812 MachineMemOperand *MMO =
9813 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9814 return getStore(Chain, dl, Val, Ptr, MMO);
9815}
9816
9819 SDValue Undef = getUNDEF(Ptr.getValueType());
9820 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9822}
9823
9827 bool IsTruncating) {
9828 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9829 EVT VT = Val.getValueType();
9830 if (VT == SVT) {
9831 IsTruncating = false;
9832 } else if (!IsTruncating) {
9833 assert(VT == SVT && "No-truncating store from different memory type!");
9834 } else {
9836 "Should only be a truncating store, not extending!");
9837 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9838 assert(VT.isVector() == SVT.isVector() &&
9839 "Cannot use trunc store to convert to or from a vector!");
9840 assert((!VT.isVector() ||
9842 "Cannot use trunc store to change the number of vector elements!");
9843 }
9844
9845 bool Indexed = AM != ISD::UNINDEXED;
9846 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9847 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9848 : getVTList(MVT::Other);
9849 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9851 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9852 ID.AddInteger(SVT.getRawBits());
9853 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9854 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9855 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9856 ID.AddInteger(MMO->getFlags());
9857 void *IP = nullptr;
9858 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9859 cast<StoreSDNode>(E)->refineAlignment(MMO);
9860 return SDValue(E, 0);
9861 }
9862 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9863 IsTruncating, SVT, MMO);
9864 createOperands(N, Ops);
9865
9866 CSEMap.InsertNode(N, IP);
9867 InsertNode(N);
9868 SDValue V(N, 0);
9869 NewSDValueDbgMsg(V, "Creating new node: ", this);
9870 return V;
9871}
9872
9875 EVT SVT, Align Alignment,
9876 MachineMemOperand::Flags MMOFlags,
9877 const AAMDNodes &AAInfo) {
9878 assert(Chain.getValueType() == MVT::Other &&
9879 "Invalid chain type");
9880
9881 MMOFlags |= MachineMemOperand::MOStore;
9882 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9883
9884 if (PtrInfo.V.isNull())
9885 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9886
9888 MachineMemOperand *MMO = MF.getMachineMemOperand(
9889 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9890 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9891}
9892
9894 SDValue Ptr, EVT SVT,
9895 MachineMemOperand *MMO) {
9896 SDValue Undef = getUNDEF(Ptr.getValueType());
9897 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9898}
9899
9903 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9904 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9905 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9906 ST->getMemoryVT(), ST->getMemOperand(), AM,
9907 ST->isTruncatingStore());
9908}
9909
9911 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9912 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9913 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9914 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9915 const MDNode *Ranges, bool IsExpanding) {
9916 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9917
9918 MMOFlags |= MachineMemOperand::MOLoad;
9919 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9920 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9921 // clients.
9922 if (PtrInfo.V.isNull())
9923 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9924
9925 TypeSize Size = MemVT.getStoreSize();
9927 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9928 Alignment, AAInfo, Ranges);
9929 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9930 MMO, IsExpanding);
9931}
9932
9934 ISD::LoadExtType ExtType, EVT VT,
9935 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9936 SDValue Offset, SDValue Mask, SDValue EVL,
9937 EVT MemVT, MachineMemOperand *MMO,
9938 bool IsExpanding) {
9939 bool Indexed = AM != ISD::UNINDEXED;
9940 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9941
9942 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9943 : getVTList(VT, MVT::Other);
9944 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9946 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9947 ID.AddInteger(MemVT.getRawBits());
9948 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9949 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9950 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9951 ID.AddInteger(MMO->getFlags());
9952 void *IP = nullptr;
9953 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9954 E->refineAlignment(MMO);
9955 E->refineRanges(MMO);
9956 return SDValue(E, 0);
9957 }
9958 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9959 ExtType, IsExpanding, MemVT, MMO);
9960 createOperands(N, Ops);
9961
9962 CSEMap.InsertNode(N, IP);
9963 InsertNode(N);
9964 SDValue V(N, 0);
9965 NewSDValueDbgMsg(V, "Creating new node: ", this);
9966 return V;
9967}
9968
9970 SDValue Ptr, SDValue Mask, SDValue EVL,
9971 MachinePointerInfo PtrInfo,
9972 MaybeAlign Alignment,
9973 MachineMemOperand::Flags MMOFlags,
9974 const AAMDNodes &AAInfo, const MDNode *Ranges,
9975 bool IsExpanding) {
9976 SDValue Undef = getUNDEF(Ptr.getValueType());
9977 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9978 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
9979 IsExpanding);
9980}
9981
9983 SDValue Ptr, SDValue Mask, SDValue EVL,
9984 MachineMemOperand *MMO, bool IsExpanding) {
9985 SDValue Undef = getUNDEF(Ptr.getValueType());
9986 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9987 Mask, EVL, VT, MMO, IsExpanding);
9988}
9989
9991 EVT VT, SDValue Chain, SDValue Ptr,
9992 SDValue Mask, SDValue EVL,
9993 MachinePointerInfo PtrInfo, EVT MemVT,
9994 MaybeAlign Alignment,
9995 MachineMemOperand::Flags MMOFlags,
9996 const AAMDNodes &AAInfo, bool IsExpanding) {
9997 SDValue Undef = getUNDEF(Ptr.getValueType());
9998 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
9999 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10000 IsExpanding);
10001}
10002
10004 EVT VT, SDValue Chain, SDValue Ptr,
10005 SDValue Mask, SDValue EVL, EVT MemVT,
10006 MachineMemOperand *MMO, bool IsExpanding) {
10007 SDValue Undef = getUNDEF(Ptr.getValueType());
10008 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10009 EVL, MemVT, MMO, IsExpanding);
10010}
10011
10015 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10016 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10017 // Don't propagate the invariant or dereferenceable flags.
10018 auto MMOFlags =
10019 LD->getMemOperand()->getFlags() &
10021 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10022 LD->getChain(), Base, Offset, LD->getMask(),
10023 LD->getVectorLength(), LD->getPointerInfo(),
10024 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10025 nullptr, LD->isExpandingLoad());
10026}
10027
10030 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10031 ISD::MemIndexedMode AM, bool IsTruncating,
10032 bool IsCompressing) {
10033 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10034 bool Indexed = AM != ISD::UNINDEXED;
10035 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10036 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10037 : getVTList(MVT::Other);
10038 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10040 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10041 ID.AddInteger(MemVT.getRawBits());
10042 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10043 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10044 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10045 ID.AddInteger(MMO->getFlags());
10046 void *IP = nullptr;
10047 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10048 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10049 return SDValue(E, 0);
10050 }
10051 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10052 IsTruncating, IsCompressing, MemVT, MMO);
10053 createOperands(N, Ops);
10054
10055 CSEMap.InsertNode(N, IP);
10056 InsertNode(N);
10057 SDValue V(N, 0);
10058 NewSDValueDbgMsg(V, "Creating new node: ", this);
10059 return V;
10060}
10061
10063 SDValue Val, SDValue Ptr, SDValue Mask,
10064 SDValue EVL, MachinePointerInfo PtrInfo,
10065 EVT SVT, Align Alignment,
10066 MachineMemOperand::Flags MMOFlags,
10067 const AAMDNodes &AAInfo,
10068 bool IsCompressing) {
10069 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10070
10071 MMOFlags |= MachineMemOperand::MOStore;
10072 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10073
10074 if (PtrInfo.V.isNull())
10075 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10076
10078 MachineMemOperand *MMO = MF.getMachineMemOperand(
10079 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10080 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10081 IsCompressing);
10082}
10083
10085 SDValue Val, SDValue Ptr, SDValue Mask,
10086 SDValue EVL, EVT SVT,
10087 MachineMemOperand *MMO,
10088 bool IsCompressing) {
10089 EVT VT = Val.getValueType();
10090
10091 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10092 if (VT == SVT)
10093 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10094 EVL, VT, MMO, ISD::UNINDEXED,
10095 /*IsTruncating*/ false, IsCompressing);
10096
10098 "Should only be a truncating store, not extending!");
10099 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10100 assert(VT.isVector() == SVT.isVector() &&
10101 "Cannot use trunc store to convert to or from a vector!");
10102 assert((!VT.isVector() ||
10104 "Cannot use trunc store to change the number of vector elements!");
10105
10106 SDVTList VTs = getVTList(MVT::Other);
10107 SDValue Undef = getUNDEF(Ptr.getValueType());
10108 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10110 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10111 ID.AddInteger(SVT.getRawBits());
10112 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10113 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10114 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10115 ID.AddInteger(MMO->getFlags());
10116 void *IP = nullptr;
10117 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10118 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10119 return SDValue(E, 0);
10120 }
10121 auto *N =
10122 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10123 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10124 createOperands(N, Ops);
10125
10126 CSEMap.InsertNode(N, IP);
10127 InsertNode(N);
10128 SDValue V(N, 0);
10129 NewSDValueDbgMsg(V, "Creating new node: ", this);
10130 return V;
10131}
10132
10136 auto *ST = cast<VPStoreSDNode>(OrigStore);
10137 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10138 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10139 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10140 Offset, ST->getMask(), ST->getVectorLength()};
10142 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10143 ID.AddInteger(ST->getMemoryVT().getRawBits());
10144 ID.AddInteger(ST->getRawSubclassData());
10145 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10146 ID.AddInteger(ST->getMemOperand()->getFlags());
10147 void *IP = nullptr;
10148 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10149 return SDValue(E, 0);
10150
10151 auto *N = newSDNode<VPStoreSDNode>(
10152 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10153 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10154 createOperands(N, Ops);
10155
10156 CSEMap.InsertNode(N, IP);
10157 InsertNode(N);
10158 SDValue V(N, 0);
10159 NewSDValueDbgMsg(V, "Creating new node: ", this);
10160 return V;
10161}
10162
10164 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10165 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10166 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10167 bool Indexed = AM != ISD::UNINDEXED;
10168 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10169
10170 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10171 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10172 : getVTList(VT, MVT::Other);
10174 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10175 ID.AddInteger(VT.getRawBits());
10176 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10177 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10178 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10179
10180 void *IP = nullptr;
10181 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10182 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10183 return SDValue(E, 0);
10184 }
10185
10186 auto *N =
10187 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10188 ExtType, IsExpanding, MemVT, MMO);
10189 createOperands(N, Ops);
10190 CSEMap.InsertNode(N, IP);
10191 InsertNode(N);
10192 SDValue V(N, 0);
10193 NewSDValueDbgMsg(V, "Creating new node: ", this);
10194 return V;
10195}
10196
10198 SDValue Ptr, SDValue Stride,
10199 SDValue Mask, SDValue EVL,
10200 MachineMemOperand *MMO,
10201 bool IsExpanding) {
10202 SDValue Undef = getUNDEF(Ptr.getValueType());
10204 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10205}
10206
10208 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10209 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10210 MachineMemOperand *MMO, bool IsExpanding) {
10211 SDValue Undef = getUNDEF(Ptr.getValueType());
10212 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10213 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10214}
10215
10217 SDValue Val, SDValue Ptr,
10218 SDValue Offset, SDValue Stride,
10219 SDValue Mask, SDValue EVL, EVT MemVT,
10220 MachineMemOperand *MMO,
10222 bool IsTruncating, bool IsCompressing) {
10223 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10224 bool Indexed = AM != ISD::UNINDEXED;
10225 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10226 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10227 : getVTList(MVT::Other);
10228 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10230 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10231 ID.AddInteger(MemVT.getRawBits());
10232 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10233 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10234 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10235 void *IP = nullptr;
10236 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10237 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10238 return SDValue(E, 0);
10239 }
10240 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10241 VTs, AM, IsTruncating,
10242 IsCompressing, MemVT, MMO);
10243 createOperands(N, Ops);
10244
10245 CSEMap.InsertNode(N, IP);
10246 InsertNode(N);
10247 SDValue V(N, 0);
10248 NewSDValueDbgMsg(V, "Creating new node: ", this);
10249 return V;
10250}
10251
10253 SDValue Val, SDValue Ptr,
10254 SDValue Stride, SDValue Mask,
10255 SDValue EVL, EVT SVT,
10256 MachineMemOperand *MMO,
10257 bool IsCompressing) {
10258 EVT VT = Val.getValueType();
10259
10260 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10261 if (VT == SVT)
10262 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10263 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10264 /*IsTruncating*/ false, IsCompressing);
10265
10267 "Should only be a truncating store, not extending!");
10268 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10269 assert(VT.isVector() == SVT.isVector() &&
10270 "Cannot use trunc store to convert to or from a vector!");
10271 assert((!VT.isVector() ||
10273 "Cannot use trunc store to change the number of vector elements!");
10274
10275 SDVTList VTs = getVTList(MVT::Other);
10276 SDValue Undef = getUNDEF(Ptr.getValueType());
10277 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10279 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10280 ID.AddInteger(SVT.getRawBits());
10281 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10282 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10283 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10284 void *IP = nullptr;
10285 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10286 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10287 return SDValue(E, 0);
10288 }
10289 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10290 VTs, ISD::UNINDEXED, true,
10291 IsCompressing, SVT, MMO);
10292 createOperands(N, Ops);
10293
10294 CSEMap.InsertNode(N, IP);
10295 InsertNode(N);
10296 SDValue V(N, 0);
10297 NewSDValueDbgMsg(V, "Creating new node: ", this);
10298 return V;
10299}
10300
10303 ISD::MemIndexType IndexType) {
10304 assert(Ops.size() == 6 && "Incompatible number of operands");
10305
10307 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10308 ID.AddInteger(VT.getRawBits());
10309 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10310 dl.getIROrder(), VTs, VT, MMO, IndexType));
10311 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10312 ID.AddInteger(MMO->getFlags());
10313 void *IP = nullptr;
10314 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10315 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10316 return SDValue(E, 0);
10317 }
10318
10319 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10320 VT, MMO, IndexType);
10321 createOperands(N, Ops);
10322
10323 assert(N->getMask().getValueType().getVectorElementCount() ==
10324 N->getValueType(0).getVectorElementCount() &&
10325 "Vector width mismatch between mask and data");
10326 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10327 N->getValueType(0).getVectorElementCount().isScalable() &&
10328 "Scalable flags of index and data do not match");
10330 N->getIndex().getValueType().getVectorElementCount(),
10331 N->getValueType(0).getVectorElementCount()) &&
10332 "Vector width mismatch between index and data");
10333 assert(isa<ConstantSDNode>(N->getScale()) &&
10334 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10335 "Scale should be a constant power of 2");
10336
10337 CSEMap.InsertNode(N, IP);
10338 InsertNode(N);
10339 SDValue V(N, 0);
10340 NewSDValueDbgMsg(V, "Creating new node: ", this);
10341 return V;
10342}
10343
10346 MachineMemOperand *MMO,
10347 ISD::MemIndexType IndexType) {
10348 assert(Ops.size() == 7 && "Incompatible number of operands");
10349
10351 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10352 ID.AddInteger(VT.getRawBits());
10353 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10354 dl.getIROrder(), VTs, VT, MMO, IndexType));
10355 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10356 ID.AddInteger(MMO->getFlags());
10357 void *IP = nullptr;
10358 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10359 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10360 return SDValue(E, 0);
10361 }
10362 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10363 VT, MMO, IndexType);
10364 createOperands(N, Ops);
10365
10366 assert(N->getMask().getValueType().getVectorElementCount() ==
10367 N->getValue().getValueType().getVectorElementCount() &&
10368 "Vector width mismatch between mask and data");
10369 assert(
10370 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10371 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10372 "Scalable flags of index and data do not match");
10374 N->getIndex().getValueType().getVectorElementCount(),
10375 N->getValue().getValueType().getVectorElementCount()) &&
10376 "Vector width mismatch between index and data");
10377 assert(isa<ConstantSDNode>(N->getScale()) &&
10378 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10379 "Scale should be a constant power of 2");
10380
10381 CSEMap.InsertNode(N, IP);
10382 InsertNode(N);
10383 SDValue V(N, 0);
10384 NewSDValueDbgMsg(V, "Creating new node: ", this);
10385 return V;
10386}
10387
10390 SDValue PassThru, EVT MemVT,
10391 MachineMemOperand *MMO,
10393 ISD::LoadExtType ExtTy, bool isExpanding) {
10394 bool Indexed = AM != ISD::UNINDEXED;
10395 assert((Indexed || Offset.isUndef()) &&
10396 "Unindexed masked load with an offset!");
10397 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10398 : getVTList(VT, MVT::Other);
10399 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10401 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10402 ID.AddInteger(MemVT.getRawBits());
10403 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10404 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10405 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10406 ID.AddInteger(MMO->getFlags());
10407 void *IP = nullptr;
10408 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10409 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10410 return SDValue(E, 0);
10411 }
10412 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10413 AM, ExtTy, isExpanding, MemVT, MMO);
10414 createOperands(N, Ops);
10415
10416 CSEMap.InsertNode(N, IP);
10417 InsertNode(N);
10418 SDValue V(N, 0);
10419 NewSDValueDbgMsg(V, "Creating new node: ", this);
10420 return V;
10421}
10422
10427 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10428 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10429 Offset, LD->getMask(), LD->getPassThru(),
10430 LD->getMemoryVT(), LD->getMemOperand(), AM,
10431 LD->getExtensionType(), LD->isExpandingLoad());
10432}
10433
10436 SDValue Mask, EVT MemVT,
10437 MachineMemOperand *MMO,
10438 ISD::MemIndexedMode AM, bool IsTruncating,
10439 bool IsCompressing) {
10440 assert(Chain.getValueType() == MVT::Other &&
10441 "Invalid chain type");
10442 bool Indexed = AM != ISD::UNINDEXED;
10443 assert((Indexed || Offset.isUndef()) &&
10444 "Unindexed masked store with an offset!");
10445 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10446 : getVTList(MVT::Other);
10447 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10449 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10450 ID.AddInteger(MemVT.getRawBits());
10451 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10452 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10453 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10454 ID.AddInteger(MMO->getFlags());
10455 void *IP = nullptr;
10456 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10457 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10458 return SDValue(E, 0);
10459 }
10460 auto *N =
10461 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10462 IsTruncating, IsCompressing, MemVT, MMO);
10463 createOperands(N, Ops);
10464
10465 CSEMap.InsertNode(N, IP);
10466 InsertNode(N);
10467 SDValue V(N, 0);
10468 NewSDValueDbgMsg(V, "Creating new node: ", this);
10469 return V;
10470}
10471
10476 assert(ST->getOffset().isUndef() &&
10477 "Masked store is already a indexed store!");
10478 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10479 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10480 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10481}
10482
10485 MachineMemOperand *MMO,
10486 ISD::MemIndexType IndexType,
10487 ISD::LoadExtType ExtTy) {
10488 assert(Ops.size() == 6 && "Incompatible number of operands");
10489
10491 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10492 ID.AddInteger(MemVT.getRawBits());
10493 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10494 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10495 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10496 ID.AddInteger(MMO->getFlags());
10497 void *IP = nullptr;
10498 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10499 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10500 return SDValue(E, 0);
10501 }
10502
10503 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10504 VTs, MemVT, MMO, IndexType, ExtTy);
10505 createOperands(N, Ops);
10506
10507 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10508 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10509 assert(N->getMask().getValueType().getVectorElementCount() ==
10510 N->getValueType(0).getVectorElementCount() &&
10511 "Vector width mismatch between mask and data");
10512 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10513 N->getValueType(0).getVectorElementCount().isScalable() &&
10514 "Scalable flags of index and data do not match");
10516 N->getIndex().getValueType().getVectorElementCount(),
10517 N->getValueType(0).getVectorElementCount()) &&
10518 "Vector width mismatch between index and data");
10519 assert(isa<ConstantSDNode>(N->getScale()) &&
10520 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10521 "Scale should be a constant power of 2");
10522
10523 CSEMap.InsertNode(N, IP);
10524 InsertNode(N);
10525 SDValue V(N, 0);
10526 NewSDValueDbgMsg(V, "Creating new node: ", this);
10527 return V;
10528}
10529
10532 MachineMemOperand *MMO,
10533 ISD::MemIndexType IndexType,
10534 bool IsTrunc) {
10535 assert(Ops.size() == 6 && "Incompatible number of operands");
10536
10538 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10539 ID.AddInteger(MemVT.getRawBits());
10540 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10541 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10542 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10543 ID.AddInteger(MMO->getFlags());
10544 void *IP = nullptr;
10545 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10546 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10547 return SDValue(E, 0);
10548 }
10549
10550 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10551 VTs, MemVT, MMO, IndexType, IsTrunc);
10552 createOperands(N, Ops);
10553
10554 assert(N->getMask().getValueType().getVectorElementCount() ==
10555 N->getValue().getValueType().getVectorElementCount() &&
10556 "Vector width mismatch between mask and data");
10557 assert(
10558 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10559 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10560 "Scalable flags of index and data do not match");
10562 N->getIndex().getValueType().getVectorElementCount(),
10563 N->getValue().getValueType().getVectorElementCount()) &&
10564 "Vector width mismatch between index and data");
10565 assert(isa<ConstantSDNode>(N->getScale()) &&
10566 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10567 "Scale should be a constant power of 2");
10568
10569 CSEMap.InsertNode(N, IP);
10570 InsertNode(N);
10571 SDValue V(N, 0);
10572 NewSDValueDbgMsg(V, "Creating new node: ", this);
10573 return V;
10574}
10575
10577 const SDLoc &dl, ArrayRef<SDValue> Ops,
10578 MachineMemOperand *MMO,
10579 ISD::MemIndexType IndexType) {
10580 assert(Ops.size() == 7 && "Incompatible number of operands");
10581
10583 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VECTOR_HISTOGRAM, VTs, Ops);
10584 ID.AddInteger(MemVT.getRawBits());
10585 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10586 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10587 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10588 ID.AddInteger(MMO->getFlags());
10589 void *IP = nullptr;
10590 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10591 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10592 return SDValue(E, 0);
10593 }
10594
10595 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10596 VTs, MemVT, MMO, IndexType);
10597 createOperands(N, Ops);
10598
10599 assert(N->getMask().getValueType().getVectorElementCount() ==
10600 N->getIndex().getValueType().getVectorElementCount() &&
10601 "Vector width mismatch between mask and data");
10602 assert(isa<ConstantSDNode>(N->getScale()) &&
10603 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10604 "Scale should be a constant power of 2");
10605 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10606
10607 CSEMap.InsertNode(N, IP);
10608 InsertNode(N);
10609 SDValue V(N, 0);
10610 NewSDValueDbgMsg(V, "Creating new node: ", this);
10611 return V;
10612}
10613
10615 SDValue Ptr, SDValue Mask, SDValue EVL,
10616 MachineMemOperand *MMO) {
10617 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10618 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10620 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10621 ID.AddInteger(VT.getRawBits());
10622 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10623 VTs, VT, MMO));
10624 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10625 ID.AddInteger(MMO->getFlags());
10626 void *IP = nullptr;
10627 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10628 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10629 return SDValue(E, 0);
10630 }
10631 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10632 VT, MMO);
10633 createOperands(N, Ops);
10634
10635 CSEMap.InsertNode(N, IP);
10636 InsertNode(N);
10637 SDValue V(N, 0);
10638 NewSDValueDbgMsg(V, "Creating new node: ", this);
10639 return V;
10640}
10641
10643 EVT MemVT, MachineMemOperand *MMO) {
10644 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10645 SDVTList VTs = getVTList(MVT::Other);
10646 SDValue Ops[] = {Chain, Ptr};
10648 AddNodeIDNode(ID, ISD::GET_FPENV_MEM, VTs, Ops);
10649 ID.AddInteger(MemVT.getRawBits());
10650 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10651 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10652 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10653 ID.AddInteger(MMO->getFlags());
10654 void *IP = nullptr;
10655 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10656 return SDValue(E, 0);
10657
10658 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10659 dl.getDebugLoc(), VTs, MemVT, MMO);
10660 createOperands(N, Ops);
10661
10662 CSEMap.InsertNode(N, IP);
10663 InsertNode(N);
10664 SDValue V(N, 0);
10665 NewSDValueDbgMsg(V, "Creating new node: ", this);
10666 return V;
10667}
10668
10670 EVT MemVT, MachineMemOperand *MMO) {
10671 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10672 SDVTList VTs = getVTList(MVT::Other);
10673 SDValue Ops[] = {Chain, Ptr};
10675 AddNodeIDNode(ID, ISD::SET_FPENV_MEM, VTs, Ops);
10676 ID.AddInteger(MemVT.getRawBits());
10677 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10678 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10679 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10680 ID.AddInteger(MMO->getFlags());
10681 void *IP = nullptr;
10682 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10683 return SDValue(E, 0);
10684
10685 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10686 dl.getDebugLoc(), VTs, MemVT, MMO);
10687 createOperands(N, Ops);
10688
10689 CSEMap.InsertNode(N, IP);
10690 InsertNode(N);
10691 SDValue V(N, 0);
10692 NewSDValueDbgMsg(V, "Creating new node: ", this);
10693 return V;
10694}
10695
10697 // select undef, T, F --> T (if T is a constant), otherwise F
10698 // select, ?, undef, F --> F
10699 // select, ?, T, undef --> T
10700 if (Cond.isUndef())
10701 return isConstantValueOfAnyType(T) ? T : F;
10702 if (T.isUndef())
10703 return F;
10704 if (F.isUndef())
10705 return T;
10706
10707 // select true, T, F --> T
10708 // select false, T, F --> F
10709 if (auto C = isBoolConstant(Cond))
10710 return *C ? T : F;
10711
10712 // select ?, T, T --> T
10713 if (T == F)
10714 return T;
10715
10716 return SDValue();
10717}
10718
10720 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10721 if (X.isUndef())
10722 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10723 // shift X, undef --> undef (because it may shift by the bitwidth)
10724 if (Y.isUndef())
10725 return getUNDEF(X.getValueType());
10726
10727 // shift 0, Y --> 0
10728 // shift X, 0 --> X
10730 return X;
10731
10732 // shift X, C >= bitwidth(X) --> undef
10733 // All vector elements must be too big (or undef) to avoid partial undefs.
10734 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10735 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10736 };
10737 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10738 return getUNDEF(X.getValueType());
10739
10740 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10741 if (X.getValueType().getScalarType() == MVT::i1)
10742 return X;
10743
10744 return SDValue();
10745}
10746
10748 SDNodeFlags Flags) {
10749 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10750 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10751 // operation is poison. That result can be relaxed to undef.
10752 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10753 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10754 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10755 (YC && YC->getValueAPF().isNaN());
10756 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10757 (YC && YC->getValueAPF().isInfinity());
10758
10759 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10760 return getUNDEF(X.getValueType());
10761
10762 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10763 return getUNDEF(X.getValueType());
10764
10765 if (!YC)
10766 return SDValue();
10767
10768 // X + -0.0 --> X
10769 if (Opcode == ISD::FADD)
10770 if (YC->getValueAPF().isNegZero())
10771 return X;
10772
10773 // X - +0.0 --> X
10774 if (Opcode == ISD::FSUB)
10775 if (YC->getValueAPF().isPosZero())
10776 return X;
10777
10778 // X * 1.0 --> X
10779 // X / 1.0 --> X
10780 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10781 if (YC->getValueAPF().isExactlyValue(1.0))
10782 return X;
10783
10784 // X * 0.0 --> 0.0
10785 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10786 if (YC->getValueAPF().isZero())
10787 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10788
10789 return SDValue();
10790}
10791
10793 SDValue Ptr, SDValue SV, unsigned Align) {
10794 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10795 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10796}
10797
10798SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10800 switch (Ops.size()) {
10801 case 0: return getNode(Opcode, DL, VT);
10802 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10803 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10804 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10805 default: break;
10806 }
10807
10808 // Copy from an SDUse array into an SDValue array for use with
10809 // the regular getNode logic.
10811 return getNode(Opcode, DL, VT, NewOps);
10812}
10813
10814SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10816 SDNodeFlags Flags;
10817 if (Inserter)
10818 Flags = Inserter->getFlags();
10819 return getNode(Opcode, DL, VT, Ops, Flags);
10820}
10821
10822SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10823 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10824 unsigned NumOps = Ops.size();
10825 switch (NumOps) {
10826 case 0: return getNode(Opcode, DL, VT);
10827 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10828 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10829 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10830 default: break;
10831 }
10832
10833#ifndef NDEBUG
10834 for (const auto &Op : Ops)
10835 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10836 "Operand is DELETED_NODE!");
10837#endif
10838
10839 switch (Opcode) {
10840 default: break;
10841 case ISD::BUILD_VECTOR:
10842 // Attempt to simplify BUILD_VECTOR.
10843 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10844 return V;
10845 break;
10847 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10848 return V;
10849 break;
10850 case ISD::SELECT_CC:
10851 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10852 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10853 "LHS and RHS of condition must have same type!");
10854 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10855 "True and False arms of SelectCC must have same type!");
10856 assert(Ops[2].getValueType() == VT &&
10857 "select_cc node must be of same type as true and false value!");
10858 assert((!Ops[0].getValueType().isVector() ||
10859 Ops[0].getValueType().getVectorElementCount() ==
10860 VT.getVectorElementCount()) &&
10861 "Expected select_cc with vector result to have the same sized "
10862 "comparison type!");
10863 break;
10864 case ISD::BR_CC:
10865 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10866 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10867 "LHS/RHS of comparison should match types!");
10868 break;
10869 case ISD::VP_ADD:
10870 case ISD::VP_SUB:
10871 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10872 if (VT.getScalarType() == MVT::i1)
10873 Opcode = ISD::VP_XOR;
10874 break;
10875 case ISD::VP_MUL:
10876 // If it is VP_MUL mask operation then turn it to VP_AND
10877 if (VT.getScalarType() == MVT::i1)
10878 Opcode = ISD::VP_AND;
10879 break;
10880 case ISD::VP_REDUCE_MUL:
10881 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10882 if (VT == MVT::i1)
10883 Opcode = ISD::VP_REDUCE_AND;
10884 break;
10885 case ISD::VP_REDUCE_ADD:
10886 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10887 if (VT == MVT::i1)
10888 Opcode = ISD::VP_REDUCE_XOR;
10889 break;
10890 case ISD::VP_REDUCE_SMAX:
10891 case ISD::VP_REDUCE_UMIN:
10892 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10893 // VP_REDUCE_AND.
10894 if (VT == MVT::i1)
10895 Opcode = ISD::VP_REDUCE_AND;
10896 break;
10897 case ISD::VP_REDUCE_SMIN:
10898 case ISD::VP_REDUCE_UMAX:
10899 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10900 // VP_REDUCE_OR.
10901 if (VT == MVT::i1)
10902 Opcode = ISD::VP_REDUCE_OR;
10903 break;
10904 }
10905
10906 // Memoize nodes.
10907 SDNode *N;
10908 SDVTList VTs = getVTList(VT);
10909
10910 if (VT != MVT::Glue) {
10912 AddNodeIDNode(ID, Opcode, VTs, Ops);
10913 void *IP = nullptr;
10914
10915 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10916 E->intersectFlagsWith(Flags);
10917 return SDValue(E, 0);
10918 }
10919
10920 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10921 createOperands(N, Ops);
10922
10923 CSEMap.InsertNode(N, IP);
10924 } else {
10925 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10926 createOperands(N, Ops);
10927 }
10928
10929 N->setFlags(Flags);
10930 InsertNode(N);
10931 SDValue V(N, 0);
10932 NewSDValueDbgMsg(V, "Creating new node: ", this);
10933 return V;
10934}
10935
10936SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10937 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10938 SDNodeFlags Flags;
10939 if (Inserter)
10940 Flags = Inserter->getFlags();
10941 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10942}
10943
10944SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10946 const SDNodeFlags Flags) {
10947 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10948}
10949
10950SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10952 SDNodeFlags Flags;
10953 if (Inserter)
10954 Flags = Inserter->getFlags();
10955 return getNode(Opcode, DL, VTList, Ops, Flags);
10956}
10957
10958SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10959 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10960 if (VTList.NumVTs == 1)
10961 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10962
10963#ifndef NDEBUG
10964 for (const auto &Op : Ops)
10965 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10966 "Operand is DELETED_NODE!");
10967#endif
10968
10969 switch (Opcode) {
10970 case ISD::SADDO:
10971 case ISD::UADDO:
10972 case ISD::SSUBO:
10973 case ISD::USUBO: {
10974 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
10975 "Invalid add/sub overflow op!");
10976 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
10977 Ops[0].getValueType() == Ops[1].getValueType() &&
10978 Ops[0].getValueType() == VTList.VTs[0] &&
10979 "Binary operator types must match!");
10980 SDValue N1 = Ops[0], N2 = Ops[1];
10981 canonicalizeCommutativeBinop(Opcode, N1, N2);
10982
10983 // (X +- 0) -> X with zero-overflow.
10984 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
10985 /*AllowTruncation*/ true);
10986 if (N2CV && N2CV->isZero()) {
10987 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
10988 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
10989 }
10990
10991 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
10992 VTList.VTs[1].getScalarType() == MVT::i1) {
10993 SDValue F1 = getFreeze(N1);
10994 SDValue F2 = getFreeze(N2);
10995 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
10996 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
10997 return getNode(ISD::MERGE_VALUES, DL, VTList,
10998 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
10999 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11000 Flags);
11001 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11002 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11003 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11004 return getNode(ISD::MERGE_VALUES, DL, VTList,
11005 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11006 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11007 Flags);
11008 }
11009 }
11010 break;
11011 }
11012 case ISD::SADDO_CARRY:
11013 case ISD::UADDO_CARRY:
11014 case ISD::SSUBO_CARRY:
11015 case ISD::USUBO_CARRY:
11016 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11017 "Invalid add/sub overflow op!");
11018 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11019 Ops[0].getValueType() == Ops[1].getValueType() &&
11020 Ops[0].getValueType() == VTList.VTs[0] &&
11021 Ops[2].getValueType() == VTList.VTs[1] &&
11022 "Binary operator types must match!");
11023 break;
11024 case ISD::SMUL_LOHI:
11025 case ISD::UMUL_LOHI: {
11026 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11027 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11028 VTList.VTs[0] == Ops[0].getValueType() &&
11029 VTList.VTs[0] == Ops[1].getValueType() &&
11030 "Binary operator types must match!");
11031 // Constant fold.
11034 if (LHS && RHS) {
11035 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11036 unsigned OutWidth = Width * 2;
11037 APInt Val = LHS->getAPIntValue();
11038 APInt Mul = RHS->getAPIntValue();
11039 if (Opcode == ISD::SMUL_LOHI) {
11040 Val = Val.sext(OutWidth);
11041 Mul = Mul.sext(OutWidth);
11042 } else {
11043 Val = Val.zext(OutWidth);
11044 Mul = Mul.zext(OutWidth);
11045 }
11046 Val *= Mul;
11047
11048 SDValue Hi =
11049 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11050 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11051 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11052 }
11053 break;
11054 }
11055 case ISD::FFREXP: {
11056 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11057 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11058 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11059
11061 int FrexpExp;
11062 APFloat FrexpMant =
11063 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11064 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11065 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11066 DL, VTList.VTs[1]);
11067 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11068 }
11069
11070 break;
11071 }
11073 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11074 "Invalid STRICT_FP_EXTEND!");
11075 assert(VTList.VTs[0].isFloatingPoint() &&
11076 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11077 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11078 "STRICT_FP_EXTEND result type should be vector iff the operand "
11079 "type is vector!");
11080 assert((!VTList.VTs[0].isVector() ||
11081 VTList.VTs[0].getVectorElementCount() ==
11082 Ops[1].getValueType().getVectorElementCount()) &&
11083 "Vector element count mismatch!");
11084 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11085 "Invalid fpext node, dst <= src!");
11086 break;
11088 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11089 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11090 "STRICT_FP_ROUND result type should be vector iff the operand "
11091 "type is vector!");
11092 assert((!VTList.VTs[0].isVector() ||
11093 VTList.VTs[0].getVectorElementCount() ==
11094 Ops[1].getValueType().getVectorElementCount()) &&
11095 "Vector element count mismatch!");
11096 assert(VTList.VTs[0].isFloatingPoint() &&
11097 Ops[1].getValueType().isFloatingPoint() &&
11098 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11099 Ops[2].getOpcode() == ISD::TargetConstant &&
11100 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11101 "Invalid STRICT_FP_ROUND!");
11102 break;
11103 }
11104
11105 // Memoize the node unless it returns a glue result.
11106 SDNode *N;
11107 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11109 AddNodeIDNode(ID, Opcode, VTList, Ops);
11110 void *IP = nullptr;
11111 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11112 E->intersectFlagsWith(Flags);
11113 return SDValue(E, 0);
11114 }
11115
11116 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11117 createOperands(N, Ops);
11118 CSEMap.InsertNode(N, IP);
11119 } else {
11120 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11121 createOperands(N, Ops);
11122 }
11123
11124 N->setFlags(Flags);
11125 InsertNode(N);
11126 SDValue V(N, 0);
11127 NewSDValueDbgMsg(V, "Creating new node: ", this);
11128 return V;
11129}
11130
11131SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11132 SDVTList VTList) {
11133 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11134}
11135
11136SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11137 SDValue N1) {
11138 SDValue Ops[] = { N1 };
11139 return getNode(Opcode, DL, VTList, Ops);
11140}
11141
11142SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11143 SDValue N1, SDValue N2) {
11144 SDValue Ops[] = { N1, N2 };
11145 return getNode(Opcode, DL, VTList, Ops);
11146}
11147
11148SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11149 SDValue N1, SDValue N2, SDValue N3) {
11150 SDValue Ops[] = { N1, N2, N3 };
11151 return getNode(Opcode, DL, VTList, Ops);
11152}
11153
11154SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11155 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11156 SDValue Ops[] = { N1, N2, N3, N4 };
11157 return getNode(Opcode, DL, VTList, Ops);
11158}
11159
11160SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11161 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11162 SDValue N5) {
11163 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11164 return getNode(Opcode, DL, VTList, Ops);
11165}
11166
11168 if (!VT.isExtended())
11169 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11170
11171 return makeVTList(&(*EVTs.insert(VT).first), 1);
11172}
11173
11176 ID.AddInteger(2U);
11177 ID.AddInteger(VT1.getRawBits());
11178 ID.AddInteger(VT2.getRawBits());
11179
11180 void *IP = nullptr;
11181 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11182 if (!Result) {
11183 EVT *Array = Allocator.Allocate<EVT>(2);
11184 Array[0] = VT1;
11185 Array[1] = VT2;
11186 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11187 VTListMap.InsertNode(Result, IP);
11188 }
11189 return Result->getSDVTList();
11190}
11191
11194 ID.AddInteger(3U);
11195 ID.AddInteger(VT1.getRawBits());
11196 ID.AddInteger(VT2.getRawBits());
11197 ID.AddInteger(VT3.getRawBits());
11198
11199 void *IP = nullptr;
11200 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11201 if (!Result) {
11202 EVT *Array = Allocator.Allocate<EVT>(3);
11203 Array[0] = VT1;
11204 Array[1] = VT2;
11205 Array[2] = VT3;
11206 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11207 VTListMap.InsertNode(Result, IP);
11208 }
11209 return Result->getSDVTList();
11210}
11211
11214 ID.AddInteger(4U);
11215 ID.AddInteger(VT1.getRawBits());
11216 ID.AddInteger(VT2.getRawBits());
11217 ID.AddInteger(VT3.getRawBits());
11218 ID.AddInteger(VT4.getRawBits());
11219
11220 void *IP = nullptr;
11221 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11222 if (!Result) {
11223 EVT *Array = Allocator.Allocate<EVT>(4);
11224 Array[0] = VT1;
11225 Array[1] = VT2;
11226 Array[2] = VT3;
11227 Array[3] = VT4;
11228 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11229 VTListMap.InsertNode(Result, IP);
11230 }
11231 return Result->getSDVTList();
11232}
11233
11235 unsigned NumVTs = VTs.size();
11237 ID.AddInteger(NumVTs);
11238 for (unsigned index = 0; index < NumVTs; index++) {
11239 ID.AddInteger(VTs[index].getRawBits());
11240 }
11241
11242 void *IP = nullptr;
11243 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11244 if (!Result) {
11245 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11246 llvm::copy(VTs, Array);
11247 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11248 VTListMap.InsertNode(Result, IP);
11249 }
11250 return Result->getSDVTList();
11251}
11252
11253
11254/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11255/// specified operands. If the resultant node already exists in the DAG,
11256/// this does not modify the specified node, instead it returns the node that
11257/// already exists. If the resultant node does not exist in the DAG, the
11258/// input node is returned. As a degenerate case, if you specify the same
11259/// input operands as the node already has, the input node is returned.
11261 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11262
11263 // Check to see if there is no change.
11264 if (Op == N->getOperand(0)) return N;
11265
11266 // See if the modified node already exists.
11267 void *InsertPos = nullptr;
11268 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11269 return Existing;
11270
11271 // Nope it doesn't. Remove the node from its current place in the maps.
11272 if (InsertPos)
11273 if (!RemoveNodeFromCSEMaps(N))
11274 InsertPos = nullptr;
11275
11276 // Now we update the operands.
11277 N->OperandList[0].set(Op);
11278
11280 // If this gets put into a CSE map, add it.
11281 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11282 return N;
11283}
11284
11286 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11287
11288 // Check to see if there is no change.
11289 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11290 return N; // No operands changed, just return the input node.
11291
11292 // See if the modified node already exists.
11293 void *InsertPos = nullptr;
11294 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11295 return Existing;
11296
11297 // Nope it doesn't. Remove the node from its current place in the maps.
11298 if (InsertPos)
11299 if (!RemoveNodeFromCSEMaps(N))
11300 InsertPos = nullptr;
11301
11302 // Now we update the operands.
11303 if (N->OperandList[0] != Op1)
11304 N->OperandList[0].set(Op1);
11305 if (N->OperandList[1] != Op2)
11306 N->OperandList[1].set(Op2);
11307
11309 // If this gets put into a CSE map, add it.
11310 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11311 return N;
11312}
11313
11316 SDValue Ops[] = { Op1, Op2, Op3 };
11317 return UpdateNodeOperands(N, Ops);
11318}
11319
11322 SDValue Op3, SDValue Op4) {
11323 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11324 return UpdateNodeOperands(N, Ops);
11325}
11326
11329 SDValue Op3, SDValue Op4, SDValue Op5) {
11330 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11331 return UpdateNodeOperands(N, Ops);
11332}
11333
11336 unsigned NumOps = Ops.size();
11337 assert(N->getNumOperands() == NumOps &&
11338 "Update with wrong number of operands");
11339
11340 // If no operands changed just return the input node.
11341 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11342 return N;
11343
11344 // See if the modified node already exists.
11345 void *InsertPos = nullptr;
11346 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11347 return Existing;
11348
11349 // Nope it doesn't. Remove the node from its current place in the maps.
11350 if (InsertPos)
11351 if (!RemoveNodeFromCSEMaps(N))
11352 InsertPos = nullptr;
11353
11354 // Now we update the operands.
11355 for (unsigned i = 0; i != NumOps; ++i)
11356 if (N->OperandList[i] != Ops[i])
11357 N->OperandList[i].set(Ops[i]);
11358
11360 // If this gets put into a CSE map, add it.
11361 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11362 return N;
11363}
11364
11365/// DropOperands - Release the operands and set this node to have
11366/// zero operands.
11368 // Unlike the code in MorphNodeTo that does this, we don't need to
11369 // watch for dead nodes here.
11370 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11371 SDUse &Use = *I++;
11372 Use.set(SDValue());
11373 }
11374}
11375
11377 ArrayRef<MachineMemOperand *> NewMemRefs) {
11378 if (NewMemRefs.empty()) {
11379 N->clearMemRefs();
11380 return;
11381 }
11382
11383 // Check if we can avoid allocating by storing a single reference directly.
11384 if (NewMemRefs.size() == 1) {
11385 N->MemRefs = NewMemRefs[0];
11386 N->NumMemRefs = 1;
11387 return;
11388 }
11389
11390 MachineMemOperand **MemRefsBuffer =
11391 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11392 llvm::copy(NewMemRefs, MemRefsBuffer);
11393 N->MemRefs = MemRefsBuffer;
11394 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11395}
11396
11397/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11398/// machine opcode.
11399///
11401 EVT VT) {
11402 SDVTList VTs = getVTList(VT);
11403 return SelectNodeTo(N, MachineOpc, VTs, {});
11404}
11405
11407 EVT VT, SDValue Op1) {
11408 SDVTList VTs = getVTList(VT);
11409 SDValue Ops[] = { Op1 };
11410 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11411}
11412
11414 EVT VT, SDValue Op1,
11415 SDValue Op2) {
11416 SDVTList VTs = getVTList(VT);
11417 SDValue Ops[] = { Op1, Op2 };
11418 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11419}
11420
11422 EVT VT, SDValue Op1,
11423 SDValue Op2, SDValue Op3) {
11424 SDVTList VTs = getVTList(VT);
11425 SDValue Ops[] = { Op1, Op2, Op3 };
11426 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11427}
11428
11431 SDVTList VTs = getVTList(VT);
11432 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11433}
11434
11436 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11437 SDVTList VTs = getVTList(VT1, VT2);
11438 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11439}
11440
11442 EVT VT1, EVT VT2) {
11443 SDVTList VTs = getVTList(VT1, VT2);
11444 return SelectNodeTo(N, MachineOpc, VTs, {});
11445}
11446
11448 EVT VT1, EVT VT2, EVT VT3,
11450 SDVTList VTs = getVTList(VT1, VT2, VT3);
11451 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11452}
11453
11455 EVT VT1, EVT VT2,
11456 SDValue Op1, SDValue Op2) {
11457 SDVTList VTs = getVTList(VT1, VT2);
11458 SDValue Ops[] = { Op1, Op2 };
11459 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11460}
11461
11464 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11465 // Reset the NodeID to -1.
11466 New->setNodeId(-1);
11467 if (New != N) {
11468 ReplaceAllUsesWith(N, New);
11470 }
11471 return New;
11472}
11473
11474/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11475/// the line number information on the merged node since it is not possible to
11476/// preserve the information that operation is associated with multiple lines.
11477/// This will make the debugger working better at -O0, were there is a higher
11478/// probability having other instructions associated with that line.
11479///
11480/// For IROrder, we keep the smaller of the two
11481SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11482 DebugLoc NLoc = N->getDebugLoc();
11483 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11484 N->setDebugLoc(DebugLoc());
11485 }
11486 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11487 N->setIROrder(Order);
11488 return N;
11489}
11490
11491/// MorphNodeTo - This *mutates* the specified node to have the specified
11492/// return type, opcode, and operands.
11493///
11494/// Note that MorphNodeTo returns the resultant node. If there is already a
11495/// node of the specified opcode and operands, it returns that node instead of
11496/// the current one. Note that the SDLoc need not be the same.
11497///
11498/// Using MorphNodeTo is faster than creating a new node and swapping it in
11499/// with ReplaceAllUsesWith both because it often avoids allocating a new
11500/// node, and because it doesn't require CSE recalculation for any of
11501/// the node's users.
11502///
11503/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11504/// As a consequence it isn't appropriate to use from within the DAG combiner or
11505/// the legalizer which maintain worklists that would need to be updated when
11506/// deleting things.
11509 // If an identical node already exists, use it.
11510 void *IP = nullptr;
11511 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11513 AddNodeIDNode(ID, Opc, VTs, Ops);
11514 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11515 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11516 }
11517
11518 if (!RemoveNodeFromCSEMaps(N))
11519 IP = nullptr;
11520
11521 // Start the morphing.
11522 N->NodeType = Opc;
11523 N->ValueList = VTs.VTs;
11524 N->NumValues = VTs.NumVTs;
11525
11526 // Clear the operands list, updating used nodes to remove this from their
11527 // use list. Keep track of any operands that become dead as a result.
11528 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11529 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11530 SDUse &Use = *I++;
11531 SDNode *Used = Use.getNode();
11532 Use.set(SDValue());
11533 if (Used->use_empty())
11534 DeadNodeSet.insert(Used);
11535 }
11536
11537 // For MachineNode, initialize the memory references information.
11539 MN->clearMemRefs();
11540
11541 // Swap for an appropriately sized array from the recycler.
11542 removeOperands(N);
11543 createOperands(N, Ops);
11544
11545 // Delete any nodes that are still dead after adding the uses for the
11546 // new operands.
11547 if (!DeadNodeSet.empty()) {
11548 SmallVector<SDNode *, 16> DeadNodes;
11549 for (SDNode *N : DeadNodeSet)
11550 if (N->use_empty())
11551 DeadNodes.push_back(N);
11552 RemoveDeadNodes(DeadNodes);
11553 }
11554
11555 if (IP)
11556 CSEMap.InsertNode(N, IP); // Memoize the new node.
11557 return N;
11558}
11559
11561 unsigned OrigOpc = Node->getOpcode();
11562 unsigned NewOpc;
11563 switch (OrigOpc) {
11564 default:
11565 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11566#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11567 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11568#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11569 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11570#include "llvm/IR/ConstrainedOps.def"
11571 }
11572
11573 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11574
11575 // We're taking this node out of the chain, so we need to re-link things.
11576 SDValue InputChain = Node->getOperand(0);
11577 SDValue OutputChain = SDValue(Node, 1);
11578 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11579
11581 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11582 Ops.push_back(Node->getOperand(i));
11583
11584 SDVTList VTs = getVTList(Node->getValueType(0));
11585 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11586
11587 // MorphNodeTo can operate in two ways: if an existing node with the
11588 // specified operands exists, it can just return it. Otherwise, it
11589 // updates the node in place to have the requested operands.
11590 if (Res == Node) {
11591 // If we updated the node in place, reset the node ID. To the isel,
11592 // this should be just like a newly allocated machine node.
11593 Res->setNodeId(-1);
11594 } else {
11597 }
11598
11599 return Res;
11600}
11601
11602/// getMachineNode - These are used for target selectors to create a new node
11603/// with specified return type(s), MachineInstr opcode, and operands.
11604///
11605/// Note that getMachineNode returns the resultant node. If there is already a
11606/// node of the specified opcode and operands, it returns that node instead of
11607/// the current one.
11609 EVT VT) {
11610 SDVTList VTs = getVTList(VT);
11611 return getMachineNode(Opcode, dl, VTs, {});
11612}
11613
11615 EVT VT, SDValue Op1) {
11616 SDVTList VTs = getVTList(VT);
11617 SDValue Ops[] = { Op1 };
11618 return getMachineNode(Opcode, dl, VTs, Ops);
11619}
11620
11622 EVT VT, SDValue Op1, SDValue Op2) {
11623 SDVTList VTs = getVTList(VT);
11624 SDValue Ops[] = { Op1, Op2 };
11625 return getMachineNode(Opcode, dl, VTs, Ops);
11626}
11627
11629 EVT VT, SDValue Op1, SDValue Op2,
11630 SDValue Op3) {
11631 SDVTList VTs = getVTList(VT);
11632 SDValue Ops[] = { Op1, Op2, Op3 };
11633 return getMachineNode(Opcode, dl, VTs, Ops);
11634}
11635
11638 SDVTList VTs = getVTList(VT);
11639 return getMachineNode(Opcode, dl, VTs, Ops);
11640}
11641
11643 EVT VT1, EVT VT2, SDValue Op1,
11644 SDValue Op2) {
11645 SDVTList VTs = getVTList(VT1, VT2);
11646 SDValue Ops[] = { Op1, Op2 };
11647 return getMachineNode(Opcode, dl, VTs, Ops);
11648}
11649
11651 EVT VT1, EVT VT2, SDValue Op1,
11652 SDValue Op2, SDValue Op3) {
11653 SDVTList VTs = getVTList(VT1, VT2);
11654 SDValue Ops[] = { Op1, Op2, Op3 };
11655 return getMachineNode(Opcode, dl, VTs, Ops);
11656}
11657
11659 EVT VT1, EVT VT2,
11661 SDVTList VTs = getVTList(VT1, VT2);
11662 return getMachineNode(Opcode, dl, VTs, Ops);
11663}
11664
11666 EVT VT1, EVT VT2, EVT VT3,
11667 SDValue Op1, SDValue Op2) {
11668 SDVTList VTs = getVTList(VT1, VT2, VT3);
11669 SDValue Ops[] = { Op1, Op2 };
11670 return getMachineNode(Opcode, dl, VTs, Ops);
11671}
11672
11674 EVT VT1, EVT VT2, EVT VT3,
11675 SDValue Op1, SDValue Op2,
11676 SDValue Op3) {
11677 SDVTList VTs = getVTList(VT1, VT2, VT3);
11678 SDValue Ops[] = { Op1, Op2, Op3 };
11679 return getMachineNode(Opcode, dl, VTs, Ops);
11680}
11681
11683 EVT VT1, EVT VT2, EVT VT3,
11685 SDVTList VTs = getVTList(VT1, VT2, VT3);
11686 return getMachineNode(Opcode, dl, VTs, Ops);
11687}
11688
11690 ArrayRef<EVT> ResultTys,
11692 SDVTList VTs = getVTList(ResultTys);
11693 return getMachineNode(Opcode, dl, VTs, Ops);
11694}
11695
11697 SDVTList VTs,
11699 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11701 void *IP = nullptr;
11702
11703 if (DoCSE) {
11705 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11706 IP = nullptr;
11707 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11708 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11709 }
11710 }
11711
11712 // Allocate a new MachineSDNode.
11713 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11714 createOperands(N, Ops);
11715
11716 if (DoCSE)
11717 CSEMap.InsertNode(N, IP);
11718
11719 InsertNode(N);
11720 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11721 return N;
11722}
11723
11724/// getTargetExtractSubreg - A convenience function for creating
11725/// TargetOpcode::EXTRACT_SUBREG nodes.
11727 SDValue Operand) {
11728 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11729 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11730 VT, Operand, SRIdxVal);
11731 return SDValue(Subreg, 0);
11732}
11733
11734/// getTargetInsertSubreg - A convenience function for creating
11735/// TargetOpcode::INSERT_SUBREG nodes.
11737 SDValue Operand, SDValue Subreg) {
11738 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11739 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11740 VT, Operand, Subreg, SRIdxVal);
11741 return SDValue(Result, 0);
11742}
11743
11744/// getNodeIfExists - Get the specified node if it's already available, or
11745/// else return NULL.
11748 bool AllowCommute) {
11749 SDNodeFlags Flags;
11750 if (Inserter)
11751 Flags = Inserter->getFlags();
11752 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11753}
11754
11757 const SDNodeFlags Flags,
11758 bool AllowCommute) {
11759 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11760 return nullptr;
11761
11762 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11764 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11765 void *IP = nullptr;
11766 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11767 E->intersectFlagsWith(Flags);
11768 return E;
11769 }
11770 return nullptr;
11771 };
11772
11773 if (SDNode *Existing = Lookup(Ops))
11774 return Existing;
11775
11776 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11777 return Lookup({Ops[1], Ops[0]});
11778
11779 return nullptr;
11780}
11781
11782/// doesNodeExist - Check if a node exists without modifying its flags.
11783bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11785 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11787 AddNodeIDNode(ID, Opcode, VTList, Ops);
11788 void *IP = nullptr;
11789 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11790 return true;
11791 }
11792 return false;
11793}
11794
11795/// getDbgValue - Creates a SDDbgValue node.
11796///
11797/// SDNode
11799 SDNode *N, unsigned R, bool IsIndirect,
11800 const DebugLoc &DL, unsigned O) {
11801 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11802 "Expected inlined-at fields to agree");
11803 return new (DbgInfo->getAlloc())
11804 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11805 {}, IsIndirect, DL, O,
11806 /*IsVariadic=*/false);
11807}
11808
11809/// Constant
11811 DIExpression *Expr,
11812 const Value *C,
11813 const DebugLoc &DL, unsigned O) {
11814 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11815 "Expected inlined-at fields to agree");
11816 return new (DbgInfo->getAlloc())
11817 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11818 /*IsIndirect=*/false, DL, O,
11819 /*IsVariadic=*/false);
11820}
11821
11822/// FrameIndex
11824 DIExpression *Expr, unsigned FI,
11825 bool IsIndirect,
11826 const DebugLoc &DL,
11827 unsigned O) {
11828 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11829 "Expected inlined-at fields to agree");
11830 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11831}
11832
11833/// FrameIndex with dependencies
11835 DIExpression *Expr, unsigned FI,
11836 ArrayRef<SDNode *> Dependencies,
11837 bool IsIndirect,
11838 const DebugLoc &DL,
11839 unsigned O) {
11840 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11841 "Expected inlined-at fields to agree");
11842 return new (DbgInfo->getAlloc())
11843 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11844 Dependencies, IsIndirect, DL, O,
11845 /*IsVariadic=*/false);
11846}
11847
11848/// VReg
11850 Register VReg, bool IsIndirect,
11851 const DebugLoc &DL, unsigned O) {
11852 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11853 "Expected inlined-at fields to agree");
11854 return new (DbgInfo->getAlloc())
11855 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11856 {}, IsIndirect, DL, O,
11857 /*IsVariadic=*/false);
11858}
11859
11862 ArrayRef<SDNode *> Dependencies,
11863 bool IsIndirect, const DebugLoc &DL,
11864 unsigned O, bool IsVariadic) {
11865 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11866 "Expected inlined-at fields to agree");
11867 return new (DbgInfo->getAlloc())
11868 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11869 DL, O, IsVariadic);
11870}
11871
11873 unsigned OffsetInBits, unsigned SizeInBits,
11874 bool InvalidateDbg) {
11875 SDNode *FromNode = From.getNode();
11876 SDNode *ToNode = To.getNode();
11877 assert(FromNode && ToNode && "Can't modify dbg values");
11878
11879 // PR35338
11880 // TODO: assert(From != To && "Redundant dbg value transfer");
11881 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11882 if (From == To || FromNode == ToNode)
11883 return;
11884
11885 if (!FromNode->getHasDebugValue())
11886 return;
11887
11888 SDDbgOperand FromLocOp =
11889 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11891
11893 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11894 if (Dbg->isInvalidated())
11895 continue;
11896
11897 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11898
11899 // Create a new location ops vector that is equal to the old vector, but
11900 // with each instance of FromLocOp replaced with ToLocOp.
11901 bool Changed = false;
11902 auto NewLocOps = Dbg->copyLocationOps();
11903 std::replace_if(
11904 NewLocOps.begin(), NewLocOps.end(),
11905 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11906 bool Match = Op == FromLocOp;
11907 Changed |= Match;
11908 return Match;
11909 },
11910 ToLocOp);
11911 // Ignore this SDDbgValue if we didn't find a matching location.
11912 if (!Changed)
11913 continue;
11914
11915 DIVariable *Var = Dbg->getVariable();
11916 auto *Expr = Dbg->getExpression();
11917 // If a fragment is requested, update the expression.
11918 if (SizeInBits) {
11919 // When splitting a larger (e.g., sign-extended) value whose
11920 // lower bits are described with an SDDbgValue, do not attempt
11921 // to transfer the SDDbgValue to the upper bits.
11922 if (auto FI = Expr->getFragmentInfo())
11923 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11924 continue;
11925 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11926 SizeInBits);
11927 if (!Fragment)
11928 continue;
11929 Expr = *Fragment;
11930 }
11931
11932 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11933 // Clone the SDDbgValue and move it to To.
11934 SDDbgValue *Clone = getDbgValueList(
11935 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11936 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11937 Dbg->isVariadic());
11938 ClonedDVs.push_back(Clone);
11939
11940 if (InvalidateDbg) {
11941 // Invalidate value and indicate the SDDbgValue should not be emitted.
11942 Dbg->setIsInvalidated();
11943 Dbg->setIsEmitted();
11944 }
11945 }
11946
11947 for (SDDbgValue *Dbg : ClonedDVs) {
11948 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11949 "Transferred DbgValues should depend on the new SDNode");
11950 AddDbgValue(Dbg, false);
11951 }
11952}
11953
11955 if (!N.getHasDebugValue())
11956 return;
11957
11958 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11959 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11960 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11961 return SDDbgOperand::fromNode(Node, ResNo);
11962 };
11963
11965 for (auto *DV : GetDbgValues(&N)) {
11966 if (DV->isInvalidated())
11967 continue;
11968 switch (N.getOpcode()) {
11969 default:
11970 break;
11971 case ISD::ADD: {
11972 SDValue N0 = N.getOperand(0);
11973 SDValue N1 = N.getOperand(1);
11974 if (!isa<ConstantSDNode>(N0)) {
11975 bool RHSConstant = isa<ConstantSDNode>(N1);
11977 if (RHSConstant)
11978 Offset = N.getConstantOperandVal(1);
11979 // We are not allowed to turn indirect debug values variadic, so
11980 // don't salvage those.
11981 if (!RHSConstant && DV->isIndirect())
11982 continue;
11983
11984 // Rewrite an ADD constant node into a DIExpression. Since we are
11985 // performing arithmetic to compute the variable's *value* in the
11986 // DIExpression, we need to mark the expression with a
11987 // DW_OP_stack_value.
11988 auto *DIExpr = DV->getExpression();
11989 auto NewLocOps = DV->copyLocationOps();
11990 bool Changed = false;
11991 size_t OrigLocOpsSize = NewLocOps.size();
11992 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
11993 // We're not given a ResNo to compare against because the whole
11994 // node is going away. We know that any ISD::ADD only has one
11995 // result, so we can assume any node match is using the result.
11996 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
11997 NewLocOps[i].getSDNode() != &N)
11998 continue;
11999 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12000 if (RHSConstant) {
12003 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12004 } else {
12005 // Convert to a variadic expression (if not already).
12006 // convertToVariadicExpression() returns a const pointer, so we use
12007 // a temporary const variable here.
12008 const auto *TmpDIExpr =
12012 ExprOps.push_back(NewLocOps.size());
12013 ExprOps.push_back(dwarf::DW_OP_plus);
12014 SDDbgOperand RHS =
12016 NewLocOps.push_back(RHS);
12017 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12018 }
12019 Changed = true;
12020 }
12021 (void)Changed;
12022 assert(Changed && "Salvage target doesn't use N");
12023
12024 bool IsVariadic =
12025 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12026
12027 auto AdditionalDependencies = DV->getAdditionalDependencies();
12028 SDDbgValue *Clone = getDbgValueList(
12029 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12030 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12031 ClonedDVs.push_back(Clone);
12032 DV->setIsInvalidated();
12033 DV->setIsEmitted();
12034 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12035 N0.getNode()->dumprFull(this);
12036 dbgs() << " into " << *DIExpr << '\n');
12037 }
12038 break;
12039 }
12040 case ISD::TRUNCATE: {
12041 SDValue N0 = N.getOperand(0);
12042 TypeSize FromSize = N0.getValueSizeInBits();
12043 TypeSize ToSize = N.getValueSizeInBits(0);
12044
12045 DIExpression *DbgExpression = DV->getExpression();
12046 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12047 auto NewLocOps = DV->copyLocationOps();
12048 bool Changed = false;
12049 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12050 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12051 NewLocOps[i].getSDNode() != &N)
12052 continue;
12053
12054 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12055 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12056 Changed = true;
12057 }
12058 assert(Changed && "Salvage target doesn't use N");
12059 (void)Changed;
12060
12061 SDDbgValue *Clone =
12062 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12063 DV->getAdditionalDependencies(), DV->isIndirect(),
12064 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12065
12066 ClonedDVs.push_back(Clone);
12067 DV->setIsInvalidated();
12068 DV->setIsEmitted();
12069 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12070 dbgs() << " into " << *DbgExpression << '\n');
12071 break;
12072 }
12073 }
12074 }
12075
12076 for (SDDbgValue *Dbg : ClonedDVs) {
12077 assert((!Dbg->getSDNodes().empty() ||
12078 llvm::any_of(Dbg->getLocationOps(),
12079 [&](const SDDbgOperand &Op) {
12080 return Op.getKind() == SDDbgOperand::FRAMEIX;
12081 })) &&
12082 "Salvaged DbgValue should depend on a new SDNode");
12083 AddDbgValue(Dbg, false);
12084 }
12085}
12086
12087/// Creates a SDDbgLabel node.
12089 const DebugLoc &DL, unsigned O) {
12090 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12091 "Expected inlined-at fields to agree");
12092 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12093}
12094
12095namespace {
12096
12097/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12098/// pointed to by a use iterator is deleted, increment the use iterator
12099/// so that it doesn't dangle.
12100///
12101class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12104
12105 void NodeDeleted(SDNode *N, SDNode *E) override {
12106 // Increment the iterator as needed.
12107 while (UI != UE && N == UI->getUser())
12108 ++UI;
12109 }
12110
12111public:
12112 RAUWUpdateListener(SelectionDAG &d,
12115 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12116};
12117
12118} // end anonymous namespace
12119
12120/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12121/// This can cause recursive merging of nodes in the DAG.
12122///
12123/// This version assumes From has a single result value.
12124///
12126 SDNode *From = FromN.getNode();
12127 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12128 "Cannot replace with this method!");
12129 assert(From != To.getNode() && "Cannot replace uses of with self");
12130
12131 // Preserve Debug Values
12132 transferDbgValues(FromN, To);
12133 // Preserve extra info.
12134 copyExtraInfo(From, To.getNode());
12135
12136 // Iterate over all the existing uses of From. New uses will be added
12137 // to the beginning of the use list, which we avoid visiting.
12138 // This specifically avoids visiting uses of From that arise while the
12139 // replacement is happening, because any such uses would be the result
12140 // of CSE: If an existing node looks like From after one of its operands
12141 // is replaced by To, we don't want to replace of all its users with To
12142 // too. See PR3018 for more info.
12143 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12144 RAUWUpdateListener Listener(*this, UI, UE);
12145 while (UI != UE) {
12146 SDNode *User = UI->getUser();
12147
12148 // This node is about to morph, remove its old self from the CSE maps.
12149 RemoveNodeFromCSEMaps(User);
12150
12151 // A user can appear in a use list multiple times, and when this
12152 // happens the uses are usually next to each other in the list.
12153 // To help reduce the number of CSE recomputations, process all
12154 // the uses of this user that we can find this way.
12155 do {
12156 SDUse &Use = *UI;
12157 ++UI;
12158 Use.set(To);
12159 if (To->isDivergent() != From->isDivergent())
12161 } while (UI != UE && UI->getUser() == User);
12162 // Now that we have modified User, add it back to the CSE maps. If it
12163 // already exists there, recursively merge the results together.
12164 AddModifiedNodeToCSEMaps(User);
12165 }
12166
12167 // If we just RAUW'd the root, take note.
12168 if (FromN == getRoot())
12169 setRoot(To);
12170}
12171
12172/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12173/// This can cause recursive merging of nodes in the DAG.
12174///
12175/// This version assumes that for each value of From, there is a
12176/// corresponding value in To in the same position with the same type.
12177///
12179#ifndef NDEBUG
12180 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12181 assert((!From->hasAnyUseOfValue(i) ||
12182 From->getValueType(i) == To->getValueType(i)) &&
12183 "Cannot use this version of ReplaceAllUsesWith!");
12184#endif
12185
12186 // Handle the trivial case.
12187 if (From == To)
12188 return;
12189
12190 // Preserve Debug Info. Only do this if there's a use.
12191 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12192 if (From->hasAnyUseOfValue(i)) {
12193 assert((i < To->getNumValues()) && "Invalid To location");
12194 transferDbgValues(SDValue(From, i), SDValue(To, i));
12195 }
12196 // Preserve extra info.
12197 copyExtraInfo(From, To);
12198
12199 // Iterate over just the existing users of From. See the comments in
12200 // the ReplaceAllUsesWith above.
12201 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12202 RAUWUpdateListener Listener(*this, UI, UE);
12203 while (UI != UE) {
12204 SDNode *User = UI->getUser();
12205
12206 // This node is about to morph, remove its old self from the CSE maps.
12207 RemoveNodeFromCSEMaps(User);
12208
12209 // A user can appear in a use list multiple times, and when this
12210 // happens the uses are usually next to each other in the list.
12211 // To help reduce the number of CSE recomputations, process all
12212 // the uses of this user that we can find this way.
12213 do {
12214 SDUse &Use = *UI;
12215 ++UI;
12216 Use.setNode(To);
12217 if (To->isDivergent() != From->isDivergent())
12219 } while (UI != UE && UI->getUser() == User);
12220
12221 // Now that we have modified User, add it back to the CSE maps. If it
12222 // already exists there, recursively merge the results together.
12223 AddModifiedNodeToCSEMaps(User);
12224 }
12225
12226 // If we just RAUW'd the root, take note.
12227 if (From == getRoot().getNode())
12228 setRoot(SDValue(To, getRoot().getResNo()));
12229}
12230
12231/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12232/// This can cause recursive merging of nodes in the DAG.
12233///
12234/// This version can replace From with any result values. To must match the
12235/// number and types of values returned by From.
12237 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12238 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12239
12240 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12241 // Preserve Debug Info.
12242 transferDbgValues(SDValue(From, i), To[i]);
12243 // Preserve extra info.
12244 copyExtraInfo(From, To[i].getNode());
12245 }
12246
12247 // Iterate over just the existing users of From. See the comments in
12248 // the ReplaceAllUsesWith above.
12249 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12250 RAUWUpdateListener Listener(*this, UI, UE);
12251 while (UI != UE) {
12252 SDNode *User = UI->getUser();
12253
12254 // This node is about to morph, remove its old self from the CSE maps.
12255 RemoveNodeFromCSEMaps(User);
12256
12257 // A user can appear in a use list multiple times, and when this happens the
12258 // uses are usually next to each other in the list. To help reduce the
12259 // number of CSE and divergence recomputations, process all the uses of this
12260 // user that we can find this way.
12261 bool To_IsDivergent = false;
12262 do {
12263 SDUse &Use = *UI;
12264 const SDValue &ToOp = To[Use.getResNo()];
12265 ++UI;
12266 Use.set(ToOp);
12267 To_IsDivergent |= ToOp->isDivergent();
12268 } while (UI != UE && UI->getUser() == User);
12269
12270 if (To_IsDivergent != From->isDivergent())
12272
12273 // Now that we have modified User, add it back to the CSE maps. If it
12274 // already exists there, recursively merge the results together.
12275 AddModifiedNodeToCSEMaps(User);
12276 }
12277
12278 // If we just RAUW'd the root, take note.
12279 if (From == getRoot().getNode())
12280 setRoot(SDValue(To[getRoot().getResNo()]));
12281}
12282
12283/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12284/// uses of other values produced by From.getNode() alone. The Deleted
12285/// vector is handled the same way as for ReplaceAllUsesWith.
12287 // Handle the really simple, really trivial case efficiently.
12288 if (From == To) return;
12289
12290 // Handle the simple, trivial, case efficiently.
12291 if (From.getNode()->getNumValues() == 1) {
12292 ReplaceAllUsesWith(From, To);
12293 return;
12294 }
12295
12296 // Preserve Debug Info.
12297 transferDbgValues(From, To);
12298 copyExtraInfo(From.getNode(), To.getNode());
12299
12300 // Iterate over just the existing users of From. See the comments in
12301 // the ReplaceAllUsesWith above.
12302 SDNode::use_iterator UI = From.getNode()->use_begin(),
12303 UE = From.getNode()->use_end();
12304 RAUWUpdateListener Listener(*this, UI, UE);
12305 while (UI != UE) {
12306 SDNode *User = UI->getUser();
12307 bool UserRemovedFromCSEMaps = false;
12308
12309 // A user can appear in a use list multiple times, and when this
12310 // happens the uses are usually next to each other in the list.
12311 // To help reduce the number of CSE recomputations, process all
12312 // the uses of this user that we can find this way.
12313 do {
12314 SDUse &Use = *UI;
12315
12316 // Skip uses of different values from the same node.
12317 if (Use.getResNo() != From.getResNo()) {
12318 ++UI;
12319 continue;
12320 }
12321
12322 // If this node hasn't been modified yet, it's still in the CSE maps,
12323 // so remove its old self from the CSE maps.
12324 if (!UserRemovedFromCSEMaps) {
12325 RemoveNodeFromCSEMaps(User);
12326 UserRemovedFromCSEMaps = true;
12327 }
12328
12329 ++UI;
12330 Use.set(To);
12331 if (To->isDivergent() != From->isDivergent())
12333 } while (UI != UE && UI->getUser() == User);
12334 // We are iterating over all uses of the From node, so if a use
12335 // doesn't use the specific value, no changes are made.
12336 if (!UserRemovedFromCSEMaps)
12337 continue;
12338
12339 // Now that we have modified User, add it back to the CSE maps. If it
12340 // already exists there, recursively merge the results together.
12341 AddModifiedNodeToCSEMaps(User);
12342 }
12343
12344 // If we just RAUW'd the root, take note.
12345 if (From == getRoot())
12346 setRoot(To);
12347}
12348
12349namespace {
12350
12351/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12352/// to record information about a use.
12353struct UseMemo {
12354 SDNode *User;
12355 unsigned Index;
12356 SDUse *Use;
12357};
12358
12359/// operator< - Sort Memos by User.
12360bool operator<(const UseMemo &L, const UseMemo &R) {
12361 return (intptr_t)L.User < (intptr_t)R.User;
12362}
12363
12364/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12365/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12366/// the node already has been taken care of recursively.
12367class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12368 SmallVectorImpl<UseMemo> &Uses;
12369
12370 void NodeDeleted(SDNode *N, SDNode *E) override {
12371 for (UseMemo &Memo : Uses)
12372 if (Memo.User == N)
12373 Memo.User = nullptr;
12374 }
12375
12376public:
12377 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12378 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12379};
12380
12381} // end anonymous namespace
12382
12383/// Return true if a glue output should propagate divergence information.
12385 switch (Node->getOpcode()) {
12386 case ISD::CopyFromReg:
12387 case ISD::CopyToReg:
12388 return false;
12389 default:
12390 return true;
12391 }
12392
12393 llvm_unreachable("covered opcode switch");
12394}
12395
12397 if (TLI->isSDNodeAlwaysUniform(N)) {
12398 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12399 "Conflicting divergence information!");
12400 return false;
12401 }
12402 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12403 return true;
12404 for (const auto &Op : N->ops()) {
12405 EVT VT = Op.getValueType();
12406
12407 // Skip Chain. It does not carry divergence.
12408 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12409 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12410 return true;
12411 }
12412 return false;
12413}
12414
12416 SmallVector<SDNode *, 16> Worklist(1, N);
12417 do {
12418 N = Worklist.pop_back_val();
12419 bool IsDivergent = calculateDivergence(N);
12420 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12421 N->SDNodeBits.IsDivergent = IsDivergent;
12422 llvm::append_range(Worklist, N->users());
12423 }
12424 } while (!Worklist.empty());
12425}
12426
12427void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12429 Order.reserve(AllNodes.size());
12430 for (auto &N : allnodes()) {
12431 unsigned NOps = N.getNumOperands();
12432 Degree[&N] = NOps;
12433 if (0 == NOps)
12434 Order.push_back(&N);
12435 }
12436 for (size_t I = 0; I != Order.size(); ++I) {
12437 SDNode *N = Order[I];
12438 for (auto *U : N->users()) {
12439 unsigned &UnsortedOps = Degree[U];
12440 if (0 == --UnsortedOps)
12441 Order.push_back(U);
12442 }
12443 }
12444}
12445
12446#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12447void SelectionDAG::VerifyDAGDivergence() {
12448 std::vector<SDNode *> TopoOrder;
12449 CreateTopologicalOrder(TopoOrder);
12450 for (auto *N : TopoOrder) {
12451 assert(calculateDivergence(N) == N->isDivergent() &&
12452 "Divergence bit inconsistency detected");
12453 }
12454}
12455#endif
12456
12457/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12458/// uses of other values produced by From.getNode() alone. The same value
12459/// may appear in both the From and To list. The Deleted vector is
12460/// handled the same way as for ReplaceAllUsesWith.
12462 const SDValue *To,
12463 unsigned Num){
12464 // Handle the simple, trivial case efficiently.
12465 if (Num == 1)
12466 return ReplaceAllUsesOfValueWith(*From, *To);
12467
12468 transferDbgValues(*From, *To);
12469 copyExtraInfo(From->getNode(), To->getNode());
12470
12471 // Read up all the uses and make records of them. This helps
12472 // processing new uses that are introduced during the
12473 // replacement process.
12475 for (unsigned i = 0; i != Num; ++i) {
12476 unsigned FromResNo = From[i].getResNo();
12477 SDNode *FromNode = From[i].getNode();
12478 for (SDUse &Use : FromNode->uses()) {
12479 if (Use.getResNo() == FromResNo) {
12480 UseMemo Memo = {Use.getUser(), i, &Use};
12481 Uses.push_back(Memo);
12482 }
12483 }
12484 }
12485
12486 // Sort the uses, so that all the uses from a given User are together.
12488 RAUOVWUpdateListener Listener(*this, Uses);
12489
12490 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12491 UseIndex != UseIndexEnd; ) {
12492 // We know that this user uses some value of From. If it is the right
12493 // value, update it.
12494 SDNode *User = Uses[UseIndex].User;
12495 // If the node has been deleted by recursive CSE updates when updating
12496 // another node, then just skip this entry.
12497 if (User == nullptr) {
12498 ++UseIndex;
12499 continue;
12500 }
12501
12502 // This node is about to morph, remove its old self from the CSE maps.
12503 RemoveNodeFromCSEMaps(User);
12504
12505 // The Uses array is sorted, so all the uses for a given User
12506 // are next to each other in the list.
12507 // To help reduce the number of CSE recomputations, process all
12508 // the uses of this user that we can find this way.
12509 do {
12510 unsigned i = Uses[UseIndex].Index;
12511 SDUse &Use = *Uses[UseIndex].Use;
12512 ++UseIndex;
12513
12514 Use.set(To[i]);
12515 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12516
12517 // Now that we have modified User, add it back to the CSE maps. If it
12518 // already exists there, recursively merge the results together.
12519 AddModifiedNodeToCSEMaps(User);
12520 }
12521}
12522
12523/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12524/// based on their topological order. It returns the maximum id and a vector
12525/// of the SDNodes* in assigned order by reference.
12527 unsigned DAGSize = 0;
12528
12529 // SortedPos tracks the progress of the algorithm. Nodes before it are
12530 // sorted, nodes after it are unsorted. When the algorithm completes
12531 // it is at the end of the list.
12532 allnodes_iterator SortedPos = allnodes_begin();
12533
12534 // Visit all the nodes. Move nodes with no operands to the front of
12535 // the list immediately. Annotate nodes that do have operands with their
12536 // operand count. Before we do this, the Node Id fields of the nodes
12537 // may contain arbitrary values. After, the Node Id fields for nodes
12538 // before SortedPos will contain the topological sort index, and the
12539 // Node Id fields for nodes At SortedPos and after will contain the
12540 // count of outstanding operands.
12542 checkForCycles(&N, this);
12543 unsigned Degree = N.getNumOperands();
12544 if (Degree == 0) {
12545 // A node with no uses, add it to the result array immediately.
12546 N.setNodeId(DAGSize++);
12547 allnodes_iterator Q(&N);
12548 if (Q != SortedPos)
12549 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12550 assert(SortedPos != AllNodes.end() && "Overran node list");
12551 ++SortedPos;
12552 } else {
12553 // Temporarily use the Node Id as scratch space for the degree count.
12554 N.setNodeId(Degree);
12555 }
12556 }
12557
12558 // Visit all the nodes. As we iterate, move nodes into sorted order,
12559 // such that by the time the end is reached all nodes will be sorted.
12560 for (SDNode &Node : allnodes()) {
12561 SDNode *N = &Node;
12562 checkForCycles(N, this);
12563 // N is in sorted position, so all its uses have one less operand
12564 // that needs to be sorted.
12565 for (SDNode *P : N->users()) {
12566 unsigned Degree = P->getNodeId();
12567 assert(Degree != 0 && "Invalid node degree");
12568 --Degree;
12569 if (Degree == 0) {
12570 // All of P's operands are sorted, so P may sorted now.
12571 P->setNodeId(DAGSize++);
12572 if (P->getIterator() != SortedPos)
12573 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12574 assert(SortedPos != AllNodes.end() && "Overran node list");
12575 ++SortedPos;
12576 } else {
12577 // Update P's outstanding operand count.
12578 P->setNodeId(Degree);
12579 }
12580 }
12581 if (Node.getIterator() == SortedPos) {
12582#ifndef NDEBUG
12584 SDNode *S = &*++I;
12585 dbgs() << "Overran sorted position:\n";
12586 S->dumprFull(this); dbgs() << "\n";
12587 dbgs() << "Checking if this is due to cycles\n";
12588 checkForCycles(this, true);
12589#endif
12590 llvm_unreachable(nullptr);
12591 }
12592 }
12593
12594 assert(SortedPos == AllNodes.end() &&
12595 "Topological sort incomplete!");
12596 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12597 "First node in topological sort is not the entry token!");
12598 assert(AllNodes.front().getNodeId() == 0 &&
12599 "First node in topological sort has non-zero id!");
12600 assert(AllNodes.front().getNumOperands() == 0 &&
12601 "First node in topological sort has operands!");
12602 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12603 "Last node in topologic sort has unexpected id!");
12604 assert(AllNodes.back().use_empty() &&
12605 "Last node in topologic sort has users!");
12606 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12607 return DAGSize;
12608}
12609
12611 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12612 SortedNodes.clear();
12613 // Node -> remaining number of outstanding operands.
12614 DenseMap<const SDNode *, unsigned> RemainingOperands;
12615
12616 // Put nodes without any operands into SortedNodes first.
12617 for (const SDNode &N : allnodes()) {
12618 checkForCycles(&N, this);
12619 unsigned NumOperands = N.getNumOperands();
12620 if (NumOperands == 0)
12621 SortedNodes.push_back(&N);
12622 else
12623 // Record their total number of outstanding operands.
12624 RemainingOperands[&N] = NumOperands;
12625 }
12626
12627 // A node is pushed into SortedNodes when all of its operands (predecessors in
12628 // the graph) are also in SortedNodes.
12629 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12630 const SDNode *N = SortedNodes[i];
12631 for (const SDNode *U : N->users()) {
12632 // HandleSDNode is never part of a DAG and therefore has no entry in
12633 // RemainingOperands.
12634 if (U->getOpcode() == ISD::HANDLENODE)
12635 continue;
12636 unsigned &NumRemOperands = RemainingOperands[U];
12637 assert(NumRemOperands && "Invalid number of remaining operands");
12638 --NumRemOperands;
12639 if (!NumRemOperands)
12640 SortedNodes.push_back(U);
12641 }
12642 }
12643
12644 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12645 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12646 "First node in topological sort is not the entry token");
12647 assert(SortedNodes.front()->getNumOperands() == 0 &&
12648 "First node in topological sort has operands");
12649}
12650
12651/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12652/// value is produced by SD.
12653void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12654 for (SDNode *SD : DB->getSDNodes()) {
12655 if (!SD)
12656 continue;
12657 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12658 SD->setHasDebugValue(true);
12659 }
12660 DbgInfo->add(DB, isParameter);
12661}
12662
12663void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12664
12666 SDValue NewMemOpChain) {
12667 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12668 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12669 // The new memory operation must have the same position as the old load in
12670 // terms of memory dependency. Create a TokenFactor for the old load and new
12671 // memory operation and update uses of the old load's output chain to use that
12672 // TokenFactor.
12673 if (OldChain == NewMemOpChain || OldChain.use_empty())
12674 return NewMemOpChain;
12675
12676 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12677 OldChain, NewMemOpChain);
12678 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12679 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12680 return TokenFactor;
12681}
12682
12684 SDValue NewMemOp) {
12685 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12686 SDValue OldChain = SDValue(OldLoad, 1);
12687 SDValue NewMemOpChain = NewMemOp.getValue(1);
12688 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12689}
12690
12692 Function **OutFunction) {
12693 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12694
12695 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12696 auto *Module = MF->getFunction().getParent();
12697 auto *Function = Module->getFunction(Symbol);
12698
12699 if (OutFunction != nullptr)
12700 *OutFunction = Function;
12701
12702 if (Function != nullptr) {
12703 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12704 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12705 }
12706
12707 std::string ErrorStr;
12708 raw_string_ostream ErrorFormatter(ErrorStr);
12709 ErrorFormatter << "Undefined external symbol ";
12710 ErrorFormatter << '"' << Symbol << '"';
12711 report_fatal_error(Twine(ErrorStr));
12712}
12713
12714//===----------------------------------------------------------------------===//
12715// SDNode Class
12716//===----------------------------------------------------------------------===//
12717
12720 return Const != nullptr && Const->isZero();
12721}
12722
12724 return V.isUndef() || isNullConstant(V);
12725}
12726
12729 return Const != nullptr && Const->isZero() && !Const->isNegative();
12730}
12731
12734 return Const != nullptr && Const->isAllOnes();
12735}
12736
12739 return Const != nullptr && Const->isOne();
12740}
12741
12744 return Const != nullptr && Const->isMinSignedValue();
12745}
12746
12747bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12748 unsigned OperandNo) {
12749 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12750 // TODO: Target-specific opcodes could be added.
12751 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12752 /*AllowTruncation*/ true)) {
12753 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12754 switch (Opcode) {
12755 case ISD::ADD:
12756 case ISD::OR:
12757 case ISD::XOR:
12758 case ISD::UMAX:
12759 return Const.isZero();
12760 case ISD::MUL:
12761 return Const.isOne();
12762 case ISD::AND:
12763 case ISD::UMIN:
12764 return Const.isAllOnes();
12765 case ISD::SMAX:
12766 return Const.isMinSignedValue();
12767 case ISD::SMIN:
12768 return Const.isMaxSignedValue();
12769 case ISD::SUB:
12770 case ISD::SHL:
12771 case ISD::SRA:
12772 case ISD::SRL:
12773 return OperandNo == 1 && Const.isZero();
12774 case ISD::UDIV:
12775 case ISD::SDIV:
12776 return OperandNo == 1 && Const.isOne();
12777 }
12778 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12779 switch (Opcode) {
12780 case ISD::FADD:
12781 return ConstFP->isZero() &&
12782 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12783 case ISD::FSUB:
12784 return OperandNo == 1 && ConstFP->isZero() &&
12785 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12786 case ISD::FMUL:
12787 return ConstFP->isExactlyValue(1.0);
12788 case ISD::FDIV:
12789 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12790 case ISD::FMINNUM:
12791 case ISD::FMAXNUM: {
12792 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12793 EVT VT = V.getValueType();
12794 const fltSemantics &Semantics = VT.getFltSemantics();
12795 APFloat NeutralAF = !Flags.hasNoNaNs()
12796 ? APFloat::getQNaN(Semantics)
12797 : !Flags.hasNoInfs()
12798 ? APFloat::getInf(Semantics)
12799 : APFloat::getLargest(Semantics);
12800 if (Opcode == ISD::FMAXNUM)
12801 NeutralAF.changeSign();
12802
12803 return ConstFP->isExactlyValue(NeutralAF);
12804 }
12805 }
12806 }
12807 return false;
12808}
12809
12811 while (V.getOpcode() == ISD::BITCAST)
12812 V = V.getOperand(0);
12813 return V;
12814}
12815
12817 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12818 V = V.getOperand(0);
12819 return V;
12820}
12821
12823 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12824 V = V.getOperand(0);
12825 return V;
12826}
12827
12829 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12830 SDValue InVec = V.getOperand(0);
12831 SDValue EltNo = V.getOperand(2);
12832 EVT VT = InVec.getValueType();
12833 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12834 if (IndexC && VT.isFixedLengthVector() &&
12835 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12836 !DemandedElts[IndexC->getZExtValue()]) {
12837 V = InVec;
12838 continue;
12839 }
12840 break;
12841 }
12842 return V;
12843}
12844
12846 while (V.getOpcode() == ISD::TRUNCATE)
12847 V = V.getOperand(0);
12848 return V;
12849}
12850
12851bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12852 if (V.getOpcode() != ISD::XOR)
12853 return false;
12854 V = peekThroughBitcasts(V.getOperand(1));
12855 unsigned NumBits = V.getScalarValueSizeInBits();
12856 ConstantSDNode *C =
12857 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12858 return C && (C->getAPIntValue().countr_one() >= NumBits);
12859}
12860
12862 bool AllowTruncation) {
12863 EVT VT = N.getValueType();
12864 APInt DemandedElts = VT.isFixedLengthVector()
12866 : APInt(1, 1);
12867 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12868}
12869
12871 bool AllowUndefs,
12872 bool AllowTruncation) {
12874 return CN;
12875
12876 // SplatVectors can truncate their operands. Ignore that case here unless
12877 // AllowTruncation is set.
12878 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12879 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12880 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12881 EVT CVT = CN->getValueType(0);
12882 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12883 if (AllowTruncation || CVT == VecEltVT)
12884 return CN;
12885 }
12886 }
12887
12889 BitVector UndefElements;
12890 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12891
12892 // BuildVectors can truncate their operands. Ignore that case here unless
12893 // AllowTruncation is set.
12894 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12895 if (CN && (UndefElements.none() || AllowUndefs)) {
12896 EVT CVT = CN->getValueType(0);
12897 EVT NSVT = N.getValueType().getScalarType();
12898 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12899 if (AllowTruncation || (CVT == NSVT))
12900 return CN;
12901 }
12902 }
12903
12904 return nullptr;
12905}
12906
12908 EVT VT = N.getValueType();
12909 APInt DemandedElts = VT.isFixedLengthVector()
12911 : APInt(1, 1);
12912 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12913}
12914
12916 const APInt &DemandedElts,
12917 bool AllowUndefs) {
12919 return CN;
12920
12922 BitVector UndefElements;
12923 ConstantFPSDNode *CN =
12924 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12925 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12926 if (CN && (UndefElements.none() || AllowUndefs))
12927 return CN;
12928 }
12929
12930 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12931 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12932 return CN;
12933
12934 return nullptr;
12935}
12936
12937bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12938 // TODO: may want to use peekThroughBitcast() here.
12939 ConstantSDNode *C =
12940 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12941 return C && C->isZero();
12942}
12943
12944bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12945 ConstantSDNode *C =
12946 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12947 return C && C->isOne();
12948}
12949
12950bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
12951 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
12952 return C && C->isExactlyValue(1.0);
12953}
12954
12955bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12957 unsigned BitWidth = N.getScalarValueSizeInBits();
12958 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12959 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12960}
12961
12962bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
12963 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12964 return C && APInt::isSameValue(C->getAPIntValue(),
12965 APInt(C->getAPIntValue().getBitWidth(), 1));
12966}
12967
12968bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
12970 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
12971 return C && C->isZero();
12972}
12973
12974bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
12975 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
12976 return C && C->isZero();
12977}
12978
12982
12983MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
12984 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
12985 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
12986 MemSDNodeBits.IsVolatile = MMO->isVolatile();
12987 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
12988 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
12989 MemSDNodeBits.IsInvariant = MMO->isInvariant();
12990
12991 // We check here that the size of the memory operand fits within the size of
12992 // the MMO. This is because the MMO might indicate only a possible address
12993 // range instead of specifying the affected memory addresses precisely.
12994 assert(
12995 (!MMO->getType().isValid() ||
12996 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
12997 "Size mismatch!");
12998}
12999
13000/// Profile - Gather unique data for the node.
13001///
13003 AddNodeIDNode(ID, this);
13004}
13005
13006namespace {
13007
13008 struct EVTArray {
13009 std::vector<EVT> VTs;
13010
13011 EVTArray() {
13012 VTs.reserve(MVT::VALUETYPE_SIZE);
13013 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13014 VTs.push_back(MVT((MVT::SimpleValueType)i));
13015 }
13016 };
13017
13018} // end anonymous namespace
13019
13020/// getValueTypeList - Return a pointer to the specified value type.
13021///
13022const EVT *SDNode::getValueTypeList(MVT VT) {
13023 static EVTArray SimpleVTArray;
13024
13025 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13026 return &SimpleVTArray.VTs[VT.SimpleTy];
13027}
13028
13029/// hasAnyUseOfValue - Return true if there are any use of the indicated
13030/// value. This method ignores uses of other values defined by this operation.
13031bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13032 assert(Value < getNumValues() && "Bad value!");
13033
13034 for (SDUse &U : uses())
13035 if (U.getResNo() == Value)
13036 return true;
13037
13038 return false;
13039}
13040
13041/// isOnlyUserOf - Return true if this node is the only use of N.
13042bool SDNode::isOnlyUserOf(const SDNode *N) const {
13043 bool Seen = false;
13044 for (const SDNode *User : N->users()) {
13045 if (User == this)
13046 Seen = true;
13047 else
13048 return false;
13049 }
13050
13051 return Seen;
13052}
13053
13054/// Return true if the only users of N are contained in Nodes.
13056 bool Seen = false;
13057 for (const SDNode *User : N->users()) {
13058 if (llvm::is_contained(Nodes, User))
13059 Seen = true;
13060 else
13061 return false;
13062 }
13063
13064 return Seen;
13065}
13066
13067/// Return true if the referenced return value is an operand of N.
13068bool SDValue::isOperandOf(const SDNode *N) const {
13069 return is_contained(N->op_values(), *this);
13070}
13071
13072bool SDNode::isOperandOf(const SDNode *N) const {
13073 return any_of(N->op_values(),
13074 [this](SDValue Op) { return this == Op.getNode(); });
13075}
13076
13077/// reachesChainWithoutSideEffects - Return true if this operand (which must
13078/// be a chain) reaches the specified operand without crossing any
13079/// side-effecting instructions on any chain path. In practice, this looks
13080/// through token factors and non-volatile loads. In order to remain efficient,
13081/// this only looks a couple of nodes in, it does not do an exhaustive search.
13082///
13083/// Note that we only need to examine chains when we're searching for
13084/// side-effects; SelectionDAG requires that all side-effects are represented
13085/// by chains, even if another operand would force a specific ordering. This
13086/// constraint is necessary to allow transformations like splitting loads.
13088 unsigned Depth) const {
13089 if (*this == Dest) return true;
13090
13091 // Don't search too deeply, we just want to be able to see through
13092 // TokenFactor's etc.
13093 if (Depth == 0) return false;
13094
13095 // If this is a token factor, all inputs to the TF happen in parallel.
13096 if (getOpcode() == ISD::TokenFactor) {
13097 // First, try a shallow search.
13098 if (is_contained((*this)->ops(), Dest)) {
13099 // We found the chain we want as an operand of this TokenFactor.
13100 // Essentially, we reach the chain without side-effects if we could
13101 // serialize the TokenFactor into a simple chain of operations with
13102 // Dest as the last operation. This is automatically true if the
13103 // chain has one use: there are no other ordering constraints.
13104 // If the chain has more than one use, we give up: some other
13105 // use of Dest might force a side-effect between Dest and the current
13106 // node.
13107 if (Dest.hasOneUse())
13108 return true;
13109 }
13110 // Next, try a deep search: check whether every operand of the TokenFactor
13111 // reaches Dest.
13112 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13113 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13114 });
13115 }
13116
13117 // Loads don't have side effects, look through them.
13118 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13119 if (Ld->isUnordered())
13120 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13121 }
13122 return false;
13123}
13124
13125bool SDNode::hasPredecessor(const SDNode *N) const {
13128 Worklist.push_back(this);
13129 return hasPredecessorHelper(N, Visited, Worklist);
13130}
13131
13133 this->Flags &= Flags;
13134}
13135
13136SDValue
13138 ArrayRef<ISD::NodeType> CandidateBinOps,
13139 bool AllowPartials) {
13140 // The pattern must end in an extract from index 0.
13141 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13142 !isNullConstant(Extract->getOperand(1)))
13143 return SDValue();
13144
13145 // Match against one of the candidate binary ops.
13146 SDValue Op = Extract->getOperand(0);
13147 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13148 return Op.getOpcode() == unsigned(BinOp);
13149 }))
13150 return SDValue();
13151
13152 // Floating-point reductions may require relaxed constraints on the final step
13153 // of the reduction because they may reorder intermediate operations.
13154 unsigned CandidateBinOp = Op.getOpcode();
13155 if (Op.getValueType().isFloatingPoint()) {
13156 SDNodeFlags Flags = Op->getFlags();
13157 switch (CandidateBinOp) {
13158 case ISD::FADD:
13159 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13160 return SDValue();
13161 break;
13162 default:
13163 llvm_unreachable("Unhandled FP opcode for binop reduction");
13164 }
13165 }
13166
13167 // Matching failed - attempt to see if we did enough stages that a partial
13168 // reduction from a subvector is possible.
13169 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13170 if (!AllowPartials || !Op)
13171 return SDValue();
13172 EVT OpVT = Op.getValueType();
13173 EVT OpSVT = OpVT.getScalarType();
13174 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13175 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13176 return SDValue();
13177 BinOp = (ISD::NodeType)CandidateBinOp;
13178 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13179 };
13180
13181 // At each stage, we're looking for something that looks like:
13182 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13183 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13184 // i32 undef, i32 undef, i32 undef, i32 undef>
13185 // %a = binop <8 x i32> %op, %s
13186 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13187 // we expect something like:
13188 // <4,5,6,7,u,u,u,u>
13189 // <2,3,u,u,u,u,u,u>
13190 // <1,u,u,u,u,u,u,u>
13191 // While a partial reduction match would be:
13192 // <2,3,u,u,u,u,u,u>
13193 // <1,u,u,u,u,u,u,u>
13194 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13195 SDValue PrevOp;
13196 for (unsigned i = 0; i < Stages; ++i) {
13197 unsigned MaskEnd = (1 << i);
13198
13199 if (Op.getOpcode() != CandidateBinOp)
13200 return PartialReduction(PrevOp, MaskEnd);
13201
13202 SDValue Op0 = Op.getOperand(0);
13203 SDValue Op1 = Op.getOperand(1);
13204
13206 if (Shuffle) {
13207 Op = Op1;
13208 } else {
13209 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13210 Op = Op0;
13211 }
13212
13213 // The first operand of the shuffle should be the same as the other operand
13214 // of the binop.
13215 if (!Shuffle || Shuffle->getOperand(0) != Op)
13216 return PartialReduction(PrevOp, MaskEnd);
13217
13218 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13219 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13220 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13221 return PartialReduction(PrevOp, MaskEnd);
13222
13223 PrevOp = Op;
13224 }
13225
13226 // Handle subvector reductions, which tend to appear after the shuffle
13227 // reduction stages.
13228 while (Op.getOpcode() == CandidateBinOp) {
13229 unsigned NumElts = Op.getValueType().getVectorNumElements();
13230 SDValue Op0 = Op.getOperand(0);
13231 SDValue Op1 = Op.getOperand(1);
13232 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13234 Op0.getOperand(0) != Op1.getOperand(0))
13235 break;
13236 SDValue Src = Op0.getOperand(0);
13237 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13238 if (NumSrcElts != (2 * NumElts))
13239 break;
13240 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13241 Op1.getConstantOperandAPInt(1) == NumElts) &&
13242 !(Op1.getConstantOperandAPInt(1) == 0 &&
13243 Op0.getConstantOperandAPInt(1) == NumElts))
13244 break;
13245 Op = Src;
13246 }
13247
13248 BinOp = (ISD::NodeType)CandidateBinOp;
13249 return Op;
13250}
13251
13253 EVT VT = N->getValueType(0);
13254 EVT EltVT = VT.getVectorElementType();
13255 unsigned NE = VT.getVectorNumElements();
13256
13257 SDLoc dl(N);
13258
13259 // If ResNE is 0, fully unroll the vector op.
13260 if (ResNE == 0)
13261 ResNE = NE;
13262 else if (NE > ResNE)
13263 NE = ResNE;
13264
13265 if (N->getNumValues() == 2) {
13266 SmallVector<SDValue, 8> Scalars0, Scalars1;
13267 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13268 EVT VT1 = N->getValueType(1);
13269 EVT EltVT1 = VT1.getVectorElementType();
13270
13271 unsigned i;
13272 for (i = 0; i != NE; ++i) {
13273 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13274 SDValue Operand = N->getOperand(j);
13275 EVT OperandVT = Operand.getValueType();
13276
13277 // A vector operand; extract a single element.
13278 EVT OperandEltVT = OperandVT.getVectorElementType();
13279 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13280 }
13281
13282 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13283 Scalars0.push_back(EltOp);
13284 Scalars1.push_back(EltOp.getValue(1));
13285 }
13286
13287 for (; i < ResNE; ++i) {
13288 Scalars0.push_back(getUNDEF(EltVT));
13289 Scalars1.push_back(getUNDEF(EltVT1));
13290 }
13291
13292 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13293 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13294 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13295 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13296 return getMergeValues({Vec0, Vec1}, dl);
13297 }
13298
13299 assert(N->getNumValues() == 1 &&
13300 "Can't unroll a vector with multiple results!");
13301
13303 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13304
13305 unsigned i;
13306 for (i= 0; i != NE; ++i) {
13307 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13308 SDValue Operand = N->getOperand(j);
13309 EVT OperandVT = Operand.getValueType();
13310 if (OperandVT.isVector()) {
13311 // A vector operand; extract a single element.
13312 EVT OperandEltVT = OperandVT.getVectorElementType();
13313 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13314 } else {
13315 // A scalar operand; just use it as is.
13316 Operands[j] = Operand;
13317 }
13318 }
13319
13320 switch (N->getOpcode()) {
13321 default: {
13322 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13323 N->getFlags()));
13324 break;
13325 }
13326 case ISD::VSELECT:
13327 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13328 break;
13329 case ISD::SHL:
13330 case ISD::SRA:
13331 case ISD::SRL:
13332 case ISD::ROTL:
13333 case ISD::ROTR:
13334 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13335 getShiftAmountOperand(Operands[0].getValueType(),
13336 Operands[1])));
13337 break;
13339 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13340 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13341 Operands[0],
13342 getValueType(ExtVT)));
13343 break;
13344 }
13345 case ISD::ADDRSPACECAST: {
13346 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13347 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13348 ASC->getSrcAddressSpace(),
13349 ASC->getDestAddressSpace()));
13350 break;
13351 }
13352 }
13353 }
13354
13355 for (; i < ResNE; ++i)
13356 Scalars.push_back(getUNDEF(EltVT));
13357
13358 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13359 return getBuildVector(VecVT, dl, Scalars);
13360}
13361
13362std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13363 SDNode *N, unsigned ResNE) {
13364 unsigned Opcode = N->getOpcode();
13365 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13366 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13367 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13368 "Expected an overflow opcode");
13369
13370 EVT ResVT = N->getValueType(0);
13371 EVT OvVT = N->getValueType(1);
13372 EVT ResEltVT = ResVT.getVectorElementType();
13373 EVT OvEltVT = OvVT.getVectorElementType();
13374 SDLoc dl(N);
13375
13376 // If ResNE is 0, fully unroll the vector op.
13377 unsigned NE = ResVT.getVectorNumElements();
13378 if (ResNE == 0)
13379 ResNE = NE;
13380 else if (NE > ResNE)
13381 NE = ResNE;
13382
13383 SmallVector<SDValue, 8> LHSScalars;
13384 SmallVector<SDValue, 8> RHSScalars;
13385 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13386 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13387
13388 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13389 SDVTList VTs = getVTList(ResEltVT, SVT);
13390 SmallVector<SDValue, 8> ResScalars;
13391 SmallVector<SDValue, 8> OvScalars;
13392 for (unsigned i = 0; i < NE; ++i) {
13393 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13394 SDValue Ov =
13395 getSelect(dl, OvEltVT, Res.getValue(1),
13396 getBoolConstant(true, dl, OvEltVT, ResVT),
13397 getConstant(0, dl, OvEltVT));
13398
13399 ResScalars.push_back(Res);
13400 OvScalars.push_back(Ov);
13401 }
13402
13403 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13404 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13405
13406 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13407 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13408 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13409 getBuildVector(NewOvVT, dl, OvScalars));
13410}
13411
13414 unsigned Bytes,
13415 int Dist) const {
13416 if (LD->isVolatile() || Base->isVolatile())
13417 return false;
13418 // TODO: probably too restrictive for atomics, revisit
13419 if (!LD->isSimple())
13420 return false;
13421 if (LD->isIndexed() || Base->isIndexed())
13422 return false;
13423 if (LD->getChain() != Base->getChain())
13424 return false;
13425 EVT VT = LD->getMemoryVT();
13426 if (VT.getSizeInBits() / 8 != Bytes)
13427 return false;
13428
13429 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13430 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13431
13432 int64_t Offset = 0;
13433 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13434 return (Dist * (int64_t)Bytes == Offset);
13435 return false;
13436}
13437
13438/// InferPtrAlignment - Infer alignment of a load / store address. Return
13439/// std::nullopt if it cannot be inferred.
13441 // If this is a GlobalAddress + cst, return the alignment.
13442 const GlobalValue *GV = nullptr;
13443 int64_t GVOffset = 0;
13444 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13445 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13446 KnownBits Known(PtrWidth);
13448 unsigned AlignBits = Known.countMinTrailingZeros();
13449 if (AlignBits)
13450 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13451 }
13452
13453 // If this is a direct reference to a stack slot, use information about the
13454 // stack slot's alignment.
13455 int FrameIdx = INT_MIN;
13456 int64_t FrameOffset = 0;
13458 FrameIdx = FI->getIndex();
13459 } else if (isBaseWithConstantOffset(Ptr) &&
13460 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
13461 // Handle FI+Cst
13462 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13463 FrameOffset = Ptr.getConstantOperandVal(1);
13464 }
13465
13466 if (FrameIdx != INT_MIN) {
13468 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13469 }
13470
13471 return std::nullopt;
13472}
13473
13474/// Split the scalar node with EXTRACT_ELEMENT using the provided
13475/// VTs and return the low/high part.
13476std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13477 const SDLoc &DL,
13478 const EVT &LoVT,
13479 const EVT &HiVT) {
13480 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13481 "Split node must be a scalar type");
13482 SDValue Lo =
13484 SDValue Hi =
13486 return std::make_pair(Lo, Hi);
13487}
13488
13489/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13490/// which is split (or expanded) into two not necessarily identical pieces.
13491std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13492 // Currently all types are split in half.
13493 EVT LoVT, HiVT;
13494 if (!VT.isVector())
13495 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13496 else
13497 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13498
13499 return std::make_pair(LoVT, HiVT);
13500}
13501
13502/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13503/// type, dependent on an enveloping VT that has been split into two identical
13504/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13505std::pair<EVT, EVT>
13507 bool *HiIsEmpty) const {
13508 EVT EltTp = VT.getVectorElementType();
13509 // Examples:
13510 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13511 // custom VL=9 with enveloping VL=8/8 yields 8/1
13512 // custom VL=10 with enveloping VL=8/8 yields 8/2
13513 // etc.
13514 ElementCount VTNumElts = VT.getVectorElementCount();
13515 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13516 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13517 "Mixing fixed width and scalable vectors when enveloping a type");
13518 EVT LoVT, HiVT;
13519 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13520 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13521 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13522 *HiIsEmpty = false;
13523 } else {
13524 // Flag that hi type has zero storage size, but return split envelop type
13525 // (this would be easier if vector types with zero elements were allowed).
13526 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13527 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13528 *HiIsEmpty = true;
13529 }
13530 return std::make_pair(LoVT, HiVT);
13531}
13532
13533/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13534/// low/high part.
13535std::pair<SDValue, SDValue>
13536SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13537 const EVT &HiVT) {
13538 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13539 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13540 "Splitting vector with an invalid mixture of fixed and scalable "
13541 "vector types");
13543 N.getValueType().getVectorMinNumElements() &&
13544 "More vector elements requested than available!");
13545 SDValue Lo, Hi;
13546 Lo = getExtractSubvector(DL, LoVT, N, 0);
13547 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13548 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13549 // IDX with the runtime scaling factor of the result vector type. For
13550 // fixed-width result vectors, that runtime scaling factor is 1.
13553 return std::make_pair(Lo, Hi);
13554}
13555
13556std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13557 const SDLoc &DL) {
13558 // Split the vector length parameter.
13559 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13560 EVT VT = N.getValueType();
13562 "Expecting the mask to be an evenly-sized vector");
13563 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13564 SDValue HalfNumElts =
13565 VecVT.isFixedLengthVector()
13566 ? getConstant(HalfMinNumElts, DL, VT)
13567 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13568 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13569 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13570 return std::make_pair(Lo, Hi);
13571}
13572
13573/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13575 EVT VT = N.getValueType();
13578 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13579}
13580
13583 unsigned Start, unsigned Count,
13584 EVT EltVT) {
13585 EVT VT = Op.getValueType();
13586 if (Count == 0)
13588 if (EltVT == EVT())
13589 EltVT = VT.getVectorElementType();
13590 SDLoc SL(Op);
13591 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13592 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13593 }
13594}
13595
13596// getAddressSpace - Return the address space this GlobalAddress belongs to.
13598 return getGlobal()->getType()->getAddressSpace();
13599}
13600
13603 return Val.MachineCPVal->getType();
13604 return Val.ConstVal->getType();
13605}
13606
13607bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13608 unsigned &SplatBitSize,
13609 bool &HasAnyUndefs,
13610 unsigned MinSplatBits,
13611 bool IsBigEndian) const {
13612 EVT VT = getValueType(0);
13613 assert(VT.isVector() && "Expected a vector type");
13614 unsigned VecWidth = VT.getSizeInBits();
13615 if (MinSplatBits > VecWidth)
13616 return false;
13617
13618 // FIXME: The widths are based on this node's type, but build vectors can
13619 // truncate their operands.
13620 SplatValue = APInt(VecWidth, 0);
13621 SplatUndef = APInt(VecWidth, 0);
13622
13623 // Get the bits. Bits with undefined values (when the corresponding element
13624 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13625 // in SplatValue. If any of the values are not constant, give up and return
13626 // false.
13627 unsigned int NumOps = getNumOperands();
13628 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13629 unsigned EltWidth = VT.getScalarSizeInBits();
13630
13631 for (unsigned j = 0; j < NumOps; ++j) {
13632 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13633 SDValue OpVal = getOperand(i);
13634 unsigned BitPos = j * EltWidth;
13635
13636 if (OpVal.isUndef())
13637 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13638 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13639 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13640 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13641 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13642 else
13643 return false;
13644 }
13645
13646 // The build_vector is all constants or undefs. Find the smallest element
13647 // size that splats the vector.
13648 HasAnyUndefs = (SplatUndef != 0);
13649
13650 // FIXME: This does not work for vectors with elements less than 8 bits.
13651 while (VecWidth > 8) {
13652 // If we can't split in half, stop here.
13653 if (VecWidth & 1)
13654 break;
13655
13656 unsigned HalfSize = VecWidth / 2;
13657 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13658 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13659 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13660 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13661
13662 // If the two halves do not match (ignoring undef bits), stop here.
13663 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13664 MinSplatBits > HalfSize)
13665 break;
13666
13667 SplatValue = HighValue | LowValue;
13668 SplatUndef = HighUndef & LowUndef;
13669
13670 VecWidth = HalfSize;
13671 }
13672
13673 // FIXME: The loop above only tries to split in halves. But if the input
13674 // vector for example is <3 x i16> it wouldn't be able to detect a
13675 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13676 // optimizations. I guess that back in the days when this helper was created
13677 // vectors normally was power-of-2 sized.
13678
13679 SplatBitSize = VecWidth;
13680 return true;
13681}
13682
13684 BitVector *UndefElements) const {
13685 unsigned NumOps = getNumOperands();
13686 if (UndefElements) {
13687 UndefElements->clear();
13688 UndefElements->resize(NumOps);
13689 }
13690 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13691 if (!DemandedElts)
13692 return SDValue();
13693 SDValue Splatted;
13694 for (unsigned i = 0; i != NumOps; ++i) {
13695 if (!DemandedElts[i])
13696 continue;
13697 SDValue Op = getOperand(i);
13698 if (Op.isUndef()) {
13699 if (UndefElements)
13700 (*UndefElements)[i] = true;
13701 } else if (!Splatted) {
13702 Splatted = Op;
13703 } else if (Splatted != Op) {
13704 return SDValue();
13705 }
13706 }
13707
13708 if (!Splatted) {
13709 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13710 assert(getOperand(FirstDemandedIdx).isUndef() &&
13711 "Can only have a splat without a constant for all undefs.");
13712 return getOperand(FirstDemandedIdx);
13713 }
13714
13715 return Splatted;
13716}
13717
13719 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13720 return getSplatValue(DemandedElts, UndefElements);
13721}
13722
13724 SmallVectorImpl<SDValue> &Sequence,
13725 BitVector *UndefElements) const {
13726 unsigned NumOps = getNumOperands();
13727 Sequence.clear();
13728 if (UndefElements) {
13729 UndefElements->clear();
13730 UndefElements->resize(NumOps);
13731 }
13732 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13733 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13734 return false;
13735
13736 // Set the undefs even if we don't find a sequence (like getSplatValue).
13737 if (UndefElements)
13738 for (unsigned I = 0; I != NumOps; ++I)
13739 if (DemandedElts[I] && getOperand(I).isUndef())
13740 (*UndefElements)[I] = true;
13741
13742 // Iteratively widen the sequence length looking for repetitions.
13743 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13744 Sequence.append(SeqLen, SDValue());
13745 for (unsigned I = 0; I != NumOps; ++I) {
13746 if (!DemandedElts[I])
13747 continue;
13748 SDValue &SeqOp = Sequence[I % SeqLen];
13750 if (Op.isUndef()) {
13751 if (!SeqOp)
13752 SeqOp = Op;
13753 continue;
13754 }
13755 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13756 Sequence.clear();
13757 break;
13758 }
13759 SeqOp = Op;
13760 }
13761 if (!Sequence.empty())
13762 return true;
13763 }
13764
13765 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13766 return false;
13767}
13768
13770 BitVector *UndefElements) const {
13771 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13772 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13773}
13774
13777 BitVector *UndefElements) const {
13779 getSplatValue(DemandedElts, UndefElements));
13780}
13781
13784 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13785}
13786
13789 BitVector *UndefElements) const {
13791 getSplatValue(DemandedElts, UndefElements));
13792}
13793
13798
13799int32_t
13801 uint32_t BitWidth) const {
13802 if (ConstantFPSDNode *CN =
13804 bool IsExact;
13805 APSInt IntVal(BitWidth);
13806 const APFloat &APF = CN->getValueAPF();
13807 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13808 APFloat::opOK ||
13809 !IsExact)
13810 return -1;
13811
13812 return IntVal.exactLogBase2();
13813 }
13814 return -1;
13815}
13816
13818 bool IsLittleEndian, unsigned DstEltSizeInBits,
13819 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13820 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13821 if (!isConstant())
13822 return false;
13823
13824 unsigned NumSrcOps = getNumOperands();
13825 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13826 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13827 "Invalid bitcast scale");
13828
13829 // Extract raw src bits.
13830 SmallVector<APInt> SrcBitElements(NumSrcOps,
13831 APInt::getZero(SrcEltSizeInBits));
13832 BitVector SrcUndeElements(NumSrcOps, false);
13833
13834 for (unsigned I = 0; I != NumSrcOps; ++I) {
13836 if (Op.isUndef()) {
13837 SrcUndeElements.set(I);
13838 continue;
13839 }
13840 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13841 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13842 assert((CInt || CFP) && "Unknown constant");
13843 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13844 : CFP->getValueAPF().bitcastToAPInt();
13845 }
13846
13847 // Recast to dst width.
13848 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13849 SrcBitElements, UndefElements, SrcUndeElements);
13850 return true;
13851}
13852
13853void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13854 unsigned DstEltSizeInBits,
13855 SmallVectorImpl<APInt> &DstBitElements,
13856 ArrayRef<APInt> SrcBitElements,
13857 BitVector &DstUndefElements,
13858 const BitVector &SrcUndefElements) {
13859 unsigned NumSrcOps = SrcBitElements.size();
13860 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13861 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13862 "Invalid bitcast scale");
13863 assert(NumSrcOps == SrcUndefElements.size() &&
13864 "Vector size mismatch");
13865
13866 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13867 DstUndefElements.clear();
13868 DstUndefElements.resize(NumDstOps, false);
13869 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13870
13871 // Concatenate src elements constant bits together into dst element.
13872 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13873 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13874 for (unsigned I = 0; I != NumDstOps; ++I) {
13875 DstUndefElements.set(I);
13876 APInt &DstBits = DstBitElements[I];
13877 for (unsigned J = 0; J != Scale; ++J) {
13878 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13879 if (SrcUndefElements[Idx])
13880 continue;
13881 DstUndefElements.reset(I);
13882 const APInt &SrcBits = SrcBitElements[Idx];
13883 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13884 "Illegal constant bitwidths");
13885 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13886 }
13887 }
13888 return;
13889 }
13890
13891 // Split src element constant bits into dst elements.
13892 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13893 for (unsigned I = 0; I != NumSrcOps; ++I) {
13894 if (SrcUndefElements[I]) {
13895 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13896 continue;
13897 }
13898 const APInt &SrcBits = SrcBitElements[I];
13899 for (unsigned J = 0; J != Scale; ++J) {
13900 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13901 APInt &DstBits = DstBitElements[Idx];
13902 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13903 }
13904 }
13905}
13906
13908 for (const SDValue &Op : op_values()) {
13909 unsigned Opc = Op.getOpcode();
13910 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13911 return false;
13912 }
13913 return true;
13914}
13915
13916std::optional<std::pair<APInt, APInt>>
13918 unsigned NumOps = getNumOperands();
13919 if (NumOps < 2)
13920 return std::nullopt;
13921
13924 return std::nullopt;
13925
13926 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13927 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13928 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13929
13930 if (Stride.isZero())
13931 return std::nullopt;
13932
13933 for (unsigned i = 2; i < NumOps; ++i) {
13935 return std::nullopt;
13936
13937 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13938 if (Val != (Start + (Stride * i)))
13939 return std::nullopt;
13940 }
13941
13942 return std::make_pair(Start, Stride);
13943}
13944
13946 // Find the first non-undef value in the shuffle mask.
13947 unsigned i, e;
13948 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
13949 /* search */;
13950
13951 // If all elements are undefined, this shuffle can be considered a splat
13952 // (although it should eventually get simplified away completely).
13953 if (i == e)
13954 return true;
13955
13956 // Make sure all remaining elements are either undef or the same as the first
13957 // non-undef value.
13958 for (int Idx = Mask[i]; i != e; ++i)
13959 if (Mask[i] >= 0 && Mask[i] != Idx)
13960 return false;
13961 return true;
13962}
13963
13964// Returns true if it is a constant integer BuildVector or constant integer,
13965// possibly hidden by a bitcast.
13967 SDValue N, bool AllowOpaques) const {
13969
13970 if (auto *C = dyn_cast<ConstantSDNode>(N))
13971 return AllowOpaques || !C->isOpaque();
13972
13974 return true;
13975
13976 // Treat a GlobalAddress supporting constant offset folding as a
13977 // constant integer.
13978 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
13979 if (GA->getOpcode() == ISD::GlobalAddress &&
13980 TLI->isOffsetFoldingLegal(GA))
13981 return true;
13982
13983 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13984 isa<ConstantSDNode>(N.getOperand(0)))
13985 return true;
13986 return false;
13987}
13988
13989// Returns true if it is a constant float BuildVector or constant float.
13992 return true;
13993
13995 return true;
13996
13997 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
13998 isa<ConstantFPSDNode>(N.getOperand(0)))
13999 return true;
14000
14001 return false;
14002}
14003
14004std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14005 ConstantSDNode *Const =
14006 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14007 if (!Const)
14008 return std::nullopt;
14009
14010 EVT VT = N->getValueType(0);
14011 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14012 switch (TLI->getBooleanContents(N.getValueType())) {
14014 if (CVal.isOne())
14015 return true;
14016 if (CVal.isZero())
14017 return false;
14018 return std::nullopt;
14020 if (CVal.isAllOnes())
14021 return true;
14022 if (CVal.isZero())
14023 return false;
14024 return std::nullopt;
14026 return CVal[0];
14027 }
14028 llvm_unreachable("Unknown BooleanContent enum");
14029}
14030
14031void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14032 assert(!Node->OperandList && "Node already has operands");
14034 "too many operands to fit into SDNode");
14035 SDUse *Ops = OperandRecycler.allocate(
14036 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14037
14038 bool IsDivergent = false;
14039 for (unsigned I = 0; I != Vals.size(); ++I) {
14040 Ops[I].setUser(Node);
14041 Ops[I].setInitial(Vals[I]);
14042 EVT VT = Ops[I].getValueType();
14043
14044 // Skip Chain. It does not carry divergence.
14045 if (VT != MVT::Other &&
14046 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14047 Ops[I].getNode()->isDivergent()) {
14048 IsDivergent = true;
14049 }
14050 }
14051 Node->NumOperands = Vals.size();
14052 Node->OperandList = Ops;
14053 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14054 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14055 Node->SDNodeBits.IsDivergent = IsDivergent;
14056 }
14057 checkForCycles(Node);
14058}
14059
14062 size_t Limit = SDNode::getMaxNumOperands();
14063 while (Vals.size() > Limit) {
14064 unsigned SliceIdx = Vals.size() - Limit;
14065 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14066 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14067 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14068 Vals.emplace_back(NewTF);
14069 }
14070 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14071}
14072
14074 EVT VT, SDNodeFlags Flags) {
14075 switch (Opcode) {
14076 default:
14077 return SDValue();
14078 case ISD::ADD:
14079 case ISD::OR:
14080 case ISD::XOR:
14081 case ISD::UMAX:
14082 return getConstant(0, DL, VT);
14083 case ISD::MUL:
14084 return getConstant(1, DL, VT);
14085 case ISD::AND:
14086 case ISD::UMIN:
14087 return getAllOnesConstant(DL, VT);
14088 case ISD::SMAX:
14090 case ISD::SMIN:
14092 case ISD::FADD:
14093 // If flags allow, prefer positive zero since it's generally cheaper
14094 // to materialize on most targets.
14095 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14096 case ISD::FMUL:
14097 return getConstantFP(1.0, DL, VT);
14098 case ISD::FMINNUM:
14099 case ISD::FMAXNUM: {
14100 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14101 const fltSemantics &Semantics = VT.getFltSemantics();
14102 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14103 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14104 APFloat::getLargest(Semantics);
14105 if (Opcode == ISD::FMAXNUM)
14106 NeutralAF.changeSign();
14107
14108 return getConstantFP(NeutralAF, DL, VT);
14109 }
14110 case ISD::FMINIMUM:
14111 case ISD::FMAXIMUM: {
14112 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14113 const fltSemantics &Semantics = VT.getFltSemantics();
14114 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14115 : APFloat::getLargest(Semantics);
14116 if (Opcode == ISD::FMAXIMUM)
14117 NeutralAF.changeSign();
14118
14119 return getConstantFP(NeutralAF, DL, VT);
14120 }
14121
14122 }
14123}
14124
14125/// Helper used to make a call to a library function that has one argument of
14126/// pointer type.
14127///
14128/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14129/// used to get or set floating-point state. They have one argument of pointer
14130/// type, which points to the memory region containing bits of the
14131/// floating-point state. The value returned by such function is ignored in the
14132/// created call.
14133///
14134/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14135/// \param Ptr Pointer used to save/load state.
14136/// \param InChain Ingoing token chain.
14137/// \returns Outgoing chain token.
14139 SDValue InChain,
14140 const SDLoc &DLoc) {
14141 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14143 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14144 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
14145 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
14146 TLI->getPointerTy(getDataLayout()));
14148 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14149 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
14150 std::move(Args));
14151 return TLI->LowerCallTo(CLI).second;
14152}
14153
14155 assert(From && To && "Invalid SDNode; empty source SDValue?");
14156 auto I = SDEI.find(From);
14157 if (I == SDEI.end())
14158 return;
14159
14160 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14161 // the iterator, hence the need to make a copy to prevent a use-after-free.
14162 NodeExtraInfo NEI = I->second;
14163 if (LLVM_LIKELY(!NEI.PCSections)) {
14164 // No deep copy required for the types of extra info set.
14165 //
14166 // FIXME: Investigate if other types of extra info also need deep copy. This
14167 // depends on the types of nodes they can be attached to: if some extra info
14168 // is only ever attached to nodes where a replacement To node is always the
14169 // node where later use and propagation of the extra info has the intended
14170 // semantics, no deep copy is required.
14171 SDEI[To] = std::move(NEI);
14172 return;
14173 }
14174
14175 const SDNode *EntrySDN = getEntryNode().getNode();
14176
14177 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14178 // through the replacement of From with To. Otherwise, replacements of a node
14179 // (From) with more complex nodes (To and its operands) may result in lost
14180 // extra info where the root node (To) is insignificant in further propagating
14181 // and using extra info when further lowering to MIR.
14182 //
14183 // In the first step pre-populate the visited set with the nodes reachable
14184 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14185 // DAG that is not new and should be left untouched.
14186 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14187 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14188 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14189 if (MaxDepth == 0) {
14190 // Remember this node in case we need to increase MaxDepth and continue
14191 // populating FromReach from this node.
14192 Leafs.emplace_back(N);
14193 return;
14194 }
14195 if (!FromReach.insert(N).second)
14196 return;
14197 for (const SDValue &Op : N->op_values())
14198 Self(Self, Op.getNode(), MaxDepth - 1);
14199 };
14200
14201 // Copy extra info to To and all its transitive operands (that are new).
14203 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14204 if (FromReach.contains(N))
14205 return true;
14206 if (!Visited.insert(N).second)
14207 return true;
14208 if (EntrySDN == N)
14209 return false;
14210 for (const SDValue &Op : N->op_values()) {
14211 if (N == To && Op.getNode() == EntrySDN) {
14212 // Special case: New node's operand is the entry node; just need to
14213 // copy extra info to new node.
14214 break;
14215 }
14216 if (!Self(Self, Op.getNode()))
14217 return false;
14218 }
14219 // Copy only if entry node was not reached.
14220 SDEI[N] = NEI;
14221 return true;
14222 };
14223
14224 // We first try with a lower MaxDepth, assuming that the path to common
14225 // operands between From and To is relatively short. This significantly
14226 // improves performance in the common case. The initial MaxDepth is big
14227 // enough to avoid retry in the common case; the last MaxDepth is large
14228 // enough to avoid having to use the fallback below (and protects from
14229 // potential stack exhaustion from recursion).
14230 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14231 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14232 // StartFrom is the previous (or initial) set of leafs reachable at the
14233 // previous maximum depth.
14235 std::swap(StartFrom, Leafs);
14236 for (const SDNode *N : StartFrom)
14237 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14238 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14239 return;
14240 // This should happen very rarely (reached the entry node).
14241 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14242 assert(!Leafs.empty());
14243 }
14244
14245 // This should not happen - but if it did, that means the subgraph reachable
14246 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14247 // could not visit all reachable common operands. Consequently, we were able
14248 // to reach the entry node.
14249 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14250 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14251 // Best-effort fallback if assertions disabled.
14252 SDEI[To] = std::move(NEI);
14253}
14254
14255#ifndef NDEBUG
14256static void checkForCyclesHelper(const SDNode *N,
14259 const llvm::SelectionDAG *DAG) {
14260 // If this node has already been checked, don't check it again.
14261 if (Checked.count(N))
14262 return;
14263
14264 // If a node has already been visited on this depth-first walk, reject it as
14265 // a cycle.
14266 if (!Visited.insert(N).second) {
14267 errs() << "Detected cycle in SelectionDAG\n";
14268 dbgs() << "Offending node:\n";
14269 N->dumprFull(DAG); dbgs() << "\n";
14270 abort();
14271 }
14272
14273 for (const SDValue &Op : N->op_values())
14274 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14275
14276 Checked.insert(N);
14277 Visited.erase(N);
14278}
14279#endif
14280
14282 const llvm::SelectionDAG *DAG,
14283 bool force) {
14284#ifndef NDEBUG
14285 bool check = force;
14286#ifdef EXPENSIVE_CHECKS
14287 check = true;
14288#endif // EXPENSIVE_CHECKS
14289 if (check) {
14290 assert(N && "Checking nonexistent SDNode");
14293 checkForCyclesHelper(N, visited, checked, DAG);
14294 }
14295#endif // !NDEBUG
14296}
14297
14298void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14299 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14300}
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:477
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h: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)