LLVM 22.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84using namespace llvm::SDPatternMatch;
85
86/// makeVTList - Return an instance of the SDVTList struct initialized with the
87/// specified members.
88static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
89 SDVTList Res = {VTs, NumVTs};
90 return Res;
91}
92
93// Default null implementations of the callbacks.
97
98void SelectionDAG::DAGNodeDeletedListener::anchor() {}
99void SelectionDAG::DAGNodeInsertedListener::anchor() {}
100
101#define DEBUG_TYPE "selectiondag"
102
103static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
104 cl::Hidden, cl::init(true),
105 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
106
107static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
108 cl::desc("Number limit for gluing ld/st of memcpy."),
109 cl::Hidden, cl::init(0));
110
112 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
113 cl::desc("DAG combiner limit number of steps when searching DAG "
114 "for predecessor nodes"));
115
117 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
118}
119
121
122//===----------------------------------------------------------------------===//
123// ConstantFPSDNode Class
124//===----------------------------------------------------------------------===//
125
126/// isExactlyValue - We don't rely on operator== working on double values, as
127/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
128/// As such, this method can be used to do an exact bit-for-bit comparison of
129/// two floating point values.
131 return getValueAPF().bitwiseIsEqual(V);
132}
133
135 const APFloat& Val) {
136 assert(VT.isFloatingPoint() && "Can only convert between FP types");
137
138 // convert modifies in place, so make a copy.
139 APFloat Val2 = APFloat(Val);
140 bool losesInfo;
142 &losesInfo);
143 return !losesInfo;
144}
145
146//===----------------------------------------------------------------------===//
147// ISD Namespace
148//===----------------------------------------------------------------------===//
149
150bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
151 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
152 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
153 unsigned EltSize =
154 N->getValueType(0).getVectorElementType().getSizeInBits();
155 SplatVal = OptAPInt->trunc(EltSize);
156 return true;
157 }
158 }
159
160 auto *BV = dyn_cast<BuildVectorSDNode>(N);
161 if (!BV)
162 return false;
163
164 APInt SplatUndef;
165 unsigned SplatBitSize;
166 bool HasUndefs;
167 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
168 // Endianness does not matter here. We are checking for a splat given the
169 // element size of the vector, and if we find such a splat for little endian
170 // layout, then that should be valid also for big endian (as the full vector
171 // size is known to be a multiple of the element size).
172 const bool IsBigEndian = false;
173 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
174 EltSize, IsBigEndian) &&
175 EltSize == SplatBitSize;
176}
177
178// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
179// specializations of the more general isConstantSplatVector()?
180
181bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
182 // Look through a bit convert.
183 while (N->getOpcode() == ISD::BITCAST)
184 N = N->getOperand(0).getNode();
185
186 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
187 APInt SplatVal;
188 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
189 }
190
191 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
192
193 unsigned i = 0, e = N->getNumOperands();
194
195 // Skip over all of the undef values.
196 while (i != e && N->getOperand(i).isUndef())
197 ++i;
198
199 // Do not accept an all-undef vector.
200 if (i == e) return false;
201
202 // Do not accept build_vectors that aren't all constants or which have non-~0
203 // elements. We have to be a bit careful here, as the type of the constant
204 // may not be the same as the type of the vector elements due to type
205 // legalization (the elements are promoted to a legal type for the target and
206 // a vector of a type may be legal when the base element type is not).
207 // We only want to check enough bits to cover the vector elements, because
208 // we care if the resultant vector is all ones, not whether the individual
209 // constants are.
210 SDValue NotZero = N->getOperand(i);
211 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
212 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
213 if (OptAPInt->countr_one() < EltSize)
214 return false;
215 } else
216 return false;
217
218 // Okay, we have at least one ~0 value, check to see if the rest match or are
219 // undefs. Even with the above element type twiddling, this should be OK, as
220 // the same type legalization should have applied to all the elements.
221 for (++i; i != e; ++i)
222 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
223 return false;
224 return true;
225}
226
227bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
228 // Look through a bit convert.
229 while (N->getOpcode() == ISD::BITCAST)
230 N = N->getOperand(0).getNode();
231
232 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
233 APInt SplatVal;
234 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
235 }
236
237 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
238
239 bool IsAllUndef = true;
240 for (const SDValue &Op : N->op_values()) {
241 if (Op.isUndef())
242 continue;
243 IsAllUndef = false;
244 // Do not accept build_vectors that aren't all constants or which have non-0
245 // elements. We have to be a bit careful here, as the type of the constant
246 // may not be the same as the type of the vector elements due to type
247 // legalization (the elements are promoted to a legal type for the target
248 // and a vector of a type may be legal when the base element type is not).
249 // We only want to check enough bits to cover the vector elements, because
250 // we care if the resultant vector is all zeros, not whether the individual
251 // constants are.
252 if (auto OptAPInt = Op->bitcastToAPInt()) {
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (OptAPInt->countr_zero() < EltSize)
255 return false;
256 } else
257 return false;
258 }
259
260 // Do not accept an all-undef vector.
261 if (IsAllUndef)
262 return false;
263 return true;
264}
265
267 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
268}
269
271 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
272}
273
275 if (N->getOpcode() != ISD::BUILD_VECTOR)
276 return false;
277
278 for (const SDValue &Op : N->op_values()) {
279 if (Op.isUndef())
280 continue;
282 return false;
283 }
284 return true;
285}
286
288 if (N->getOpcode() != ISD::BUILD_VECTOR)
289 return false;
290
291 for (const SDValue &Op : N->op_values()) {
292 if (Op.isUndef())
293 continue;
295 return false;
296 }
297 return true;
298}
299
300bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
301 bool Signed) {
302 assert(N->getValueType(0).isVector() && "Expected a vector!");
303
304 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
305 if (EltSize <= NewEltSize)
306 return false;
307
308 if (N->getOpcode() == ISD::ZERO_EXTEND) {
309 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
310 NewEltSize) &&
311 !Signed;
312 }
313 if (N->getOpcode() == ISD::SIGN_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 Signed;
317 }
318 if (N->getOpcode() != ISD::BUILD_VECTOR)
319 return false;
320
321 for (const SDValue &Op : N->op_values()) {
322 if (Op.isUndef())
323 continue;
325 return false;
326
327 APInt C = Op->getAsAPIntVal().trunc(EltSize);
328 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
329 return false;
330 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
331 return false;
332 }
333
334 return true;
335}
336
338 // Return false if the node has no operands.
339 // This is "logically inconsistent" with the definition of "all" but
340 // is probably the desired behavior.
341 if (N->getNumOperands() == 0)
342 return false;
343 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
344}
345
347 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
348}
349
350template <typename ConstNodeType>
352 std::function<bool(ConstNodeType *)> Match,
353 bool AllowUndefs, bool AllowTruncation) {
354 // FIXME: Add support for scalar UNDEF cases?
355 if (auto *C = dyn_cast<ConstNodeType>(Op))
356 return Match(C);
357
358 // FIXME: Add support for vector UNDEF cases?
359 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
360 ISD::SPLAT_VECTOR != Op.getOpcode())
361 return false;
362
363 EVT SVT = Op.getValueType().getScalarType();
364 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
365 if (AllowUndefs && Op.getOperand(i).isUndef()) {
366 if (!Match(nullptr))
367 return false;
368 continue;
369 }
370
371 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
372 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
373 !Match(Cst))
374 return false;
375 }
376 return true;
377}
378// Build used template types.
380 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
383
385 SDValue LHS, SDValue RHS,
386 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
387 bool AllowUndefs, bool AllowTypeMismatch) {
388 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
389 return false;
390
391 // TODO: Add support for scalar UNDEF cases?
392 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
393 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
394 return Match(LHSCst, RHSCst);
395
396 // TODO: Add support for vector UNDEF cases?
397 if (LHS.getOpcode() != RHS.getOpcode() ||
398 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
399 LHS.getOpcode() != ISD::SPLAT_VECTOR))
400 return false;
401
402 EVT SVT = LHS.getValueType().getScalarType();
403 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
404 SDValue LHSOp = LHS.getOperand(i);
405 SDValue RHSOp = RHS.getOperand(i);
406 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
407 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
408 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
409 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
410 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
411 return false;
412 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
413 LHSOp.getValueType() != RHSOp.getValueType()))
414 return false;
415 if (!Match(LHSCst, RHSCst))
416 return false;
417 }
418 return true;
419}
420
422 switch (MinMaxOpc) {
423 default:
424 llvm_unreachable("unrecognized opcode");
425 case ISD::UMIN:
426 return ISD::UMAX;
427 case ISD::UMAX:
428 return ISD::UMIN;
429 case ISD::SMIN:
430 return ISD::SMAX;
431 case ISD::SMAX:
432 return ISD::SMIN;
433 }
434}
435
437 switch (VecReduceOpcode) {
438 default:
439 llvm_unreachable("Expected VECREDUCE opcode");
440 case ISD::VECREDUCE_FADD:
441 case ISD::VECREDUCE_SEQ_FADD:
442 case ISD::VP_REDUCE_FADD:
443 case ISD::VP_REDUCE_SEQ_FADD:
444 return ISD::FADD;
445 case ISD::VECREDUCE_FMUL:
446 case ISD::VECREDUCE_SEQ_FMUL:
447 case ISD::VP_REDUCE_FMUL:
448 case ISD::VP_REDUCE_SEQ_FMUL:
449 return ISD::FMUL;
450 case ISD::VECREDUCE_ADD:
451 case ISD::VP_REDUCE_ADD:
452 return ISD::ADD;
453 case ISD::VECREDUCE_MUL:
454 case ISD::VP_REDUCE_MUL:
455 return ISD::MUL;
456 case ISD::VECREDUCE_AND:
457 case ISD::VP_REDUCE_AND:
458 return ISD::AND;
459 case ISD::VECREDUCE_OR:
460 case ISD::VP_REDUCE_OR:
461 return ISD::OR;
462 case ISD::VECREDUCE_XOR:
463 case ISD::VP_REDUCE_XOR:
464 return ISD::XOR;
465 case ISD::VECREDUCE_SMAX:
466 case ISD::VP_REDUCE_SMAX:
467 return ISD::SMAX;
468 case ISD::VECREDUCE_SMIN:
469 case ISD::VP_REDUCE_SMIN:
470 return ISD::SMIN;
471 case ISD::VECREDUCE_UMAX:
472 case ISD::VP_REDUCE_UMAX:
473 return ISD::UMAX;
474 case ISD::VECREDUCE_UMIN:
475 case ISD::VP_REDUCE_UMIN:
476 return ISD::UMIN;
477 case ISD::VECREDUCE_FMAX:
478 case ISD::VP_REDUCE_FMAX:
479 return ISD::FMAXNUM;
480 case ISD::VECREDUCE_FMIN:
481 case ISD::VP_REDUCE_FMIN:
482 return ISD::FMINNUM;
483 case ISD::VECREDUCE_FMAXIMUM:
484 case ISD::VP_REDUCE_FMAXIMUM:
485 return ISD::FMAXIMUM;
486 case ISD::VECREDUCE_FMINIMUM:
487 case ISD::VP_REDUCE_FMINIMUM:
488 return ISD::FMINIMUM;
489 }
490}
491
492bool ISD::isVPOpcode(unsigned Opcode) {
493 switch (Opcode) {
494 default:
495 return false;
496#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
497 case ISD::VPSD: \
498 return true;
499#include "llvm/IR/VPIntrinsics.def"
500 }
501}
502
503bool ISD::isVPBinaryOp(unsigned Opcode) {
504 switch (Opcode) {
505 default:
506 break;
507#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
508#define VP_PROPERTY_BINARYOP return true;
509#define END_REGISTER_VP_SDNODE(VPSD) break;
510#include "llvm/IR/VPIntrinsics.def"
511 }
512 return false;
513}
514
515bool ISD::isVPReduction(unsigned Opcode) {
516 switch (Opcode) {
517 default:
518 return false;
519 case ISD::VP_REDUCE_ADD:
520 case ISD::VP_REDUCE_MUL:
521 case ISD::VP_REDUCE_AND:
522 case ISD::VP_REDUCE_OR:
523 case ISD::VP_REDUCE_XOR:
524 case ISD::VP_REDUCE_SMAX:
525 case ISD::VP_REDUCE_SMIN:
526 case ISD::VP_REDUCE_UMAX:
527 case ISD::VP_REDUCE_UMIN:
528 case ISD::VP_REDUCE_FMAX:
529 case ISD::VP_REDUCE_FMIN:
530 case ISD::VP_REDUCE_FMAXIMUM:
531 case ISD::VP_REDUCE_FMINIMUM:
532 case ISD::VP_REDUCE_FADD:
533 case ISD::VP_REDUCE_FMUL:
534 case ISD::VP_REDUCE_SEQ_FADD:
535 case ISD::VP_REDUCE_SEQ_FMUL:
536 return true;
537 }
538}
539
540/// The operand position of the vector mask.
541std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
542 switch (Opcode) {
543 default:
544 return std::nullopt;
545#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
546 case ISD::VPSD: \
547 return MASKPOS;
548#include "llvm/IR/VPIntrinsics.def"
549 }
550}
551
552/// The operand position of the explicit vector length parameter.
553std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
554 switch (Opcode) {
555 default:
556 return std::nullopt;
557#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
558 case ISD::VPSD: \
559 return EVLPOS;
560#include "llvm/IR/VPIntrinsics.def"
561 }
562}
563
564std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
565 bool hasFPExcept) {
566 // FIXME: Return strict opcodes in case of fp exceptions.
567 switch (VPOpcode) {
568 default:
569 return std::nullopt;
570#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
571#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
572#define END_REGISTER_VP_SDNODE(VPOPC) break;
573#include "llvm/IR/VPIntrinsics.def"
574 }
575 return std::nullopt;
576}
577
578std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
579 switch (Opcode) {
580 default:
581 return std::nullopt;
582#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
583#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
584#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
585#include "llvm/IR/VPIntrinsics.def"
586 }
587}
588
590 switch (ExtType) {
591 case ISD::EXTLOAD:
592 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
593 case ISD::SEXTLOAD:
594 return ISD::SIGN_EXTEND;
595 case ISD::ZEXTLOAD:
596 return ISD::ZERO_EXTEND;
597 default:
598 break;
599 }
600
601 llvm_unreachable("Invalid LoadExtType");
602}
603
605 // To perform this operation, we just need to swap the L and G bits of the
606 // operation.
607 unsigned OldL = (Operation >> 2) & 1;
608 unsigned OldG = (Operation >> 1) & 1;
609 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
610 (OldL << 1) | // New G bit
611 (OldG << 2)); // New L bit.
612}
613
615 unsigned Operation = Op;
616 if (isIntegerLike)
617 Operation ^= 7; // Flip L, G, E bits, but not U.
618 else
619 Operation ^= 15; // Flip all of the condition bits.
620
622 Operation &= ~8; // Don't let N and U bits get set.
623
624 return ISD::CondCode(Operation);
625}
626
630
632 bool isIntegerLike) {
633 return getSetCCInverseImpl(Op, isIntegerLike);
634}
635
636/// For an integer comparison, return 1 if the comparison is a signed operation
637/// and 2 if the result is an unsigned comparison. Return zero if the operation
638/// does not depend on the sign of the input (setne and seteq).
639static int isSignedOp(ISD::CondCode Opcode) {
640 switch (Opcode) {
641 default: llvm_unreachable("Illegal integer setcc operation!");
642 case ISD::SETEQ:
643 case ISD::SETNE: return 0;
644 case ISD::SETLT:
645 case ISD::SETLE:
646 case ISD::SETGT:
647 case ISD::SETGE: return 1;
648 case ISD::SETULT:
649 case ISD::SETULE:
650 case ISD::SETUGT:
651 case ISD::SETUGE: return 2;
652 }
653}
654
656 EVT Type) {
657 bool IsInteger = Type.isInteger();
658 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
659 // Cannot fold a signed integer setcc with an unsigned integer setcc.
660 return ISD::SETCC_INVALID;
661
662 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
663
664 // If the N and U bits get set, then the resultant comparison DOES suddenly
665 // care about orderedness, and it is true when ordered.
666 if (Op > ISD::SETTRUE2)
667 Op &= ~16; // Clear the U bit if the N bit is set.
668
669 // Canonicalize illegal integer setcc's.
670 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
671 Op = ISD::SETNE;
672
673 return ISD::CondCode(Op);
674}
675
677 EVT Type) {
678 bool IsInteger = Type.isInteger();
679 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
680 // Cannot fold a signed setcc with an unsigned setcc.
681 return ISD::SETCC_INVALID;
682
683 // Combine all of the condition bits.
684 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
685
686 // Canonicalize illegal integer setcc's.
687 if (IsInteger) {
688 switch (Result) {
689 default: break;
690 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
691 case ISD::SETOEQ: // SETEQ & SETU[LG]E
692 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
693 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
694 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
695 }
696 }
697
698 return Result;
699}
700
701//===----------------------------------------------------------------------===//
702// SDNode Profile Support
703//===----------------------------------------------------------------------===//
704
705/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
706static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
707 ID.AddInteger(OpC);
708}
709
710/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
711/// solely with their pointer.
713 ID.AddPointer(VTList.VTs);
714}
715
716/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
719 for (const auto &Op : Ops) {
720 ID.AddPointer(Op.getNode());
721 ID.AddInteger(Op.getResNo());
722 }
723}
724
725/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
728 for (const auto &Op : Ops) {
729 ID.AddPointer(Op.getNode());
730 ID.AddInteger(Op.getResNo());
731 }
732}
733
734static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
735 SDVTList VTList, ArrayRef<SDValue> OpList) {
736 AddNodeIDOpcode(ID, OpC);
737 AddNodeIDValueTypes(ID, VTList);
738 AddNodeIDOperands(ID, OpList);
739}
740
741/// If this is an SDNode with special info, add this info to the NodeID data.
743 switch (N->getOpcode()) {
746 case ISD::MCSymbol:
747 llvm_unreachable("Should only be used on nodes with operands");
748 default: break; // Normal nodes don't need extra info.
750 case ISD::Constant: {
752 ID.AddPointer(C->getConstantIntValue());
753 ID.AddBoolean(C->isOpaque());
754 break;
755 }
757 case ISD::ConstantFP:
758 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
759 break;
765 ID.AddPointer(GA->getGlobal());
766 ID.AddInteger(GA->getOffset());
767 ID.AddInteger(GA->getTargetFlags());
768 break;
769 }
770 case ISD::BasicBlock:
772 break;
773 case ISD::Register:
774 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
775 break;
777 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
778 break;
779 case ISD::SRCVALUE:
780 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
781 break;
782 case ISD::FrameIndex:
784 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
785 break;
786 case ISD::PSEUDO_PROBE:
787 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
788 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
789 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
790 break;
791 case ISD::JumpTable:
793 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
794 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
795 break;
799 ID.AddInteger(CP->getAlign().value());
800 ID.AddInteger(CP->getOffset());
801 if (CP->isMachineConstantPoolEntry())
802 CP->getMachineCPVal()->addSelectionDAGCSEId(ID);
803 else
804 ID.AddPointer(CP->getConstVal());
805 ID.AddInteger(CP->getTargetFlags());
806 break;
807 }
808 case ISD::TargetIndex: {
810 ID.AddInteger(TI->getIndex());
811 ID.AddInteger(TI->getOffset());
812 ID.AddInteger(TI->getTargetFlags());
813 break;
814 }
815 case ISD::LOAD: {
816 const LoadSDNode *LD = cast<LoadSDNode>(N);
817 ID.AddInteger(LD->getMemoryVT().getRawBits());
818 ID.AddInteger(LD->getRawSubclassData());
819 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
820 ID.AddInteger(LD->getMemOperand()->getFlags());
821 break;
822 }
823 case ISD::STORE: {
824 const StoreSDNode *ST = cast<StoreSDNode>(N);
825 ID.AddInteger(ST->getMemoryVT().getRawBits());
826 ID.AddInteger(ST->getRawSubclassData());
827 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
828 ID.AddInteger(ST->getMemOperand()->getFlags());
829 break;
830 }
831 case ISD::VP_LOAD: {
832 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
833 ID.AddInteger(ELD->getMemoryVT().getRawBits());
834 ID.AddInteger(ELD->getRawSubclassData());
835 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
836 ID.AddInteger(ELD->getMemOperand()->getFlags());
837 break;
838 }
839 case ISD::VP_LOAD_FF: {
840 const auto *LD = cast<VPLoadFFSDNode>(N);
841 ID.AddInteger(LD->getMemoryVT().getRawBits());
842 ID.AddInteger(LD->getRawSubclassData());
843 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
844 ID.AddInteger(LD->getMemOperand()->getFlags());
845 break;
846 }
847 case ISD::VP_STORE: {
848 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
849 ID.AddInteger(EST->getMemoryVT().getRawBits());
850 ID.AddInteger(EST->getRawSubclassData());
851 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
852 ID.AddInteger(EST->getMemOperand()->getFlags());
853 break;
854 }
855 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
857 ID.AddInteger(SLD->getMemoryVT().getRawBits());
858 ID.AddInteger(SLD->getRawSubclassData());
859 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
860 break;
861 }
862 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
864 ID.AddInteger(SST->getMemoryVT().getRawBits());
865 ID.AddInteger(SST->getRawSubclassData());
866 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
867 break;
868 }
869 case ISD::VP_GATHER: {
871 ID.AddInteger(EG->getMemoryVT().getRawBits());
872 ID.AddInteger(EG->getRawSubclassData());
873 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
874 ID.AddInteger(EG->getMemOperand()->getFlags());
875 break;
876 }
877 case ISD::VP_SCATTER: {
879 ID.AddInteger(ES->getMemoryVT().getRawBits());
880 ID.AddInteger(ES->getRawSubclassData());
881 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
882 ID.AddInteger(ES->getMemOperand()->getFlags());
883 break;
884 }
885 case ISD::MLOAD: {
887 ID.AddInteger(MLD->getMemoryVT().getRawBits());
888 ID.AddInteger(MLD->getRawSubclassData());
889 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
890 ID.AddInteger(MLD->getMemOperand()->getFlags());
891 break;
892 }
893 case ISD::MSTORE: {
895 ID.AddInteger(MST->getMemoryVT().getRawBits());
896 ID.AddInteger(MST->getRawSubclassData());
897 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
898 ID.AddInteger(MST->getMemOperand()->getFlags());
899 break;
900 }
901 case ISD::MGATHER: {
903 ID.AddInteger(MG->getMemoryVT().getRawBits());
904 ID.AddInteger(MG->getRawSubclassData());
905 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
906 ID.AddInteger(MG->getMemOperand()->getFlags());
907 break;
908 }
909 case ISD::MSCATTER: {
911 ID.AddInteger(MS->getMemoryVT().getRawBits());
912 ID.AddInteger(MS->getRawSubclassData());
913 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
914 ID.AddInteger(MS->getMemOperand()->getFlags());
915 break;
916 }
917 case ISD::ATOMIC_CMP_SWAP:
918 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
919 case ISD::ATOMIC_SWAP:
920 case ISD::ATOMIC_LOAD_ADD:
921 case ISD::ATOMIC_LOAD_SUB:
922 case ISD::ATOMIC_LOAD_AND:
923 case ISD::ATOMIC_LOAD_CLR:
924 case ISD::ATOMIC_LOAD_OR:
925 case ISD::ATOMIC_LOAD_XOR:
926 case ISD::ATOMIC_LOAD_NAND:
927 case ISD::ATOMIC_LOAD_MIN:
928 case ISD::ATOMIC_LOAD_MAX:
929 case ISD::ATOMIC_LOAD_UMIN:
930 case ISD::ATOMIC_LOAD_UMAX:
931 case ISD::ATOMIC_LOAD:
932 case ISD::ATOMIC_STORE: {
933 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
934 ID.AddInteger(AT->getMemoryVT().getRawBits());
935 ID.AddInteger(AT->getRawSubclassData());
936 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
937 ID.AddInteger(AT->getMemOperand()->getFlags());
938 break;
939 }
940 case ISD::VECTOR_SHUFFLE: {
941 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
942 for (int M : Mask)
943 ID.AddInteger(M);
944 break;
945 }
946 case ISD::ADDRSPACECAST: {
948 ID.AddInteger(ASC->getSrcAddressSpace());
949 ID.AddInteger(ASC->getDestAddressSpace());
950 break;
951 }
953 case ISD::BlockAddress: {
955 ID.AddPointer(BA->getBlockAddress());
956 ID.AddInteger(BA->getOffset());
957 ID.AddInteger(BA->getTargetFlags());
958 break;
959 }
960 case ISD::AssertAlign:
961 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
962 break;
963 case ISD::PREFETCH:
966 // Handled by MemIntrinsicSDNode check after the switch.
967 break;
968 case ISD::MDNODE_SDNODE:
969 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
970 break;
971 } // end switch (N->getOpcode())
972
973 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
974 // to check.
975 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
976 ID.AddInteger(MN->getRawSubclassData());
977 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
978 ID.AddInteger(MN->getMemOperand()->getFlags());
979 ID.AddInteger(MN->getMemoryVT().getRawBits());
980 }
981}
982
983/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
984/// data.
985static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
986 AddNodeIDOpcode(ID, N->getOpcode());
987 // Add the return value info.
988 AddNodeIDValueTypes(ID, N->getVTList());
989 // Add the operand info.
990 AddNodeIDOperands(ID, N->ops());
991
992 // Handle SDNode leafs with special info.
994}
995
996//===----------------------------------------------------------------------===//
997// SelectionDAG Class
998//===----------------------------------------------------------------------===//
999
1000/// doNotCSE - Return true if CSE should not be performed for this node.
1001static bool doNotCSE(SDNode *N) {
1002 if (N->getValueType(0) == MVT::Glue)
1003 return true; // Never CSE anything that produces a glue result.
1004
1005 switch (N->getOpcode()) {
1006 default: break;
1007 case ISD::HANDLENODE:
1008 case ISD::EH_LABEL:
1009 return true; // Never CSE these nodes.
1010 }
1011
1012 // Check that remaining values produced are not flags.
1013 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1014 if (N->getValueType(i) == MVT::Glue)
1015 return true; // Never CSE anything that produces a glue result.
1016
1017 return false;
1018}
1019
1020/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1021/// SelectionDAG.
1023 // Create a dummy node (which is not added to allnodes), that adds a reference
1024 // to the root node, preventing it from being deleted.
1025 HandleSDNode Dummy(getRoot());
1026
1027 SmallVector<SDNode*, 128> DeadNodes;
1028
1029 // Add all obviously-dead nodes to the DeadNodes worklist.
1030 for (SDNode &Node : allnodes())
1031 if (Node.use_empty())
1032 DeadNodes.push_back(&Node);
1033
1034 RemoveDeadNodes(DeadNodes);
1035
1036 // If the root changed (e.g. it was a dead load, update the root).
1037 setRoot(Dummy.getValue());
1038}
1039
1040/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1041/// given list, and any nodes that become unreachable as a result.
1043
1044 // Process the worklist, deleting the nodes and adding their uses to the
1045 // worklist.
1046 while (!DeadNodes.empty()) {
1047 SDNode *N = DeadNodes.pop_back_val();
1048 // Skip to next node if we've already managed to delete the node. This could
1049 // happen if replacing a node causes a node previously added to the node to
1050 // be deleted.
1051 if (N->getOpcode() == ISD::DELETED_NODE)
1052 continue;
1053
1054 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1055 DUL->NodeDeleted(N, nullptr);
1056
1057 // Take the node out of the appropriate CSE map.
1058 RemoveNodeFromCSEMaps(N);
1059
1060 // Next, brutally remove the operand list. This is safe to do, as there are
1061 // no cycles in the graph.
1062 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1063 SDUse &Use = *I++;
1064 SDNode *Operand = Use.getNode();
1065 Use.set(SDValue());
1066
1067 // Now that we removed this operand, see if there are no uses of it left.
1068 if (Operand->use_empty())
1069 DeadNodes.push_back(Operand);
1070 }
1071
1072 DeallocateNode(N);
1073 }
1074}
1075
1077 SmallVector<SDNode*, 16> DeadNodes(1, N);
1078
1079 // Create a dummy node that adds a reference to the root node, preventing
1080 // it from being deleted. (This matters if the root is an operand of the
1081 // dead node.)
1082 HandleSDNode Dummy(getRoot());
1083
1084 RemoveDeadNodes(DeadNodes);
1085}
1086
1088 // First take this out of the appropriate CSE map.
1089 RemoveNodeFromCSEMaps(N);
1090
1091 // Finally, remove uses due to operands of this node, remove from the
1092 // AllNodes list, and delete the node.
1093 DeleteNodeNotInCSEMaps(N);
1094}
1095
1096void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1097 assert(N->getIterator() != AllNodes.begin() &&
1098 "Cannot delete the entry node!");
1099 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1100
1101 // Drop all of the operands and decrement used node's use counts.
1102 N->DropOperands();
1103
1104 DeallocateNode(N);
1105}
1106
1107void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1108 assert(!(V->isVariadic() && isParameter));
1109 if (isParameter)
1110 ByvalParmDbgValues.push_back(V);
1111 else
1112 DbgValues.push_back(V);
1113 for (const SDNode *Node : V->getSDNodes())
1114 if (Node)
1115 DbgValMap[Node].push_back(V);
1116}
1117
1119 DbgValMapType::iterator I = DbgValMap.find(Node);
1120 if (I == DbgValMap.end())
1121 return;
1122 for (auto &Val: I->second)
1123 Val->setIsInvalidated();
1124 DbgValMap.erase(I);
1125}
1126
1127void SelectionDAG::DeallocateNode(SDNode *N) {
1128 // If we have operands, deallocate them.
1130
1131 NodeAllocator.Deallocate(AllNodes.remove(N));
1132
1133 // Set the opcode to DELETED_NODE to help catch bugs when node
1134 // memory is reallocated.
1135 // FIXME: There are places in SDag that have grown a dependency on the opcode
1136 // value in the released node.
1137 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1138 N->NodeType = ISD::DELETED_NODE;
1139
1140 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1141 // them and forget about that node.
1142 DbgInfo->erase(N);
1143
1144 // Invalidate extra info.
1145 SDEI.erase(N);
1146}
1147
1148#ifndef NDEBUG
1149/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1150void SelectionDAG::verifyNode(SDNode *N) const {
1151 switch (N->getOpcode()) {
1152 default:
1153 if (N->isTargetOpcode())
1155 break;
1156 case ISD::BUILD_PAIR: {
1157 EVT VT = N->getValueType(0);
1158 assert(N->getNumValues() == 1 && "Too many results!");
1159 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1160 "Wrong return type!");
1161 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1162 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1163 "Mismatched operand types!");
1164 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1165 "Wrong operand type!");
1166 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1167 "Wrong return type size");
1168 break;
1169 }
1170 case ISD::BUILD_VECTOR: {
1171 assert(N->getNumValues() == 1 && "Too many results!");
1172 assert(N->getValueType(0).isVector() && "Wrong return type!");
1173 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1174 "Wrong number of operands!");
1175 EVT EltVT = N->getValueType(0).getVectorElementType();
1176 for (const SDUse &Op : N->ops()) {
1177 assert((Op.getValueType() == EltVT ||
1178 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1179 EltVT.bitsLE(Op.getValueType()))) &&
1180 "Wrong operand type!");
1181 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1182 "Operands must all have the same type");
1183 }
1184 break;
1185 }
1186 }
1187}
1188#endif // NDEBUG
1189
1190/// Insert a newly allocated node into the DAG.
1191///
1192/// Handles insertion into the all nodes list and CSE map, as well as
1193/// verification and other common operations when a new node is allocated.
1194void SelectionDAG::InsertNode(SDNode *N) {
1195 AllNodes.push_back(N);
1196#ifndef NDEBUG
1197 N->PersistentId = NextPersistentId++;
1198 verifyNode(N);
1199#endif
1200 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1201 DUL->NodeInserted(N);
1202}
1203
1204/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1205/// correspond to it. This is useful when we're about to delete or repurpose
1206/// the node. We don't want future request for structurally identical nodes
1207/// to return N anymore.
1208bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1209 bool Erased = false;
1210 switch (N->getOpcode()) {
1211 case ISD::HANDLENODE: return false; // noop.
1212 case ISD::CONDCODE:
1213 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1214 "Cond code doesn't exist!");
1215 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1216 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1217 break;
1219 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1220 break;
1222 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1223 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1224 ESN->getSymbol(), ESN->getTargetFlags()));
1225 break;
1226 }
1227 case ISD::MCSymbol: {
1228 auto *MCSN = cast<MCSymbolSDNode>(N);
1229 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1230 break;
1231 }
1232 case ISD::VALUETYPE: {
1233 EVT VT = cast<VTSDNode>(N)->getVT();
1234 if (VT.isExtended()) {
1235 Erased = ExtendedValueTypeNodes.erase(VT);
1236 } else {
1237 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1238 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1239 }
1240 break;
1241 }
1242 default:
1243 // Remove it from the CSE Map.
1244 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1245 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1246 Erased = CSEMap.RemoveNode(N);
1247 break;
1248 }
1249#ifndef NDEBUG
1250 // Verify that the node was actually in one of the CSE maps, unless it has a
1251 // glue result (which cannot be CSE'd) or is one of the special cases that are
1252 // not subject to CSE.
1253 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1254 !N->isMachineOpcode() && !doNotCSE(N)) {
1255 N->dump(this);
1256 dbgs() << "\n";
1257 llvm_unreachable("Node is not in map!");
1258 }
1259#endif
1260 return Erased;
1261}
1262
1263/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1264/// maps and modified in place. Add it back to the CSE maps, unless an identical
1265/// node already exists, in which case transfer all its users to the existing
1266/// node. This transfer can potentially trigger recursive merging.
1267void
1268SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1269 // For node types that aren't CSE'd, just act as if no identical node
1270 // already exists.
1271 if (!doNotCSE(N)) {
1272 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1273 if (Existing != N) {
1274 // If there was already an existing matching node, use ReplaceAllUsesWith
1275 // to replace the dead one with the existing one. This can cause
1276 // recursive merging of other unrelated nodes down the line.
1277 Existing->intersectFlagsWith(N->getFlags());
1278 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1279 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1280 ReplaceAllUsesWith(N, Existing);
1281
1282 // N is now dead. Inform the listeners and delete it.
1283 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1284 DUL->NodeDeleted(N, Existing);
1285 DeleteNodeNotInCSEMaps(N);
1286 return;
1287 }
1288 }
1289
1290 // If the node doesn't already exist, we updated it. Inform listeners.
1291 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1292 DUL->NodeUpdated(N);
1293}
1294
1295/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1296/// were replaced with those specified. If this node is never memoized,
1297/// return null, otherwise return a pointer to the slot it would take. If a
1298/// node already exists with these operands, the slot will be non-null.
1299SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1300 void *&InsertPos) {
1301 if (doNotCSE(N))
1302 return nullptr;
1303
1304 SDValue Ops[] = { Op };
1305 FoldingSetNodeID ID;
1306 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1308 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1309 if (Node)
1310 Node->intersectFlagsWith(N->getFlags());
1311 return Node;
1312}
1313
1314/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1315/// were replaced with those specified. If this node is never memoized,
1316/// return null, otherwise return a pointer to the slot it would take. If a
1317/// node already exists with these operands, the slot will be non-null.
1318SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1319 SDValue Op1, SDValue Op2,
1320 void *&InsertPos) {
1321 if (doNotCSE(N))
1322 return nullptr;
1323
1324 SDValue Ops[] = { Op1, Op2 };
1325 FoldingSetNodeID ID;
1326 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1328 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1329 if (Node)
1330 Node->intersectFlagsWith(N->getFlags());
1331 return Node;
1332}
1333
1334/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1335/// were replaced with those specified. If this node is never memoized,
1336/// return null, otherwise return a pointer to the slot it would take. If a
1337/// node already exists with these operands, the slot will be non-null.
1338SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1339 void *&InsertPos) {
1340 if (doNotCSE(N))
1341 return nullptr;
1342
1343 FoldingSetNodeID ID;
1344 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1346 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1347 if (Node)
1348 Node->intersectFlagsWith(N->getFlags());
1349 return Node;
1350}
1351
1353 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1354 : VT.getTypeForEVT(*getContext());
1355
1356 return getDataLayout().getABITypeAlign(Ty);
1357}
1358
1359// EntryNode could meaningfully have debug info if we can find it...
1361 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1362 getVTList(MVT::Other, MVT::Glue)),
1363 Root(getEntryNode()) {
1364 InsertNode(&EntryNode);
1365 DbgInfo = new SDDbgInfo();
1366}
1367
1369 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1370 const TargetLibraryInfo *LibraryInfo,
1371 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1373 FunctionVarLocs const *VarLocs) {
1374 MF = &NewMF;
1375 SDAGISelPass = PassPtr;
1376 ORE = &NewORE;
1379 LibInfo = LibraryInfo;
1380 Context = &MF->getFunction().getContext();
1381 UA = NewUA;
1382 PSI = PSIin;
1383 BFI = BFIin;
1384 MMI = &MMIin;
1385 FnVarLocs = VarLocs;
1386}
1387
1389 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1390 allnodes_clear();
1391 OperandRecycler.clear(OperandAllocator);
1392 delete DbgInfo;
1393}
1394
1396 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1397}
1398
1399void SelectionDAG::allnodes_clear() {
1400 assert(&*AllNodes.begin() == &EntryNode);
1401 AllNodes.remove(AllNodes.begin());
1402 while (!AllNodes.empty())
1403 DeallocateNode(&AllNodes.front());
1404#ifndef NDEBUG
1405 NextPersistentId = 0;
1406#endif
1407}
1408
1409SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1410 void *&InsertPos) {
1411 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1412 if (N) {
1413 switch (N->getOpcode()) {
1414 default: break;
1415 case ISD::Constant:
1416 case ISD::ConstantFP:
1417 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1418 "debug location. Use another overload.");
1419 }
1420 }
1421 return N;
1422}
1423
1424SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1425 const SDLoc &DL, void *&InsertPos) {
1426 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1427 if (N) {
1428 switch (N->getOpcode()) {
1429 case ISD::Constant:
1430 case ISD::ConstantFP:
1431 // Erase debug location from the node if the node is used at several
1432 // different places. Do not propagate one location to all uses as it
1433 // will cause a worse single stepping debugging experience.
1434 if (N->getDebugLoc() != DL.getDebugLoc())
1435 N->setDebugLoc(DebugLoc());
1436 break;
1437 default:
1438 // When the node's point of use is located earlier in the instruction
1439 // sequence than its prior point of use, update its debug info to the
1440 // earlier location.
1441 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1442 N->setDebugLoc(DL.getDebugLoc());
1443 break;
1444 }
1445 }
1446 return N;
1447}
1448
1450 allnodes_clear();
1451 OperandRecycler.clear(OperandAllocator);
1452 OperandAllocator.Reset();
1453 CSEMap.clear();
1454
1455 ExtendedValueTypeNodes.clear();
1456 ExternalSymbols.clear();
1457 TargetExternalSymbols.clear();
1458 MCSymbols.clear();
1459 SDEI.clear();
1460 llvm::fill(CondCodeNodes, nullptr);
1461 llvm::fill(ValueTypeNodes, nullptr);
1462
1463 EntryNode.UseList = nullptr;
1464 InsertNode(&EntryNode);
1465 Root = getEntryNode();
1466 DbgInfo->clear();
1467}
1468
1470 return VT.bitsGT(Op.getValueType())
1471 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1472 : getNode(ISD::FP_ROUND, DL, VT, Op,
1473 getIntPtrConstant(0, DL, /*isTarget=*/true));
1474}
1475
1476std::pair<SDValue, SDValue>
1478 const SDLoc &DL, EVT VT) {
1479 assert(!VT.bitsEq(Op.getValueType()) &&
1480 "Strict no-op FP extend/round not allowed.");
1481 SDValue Res =
1482 VT.bitsGT(Op.getValueType())
1483 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1484 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1485 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1486
1487 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1488}
1489
1491 return VT.bitsGT(Op.getValueType()) ?
1492 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1493 getNode(ISD::TRUNCATE, DL, VT, Op);
1494}
1495
1497 return VT.bitsGT(Op.getValueType()) ?
1498 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1499 getNode(ISD::TRUNCATE, DL, VT, Op);
1500}
1501
1503 return VT.bitsGT(Op.getValueType()) ?
1504 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1505 getNode(ISD::TRUNCATE, DL, VT, Op);
1506}
1507
1509 EVT VT) {
1510 assert(!VT.isVector());
1511 auto Type = Op.getValueType();
1512 SDValue DestOp;
1513 if (Type == VT)
1514 return Op;
1515 auto Size = Op.getValueSizeInBits();
1516 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1517 if (DestOp.getValueType() == VT)
1518 return DestOp;
1519
1520 return getAnyExtOrTrunc(DestOp, DL, VT);
1521}
1522
1524 EVT VT) {
1525 assert(!VT.isVector());
1526 auto Type = Op.getValueType();
1527 SDValue DestOp;
1528 if (Type == VT)
1529 return Op;
1530 auto Size = Op.getValueSizeInBits();
1531 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1532 if (DestOp.getValueType() == VT)
1533 return DestOp;
1534
1535 return getSExtOrTrunc(DestOp, DL, VT);
1536}
1537
1539 EVT VT) {
1540 assert(!VT.isVector());
1541 auto Type = Op.getValueType();
1542 SDValue DestOp;
1543 if (Type == VT)
1544 return Op;
1545 auto Size = Op.getValueSizeInBits();
1546 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1547 if (DestOp.getValueType() == VT)
1548 return DestOp;
1549
1550 return getZExtOrTrunc(DestOp, DL, VT);
1551}
1552
1554 EVT OpVT) {
1555 if (VT.bitsLE(Op.getValueType()))
1556 return getNode(ISD::TRUNCATE, SL, VT, Op);
1557
1558 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1559 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1560}
1561
1563 EVT OpVT = Op.getValueType();
1564 assert(VT.isInteger() && OpVT.isInteger() &&
1565 "Cannot getZeroExtendInReg FP types");
1566 assert(VT.isVector() == OpVT.isVector() &&
1567 "getZeroExtendInReg type should be vector iff the operand "
1568 "type is vector!");
1569 assert((!VT.isVector() ||
1571 "Vector element counts must match in getZeroExtendInReg");
1572 assert(VT.bitsLE(OpVT) && "Not extending!");
1573 if (OpVT == VT)
1574 return Op;
1576 VT.getScalarSizeInBits());
1577 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1578}
1579
1581 SDValue EVL, const SDLoc &DL,
1582 EVT VT) {
1583 EVT OpVT = Op.getValueType();
1584 assert(VT.isInteger() && OpVT.isInteger() &&
1585 "Cannot getVPZeroExtendInReg FP types");
1586 assert(VT.isVector() && OpVT.isVector() &&
1587 "getVPZeroExtendInReg type and operand type should be vector!");
1589 "Vector element counts must match in getZeroExtendInReg");
1590 assert(VT.bitsLE(OpVT) && "Not extending!");
1591 if (OpVT == VT)
1592 return Op;
1594 VT.getScalarSizeInBits());
1595 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1596 EVL);
1597}
1598
1600 // Only unsigned pointer semantics are supported right now. In the future this
1601 // might delegate to TLI to check pointer signedness.
1602 return getZExtOrTrunc(Op, DL, VT);
1603}
1604
1606 // Only unsigned pointer semantics are supported right now. In the future this
1607 // might delegate to TLI to check pointer signedness.
1608 return getZeroExtendInReg(Op, DL, VT);
1609}
1610
1612 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1613}
1614
1615/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1617 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1618}
1619
1621 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1622 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1623}
1624
1626 SDValue Mask, SDValue EVL, EVT VT) {
1627 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1628 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1629}
1630
1632 SDValue Mask, SDValue EVL) {
1633 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1634}
1635
1637 SDValue Mask, SDValue EVL) {
1638 if (VT.bitsGT(Op.getValueType()))
1639 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1640 if (VT.bitsLT(Op.getValueType()))
1641 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1642 return Op;
1643}
1644
1646 EVT OpVT) {
1647 if (!V)
1648 return getConstant(0, DL, VT);
1649
1650 switch (TLI->getBooleanContents(OpVT)) {
1653 return getConstant(1, DL, VT);
1655 return getAllOnesConstant(DL, VT);
1656 }
1657 llvm_unreachable("Unexpected boolean content enum!");
1658}
1659
1661 bool isT, bool isO) {
1662 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1663 DL, VT, isT, isO);
1664}
1665
1667 bool isT, bool isO) {
1668 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1669}
1670
1672 EVT VT, bool isT, bool isO) {
1673 assert(VT.isInteger() && "Cannot create FP integer constant!");
1674
1675 EVT EltVT = VT.getScalarType();
1676 const ConstantInt *Elt = &Val;
1677
1678 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1679 // to-be-splatted scalar ConstantInt.
1680 if (isa<VectorType>(Elt->getType()))
1681 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1682
1683 // In some cases the vector type is legal but the element type is illegal and
1684 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1685 // inserted value (the type does not need to match the vector element type).
1686 // Any extra bits introduced will be truncated away.
1687 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1689 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1690 APInt NewVal;
1691 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1692 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1693 else
1694 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1695 Elt = ConstantInt::get(*getContext(), NewVal);
1696 }
1697 // In other cases the element type is illegal and needs to be expanded, for
1698 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1699 // the value into n parts and use a vector type with n-times the elements.
1700 // Then bitcast to the type requested.
1701 // Legalizing constants too early makes the DAGCombiner's job harder so we
1702 // only legalize if the DAG tells us we must produce legal types.
1703 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1704 TLI->getTypeAction(*getContext(), EltVT) ==
1706 const APInt &NewVal = Elt->getValue();
1707 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1708 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1709
1710 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1711 if (VT.isScalableVector() ||
1712 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1713 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1714 "Can only handle an even split!");
1715 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1716
1717 SmallVector<SDValue, 2> ScalarParts;
1718 for (unsigned i = 0; i != Parts; ++i)
1719 ScalarParts.push_back(getConstant(
1720 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1721 ViaEltVT, isT, isO));
1722
1723 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1724 }
1725
1726 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1727 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1728
1729 // Check the temporary vector is the correct size. If this fails then
1730 // getTypeToTransformTo() probably returned a type whose size (in bits)
1731 // isn't a power-of-2 factor of the requested type size.
1732 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1733
1734 SmallVector<SDValue, 2> EltParts;
1735 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1736 EltParts.push_back(getConstant(
1737 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1738 ViaEltVT, isT, isO));
1739
1740 // EltParts is currently in little endian order. If we actually want
1741 // big-endian order then reverse it now.
1742 if (getDataLayout().isBigEndian())
1743 std::reverse(EltParts.begin(), EltParts.end());
1744
1745 // The elements must be reversed when the element order is different
1746 // to the endianness of the elements (because the BITCAST is itself a
1747 // vector shuffle in this situation). However, we do not need any code to
1748 // perform this reversal because getConstant() is producing a vector
1749 // splat.
1750 // This situation occurs in MIPS MSA.
1751
1753 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1754 llvm::append_range(Ops, EltParts);
1755
1756 SDValue V =
1757 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1758 return V;
1759 }
1760
1761 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1762 "APInt size does not match type size!");
1763 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1764 SDVTList VTs = getVTList(EltVT);
1766 AddNodeIDNode(ID, Opc, VTs, {});
1767 ID.AddPointer(Elt);
1768 ID.AddBoolean(isO);
1769 void *IP = nullptr;
1770 SDNode *N = nullptr;
1771 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1772 if (!VT.isVector())
1773 return SDValue(N, 0);
1774
1775 if (!N) {
1776 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1777 CSEMap.InsertNode(N, IP);
1778 InsertNode(N);
1779 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1780 }
1781
1782 SDValue Result(N, 0);
1783 if (VT.isVector())
1784 Result = getSplat(VT, DL, Result);
1785 return Result;
1786}
1787
1789 bool isT, bool isO) {
1790 unsigned Size = VT.getScalarSizeInBits();
1791 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1792}
1793
1795 bool IsOpaque) {
1797 IsTarget, IsOpaque);
1798}
1799
1801 bool isTarget) {
1802 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1803}
1804
1806 const SDLoc &DL) {
1807 assert(VT.isInteger() && "Shift amount is not an integer type!");
1808 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1809 return getConstant(Val, DL, ShiftVT);
1810}
1811
1813 const SDLoc &DL) {
1814 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1815 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1816}
1817
1819 bool isTarget) {
1820 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1821}
1822
1824 bool isTarget) {
1825 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1826}
1827
1829 EVT VT, bool isTarget) {
1830 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1831
1832 EVT EltVT = VT.getScalarType();
1833 const ConstantFP *Elt = &V;
1834
1835 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1836 // the to-be-splatted scalar ConstantFP.
1837 if (isa<VectorType>(Elt->getType()))
1838 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1839
1840 // Do the map lookup using the actual bit pattern for the floating point
1841 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1842 // we don't have issues with SNANs.
1843 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1844 SDVTList VTs = getVTList(EltVT);
1846 AddNodeIDNode(ID, Opc, VTs, {});
1847 ID.AddPointer(Elt);
1848 void *IP = nullptr;
1849 SDNode *N = nullptr;
1850 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1851 if (!VT.isVector())
1852 return SDValue(N, 0);
1853
1854 if (!N) {
1855 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1856 CSEMap.InsertNode(N, IP);
1857 InsertNode(N);
1858 }
1859
1860 SDValue Result(N, 0);
1861 if (VT.isVector())
1862 Result = getSplat(VT, DL, Result);
1863 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1864 return Result;
1865}
1866
1868 bool isTarget) {
1869 EVT EltVT = VT.getScalarType();
1870 if (EltVT == MVT::f32)
1871 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1872 if (EltVT == MVT::f64)
1873 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1874 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1875 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1876 bool Ignored;
1877 APFloat APF = APFloat(Val);
1879 &Ignored);
1880 return getConstantFP(APF, DL, VT, isTarget);
1881 }
1882 llvm_unreachable("Unsupported type in getConstantFP");
1883}
1884
1886 EVT VT, int64_t Offset, bool isTargetGA,
1887 unsigned TargetFlags) {
1888 assert((TargetFlags == 0 || isTargetGA) &&
1889 "Cannot set target flags on target-independent globals");
1890
1891 // Truncate (with sign-extension) the offset value to the pointer size.
1893 if (BitWidth < 64)
1895
1896 unsigned Opc;
1897 if (GV->isThreadLocal())
1899 else
1901
1902 SDVTList VTs = getVTList(VT);
1904 AddNodeIDNode(ID, Opc, VTs, {});
1905 ID.AddPointer(GV);
1906 ID.AddInteger(Offset);
1907 ID.AddInteger(TargetFlags);
1908 void *IP = nullptr;
1909 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1910 return SDValue(E, 0);
1911
1912 auto *N = newSDNode<GlobalAddressSDNode>(
1913 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1914 CSEMap.InsertNode(N, IP);
1915 InsertNode(N);
1916 return SDValue(N, 0);
1917}
1918
1920 SDVTList VTs = getVTList(MVT::Untyped);
1923 ID.AddPointer(GV);
1924 void *IP = nullptr;
1925 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1926 return SDValue(E, 0);
1927
1928 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1929 CSEMap.InsertNode(N, IP);
1930 InsertNode(N);
1931 return SDValue(N, 0);
1932}
1933
1934SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1935 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1936 SDVTList VTs = getVTList(VT);
1938 AddNodeIDNode(ID, Opc, VTs, {});
1939 ID.AddInteger(FI);
1940 void *IP = nullptr;
1941 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1942 return SDValue(E, 0);
1943
1944 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1945 CSEMap.InsertNode(N, IP);
1946 InsertNode(N);
1947 return SDValue(N, 0);
1948}
1949
1950SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1951 unsigned TargetFlags) {
1952 assert((TargetFlags == 0 || isTarget) &&
1953 "Cannot set target flags on target-independent jump tables");
1954 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1955 SDVTList VTs = getVTList(VT);
1957 AddNodeIDNode(ID, Opc, VTs, {});
1958 ID.AddInteger(JTI);
1959 ID.AddInteger(TargetFlags);
1960 void *IP = nullptr;
1961 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1962 return SDValue(E, 0);
1963
1964 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1965 CSEMap.InsertNode(N, IP);
1966 InsertNode(N);
1967 return SDValue(N, 0);
1968}
1969
1971 const SDLoc &DL) {
1973 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1974 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1975}
1976
1978 MaybeAlign Alignment, int Offset,
1979 bool isTarget, unsigned TargetFlags) {
1980 assert((TargetFlags == 0 || isTarget) &&
1981 "Cannot set target flags on target-independent globals");
1982 if (!Alignment)
1983 Alignment = shouldOptForSize()
1984 ? getDataLayout().getABITypeAlign(C->getType())
1985 : getDataLayout().getPrefTypeAlign(C->getType());
1986 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1987 SDVTList VTs = getVTList(VT);
1989 AddNodeIDNode(ID, Opc, VTs, {});
1990 ID.AddInteger(Alignment->value());
1991 ID.AddInteger(Offset);
1992 ID.AddPointer(C);
1993 ID.AddInteger(TargetFlags);
1994 void *IP = nullptr;
1995 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1996 return SDValue(E, 0);
1997
1998 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1999 TargetFlags);
2000 CSEMap.InsertNode(N, IP);
2001 InsertNode(N);
2002 SDValue V = SDValue(N, 0);
2003 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2004 return V;
2005}
2006
2008 MaybeAlign Alignment, int Offset,
2009 bool isTarget, unsigned TargetFlags) {
2010 assert((TargetFlags == 0 || isTarget) &&
2011 "Cannot set target flags on target-independent globals");
2012 if (!Alignment)
2013 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2014 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2015 SDVTList VTs = getVTList(VT);
2017 AddNodeIDNode(ID, Opc, VTs, {});
2018 ID.AddInteger(Alignment->value());
2019 ID.AddInteger(Offset);
2020 C->addSelectionDAGCSEId(ID);
2021 ID.AddInteger(TargetFlags);
2022 void *IP = nullptr;
2023 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2024 return SDValue(E, 0);
2025
2026 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2027 TargetFlags);
2028 CSEMap.InsertNode(N, IP);
2029 InsertNode(N);
2030 return SDValue(N, 0);
2031}
2032
2035 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2036 ID.AddPointer(MBB);
2037 void *IP = nullptr;
2038 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2039 return SDValue(E, 0);
2040
2041 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2042 CSEMap.InsertNode(N, IP);
2043 InsertNode(N);
2044 return SDValue(N, 0);
2045}
2046
2048 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2049 ValueTypeNodes.size())
2050 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2051
2052 SDNode *&N = VT.isExtended() ?
2053 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2054
2055 if (N) return SDValue(N, 0);
2056 N = newSDNode<VTSDNode>(VT);
2057 InsertNode(N);
2058 return SDValue(N, 0);
2059}
2060
2062 SDNode *&N = ExternalSymbols[Sym];
2063 if (N) return SDValue(N, 0);
2064 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2065 InsertNode(N);
2066 return SDValue(N, 0);
2067}
2068
2070 SDNode *&N = MCSymbols[Sym];
2071 if (N)
2072 return SDValue(N, 0);
2073 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2074 InsertNode(N);
2075 return SDValue(N, 0);
2076}
2077
2079 unsigned TargetFlags) {
2080 SDNode *&N =
2081 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2082 if (N) return SDValue(N, 0);
2083 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2084 InsertNode(N);
2085 return SDValue(N, 0);
2086}
2087
2089 if ((unsigned)Cond >= CondCodeNodes.size())
2090 CondCodeNodes.resize(Cond+1);
2091
2092 if (!CondCodeNodes[Cond]) {
2093 auto *N = newSDNode<CondCodeSDNode>(Cond);
2094 CondCodeNodes[Cond] = N;
2095 InsertNode(N);
2096 }
2097
2098 return SDValue(CondCodeNodes[Cond], 0);
2099}
2100
2102 bool ConstantFold) {
2103 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2104 "APInt size does not match type size!");
2105
2106 if (MulImm == 0)
2107 return getConstant(0, DL, VT);
2108
2109 if (ConstantFold) {
2110 const MachineFunction &MF = getMachineFunction();
2111 const Function &F = MF.getFunction();
2112 ConstantRange CR = getVScaleRange(&F, 64);
2113 if (const APInt *C = CR.getSingleElement())
2114 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2115 }
2116
2117 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2118}
2119
2121 bool ConstantFold) {
2122 if (EC.isScalable())
2123 return getVScale(DL, VT,
2124 APInt(VT.getSizeInBits(), EC.getKnownMinValue()));
2125
2126 return getConstant(EC.getKnownMinValue(), DL, VT);
2127}
2128
2130 APInt One(ResVT.getScalarSizeInBits(), 1);
2131 return getStepVector(DL, ResVT, One);
2132}
2133
2135 const APInt &StepVal) {
2136 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2137 if (ResVT.isScalableVector())
2138 return getNode(
2139 ISD::STEP_VECTOR, DL, ResVT,
2140 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2141
2142 SmallVector<SDValue, 16> OpsStepConstants;
2143 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2144 OpsStepConstants.push_back(
2145 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2146 return getBuildVector(ResVT, DL, OpsStepConstants);
2147}
2148
2149/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2150/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2155
2157 SDValue N2, ArrayRef<int> Mask) {
2158 assert(VT.getVectorNumElements() == Mask.size() &&
2159 "Must have the same number of vector elements as mask elements!");
2160 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2161 "Invalid VECTOR_SHUFFLE");
2162
2163 // Canonicalize shuffle undef, undef -> undef
2164 if (N1.isUndef() && N2.isUndef())
2165 return getUNDEF(VT);
2166
2167 // Validate that all indices in Mask are within the range of the elements
2168 // input to the shuffle.
2169 int NElts = Mask.size();
2170 assert(llvm::all_of(Mask,
2171 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2172 "Index out of range");
2173
2174 // Copy the mask so we can do any needed cleanup.
2175 SmallVector<int, 8> MaskVec(Mask);
2176
2177 // Canonicalize shuffle v, v -> v, undef
2178 if (N1 == N2) {
2179 N2 = getUNDEF(VT);
2180 for (int i = 0; i != NElts; ++i)
2181 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2182 }
2183
2184 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2185 if (N1.isUndef())
2186 commuteShuffle(N1, N2, MaskVec);
2187
2188 if (TLI->hasVectorBlend()) {
2189 // If shuffling a splat, try to blend the splat instead. We do this here so
2190 // that even when this arises during lowering we don't have to re-handle it.
2191 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2192 BitVector UndefElements;
2193 SDValue Splat = BV->getSplatValue(&UndefElements);
2194 if (!Splat)
2195 return;
2196
2197 for (int i = 0; i < NElts; ++i) {
2198 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2199 continue;
2200
2201 // If this input comes from undef, mark it as such.
2202 if (UndefElements[MaskVec[i] - Offset]) {
2203 MaskVec[i] = -1;
2204 continue;
2205 }
2206
2207 // If we can blend a non-undef lane, use that instead.
2208 if (!UndefElements[i])
2209 MaskVec[i] = i + Offset;
2210 }
2211 };
2212 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2213 BlendSplat(N1BV, 0);
2214 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2215 BlendSplat(N2BV, NElts);
2216 }
2217
2218 // Canonicalize all index into lhs, -> shuffle lhs, undef
2219 // Canonicalize all index into rhs, -> shuffle rhs, undef
2220 bool AllLHS = true, AllRHS = true;
2221 bool N2Undef = N2.isUndef();
2222 for (int i = 0; i != NElts; ++i) {
2223 if (MaskVec[i] >= NElts) {
2224 if (N2Undef)
2225 MaskVec[i] = -1;
2226 else
2227 AllLHS = false;
2228 } else if (MaskVec[i] >= 0) {
2229 AllRHS = false;
2230 }
2231 }
2232 if (AllLHS && AllRHS)
2233 return getUNDEF(VT);
2234 if (AllLHS && !N2Undef)
2235 N2 = getUNDEF(VT);
2236 if (AllRHS) {
2237 N1 = getUNDEF(VT);
2238 commuteShuffle(N1, N2, MaskVec);
2239 }
2240 // Reset our undef status after accounting for the mask.
2241 N2Undef = N2.isUndef();
2242 // Re-check whether both sides ended up undef.
2243 if (N1.isUndef() && N2Undef)
2244 return getUNDEF(VT);
2245
2246 // If Identity shuffle return that node.
2247 bool Identity = true, AllSame = true;
2248 for (int i = 0; i != NElts; ++i) {
2249 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2250 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2251 }
2252 if (Identity && NElts)
2253 return N1;
2254
2255 // Shuffling a constant splat doesn't change the result.
2256 if (N2Undef) {
2257 SDValue V = N1;
2258
2259 // Look through any bitcasts. We check that these don't change the number
2260 // (and size) of elements and just changes their types.
2261 while (V.getOpcode() == ISD::BITCAST)
2262 V = V->getOperand(0);
2263
2264 // A splat should always show up as a build vector node.
2265 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2266 BitVector UndefElements;
2267 SDValue Splat = BV->getSplatValue(&UndefElements);
2268 // If this is a splat of an undef, shuffling it is also undef.
2269 if (Splat && Splat.isUndef())
2270 return getUNDEF(VT);
2271
2272 bool SameNumElts =
2273 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2274
2275 // We only have a splat which can skip shuffles if there is a splatted
2276 // value and no undef lanes rearranged by the shuffle.
2277 if (Splat && UndefElements.none()) {
2278 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2279 // number of elements match or the value splatted is a zero constant.
2280 if (SameNumElts || isNullConstant(Splat))
2281 return N1;
2282 }
2283
2284 // If the shuffle itself creates a splat, build the vector directly.
2285 if (AllSame && SameNumElts) {
2286 EVT BuildVT = BV->getValueType(0);
2287 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2288 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2289
2290 // We may have jumped through bitcasts, so the type of the
2291 // BUILD_VECTOR may not match the type of the shuffle.
2292 if (BuildVT != VT)
2293 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2294 return NewBV;
2295 }
2296 }
2297 }
2298
2299 SDVTList VTs = getVTList(VT);
2301 SDValue Ops[2] = { N1, N2 };
2303 for (int i = 0; i != NElts; ++i)
2304 ID.AddInteger(MaskVec[i]);
2305
2306 void* IP = nullptr;
2307 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2308 return SDValue(E, 0);
2309
2310 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2311 // SDNode doesn't have access to it. This memory will be "leaked" when
2312 // the node is deallocated, but recovered when the NodeAllocator is released.
2313 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2314 llvm::copy(MaskVec, MaskAlloc);
2315
2316 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2317 dl.getDebugLoc(), MaskAlloc);
2318 createOperands(N, Ops);
2319
2320 CSEMap.InsertNode(N, IP);
2321 InsertNode(N);
2322 SDValue V = SDValue(N, 0);
2323 NewSDValueDbgMsg(V, "Creating new node: ", this);
2324 return V;
2325}
2326
2328 EVT VT = SV.getValueType(0);
2329 SmallVector<int, 8> MaskVec(SV.getMask());
2331
2332 SDValue Op0 = SV.getOperand(0);
2333 SDValue Op1 = SV.getOperand(1);
2334 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2335}
2336
2338 SDVTList VTs = getVTList(VT);
2340 AddNodeIDNode(ID, ISD::Register, VTs, {});
2341 ID.AddInteger(Reg.id());
2342 void *IP = nullptr;
2343 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2344 return SDValue(E, 0);
2345
2346 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2347 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2348 CSEMap.InsertNode(N, IP);
2349 InsertNode(N);
2350 return SDValue(N, 0);
2351}
2352
2355 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2356 ID.AddPointer(RegMask);
2357 void *IP = nullptr;
2358 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2359 return SDValue(E, 0);
2360
2361 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2362 CSEMap.InsertNode(N, IP);
2363 InsertNode(N);
2364 return SDValue(N, 0);
2365}
2366
2368 MCSymbol *Label) {
2369 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2370}
2371
2372SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2373 SDValue Root, MCSymbol *Label) {
2375 SDValue Ops[] = { Root };
2376 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2377 ID.AddPointer(Label);
2378 void *IP = nullptr;
2379 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2380 return SDValue(E, 0);
2381
2382 auto *N =
2383 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2384 createOperands(N, Ops);
2385
2386 CSEMap.InsertNode(N, IP);
2387 InsertNode(N);
2388 return SDValue(N, 0);
2389}
2390
2392 int64_t Offset, bool isTarget,
2393 unsigned TargetFlags) {
2394 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2395 SDVTList VTs = getVTList(VT);
2396
2398 AddNodeIDNode(ID, Opc, VTs, {});
2399 ID.AddPointer(BA);
2400 ID.AddInteger(Offset);
2401 ID.AddInteger(TargetFlags);
2402 void *IP = nullptr;
2403 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2404 return SDValue(E, 0);
2405
2406 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2407 CSEMap.InsertNode(N, IP);
2408 InsertNode(N);
2409 return SDValue(N, 0);
2410}
2411
2414 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2415 ID.AddPointer(V);
2416
2417 void *IP = nullptr;
2418 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2419 return SDValue(E, 0);
2420
2421 auto *N = newSDNode<SrcValueSDNode>(V);
2422 CSEMap.InsertNode(N, IP);
2423 InsertNode(N);
2424 return SDValue(N, 0);
2425}
2426
2429 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2430 ID.AddPointer(MD);
2431
2432 void *IP = nullptr;
2433 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2434 return SDValue(E, 0);
2435
2436 auto *N = newSDNode<MDNodeSDNode>(MD);
2437 CSEMap.InsertNode(N, IP);
2438 InsertNode(N);
2439 return SDValue(N, 0);
2440}
2441
2443 if (VT == V.getValueType())
2444 return V;
2445
2446 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2447}
2448
2450 unsigned SrcAS, unsigned DestAS) {
2451 SDVTList VTs = getVTList(VT);
2452 SDValue Ops[] = {Ptr};
2454 AddNodeIDNode(ID, ISD::ADDRSPACECAST, VTs, Ops);
2455 ID.AddInteger(SrcAS);
2456 ID.AddInteger(DestAS);
2457
2458 void *IP = nullptr;
2459 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2460 return SDValue(E, 0);
2461
2462 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2463 VTs, SrcAS, DestAS);
2464 createOperands(N, Ops);
2465
2466 CSEMap.InsertNode(N, IP);
2467 InsertNode(N);
2468 return SDValue(N, 0);
2469}
2470
2472 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2473}
2474
2475/// getShiftAmountOperand - Return the specified value casted to
2476/// the target's desired shift amount type.
2478 EVT OpTy = Op.getValueType();
2479 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2480 if (OpTy == ShTy || OpTy.isVector()) return Op;
2481
2482 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2483}
2484
2486 SDLoc dl(Node);
2488 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2489 EVT VT = Node->getValueType(0);
2490 SDValue Tmp1 = Node->getOperand(0);
2491 SDValue Tmp2 = Node->getOperand(1);
2492 const MaybeAlign MA(Node->getConstantOperandVal(3));
2493
2494 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2495 Tmp2, MachinePointerInfo(V));
2496 SDValue VAList = VAListLoad;
2497
2498 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2499 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2500 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2501
2502 VAList = getNode(
2503 ISD::AND, dl, VAList.getValueType(), VAList,
2504 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2505 }
2506
2507 // Increment the pointer, VAList, to the next vaarg
2508 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2509 getConstant(getDataLayout().getTypeAllocSize(
2510 VT.getTypeForEVT(*getContext())),
2511 dl, VAList.getValueType()));
2512 // Store the incremented VAList to the legalized pointer
2513 Tmp1 =
2514 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2515 // Load the actual argument out of the pointer VAList
2516 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2517}
2518
2520 SDLoc dl(Node);
2522 // This defaults to loading a pointer from the input and storing it to the
2523 // output, returning the chain.
2524 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2525 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2526 SDValue Tmp1 =
2527 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2528 Node->getOperand(2), MachinePointerInfo(VS));
2529 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2530 MachinePointerInfo(VD));
2531}
2532
2534 const DataLayout &DL = getDataLayout();
2535 Type *Ty = VT.getTypeForEVT(*getContext());
2536 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2537
2538 if (TLI->isTypeLegal(VT) || !VT.isVector())
2539 return RedAlign;
2540
2541 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2542 const Align StackAlign = TFI->getStackAlign();
2543
2544 // See if we can choose a smaller ABI alignment in cases where it's an
2545 // illegal vector type that will get broken down.
2546 if (RedAlign > StackAlign) {
2547 EVT IntermediateVT;
2548 MVT RegisterVT;
2549 unsigned NumIntermediates;
2550 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2551 NumIntermediates, RegisterVT);
2552 Ty = IntermediateVT.getTypeForEVT(*getContext());
2553 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2554 if (RedAlign2 < RedAlign)
2555 RedAlign = RedAlign2;
2556
2557 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2558 // If the stack is not realignable, the alignment should be limited to the
2559 // StackAlignment
2560 RedAlign = std::min(RedAlign, StackAlign);
2561 }
2562
2563 return RedAlign;
2564}
2565
2567 MachineFrameInfo &MFI = MF->getFrameInfo();
2568 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2569 int StackID = 0;
2570 if (Bytes.isScalable())
2571 StackID = TFI->getStackIDForScalableVectors();
2572 // The stack id gives an indication of whether the object is scalable or
2573 // not, so it's safe to pass in the minimum size here.
2574 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2575 false, nullptr, StackID);
2576 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2577}
2578
2580 Type *Ty = VT.getTypeForEVT(*getContext());
2581 Align StackAlign =
2582 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2583 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2584}
2585
2587 TypeSize VT1Size = VT1.getStoreSize();
2588 TypeSize VT2Size = VT2.getStoreSize();
2589 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2590 "Don't know how to choose the maximum size when creating a stack "
2591 "temporary");
2592 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2593 ? VT1Size
2594 : VT2Size;
2595
2596 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2597 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2598 const DataLayout &DL = getDataLayout();
2599 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2600 return CreateStackTemporary(Bytes, Align);
2601}
2602
2604 ISD::CondCode Cond, const SDLoc &dl) {
2605 EVT OpVT = N1.getValueType();
2606
2607 auto GetUndefBooleanConstant = [&]() {
2608 if (VT.getScalarType() == MVT::i1 ||
2609 TLI->getBooleanContents(OpVT) ==
2611 return getUNDEF(VT);
2612 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2613 // so we cannot use getUNDEF(). Return zero instead.
2614 return getConstant(0, dl, VT);
2615 };
2616
2617 // These setcc operations always fold.
2618 switch (Cond) {
2619 default: break;
2620 case ISD::SETFALSE:
2621 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2622 case ISD::SETTRUE:
2623 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2624
2625 case ISD::SETOEQ:
2626 case ISD::SETOGT:
2627 case ISD::SETOGE:
2628 case ISD::SETOLT:
2629 case ISD::SETOLE:
2630 case ISD::SETONE:
2631 case ISD::SETO:
2632 case ISD::SETUO:
2633 case ISD::SETUEQ:
2634 case ISD::SETUNE:
2635 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2636 break;
2637 }
2638
2639 if (OpVT.isInteger()) {
2640 // For EQ and NE, we can always pick a value for the undef to make the
2641 // predicate pass or fail, so we can return undef.
2642 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2643 // icmp eq/ne X, undef -> undef.
2644 if ((N1.isUndef() || N2.isUndef()) &&
2645 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2646 return GetUndefBooleanConstant();
2647
2648 // If both operands are undef, we can return undef for int comparison.
2649 // icmp undef, undef -> undef.
2650 if (N1.isUndef() && N2.isUndef())
2651 return GetUndefBooleanConstant();
2652
2653 // icmp X, X -> true/false
2654 // icmp X, undef -> true/false because undef could be X.
2655 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2656 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2657 }
2658
2660 const APInt &C2 = N2C->getAPIntValue();
2662 const APInt &C1 = N1C->getAPIntValue();
2663
2665 dl, VT, OpVT);
2666 }
2667 }
2668
2669 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2670 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2671
2672 if (N1CFP && N2CFP) {
2673 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2674 switch (Cond) {
2675 default: break;
2676 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2677 return GetUndefBooleanConstant();
2678 [[fallthrough]];
2679 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2680 OpVT);
2681 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2682 return GetUndefBooleanConstant();
2683 [[fallthrough]];
2685 R==APFloat::cmpLessThan, dl, VT,
2686 OpVT);
2687 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2688 return GetUndefBooleanConstant();
2689 [[fallthrough]];
2690 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2691 OpVT);
2692 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2693 return GetUndefBooleanConstant();
2694 [[fallthrough]];
2696 VT, OpVT);
2697 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2698 return GetUndefBooleanConstant();
2699 [[fallthrough]];
2701 R==APFloat::cmpEqual, dl, VT,
2702 OpVT);
2703 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2704 return GetUndefBooleanConstant();
2705 [[fallthrough]];
2707 R==APFloat::cmpEqual, dl, VT, OpVT);
2708 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2709 OpVT);
2710 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2711 OpVT);
2713 R==APFloat::cmpEqual, dl, VT,
2714 OpVT);
2715 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2716 OpVT);
2718 R==APFloat::cmpLessThan, dl, VT,
2719 OpVT);
2721 R==APFloat::cmpUnordered, dl, VT,
2722 OpVT);
2724 VT, OpVT);
2725 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2726 OpVT);
2727 }
2728 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2729 // Ensure that the constant occurs on the RHS.
2731 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2732 return SDValue();
2733 return getSetCC(dl, VT, N2, N1, SwappedCond);
2734 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2735 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2736 // If an operand is known to be a nan (or undef that could be a nan), we can
2737 // fold it.
2738 // Choosing NaN for the undef will always make unordered comparison succeed
2739 // and ordered comparison fails.
2740 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2741 switch (ISD::getUnorderedFlavor(Cond)) {
2742 default:
2743 llvm_unreachable("Unknown flavor!");
2744 case 0: // Known false.
2745 return getBoolConstant(false, dl, VT, OpVT);
2746 case 1: // Known true.
2747 return getBoolConstant(true, dl, VT, OpVT);
2748 case 2: // Undefined.
2749 return GetUndefBooleanConstant();
2750 }
2751 }
2752
2753 // Could not fold it.
2754 return SDValue();
2755}
2756
2757/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2758/// use this predicate to simplify operations downstream.
2760 unsigned BitWidth = Op.getScalarValueSizeInBits();
2762}
2763
2765 if (Depth >= MaxRecursionDepth)
2766 return false; // Limit search depth.
2767
2768 unsigned Opc = Op.getOpcode();
2769 switch (Opc) {
2770 case ISD::FABS:
2771 return true;
2772 case ISD::AssertNoFPClass: {
2773 FPClassTest NoFPClass =
2774 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2775
2776 const FPClassTest TestMask = fcNan | fcNegative;
2777 return (NoFPClass & TestMask) == TestMask;
2778 }
2779 case ISD::ARITH_FENCE:
2780 return SignBitIsZeroFP(Op, Depth + 1);
2781 case ISD::FEXP:
2782 case ISD::FEXP2:
2783 case ISD::FEXP10:
2784 return Op->getFlags().hasNoNaNs();
2785 default:
2786 return false;
2787 }
2788
2789 llvm_unreachable("covered opcode switch");
2790}
2791
2792/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2793/// this predicate to simplify operations downstream. Mask is known to be zero
2794/// for bits that V cannot have.
2796 unsigned Depth) const {
2797 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2798}
2799
2800/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2801/// DemandedElts. We use this predicate to simplify operations downstream.
2802/// Mask is known to be zero for bits that V cannot have.
2804 const APInt &DemandedElts,
2805 unsigned Depth) const {
2806 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2807}
2808
2809/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2810/// DemandedElts. We use this predicate to simplify operations downstream.
2812 unsigned Depth /* = 0 */) const {
2813 return computeKnownBits(V, DemandedElts, Depth).isZero();
2814}
2815
2816/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2818 unsigned Depth) const {
2819 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2820}
2821
2823 const APInt &DemandedElts,
2824 unsigned Depth) const {
2825 EVT VT = Op.getValueType();
2826 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2827
2828 unsigned NumElts = VT.getVectorNumElements();
2829 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2830
2831 APInt KnownZeroElements = APInt::getZero(NumElts);
2832 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2833 if (!DemandedElts[EltIdx])
2834 continue; // Don't query elements that are not demanded.
2835 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2836 if (MaskedVectorIsZero(Op, Mask, Depth))
2837 KnownZeroElements.setBit(EltIdx);
2838 }
2839 return KnownZeroElements;
2840}
2841
2842/// isSplatValue - Return true if the vector V has the same value
2843/// across all DemandedElts. For scalable vectors, we don't know the
2844/// number of lanes at compile time. Instead, we use a 1 bit APInt
2845/// to represent a conservative value for all lanes; that is, that
2846/// one bit value is implicitly splatted across all lanes.
2847bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2848 APInt &UndefElts, unsigned Depth) const {
2849 unsigned Opcode = V.getOpcode();
2850 EVT VT = V.getValueType();
2851 assert(VT.isVector() && "Vector type expected");
2852 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2853 "scalable demanded bits are ignored");
2854
2855 if (!DemandedElts)
2856 return false; // No demanded elts, better to assume we don't know anything.
2857
2858 if (Depth >= MaxRecursionDepth)
2859 return false; // Limit search depth.
2860
2861 // Deal with some common cases here that work for both fixed and scalable
2862 // vector types.
2863 switch (Opcode) {
2864 case ISD::SPLAT_VECTOR:
2865 UndefElts = V.getOperand(0).isUndef()
2866 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2867 : APInt(DemandedElts.getBitWidth(), 0);
2868 return true;
2869 case ISD::ADD:
2870 case ISD::SUB:
2871 case ISD::AND:
2872 case ISD::XOR:
2873 case ISD::OR: {
2874 APInt UndefLHS, UndefRHS;
2875 SDValue LHS = V.getOperand(0);
2876 SDValue RHS = V.getOperand(1);
2877 // Only recognize splats with the same demanded undef elements for both
2878 // operands, otherwise we might fail to handle binop-specific undef
2879 // handling.
2880 // e.g. (and undef, 0) -> 0 etc.
2881 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2882 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2883 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2884 UndefElts = UndefLHS | UndefRHS;
2885 return true;
2886 }
2887 return false;
2888 }
2889 case ISD::ABS:
2890 case ISD::TRUNCATE:
2891 case ISD::SIGN_EXTEND:
2892 case ISD::ZERO_EXTEND:
2893 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2894 default:
2895 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2896 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2897 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2898 Depth);
2899 break;
2900 }
2901
2902 // We don't support other cases than those above for scalable vectors at
2903 // the moment.
2904 if (VT.isScalableVector())
2905 return false;
2906
2907 unsigned NumElts = VT.getVectorNumElements();
2908 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2909 UndefElts = APInt::getZero(NumElts);
2910
2911 switch (Opcode) {
2912 case ISD::BUILD_VECTOR: {
2913 SDValue Scl;
2914 for (unsigned i = 0; i != NumElts; ++i) {
2915 SDValue Op = V.getOperand(i);
2916 if (Op.isUndef()) {
2917 UndefElts.setBit(i);
2918 continue;
2919 }
2920 if (!DemandedElts[i])
2921 continue;
2922 if (Scl && Scl != Op)
2923 return false;
2924 Scl = Op;
2925 }
2926 return true;
2927 }
2928 case ISD::VECTOR_SHUFFLE: {
2929 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2930 APInt DemandedLHS = APInt::getZero(NumElts);
2931 APInt DemandedRHS = APInt::getZero(NumElts);
2932 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2933 for (int i = 0; i != (int)NumElts; ++i) {
2934 int M = Mask[i];
2935 if (M < 0) {
2936 UndefElts.setBit(i);
2937 continue;
2938 }
2939 if (!DemandedElts[i])
2940 continue;
2941 if (M < (int)NumElts)
2942 DemandedLHS.setBit(M);
2943 else
2944 DemandedRHS.setBit(M - NumElts);
2945 }
2946
2947 // If we aren't demanding either op, assume there's no splat.
2948 // If we are demanding both ops, assume there's no splat.
2949 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2950 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2951 return false;
2952
2953 // See if the demanded elts of the source op is a splat or we only demand
2954 // one element, which should always be a splat.
2955 // TODO: Handle source ops splats with undefs.
2956 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2957 APInt SrcUndefs;
2958 return (SrcElts.popcount() == 1) ||
2959 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2960 (SrcElts & SrcUndefs).isZero());
2961 };
2962 if (!DemandedLHS.isZero())
2963 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2964 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2965 }
2967 // Offset the demanded elts by the subvector index.
2968 SDValue Src = V.getOperand(0);
2969 // We don't support scalable vectors at the moment.
2970 if (Src.getValueType().isScalableVector())
2971 return false;
2972 uint64_t Idx = V.getConstantOperandVal(1);
2973 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2974 APInt UndefSrcElts;
2975 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
2976 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2977 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
2978 return true;
2979 }
2980 break;
2981 }
2985 // Widen the demanded elts by the src element count.
2986 SDValue Src = V.getOperand(0);
2987 // We don't support scalable vectors at the moment.
2988 if (Src.getValueType().isScalableVector())
2989 return false;
2990 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
2991 APInt UndefSrcElts;
2992 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
2993 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
2994 UndefElts = UndefSrcElts.trunc(NumElts);
2995 return true;
2996 }
2997 break;
2998 }
2999 case ISD::BITCAST: {
3000 SDValue Src = V.getOperand(0);
3001 EVT SrcVT = Src.getValueType();
3002 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3003 unsigned BitWidth = VT.getScalarSizeInBits();
3004
3005 // Ignore bitcasts from unsupported types.
3006 // TODO: Add fp support?
3007 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3008 break;
3009
3010 // Bitcast 'small element' vector to 'large element' vector.
3011 if ((BitWidth % SrcBitWidth) == 0) {
3012 // See if each sub element is a splat.
3013 unsigned Scale = BitWidth / SrcBitWidth;
3014 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3015 APInt ScaledDemandedElts =
3016 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3017 for (unsigned I = 0; I != Scale; ++I) {
3018 APInt SubUndefElts;
3019 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3020 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3021 SubDemandedElts &= ScaledDemandedElts;
3022 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3023 return false;
3024 // TODO: Add support for merging sub undef elements.
3025 if (!SubUndefElts.isZero())
3026 return false;
3027 }
3028 return true;
3029 }
3030 break;
3031 }
3032 }
3033
3034 return false;
3035}
3036
3037/// Helper wrapper to main isSplatValue function.
3038bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3039 EVT VT = V.getValueType();
3040 assert(VT.isVector() && "Vector type expected");
3041
3042 APInt UndefElts;
3043 // Since the number of lanes in a scalable vector is unknown at compile time,
3044 // we track one bit which is implicitly broadcast to all lanes. This means
3045 // that all lanes in a scalable vector are considered demanded.
3046 APInt DemandedElts
3048 return isSplatValue(V, DemandedElts, UndefElts) &&
3049 (AllowUndefs || !UndefElts);
3050}
3051
3054
3055 EVT VT = V.getValueType();
3056 unsigned Opcode = V.getOpcode();
3057 switch (Opcode) {
3058 default: {
3059 APInt UndefElts;
3060 // Since the number of lanes in a scalable vector is unknown at compile time,
3061 // we track one bit which is implicitly broadcast to all lanes. This means
3062 // that all lanes in a scalable vector are considered demanded.
3063 APInt DemandedElts
3065
3066 if (isSplatValue(V, DemandedElts, UndefElts)) {
3067 if (VT.isScalableVector()) {
3068 // DemandedElts and UndefElts are ignored for scalable vectors, since
3069 // the only supported cases are SPLAT_VECTOR nodes.
3070 SplatIdx = 0;
3071 } else {
3072 // Handle case where all demanded elements are UNDEF.
3073 if (DemandedElts.isSubsetOf(UndefElts)) {
3074 SplatIdx = 0;
3075 return getUNDEF(VT);
3076 }
3077 SplatIdx = (UndefElts & DemandedElts).countr_one();
3078 }
3079 return V;
3080 }
3081 break;
3082 }
3083 case ISD::SPLAT_VECTOR:
3084 SplatIdx = 0;
3085 return V;
3086 case ISD::VECTOR_SHUFFLE: {
3087 assert(!VT.isScalableVector());
3088 // Check if this is a shuffle node doing a splat.
3089 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3090 // getTargetVShiftNode currently struggles without the splat source.
3091 auto *SVN = cast<ShuffleVectorSDNode>(V);
3092 if (!SVN->isSplat())
3093 break;
3094 int Idx = SVN->getSplatIndex();
3095 int NumElts = V.getValueType().getVectorNumElements();
3096 SplatIdx = Idx % NumElts;
3097 return V.getOperand(Idx / NumElts);
3098 }
3099 }
3100
3101 return SDValue();
3102}
3103
3105 int SplatIdx;
3106 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3107 EVT SVT = SrcVector.getValueType().getScalarType();
3108 EVT LegalSVT = SVT;
3109 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3110 if (!SVT.isInteger())
3111 return SDValue();
3112 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3113 if (LegalSVT.bitsLT(SVT))
3114 return SDValue();
3115 }
3116 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3117 }
3118 return SDValue();
3119}
3120
3121std::optional<ConstantRange>
3123 unsigned Depth) const {
3124 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3125 V.getOpcode() == ISD::SRA) &&
3126 "Unknown shift node");
3127 // Shifting more than the bitwidth is not valid.
3128 unsigned BitWidth = V.getScalarValueSizeInBits();
3129
3130 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3131 const APInt &ShAmt = Cst->getAPIntValue();
3132 if (ShAmt.uge(BitWidth))
3133 return std::nullopt;
3134 return ConstantRange(ShAmt);
3135 }
3136
3137 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3138 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3139 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3140 if (!DemandedElts[i])
3141 continue;
3142 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3143 if (!SA) {
3144 MinAmt = MaxAmt = nullptr;
3145 break;
3146 }
3147 const APInt &ShAmt = SA->getAPIntValue();
3148 if (ShAmt.uge(BitWidth))
3149 return std::nullopt;
3150 if (!MinAmt || MinAmt->ugt(ShAmt))
3151 MinAmt = &ShAmt;
3152 if (!MaxAmt || MaxAmt->ult(ShAmt))
3153 MaxAmt = &ShAmt;
3154 }
3155 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3156 "Failed to find matching min/max shift amounts");
3157 if (MinAmt && MaxAmt)
3158 return ConstantRange(*MinAmt, *MaxAmt + 1);
3159 }
3160
3161 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3162 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3163 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3164 if (KnownAmt.getMaxValue().ult(BitWidth))
3165 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3166
3167 return std::nullopt;
3168}
3169
3170std::optional<unsigned>
3172 unsigned Depth) const {
3173 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3174 V.getOpcode() == ISD::SRA) &&
3175 "Unknown shift node");
3176 if (std::optional<ConstantRange> AmtRange =
3177 getValidShiftAmountRange(V, DemandedElts, Depth))
3178 if (const APInt *ShAmt = AmtRange->getSingleElement())
3179 return ShAmt->getZExtValue();
3180 return std::nullopt;
3181}
3182
3183std::optional<unsigned>
3185 EVT VT = V.getValueType();
3186 APInt DemandedElts = VT.isFixedLengthVector()
3188 : APInt(1, 1);
3189 return getValidShiftAmount(V, DemandedElts, Depth);
3190}
3191
3192std::optional<unsigned>
3194 unsigned Depth) const {
3195 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3196 V.getOpcode() == ISD::SRA) &&
3197 "Unknown shift node");
3198 if (std::optional<ConstantRange> AmtRange =
3199 getValidShiftAmountRange(V, DemandedElts, Depth))
3200 return AmtRange->getUnsignedMin().getZExtValue();
3201 return std::nullopt;
3202}
3203
3204std::optional<unsigned>
3206 EVT VT = V.getValueType();
3207 APInt DemandedElts = VT.isFixedLengthVector()
3209 : APInt(1, 1);
3210 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3211}
3212
3213std::optional<unsigned>
3215 unsigned Depth) const {
3216 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3217 V.getOpcode() == ISD::SRA) &&
3218 "Unknown shift node");
3219 if (std::optional<ConstantRange> AmtRange =
3220 getValidShiftAmountRange(V, DemandedElts, Depth))
3221 return AmtRange->getUnsignedMax().getZExtValue();
3222 return std::nullopt;
3223}
3224
3225std::optional<unsigned>
3227 EVT VT = V.getValueType();
3228 APInt DemandedElts = VT.isFixedLengthVector()
3230 : APInt(1, 1);
3231 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3232}
3233
3234/// Determine which bits of Op are known to be either zero or one and return
3235/// them in Known. For vectors, the known bits are those that are shared by
3236/// every vector element.
3238 EVT VT = Op.getValueType();
3239
3240 // Since the number of lanes in a scalable vector is unknown at compile time,
3241 // we track one bit which is implicitly broadcast to all lanes. This means
3242 // that all lanes in a scalable vector are considered demanded.
3243 APInt DemandedElts = VT.isFixedLengthVector()
3245 : APInt(1, 1);
3246 return computeKnownBits(Op, DemandedElts, Depth);
3247}
3248
3249/// Determine which bits of Op are known to be either zero or one and return
3250/// them in Known. The DemandedElts argument allows us to only collect the known
3251/// bits that are shared by the requested vector elements.
3253 unsigned Depth) const {
3254 unsigned BitWidth = Op.getScalarValueSizeInBits();
3255
3256 KnownBits Known(BitWidth); // Don't know anything.
3257
3258 if (auto OptAPInt = Op->bitcastToAPInt()) {
3259 // We know all of the bits for a constant!
3260 return KnownBits::makeConstant(*std::move(OptAPInt));
3261 }
3262
3263 if (Depth >= MaxRecursionDepth)
3264 return Known; // Limit search depth.
3265
3266 KnownBits Known2;
3267 unsigned NumElts = DemandedElts.getBitWidth();
3268 assert((!Op.getValueType().isFixedLengthVector() ||
3269 NumElts == Op.getValueType().getVectorNumElements()) &&
3270 "Unexpected vector size");
3271
3272 if (!DemandedElts)
3273 return Known; // No demanded elts, better to assume we don't know anything.
3274
3275 unsigned Opcode = Op.getOpcode();
3276 switch (Opcode) {
3277 case ISD::MERGE_VALUES:
3278 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3279 Depth + 1);
3280 case ISD::SPLAT_VECTOR: {
3281 SDValue SrcOp = Op.getOperand(0);
3282 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3283 "Expected SPLAT_VECTOR implicit truncation");
3284 // Implicitly truncate the bits to match the official semantics of
3285 // SPLAT_VECTOR.
3286 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3287 break;
3288 }
3290 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3291 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3292 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3293 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3294 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3295 }
3296 break;
3297 }
3298 case ISD::STEP_VECTOR: {
3299 const APInt &Step = Op.getConstantOperandAPInt(0);
3300
3301 if (Step.isPowerOf2())
3302 Known.Zero.setLowBits(Step.logBase2());
3303
3305
3306 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3307 break;
3308 const APInt MinNumElts =
3309 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3310
3311 bool Overflow;
3312 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3314 .umul_ov(MinNumElts, Overflow);
3315 if (Overflow)
3316 break;
3317
3318 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3319 if (Overflow)
3320 break;
3321
3322 Known.Zero.setHighBits(MaxValue.countl_zero());
3323 break;
3324 }
3325 case ISD::BUILD_VECTOR:
3326 assert(!Op.getValueType().isScalableVector());
3327 // Collect the known bits that are shared by every demanded vector element.
3328 Known.setAllConflict();
3329 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3330 if (!DemandedElts[i])
3331 continue;
3332
3333 SDValue SrcOp = Op.getOperand(i);
3334 Known2 = computeKnownBits(SrcOp, Depth + 1);
3335
3336 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3337 if (SrcOp.getValueSizeInBits() != BitWidth) {
3338 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3339 "Expected BUILD_VECTOR implicit truncation");
3340 Known2 = Known2.trunc(BitWidth);
3341 }
3342
3343 // Known bits are the values that are shared by every demanded element.
3344 Known = Known.intersectWith(Known2);
3345
3346 // If we don't know any bits, early out.
3347 if (Known.isUnknown())
3348 break;
3349 }
3350 break;
3351 case ISD::VECTOR_COMPRESS: {
3352 SDValue Vec = Op.getOperand(0);
3353 SDValue PassThru = Op.getOperand(2);
3354 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3355 // If we don't know any bits, early out.
3356 if (Known.isUnknown())
3357 break;
3358 Known2 = computeKnownBits(Vec, Depth + 1);
3359 Known = Known.intersectWith(Known2);
3360 break;
3361 }
3362 case ISD::VECTOR_SHUFFLE: {
3363 assert(!Op.getValueType().isScalableVector());
3364 // Collect the known bits that are shared by every vector element referenced
3365 // by the shuffle.
3366 APInt DemandedLHS, DemandedRHS;
3368 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3369 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3370 DemandedLHS, DemandedRHS))
3371 break;
3372
3373 // Known bits are the values that are shared by every demanded element.
3374 Known.setAllConflict();
3375 if (!!DemandedLHS) {
3376 SDValue LHS = Op.getOperand(0);
3377 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3378 Known = Known.intersectWith(Known2);
3379 }
3380 // If we don't know any bits, early out.
3381 if (Known.isUnknown())
3382 break;
3383 if (!!DemandedRHS) {
3384 SDValue RHS = Op.getOperand(1);
3385 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3386 Known = Known.intersectWith(Known2);
3387 }
3388 break;
3389 }
3390 case ISD::VSCALE: {
3392 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3393 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3394 break;
3395 }
3396 case ISD::CONCAT_VECTORS: {
3397 if (Op.getValueType().isScalableVector())
3398 break;
3399 // Split DemandedElts and test each of the demanded subvectors.
3400 Known.setAllConflict();
3401 EVT SubVectorVT = Op.getOperand(0).getValueType();
3402 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3403 unsigned NumSubVectors = Op.getNumOperands();
3404 for (unsigned i = 0; i != NumSubVectors; ++i) {
3405 APInt DemandedSub =
3406 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3407 if (!!DemandedSub) {
3408 SDValue Sub = Op.getOperand(i);
3409 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3410 Known = Known.intersectWith(Known2);
3411 }
3412 // If we don't know any bits, early out.
3413 if (Known.isUnknown())
3414 break;
3415 }
3416 break;
3417 }
3418 case ISD::INSERT_SUBVECTOR: {
3419 if (Op.getValueType().isScalableVector())
3420 break;
3421 // Demand any elements from the subvector and the remainder from the src its
3422 // inserted into.
3423 SDValue Src = Op.getOperand(0);
3424 SDValue Sub = Op.getOperand(1);
3425 uint64_t Idx = Op.getConstantOperandVal(2);
3426 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3427 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3428 APInt DemandedSrcElts = DemandedElts;
3429 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3430
3431 Known.setAllConflict();
3432 if (!!DemandedSubElts) {
3433 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3434 if (Known.isUnknown())
3435 break; // early-out.
3436 }
3437 if (!!DemandedSrcElts) {
3438 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3439 Known = Known.intersectWith(Known2);
3440 }
3441 break;
3442 }
3444 // Offset the demanded elts by the subvector index.
3445 SDValue Src = Op.getOperand(0);
3446 // Bail until we can represent demanded elements for scalable vectors.
3447 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3448 break;
3449 uint64_t Idx = Op.getConstantOperandVal(1);
3450 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3451 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3452 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3453 break;
3454 }
3455 case ISD::SCALAR_TO_VECTOR: {
3456 if (Op.getValueType().isScalableVector())
3457 break;
3458 // We know about scalar_to_vector as much as we know about it source,
3459 // which becomes the first element of otherwise unknown vector.
3460 if (DemandedElts != 1)
3461 break;
3462
3463 SDValue N0 = Op.getOperand(0);
3464 Known = computeKnownBits(N0, Depth + 1);
3465 if (N0.getValueSizeInBits() != BitWidth)
3466 Known = Known.trunc(BitWidth);
3467
3468 break;
3469 }
3470 case ISD::BITCAST: {
3471 if (Op.getValueType().isScalableVector())
3472 break;
3473
3474 SDValue N0 = Op.getOperand(0);
3475 EVT SubVT = N0.getValueType();
3476 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3477
3478 // Ignore bitcasts from unsupported types.
3479 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3480 break;
3481
3482 // Fast handling of 'identity' bitcasts.
3483 if (BitWidth == SubBitWidth) {
3484 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3485 break;
3486 }
3487
3488 bool IsLE = getDataLayout().isLittleEndian();
3489
3490 // Bitcast 'small element' vector to 'large element' scalar/vector.
3491 if ((BitWidth % SubBitWidth) == 0) {
3492 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3493
3494 // Collect known bits for the (larger) output by collecting the known
3495 // bits from each set of sub elements and shift these into place.
3496 // We need to separately call computeKnownBits for each set of
3497 // sub elements as the knownbits for each is likely to be different.
3498 unsigned SubScale = BitWidth / SubBitWidth;
3499 APInt SubDemandedElts(NumElts * SubScale, 0);
3500 for (unsigned i = 0; i != NumElts; ++i)
3501 if (DemandedElts[i])
3502 SubDemandedElts.setBit(i * SubScale);
3503
3504 for (unsigned i = 0; i != SubScale; ++i) {
3505 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3506 Depth + 1);
3507 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3508 Known.insertBits(Known2, SubBitWidth * Shifts);
3509 }
3510 }
3511
3512 // Bitcast 'large element' scalar/vector to 'small element' vector.
3513 if ((SubBitWidth % BitWidth) == 0) {
3514 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3515
3516 // Collect known bits for the (smaller) output by collecting the known
3517 // bits from the overlapping larger input elements and extracting the
3518 // sub sections we actually care about.
3519 unsigned SubScale = SubBitWidth / BitWidth;
3520 APInt SubDemandedElts =
3521 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3522 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3523
3524 Known.setAllConflict();
3525 for (unsigned i = 0; i != NumElts; ++i)
3526 if (DemandedElts[i]) {
3527 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3528 unsigned Offset = (Shifts % SubScale) * BitWidth;
3529 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3530 // If we don't know any bits, early out.
3531 if (Known.isUnknown())
3532 break;
3533 }
3534 }
3535 break;
3536 }
3537 case ISD::AND:
3538 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3539 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3540
3541 Known &= Known2;
3542 break;
3543 case ISD::OR:
3544 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3545 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3546
3547 Known |= Known2;
3548 break;
3549 case ISD::XOR:
3550 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3551 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3552
3553 Known ^= Known2;
3554 break;
3555 case ISD::MUL: {
3556 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3557 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3558 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3559 // TODO: SelfMultiply can be poison, but not undef.
3560 if (SelfMultiply)
3561 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3562 Op.getOperand(0), DemandedElts, false, Depth + 1);
3563 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3564
3565 // If the multiplication is known not to overflow, the product of a number
3566 // with itself is non-negative. Only do this if we didn't already computed
3567 // the opposite value for the sign bit.
3568 if (Op->getFlags().hasNoSignedWrap() &&
3569 Op.getOperand(0) == Op.getOperand(1) &&
3570 !Known.isNegative())
3571 Known.makeNonNegative();
3572 break;
3573 }
3574 case ISD::MULHU: {
3575 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3576 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3577 Known = KnownBits::mulhu(Known, Known2);
3578 break;
3579 }
3580 case ISD::MULHS: {
3581 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3582 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3583 Known = KnownBits::mulhs(Known, Known2);
3584 break;
3585 }
3586 case ISD::ABDU: {
3587 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3588 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3589 Known = KnownBits::abdu(Known, Known2);
3590 break;
3591 }
3592 case ISD::ABDS: {
3593 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3594 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3595 Known = KnownBits::abds(Known, Known2);
3596 unsigned SignBits1 =
3597 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3598 if (SignBits1 == 1)
3599 break;
3600 unsigned SignBits0 =
3601 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3602 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3603 break;
3604 }
3605 case ISD::UMUL_LOHI: {
3606 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3607 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3608 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3609 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3610 if (Op.getResNo() == 0)
3611 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3612 else
3613 Known = KnownBits::mulhu(Known, Known2);
3614 break;
3615 }
3616 case ISD::SMUL_LOHI: {
3617 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3618 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3619 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3620 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3621 if (Op.getResNo() == 0)
3622 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3623 else
3624 Known = KnownBits::mulhs(Known, Known2);
3625 break;
3626 }
3627 case ISD::AVGFLOORU: {
3628 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3629 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3630 Known = KnownBits::avgFloorU(Known, Known2);
3631 break;
3632 }
3633 case ISD::AVGCEILU: {
3634 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3635 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3636 Known = KnownBits::avgCeilU(Known, Known2);
3637 break;
3638 }
3639 case ISD::AVGFLOORS: {
3640 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3641 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3642 Known = KnownBits::avgFloorS(Known, Known2);
3643 break;
3644 }
3645 case ISD::AVGCEILS: {
3646 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3647 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3648 Known = KnownBits::avgCeilS(Known, Known2);
3649 break;
3650 }
3651 case ISD::SELECT:
3652 case ISD::VSELECT:
3653 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3654 // If we don't know any bits, early out.
3655 if (Known.isUnknown())
3656 break;
3657 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3658
3659 // Only known if known in both the LHS and RHS.
3660 Known = Known.intersectWith(Known2);
3661 break;
3662 case ISD::SELECT_CC:
3663 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3664 // If we don't know any bits, early out.
3665 if (Known.isUnknown())
3666 break;
3667 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3668
3669 // Only known if known in both the LHS and RHS.
3670 Known = Known.intersectWith(Known2);
3671 break;
3672 case ISD::SMULO:
3673 case ISD::UMULO:
3674 if (Op.getResNo() != 1)
3675 break;
3676 // The boolean result conforms to getBooleanContents.
3677 // If we know the result of a setcc has the top bits zero, use this info.
3678 // We know that we have an integer-based boolean since these operations
3679 // are only available for integer.
3680 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3682 BitWidth > 1)
3683 Known.Zero.setBitsFrom(1);
3684 break;
3685 case ISD::SETCC:
3686 case ISD::SETCCCARRY:
3687 case ISD::STRICT_FSETCC:
3688 case ISD::STRICT_FSETCCS: {
3689 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3690 // If we know the result of a setcc has the top bits zero, use this info.
3691 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3693 BitWidth > 1)
3694 Known.Zero.setBitsFrom(1);
3695 break;
3696 }
3697 case ISD::SHL: {
3698 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3699 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3700
3701 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3702 bool NSW = Op->getFlags().hasNoSignedWrap();
3703
3704 bool ShAmtNonZero = Known2.isNonZero();
3705
3706 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3707
3708 // Minimum shift low bits are known zero.
3709 if (std::optional<unsigned> ShMinAmt =
3710 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3711 Known.Zero.setLowBits(*ShMinAmt);
3712 break;
3713 }
3714 case ISD::SRL:
3715 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3716 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3717 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3718 Op->getFlags().hasExact());
3719
3720 // Minimum shift high bits are known zero.
3721 if (std::optional<unsigned> ShMinAmt =
3722 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3723 Known.Zero.setHighBits(*ShMinAmt);
3724 break;
3725 case ISD::SRA:
3726 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3727 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3728 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3729 Op->getFlags().hasExact());
3730 break;
3731 case ISD::ROTL:
3732 case ISD::ROTR:
3733 if (ConstantSDNode *C =
3734 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3735 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3736
3737 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3738
3739 // Canonicalize to ROTR.
3740 if (Opcode == ISD::ROTL && Amt != 0)
3741 Amt = BitWidth - Amt;
3742
3743 Known.Zero = Known.Zero.rotr(Amt);
3744 Known.One = Known.One.rotr(Amt);
3745 }
3746 break;
3747 case ISD::FSHL:
3748 case ISD::FSHR:
3749 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3750 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3751
3752 // For fshl, 0-shift returns the 1st arg.
3753 // For fshr, 0-shift returns the 2nd arg.
3754 if (Amt == 0) {
3755 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3756 DemandedElts, Depth + 1);
3757 break;
3758 }
3759
3760 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3761 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3762 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3763 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3764 if (Opcode == ISD::FSHL) {
3765 Known <<= Amt;
3766 Known2 >>= BitWidth - Amt;
3767 } else {
3768 Known <<= BitWidth - Amt;
3769 Known2 >>= Amt;
3770 }
3771 Known = Known.unionWith(Known2);
3772 }
3773 break;
3774 case ISD::SHL_PARTS:
3775 case ISD::SRA_PARTS:
3776 case ISD::SRL_PARTS: {
3777 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3778
3779 // Collect lo/hi source values and concatenate.
3780 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3781 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3782 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3783 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3784 Known = Known2.concat(Known);
3785
3786 // Collect shift amount.
3787 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3788
3789 if (Opcode == ISD::SHL_PARTS)
3790 Known = KnownBits::shl(Known, Known2);
3791 else if (Opcode == ISD::SRA_PARTS)
3792 Known = KnownBits::ashr(Known, Known2);
3793 else // if (Opcode == ISD::SRL_PARTS)
3794 Known = KnownBits::lshr(Known, Known2);
3795
3796 // TODO: Minimum shift low/high bits are known zero.
3797
3798 if (Op.getResNo() == 0)
3799 Known = Known.extractBits(LoBits, 0);
3800 else
3801 Known = Known.extractBits(HiBits, LoBits);
3802 break;
3803 }
3805 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3806 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3807 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3808 break;
3809 }
3810 case ISD::CTTZ:
3811 case ISD::CTTZ_ZERO_UNDEF: {
3812 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3813 // If we have a known 1, its position is our upper bound.
3814 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3815 unsigned LowBits = llvm::bit_width(PossibleTZ);
3816 Known.Zero.setBitsFrom(LowBits);
3817 break;
3818 }
3819 case ISD::CTLZ:
3820 case ISD::CTLZ_ZERO_UNDEF: {
3821 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3822 // If we have a known 1, its position is our upper bound.
3823 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3824 unsigned LowBits = llvm::bit_width(PossibleLZ);
3825 Known.Zero.setBitsFrom(LowBits);
3826 break;
3827 }
3828 case ISD::CTPOP: {
3829 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3830 // If we know some of the bits are zero, they can't be one.
3831 unsigned PossibleOnes = Known2.countMaxPopulation();
3832 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3833 break;
3834 }
3835 case ISD::PARITY: {
3836 // Parity returns 0 everywhere but the LSB.
3837 Known.Zero.setBitsFrom(1);
3838 break;
3839 }
3840 case ISD::MGATHER:
3841 case ISD::MLOAD: {
3842 ISD::LoadExtType ETy =
3843 (Opcode == ISD::MGATHER)
3844 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3845 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3846 if (ETy == ISD::ZEXTLOAD) {
3847 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3848 KnownBits Known0(MemVT.getScalarSizeInBits());
3849 return Known0.zext(BitWidth);
3850 }
3851 break;
3852 }
3853 case ISD::LOAD: {
3855 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3856 if (ISD::isNON_EXTLoad(LD) && Cst) {
3857 // Determine any common known bits from the loaded constant pool value.
3858 Type *CstTy = Cst->getType();
3859 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3860 !Op.getValueType().isScalableVector()) {
3861 // If its a vector splat, then we can (quickly) reuse the scalar path.
3862 // NOTE: We assume all elements match and none are UNDEF.
3863 if (CstTy->isVectorTy()) {
3864 if (const Constant *Splat = Cst->getSplatValue()) {
3865 Cst = Splat;
3866 CstTy = Cst->getType();
3867 }
3868 }
3869 // TODO - do we need to handle different bitwidths?
3870 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3871 // Iterate across all vector elements finding common known bits.
3872 Known.setAllConflict();
3873 for (unsigned i = 0; i != NumElts; ++i) {
3874 if (!DemandedElts[i])
3875 continue;
3876 if (Constant *Elt = Cst->getAggregateElement(i)) {
3877 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3878 const APInt &Value = CInt->getValue();
3879 Known.One &= Value;
3880 Known.Zero &= ~Value;
3881 continue;
3882 }
3883 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3884 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3885 Known.One &= Value;
3886 Known.Zero &= ~Value;
3887 continue;
3888 }
3889 }
3890 Known.One.clearAllBits();
3891 Known.Zero.clearAllBits();
3892 break;
3893 }
3894 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3895 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3896 Known = KnownBits::makeConstant(CInt->getValue());
3897 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3898 Known =
3899 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3900 }
3901 }
3902 }
3903 } else if (Op.getResNo() == 0) {
3904 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3905 KnownBits KnownScalarMemory(ScalarMemorySize);
3906 if (const MDNode *MD = LD->getRanges())
3907 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3908
3909 // Extend the Known bits from memory to the size of the scalar result.
3910 if (ISD::isZEXTLoad(Op.getNode()))
3911 Known = KnownScalarMemory.zext(BitWidth);
3912 else if (ISD::isSEXTLoad(Op.getNode()))
3913 Known = KnownScalarMemory.sext(BitWidth);
3914 else if (ISD::isEXTLoad(Op.getNode()))
3915 Known = KnownScalarMemory.anyext(BitWidth);
3916 else
3917 Known = KnownScalarMemory;
3918 assert(Known.getBitWidth() == BitWidth);
3919 return Known;
3920 }
3921 break;
3922 }
3924 if (Op.getValueType().isScalableVector())
3925 break;
3926 EVT InVT = Op.getOperand(0).getValueType();
3927 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3928 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3929 Known = Known.zext(BitWidth);
3930 break;
3931 }
3932 case ISD::ZERO_EXTEND: {
3933 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3934 Known = Known.zext(BitWidth);
3935 break;
3936 }
3938 if (Op.getValueType().isScalableVector())
3939 break;
3940 EVT InVT = Op.getOperand(0).getValueType();
3941 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3942 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3943 // If the sign bit is known to be zero or one, then sext will extend
3944 // it to the top bits, else it will just zext.
3945 Known = Known.sext(BitWidth);
3946 break;
3947 }
3948 case ISD::SIGN_EXTEND: {
3949 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3950 // If the sign bit is known to be zero or one, then sext will extend
3951 // it to the top bits, else it will just zext.
3952 Known = Known.sext(BitWidth);
3953 break;
3954 }
3956 if (Op.getValueType().isScalableVector())
3957 break;
3958 EVT InVT = Op.getOperand(0).getValueType();
3959 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3960 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3961 Known = Known.anyext(BitWidth);
3962 break;
3963 }
3964 case ISD::ANY_EXTEND: {
3965 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3966 Known = Known.anyext(BitWidth);
3967 break;
3968 }
3969 case ISD::TRUNCATE: {
3970 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3971 Known = Known.trunc(BitWidth);
3972 break;
3973 }
3974 case ISD::AssertZext: {
3975 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3977 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3978 Known.Zero |= (~InMask);
3979 Known.One &= (~Known.Zero);
3980 break;
3981 }
3982 case ISD::AssertAlign: {
3983 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
3984 assert(LogOfAlign != 0);
3985
3986 // TODO: Should use maximum with source
3987 // If a node is guaranteed to be aligned, set low zero bits accordingly as
3988 // well as clearing one bits.
3989 Known.Zero.setLowBits(LogOfAlign);
3990 Known.One.clearLowBits(LogOfAlign);
3991 break;
3992 }
3993 case ISD::AssertNoFPClass: {
3994 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3995
3996 FPClassTest NoFPClass =
3997 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
3998 const FPClassTest NegativeTestMask = fcNan | fcNegative;
3999 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4000 // Cannot be negative.
4001 Known.makeNonNegative();
4002 }
4003
4004 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4005 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4006 // Cannot be positive.
4007 Known.makeNegative();
4008 }
4009
4010 break;
4011 }
4012 case ISD::FGETSIGN:
4013 // All bits are zero except the low bit.
4014 Known.Zero.setBitsFrom(1);
4015 break;
4016 case ISD::ADD:
4017 case ISD::SUB: {
4018 SDNodeFlags Flags = Op.getNode()->getFlags();
4019 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4020 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4022 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4023 Flags.hasNoUnsignedWrap(), Known, Known2);
4024 break;
4025 }
4026 case ISD::USUBO:
4027 case ISD::SSUBO:
4028 case ISD::USUBO_CARRY:
4029 case ISD::SSUBO_CARRY:
4030 if (Op.getResNo() == 1) {
4031 // If we know the result of a setcc has the top bits zero, use this info.
4032 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4034 BitWidth > 1)
4035 Known.Zero.setBitsFrom(1);
4036 break;
4037 }
4038 [[fallthrough]];
4039 case ISD::SUBC: {
4040 assert(Op.getResNo() == 0 &&
4041 "We only compute knownbits for the difference here.");
4042
4043 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4044 KnownBits Borrow(1);
4045 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4046 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4047 // Borrow has bit width 1
4048 Borrow = Borrow.trunc(1);
4049 } else {
4050 Borrow.setAllZero();
4051 }
4052
4053 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4054 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4055 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4056 break;
4057 }
4058 case ISD::UADDO:
4059 case ISD::SADDO:
4060 case ISD::UADDO_CARRY:
4061 case ISD::SADDO_CARRY:
4062 if (Op.getResNo() == 1) {
4063 // If we know the result of a setcc has the top bits zero, use this info.
4064 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4066 BitWidth > 1)
4067 Known.Zero.setBitsFrom(1);
4068 break;
4069 }
4070 [[fallthrough]];
4071 case ISD::ADDC:
4072 case ISD::ADDE: {
4073 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4074
4075 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4076 KnownBits Carry(1);
4077 if (Opcode == ISD::ADDE)
4078 // Can't track carry from glue, set carry to unknown.
4079 Carry.resetAll();
4080 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4081 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4082 // Carry has bit width 1
4083 Carry = Carry.trunc(1);
4084 } else {
4085 Carry.setAllZero();
4086 }
4087
4088 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4089 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4090 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4091 break;
4092 }
4093 case ISD::UDIV: {
4094 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4095 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4096 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4097 break;
4098 }
4099 case ISD::SDIV: {
4100 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4101 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4102 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4103 break;
4104 }
4105 case ISD::SREM: {
4106 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4107 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4108 Known = KnownBits::srem(Known, Known2);
4109 break;
4110 }
4111 case ISD::UREM: {
4112 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4113 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4114 Known = KnownBits::urem(Known, Known2);
4115 break;
4116 }
4117 case ISD::EXTRACT_ELEMENT: {
4118 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4119 const unsigned Index = Op.getConstantOperandVal(1);
4120 const unsigned EltBitWidth = Op.getValueSizeInBits();
4121
4122 // Remove low part of known bits mask
4123 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4124 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4125
4126 // Remove high part of known bit mask
4127 Known = Known.trunc(EltBitWidth);
4128 break;
4129 }
4131 SDValue InVec = Op.getOperand(0);
4132 SDValue EltNo = Op.getOperand(1);
4133 EVT VecVT = InVec.getValueType();
4134 // computeKnownBits not yet implemented for scalable vectors.
4135 if (VecVT.isScalableVector())
4136 break;
4137 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4138 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4139
4140 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4141 // anything about the extended bits.
4142 if (BitWidth > EltBitWidth)
4143 Known = Known.trunc(EltBitWidth);
4144
4145 // If we know the element index, just demand that vector element, else for
4146 // an unknown element index, ignore DemandedElts and demand them all.
4147 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4148 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4149 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4150 DemandedSrcElts =
4151 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4152
4153 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4154 if (BitWidth > EltBitWidth)
4155 Known = Known.anyext(BitWidth);
4156 break;
4157 }
4159 if (Op.getValueType().isScalableVector())
4160 break;
4161
4162 // If we know the element index, split the demand between the
4163 // source vector and the inserted element, otherwise assume we need
4164 // the original demanded vector elements and the value.
4165 SDValue InVec = Op.getOperand(0);
4166 SDValue InVal = Op.getOperand(1);
4167 SDValue EltNo = Op.getOperand(2);
4168 bool DemandedVal = true;
4169 APInt DemandedVecElts = DemandedElts;
4170 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4171 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4172 unsigned EltIdx = CEltNo->getZExtValue();
4173 DemandedVal = !!DemandedElts[EltIdx];
4174 DemandedVecElts.clearBit(EltIdx);
4175 }
4176 Known.setAllConflict();
4177 if (DemandedVal) {
4178 Known2 = computeKnownBits(InVal, Depth + 1);
4179 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4180 }
4181 if (!!DemandedVecElts) {
4182 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4183 Known = Known.intersectWith(Known2);
4184 }
4185 break;
4186 }
4187 case ISD::BITREVERSE: {
4188 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4189 Known = Known2.reverseBits();
4190 break;
4191 }
4192 case ISD::BSWAP: {
4193 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4194 Known = Known2.byteSwap();
4195 break;
4196 }
4197 case ISD::ABS: {
4198 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4199 Known = Known2.abs();
4200 Known.Zero.setHighBits(
4201 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4202 break;
4203 }
4204 case ISD::USUBSAT: {
4205 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4206 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4207 Known = KnownBits::usub_sat(Known, Known2);
4208 break;
4209 }
4210 case ISD::UMIN: {
4211 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4212 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4213 Known = KnownBits::umin(Known, Known2);
4214 break;
4215 }
4216 case ISD::UMAX: {
4217 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4218 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4219 Known = KnownBits::umax(Known, Known2);
4220 break;
4221 }
4222 case ISD::SMIN:
4223 case ISD::SMAX: {
4224 // If we have a clamp pattern, we know that the number of sign bits will be
4225 // the minimum of the clamp min/max range.
4226 bool IsMax = (Opcode == ISD::SMAX);
4227 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4228 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4229 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4230 CstHigh =
4231 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4232 if (CstLow && CstHigh) {
4233 if (!IsMax)
4234 std::swap(CstLow, CstHigh);
4235
4236 const APInt &ValueLow = CstLow->getAPIntValue();
4237 const APInt &ValueHigh = CstHigh->getAPIntValue();
4238 if (ValueLow.sle(ValueHigh)) {
4239 unsigned LowSignBits = ValueLow.getNumSignBits();
4240 unsigned HighSignBits = ValueHigh.getNumSignBits();
4241 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4242 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4243 Known.One.setHighBits(MinSignBits);
4244 break;
4245 }
4246 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4247 Known.Zero.setHighBits(MinSignBits);
4248 break;
4249 }
4250 }
4251 }
4252
4253 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4254 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4255 if (IsMax)
4256 Known = KnownBits::smax(Known, Known2);
4257 else
4258 Known = KnownBits::smin(Known, Known2);
4259
4260 // For SMAX, if CstLow is non-negative we know the result will be
4261 // non-negative and thus all sign bits are 0.
4262 // TODO: There's an equivalent of this for smin with negative constant for
4263 // known ones.
4264 if (IsMax && CstLow) {
4265 const APInt &ValueLow = CstLow->getAPIntValue();
4266 if (ValueLow.isNonNegative()) {
4267 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4268 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4269 }
4270 }
4271
4272 break;
4273 }
4274 case ISD::UINT_TO_FP: {
4275 Known.makeNonNegative();
4276 break;
4277 }
4278 case ISD::SINT_TO_FP: {
4279 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4280 if (Known2.isNonNegative())
4281 Known.makeNonNegative();
4282 else if (Known2.isNegative())
4283 Known.makeNegative();
4284 break;
4285 }
4286 case ISD::FP_TO_UINT_SAT: {
4287 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4288 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4290 break;
4291 }
4292 case ISD::ATOMIC_LOAD: {
4293 // If we are looking at the loaded value.
4294 if (Op.getResNo() == 0) {
4295 auto *AT = cast<AtomicSDNode>(Op);
4296 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4297 KnownBits KnownScalarMemory(ScalarMemorySize);
4298 if (const MDNode *MD = AT->getRanges())
4299 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4300
4301 switch (AT->getExtensionType()) {
4302 case ISD::ZEXTLOAD:
4303 Known = KnownScalarMemory.zext(BitWidth);
4304 break;
4305 case ISD::SEXTLOAD:
4306 Known = KnownScalarMemory.sext(BitWidth);
4307 break;
4308 case ISD::EXTLOAD:
4309 switch (TLI->getExtendForAtomicOps()) {
4310 case ISD::ZERO_EXTEND:
4311 Known = KnownScalarMemory.zext(BitWidth);
4312 break;
4313 case ISD::SIGN_EXTEND:
4314 Known = KnownScalarMemory.sext(BitWidth);
4315 break;
4316 default:
4317 Known = KnownScalarMemory.anyext(BitWidth);
4318 break;
4319 }
4320 break;
4321 case ISD::NON_EXTLOAD:
4322 Known = KnownScalarMemory;
4323 break;
4324 }
4325 assert(Known.getBitWidth() == BitWidth);
4326 }
4327 break;
4328 }
4329 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4330 if (Op.getResNo() == 1) {
4331 // The boolean result conforms to getBooleanContents.
4332 // If we know the result of a setcc has the top bits zero, use this info.
4333 // We know that we have an integer-based boolean since these operations
4334 // are only available for integer.
4335 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4337 BitWidth > 1)
4338 Known.Zero.setBitsFrom(1);
4339 break;
4340 }
4341 [[fallthrough]];
4342 case ISD::ATOMIC_CMP_SWAP:
4343 case ISD::ATOMIC_SWAP:
4344 case ISD::ATOMIC_LOAD_ADD:
4345 case ISD::ATOMIC_LOAD_SUB:
4346 case ISD::ATOMIC_LOAD_AND:
4347 case ISD::ATOMIC_LOAD_CLR:
4348 case ISD::ATOMIC_LOAD_OR:
4349 case ISD::ATOMIC_LOAD_XOR:
4350 case ISD::ATOMIC_LOAD_NAND:
4351 case ISD::ATOMIC_LOAD_MIN:
4352 case ISD::ATOMIC_LOAD_MAX:
4353 case ISD::ATOMIC_LOAD_UMIN:
4354 case ISD::ATOMIC_LOAD_UMAX: {
4355 // If we are looking at the loaded value.
4356 if (Op.getResNo() == 0) {
4357 auto *AT = cast<AtomicSDNode>(Op);
4358 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4359
4360 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4361 Known.Zero.setBitsFrom(MemBits);
4362 }
4363 break;
4364 }
4365 case ISD::FrameIndex:
4367 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4368 Known, getMachineFunction());
4369 break;
4370
4371 default:
4372 if (Opcode < ISD::BUILTIN_OP_END)
4373 break;
4374 [[fallthrough]];
4378 // TODO: Probably okay to remove after audit; here to reduce change size
4379 // in initial enablement patch for scalable vectors
4380 if (Op.getValueType().isScalableVector())
4381 break;
4382
4383 // Allow the target to implement this method for its nodes.
4384 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4385 break;
4386 }
4387
4388 return Known;
4389}
4390
4391/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4404
4407 // X + 0 never overflow
4408 if (isNullConstant(N1))
4409 return OFK_Never;
4410
4411 // If both operands each have at least two sign bits, the addition
4412 // cannot overflow.
4413 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4414 return OFK_Never;
4415
4416 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4417 return OFK_Sometime;
4418}
4419
4422 // X + 0 never overflow
4423 if (isNullConstant(N1))
4424 return OFK_Never;
4425
4426 // mulhi + 1 never overflow
4427 KnownBits N1Known = computeKnownBits(N1);
4428 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4429 N1Known.getMaxValue().ult(2))
4430 return OFK_Never;
4431
4432 KnownBits N0Known = computeKnownBits(N0);
4433 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4434 N0Known.getMaxValue().ult(2))
4435 return OFK_Never;
4436
4437 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4438 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4439 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4440 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4441}
4442
4445 // X - 0 never overflow
4446 if (isNullConstant(N1))
4447 return OFK_Never;
4448
4449 // If both operands each have at least two sign bits, the subtraction
4450 // cannot overflow.
4451 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4452 return OFK_Never;
4453
4454 KnownBits N0Known = computeKnownBits(N0);
4455 KnownBits N1Known = computeKnownBits(N1);
4456 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4457 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4458 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4459}
4460
4463 // X - 0 never overflow
4464 if (isNullConstant(N1))
4465 return OFK_Never;
4466
4467 KnownBits N0Known = computeKnownBits(N0);
4468 KnownBits N1Known = computeKnownBits(N1);
4469 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4470 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4471 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4472}
4473
4476 // X * 0 and X * 1 never overflow.
4477 if (isNullConstant(N1) || isOneConstant(N1))
4478 return OFK_Never;
4479
4480 KnownBits N0Known = computeKnownBits(N0);
4481 KnownBits N1Known = computeKnownBits(N1);
4482 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4483 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4484 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4485}
4486
4489 // X * 0 and X * 1 never overflow.
4490 if (isNullConstant(N1) || isOneConstant(N1))
4491 return OFK_Never;
4492
4493 // Get the size of the result.
4494 unsigned BitWidth = N0.getScalarValueSizeInBits();
4495
4496 // Sum of the sign bits.
4497 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4498
4499 // If we have enough sign bits, then there's no overflow.
4500 if (SignBits > BitWidth + 1)
4501 return OFK_Never;
4502
4503 if (SignBits == BitWidth + 1) {
4504 // The overflow occurs when the true multiplication of the
4505 // the operands is the minimum negative number.
4506 KnownBits N0Known = computeKnownBits(N0);
4507 KnownBits N1Known = computeKnownBits(N1);
4508 // If one of the operands is non-negative, then there's no
4509 // overflow.
4510 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4511 return OFK_Never;
4512 }
4513
4514 return OFK_Sometime;
4515}
4516
4518 if (Depth >= MaxRecursionDepth)
4519 return false; // Limit search depth.
4520
4521 EVT OpVT = Val.getValueType();
4522 unsigned BitWidth = OpVT.getScalarSizeInBits();
4523
4524 // Is the constant a known power of 2?
4526 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4527 }))
4528 return true;
4529
4530 // A left-shift of a constant one will have exactly one bit set because
4531 // shifting the bit off the end is undefined.
4532 if (Val.getOpcode() == ISD::SHL) {
4533 auto *C = isConstOrConstSplat(Val.getOperand(0));
4534 if (C && C->getAPIntValue() == 1)
4535 return true;
4536 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4537 isKnownNeverZero(Val, Depth);
4538 }
4539
4540 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4541 // one bit set.
4542 if (Val.getOpcode() == ISD::SRL) {
4543 auto *C = isConstOrConstSplat(Val.getOperand(0));
4544 if (C && C->getAPIntValue().isSignMask())
4545 return true;
4546 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4547 isKnownNeverZero(Val, Depth);
4548 }
4549
4550 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4551 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4552
4553 // Are all operands of a build vector constant powers of two?
4554 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4555 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4556 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4557 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4558 return false;
4559 }))
4560 return true;
4561
4562 // Is the operand of a splat vector a constant power of two?
4563 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4565 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4566 return true;
4567
4568 // vscale(power-of-two) is a power-of-two for some targets
4569 if (Val.getOpcode() == ISD::VSCALE &&
4570 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4572 return true;
4573
4574 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4575 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4576 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4578
4579 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4580 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4582
4583 // Looking for `x & -x` pattern:
4584 // If x == 0:
4585 // x & -x -> 0
4586 // If x != 0:
4587 // x & -x -> non-zero pow2
4588 // so if we find the pattern return whether we know `x` is non-zero.
4589 SDValue X;
4590 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4591 return isKnownNeverZero(X, Depth);
4592
4593 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4594 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4595
4596 // More could be done here, though the above checks are enough
4597 // to handle some common cases.
4598 return false;
4599}
4600
4602 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4603 return C1->getValueAPF().getExactLog2Abs() >= 0;
4604
4605 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4606 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4607
4608 return false;
4609}
4610
4612 EVT VT = Op.getValueType();
4613
4614 // Since the number of lanes in a scalable vector is unknown at compile time,
4615 // we track one bit which is implicitly broadcast to all lanes. This means
4616 // that all lanes in a scalable vector are considered demanded.
4617 APInt DemandedElts = VT.isFixedLengthVector()
4619 : APInt(1, 1);
4620 return ComputeNumSignBits(Op, DemandedElts, Depth);
4621}
4622
4623unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4624 unsigned Depth) const {
4625 EVT VT = Op.getValueType();
4626 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4627 unsigned VTBits = VT.getScalarSizeInBits();
4628 unsigned NumElts = DemandedElts.getBitWidth();
4629 unsigned Tmp, Tmp2;
4630 unsigned FirstAnswer = 1;
4631
4632 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4633 const APInt &Val = C->getAPIntValue();
4634 return Val.getNumSignBits();
4635 }
4636
4637 if (Depth >= MaxRecursionDepth)
4638 return 1; // Limit search depth.
4639
4640 if (!DemandedElts)
4641 return 1; // No demanded elts, better to assume we don't know anything.
4642
4643 unsigned Opcode = Op.getOpcode();
4644 switch (Opcode) {
4645 default: break;
4646 case ISD::AssertSext:
4647 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4648 return VTBits-Tmp+1;
4649 case ISD::AssertZext:
4650 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4651 return VTBits-Tmp;
4652 case ISD::FREEZE:
4653 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4654 /*PoisonOnly=*/false))
4655 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4656 break;
4657 case ISD::MERGE_VALUES:
4658 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4659 Depth + 1);
4660 case ISD::SPLAT_VECTOR: {
4661 // Check if the sign bits of source go down as far as the truncated value.
4662 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4663 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4664 if (NumSrcSignBits > (NumSrcBits - VTBits))
4665 return NumSrcSignBits - (NumSrcBits - VTBits);
4666 break;
4667 }
4668 case ISD::BUILD_VECTOR:
4669 assert(!VT.isScalableVector());
4670 Tmp = VTBits;
4671 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4672 if (!DemandedElts[i])
4673 continue;
4674
4675 SDValue SrcOp = Op.getOperand(i);
4676 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4677 // for constant nodes to ensure we only look at the sign bits.
4679 APInt T = C->getAPIntValue().trunc(VTBits);
4680 Tmp2 = T.getNumSignBits();
4681 } else {
4682 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4683
4684 if (SrcOp.getValueSizeInBits() != VTBits) {
4685 assert(SrcOp.getValueSizeInBits() > VTBits &&
4686 "Expected BUILD_VECTOR implicit truncation");
4687 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4688 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4689 }
4690 }
4691 Tmp = std::min(Tmp, Tmp2);
4692 }
4693 return Tmp;
4694
4695 case ISD::VECTOR_COMPRESS: {
4696 SDValue Vec = Op.getOperand(0);
4697 SDValue PassThru = Op.getOperand(2);
4698 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4699 if (Tmp == 1)
4700 return 1;
4701 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4702 Tmp = std::min(Tmp, Tmp2);
4703 return Tmp;
4704 }
4705
4706 case ISD::VECTOR_SHUFFLE: {
4707 // Collect the minimum number of sign bits that are shared by every vector
4708 // element referenced by the shuffle.
4709 APInt DemandedLHS, DemandedRHS;
4711 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4712 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4713 DemandedLHS, DemandedRHS))
4714 return 1;
4715
4716 Tmp = std::numeric_limits<unsigned>::max();
4717 if (!!DemandedLHS)
4718 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4719 if (!!DemandedRHS) {
4720 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4721 Tmp = std::min(Tmp, Tmp2);
4722 }
4723 // If we don't know anything, early out and try computeKnownBits fall-back.
4724 if (Tmp == 1)
4725 break;
4726 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4727 return Tmp;
4728 }
4729
4730 case ISD::BITCAST: {
4731 if (VT.isScalableVector())
4732 break;
4733 SDValue N0 = Op.getOperand(0);
4734 EVT SrcVT = N0.getValueType();
4735 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4736
4737 // Ignore bitcasts from unsupported types..
4738 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4739 break;
4740
4741 // Fast handling of 'identity' bitcasts.
4742 if (VTBits == SrcBits)
4743 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4744
4745 bool IsLE = getDataLayout().isLittleEndian();
4746
4747 // Bitcast 'large element' scalar/vector to 'small element' vector.
4748 if ((SrcBits % VTBits) == 0) {
4749 assert(VT.isVector() && "Expected bitcast to vector");
4750
4751 unsigned Scale = SrcBits / VTBits;
4752 APInt SrcDemandedElts =
4753 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4754
4755 // Fast case - sign splat can be simply split across the small elements.
4756 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4757 if (Tmp == SrcBits)
4758 return VTBits;
4759
4760 // Slow case - determine how far the sign extends into each sub-element.
4761 Tmp2 = VTBits;
4762 for (unsigned i = 0; i != NumElts; ++i)
4763 if (DemandedElts[i]) {
4764 unsigned SubOffset = i % Scale;
4765 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4766 SubOffset = SubOffset * VTBits;
4767 if (Tmp <= SubOffset)
4768 return 1;
4769 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4770 }
4771 return Tmp2;
4772 }
4773 break;
4774 }
4775
4777 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4778 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4779 return VTBits - Tmp + 1;
4780 case ISD::SIGN_EXTEND:
4781 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4782 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4784 // Max of the input and what this extends.
4785 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4786 Tmp = VTBits-Tmp+1;
4787 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4788 return std::max(Tmp, Tmp2);
4790 if (VT.isScalableVector())
4791 break;
4792 SDValue Src = Op.getOperand(0);
4793 EVT SrcVT = Src.getValueType();
4794 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4795 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4796 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4797 }
4798 case ISD::SRA:
4799 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4800 // SRA X, C -> adds C sign bits.
4801 if (std::optional<unsigned> ShAmt =
4802 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4803 Tmp = std::min(Tmp + *ShAmt, VTBits);
4804 return Tmp;
4805 case ISD::SHL:
4806 if (std::optional<ConstantRange> ShAmtRange =
4807 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4808 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4809 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4810 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4811 // shifted out, then we can compute the number of sign bits for the
4812 // operand being extended. A future improvement could be to pass along the
4813 // "shifted left by" information in the recursive calls to
4814 // ComputeKnownSignBits. Allowing us to handle this more generically.
4815 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4816 SDValue Ext = Op.getOperand(0);
4817 EVT ExtVT = Ext.getValueType();
4818 SDValue Extendee = Ext.getOperand(0);
4819 EVT ExtendeeVT = Extendee.getValueType();
4820 unsigned SizeDifference =
4821 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4822 if (SizeDifference <= MinShAmt) {
4823 Tmp = SizeDifference +
4824 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4825 if (MaxShAmt < Tmp)
4826 return Tmp - MaxShAmt;
4827 }
4828 }
4829 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4830 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4831 if (MaxShAmt < Tmp)
4832 return Tmp - MaxShAmt;
4833 }
4834 break;
4835 case ISD::AND:
4836 case ISD::OR:
4837 case ISD::XOR: // NOT is handled here.
4838 // Logical binary ops preserve the number of sign bits at the worst.
4839 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4840 if (Tmp != 1) {
4841 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4842 FirstAnswer = std::min(Tmp, Tmp2);
4843 // We computed what we know about the sign bits as our first
4844 // answer. Now proceed to the generic code that uses
4845 // computeKnownBits, and pick whichever answer is better.
4846 }
4847 break;
4848
4849 case ISD::SELECT:
4850 case ISD::VSELECT:
4851 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4852 if (Tmp == 1) return 1; // Early out.
4853 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4854 return std::min(Tmp, Tmp2);
4855 case ISD::SELECT_CC:
4856 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4857 if (Tmp == 1) return 1; // Early out.
4858 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4859 return std::min(Tmp, Tmp2);
4860
4861 case ISD::SMIN:
4862 case ISD::SMAX: {
4863 // If we have a clamp pattern, we know that the number of sign bits will be
4864 // the minimum of the clamp min/max range.
4865 bool IsMax = (Opcode == ISD::SMAX);
4866 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4867 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4868 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4869 CstHigh =
4870 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4871 if (CstLow && CstHigh) {
4872 if (!IsMax)
4873 std::swap(CstLow, CstHigh);
4874 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4875 Tmp = CstLow->getAPIntValue().getNumSignBits();
4876 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4877 return std::min(Tmp, Tmp2);
4878 }
4879 }
4880
4881 // Fallback - just get the minimum number of sign bits of the operands.
4882 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4883 if (Tmp == 1)
4884 return 1; // Early out.
4885 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4886 return std::min(Tmp, Tmp2);
4887 }
4888 case ISD::UMIN:
4889 case ISD::UMAX:
4890 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4891 if (Tmp == 1)
4892 return 1; // Early out.
4893 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4894 return std::min(Tmp, Tmp2);
4895 case ISD::SSUBO_CARRY:
4896 case ISD::USUBO_CARRY:
4897 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4898 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4899 return VTBits;
4900 [[fallthrough]];
4901 case ISD::SADDO:
4902 case ISD::UADDO:
4903 case ISD::SADDO_CARRY:
4904 case ISD::UADDO_CARRY:
4905 case ISD::SSUBO:
4906 case ISD::USUBO:
4907 case ISD::SMULO:
4908 case ISD::UMULO:
4909 if (Op.getResNo() != 1)
4910 break;
4911 // The boolean result conforms to getBooleanContents. Fall through.
4912 // If setcc returns 0/-1, all bits are sign bits.
4913 // We know that we have an integer-based boolean since these operations
4914 // are only available for integer.
4915 if (TLI->getBooleanContents(VT.isVector(), false) ==
4917 return VTBits;
4918 break;
4919 case ISD::SETCC:
4920 case ISD::SETCCCARRY:
4921 case ISD::STRICT_FSETCC:
4922 case ISD::STRICT_FSETCCS: {
4923 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4924 // If setcc returns 0/-1, all bits are sign bits.
4925 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4927 return VTBits;
4928 break;
4929 }
4930 case ISD::ROTL:
4931 case ISD::ROTR:
4932 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4933
4934 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4935 if (Tmp == VTBits)
4936 return VTBits;
4937
4938 if (ConstantSDNode *C =
4939 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4940 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4941
4942 // Handle rotate right by N like a rotate left by 32-N.
4943 if (Opcode == ISD::ROTR)
4944 RotAmt = (VTBits - RotAmt) % VTBits;
4945
4946 // If we aren't rotating out all of the known-in sign bits, return the
4947 // number that are left. This handles rotl(sext(x), 1) for example.
4948 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4949 }
4950 break;
4951 case ISD::ADD:
4952 case ISD::ADDC:
4953 // TODO: Move Operand 1 check before Operand 0 check
4954 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4955 if (Tmp == 1) return 1; // Early out.
4956
4957 // Special case decrementing a value (ADD X, -1):
4958 if (ConstantSDNode *CRHS =
4959 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4960 if (CRHS->isAllOnes()) {
4961 KnownBits Known =
4962 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4963
4964 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4965 // sign bits set.
4966 if ((Known.Zero | 1).isAllOnes())
4967 return VTBits;
4968
4969 // If we are subtracting one from a positive number, there is no carry
4970 // out of the result.
4971 if (Known.isNonNegative())
4972 return Tmp;
4973 }
4974
4975 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4976 if (Tmp2 == 1) return 1; // Early out.
4977
4978 // Add can have at most one carry bit. Thus we know that the output
4979 // is, at worst, one more bit than the inputs.
4980 return std::min(Tmp, Tmp2) - 1;
4981 case ISD::SUB:
4982 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4983 if (Tmp2 == 1) return 1; // Early out.
4984
4985 // Handle NEG.
4986 if (ConstantSDNode *CLHS =
4987 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
4988 if (CLHS->isZero()) {
4989 KnownBits Known =
4990 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4991 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4992 // sign bits set.
4993 if ((Known.Zero | 1).isAllOnes())
4994 return VTBits;
4995
4996 // If the input is known to be positive (the sign bit is known clear),
4997 // the output of the NEG has the same number of sign bits as the input.
4998 if (Known.isNonNegative())
4999 return Tmp2;
5000
5001 // Otherwise, we treat this like a SUB.
5002 }
5003
5004 // Sub can have at most one carry bit. Thus we know that the output
5005 // is, at worst, one more bit than the inputs.
5006 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5007 if (Tmp == 1) return 1; // Early out.
5008 return std::min(Tmp, Tmp2) - 1;
5009 case ISD::MUL: {
5010 // The output of the Mul can be at most twice the valid bits in the inputs.
5011 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5012 if (SignBitsOp0 == 1)
5013 break;
5014 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5015 if (SignBitsOp1 == 1)
5016 break;
5017 unsigned OutValidBits =
5018 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5019 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5020 }
5021 case ISD::AVGCEILS:
5022 case ISD::AVGFLOORS:
5023 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5024 if (Tmp == 1)
5025 return 1; // Early out.
5026 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5027 return std::min(Tmp, Tmp2);
5028 case ISD::SREM:
5029 // The sign bit is the LHS's sign bit, except when the result of the
5030 // remainder is zero. The magnitude of the result should be less than or
5031 // equal to the magnitude of the LHS. Therefore, the result should have
5032 // at least as many sign bits as the left hand side.
5033 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5034 case ISD::TRUNCATE: {
5035 // Check if the sign bits of source go down as far as the truncated value.
5036 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5037 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5038 if (NumSrcSignBits > (NumSrcBits - VTBits))
5039 return NumSrcSignBits - (NumSrcBits - VTBits);
5040 break;
5041 }
5042 case ISD::EXTRACT_ELEMENT: {
5043 if (VT.isScalableVector())
5044 break;
5045 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5046 const int BitWidth = Op.getValueSizeInBits();
5047 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5048
5049 // Get reverse index (starting from 1), Op1 value indexes elements from
5050 // little end. Sign starts at big end.
5051 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5052
5053 // If the sign portion ends in our element the subtraction gives correct
5054 // result. Otherwise it gives either negative or > bitwidth result
5055 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5056 }
5058 if (VT.isScalableVector())
5059 break;
5060 // If we know the element index, split the demand between the
5061 // source vector and the inserted element, otherwise assume we need
5062 // the original demanded vector elements and the value.
5063 SDValue InVec = Op.getOperand(0);
5064 SDValue InVal = Op.getOperand(1);
5065 SDValue EltNo = Op.getOperand(2);
5066 bool DemandedVal = true;
5067 APInt DemandedVecElts = DemandedElts;
5068 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5069 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5070 unsigned EltIdx = CEltNo->getZExtValue();
5071 DemandedVal = !!DemandedElts[EltIdx];
5072 DemandedVecElts.clearBit(EltIdx);
5073 }
5074 Tmp = std::numeric_limits<unsigned>::max();
5075 if (DemandedVal) {
5076 // TODO - handle implicit truncation of inserted elements.
5077 if (InVal.getScalarValueSizeInBits() != VTBits)
5078 break;
5079 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5080 Tmp = std::min(Tmp, Tmp2);
5081 }
5082 if (!!DemandedVecElts) {
5083 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5084 Tmp = std::min(Tmp, Tmp2);
5085 }
5086 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5087 return Tmp;
5088 }
5090 assert(!VT.isScalableVector());
5091 SDValue InVec = Op.getOperand(0);
5092 SDValue EltNo = Op.getOperand(1);
5093 EVT VecVT = InVec.getValueType();
5094 // ComputeNumSignBits not yet implemented for scalable vectors.
5095 if (VecVT.isScalableVector())
5096 break;
5097 const unsigned BitWidth = Op.getValueSizeInBits();
5098 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5099 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5100
5101 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5102 // anything about sign bits. But if the sizes match we can derive knowledge
5103 // about sign bits from the vector operand.
5104 if (BitWidth != EltBitWidth)
5105 break;
5106
5107 // If we know the element index, just demand that vector element, else for
5108 // an unknown element index, ignore DemandedElts and demand them all.
5109 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5110 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5111 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5112 DemandedSrcElts =
5113 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5114
5115 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5116 }
5118 // Offset the demanded elts by the subvector index.
5119 SDValue Src = Op.getOperand(0);
5120 // Bail until we can represent demanded elements for scalable vectors.
5121 if (Src.getValueType().isScalableVector())
5122 break;
5123 uint64_t Idx = Op.getConstantOperandVal(1);
5124 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5125 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5126 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5127 }
5128 case ISD::CONCAT_VECTORS: {
5129 if (VT.isScalableVector())
5130 break;
5131 // Determine the minimum number of sign bits across all demanded
5132 // elts of the input vectors. Early out if the result is already 1.
5133 Tmp = std::numeric_limits<unsigned>::max();
5134 EVT SubVectorVT = Op.getOperand(0).getValueType();
5135 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5136 unsigned NumSubVectors = Op.getNumOperands();
5137 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5138 APInt DemandedSub =
5139 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5140 if (!DemandedSub)
5141 continue;
5142 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5143 Tmp = std::min(Tmp, Tmp2);
5144 }
5145 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5146 return Tmp;
5147 }
5148 case ISD::INSERT_SUBVECTOR: {
5149 if (VT.isScalableVector())
5150 break;
5151 // Demand any elements from the subvector and the remainder from the src its
5152 // inserted into.
5153 SDValue Src = Op.getOperand(0);
5154 SDValue Sub = Op.getOperand(1);
5155 uint64_t Idx = Op.getConstantOperandVal(2);
5156 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5157 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5158 APInt DemandedSrcElts = DemandedElts;
5159 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5160
5161 Tmp = std::numeric_limits<unsigned>::max();
5162 if (!!DemandedSubElts) {
5163 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5164 if (Tmp == 1)
5165 return 1; // early-out
5166 }
5167 if (!!DemandedSrcElts) {
5168 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5169 Tmp = std::min(Tmp, Tmp2);
5170 }
5171 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5172 return Tmp;
5173 }
5174 case ISD::LOAD: {
5176 if (const MDNode *Ranges = LD->getRanges()) {
5177 if (DemandedElts != 1)
5178 break;
5179
5181 if (VTBits > CR.getBitWidth()) {
5182 switch (LD->getExtensionType()) {
5183 case ISD::SEXTLOAD:
5184 CR = CR.signExtend(VTBits);
5185 break;
5186 case ISD::ZEXTLOAD:
5187 CR = CR.zeroExtend(VTBits);
5188 break;
5189 default:
5190 break;
5191 }
5192 }
5193
5194 if (VTBits != CR.getBitWidth())
5195 break;
5196 return std::min(CR.getSignedMin().getNumSignBits(),
5198 }
5199
5200 break;
5201 }
5202 case ISD::ATOMIC_CMP_SWAP:
5203 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
5204 case ISD::ATOMIC_SWAP:
5205 case ISD::ATOMIC_LOAD_ADD:
5206 case ISD::ATOMIC_LOAD_SUB:
5207 case ISD::ATOMIC_LOAD_AND:
5208 case ISD::ATOMIC_LOAD_CLR:
5209 case ISD::ATOMIC_LOAD_OR:
5210 case ISD::ATOMIC_LOAD_XOR:
5211 case ISD::ATOMIC_LOAD_NAND:
5212 case ISD::ATOMIC_LOAD_MIN:
5213 case ISD::ATOMIC_LOAD_MAX:
5214 case ISD::ATOMIC_LOAD_UMIN:
5215 case ISD::ATOMIC_LOAD_UMAX:
5216 case ISD::ATOMIC_LOAD: {
5217 auto *AT = cast<AtomicSDNode>(Op);
5218 // If we are looking at the loaded value.
5219 if (Op.getResNo() == 0) {
5220 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5221 if (Tmp == VTBits)
5222 return 1; // early-out
5223
5224 // For atomic_load, prefer to use the extension type.
5225 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5226 switch (AT->getExtensionType()) {
5227 default:
5228 break;
5229 case ISD::SEXTLOAD:
5230 return VTBits - Tmp + 1;
5231 case ISD::ZEXTLOAD:
5232 return VTBits - Tmp;
5233 }
5234 }
5235
5236 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5237 return VTBits - Tmp + 1;
5238 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5239 return VTBits - Tmp;
5240 }
5241 break;
5242 }
5243 }
5244
5245 // If we are looking at the loaded value of the SDNode.
5246 if (Op.getResNo() == 0) {
5247 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5248 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5249 unsigned ExtType = LD->getExtensionType();
5250 switch (ExtType) {
5251 default: break;
5252 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5253 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5254 return VTBits - Tmp + 1;
5255 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5256 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5257 return VTBits - Tmp;
5258 case ISD::NON_EXTLOAD:
5259 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5260 // We only need to handle vectors - computeKnownBits should handle
5261 // scalar cases.
5262 Type *CstTy = Cst->getType();
5263 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5264 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5265 VTBits == CstTy->getScalarSizeInBits()) {
5266 Tmp = VTBits;
5267 for (unsigned i = 0; i != NumElts; ++i) {
5268 if (!DemandedElts[i])
5269 continue;
5270 if (Constant *Elt = Cst->getAggregateElement(i)) {
5271 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5272 const APInt &Value = CInt->getValue();
5273 Tmp = std::min(Tmp, Value.getNumSignBits());
5274 continue;
5275 }
5276 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5277 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5278 Tmp = std::min(Tmp, Value.getNumSignBits());
5279 continue;
5280 }
5281 }
5282 // Unknown type. Conservatively assume no bits match sign bit.
5283 return 1;
5284 }
5285 return Tmp;
5286 }
5287 }
5288 break;
5289 }
5290 }
5291 }
5292
5293 // Allow the target to implement this method for its nodes.
5294 if (Opcode >= ISD::BUILTIN_OP_END ||
5295 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5296 Opcode == ISD::INTRINSIC_W_CHAIN ||
5297 Opcode == ISD::INTRINSIC_VOID) {
5298 // TODO: This can probably be removed once target code is audited. This
5299 // is here purely to reduce patch size and review complexity.
5300 if (!VT.isScalableVector()) {
5301 unsigned NumBits =
5302 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5303 if (NumBits > 1)
5304 FirstAnswer = std::max(FirstAnswer, NumBits);
5305 }
5306 }
5307
5308 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5309 // use this information.
5310 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5311 return std::max(FirstAnswer, Known.countMinSignBits());
5312}
5313
5315 unsigned Depth) const {
5316 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5317 return Op.getScalarValueSizeInBits() - SignBits + 1;
5318}
5319
5321 const APInt &DemandedElts,
5322 unsigned Depth) const {
5323 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5324 return Op.getScalarValueSizeInBits() - SignBits + 1;
5325}
5326
5328 unsigned Depth) const {
5329 // Early out for FREEZE.
5330 if (Op.getOpcode() == ISD::FREEZE)
5331 return true;
5332
5333 EVT VT = Op.getValueType();
5334 APInt DemandedElts = VT.isFixedLengthVector()
5336 : APInt(1, 1);
5337 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5338}
5339
5341 const APInt &DemandedElts,
5342 bool PoisonOnly,
5343 unsigned Depth) const {
5344 unsigned Opcode = Op.getOpcode();
5345
5346 // Early out for FREEZE.
5347 if (Opcode == ISD::FREEZE)
5348 return true;
5349
5350 if (Depth >= MaxRecursionDepth)
5351 return false; // Limit search depth.
5352
5353 if (isIntOrFPConstant(Op))
5354 return true;
5355
5356 switch (Opcode) {
5357 case ISD::CONDCODE:
5358 case ISD::VALUETYPE:
5359 case ISD::FrameIndex:
5361 case ISD::CopyFromReg:
5362 return true;
5363
5364 case ISD::POISON:
5365 return false;
5366
5367 case ISD::UNDEF:
5368 return PoisonOnly;
5369
5370 case ISD::BUILD_VECTOR:
5371 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5372 // this shouldn't affect the result.
5373 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5374 if (!DemandedElts[i])
5375 continue;
5377 Depth + 1))
5378 return false;
5379 }
5380 return true;
5381
5383 SDValue Src = Op.getOperand(0);
5384 if (Src.getValueType().isScalableVector())
5385 break;
5386 uint64_t Idx = Op.getConstantOperandVal(1);
5387 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5388 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5389 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5390 Depth + 1);
5391 }
5392
5393 case ISD::INSERT_SUBVECTOR: {
5394 if (Op.getValueType().isScalableVector())
5395 break;
5396 SDValue Src = Op.getOperand(0);
5397 SDValue Sub = Op.getOperand(1);
5398 uint64_t Idx = Op.getConstantOperandVal(2);
5399 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5400 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5401 APInt DemandedSrcElts = DemandedElts;
5402 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5403
5404 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5405 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5406 return false;
5407 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5408 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5409 return false;
5410 return true;
5411 }
5412
5414 SDValue Src = Op.getOperand(0);
5415 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5416 EVT SrcVT = Src.getValueType();
5417 if (SrcVT.isFixedLengthVector() && IndexC &&
5418 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5419 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5420 IndexC->getZExtValue());
5421 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5422 Depth + 1);
5423 }
5424 break;
5425 }
5426
5428 SDValue InVec = Op.getOperand(0);
5429 SDValue InVal = Op.getOperand(1);
5430 SDValue EltNo = Op.getOperand(2);
5431 EVT VT = InVec.getValueType();
5432 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5433 if (IndexC && VT.isFixedLengthVector() &&
5434 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5435 if (DemandedElts[IndexC->getZExtValue()] &&
5437 return false;
5438 APInt InVecDemandedElts = DemandedElts;
5439 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5440 if (!!InVecDemandedElts &&
5442 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5443 InVecDemandedElts, PoisonOnly, Depth + 1))
5444 return false;
5445 return true;
5446 }
5447 break;
5448 }
5449
5451 // Check upper (known undef) elements.
5452 if (DemandedElts.ugt(1) && !PoisonOnly)
5453 return false;
5454 // Check element zero.
5455 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5456 Op.getOperand(0), PoisonOnly, Depth + 1))
5457 return false;
5458 return true;
5459
5460 case ISD::SPLAT_VECTOR:
5461 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5462 Depth + 1);
5463
5464 case ISD::VECTOR_SHUFFLE: {
5465 APInt DemandedLHS, DemandedRHS;
5466 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5467 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5468 DemandedElts, DemandedLHS, DemandedRHS,
5469 /*AllowUndefElts=*/false))
5470 return false;
5471 if (!DemandedLHS.isZero() &&
5472 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5473 PoisonOnly, Depth + 1))
5474 return false;
5475 if (!DemandedRHS.isZero() &&
5476 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5477 PoisonOnly, Depth + 1))
5478 return false;
5479 return true;
5480 }
5481
5482 case ISD::SHL:
5483 case ISD::SRL:
5484 case ISD::SRA:
5485 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5486 // enough to check operand 0 if Op can't create undef/poison.
5487 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5488 /*ConsiderFlags*/ true, Depth) &&
5489 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5490 PoisonOnly, Depth + 1);
5491
5492 case ISD::BSWAP:
5493 case ISD::CTPOP:
5494 case ISD::BITREVERSE:
5495 case ISD::AND:
5496 case ISD::OR:
5497 case ISD::XOR:
5498 case ISD::ADD:
5499 case ISD::SUB:
5500 case ISD::MUL:
5501 case ISD::SADDSAT:
5502 case ISD::UADDSAT:
5503 case ISD::SSUBSAT:
5504 case ISD::USUBSAT:
5505 case ISD::SSHLSAT:
5506 case ISD::USHLSAT:
5507 case ISD::SMIN:
5508 case ISD::SMAX:
5509 case ISD::UMIN:
5510 case ISD::UMAX:
5511 case ISD::ZERO_EXTEND:
5512 case ISD::SIGN_EXTEND:
5513 case ISD::ANY_EXTEND:
5514 case ISD::TRUNCATE:
5515 case ISD::VSELECT: {
5516 // If Op can't create undef/poison and none of its operands are undef/poison
5517 // then Op is never undef/poison. A difference from the more common check
5518 // below, outside the switch, is that we handle elementwise operations for
5519 // which the DemandedElts mask is valid for all operands here.
5520 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5521 /*ConsiderFlags*/ true, Depth) &&
5522 all_of(Op->ops(), [&](SDValue V) {
5523 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5524 PoisonOnly, Depth + 1);
5525 });
5526 }
5527
5528 // TODO: Search for noundef attributes from library functions.
5529
5530 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5531
5532 default:
5533 // Allow the target to implement this method for its nodes.
5534 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5535 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5536 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5537 Op, DemandedElts, *this, PoisonOnly, Depth);
5538 break;
5539 }
5540
5541 // If Op can't create undef/poison and none of its operands are undef/poison
5542 // then Op is never undef/poison.
5543 // NOTE: TargetNodes can handle this in themselves in
5544 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5545 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5546 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5547 Depth) &&
5548 all_of(Op->ops(), [&](SDValue V) {
5549 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5550 });
5551}
5552
5554 bool ConsiderFlags,
5555 unsigned Depth) const {
5556 EVT VT = Op.getValueType();
5557 APInt DemandedElts = VT.isFixedLengthVector()
5559 : APInt(1, 1);
5560 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5561 Depth);
5562}
5563
5565 bool PoisonOnly, bool ConsiderFlags,
5566 unsigned Depth) const {
5567 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5568 return true;
5569
5570 unsigned Opcode = Op.getOpcode();
5571 switch (Opcode) {
5572 case ISD::AssertSext:
5573 case ISD::AssertZext:
5574 case ISD::AssertAlign:
5576 // Assertion nodes can create poison if the assertion fails.
5577 return true;
5578
5579 case ISD::FREEZE:
5583 case ISD::SADDSAT:
5584 case ISD::UADDSAT:
5585 case ISD::SSUBSAT:
5586 case ISD::USUBSAT:
5587 case ISD::MULHU:
5588 case ISD::MULHS:
5589 case ISD::AVGFLOORS:
5590 case ISD::AVGFLOORU:
5591 case ISD::AVGCEILS:
5592 case ISD::AVGCEILU:
5593 case ISD::ABDU:
5594 case ISD::ABDS:
5595 case ISD::SMIN:
5596 case ISD::SMAX:
5597 case ISD::SCMP:
5598 case ISD::UMIN:
5599 case ISD::UMAX:
5600 case ISD::UCMP:
5601 case ISD::AND:
5602 case ISD::XOR:
5603 case ISD::ROTL:
5604 case ISD::ROTR:
5605 case ISD::FSHL:
5606 case ISD::FSHR:
5607 case ISD::BSWAP:
5608 case ISD::CTTZ:
5609 case ISD::CTLZ:
5610 case ISD::CTPOP:
5611 case ISD::BITREVERSE:
5612 case ISD::PARITY:
5613 case ISD::SIGN_EXTEND:
5614 case ISD::TRUNCATE:
5618 case ISD::BITCAST:
5619 case ISD::BUILD_VECTOR:
5620 case ISD::BUILD_PAIR:
5621 case ISD::SPLAT_VECTOR:
5622 case ISD::FABS:
5623 return false;
5624
5625 case ISD::ABS:
5626 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5627 // Different to Intrinsic::abs.
5628 return false;
5629
5630 case ISD::ADDC:
5631 case ISD::SUBC:
5632 case ISD::ADDE:
5633 case ISD::SUBE:
5634 case ISD::SADDO:
5635 case ISD::SSUBO:
5636 case ISD::SMULO:
5637 case ISD::SADDO_CARRY:
5638 case ISD::SSUBO_CARRY:
5639 case ISD::UADDO:
5640 case ISD::USUBO:
5641 case ISD::UMULO:
5642 case ISD::UADDO_CARRY:
5643 case ISD::USUBO_CARRY:
5644 // No poison on result or overflow flags.
5645 return false;
5646
5647 case ISD::SELECT_CC:
5648 case ISD::SETCC: {
5649 // Integer setcc cannot create undef or poison.
5650 if (Op.getOperand(0).getValueType().isInteger())
5651 return false;
5652
5653 // FP compares are more complicated. They can create poison for nan/infinity
5654 // based on options and flags. The options and flags also cause special
5655 // nonan condition codes to be used. Those condition codes may be preserved
5656 // even if the nonan flag is dropped somewhere.
5657 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5658 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5659 return (unsigned)CCCode & 0x10U;
5660 }
5661
5662 case ISD::OR:
5663 case ISD::ZERO_EXTEND:
5664 case ISD::SELECT:
5665 case ISD::VSELECT:
5666 case ISD::ADD:
5667 case ISD::SUB:
5668 case ISD::MUL:
5669 case ISD::FNEG:
5670 case ISD::FADD:
5671 case ISD::FSUB:
5672 case ISD::FMUL:
5673 case ISD::FDIV:
5674 case ISD::FREM:
5675 case ISD::FCOPYSIGN:
5676 case ISD::FMA:
5677 case ISD::FMAD:
5678 case ISD::FMULADD:
5679 case ISD::FP_EXTEND:
5682 // No poison except from flags (which is handled above)
5683 return false;
5684
5685 case ISD::SHL:
5686 case ISD::SRL:
5687 case ISD::SRA:
5688 // If the max shift amount isn't in range, then the shift can
5689 // create poison.
5690 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5691
5694 // If the amount is zero then the result will be poison.
5695 // TODO: Add isKnownNeverZero DemandedElts handling.
5696 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5697
5699 // Check if we demand any upper (undef) elements.
5700 return !PoisonOnly && DemandedElts.ugt(1);
5701
5704 // Ensure that the element index is in bounds.
5705 EVT VecVT = Op.getOperand(0).getValueType();
5706 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5707 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5708 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5709 }
5710
5711 case ISD::VECTOR_SHUFFLE: {
5712 // Check for any demanded shuffle element that is undef.
5713 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5714 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5715 if (Elt < 0 && DemandedElts[Idx])
5716 return true;
5717 return false;
5718 }
5719
5721 return false;
5722
5723 default:
5724 // Allow the target to implement this method for its nodes.
5725 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5726 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5727 return TLI->canCreateUndefOrPoisonForTargetNode(
5728 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5729 break;
5730 }
5731
5732 // Be conservative and return true.
5733 return true;
5734}
5735
5736bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5737 unsigned Opcode = Op.getOpcode();
5738 if (Opcode == ISD::OR)
5739 return Op->getFlags().hasDisjoint() ||
5740 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5741 if (Opcode == ISD::XOR)
5742 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5743 return false;
5744}
5745
5747 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5748 (Op.isAnyAdd() || isADDLike(Op));
5749}
5750
5752 unsigned Depth) const {
5753 EVT VT = Op.getValueType();
5754
5755 // Since the number of lanes in a scalable vector is unknown at compile time,
5756 // we track one bit which is implicitly broadcast to all lanes. This means
5757 // that all lanes in a scalable vector are considered demanded.
5758 APInt DemandedElts = VT.isFixedLengthVector()
5760 : APInt(1, 1);
5761
5762 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5763}
5764
5766 bool SNaN, unsigned Depth) const {
5767 assert(!DemandedElts.isZero() && "No demanded elements");
5768
5769 // If we're told that NaNs won't happen, assume they won't.
5770 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5771 return true;
5772
5773 if (Depth >= MaxRecursionDepth)
5774 return false; // Limit search depth.
5775
5776 // If the value is a constant, we can obviously see if it is a NaN or not.
5778 return !C->getValueAPF().isNaN() ||
5779 (SNaN && !C->getValueAPF().isSignaling());
5780 }
5781
5782 unsigned Opcode = Op.getOpcode();
5783 switch (Opcode) {
5784 case ISD::FADD:
5785 case ISD::FSUB:
5786 case ISD::FMUL:
5787 case ISD::FDIV:
5788 case ISD::FREM:
5789 case ISD::FSIN:
5790 case ISD::FCOS:
5791 case ISD::FTAN:
5792 case ISD::FASIN:
5793 case ISD::FACOS:
5794 case ISD::FATAN:
5795 case ISD::FATAN2:
5796 case ISD::FSINH:
5797 case ISD::FCOSH:
5798 case ISD::FTANH:
5799 case ISD::FMA:
5800 case ISD::FMULADD:
5801 case ISD::FMAD: {
5802 if (SNaN)
5803 return true;
5804 // TODO: Need isKnownNeverInfinity
5805 return false;
5806 }
5807 case ISD::FCANONICALIZE:
5808 case ISD::FEXP:
5809 case ISD::FEXP2:
5810 case ISD::FEXP10:
5811 case ISD::FTRUNC:
5812 case ISD::FFLOOR:
5813 case ISD::FCEIL:
5814 case ISD::FROUND:
5815 case ISD::FROUNDEVEN:
5816 case ISD::LROUND:
5817 case ISD::LLROUND:
5818 case ISD::FRINT:
5819 case ISD::LRINT:
5820 case ISD::LLRINT:
5821 case ISD::FNEARBYINT:
5822 case ISD::FLDEXP: {
5823 if (SNaN)
5824 return true;
5825 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5826 }
5827 case ISD::FABS:
5828 case ISD::FNEG:
5829 case ISD::FCOPYSIGN: {
5830 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5831 }
5832 case ISD::SELECT:
5833 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5834 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5835 case ISD::FP_EXTEND:
5836 case ISD::FP_ROUND: {
5837 if (SNaN)
5838 return true;
5839 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5840 }
5841 case ISD::SINT_TO_FP:
5842 case ISD::UINT_TO_FP:
5843 return true;
5844 case ISD::FSQRT: // Need is known positive
5845 case ISD::FLOG:
5846 case ISD::FLOG2:
5847 case ISD::FLOG10:
5848 case ISD::FPOWI:
5849 case ISD::FPOW: {
5850 if (SNaN)
5851 return true;
5852 // TODO: Refine on operand
5853 return false;
5854 }
5855 case ISD::FMINNUM:
5856 case ISD::FMAXNUM:
5857 case ISD::FMINIMUMNUM:
5858 case ISD::FMAXIMUMNUM: {
5859 // Only one needs to be known not-nan, since it will be returned if the
5860 // other ends up being one.
5861 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5862 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5863 }
5864 case ISD::FMINNUM_IEEE:
5865 case ISD::FMAXNUM_IEEE: {
5866 if (SNaN)
5867 return true;
5868 // This can return a NaN if either operand is an sNaN, or if both operands
5869 // are NaN.
5870 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5871 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5872 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5873 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5874 }
5875 case ISD::FMINIMUM:
5876 case ISD::FMAXIMUM: {
5877 // TODO: Does this quiet or return the origina NaN as-is?
5878 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5879 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5880 }
5882 SDValue Src = Op.getOperand(0);
5883 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5884 EVT SrcVT = Src.getValueType();
5885 if (SrcVT.isFixedLengthVector() && Idx &&
5886 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5887 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5888 Idx->getZExtValue());
5889 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5890 }
5891 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5892 }
5894 SDValue Src = Op.getOperand(0);
5895 if (Src.getValueType().isFixedLengthVector()) {
5896 unsigned Idx = Op.getConstantOperandVal(1);
5897 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5898 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5899 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5900 }
5901 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5902 }
5903 case ISD::INSERT_SUBVECTOR: {
5904 SDValue BaseVector = Op.getOperand(0);
5905 SDValue SubVector = Op.getOperand(1);
5906 EVT BaseVectorVT = BaseVector.getValueType();
5907 if (BaseVectorVT.isFixedLengthVector()) {
5908 unsigned Idx = Op.getConstantOperandVal(2);
5909 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5910 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5911
5912 // Clear/Extract the bits at the position where the subvector will be
5913 // inserted.
5914 APInt DemandedMask =
5915 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5916 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5917 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5918
5919 bool NeverNaN = true;
5920 if (!DemandedSrcElts.isZero())
5921 NeverNaN &=
5922 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5923 if (NeverNaN && !DemandedSubElts.isZero())
5924 NeverNaN &=
5925 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5926 return NeverNaN;
5927 }
5928 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5929 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5930 }
5931 case ISD::BUILD_VECTOR: {
5932 unsigned NumElts = Op.getNumOperands();
5933 for (unsigned I = 0; I != NumElts; ++I)
5934 if (DemandedElts[I] &&
5935 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
5936 return false;
5937 return true;
5938 }
5939 case ISD::AssertNoFPClass: {
5940 FPClassTest NoFPClass =
5941 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
5942 if ((NoFPClass & fcNan) == fcNan)
5943 return true;
5944 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
5945 return true;
5946 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5947 }
5948 default:
5949 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5950 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
5951 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
5952 Depth);
5953 }
5954
5955 return false;
5956 }
5957}
5958
5960 assert(Op.getValueType().isFloatingPoint() &&
5961 "Floating point type expected");
5962
5963 // If the value is a constant, we can obviously see if it is a zero or not.
5965 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5966}
5967
5969 if (Depth >= MaxRecursionDepth)
5970 return false; // Limit search depth.
5971
5972 assert(!Op.getValueType().isFloatingPoint() &&
5973 "Floating point types unsupported - use isKnownNeverZeroFloat");
5974
5975 // If the value is a constant, we can obviously see if it is a zero or not.
5977 [](ConstantSDNode *C) { return !C->isZero(); }))
5978 return true;
5979
5980 // TODO: Recognize more cases here. Most of the cases are also incomplete to
5981 // some degree.
5982 switch (Op.getOpcode()) {
5983 default:
5984 break;
5985
5986 case ISD::OR:
5987 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
5988 isKnownNeverZero(Op.getOperand(0), Depth + 1);
5989
5990 case ISD::VSELECT:
5991 case ISD::SELECT:
5992 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
5993 isKnownNeverZero(Op.getOperand(2), Depth + 1);
5994
5995 case ISD::SHL: {
5996 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
5997 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
5998 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
5999 // 1 << X is never zero.
6000 if (ValKnown.One[0])
6001 return true;
6002 // If max shift cnt of known ones is non-zero, result is non-zero.
6003 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6004 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6005 !ValKnown.One.shl(MaxCnt).isZero())
6006 return true;
6007 break;
6008 }
6009 case ISD::UADDSAT:
6010 case ISD::UMAX:
6011 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6012 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6013
6014 // For smin/smax: If either operand is known negative/positive
6015 // respectively we don't need the other to be known at all.
6016 case ISD::SMAX: {
6017 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6018 if (Op1.isStrictlyPositive())
6019 return true;
6020
6021 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6022 if (Op0.isStrictlyPositive())
6023 return true;
6024
6025 if (Op1.isNonZero() && Op0.isNonZero())
6026 return true;
6027
6028 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6029 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6030 }
6031 case ISD::SMIN: {
6032 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6033 if (Op1.isNegative())
6034 return true;
6035
6036 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6037 if (Op0.isNegative())
6038 return true;
6039
6040 if (Op1.isNonZero() && Op0.isNonZero())
6041 return true;
6042
6043 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6044 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6045 }
6046 case ISD::UMIN:
6047 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6048 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6049
6050 case ISD::ROTL:
6051 case ISD::ROTR:
6052 case ISD::BITREVERSE:
6053 case ISD::BSWAP:
6054 case ISD::CTPOP:
6055 case ISD::ABS:
6056 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6057
6058 case ISD::SRA:
6059 case ISD::SRL: {
6060 if (Op->getFlags().hasExact())
6061 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6062 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6063 if (ValKnown.isNegative())
6064 return true;
6065 // If max shift cnt of known ones is non-zero, result is non-zero.
6066 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6067 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6068 !ValKnown.One.lshr(MaxCnt).isZero())
6069 return true;
6070 break;
6071 }
6072 case ISD::UDIV:
6073 case ISD::SDIV:
6074 // div exact can only produce a zero if the dividend is zero.
6075 // TODO: For udiv this is also true if Op1 u<= Op0
6076 if (Op->getFlags().hasExact())
6077 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6078 break;
6079
6080 case ISD::ADD:
6081 if (Op->getFlags().hasNoUnsignedWrap())
6082 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6083 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6084 return true;
6085 // TODO: There are a lot more cases we can prove for add.
6086 break;
6087
6088 case ISD::SUB: {
6089 if (isNullConstant(Op.getOperand(0)))
6090 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6091
6092 std::optional<bool> ne =
6093 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6094 computeKnownBits(Op.getOperand(1), Depth + 1));
6095 return ne && *ne;
6096 }
6097
6098 case ISD::MUL:
6099 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6100 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6101 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6102 return true;
6103 break;
6104
6105 case ISD::ZERO_EXTEND:
6106 case ISD::SIGN_EXTEND:
6107 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6108 case ISD::VSCALE: {
6110 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6111 ConstantRange CR =
6112 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6113 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6114 return true;
6115 break;
6116 }
6117 }
6118
6120}
6121
6123 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6124 return !C1->isNegative();
6125
6126 switch (Op.getOpcode()) {
6127 case ISD::FABS:
6128 case ISD::FEXP:
6129 case ISD::FEXP2:
6130 case ISD::FEXP10:
6131 return true;
6132 default:
6133 return false;
6134 }
6135
6136 llvm_unreachable("covered opcode switch");
6137}
6138
6140 // Check the obvious case.
6141 if (A == B) return true;
6142
6143 // For negative and positive zero.
6146 if (CA->isZero() && CB->isZero()) return true;
6147
6148 // Otherwise they may not be equal.
6149 return false;
6150}
6151
6152// Only bits set in Mask must be negated, other bits may be arbitrary.
6154 if (isBitwiseNot(V, AllowUndefs))
6155 return V.getOperand(0);
6156
6157 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6158 // bits in the non-extended part.
6159 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6160 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6161 return SDValue();
6162 SDValue ExtArg = V.getOperand(0);
6163 if (ExtArg.getScalarValueSizeInBits() >=
6164 MaskC->getAPIntValue().getActiveBits() &&
6165 isBitwiseNot(ExtArg, AllowUndefs) &&
6166 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6167 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6168 return ExtArg.getOperand(0).getOperand(0);
6169 return SDValue();
6170}
6171
6173 // Match masked merge pattern (X & ~M) op (Y & M)
6174 // Including degenerate case (X & ~M) op M
6175 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6176 SDValue Other) {
6177 if (SDValue NotOperand =
6178 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6179 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6180 NotOperand->getOpcode() == ISD::TRUNCATE)
6181 NotOperand = NotOperand->getOperand(0);
6182
6183 if (Other == NotOperand)
6184 return true;
6185 if (Other->getOpcode() == ISD::AND)
6186 return NotOperand == Other->getOperand(0) ||
6187 NotOperand == Other->getOperand(1);
6188 }
6189 return false;
6190 };
6191
6192 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6193 A = A->getOperand(0);
6194
6195 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6196 B = B->getOperand(0);
6197
6198 if (A->getOpcode() == ISD::AND)
6199 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6200 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6201 return false;
6202}
6203
6204// FIXME: unify with llvm::haveNoCommonBitsSet.
6206 assert(A.getValueType() == B.getValueType() &&
6207 "Values must have the same type");
6210 return true;
6213}
6214
6215static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6216 SelectionDAG &DAG) {
6217 if (cast<ConstantSDNode>(Step)->isZero())
6218 return DAG.getConstant(0, DL, VT);
6219
6220 return SDValue();
6221}
6222
6225 SelectionDAG &DAG) {
6226 int NumOps = Ops.size();
6227 assert(NumOps != 0 && "Can't build an empty vector!");
6228 assert(!VT.isScalableVector() &&
6229 "BUILD_VECTOR cannot be used with scalable types");
6230 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6231 "Incorrect element count in BUILD_VECTOR!");
6232
6233 // BUILD_VECTOR of UNDEFs is UNDEF.
6234 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6235 return DAG.getUNDEF(VT);
6236
6237 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6238 SDValue IdentitySrc;
6239 bool IsIdentity = true;
6240 for (int i = 0; i != NumOps; ++i) {
6242 Ops[i].getOperand(0).getValueType() != VT ||
6243 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6244 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6245 Ops[i].getConstantOperandAPInt(1) != i) {
6246 IsIdentity = false;
6247 break;
6248 }
6249 IdentitySrc = Ops[i].getOperand(0);
6250 }
6251 if (IsIdentity)
6252 return IdentitySrc;
6253
6254 return SDValue();
6255}
6256
6257/// Try to simplify vector concatenation to an input value, undef, or build
6258/// vector.
6261 SelectionDAG &DAG) {
6262 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6264 [Ops](SDValue Op) {
6265 return Ops[0].getValueType() == Op.getValueType();
6266 }) &&
6267 "Concatenation of vectors with inconsistent value types!");
6268 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6269 VT.getVectorElementCount() &&
6270 "Incorrect element count in vector concatenation!");
6271
6272 if (Ops.size() == 1)
6273 return Ops[0];
6274
6275 // Concat of UNDEFs is UNDEF.
6276 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6277 return DAG.getUNDEF(VT);
6278
6279 // Scan the operands and look for extract operations from a single source
6280 // that correspond to insertion at the same location via this concatenation:
6281 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6282 SDValue IdentitySrc;
6283 bool IsIdentity = true;
6284 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6285 SDValue Op = Ops[i];
6286 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6287 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6288 Op.getOperand(0).getValueType() != VT ||
6289 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6290 Op.getConstantOperandVal(1) != IdentityIndex) {
6291 IsIdentity = false;
6292 break;
6293 }
6294 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6295 "Unexpected identity source vector for concat of extracts");
6296 IdentitySrc = Op.getOperand(0);
6297 }
6298 if (IsIdentity) {
6299 assert(IdentitySrc && "Failed to set source vector of extracts");
6300 return IdentitySrc;
6301 }
6302
6303 // The code below this point is only designed to work for fixed width
6304 // vectors, so we bail out for now.
6305 if (VT.isScalableVector())
6306 return SDValue();
6307
6308 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6309 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6310 // BUILD_VECTOR.
6311 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6312 EVT SVT = VT.getScalarType();
6314 for (SDValue Op : Ops) {
6315 EVT OpVT = Op.getValueType();
6316 if (Op.isUndef())
6317 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6318 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6319 Elts.append(Op->op_begin(), Op->op_end());
6320 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6321 OpVT.getVectorNumElements() == 1 &&
6322 isNullConstant(Op.getOperand(2)))
6323 Elts.push_back(Op.getOperand(1));
6324 else
6325 return SDValue();
6326 }
6327
6328 // BUILD_VECTOR requires all inputs to be of the same type, find the
6329 // maximum type and extend them all.
6330 for (SDValue Op : Elts)
6331 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6332
6333 if (SVT.bitsGT(VT.getScalarType())) {
6334 for (SDValue &Op : Elts) {
6335 if (Op.isUndef())
6336 Op = DAG.getUNDEF(SVT);
6337 else
6338 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6339 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6340 : DAG.getSExtOrTrunc(Op, DL, SVT);
6341 }
6342 }
6343
6344 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6345 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6346 return V;
6347}
6348
6349/// Gets or creates the specified node.
6350SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6351 SDVTList VTs = getVTList(VT);
6353 AddNodeIDNode(ID, Opcode, VTs, {});
6354 void *IP = nullptr;
6355 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6356 return SDValue(E, 0);
6357
6358 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6359 CSEMap.InsertNode(N, IP);
6360
6361 InsertNode(N);
6362 SDValue V = SDValue(N, 0);
6363 NewSDValueDbgMsg(V, "Creating new node: ", this);
6364 return V;
6365}
6366
6367SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6368 SDValue N1) {
6369 SDNodeFlags Flags;
6370 if (Inserter)
6371 Flags = Inserter->getFlags();
6372 return getNode(Opcode, DL, VT, N1, Flags);
6373}
6374
6375SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6376 SDValue N1, const SDNodeFlags Flags) {
6377 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6378
6379 // Constant fold unary operations with a vector integer or float operand.
6380 switch (Opcode) {
6381 default:
6382 // FIXME: Entirely reasonable to perform folding of other unary
6383 // operations here as the need arises.
6384 break;
6385 case ISD::FNEG:
6386 case ISD::FABS:
6387 case ISD::FCEIL:
6388 case ISD::FTRUNC:
6389 case ISD::FFLOOR:
6390 case ISD::FP_EXTEND:
6391 case ISD::FP_TO_SINT:
6392 case ISD::FP_TO_UINT:
6393 case ISD::FP_TO_FP16:
6394 case ISD::FP_TO_BF16:
6395 case ISD::TRUNCATE:
6396 case ISD::ANY_EXTEND:
6397 case ISD::ZERO_EXTEND:
6398 case ISD::SIGN_EXTEND:
6399 case ISD::UINT_TO_FP:
6400 case ISD::SINT_TO_FP:
6401 case ISD::FP16_TO_FP:
6402 case ISD::BF16_TO_FP:
6403 case ISD::BITCAST:
6404 case ISD::ABS:
6405 case ISD::BITREVERSE:
6406 case ISD::BSWAP:
6407 case ISD::CTLZ:
6409 case ISD::CTTZ:
6411 case ISD::CTPOP:
6412 case ISD::STEP_VECTOR: {
6413 SDValue Ops = {N1};
6414 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6415 return Fold;
6416 }
6417 }
6418
6419 unsigned OpOpcode = N1.getNode()->getOpcode();
6420 switch (Opcode) {
6421 case ISD::STEP_VECTOR:
6422 assert(VT.isScalableVector() &&
6423 "STEP_VECTOR can only be used with scalable types");
6424 assert(OpOpcode == ISD::TargetConstant &&
6425 VT.getVectorElementType() == N1.getValueType() &&
6426 "Unexpected step operand");
6427 break;
6428 case ISD::FREEZE:
6429 assert(VT == N1.getValueType() && "Unexpected VT!");
6430 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6431 return N1;
6432 break;
6433 case ISD::TokenFactor:
6434 case ISD::MERGE_VALUES:
6436 return N1; // Factor, merge or concat of one node? No need.
6437 case ISD::BUILD_VECTOR: {
6438 // Attempt to simplify BUILD_VECTOR.
6439 SDValue Ops[] = {N1};
6440 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6441 return V;
6442 break;
6443 }
6444 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6445 case ISD::FP_EXTEND:
6447 "Invalid FP cast!");
6448 if (N1.getValueType() == VT) return N1; // noop conversion.
6449 assert((!VT.isVector() || VT.getVectorElementCount() ==
6451 "Vector element count mismatch!");
6452 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6453 if (N1.isUndef())
6454 return getUNDEF(VT);
6455 break;
6456 case ISD::FP_TO_SINT:
6457 case ISD::FP_TO_UINT:
6458 if (N1.isUndef())
6459 return getUNDEF(VT);
6460 break;
6461 case ISD::SINT_TO_FP:
6462 case ISD::UINT_TO_FP:
6463 // [us]itofp(undef) = 0, because the result value is bounded.
6464 if (N1.isUndef())
6465 return getConstantFP(0.0, DL, VT);
6466 break;
6467 case ISD::SIGN_EXTEND:
6468 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6469 "Invalid SIGN_EXTEND!");
6470 assert(VT.isVector() == N1.getValueType().isVector() &&
6471 "SIGN_EXTEND result type type should be vector iff the operand "
6472 "type is vector!");
6473 if (N1.getValueType() == VT) return N1; // noop extension
6474 assert((!VT.isVector() || VT.getVectorElementCount() ==
6476 "Vector element count mismatch!");
6477 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6478 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6479 SDNodeFlags Flags;
6480 if (OpOpcode == ISD::ZERO_EXTEND)
6481 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6482 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6483 transferDbgValues(N1, NewVal);
6484 return NewVal;
6485 }
6486
6487 if (OpOpcode == ISD::POISON)
6488 return getPOISON(VT);
6489
6490 if (N1.isUndef())
6491 // sext(undef) = 0, because the top bits will all be the same.
6492 return getConstant(0, DL, VT);
6493
6494 // Skip unnecessary sext_inreg pattern:
6495 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6496 if (OpOpcode == ISD::TRUNCATE) {
6497 SDValue OpOp = N1.getOperand(0);
6498 if (OpOp.getValueType() == VT) {
6499 unsigned NumSignExtBits =
6501 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6502 transferDbgValues(N1, OpOp);
6503 return OpOp;
6504 }
6505 }
6506 }
6507 break;
6508 case ISD::ZERO_EXTEND:
6509 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6510 "Invalid ZERO_EXTEND!");
6511 assert(VT.isVector() == N1.getValueType().isVector() &&
6512 "ZERO_EXTEND result type type should be vector iff the operand "
6513 "type is vector!");
6514 if (N1.getValueType() == VT) return N1; // noop extension
6515 assert((!VT.isVector() || VT.getVectorElementCount() ==
6517 "Vector element count mismatch!");
6518 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6519 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6520 SDNodeFlags Flags;
6521 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6522 SDValue NewVal =
6523 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6524 transferDbgValues(N1, NewVal);
6525 return NewVal;
6526 }
6527
6528 if (OpOpcode == ISD::POISON)
6529 return getPOISON(VT);
6530
6531 if (N1.isUndef())
6532 // zext(undef) = 0, because the top bits will be zero.
6533 return getConstant(0, DL, VT);
6534
6535 // Skip unnecessary zext_inreg pattern:
6536 // (zext (trunc x)) -> x iff the upper bits are known zero.
6537 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6538 // use to recognise zext_inreg patterns.
6539 if (OpOpcode == ISD::TRUNCATE) {
6540 SDValue OpOp = N1.getOperand(0);
6541 if (OpOp.getValueType() == VT) {
6542 if (OpOp.getOpcode() != ISD::AND) {
6545 if (MaskedValueIsZero(OpOp, HiBits)) {
6546 transferDbgValues(N1, OpOp);
6547 return OpOp;
6548 }
6549 }
6550 }
6551 }
6552 break;
6553 case ISD::ANY_EXTEND:
6554 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6555 "Invalid ANY_EXTEND!");
6556 assert(VT.isVector() == N1.getValueType().isVector() &&
6557 "ANY_EXTEND result type type should be vector iff the operand "
6558 "type is vector!");
6559 if (N1.getValueType() == VT) return N1; // noop extension
6560 assert((!VT.isVector() || VT.getVectorElementCount() ==
6562 "Vector element count mismatch!");
6563 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6564
6565 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6566 OpOpcode == ISD::ANY_EXTEND) {
6567 SDNodeFlags Flags;
6568 if (OpOpcode == ISD::ZERO_EXTEND)
6569 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6570 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6571 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6572 }
6573 if (N1.isUndef())
6574 return getUNDEF(VT);
6575
6576 // (ext (trunc x)) -> x
6577 if (OpOpcode == ISD::TRUNCATE) {
6578 SDValue OpOp = N1.getOperand(0);
6579 if (OpOp.getValueType() == VT) {
6580 transferDbgValues(N1, OpOp);
6581 return OpOp;
6582 }
6583 }
6584 break;
6585 case ISD::TRUNCATE:
6586 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6587 "Invalid TRUNCATE!");
6588 assert(VT.isVector() == N1.getValueType().isVector() &&
6589 "TRUNCATE result type type should be vector iff the operand "
6590 "type is vector!");
6591 if (N1.getValueType() == VT) return N1; // noop truncate
6592 assert((!VT.isVector() || VT.getVectorElementCount() ==
6594 "Vector element count mismatch!");
6595 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6596 if (OpOpcode == ISD::TRUNCATE)
6597 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6598 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6599 OpOpcode == ISD::ANY_EXTEND) {
6600 // If the source is smaller than the dest, we still need an extend.
6602 VT.getScalarType())) {
6603 SDNodeFlags Flags;
6604 if (OpOpcode == ISD::ZERO_EXTEND)
6605 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6606 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6607 }
6608 if (N1.getOperand(0).getValueType().bitsGT(VT))
6609 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6610 return N1.getOperand(0);
6611 }
6612 if (N1.isUndef())
6613 return getUNDEF(VT);
6614 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6615 return getVScale(DL, VT,
6617 break;
6621 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6622 assert(N1.getValueType().bitsLE(VT) &&
6623 "The input must be the same size or smaller than the result.");
6626 "The destination vector type must have fewer lanes than the input.");
6627 break;
6628 case ISD::ABS:
6629 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6630 if (N1.isUndef())
6631 return getConstant(0, DL, VT);
6632 break;
6633 case ISD::BSWAP:
6634 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6635 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6636 "BSWAP types must be a multiple of 16 bits!");
6637 if (N1.isUndef())
6638 return getUNDEF(VT);
6639 // bswap(bswap(X)) -> X.
6640 if (OpOpcode == ISD::BSWAP)
6641 return N1.getOperand(0);
6642 break;
6643 case ISD::BITREVERSE:
6644 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6645 if (N1.isUndef())
6646 return getUNDEF(VT);
6647 break;
6648 case ISD::BITCAST:
6650 "Cannot BITCAST between types of different sizes!");
6651 if (VT == N1.getValueType()) return N1; // noop conversion.
6652 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6653 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6654 if (N1.isUndef())
6655 return getUNDEF(VT);
6656 break;
6658 assert(VT.isVector() && !N1.getValueType().isVector() &&
6659 (VT.getVectorElementType() == N1.getValueType() ||
6661 N1.getValueType().isInteger() &&
6663 "Illegal SCALAR_TO_VECTOR node!");
6664 if (N1.isUndef())
6665 return getUNDEF(VT);
6666 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6667 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6669 N1.getConstantOperandVal(1) == 0 &&
6670 N1.getOperand(0).getValueType() == VT)
6671 return N1.getOperand(0);
6672 break;
6673 case ISD::FNEG:
6674 // Negation of an unknown bag of bits is still completely undefined.
6675 if (N1.isUndef())
6676 return getUNDEF(VT);
6677
6678 if (OpOpcode == ISD::FNEG) // --X -> X
6679 return N1.getOperand(0);
6680 break;
6681 case ISD::FABS:
6682 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6683 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6684 break;
6685 case ISD::VSCALE:
6686 assert(VT == N1.getValueType() && "Unexpected VT!");
6687 break;
6688 case ISD::CTPOP:
6689 if (N1.getValueType().getScalarType() == MVT::i1)
6690 return N1;
6691 break;
6692 case ISD::CTLZ:
6693 case ISD::CTTZ:
6694 if (N1.getValueType().getScalarType() == MVT::i1)
6695 return getNOT(DL, N1, N1.getValueType());
6696 break;
6697 case ISD::VECREDUCE_ADD:
6698 if (N1.getValueType().getScalarType() == MVT::i1)
6699 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6700 break;
6701 case ISD::VECREDUCE_SMIN:
6702 case ISD::VECREDUCE_UMAX:
6703 if (N1.getValueType().getScalarType() == MVT::i1)
6704 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6705 break;
6706 case ISD::VECREDUCE_SMAX:
6707 case ISD::VECREDUCE_UMIN:
6708 if (N1.getValueType().getScalarType() == MVT::i1)
6709 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6710 break;
6711 case ISD::SPLAT_VECTOR:
6712 assert(VT.isVector() && "Wrong return type!");
6713 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6714 // that for now.
6716 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6718 N1.getValueType().isInteger() &&
6720 "Wrong operand type!");
6721 break;
6722 }
6723
6724 SDNode *N;
6725 SDVTList VTs = getVTList(VT);
6726 SDValue Ops[] = {N1};
6727 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6729 AddNodeIDNode(ID, Opcode, VTs, Ops);
6730 void *IP = nullptr;
6731 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6732 E->intersectFlagsWith(Flags);
6733 return SDValue(E, 0);
6734 }
6735
6736 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6737 N->setFlags(Flags);
6738 createOperands(N, Ops);
6739 CSEMap.InsertNode(N, IP);
6740 } else {
6741 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6742 createOperands(N, Ops);
6743 }
6744
6745 InsertNode(N);
6746 SDValue V = SDValue(N, 0);
6747 NewSDValueDbgMsg(V, "Creating new node: ", this);
6748 return V;
6749}
6750
6751static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6752 const APInt &C2) {
6753 switch (Opcode) {
6754 case ISD::ADD: return C1 + C2;
6755 case ISD::SUB: return C1 - C2;
6756 case ISD::MUL: return C1 * C2;
6757 case ISD::AND: return C1 & C2;
6758 case ISD::OR: return C1 | C2;
6759 case ISD::XOR: return C1 ^ C2;
6760 case ISD::SHL: return C1 << C2;
6761 case ISD::SRL: return C1.lshr(C2);
6762 case ISD::SRA: return C1.ashr(C2);
6763 case ISD::ROTL: return C1.rotl(C2);
6764 case ISD::ROTR: return C1.rotr(C2);
6765 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6766 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6767 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6768 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6769 case ISD::SADDSAT: return C1.sadd_sat(C2);
6770 case ISD::UADDSAT: return C1.uadd_sat(C2);
6771 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6772 case ISD::USUBSAT: return C1.usub_sat(C2);
6773 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6774 case ISD::USHLSAT: return C1.ushl_sat(C2);
6775 case ISD::UDIV:
6776 if (!C2.getBoolValue())
6777 break;
6778 return C1.udiv(C2);
6779 case ISD::UREM:
6780 if (!C2.getBoolValue())
6781 break;
6782 return C1.urem(C2);
6783 case ISD::SDIV:
6784 if (!C2.getBoolValue())
6785 break;
6786 return C1.sdiv(C2);
6787 case ISD::SREM:
6788 if (!C2.getBoolValue())
6789 break;
6790 return C1.srem(C2);
6791 case ISD::AVGFLOORS:
6792 return APIntOps::avgFloorS(C1, C2);
6793 case ISD::AVGFLOORU:
6794 return APIntOps::avgFloorU(C1, C2);
6795 case ISD::AVGCEILS:
6796 return APIntOps::avgCeilS(C1, C2);
6797 case ISD::AVGCEILU:
6798 return APIntOps::avgCeilU(C1, C2);
6799 case ISD::ABDS:
6800 return APIntOps::abds(C1, C2);
6801 case ISD::ABDU:
6802 return APIntOps::abdu(C1, C2);
6803 case ISD::MULHS:
6804 return APIntOps::mulhs(C1, C2);
6805 case ISD::MULHU:
6806 return APIntOps::mulhu(C1, C2);
6807 }
6808 return std::nullopt;
6809}
6810// Handle constant folding with UNDEF.
6811// TODO: Handle more cases.
6812static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6813 bool IsUndef1, const APInt &C2,
6814 bool IsUndef2) {
6815 if (!(IsUndef1 || IsUndef2))
6816 return FoldValue(Opcode, C1, C2);
6817
6818 // Fold and(x, undef) -> 0
6819 // Fold mul(x, undef) -> 0
6820 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6821 return APInt::getZero(C1.getBitWidth());
6822
6823 return std::nullopt;
6824}
6825
6827 const GlobalAddressSDNode *GA,
6828 const SDNode *N2) {
6829 if (GA->getOpcode() != ISD::GlobalAddress)
6830 return SDValue();
6831 if (!TLI->isOffsetFoldingLegal(GA))
6832 return SDValue();
6833 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6834 if (!C2)
6835 return SDValue();
6836 int64_t Offset = C2->getSExtValue();
6837 switch (Opcode) {
6838 case ISD::ADD:
6839 case ISD::PTRADD:
6840 break;
6841 case ISD::SUB: Offset = -uint64_t(Offset); break;
6842 default: return SDValue();
6843 }
6844 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6845 GA->getOffset() + uint64_t(Offset));
6846}
6847
6849 switch (Opcode) {
6850 case ISD::SDIV:
6851 case ISD::UDIV:
6852 case ISD::SREM:
6853 case ISD::UREM: {
6854 // If a divisor is zero/undef or any element of a divisor vector is
6855 // zero/undef, the whole op is undef.
6856 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6857 SDValue Divisor = Ops[1];
6858 if (Divisor.isUndef() || isNullConstant(Divisor))
6859 return true;
6860
6861 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6862 llvm::any_of(Divisor->op_values(),
6863 [](SDValue V) { return V.isUndef() ||
6864 isNullConstant(V); });
6865 // TODO: Handle signed overflow.
6866 }
6867 // TODO: Handle oversized shifts.
6868 default:
6869 return false;
6870 }
6871}
6872
6875 SDNodeFlags Flags) {
6876 // If the opcode is a target-specific ISD node, there's nothing we can
6877 // do here and the operand rules may not line up with the below, so
6878 // bail early.
6879 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6880 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6881 // foldCONCAT_VECTORS in getNode before this is called.
6882 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6883 return SDValue();
6884
6885 unsigned NumOps = Ops.size();
6886 if (NumOps == 0)
6887 return SDValue();
6888
6889 if (isUndef(Opcode, Ops))
6890 return getUNDEF(VT);
6891
6892 // Handle unary special cases.
6893 if (NumOps == 1) {
6894 SDValue N1 = Ops[0];
6895
6896 // Constant fold unary operations with an integer constant operand. Even
6897 // opaque constant will be folded, because the folding of unary operations
6898 // doesn't create new constants with different values. Nevertheless, the
6899 // opaque flag is preserved during folding to prevent future folding with
6900 // other constants.
6901 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6902 const APInt &Val = C->getAPIntValue();
6903 switch (Opcode) {
6904 case ISD::SIGN_EXTEND:
6905 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6906 C->isTargetOpcode(), C->isOpaque());
6907 case ISD::TRUNCATE:
6908 if (C->isOpaque())
6909 break;
6910 [[fallthrough]];
6911 case ISD::ZERO_EXTEND:
6912 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6913 C->isTargetOpcode(), C->isOpaque());
6914 case ISD::ANY_EXTEND:
6915 // Some targets like RISCV prefer to sign extend some types.
6916 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6917 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6918 C->isTargetOpcode(), C->isOpaque());
6919 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6920 C->isTargetOpcode(), C->isOpaque());
6921 case ISD::ABS:
6922 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6923 C->isOpaque());
6924 case ISD::BITREVERSE:
6925 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6926 C->isOpaque());
6927 case ISD::BSWAP:
6928 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6929 C->isOpaque());
6930 case ISD::CTPOP:
6931 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
6932 C->isOpaque());
6933 case ISD::CTLZ:
6935 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
6936 C->isOpaque());
6937 case ISD::CTTZ:
6939 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
6940 C->isOpaque());
6941 case ISD::UINT_TO_FP:
6942 case ISD::SINT_TO_FP: {
6944 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
6946 return getConstantFP(FPV, DL, VT);
6947 }
6948 case ISD::FP16_TO_FP:
6949 case ISD::BF16_TO_FP: {
6950 bool Ignored;
6951 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
6952 : APFloat::BFloat(),
6953 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
6954
6955 // This can return overflow, underflow, or inexact; we don't care.
6956 // FIXME need to be more flexible about rounding mode.
6958 &Ignored);
6959 return getConstantFP(FPV, DL, VT);
6960 }
6961 case ISD::STEP_VECTOR:
6962 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
6963 return V;
6964 break;
6965 case ISD::BITCAST:
6966 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
6967 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
6968 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
6969 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
6970 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
6971 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
6972 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
6973 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
6974 break;
6975 }
6976 }
6977
6978 // Constant fold unary operations with a floating point constant operand.
6979 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
6980 APFloat V = C->getValueAPF(); // make copy
6981 switch (Opcode) {
6982 case ISD::FNEG:
6983 V.changeSign();
6984 return getConstantFP(V, DL, VT);
6985 case ISD::FABS:
6986 V.clearSign();
6987 return getConstantFP(V, DL, VT);
6988 case ISD::FCEIL: {
6989 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
6991 return getConstantFP(V, DL, VT);
6992 return SDValue();
6993 }
6994 case ISD::FTRUNC: {
6995 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
6997 return getConstantFP(V, DL, VT);
6998 return SDValue();
6999 }
7000 case ISD::FFLOOR: {
7001 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7003 return getConstantFP(V, DL, VT);
7004 return SDValue();
7005 }
7006 case ISD::FP_EXTEND: {
7007 bool ignored;
7008 // This can return overflow, underflow, or inexact; we don't care.
7009 // FIXME need to be more flexible about rounding mode.
7010 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7011 &ignored);
7012 return getConstantFP(V, DL, VT);
7013 }
7014 case ISD::FP_TO_SINT:
7015 case ISD::FP_TO_UINT: {
7016 bool ignored;
7017 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7018 // FIXME need to be more flexible about rounding mode.
7020 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7021 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7022 break;
7023 return getConstant(IntVal, DL, VT);
7024 }
7025 case ISD::FP_TO_FP16:
7026 case ISD::FP_TO_BF16: {
7027 bool Ignored;
7028 // This can return overflow, underflow, or inexact; we don't care.
7029 // FIXME need to be more flexible about rounding mode.
7030 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7031 : APFloat::BFloat(),
7033 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7034 }
7035 case ISD::BITCAST:
7036 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7037 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7038 VT);
7039 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7040 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7041 VT);
7042 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7043 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7044 VT);
7045 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7046 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7047 break;
7048 }
7049 }
7050
7051 // Early-out if we failed to constant fold a bitcast.
7052 if (Opcode == ISD::BITCAST)
7053 return SDValue();
7054 }
7055
7056 // Handle binops special cases.
7057 if (NumOps == 2) {
7058 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7059 return CFP;
7060
7061 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7062 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7063 if (C1->isOpaque() || C2->isOpaque())
7064 return SDValue();
7065
7066 std::optional<APInt> FoldAttempt =
7067 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7068 if (!FoldAttempt)
7069 return SDValue();
7070
7071 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7072 assert((!Folded || !VT.isVector()) &&
7073 "Can't fold vectors ops with scalar operands");
7074 return Folded;
7075 }
7076 }
7077
7078 // fold (add Sym, c) -> Sym+c
7080 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7081 if (TLI->isCommutativeBinOp(Opcode))
7083 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7084
7085 // fold (sext_in_reg c1) -> c2
7086 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7087 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7088
7089 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7090 unsigned FromBits = EVT.getScalarSizeInBits();
7091 Val <<= Val.getBitWidth() - FromBits;
7092 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7093 return getConstant(Val, DL, ConstantVT);
7094 };
7095
7096 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7097 const APInt &Val = C1->getAPIntValue();
7098 return SignExtendInReg(Val, VT);
7099 }
7100
7102 SmallVector<SDValue, 8> ScalarOps;
7103 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7104 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7105 SDValue Op = Ops[0].getOperand(I);
7106 if (Op.isUndef()) {
7107 ScalarOps.push_back(getUNDEF(OpVT));
7108 continue;
7109 }
7110 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7111 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7112 }
7113 return getBuildVector(VT, DL, ScalarOps);
7114 }
7115
7116 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7117 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7118 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7119 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7120 Ops[0].getOperand(0).getValueType()));
7121 }
7122 }
7123
7124 // Handle fshl/fshr special cases.
7125 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7126 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7127 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7128 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7129
7130 if (C1 && C2 && C3) {
7131 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7132 return SDValue();
7133 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7134 &V3 = C3->getAPIntValue();
7135
7136 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7137 : APIntOps::fshr(V1, V2, V3);
7138 return getConstant(FoldedVal, DL, VT);
7139 }
7140 }
7141
7142 // Handle fma/fmad special cases.
7143 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7144 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7145 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7146 Ops[2].getValueType() == VT && "FMA types must match!");
7150 if (C1 && C2 && C3) {
7151 APFloat V1 = C1->getValueAPF();
7152 const APFloat &V2 = C2->getValueAPF();
7153 const APFloat &V3 = C3->getValueAPF();
7154 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7157 } else
7159 return getConstantFP(V1, DL, VT);
7160 }
7161 }
7162
7163 // This is for vector folding only from here on.
7164 if (!VT.isVector())
7165 return SDValue();
7166
7167 ElementCount NumElts = VT.getVectorElementCount();
7168
7169 // See if we can fold through any bitcasted integer ops.
7170 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7171 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7172 (Ops[0].getOpcode() == ISD::BITCAST ||
7173 Ops[1].getOpcode() == ISD::BITCAST)) {
7176 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7177 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7178 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7179 N2.getValueType().isInteger()) {
7180 bool IsLE = getDataLayout().isLittleEndian();
7181 unsigned EltBits = VT.getScalarSizeInBits();
7182 SmallVector<APInt> RawBits1, RawBits2;
7183 BitVector UndefElts1, UndefElts2;
7184 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7185 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7186 SmallVector<APInt> RawBits;
7187 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7188 std::optional<APInt> Fold = FoldValueWithUndef(
7189 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7190 if (!Fold)
7191 break;
7192 RawBits.push_back(*Fold);
7193 }
7194 if (RawBits.size() == NumElts.getFixedValue()) {
7195 // We have constant folded, but we might need to cast this again back
7196 // to the original (possibly legalized) type.
7197 EVT BVVT, BVEltVT;
7198 if (N1.getValueType() == VT) {
7199 BVVT = N1.getValueType();
7200 BVEltVT = BV1->getOperand(0).getValueType();
7201 } else {
7202 BVVT = N2.getValueType();
7203 BVEltVT = BV2->getOperand(0).getValueType();
7204 }
7205 unsigned BVEltBits = BVEltVT.getSizeInBits();
7206 SmallVector<APInt> DstBits;
7207 BitVector DstUndefs;
7209 DstBits, RawBits, DstUndefs,
7210 BitVector(RawBits.size(), false));
7211 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7212 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7213 if (DstUndefs[I])
7214 continue;
7215 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7216 }
7217 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7218 }
7219 }
7220 }
7221 }
7222
7223 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7224 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7225 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7226 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7227 APInt RHSVal;
7228 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7229 APInt NewStep = Opcode == ISD::MUL
7230 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7231 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7232 return getStepVector(DL, VT, NewStep);
7233 }
7234 }
7235
7236 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7237 return !Op.getValueType().isVector() ||
7238 Op.getValueType().getVectorElementCount() == NumElts;
7239 };
7240
7241 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7242 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7243 Op.getOpcode() == ISD::BUILD_VECTOR ||
7244 Op.getOpcode() == ISD::SPLAT_VECTOR;
7245 };
7246
7247 // All operands must be vector types with the same number of elements as
7248 // the result type and must be either UNDEF or a build/splat vector
7249 // or UNDEF scalars.
7250 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7251 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7252 return SDValue();
7253
7254 // If we are comparing vectors, then the result needs to be a i1 boolean that
7255 // is then extended back to the legal result type depending on how booleans
7256 // are represented.
7257 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7258 ISD::NodeType ExtendCode =
7259 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7260 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7262
7263 // Find legal integer scalar type for constant promotion and
7264 // ensure that its scalar size is at least as large as source.
7265 EVT LegalSVT = VT.getScalarType();
7266 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7267 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7268 if (LegalSVT.bitsLT(VT.getScalarType()))
7269 return SDValue();
7270 }
7271
7272 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7273 // only have one operand to check. For fixed-length vector types we may have
7274 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7275 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7276
7277 // Constant fold each scalar lane separately.
7278 SmallVector<SDValue, 4> ScalarResults;
7279 for (unsigned I = 0; I != NumVectorElts; I++) {
7280 SmallVector<SDValue, 4> ScalarOps;
7281 for (SDValue Op : Ops) {
7282 EVT InSVT = Op.getValueType().getScalarType();
7283 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7284 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7285 if (Op.isUndef())
7286 ScalarOps.push_back(getUNDEF(InSVT));
7287 else
7288 ScalarOps.push_back(Op);
7289 continue;
7290 }
7291
7292 SDValue ScalarOp =
7293 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7294 EVT ScalarVT = ScalarOp.getValueType();
7295
7296 // Build vector (integer) scalar operands may need implicit
7297 // truncation - do this before constant folding.
7298 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7299 // Don't create illegally-typed nodes unless they're constants or undef
7300 // - if we fail to constant fold we can't guarantee the (dead) nodes
7301 // we're creating will be cleaned up before being visited for
7302 // legalization.
7303 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7304 !isa<ConstantSDNode>(ScalarOp) &&
7305 TLI->getTypeAction(*getContext(), InSVT) !=
7307 return SDValue();
7308 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7309 }
7310
7311 ScalarOps.push_back(ScalarOp);
7312 }
7313
7314 // Constant fold the scalar operands.
7315 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7316
7317 // Scalar folding only succeeded if the result is a constant or UNDEF.
7318 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7319 ScalarResult.getOpcode() != ISD::ConstantFP)
7320 return SDValue();
7321
7322 // Legalize the (integer) scalar constant if necessary. We only do
7323 // this once we know the folding succeeded, since otherwise we would
7324 // get a node with illegal type which has a user.
7325 if (LegalSVT != SVT)
7326 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7327
7328 ScalarResults.push_back(ScalarResult);
7329 }
7330
7331 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7332 : getBuildVector(VT, DL, ScalarResults);
7333 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7334 return V;
7335}
7336
7339 // TODO: Add support for unary/ternary fp opcodes.
7340 if (Ops.size() != 2)
7341 return SDValue();
7342
7343 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7344 // should. That will require dealing with a potentially non-default
7345 // rounding mode, checking the "opStatus" return value from the APFloat
7346 // math calculations, and possibly other variations.
7347 SDValue N1 = Ops[0];
7348 SDValue N2 = Ops[1];
7349 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7350 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7351 if (N1CFP && N2CFP) {
7352 APFloat C1 = N1CFP->getValueAPF(); // make copy
7353 const APFloat &C2 = N2CFP->getValueAPF();
7354 switch (Opcode) {
7355 case ISD::FADD:
7357 return getConstantFP(C1, DL, VT);
7358 case ISD::FSUB:
7360 return getConstantFP(C1, DL, VT);
7361 case ISD::FMUL:
7363 return getConstantFP(C1, DL, VT);
7364 case ISD::FDIV:
7366 return getConstantFP(C1, DL, VT);
7367 case ISD::FREM:
7368 C1.mod(C2);
7369 return getConstantFP(C1, DL, VT);
7370 case ISD::FCOPYSIGN:
7371 C1.copySign(C2);
7372 return getConstantFP(C1, DL, VT);
7373 case ISD::FMINNUM:
7374 return getConstantFP(minnum(C1, C2), DL, VT);
7375 case ISD::FMAXNUM:
7376 return getConstantFP(maxnum(C1, C2), DL, VT);
7377 case ISD::FMINIMUM:
7378 return getConstantFP(minimum(C1, C2), DL, VT);
7379 case ISD::FMAXIMUM:
7380 return getConstantFP(maximum(C1, C2), DL, VT);
7381 case ISD::FMINIMUMNUM:
7382 return getConstantFP(minimumnum(C1, C2), DL, VT);
7383 case ISD::FMAXIMUMNUM:
7384 return getConstantFP(maximumnum(C1, C2), DL, VT);
7385 default: break;
7386 }
7387 }
7388 if (N1CFP && Opcode == ISD::FP_ROUND) {
7389 APFloat C1 = N1CFP->getValueAPF(); // make copy
7390 bool Unused;
7391 // This can return overflow, underflow, or inexact; we don't care.
7392 // FIXME need to be more flexible about rounding mode.
7394 &Unused);
7395 return getConstantFP(C1, DL, VT);
7396 }
7397
7398 switch (Opcode) {
7399 case ISD::FSUB:
7400 // -0.0 - undef --> undef (consistent with "fneg undef")
7401 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7402 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7403 return getUNDEF(VT);
7404 [[fallthrough]];
7405
7406 case ISD::FADD:
7407 case ISD::FMUL:
7408 case ISD::FDIV:
7409 case ISD::FREM:
7410 // If both operands are undef, the result is undef. If 1 operand is undef,
7411 // the result is NaN. This should match the behavior of the IR optimizer.
7412 if (N1.isUndef() && N2.isUndef())
7413 return getUNDEF(VT);
7414 if (N1.isUndef() || N2.isUndef())
7416 }
7417 return SDValue();
7418}
7419
7421 const SDLoc &DL, EVT DstEltVT) {
7422 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7423
7424 // If this is already the right type, we're done.
7425 if (SrcEltVT == DstEltVT)
7426 return SDValue(BV, 0);
7427
7428 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7429 unsigned DstBitSize = DstEltVT.getSizeInBits();
7430
7431 // If this is a conversion of N elements of one type to N elements of another
7432 // type, convert each element. This handles FP<->INT cases.
7433 if (SrcBitSize == DstBitSize) {
7435 for (SDValue Op : BV->op_values()) {
7436 // If the vector element type is not legal, the BUILD_VECTOR operands
7437 // are promoted and implicitly truncated. Make that explicit here.
7438 if (Op.getValueType() != SrcEltVT)
7439 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7440 Ops.push_back(getBitcast(DstEltVT, Op));
7441 }
7442 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7444 return getBuildVector(VT, DL, Ops);
7445 }
7446
7447 // Otherwise, we're growing or shrinking the elements. To avoid having to
7448 // handle annoying details of growing/shrinking FP values, we convert them to
7449 // int first.
7450 if (SrcEltVT.isFloatingPoint()) {
7451 // Convert the input float vector to a int vector where the elements are the
7452 // same sizes.
7453 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7454 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7456 DstEltVT);
7457 return SDValue();
7458 }
7459
7460 // Now we know the input is an integer vector. If the output is a FP type,
7461 // convert to integer first, then to FP of the right size.
7462 if (DstEltVT.isFloatingPoint()) {
7463 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7464 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7466 DstEltVT);
7467 return SDValue();
7468 }
7469
7470 // Okay, we know the src/dst types are both integers of differing types.
7471 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7472
7473 // Extract the constant raw bit data.
7474 BitVector UndefElements;
7475 SmallVector<APInt> RawBits;
7476 bool IsLE = getDataLayout().isLittleEndian();
7477 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7478 return SDValue();
7479
7481 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7482 if (UndefElements[I])
7483 Ops.push_back(getUNDEF(DstEltVT));
7484 else
7485 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7486 }
7487
7488 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7489 return getBuildVector(VT, DL, Ops);
7490}
7491
7493 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7494
7495 // There's no need to assert on a byte-aligned pointer. All pointers are at
7496 // least byte aligned.
7497 if (A == Align(1))
7498 return Val;
7499
7500 SDVTList VTs = getVTList(Val.getValueType());
7502 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7503 ID.AddInteger(A.value());
7504
7505 void *IP = nullptr;
7506 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7507 return SDValue(E, 0);
7508
7509 auto *N =
7510 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7511 createOperands(N, {Val});
7512
7513 CSEMap.InsertNode(N, IP);
7514 InsertNode(N);
7515
7516 SDValue V(N, 0);
7517 NewSDValueDbgMsg(V, "Creating new node: ", this);
7518 return V;
7519}
7520
7521SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7522 SDValue N1, SDValue N2) {
7523 SDNodeFlags Flags;
7524 if (Inserter)
7525 Flags = Inserter->getFlags();
7526 return getNode(Opcode, DL, VT, N1, N2, Flags);
7527}
7528
7530 SDValue &N2) const {
7531 if (!TLI->isCommutativeBinOp(Opcode))
7532 return;
7533
7534 // Canonicalize:
7535 // binop(const, nonconst) -> binop(nonconst, const)
7538 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7539 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7540 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7541 std::swap(N1, N2);
7542
7543 // Canonicalize:
7544 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7545 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7547 std::swap(N1, N2);
7548}
7549
7550SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7551 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7553 N2.getOpcode() != ISD::DELETED_NODE &&
7554 "Operand is DELETED_NODE!");
7555
7556 canonicalizeCommutativeBinop(Opcode, N1, N2);
7557
7558 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7559 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7560
7561 // Don't allow undefs in vector splats - we might be returning N2 when folding
7562 // to zero etc.
7563 ConstantSDNode *N2CV =
7564 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7565
7566 switch (Opcode) {
7567 default: break;
7568 case ISD::TokenFactor:
7569 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7570 N2.getValueType() == MVT::Other && "Invalid token factor!");
7571 // Fold trivial token factors.
7572 if (N1.getOpcode() == ISD::EntryToken) return N2;
7573 if (N2.getOpcode() == ISD::EntryToken) return N1;
7574 if (N1 == N2) return N1;
7575 break;
7576 case ISD::BUILD_VECTOR: {
7577 // Attempt to simplify BUILD_VECTOR.
7578 SDValue Ops[] = {N1, N2};
7579 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7580 return V;
7581 break;
7582 }
7583 case ISD::CONCAT_VECTORS: {
7584 SDValue Ops[] = {N1, N2};
7585 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7586 return V;
7587 break;
7588 }
7589 case ISD::AND:
7590 assert(VT.isInteger() && "This operator does not apply to FP types!");
7591 assert(N1.getValueType() == N2.getValueType() &&
7592 N1.getValueType() == VT && "Binary operator types must match!");
7593 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7594 // worth handling here.
7595 if (N2CV && N2CV->isZero())
7596 return N2;
7597 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7598 return N1;
7599 break;
7600 case ISD::OR:
7601 case ISD::XOR:
7602 case ISD::ADD:
7603 case ISD::PTRADD:
7604 case ISD::SUB:
7605 assert(VT.isInteger() && "This operator does not apply to FP types!");
7606 assert(N1.getValueType() == N2.getValueType() &&
7607 N1.getValueType() == VT && "Binary operator types must match!");
7608 // The equal operand types requirement is unnecessarily strong for PTRADD.
7609 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7610 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7611 // logic everywhere where PTRADDs may be folded or combined to properly
7612 // support them. If/when we introduce pointer types to the SDAG, we will
7613 // need to relax this constraint.
7614
7615 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7616 // it's worth handling here.
7617 if (N2CV && N2CV->isZero())
7618 return N1;
7619 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7620 VT.getScalarType() == MVT::i1)
7621 return getNode(ISD::XOR, DL, VT, N1, N2);
7622 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7623 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7624 N2.getOpcode() == ISD::VSCALE) {
7625 const APInt &C1 = N1->getConstantOperandAPInt(0);
7626 const APInt &C2 = N2->getConstantOperandAPInt(0);
7627 return getVScale(DL, VT, C1 + C2);
7628 }
7629 break;
7630 case ISD::MUL:
7631 assert(VT.isInteger() && "This operator does not apply to FP types!");
7632 assert(N1.getValueType() == N2.getValueType() &&
7633 N1.getValueType() == VT && "Binary operator types must match!");
7634 if (VT.getScalarType() == MVT::i1)
7635 return getNode(ISD::AND, DL, VT, N1, N2);
7636 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7637 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7638 const APInt &N2CImm = N2C->getAPIntValue();
7639 return getVScale(DL, VT, MulImm * N2CImm);
7640 }
7641 break;
7642 case ISD::UDIV:
7643 case ISD::UREM:
7644 case ISD::MULHU:
7645 case ISD::MULHS:
7646 case ISD::SDIV:
7647 case ISD::SREM:
7648 case ISD::SADDSAT:
7649 case ISD::SSUBSAT:
7650 case ISD::UADDSAT:
7651 case ISD::USUBSAT:
7652 assert(VT.isInteger() && "This operator does not apply to FP types!");
7653 assert(N1.getValueType() == N2.getValueType() &&
7654 N1.getValueType() == VT && "Binary operator types must match!");
7655 if (VT.getScalarType() == MVT::i1) {
7656 // fold (add_sat x, y) -> (or x, y) for bool types.
7657 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7658 return getNode(ISD::OR, DL, VT, N1, N2);
7659 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7660 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7661 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7662 }
7663 break;
7664 case ISD::SCMP:
7665 case ISD::UCMP:
7666 assert(N1.getValueType() == N2.getValueType() &&
7667 "Types of operands of UCMP/SCMP must match");
7668 assert(N1.getValueType().isVector() == VT.isVector() &&
7669 "Operands and return type of must both be scalars or vectors");
7670 if (VT.isVector())
7673 "Result and operands must have the same number of elements");
7674 break;
7675 case ISD::AVGFLOORS:
7676 case ISD::AVGFLOORU:
7677 case ISD::AVGCEILS:
7678 case ISD::AVGCEILU:
7679 assert(VT.isInteger() && "This operator does not apply to FP types!");
7680 assert(N1.getValueType() == N2.getValueType() &&
7681 N1.getValueType() == VT && "Binary operator types must match!");
7682 break;
7683 case ISD::ABDS:
7684 case ISD::ABDU:
7685 assert(VT.isInteger() && "This operator does not apply to FP types!");
7686 assert(N1.getValueType() == N2.getValueType() &&
7687 N1.getValueType() == VT && "Binary operator types must match!");
7688 if (VT.getScalarType() == MVT::i1)
7689 return getNode(ISD::XOR, DL, VT, N1, N2);
7690 break;
7691 case ISD::SMIN:
7692 case ISD::UMAX:
7693 assert(VT.isInteger() && "This operator does not apply to FP types!");
7694 assert(N1.getValueType() == N2.getValueType() &&
7695 N1.getValueType() == VT && "Binary operator types must match!");
7696 if (VT.getScalarType() == MVT::i1)
7697 return getNode(ISD::OR, DL, VT, N1, N2);
7698 break;
7699 case ISD::SMAX:
7700 case ISD::UMIN:
7701 assert(VT.isInteger() && "This operator does not apply to FP types!");
7702 assert(N1.getValueType() == N2.getValueType() &&
7703 N1.getValueType() == VT && "Binary operator types must match!");
7704 if (VT.getScalarType() == MVT::i1)
7705 return getNode(ISD::AND, DL, VT, N1, N2);
7706 break;
7707 case ISD::FADD:
7708 case ISD::FSUB:
7709 case ISD::FMUL:
7710 case ISD::FDIV:
7711 case ISD::FREM:
7712 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7713 assert(N1.getValueType() == N2.getValueType() &&
7714 N1.getValueType() == VT && "Binary operator types must match!");
7715 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7716 return V;
7717 break;
7718 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7719 assert(N1.getValueType() == VT &&
7722 "Invalid FCOPYSIGN!");
7723 break;
7724 case ISD::SHL:
7725 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7726 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7727 const APInt &ShiftImm = N2C->getAPIntValue();
7728 return getVScale(DL, VT, MulImm << ShiftImm);
7729 }
7730 [[fallthrough]];
7731 case ISD::SRA:
7732 case ISD::SRL:
7733 if (SDValue V = simplifyShift(N1, N2))
7734 return V;
7735 [[fallthrough]];
7736 case ISD::ROTL:
7737 case ISD::ROTR:
7738 assert(VT == N1.getValueType() &&
7739 "Shift operators return type must be the same as their first arg");
7740 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7741 "Shifts only work on integers");
7742 assert((!VT.isVector() || VT == N2.getValueType()) &&
7743 "Vector shift amounts must be in the same as their first arg");
7744 // Verify that the shift amount VT is big enough to hold valid shift
7745 // amounts. This catches things like trying to shift an i1024 value by an
7746 // i8, which is easy to fall into in generic code that uses
7747 // TLI.getShiftAmount().
7750 "Invalid use of small shift amount with oversized value!");
7751
7752 // Always fold shifts of i1 values so the code generator doesn't need to
7753 // handle them. Since we know the size of the shift has to be less than the
7754 // size of the value, the shift/rotate count is guaranteed to be zero.
7755 if (VT == MVT::i1)
7756 return N1;
7757 if (N2CV && N2CV->isZero())
7758 return N1;
7759 break;
7760 case ISD::FP_ROUND:
7762 VT.bitsLE(N1.getValueType()) && N2C &&
7763 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7764 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7765 if (N1.getValueType() == VT) return N1; // noop conversion.
7766 break;
7767 case ISD::AssertNoFPClass: {
7769 "AssertNoFPClass is used for a non-floating type");
7770 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7771 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7772 assert(llvm::to_underlying(NoFPClass) <=
7774 "FPClassTest value too large");
7775 (void)NoFPClass;
7776 break;
7777 }
7778 case ISD::AssertSext:
7779 case ISD::AssertZext: {
7780 EVT EVT = cast<VTSDNode>(N2)->getVT();
7781 assert(VT == N1.getValueType() && "Not an inreg extend!");
7782 assert(VT.isInteger() && EVT.isInteger() &&
7783 "Cannot *_EXTEND_INREG FP types");
7784 assert(!EVT.isVector() &&
7785 "AssertSExt/AssertZExt type should be the vector element type "
7786 "rather than the vector type!");
7787 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7788 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7789 break;
7790 }
7792 EVT EVT = cast<VTSDNode>(N2)->getVT();
7793 assert(VT == N1.getValueType() && "Not an inreg extend!");
7794 assert(VT.isInteger() && EVT.isInteger() &&
7795 "Cannot *_EXTEND_INREG FP types");
7796 assert(EVT.isVector() == VT.isVector() &&
7797 "SIGN_EXTEND_INREG type should be vector iff the operand "
7798 "type is vector!");
7799 assert((!EVT.isVector() ||
7801 "Vector element counts must match in SIGN_EXTEND_INREG");
7802 assert(EVT.bitsLE(VT) && "Not extending!");
7803 if (EVT == VT) return N1; // Not actually extending
7804 break;
7805 }
7807 case ISD::FP_TO_UINT_SAT: {
7808 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7809 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7810 assert(N1.getValueType().isVector() == VT.isVector() &&
7811 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7812 "vector!");
7813 assert((!VT.isVector() || VT.getVectorElementCount() ==
7815 "Vector element counts must match in FP_TO_*INT_SAT");
7816 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7817 "Type to saturate to must be a scalar.");
7818 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7819 "Not extending!");
7820 break;
7821 }
7824 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7825 element type of the vector.");
7826
7827 // Extract from an undefined value or using an undefined index is undefined.
7828 if (N1.isUndef() || N2.isUndef())
7829 return getUNDEF(VT);
7830
7831 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7832 // vectors. For scalable vectors we will provide appropriate support for
7833 // dealing with arbitrary indices.
7834 if (N2C && N1.getValueType().isFixedLengthVector() &&
7835 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7836 return getUNDEF(VT);
7837
7838 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7839 // expanding copies of large vectors from registers. This only works for
7840 // fixed length vectors, since we need to know the exact number of
7841 // elements.
7842 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7844 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7845 return getExtractVectorElt(DL, VT,
7846 N1.getOperand(N2C->getZExtValue() / Factor),
7847 N2C->getZExtValue() % Factor);
7848 }
7849
7850 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7851 // lowering is expanding large vector constants.
7852 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7853 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7856 "BUILD_VECTOR used for scalable vectors");
7857 unsigned Index =
7858 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7859 SDValue Elt = N1.getOperand(Index);
7860
7861 if (VT != Elt.getValueType())
7862 // If the vector element type is not legal, the BUILD_VECTOR operands
7863 // are promoted and implicitly truncated, and the result implicitly
7864 // extended. Make that explicit here.
7865 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7866
7867 return Elt;
7868 }
7869
7870 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7871 // operations are lowered to scalars.
7872 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7873 // If the indices are the same, return the inserted element else
7874 // if the indices are known different, extract the element from
7875 // the original vector.
7876 SDValue N1Op2 = N1.getOperand(2);
7878
7879 if (N1Op2C && N2C) {
7880 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7881 if (VT == N1.getOperand(1).getValueType())
7882 return N1.getOperand(1);
7883 if (VT.isFloatingPoint()) {
7885 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7886 }
7887 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7888 }
7889 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7890 }
7891 }
7892
7893 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7894 // when vector types are scalarized and v1iX is legal.
7895 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7896 // Here we are completely ignoring the extract element index (N2),
7897 // which is fine for fixed width vectors, since any index other than 0
7898 // is undefined anyway. However, this cannot be ignored for scalable
7899 // vectors - in theory we could support this, but we don't want to do this
7900 // without a profitability check.
7901 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7903 N1.getValueType().getVectorNumElements() == 1) {
7904 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7905 N1.getOperand(1));
7906 }
7907 break;
7909 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7910 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7911 (N1.getValueType().isInteger() == VT.isInteger()) &&
7912 N1.getValueType() != VT &&
7913 "Wrong types for EXTRACT_ELEMENT!");
7914
7915 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7916 // 64-bit integers into 32-bit parts. Instead of building the extract of
7917 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7918 if (N1.getOpcode() == ISD::BUILD_PAIR)
7919 return N1.getOperand(N2C->getZExtValue());
7920
7921 // EXTRACT_ELEMENT of a constant int is also very common.
7922 if (N1C) {
7923 unsigned ElementSize = VT.getSizeInBits();
7924 unsigned Shift = ElementSize * N2C->getZExtValue();
7925 const APInt &Val = N1C->getAPIntValue();
7926 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
7927 }
7928 break;
7930 EVT N1VT = N1.getValueType();
7931 assert(VT.isVector() && N1VT.isVector() &&
7932 "Extract subvector VTs must be vectors!");
7934 "Extract subvector VTs must have the same element type!");
7935 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
7936 "Cannot extract a scalable vector from a fixed length vector!");
7937 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7939 "Extract subvector must be from larger vector to smaller vector!");
7940 assert(N2C && "Extract subvector index must be a constant");
7941 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
7942 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
7943 N1VT.getVectorMinNumElements()) &&
7944 "Extract subvector overflow!");
7945 assert(N2C->getAPIntValue().getBitWidth() ==
7946 TLI->getVectorIdxWidth(getDataLayout()) &&
7947 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
7948 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
7949 "Extract index is not a multiple of the output vector length");
7950
7951 // Trivial extraction.
7952 if (VT == N1VT)
7953 return N1;
7954
7955 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
7956 if (N1.isUndef())
7957 return getUNDEF(VT);
7958
7959 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
7960 // the concat have the same type as the extract.
7961 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
7962 VT == N1.getOperand(0).getValueType()) {
7963 unsigned Factor = VT.getVectorMinNumElements();
7964 return N1.getOperand(N2C->getZExtValue() / Factor);
7965 }
7966
7967 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
7968 // during shuffle legalization.
7969 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
7970 VT == N1.getOperand(1).getValueType())
7971 return N1.getOperand(1);
7972 break;
7973 }
7974 }
7975
7976 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
7977 switch (Opcode) {
7978 case ISD::XOR:
7979 case ISD::ADD:
7980 case ISD::PTRADD:
7981 case ISD::SUB:
7983 case ISD::UDIV:
7984 case ISD::SDIV:
7985 case ISD::UREM:
7986 case ISD::SREM:
7987 case ISD::MUL:
7988 case ISD::AND:
7989 case ISD::SSUBSAT:
7990 case ISD::USUBSAT:
7991 case ISD::UMIN:
7992 case ISD::OR:
7993 case ISD::SADDSAT:
7994 case ISD::UADDSAT:
7995 case ISD::UMAX:
7996 case ISD::SMAX:
7997 case ISD::SMIN:
7998 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
7999 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8000 }
8001 }
8002
8003 // Canonicalize an UNDEF to the RHS, even over a constant.
8004 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8005 if (TLI->isCommutativeBinOp(Opcode)) {
8006 std::swap(N1, N2);
8007 } else {
8008 switch (Opcode) {
8009 case ISD::PTRADD:
8010 case ISD::SUB:
8011 // fold op(undef, non_undef_arg2) -> undef.
8012 return N1;
8014 case ISD::UDIV:
8015 case ISD::SDIV:
8016 case ISD::UREM:
8017 case ISD::SREM:
8018 case ISD::SSUBSAT:
8019 case ISD::USUBSAT:
8020 // fold op(undef, non_undef_arg2) -> 0.
8021 return getConstant(0, DL, VT);
8022 }
8023 }
8024 }
8025
8026 // Fold a bunch of operators when the RHS is undef.
8027 if (N2.getOpcode() == ISD::UNDEF) {
8028 switch (Opcode) {
8029 case ISD::XOR:
8030 if (N1.getOpcode() == ISD::UNDEF)
8031 // Handle undef ^ undef -> 0 special case. This is a common
8032 // idiom (misuse).
8033 return getConstant(0, DL, VT);
8034 [[fallthrough]];
8035 case ISD::ADD:
8036 case ISD::PTRADD:
8037 case ISD::SUB:
8038 // fold op(arg1, undef) -> undef.
8039 return N2;
8040 case ISD::UDIV:
8041 case ISD::SDIV:
8042 case ISD::UREM:
8043 case ISD::SREM:
8044 // fold op(arg1, undef) -> poison.
8045 return getPOISON(VT);
8046 case ISD::MUL:
8047 case ISD::AND:
8048 case ISD::SSUBSAT:
8049 case ISD::USUBSAT:
8050 case ISD::UMIN:
8051 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8052 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8053 case ISD::OR:
8054 case ISD::SADDSAT:
8055 case ISD::UADDSAT:
8056 case ISD::UMAX:
8057 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8058 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8059 case ISD::SMAX:
8060 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8061 return N1.getOpcode() == ISD::UNDEF
8062 ? N2
8063 : getConstant(
8065 VT);
8066 case ISD::SMIN:
8067 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8068 return N1.getOpcode() == ISD::UNDEF
8069 ? N2
8070 : getConstant(
8072 VT);
8073 }
8074 }
8075
8076 // Perform trivial constant folding.
8077 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8078 return SV;
8079
8080 // Memoize this node if possible.
8081 SDNode *N;
8082 SDVTList VTs = getVTList(VT);
8083 SDValue Ops[] = {N1, N2};
8084 if (VT != MVT::Glue) {
8086 AddNodeIDNode(ID, Opcode, VTs, Ops);
8087 void *IP = nullptr;
8088 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8089 E->intersectFlagsWith(Flags);
8090 return SDValue(E, 0);
8091 }
8092
8093 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8094 N->setFlags(Flags);
8095 createOperands(N, Ops);
8096 CSEMap.InsertNode(N, IP);
8097 } else {
8098 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8099 createOperands(N, Ops);
8100 }
8101
8102 InsertNode(N);
8103 SDValue V = SDValue(N, 0);
8104 NewSDValueDbgMsg(V, "Creating new node: ", this);
8105 return V;
8106}
8107
8108SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8109 SDValue N1, SDValue N2, SDValue N3) {
8110 SDNodeFlags Flags;
8111 if (Inserter)
8112 Flags = Inserter->getFlags();
8113 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8114}
8115
8116SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8117 SDValue N1, SDValue N2, SDValue N3,
8118 const SDNodeFlags Flags) {
8120 N2.getOpcode() != ISD::DELETED_NODE &&
8121 N3.getOpcode() != ISD::DELETED_NODE &&
8122 "Operand is DELETED_NODE!");
8123 // Perform various simplifications.
8124 switch (Opcode) {
8125 case ISD::BUILD_VECTOR: {
8126 // Attempt to simplify BUILD_VECTOR.
8127 SDValue Ops[] = {N1, N2, N3};
8128 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8129 return V;
8130 break;
8131 }
8132 case ISD::CONCAT_VECTORS: {
8133 SDValue Ops[] = {N1, N2, N3};
8134 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8135 return V;
8136 break;
8137 }
8138 case ISD::SETCC: {
8139 assert(VT.isInteger() && "SETCC result type must be an integer!");
8140 assert(N1.getValueType() == N2.getValueType() &&
8141 "SETCC operands must have the same type!");
8142 assert(VT.isVector() == N1.getValueType().isVector() &&
8143 "SETCC type should be vector iff the operand type is vector!");
8144 assert((!VT.isVector() || VT.getVectorElementCount() ==
8146 "SETCC vector element counts must match!");
8147 // Use FoldSetCC to simplify SETCC's.
8148 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8149 return V;
8150 break;
8151 }
8152 case ISD::SELECT:
8153 case ISD::VSELECT:
8154 if (SDValue V = simplifySelect(N1, N2, N3))
8155 return V;
8156 break;
8158 llvm_unreachable("should use getVectorShuffle constructor!");
8159 case ISD::VECTOR_SPLICE: {
8160 if (cast<ConstantSDNode>(N3)->isZero())
8161 return N1;
8162 break;
8163 }
8165 assert(VT.isVector() && VT == N1.getValueType() &&
8166 "INSERT_VECTOR_ELT vector type mismatch");
8168 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8169 assert((!VT.isFloatingPoint() ||
8170 VT.getVectorElementType() == N2.getValueType()) &&
8171 "INSERT_VECTOR_ELT fp scalar type mismatch");
8172 assert((!VT.isInteger() ||
8174 "INSERT_VECTOR_ELT int scalar size mismatch");
8175
8176 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8177 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8178 // for scalable vectors where we will generate appropriate code to
8179 // deal with out-of-bounds cases correctly.
8180 if (N3C && VT.isFixedLengthVector() &&
8181 N3C->getZExtValue() >= VT.getVectorNumElements())
8182 return getUNDEF(VT);
8183
8184 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8185 if (N3.isUndef())
8186 return getUNDEF(VT);
8187
8188 // If inserting poison, just use the input vector.
8189 if (N2.getOpcode() == ISD::POISON)
8190 return N1;
8191
8192 // Inserting undef into undef/poison is still undef.
8193 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8194 return getUNDEF(VT);
8195
8196 // If the inserted element is an UNDEF, just use the input vector.
8197 // But not if skipping the insert could make the result more poisonous.
8198 if (N2.isUndef()) {
8199 if (N3C && VT.isFixedLengthVector()) {
8200 APInt EltMask =
8201 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8202 if (isGuaranteedNotToBePoison(N1, EltMask))
8203 return N1;
8204 } else if (isGuaranteedNotToBePoison(N1))
8205 return N1;
8206 }
8207 break;
8208 }
8209 case ISD::INSERT_SUBVECTOR: {
8210 // If inserting poison, just use the input vector,
8211 if (N2.getOpcode() == ISD::POISON)
8212 return N1;
8213
8214 // Inserting undef into undef/poison is still undef.
8215 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8216 return getUNDEF(VT);
8217
8218 EVT N2VT = N2.getValueType();
8219 assert(VT == N1.getValueType() &&
8220 "Dest and insert subvector source types must match!");
8221 assert(VT.isVector() && N2VT.isVector() &&
8222 "Insert subvector VTs must be vectors!");
8224 "Insert subvector VTs must have the same element type!");
8225 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8226 "Cannot insert a scalable vector into a fixed length vector!");
8227 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8229 "Insert subvector must be from smaller vector to larger vector!");
8231 "Insert subvector index must be constant");
8232 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8233 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8235 "Insert subvector overflow!");
8237 TLI->getVectorIdxWidth(getDataLayout()) &&
8238 "Constant index for INSERT_SUBVECTOR has an invalid size");
8239
8240 // Trivial insertion.
8241 if (VT == N2VT)
8242 return N2;
8243
8244 // If this is an insert of an extracted vector into an undef/poison vector,
8245 // we can just use the input to the extract. But not if skipping the
8246 // extract+insert could make the result more poisonous.
8247 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8248 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8249 if (N1.getOpcode() == ISD::POISON)
8250 return N2.getOperand(0);
8251 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8252 unsigned LoBit = N3->getAsZExtVal();
8253 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8254 APInt EltMask =
8255 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8256 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8257 return N2.getOperand(0);
8258 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8259 return N2.getOperand(0);
8260 }
8261
8262 // If the inserted subvector is UNDEF, just use the input vector.
8263 // But not if skipping the insert could make the result more poisonous.
8264 if (N2.isUndef()) {
8265 if (VT.isFixedLengthVector()) {
8266 unsigned LoBit = N3->getAsZExtVal();
8267 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8268 APInt EltMask =
8269 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8270 if (isGuaranteedNotToBePoison(N1, EltMask))
8271 return N1;
8272 } else if (isGuaranteedNotToBePoison(N1))
8273 return N1;
8274 }
8275 break;
8276 }
8277 case ISD::BITCAST:
8278 // Fold bit_convert nodes from a type to themselves.
8279 if (N1.getValueType() == VT)
8280 return N1;
8281 break;
8282 case ISD::VP_TRUNCATE:
8283 case ISD::VP_SIGN_EXTEND:
8284 case ISD::VP_ZERO_EXTEND:
8285 // Don't create noop casts.
8286 if (N1.getValueType() == VT)
8287 return N1;
8288 break;
8289 case ISD::VECTOR_COMPRESS: {
8290 [[maybe_unused]] EVT VecVT = N1.getValueType();
8291 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8292 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8293 assert(VT == VecVT && "Vector and result type don't match.");
8294 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8295 "All inputs must be vectors.");
8296 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8298 "Vector and mask must have same number of elements.");
8299
8300 if (N1.isUndef() || N2.isUndef())
8301 return N3;
8302
8303 break;
8304 }
8305 case ISD::PARTIAL_REDUCE_UMLA:
8306 case ISD::PARTIAL_REDUCE_SMLA:
8307 case ISD::PARTIAL_REDUCE_SUMLA:
8308 case ISD::PARTIAL_REDUCE_FMLA: {
8309 [[maybe_unused]] EVT AccVT = N1.getValueType();
8310 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8311 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8312 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8313 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8314 "node to have the same type!");
8315 assert(VT.isVector() && VT == AccVT &&
8316 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8317 "the same type as its result!");
8319 AccVT.getVectorElementCount()) &&
8320 "Expected the element count of the second and third operands of the "
8321 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8322 "element count of the first operand and the result!");
8324 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8325 "node to have an element type which is the same as or smaller than "
8326 "the element type of the first operand and result!");
8327 break;
8328 }
8329 }
8330
8331 // Perform trivial constant folding for arithmetic operators.
8332 switch (Opcode) {
8333 case ISD::FMA:
8334 case ISD::FMAD:
8335 case ISD::SETCC:
8336 case ISD::FSHL:
8337 case ISD::FSHR:
8338 if (SDValue SV =
8339 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8340 return SV;
8341 break;
8342 }
8343
8344 // Memoize node if it doesn't produce a glue result.
8345 SDNode *N;
8346 SDVTList VTs = getVTList(VT);
8347 SDValue Ops[] = {N1, N2, N3};
8348 if (VT != MVT::Glue) {
8350 AddNodeIDNode(ID, Opcode, VTs, Ops);
8351 void *IP = nullptr;
8352 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8353 E->intersectFlagsWith(Flags);
8354 return SDValue(E, 0);
8355 }
8356
8357 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8358 N->setFlags(Flags);
8359 createOperands(N, Ops);
8360 CSEMap.InsertNode(N, IP);
8361 } else {
8362 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8363 createOperands(N, Ops);
8364 }
8365
8366 InsertNode(N);
8367 SDValue V = SDValue(N, 0);
8368 NewSDValueDbgMsg(V, "Creating new node: ", this);
8369 return V;
8370}
8371
8372SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8373 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8374 const SDNodeFlags Flags) {
8375 SDValue Ops[] = { N1, N2, N3, N4 };
8376 return getNode(Opcode, DL, VT, Ops, Flags);
8377}
8378
8379SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8380 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8381 SDNodeFlags Flags;
8382 if (Inserter)
8383 Flags = Inserter->getFlags();
8384 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8385}
8386
8387SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8388 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8389 SDValue N5, const SDNodeFlags Flags) {
8390 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8391 return getNode(Opcode, DL, VT, Ops, Flags);
8392}
8393
8394SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8395 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8396 SDValue N5) {
8397 SDNodeFlags Flags;
8398 if (Inserter)
8399 Flags = Inserter->getFlags();
8400 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8401}
8402
8403/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8404/// the incoming stack arguments to be loaded from the stack.
8406 SmallVector<SDValue, 8> ArgChains;
8407
8408 // Include the original chain at the beginning of the list. When this is
8409 // used by target LowerCall hooks, this helps legalize find the
8410 // CALLSEQ_BEGIN node.
8411 ArgChains.push_back(Chain);
8412
8413 // Add a chain value for each stack argument.
8414 for (SDNode *U : getEntryNode().getNode()->users())
8415 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8416 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8417 if (FI->getIndex() < 0)
8418 ArgChains.push_back(SDValue(L, 1));
8419
8420 // Build a tokenfactor for all the chains.
8421 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8422}
8423
8424/// getMemsetValue - Vectorized representation of the memset value
8425/// operand.
8427 const SDLoc &dl) {
8428 assert(!Value.isUndef());
8429
8430 unsigned NumBits = VT.getScalarSizeInBits();
8432 assert(C->getAPIntValue().getBitWidth() == 8);
8433 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8434 if (VT.isInteger()) {
8435 bool IsOpaque = VT.getSizeInBits() > 64 ||
8436 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8437 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8438 }
8439 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8440 }
8441
8442 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8443 EVT IntVT = VT.getScalarType();
8444 if (!IntVT.isInteger())
8445 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8446
8447 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8448 if (NumBits > 8) {
8449 // Use a multiplication with 0x010101... to extend the input to the
8450 // required length.
8451 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8452 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8453 DAG.getConstant(Magic, dl, IntVT));
8454 }
8455
8456 if (VT != Value.getValueType() && !VT.isInteger())
8457 Value = DAG.getBitcast(VT.getScalarType(), Value);
8458 if (VT != Value.getValueType())
8459 Value = DAG.getSplatBuildVector(VT, dl, Value);
8460
8461 return Value;
8462}
8463
8464/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8465/// used when a memcpy is turned into a memset when the source is a constant
8466/// string ptr.
8468 const TargetLowering &TLI,
8469 const ConstantDataArraySlice &Slice) {
8470 // Handle vector with all elements zero.
8471 if (Slice.Array == nullptr) {
8472 if (VT.isInteger())
8473 return DAG.getConstant(0, dl, VT);
8474 return DAG.getNode(ISD::BITCAST, dl, VT,
8475 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8476 }
8477
8478 assert(!VT.isVector() && "Can't handle vector type here!");
8479 unsigned NumVTBits = VT.getSizeInBits();
8480 unsigned NumVTBytes = NumVTBits / 8;
8481 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8482
8483 APInt Val(NumVTBits, 0);
8484 if (DAG.getDataLayout().isLittleEndian()) {
8485 for (unsigned i = 0; i != NumBytes; ++i)
8486 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8487 } else {
8488 for (unsigned i = 0; i != NumBytes; ++i)
8489 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8490 }
8491
8492 // If the "cost" of materializing the integer immediate is less than the cost
8493 // of a load, then it is cost effective to turn the load into the immediate.
8494 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8495 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8496 return DAG.getConstant(Val, dl, VT);
8497 return SDValue();
8498}
8499
8501 const SDLoc &DL,
8502 const SDNodeFlags Flags) {
8503 EVT VT = Base.getValueType();
8504 SDValue Index;
8505
8506 if (Offset.isScalable())
8507 Index = getVScale(DL, Base.getValueType(),
8508 APInt(Base.getValueSizeInBits().getFixedValue(),
8509 Offset.getKnownMinValue()));
8510 else
8511 Index = getConstant(Offset.getFixedValue(), DL, VT);
8512
8513 return getMemBasePlusOffset(Base, Index, DL, Flags);
8514}
8515
8517 const SDLoc &DL,
8518 const SDNodeFlags Flags) {
8519 assert(Offset.getValueType().isInteger());
8520 EVT BasePtrVT = Ptr.getValueType();
8521 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8522 BasePtrVT))
8523 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8524 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8525 SDNodeFlags AddFlags = Flags;
8526 AddFlags.setInBounds(false);
8527 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8528}
8529
8530/// Returns true if memcpy source is constant data.
8532 uint64_t SrcDelta = 0;
8533 GlobalAddressSDNode *G = nullptr;
8534 if (Src.getOpcode() == ISD::GlobalAddress)
8536 else if (Src->isAnyAdd() &&
8537 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8538 Src.getOperand(1).getOpcode() == ISD::Constant) {
8539 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8540 SrcDelta = Src.getConstantOperandVal(1);
8541 }
8542 if (!G)
8543 return false;
8544
8545 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8546 SrcDelta + G->getOffset());
8547}
8548
8550 SelectionDAG &DAG) {
8551 // On Darwin, -Os means optimize for size without hurting performance, so
8552 // only really optimize for size when -Oz (MinSize) is used.
8554 return MF.getFunction().hasMinSize();
8555 return DAG.shouldOptForSize();
8556}
8557
8559 SmallVector<SDValue, 32> &OutChains, unsigned From,
8560 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8561 SmallVector<SDValue, 16> &OutStoreChains) {
8562 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8563 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8564 SmallVector<SDValue, 16> GluedLoadChains;
8565 for (unsigned i = From; i < To; ++i) {
8566 OutChains.push_back(OutLoadChains[i]);
8567 GluedLoadChains.push_back(OutLoadChains[i]);
8568 }
8569
8570 // Chain for all loads.
8571 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8572 GluedLoadChains);
8573
8574 for (unsigned i = From; i < To; ++i) {
8575 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8576 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8577 ST->getBasePtr(), ST->getMemoryVT(),
8578 ST->getMemOperand());
8579 OutChains.push_back(NewStore);
8580 }
8581}
8582
8584 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8585 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8586 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8587 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8588 // Turn a memcpy of undef to nop.
8589 // FIXME: We need to honor volatile even is Src is undef.
8590 if (Src.isUndef())
8591 return Chain;
8592
8593 // Expand memcpy to a series of load and store ops if the size operand falls
8594 // below a certain threshold.
8595 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8596 // rather than maybe a humongous number of loads and stores.
8597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8598 const DataLayout &DL = DAG.getDataLayout();
8599 LLVMContext &C = *DAG.getContext();
8600 std::vector<EVT> MemOps;
8601 bool DstAlignCanChange = false;
8603 MachineFrameInfo &MFI = MF.getFrameInfo();
8604 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8606 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8607 DstAlignCanChange = true;
8608 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8609 if (!SrcAlign || Alignment > *SrcAlign)
8610 SrcAlign = Alignment;
8611 assert(SrcAlign && "SrcAlign must be set");
8613 // If marked as volatile, perform a copy even when marked as constant.
8614 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8615 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8616 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8617 const MemOp Op = isZeroConstant
8618 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8619 /*IsZeroMemset*/ true, isVol)
8620 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8621 *SrcAlign, isVol, CopyFromConstant);
8622 if (!TLI.findOptimalMemOpLowering(
8623 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8624 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8625 return SDValue();
8626
8627 if (DstAlignCanChange) {
8628 Type *Ty = MemOps[0].getTypeForEVT(C);
8629 Align NewAlign = DL.getABITypeAlign(Ty);
8630
8631 // Don't promote to an alignment that would require dynamic stack
8632 // realignment which may conflict with optimizations such as tail call
8633 // optimization.
8635 if (!TRI->hasStackRealignment(MF))
8636 if (MaybeAlign StackAlign = DL.getStackAlignment())
8637 NewAlign = std::min(NewAlign, *StackAlign);
8638
8639 if (NewAlign > Alignment) {
8640 // Give the stack frame object a larger alignment if needed.
8641 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8642 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8643 Alignment = NewAlign;
8644 }
8645 }
8646
8647 // Prepare AAInfo for loads/stores after lowering this memcpy.
8648 AAMDNodes NewAAInfo = AAInfo;
8649 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8650
8651 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8652 bool isConstant =
8653 BatchAA && SrcVal &&
8654 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8655
8656 MachineMemOperand::Flags MMOFlags =
8658 SmallVector<SDValue, 16> OutLoadChains;
8659 SmallVector<SDValue, 16> OutStoreChains;
8660 SmallVector<SDValue, 32> OutChains;
8661 unsigned NumMemOps = MemOps.size();
8662 uint64_t SrcOff = 0, DstOff = 0;
8663 for (unsigned i = 0; i != NumMemOps; ++i) {
8664 EVT VT = MemOps[i];
8665 unsigned VTSize = VT.getSizeInBits() / 8;
8666 SDValue Value, Store;
8667
8668 if (VTSize > Size) {
8669 // Issuing an unaligned load / store pair that overlaps with the previous
8670 // pair. Adjust the offset accordingly.
8671 assert(i == NumMemOps-1 && i != 0);
8672 SrcOff -= VTSize - Size;
8673 DstOff -= VTSize - Size;
8674 }
8675
8676 if (CopyFromConstant &&
8677 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8678 // It's unlikely a store of a vector immediate can be done in a single
8679 // instruction. It would require a load from a constantpool first.
8680 // We only handle zero vectors here.
8681 // FIXME: Handle other cases where store of vector immediate is done in
8682 // a single instruction.
8683 ConstantDataArraySlice SubSlice;
8684 if (SrcOff < Slice.Length) {
8685 SubSlice = Slice;
8686 SubSlice.move(SrcOff);
8687 } else {
8688 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8689 SubSlice.Array = nullptr;
8690 SubSlice.Offset = 0;
8691 SubSlice.Length = VTSize;
8692 }
8693 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8694 if (Value.getNode()) {
8695 Store = DAG.getStore(
8696 Chain, dl, Value,
8697 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8698 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8699 OutChains.push_back(Store);
8700 }
8701 }
8702
8703 if (!Store.getNode()) {
8704 // The type might not be legal for the target. This should only happen
8705 // if the type is smaller than a legal type, as on PPC, so the right
8706 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8707 // to Load/Store if NVT==VT.
8708 // FIXME does the case above also need this?
8709 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8710 assert(NVT.bitsGE(VT));
8711
8712 bool isDereferenceable =
8713 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8714 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8715 if (isDereferenceable)
8717 if (isConstant)
8718 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8719
8720 Value = DAG.getExtLoad(
8721 ISD::EXTLOAD, dl, NVT, Chain,
8722 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8723 SrcPtrInfo.getWithOffset(SrcOff), VT,
8724 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8725 OutLoadChains.push_back(Value.getValue(1));
8726
8727 Store = DAG.getTruncStore(
8728 Chain, dl, Value,
8729 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8730 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8731 OutStoreChains.push_back(Store);
8732 }
8733 SrcOff += VTSize;
8734 DstOff += VTSize;
8735 Size -= VTSize;
8736 }
8737
8738 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8740 unsigned NumLdStInMemcpy = OutStoreChains.size();
8741
8742 if (NumLdStInMemcpy) {
8743 // It may be that memcpy might be converted to memset if it's memcpy
8744 // of constants. In such a case, we won't have loads and stores, but
8745 // just stores. In the absence of loads, there is nothing to gang up.
8746 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8747 // If target does not care, just leave as it.
8748 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8749 OutChains.push_back(OutLoadChains[i]);
8750 OutChains.push_back(OutStoreChains[i]);
8751 }
8752 } else {
8753 // Ld/St less than/equal limit set by target.
8754 if (NumLdStInMemcpy <= GluedLdStLimit) {
8755 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8756 NumLdStInMemcpy, OutLoadChains,
8757 OutStoreChains);
8758 } else {
8759 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8760 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8761 unsigned GlueIter = 0;
8762
8763 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8764 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8765 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8766
8767 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8768 OutLoadChains, OutStoreChains);
8769 GlueIter += GluedLdStLimit;
8770 }
8771
8772 // Residual ld/st.
8773 if (RemainingLdStInMemcpy) {
8774 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8775 RemainingLdStInMemcpy, OutLoadChains,
8776 OutStoreChains);
8777 }
8778 }
8779 }
8780 }
8781 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8782}
8783
8785 SDValue Chain, SDValue Dst, SDValue Src,
8786 uint64_t Size, Align Alignment,
8787 bool isVol, bool AlwaysInline,
8788 MachinePointerInfo DstPtrInfo,
8789 MachinePointerInfo SrcPtrInfo,
8790 const AAMDNodes &AAInfo) {
8791 // Turn a memmove of undef to nop.
8792 // FIXME: We need to honor volatile even is Src is undef.
8793 if (Src.isUndef())
8794 return Chain;
8795
8796 // Expand memmove to a series of load and store ops if the size operand falls
8797 // below a certain threshold.
8798 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8799 const DataLayout &DL = DAG.getDataLayout();
8800 LLVMContext &C = *DAG.getContext();
8801 std::vector<EVT> MemOps;
8802 bool DstAlignCanChange = false;
8804 MachineFrameInfo &MFI = MF.getFrameInfo();
8805 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8807 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8808 DstAlignCanChange = true;
8809 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8810 if (!SrcAlign || Alignment > *SrcAlign)
8811 SrcAlign = Alignment;
8812 assert(SrcAlign && "SrcAlign must be set");
8813 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8814 if (!TLI.findOptimalMemOpLowering(
8815 C, MemOps, Limit,
8816 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8817 /*IsVolatile*/ true),
8818 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8819 MF.getFunction().getAttributes()))
8820 return SDValue();
8821
8822 if (DstAlignCanChange) {
8823 Type *Ty = MemOps[0].getTypeForEVT(C);
8824 Align NewAlign = DL.getABITypeAlign(Ty);
8825
8826 // Don't promote to an alignment that would require dynamic stack
8827 // realignment which may conflict with optimizations such as tail call
8828 // optimization.
8830 if (!TRI->hasStackRealignment(MF))
8831 if (MaybeAlign StackAlign = DL.getStackAlignment())
8832 NewAlign = std::min(NewAlign, *StackAlign);
8833
8834 if (NewAlign > Alignment) {
8835 // Give the stack frame object a larger alignment if needed.
8836 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8837 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8838 Alignment = NewAlign;
8839 }
8840 }
8841
8842 // Prepare AAInfo for loads/stores after lowering this memmove.
8843 AAMDNodes NewAAInfo = AAInfo;
8844 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8845
8846 MachineMemOperand::Flags MMOFlags =
8848 uint64_t SrcOff = 0, DstOff = 0;
8849 SmallVector<SDValue, 8> LoadValues;
8850 SmallVector<SDValue, 8> LoadChains;
8851 SmallVector<SDValue, 8> OutChains;
8852 unsigned NumMemOps = MemOps.size();
8853 for (unsigned i = 0; i < NumMemOps; i++) {
8854 EVT VT = MemOps[i];
8855 unsigned VTSize = VT.getSizeInBits() / 8;
8856 SDValue Value;
8857
8858 bool isDereferenceable =
8859 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8860 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8861 if (isDereferenceable)
8863
8864 Value = DAG.getLoad(
8865 VT, dl, Chain,
8866 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8867 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8868 LoadValues.push_back(Value);
8869 LoadChains.push_back(Value.getValue(1));
8870 SrcOff += VTSize;
8871 }
8872 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8873 OutChains.clear();
8874 for (unsigned i = 0; i < NumMemOps; i++) {
8875 EVT VT = MemOps[i];
8876 unsigned VTSize = VT.getSizeInBits() / 8;
8877 SDValue Store;
8878
8879 Store = DAG.getStore(
8880 Chain, dl, LoadValues[i],
8881 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8882 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8883 OutChains.push_back(Store);
8884 DstOff += VTSize;
8885 }
8886
8887 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8888}
8889
8890/// Lower the call to 'memset' intrinsic function into a series of store
8891/// operations.
8892///
8893/// \param DAG Selection DAG where lowered code is placed.
8894/// \param dl Link to corresponding IR location.
8895/// \param Chain Control flow dependency.
8896/// \param Dst Pointer to destination memory location.
8897/// \param Src Value of byte to write into the memory.
8898/// \param Size Number of bytes to write.
8899/// \param Alignment Alignment of the destination in bytes.
8900/// \param isVol True if destination is volatile.
8901/// \param AlwaysInline Makes sure no function call is generated.
8902/// \param DstPtrInfo IR information on the memory pointer.
8903/// \returns New head in the control flow, if lowering was successful, empty
8904/// SDValue otherwise.
8905///
8906/// The function tries to replace 'llvm.memset' intrinsic with several store
8907/// operations and value calculation code. This is usually profitable for small
8908/// memory size or when the semantic requires inlining.
8910 SDValue Chain, SDValue Dst, SDValue Src,
8911 uint64_t Size, Align Alignment, bool isVol,
8912 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8913 const AAMDNodes &AAInfo) {
8914 // Turn a memset of undef to nop.
8915 // FIXME: We need to honor volatile even is Src is undef.
8916 if (Src.isUndef())
8917 return Chain;
8918
8919 // Expand memset to a series of load/store ops if the size operand
8920 // falls below a certain threshold.
8921 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8922 std::vector<EVT> MemOps;
8923 bool DstAlignCanChange = false;
8924 LLVMContext &C = *DAG.getContext();
8926 MachineFrameInfo &MFI = MF.getFrameInfo();
8927 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8929 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8930 DstAlignCanChange = true;
8931 bool IsZeroVal = isNullConstant(Src);
8932 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
8933
8934 if (!TLI.findOptimalMemOpLowering(
8935 C, MemOps, Limit,
8936 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
8937 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
8938 return SDValue();
8939
8940 if (DstAlignCanChange) {
8941 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
8942 const DataLayout &DL = DAG.getDataLayout();
8943 Align NewAlign = DL.getABITypeAlign(Ty);
8944
8945 // Don't promote to an alignment that would require dynamic stack
8946 // realignment which may conflict with optimizations such as tail call
8947 // optimization.
8949 if (!TRI->hasStackRealignment(MF))
8950 if (MaybeAlign StackAlign = DL.getStackAlignment())
8951 NewAlign = std::min(NewAlign, *StackAlign);
8952
8953 if (NewAlign > Alignment) {
8954 // Give the stack frame object a larger alignment if needed.
8955 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8956 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8957 Alignment = NewAlign;
8958 }
8959 }
8960
8961 SmallVector<SDValue, 8> OutChains;
8962 uint64_t DstOff = 0;
8963 unsigned NumMemOps = MemOps.size();
8964
8965 // Find the largest store and generate the bit pattern for it.
8966 EVT LargestVT = MemOps[0];
8967 for (unsigned i = 1; i < NumMemOps; i++)
8968 if (MemOps[i].bitsGT(LargestVT))
8969 LargestVT = MemOps[i];
8970 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
8971
8972 // Prepare AAInfo for loads/stores after lowering this memset.
8973 AAMDNodes NewAAInfo = AAInfo;
8974 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8975
8976 for (unsigned i = 0; i < NumMemOps; i++) {
8977 EVT VT = MemOps[i];
8978 unsigned VTSize = VT.getSizeInBits() / 8;
8979 if (VTSize > Size) {
8980 // Issuing an unaligned load / store pair that overlaps with the previous
8981 // pair. Adjust the offset accordingly.
8982 assert(i == NumMemOps-1 && i != 0);
8983 DstOff -= VTSize - Size;
8984 }
8985
8986 // If this store is smaller than the largest store see whether we can get
8987 // the smaller value for free with a truncate or extract vector element and
8988 // then store.
8989 SDValue Value = MemSetValue;
8990 if (VT.bitsLT(LargestVT)) {
8991 unsigned Index;
8992 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
8993 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
8994 if (!LargestVT.isVector() && !VT.isVector() &&
8995 TLI.isTruncateFree(LargestVT, VT))
8996 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
8997 else if (LargestVT.isVector() && !VT.isVector() &&
8999 LargestVT.getTypeForEVT(*DAG.getContext()),
9000 VT.getSizeInBits(), Index) &&
9001 TLI.isTypeLegal(SVT) &&
9002 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9003 // Target which can combine store(extractelement VectorTy, Idx) can get
9004 // the smaller value for free.
9005 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9006 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9007 } else
9008 Value = getMemsetValue(Src, VT, DAG, dl);
9009 }
9010 assert(Value.getValueType() == VT && "Value with wrong type.");
9011 SDValue Store = DAG.getStore(
9012 Chain, dl, Value,
9013 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9014 DstPtrInfo.getWithOffset(DstOff), Alignment,
9016 NewAAInfo);
9017 OutChains.push_back(Store);
9018 DstOff += VT.getSizeInBits() / 8;
9019 Size -= VTSize;
9020 }
9021
9022 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9023}
9024
9026 unsigned AS) {
9027 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9028 // pointer operands can be losslessly bitcasted to pointers of address space 0
9029 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9030 report_fatal_error("cannot lower memory intrinsic in address space " +
9031 Twine(AS));
9032 }
9033}
9034
9036 const SelectionDAG *SelDAG,
9037 bool AllowReturnsFirstArg) {
9038 if (!CI || !CI->isTailCall())
9039 return false;
9040 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9041 // helper symbol we lower to.
9042 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9043 AllowReturnsFirstArg &&
9045}
9046
9047std::pair<SDValue, SDValue>
9049 SDValue Mem1, SDValue Size, const CallInst *CI) {
9050 const char *LibCallName = TLI->getLibcallName(RTLIB::MEMCMP);
9051 if (!LibCallName)
9052 return {};
9053
9056 {Mem0, PT},
9057 {Mem1, PT},
9059
9061 bool IsTailCall =
9062 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9063
9064 CLI.setDebugLoc(dl)
9065 .setChain(Chain)
9066 .setLibCallee(
9067 TLI->getLibcallCallingConv(RTLIB::MEMCMP),
9069 getExternalSymbol(LibCallName, TLI->getPointerTy(getDataLayout())),
9070 std::move(Args))
9071 .setTailCall(IsTailCall);
9072
9073 return TLI->LowerCallTo(CLI);
9074}
9075
9076std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9077 const SDLoc &dl,
9078 SDValue Src,
9079 const CallInst *CI) {
9080 const char *LibCallName = TLI->getLibcallName(RTLIB::STRLEN);
9081 if (!LibCallName)
9082 return {};
9083
9084 // Emit a library call.
9087
9089 bool IsTailCall =
9090 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9091
9092 CLI.setDebugLoc(dl)
9093 .setChain(Chain)
9094 .setLibCallee(TLI->getLibcallCallingConv(RTLIB::STRLEN), CI->getType(),
9096 LibCallName, TLI->getProgramPointerTy(getDataLayout())),
9097 std::move(Args))
9098 .setTailCall(IsTailCall);
9099
9100 return TLI->LowerCallTo(CLI);
9101}
9102
9104 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9105 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9106 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9107 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9108 BatchAAResults *BatchAA) {
9109 // Check to see if we should lower the memcpy to loads and stores first.
9110 // For cases within the target-specified limits, this is the best choice.
9112 if (ConstantSize) {
9113 // Memcpy with size zero? Just return the original chain.
9114 if (ConstantSize->isZero())
9115 return Chain;
9116
9118 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9119 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9120 if (Result.getNode())
9121 return Result;
9122 }
9123
9124 // Then check to see if we should lower the memcpy with target-specific
9125 // code. If the target chooses to do this, this is the next best.
9126 if (TSI) {
9127 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9128 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9129 DstPtrInfo, SrcPtrInfo);
9130 if (Result.getNode())
9131 return Result;
9132 }
9133
9134 // If we really need inline code and the target declined to provide it,
9135 // use a (potentially long) sequence of loads and stores.
9136 if (AlwaysInline) {
9137 assert(ConstantSize && "AlwaysInline requires a constant size!");
9139 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9140 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9141 }
9142
9145
9146 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9147 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9148 // respect volatile, so they may do things like read or write memory
9149 // beyond the given memory regions. But fixing this isn't easy, and most
9150 // people don't care.
9151
9152 // Emit a library call.
9155 Args.emplace_back(Dst, PtrTy);
9156 Args.emplace_back(Src, PtrTy);
9157 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9158 // FIXME: pass in SDLoc
9160 bool IsTailCall = false;
9161 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9162
9163 if (OverrideTailCall.has_value()) {
9164 IsTailCall = *OverrideTailCall;
9165 } else {
9166 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9167 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9168 }
9169
9170 CLI.setDebugLoc(dl)
9171 .setChain(Chain)
9172 .setLibCallee(
9173 TLI->getLibcallImplCallingConv(MemCpyImpl),
9174 Dst.getValueType().getTypeForEVT(*getContext()),
9175 getExternalSymbol(TLI->getLibcallImplName(MemCpyImpl).data(),
9176 TLI->getPointerTy(getDataLayout())),
9177 std::move(Args))
9179 .setTailCall(IsTailCall);
9180
9181 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9182 return CallResult.second;
9183}
9184
9186 SDValue Dst, SDValue Src, SDValue Size,
9187 Type *SizeTy, unsigned ElemSz,
9188 bool isTailCall,
9189 MachinePointerInfo DstPtrInfo,
9190 MachinePointerInfo SrcPtrInfo) {
9191 // Emit a library call.
9194 Args.emplace_back(Dst, ArgTy);
9195 Args.emplace_back(Src, ArgTy);
9196 Args.emplace_back(Size, SizeTy);
9197
9198 RTLIB::Libcall LibraryCall =
9200 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9201 report_fatal_error("Unsupported element size");
9202
9204 CLI.setDebugLoc(dl)
9205 .setChain(Chain)
9206 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9208 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9209 TLI->getPointerTy(getDataLayout())),
9210 std::move(Args))
9212 .setTailCall(isTailCall);
9213
9214 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9215 return CallResult.second;
9216}
9217
9219 SDValue Src, SDValue Size, Align Alignment,
9220 bool isVol, const CallInst *CI,
9221 std::optional<bool> OverrideTailCall,
9222 MachinePointerInfo DstPtrInfo,
9223 MachinePointerInfo SrcPtrInfo,
9224 const AAMDNodes &AAInfo,
9225 BatchAAResults *BatchAA) {
9226 // Check to see if we should lower the memmove to loads and stores first.
9227 // For cases within the target-specified limits, this is the best choice.
9229 if (ConstantSize) {
9230 // Memmove with size zero? Just return the original chain.
9231 if (ConstantSize->isZero())
9232 return Chain;
9233
9235 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9236 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9237 if (Result.getNode())
9238 return Result;
9239 }
9240
9241 // Then check to see if we should lower the memmove with target-specific
9242 // code. If the target chooses to do this, this is the next best.
9243 if (TSI) {
9244 SDValue Result =
9245 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9246 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9247 if (Result.getNode())
9248 return Result;
9249 }
9250
9253
9254 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9255 // not be safe. See memcpy above for more details.
9256
9257 // Emit a library call.
9260 Args.emplace_back(Dst, PtrTy);
9261 Args.emplace_back(Src, PtrTy);
9262 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9263 // FIXME: pass in SDLoc
9265
9266 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9267
9268 bool IsTailCall = false;
9269 if (OverrideTailCall.has_value()) {
9270 IsTailCall = *OverrideTailCall;
9271 } else {
9272 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9273 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9274 }
9275
9276 CLI.setDebugLoc(dl)
9277 .setChain(Chain)
9278 .setLibCallee(
9279 TLI->getLibcallImplCallingConv(MemmoveImpl),
9280 Dst.getValueType().getTypeForEVT(*getContext()),
9281 getExternalSymbol(TLI->getLibcallImplName(MemmoveImpl).data(),
9282 TLI->getPointerTy(getDataLayout())),
9283 std::move(Args))
9285 .setTailCall(IsTailCall);
9286
9287 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9288 return CallResult.second;
9289}
9290
9292 SDValue Dst, SDValue Src, SDValue Size,
9293 Type *SizeTy, unsigned ElemSz,
9294 bool isTailCall,
9295 MachinePointerInfo DstPtrInfo,
9296 MachinePointerInfo SrcPtrInfo) {
9297 // Emit a library call.
9299 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9300 Args.emplace_back(Dst, IntPtrTy);
9301 Args.emplace_back(Src, IntPtrTy);
9302 Args.emplace_back(Size, SizeTy);
9303
9304 RTLIB::Libcall LibraryCall =
9306 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9307 report_fatal_error("Unsupported element size");
9308
9310 CLI.setDebugLoc(dl)
9311 .setChain(Chain)
9312 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9314 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9315 TLI->getPointerTy(getDataLayout())),
9316 std::move(Args))
9318 .setTailCall(isTailCall);
9319
9320 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9321 return CallResult.second;
9322}
9323
9325 SDValue Src, SDValue Size, Align Alignment,
9326 bool isVol, bool AlwaysInline,
9327 const CallInst *CI,
9328 MachinePointerInfo DstPtrInfo,
9329 const AAMDNodes &AAInfo) {
9330 // Check to see if we should lower the memset to stores first.
9331 // For cases within the target-specified limits, this is the best choice.
9333 if (ConstantSize) {
9334 // Memset with size zero? Just return the original chain.
9335 if (ConstantSize->isZero())
9336 return Chain;
9337
9338 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9339 ConstantSize->getZExtValue(), Alignment,
9340 isVol, false, DstPtrInfo, AAInfo);
9341
9342 if (Result.getNode())
9343 return Result;
9344 }
9345
9346 // Then check to see if we should lower the memset with target-specific
9347 // code. If the target chooses to do this, this is the next best.
9348 if (TSI) {
9349 SDValue Result = TSI->EmitTargetCodeForMemset(
9350 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9351 if (Result.getNode())
9352 return Result;
9353 }
9354
9355 // If we really need inline code and the target declined to provide it,
9356 // use a (potentially long) sequence of loads and stores.
9357 if (AlwaysInline) {
9358 assert(ConstantSize && "AlwaysInline requires a constant size!");
9359 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9360 ConstantSize->getZExtValue(), Alignment,
9361 isVol, true, DstPtrInfo, AAInfo);
9362 assert(Result &&
9363 "getMemsetStores must return a valid sequence when AlwaysInline");
9364 return Result;
9365 }
9366
9368
9369 // Emit a library call.
9370 auto &Ctx = *getContext();
9371 const auto& DL = getDataLayout();
9372
9374 // FIXME: pass in SDLoc
9375 CLI.setDebugLoc(dl).setChain(Chain);
9376
9377 const char *BzeroName = getTargetLoweringInfo().getLibcallName(RTLIB::BZERO);
9378
9379 bool UseBZero = isNullConstant(Src) && BzeroName;
9380 // If zeroing out and bzero is present, use it.
9381 if (UseBZero) {
9383 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9384 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9385 CLI.setLibCallee(
9386 TLI->getLibcallCallingConv(RTLIB::BZERO), Type::getVoidTy(Ctx),
9387 getExternalSymbol(BzeroName, TLI->getPointerTy(DL)), std::move(Args));
9388 } else {
9390 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9391 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9392 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9393 CLI.setLibCallee(TLI->getLibcallCallingConv(RTLIB::MEMSET),
9394 Dst.getValueType().getTypeForEVT(Ctx),
9395 getExternalSymbol(TLI->getLibcallName(RTLIB::MEMSET),
9396 TLI->getPointerTy(DL)),
9397 std::move(Args));
9398 }
9399
9400 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9401 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9402
9403 // If we're going to use bzero, make sure not to tail call unless the
9404 // subsequent return doesn't need a value, as bzero doesn't return the first
9405 // arg unlike memset.
9406 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9407 bool IsTailCall =
9408 CI && CI->isTailCall() &&
9409 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9410 CLI.setDiscardResult().setTailCall(IsTailCall);
9411
9412 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9413 return CallResult.second;
9414}
9415
9418 Type *SizeTy, unsigned ElemSz,
9419 bool isTailCall,
9420 MachinePointerInfo DstPtrInfo) {
9421 // Emit a library call.
9423 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9424 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9425 Args.emplace_back(Size, SizeTy);
9426
9427 RTLIB::Libcall LibraryCall =
9429 if (LibraryCall == RTLIB::UNKNOWN_LIBCALL)
9430 report_fatal_error("Unsupported element size");
9431
9433 CLI.setDebugLoc(dl)
9434 .setChain(Chain)
9435 .setLibCallee(TLI->getLibcallCallingConv(LibraryCall),
9437 getExternalSymbol(TLI->getLibcallName(LibraryCall),
9438 TLI->getPointerTy(getDataLayout())),
9439 std::move(Args))
9441 .setTailCall(isTailCall);
9442
9443 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9444 return CallResult.second;
9445}
9446
9447SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9449 MachineMemOperand *MMO,
9450 ISD::LoadExtType ExtType) {
9452 AddNodeIDNode(ID, Opcode, VTList, Ops);
9453 ID.AddInteger(MemVT.getRawBits());
9454 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9455 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9456 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9457 ID.AddInteger(MMO->getFlags());
9458 void* IP = nullptr;
9459 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9460 E->refineAlignment(MMO);
9461 E->refineRanges(MMO);
9462 return SDValue(E, 0);
9463 }
9464
9465 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9466 VTList, MemVT, MMO, ExtType);
9467 createOperands(N, Ops);
9468
9469 CSEMap.InsertNode(N, IP);
9470 InsertNode(N);
9471 SDValue V(N, 0);
9472 NewSDValueDbgMsg(V, "Creating new node: ", this);
9473 return V;
9474}
9475
9477 EVT MemVT, SDVTList VTs, SDValue Chain,
9478 SDValue Ptr, SDValue Cmp, SDValue Swp,
9479 MachineMemOperand *MMO) {
9480 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9481 Opcode == ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
9482 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9483
9484 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9485 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9486}
9487
9488SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9489 SDValue Chain, SDValue Ptr, SDValue Val,
9490 MachineMemOperand *MMO) {
9491 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9492 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9493 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9494 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9495 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9496 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9497 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9498 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9499 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9500 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9501 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9502 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9503 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9504 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9505 Opcode == ISD::ATOMIC_STORE) &&
9506 "Invalid Atomic Op");
9507
9508 EVT VT = Val.getValueType();
9509
9510 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9511 getVTList(VT, MVT::Other);
9512 SDValue Ops[] = {Chain, Ptr, Val};
9513 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9514}
9515
9517 EVT MemVT, EVT VT, SDValue Chain,
9518 SDValue Ptr, MachineMemOperand *MMO) {
9519 SDVTList VTs = getVTList(VT, MVT::Other);
9520 SDValue Ops[] = {Chain, Ptr};
9521 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9522}
9523
9524/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9526 if (Ops.size() == 1)
9527 return Ops[0];
9528
9530 VTs.reserve(Ops.size());
9531 for (const SDValue &Op : Ops)
9532 VTs.push_back(Op.getValueType());
9533 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9534}
9535
9537 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9538 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9540 const AAMDNodes &AAInfo) {
9541 if (Size.hasValue() && !Size.getValue())
9543
9545 MachineMemOperand *MMO =
9546 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9547
9548 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9549}
9550
9552 SDVTList VTList,
9553 ArrayRef<SDValue> Ops, EVT MemVT,
9554 MachineMemOperand *MMO) {
9555 assert(
9556 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9557 Opcode == ISD::PREFETCH ||
9558 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9559 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9560 "Opcode is not a memory-accessing opcode!");
9561
9562 // Memoize the node unless it returns a glue result.
9564 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9566 AddNodeIDNode(ID, Opcode, VTList, Ops);
9567 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9568 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9569 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9570 ID.AddInteger(MMO->getFlags());
9571 ID.AddInteger(MemVT.getRawBits());
9572 void *IP = nullptr;
9573 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9574 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9575 return SDValue(E, 0);
9576 }
9577
9578 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9579 VTList, MemVT, MMO);
9580 createOperands(N, Ops);
9581
9582 CSEMap.InsertNode(N, IP);
9583 } else {
9584 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9585 VTList, MemVT, MMO);
9586 createOperands(N, Ops);
9587 }
9588 InsertNode(N);
9589 SDValue V(N, 0);
9590 NewSDValueDbgMsg(V, "Creating new node: ", this);
9591 return V;
9592}
9593
9595 SDValue Chain, int FrameIndex) {
9596 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9597 const auto VTs = getVTList(MVT::Other);
9598 SDValue Ops[2] = {
9599 Chain,
9600 getFrameIndex(FrameIndex,
9601 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9602 true)};
9603
9605 AddNodeIDNode(ID, Opcode, VTs, Ops);
9606 ID.AddInteger(FrameIndex);
9607 void *IP = nullptr;
9608 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9609 return SDValue(E, 0);
9610
9611 LifetimeSDNode *N =
9612 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9613 createOperands(N, Ops);
9614 CSEMap.InsertNode(N, IP);
9615 InsertNode(N);
9616 SDValue V(N, 0);
9617 NewSDValueDbgMsg(V, "Creating new node: ", this);
9618 return V;
9619}
9620
9622 uint64_t Guid, uint64_t Index,
9623 uint32_t Attr) {
9624 const unsigned Opcode = ISD::PSEUDO_PROBE;
9625 const auto VTs = getVTList(MVT::Other);
9626 SDValue Ops[] = {Chain};
9628 AddNodeIDNode(ID, Opcode, VTs, Ops);
9629 ID.AddInteger(Guid);
9630 ID.AddInteger(Index);
9631 void *IP = nullptr;
9632 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9633 return SDValue(E, 0);
9634
9635 auto *N = newSDNode<PseudoProbeSDNode>(
9636 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9637 createOperands(N, Ops);
9638 CSEMap.InsertNode(N, IP);
9639 InsertNode(N);
9640 SDValue V(N, 0);
9641 NewSDValueDbgMsg(V, "Creating new node: ", this);
9642 return V;
9643}
9644
9645/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9646/// MachinePointerInfo record from it. This is particularly useful because the
9647/// code generator has many cases where it doesn't bother passing in a
9648/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9650 SelectionDAG &DAG, SDValue Ptr,
9651 int64_t Offset = 0) {
9652 // If this is FI+Offset, we can model it.
9653 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9655 FI->getIndex(), Offset);
9656
9657 // If this is (FI+Offset1)+Offset2, we can model it.
9658 if (Ptr.getOpcode() != ISD::ADD ||
9661 return Info;
9662
9663 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9665 DAG.getMachineFunction(), FI,
9666 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9667}
9668
9669/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9670/// MachinePointerInfo record from it. This is particularly useful because the
9671/// code generator has many cases where it doesn't bother passing in a
9672/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9674 SelectionDAG &DAG, SDValue Ptr,
9675 SDValue OffsetOp) {
9676 // If the 'Offset' value isn't a constant, we can't handle this.
9678 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9679 if (OffsetOp.isUndef())
9680 return InferPointerInfo(Info, DAG, Ptr);
9681 return Info;
9682}
9683
9685 EVT VT, const SDLoc &dl, SDValue Chain,
9686 SDValue Ptr, SDValue Offset,
9687 MachinePointerInfo PtrInfo, EVT MemVT,
9688 Align Alignment,
9689 MachineMemOperand::Flags MMOFlags,
9690 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9691 assert(Chain.getValueType() == MVT::Other &&
9692 "Invalid chain type");
9693
9694 MMOFlags |= MachineMemOperand::MOLoad;
9695 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9696 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9697 // clients.
9698 if (PtrInfo.V.isNull())
9699 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9700
9701 TypeSize Size = MemVT.getStoreSize();
9703 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9704 Alignment, AAInfo, Ranges);
9705 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9706}
9707
9709 EVT VT, const SDLoc &dl, SDValue Chain,
9710 SDValue Ptr, SDValue Offset, EVT MemVT,
9711 MachineMemOperand *MMO) {
9712 if (VT == MemVT) {
9713 ExtType = ISD::NON_EXTLOAD;
9714 } else if (ExtType == ISD::NON_EXTLOAD) {
9715 assert(VT == MemVT && "Non-extending load from different memory type!");
9716 } else {
9717 // Extending load.
9718 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9719 "Should only be an extending load, not truncating!");
9720 assert(VT.isInteger() == MemVT.isInteger() &&
9721 "Cannot convert from FP to Int or Int -> FP!");
9722 assert(VT.isVector() == MemVT.isVector() &&
9723 "Cannot use an ext load to convert to or from a vector!");
9724 assert((!VT.isVector() ||
9726 "Cannot use an ext load to change the number of vector elements!");
9727 }
9728
9729 assert((!MMO->getRanges() ||
9731 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9732 MemVT.isInteger())) &&
9733 "Range metadata and load type must match!");
9734
9735 bool Indexed = AM != ISD::UNINDEXED;
9736 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9737
9738 SDVTList VTs = Indexed ?
9739 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9740 SDValue Ops[] = { Chain, Ptr, Offset };
9742 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9743 ID.AddInteger(MemVT.getRawBits());
9744 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9745 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9746 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9747 ID.AddInteger(MMO->getFlags());
9748 void *IP = nullptr;
9749 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9750 E->refineAlignment(MMO);
9751 E->refineRanges(MMO);
9752 return SDValue(E, 0);
9753 }
9754 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9755 ExtType, MemVT, MMO);
9756 createOperands(N, Ops);
9757
9758 CSEMap.InsertNode(N, IP);
9759 InsertNode(N);
9760 SDValue V(N, 0);
9761 NewSDValueDbgMsg(V, "Creating new node: ", this);
9762 return V;
9763}
9764
9766 SDValue Ptr, MachinePointerInfo PtrInfo,
9767 MaybeAlign Alignment,
9768 MachineMemOperand::Flags MMOFlags,
9769 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9770 SDValue Undef = getUNDEF(Ptr.getValueType());
9771 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9772 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9773}
9774
9776 SDValue Ptr, MachineMemOperand *MMO) {
9777 SDValue Undef = getUNDEF(Ptr.getValueType());
9778 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9779 VT, MMO);
9780}
9781
9783 EVT VT, SDValue Chain, SDValue Ptr,
9784 MachinePointerInfo PtrInfo, EVT MemVT,
9785 MaybeAlign Alignment,
9786 MachineMemOperand::Flags MMOFlags,
9787 const AAMDNodes &AAInfo) {
9788 SDValue Undef = getUNDEF(Ptr.getValueType());
9789 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9790 MemVT, Alignment, MMOFlags, AAInfo);
9791}
9792
9794 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9795 MachineMemOperand *MMO) {
9796 SDValue Undef = getUNDEF(Ptr.getValueType());
9797 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9798 MemVT, MMO);
9799}
9800
9804 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9805 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9806 // Don't propagate the invariant or dereferenceable flags.
9807 auto MMOFlags =
9808 LD->getMemOperand()->getFlags() &
9810 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9811 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9812 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9813}
9814
9816 SDValue Ptr, MachinePointerInfo PtrInfo,
9817 Align Alignment,
9818 MachineMemOperand::Flags MMOFlags,
9819 const AAMDNodes &AAInfo) {
9820 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9821
9822 MMOFlags |= MachineMemOperand::MOStore;
9823 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9824
9825 if (PtrInfo.V.isNull())
9826 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9827
9830 MachineMemOperand *MMO =
9831 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9832 return getStore(Chain, dl, Val, Ptr, MMO);
9833}
9834
9836 SDValue Ptr, MachineMemOperand *MMO) {
9837 SDValue Undef = getUNDEF(Ptr.getValueType());
9838 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9840}
9841
9843 SDValue Ptr, SDValue Offset, EVT SVT,
9845 bool IsTruncating) {
9846 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9847 EVT VT = Val.getValueType();
9848 if (VT == SVT) {
9849 IsTruncating = false;
9850 } else if (!IsTruncating) {
9851 assert(VT == SVT && "No-truncating store from different memory type!");
9852 } else {
9854 "Should only be a truncating store, not extending!");
9855 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9856 assert(VT.isVector() == SVT.isVector() &&
9857 "Cannot use trunc store to convert to or from a vector!");
9858 assert((!VT.isVector() ||
9860 "Cannot use trunc store to change the number of vector elements!");
9861 }
9862
9863 bool Indexed = AM != ISD::UNINDEXED;
9864 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9865 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9866 : getVTList(MVT::Other);
9867 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9869 AddNodeIDNode(ID, ISD::STORE, VTs, Ops);
9870 ID.AddInteger(SVT.getRawBits());
9871 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9872 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9873 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9874 ID.AddInteger(MMO->getFlags());
9875 void *IP = nullptr;
9876 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9877 cast<StoreSDNode>(E)->refineAlignment(MMO);
9878 return SDValue(E, 0);
9879 }
9880 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9881 IsTruncating, SVT, MMO);
9882 createOperands(N, Ops);
9883
9884 CSEMap.InsertNode(N, IP);
9885 InsertNode(N);
9886 SDValue V(N, 0);
9887 NewSDValueDbgMsg(V, "Creating new node: ", this);
9888 return V;
9889}
9890
9892 SDValue Ptr, MachinePointerInfo PtrInfo,
9893 EVT SVT, Align Alignment,
9894 MachineMemOperand::Flags MMOFlags,
9895 const AAMDNodes &AAInfo) {
9896 assert(Chain.getValueType() == MVT::Other &&
9897 "Invalid chain type");
9898
9899 MMOFlags |= MachineMemOperand::MOStore;
9900 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9901
9902 if (PtrInfo.V.isNull())
9903 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9904
9906 MachineMemOperand *MMO = MF.getMachineMemOperand(
9907 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9908 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9909}
9910
9912 SDValue Ptr, EVT SVT,
9913 MachineMemOperand *MMO) {
9914 SDValue Undef = getUNDEF(Ptr.getValueType());
9915 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9916}
9917
9921 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9922 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9923 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9924 ST->getMemoryVT(), ST->getMemOperand(), AM,
9925 ST->isTruncatingStore());
9926}
9927
9929 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9930 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
9931 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
9932 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
9933 const MDNode *Ranges, bool IsExpanding) {
9934 MMOFlags |= MachineMemOperand::MOLoad;
9935 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9936 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9937 // clients.
9938 if (PtrInfo.V.isNull())
9939 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9940
9941 TypeSize Size = MemVT.getStoreSize();
9943 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9944 Alignment, AAInfo, Ranges);
9945 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
9946 MMO, IsExpanding);
9947}
9948
9950 ISD::LoadExtType ExtType, EVT VT,
9951 const SDLoc &dl, SDValue Chain, SDValue Ptr,
9952 SDValue Offset, SDValue Mask, SDValue EVL,
9953 EVT MemVT, MachineMemOperand *MMO,
9954 bool IsExpanding) {
9955 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9956 assert(Mask.getValueType().getVectorElementCount() ==
9957 VT.getVectorElementCount() &&
9958 "Vector width mismatch between mask and data");
9959
9960 bool Indexed = AM != ISD::UNINDEXED;
9961 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9962
9963 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
9964 : getVTList(VT, MVT::Other);
9965 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
9967 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
9968 ID.AddInteger(MemVT.getRawBits());
9969 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
9970 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
9971 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9972 ID.AddInteger(MMO->getFlags());
9973 void *IP = nullptr;
9974 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9975 E->refineAlignment(MMO);
9976 E->refineRanges(MMO);
9977 return SDValue(E, 0);
9978 }
9979 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9980 ExtType, IsExpanding, MemVT, MMO);
9981 createOperands(N, Ops);
9982
9983 CSEMap.InsertNode(N, IP);
9984 InsertNode(N);
9985 SDValue V(N, 0);
9986 NewSDValueDbgMsg(V, "Creating new node: ", this);
9987 return V;
9988}
9989
9991 SDValue Ptr, SDValue Mask, SDValue EVL,
9992 MachinePointerInfo PtrInfo,
9993 MaybeAlign Alignment,
9994 MachineMemOperand::Flags MMOFlags,
9995 const AAMDNodes &AAInfo, const MDNode *Ranges,
9996 bool IsExpanding) {
9997 SDValue Undef = getUNDEF(Ptr.getValueType());
9998 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9999 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10000 IsExpanding);
10001}
10002
10004 SDValue Ptr, SDValue Mask, SDValue EVL,
10005 MachineMemOperand *MMO, bool IsExpanding) {
10006 SDValue Undef = getUNDEF(Ptr.getValueType());
10007 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10008 Mask, EVL, VT, MMO, IsExpanding);
10009}
10010
10012 EVT VT, SDValue Chain, SDValue Ptr,
10013 SDValue Mask, SDValue EVL,
10014 MachinePointerInfo PtrInfo, EVT MemVT,
10015 MaybeAlign Alignment,
10016 MachineMemOperand::Flags MMOFlags,
10017 const AAMDNodes &AAInfo, bool IsExpanding) {
10018 SDValue Undef = getUNDEF(Ptr.getValueType());
10019 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10020 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10021 IsExpanding);
10022}
10023
10025 EVT VT, SDValue Chain, SDValue Ptr,
10026 SDValue Mask, SDValue EVL, EVT MemVT,
10027 MachineMemOperand *MMO, bool IsExpanding) {
10028 SDValue Undef = getUNDEF(Ptr.getValueType());
10029 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10030 EVL, MemVT, MMO, IsExpanding);
10031}
10032
10036 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10037 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10038 // Don't propagate the invariant or dereferenceable flags.
10039 auto MMOFlags =
10040 LD->getMemOperand()->getFlags() &
10042 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10043 LD->getChain(), Base, Offset, LD->getMask(),
10044 LD->getVectorLength(), LD->getPointerInfo(),
10045 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10046 nullptr, LD->isExpandingLoad());
10047}
10048
10050 SDValue Ptr, SDValue Offset, SDValue Mask,
10051 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10052 ISD::MemIndexedMode AM, bool IsTruncating,
10053 bool IsCompressing) {
10054 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10055 assert(Mask.getValueType().getVectorElementCount() ==
10057 "Vector width mismatch between mask and data");
10058
10059 bool Indexed = AM != ISD::UNINDEXED;
10060 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10061 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10062 : getVTList(MVT::Other);
10063 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10065 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10066 ID.AddInteger(MemVT.getRawBits());
10067 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10068 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10069 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10070 ID.AddInteger(MMO->getFlags());
10071 void *IP = nullptr;
10072 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10073 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10074 return SDValue(E, 0);
10075 }
10076 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10077 IsTruncating, IsCompressing, MemVT, MMO);
10078 createOperands(N, Ops);
10079
10080 CSEMap.InsertNode(N, IP);
10081 InsertNode(N);
10082 SDValue V(N, 0);
10083 NewSDValueDbgMsg(V, "Creating new node: ", this);
10084 return V;
10085}
10086
10088 SDValue Val, SDValue Ptr, SDValue Mask,
10089 SDValue EVL, MachinePointerInfo PtrInfo,
10090 EVT SVT, Align Alignment,
10091 MachineMemOperand::Flags MMOFlags,
10092 const AAMDNodes &AAInfo,
10093 bool IsCompressing) {
10094 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10095
10096 MMOFlags |= MachineMemOperand::MOStore;
10097 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10098
10099 if (PtrInfo.V.isNull())
10100 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10101
10103 MachineMemOperand *MMO = MF.getMachineMemOperand(
10104 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10105 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10106 IsCompressing);
10107}
10108
10110 SDValue Val, SDValue Ptr, SDValue Mask,
10111 SDValue EVL, EVT SVT,
10112 MachineMemOperand *MMO,
10113 bool IsCompressing) {
10114 EVT VT = Val.getValueType();
10115
10116 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10117 if (VT == SVT)
10118 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10119 EVL, VT, MMO, ISD::UNINDEXED,
10120 /*IsTruncating*/ false, IsCompressing);
10121
10123 "Should only be a truncating store, not extending!");
10124 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10125 assert(VT.isVector() == SVT.isVector() &&
10126 "Cannot use trunc store to convert to or from a vector!");
10127 assert((!VT.isVector() ||
10129 "Cannot use trunc store to change the number of vector elements!");
10130
10131 SDVTList VTs = getVTList(MVT::Other);
10132 SDValue Undef = getUNDEF(Ptr.getValueType());
10133 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10135 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10136 ID.AddInteger(SVT.getRawBits());
10137 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10138 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10139 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10140 ID.AddInteger(MMO->getFlags());
10141 void *IP = nullptr;
10142 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10143 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10144 return SDValue(E, 0);
10145 }
10146 auto *N =
10147 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10148 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10149 createOperands(N, Ops);
10150
10151 CSEMap.InsertNode(N, IP);
10152 InsertNode(N);
10153 SDValue V(N, 0);
10154 NewSDValueDbgMsg(V, "Creating new node: ", this);
10155 return V;
10156}
10157
10161 auto *ST = cast<VPStoreSDNode>(OrigStore);
10162 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10163 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10164 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10165 Offset, ST->getMask(), ST->getVectorLength()};
10167 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10168 ID.AddInteger(ST->getMemoryVT().getRawBits());
10169 ID.AddInteger(ST->getRawSubclassData());
10170 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10171 ID.AddInteger(ST->getMemOperand()->getFlags());
10172 void *IP = nullptr;
10173 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10174 return SDValue(E, 0);
10175
10176 auto *N = newSDNode<VPStoreSDNode>(
10177 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10178 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10179 createOperands(N, Ops);
10180
10181 CSEMap.InsertNode(N, IP);
10182 InsertNode(N);
10183 SDValue V(N, 0);
10184 NewSDValueDbgMsg(V, "Creating new node: ", this);
10185 return V;
10186}
10187
10189 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10190 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10191 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10192 bool Indexed = AM != ISD::UNINDEXED;
10193 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10194
10195 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10196 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10197 : getVTList(VT, MVT::Other);
10199 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10200 ID.AddInteger(VT.getRawBits());
10201 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10202 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10203 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10204
10205 void *IP = nullptr;
10206 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10207 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10208 return SDValue(E, 0);
10209 }
10210
10211 auto *N =
10212 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10213 ExtType, IsExpanding, MemVT, MMO);
10214 createOperands(N, Ops);
10215 CSEMap.InsertNode(N, IP);
10216 InsertNode(N);
10217 SDValue V(N, 0);
10218 NewSDValueDbgMsg(V, "Creating new node: ", this);
10219 return V;
10220}
10221
10223 SDValue Ptr, SDValue Stride,
10224 SDValue Mask, SDValue EVL,
10225 MachineMemOperand *MMO,
10226 bool IsExpanding) {
10227 SDValue Undef = getUNDEF(Ptr.getValueType());
10228 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10229 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10230}
10231
10233 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10234 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10235 MachineMemOperand *MMO, bool IsExpanding) {
10236 SDValue Undef = getUNDEF(Ptr.getValueType());
10237 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10238 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10239}
10240
10242 SDValue Val, SDValue Ptr,
10243 SDValue Offset, SDValue Stride,
10244 SDValue Mask, SDValue EVL, EVT MemVT,
10245 MachineMemOperand *MMO,
10247 bool IsTruncating, bool IsCompressing) {
10248 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10249 bool Indexed = AM != ISD::UNINDEXED;
10250 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10251 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10252 : getVTList(MVT::Other);
10253 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10255 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10256 ID.AddInteger(MemVT.getRawBits());
10257 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10258 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10259 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10260 void *IP = nullptr;
10261 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10262 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10263 return SDValue(E, 0);
10264 }
10265 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10266 VTs, AM, IsTruncating,
10267 IsCompressing, MemVT, MMO);
10268 createOperands(N, Ops);
10269
10270 CSEMap.InsertNode(N, IP);
10271 InsertNode(N);
10272 SDValue V(N, 0);
10273 NewSDValueDbgMsg(V, "Creating new node: ", this);
10274 return V;
10275}
10276
10278 SDValue Val, SDValue Ptr,
10279 SDValue Stride, SDValue Mask,
10280 SDValue EVL, EVT SVT,
10281 MachineMemOperand *MMO,
10282 bool IsCompressing) {
10283 EVT VT = Val.getValueType();
10284
10285 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10286 if (VT == SVT)
10287 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10288 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10289 /*IsTruncating*/ false, IsCompressing);
10290
10292 "Should only be a truncating store, not extending!");
10293 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10294 assert(VT.isVector() == SVT.isVector() &&
10295 "Cannot use trunc store to convert to or from a vector!");
10296 assert((!VT.isVector() ||
10298 "Cannot use trunc store to change the number of vector elements!");
10299
10300 SDVTList VTs = getVTList(MVT::Other);
10301 SDValue Undef = getUNDEF(Ptr.getValueType());
10302 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10304 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10305 ID.AddInteger(SVT.getRawBits());
10306 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10307 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10308 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10309 void *IP = nullptr;
10310 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10311 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10312 return SDValue(E, 0);
10313 }
10314 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10315 VTs, ISD::UNINDEXED, true,
10316 IsCompressing, SVT, MMO);
10317 createOperands(N, Ops);
10318
10319 CSEMap.InsertNode(N, IP);
10320 InsertNode(N);
10321 SDValue V(N, 0);
10322 NewSDValueDbgMsg(V, "Creating new node: ", this);
10323 return V;
10324}
10325
10328 ISD::MemIndexType IndexType) {
10329 assert(Ops.size() == 6 && "Incompatible number of operands");
10330
10332 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10333 ID.AddInteger(VT.getRawBits());
10334 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10335 dl.getIROrder(), VTs, VT, MMO, IndexType));
10336 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10337 ID.AddInteger(MMO->getFlags());
10338 void *IP = nullptr;
10339 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10340 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10341 return SDValue(E, 0);
10342 }
10343
10344 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10345 VT, MMO, IndexType);
10346 createOperands(N, Ops);
10347
10348 assert(N->getMask().getValueType().getVectorElementCount() ==
10349 N->getValueType(0).getVectorElementCount() &&
10350 "Vector width mismatch between mask and data");
10351 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10352 N->getValueType(0).getVectorElementCount().isScalable() &&
10353 "Scalable flags of index and data do not match");
10355 N->getIndex().getValueType().getVectorElementCount(),
10356 N->getValueType(0).getVectorElementCount()) &&
10357 "Vector width mismatch between index and data");
10358 assert(isa<ConstantSDNode>(N->getScale()) &&
10359 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10360 "Scale should be a constant power of 2");
10361
10362 CSEMap.InsertNode(N, IP);
10363 InsertNode(N);
10364 SDValue V(N, 0);
10365 NewSDValueDbgMsg(V, "Creating new node: ", this);
10366 return V;
10367}
10368
10371 MachineMemOperand *MMO,
10372 ISD::MemIndexType IndexType) {
10373 assert(Ops.size() == 7 && "Incompatible number of operands");
10374
10376 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10377 ID.AddInteger(VT.getRawBits());
10378 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10379 dl.getIROrder(), VTs, VT, MMO, IndexType));
10380 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10381 ID.AddInteger(MMO->getFlags());
10382 void *IP = nullptr;
10383 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10384 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10385 return SDValue(E, 0);
10386 }
10387 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10388 VT, MMO, IndexType);
10389 createOperands(N, Ops);
10390
10391 assert(N->getMask().getValueType().getVectorElementCount() ==
10392 N->getValue().getValueType().getVectorElementCount() &&
10393 "Vector width mismatch between mask and data");
10394 assert(
10395 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10396 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10397 "Scalable flags of index and data do not match");
10399 N->getIndex().getValueType().getVectorElementCount(),
10400 N->getValue().getValueType().getVectorElementCount()) &&
10401 "Vector width mismatch between index and data");
10402 assert(isa<ConstantSDNode>(N->getScale()) &&
10403 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10404 "Scale should be a constant power of 2");
10405
10406 CSEMap.InsertNode(N, IP);
10407 InsertNode(N);
10408 SDValue V(N, 0);
10409 NewSDValueDbgMsg(V, "Creating new node: ", this);
10410 return V;
10411}
10412
10415 SDValue PassThru, EVT MemVT,
10416 MachineMemOperand *MMO,
10418 ISD::LoadExtType ExtTy, bool isExpanding) {
10419 bool Indexed = AM != ISD::UNINDEXED;
10420 assert((Indexed || Offset.isUndef()) &&
10421 "Unindexed masked load with an offset!");
10422 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10423 : getVTList(VT, MVT::Other);
10424 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10426 AddNodeIDNode(ID, ISD::MLOAD, VTs, Ops);
10427 ID.AddInteger(MemVT.getRawBits());
10428 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10429 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10430 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10431 ID.AddInteger(MMO->getFlags());
10432 void *IP = nullptr;
10433 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10434 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10435 return SDValue(E, 0);
10436 }
10437 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10438 AM, ExtTy, isExpanding, MemVT, MMO);
10439 createOperands(N, Ops);
10440
10441 CSEMap.InsertNode(N, IP);
10442 InsertNode(N);
10443 SDValue V(N, 0);
10444 NewSDValueDbgMsg(V, "Creating new node: ", this);
10445 return V;
10446}
10447
10452 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10453 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10454 Offset, LD->getMask(), LD->getPassThru(),
10455 LD->getMemoryVT(), LD->getMemOperand(), AM,
10456 LD->getExtensionType(), LD->isExpandingLoad());
10457}
10458
10461 SDValue Mask, EVT MemVT,
10462 MachineMemOperand *MMO,
10463 ISD::MemIndexedMode AM, bool IsTruncating,
10464 bool IsCompressing) {
10465 assert(Chain.getValueType() == MVT::Other &&
10466 "Invalid chain type");
10467 bool Indexed = AM != ISD::UNINDEXED;
10468 assert((Indexed || Offset.isUndef()) &&
10469 "Unindexed masked store with an offset!");
10470 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10471 : getVTList(MVT::Other);
10472 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10474 AddNodeIDNode(ID, ISD::MSTORE, VTs, Ops);
10475 ID.AddInteger(MemVT.getRawBits());
10476 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10477 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10478 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10479 ID.AddInteger(MMO->getFlags());
10480 void *IP = nullptr;
10481 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10482 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10483 return SDValue(E, 0);
10484 }
10485 auto *N =
10486 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10487 IsTruncating, IsCompressing, MemVT, MMO);
10488 createOperands(N, Ops);
10489
10490 CSEMap.InsertNode(N, IP);
10491 InsertNode(N);
10492 SDValue V(N, 0);
10493 NewSDValueDbgMsg(V, "Creating new node: ", this);
10494 return V;
10495}
10496
10501 assert(ST->getOffset().isUndef() &&
10502 "Masked store is already a indexed store!");
10503 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10504 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10505 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10506}
10507
10510 MachineMemOperand *MMO,
10511 ISD::MemIndexType IndexType,
10512 ISD::LoadExtType ExtTy) {
10513 assert(Ops.size() == 6 && "Incompatible number of operands");
10514
10516 AddNodeIDNode(ID, ISD::MGATHER, VTs, Ops);
10517 ID.AddInteger(MemVT.getRawBits());
10518 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10519 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10520 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10521 ID.AddInteger(MMO->getFlags());
10522 void *IP = nullptr;
10523 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10524 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10525 return SDValue(E, 0);
10526 }
10527
10528 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10529 VTs, MemVT, MMO, IndexType, ExtTy);
10530 createOperands(N, Ops);
10531
10532 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10533 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10534 assert(N->getMask().getValueType().getVectorElementCount() ==
10535 N->getValueType(0).getVectorElementCount() &&
10536 "Vector width mismatch between mask and data");
10537 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10538 N->getValueType(0).getVectorElementCount().isScalable() &&
10539 "Scalable flags of index and data do not match");
10541 N->getIndex().getValueType().getVectorElementCount(),
10542 N->getValueType(0).getVectorElementCount()) &&
10543 "Vector width mismatch between index and data");
10544 assert(isa<ConstantSDNode>(N->getScale()) &&
10545 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10546 "Scale should be a constant power of 2");
10547
10548 CSEMap.InsertNode(N, IP);
10549 InsertNode(N);
10550 SDValue V(N, 0);
10551 NewSDValueDbgMsg(V, "Creating new node: ", this);
10552 return V;
10553}
10554
10557 MachineMemOperand *MMO,
10558 ISD::MemIndexType IndexType,
10559 bool IsTrunc) {
10560 assert(Ops.size() == 6 && "Incompatible number of operands");
10561
10563 AddNodeIDNode(ID, ISD::MSCATTER, VTs, Ops);
10564 ID.AddInteger(MemVT.getRawBits());
10565 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10566 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10567 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10568 ID.AddInteger(MMO->getFlags());
10569 void *IP = nullptr;
10570 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10571 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10572 return SDValue(E, 0);
10573 }
10574
10575 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10576 VTs, MemVT, MMO, IndexType, IsTrunc);
10577 createOperands(N, Ops);
10578
10579 assert(N->getMask().getValueType().getVectorElementCount() ==
10580 N->getValue().getValueType().getVectorElementCount() &&
10581 "Vector width mismatch between mask and data");
10582 assert(
10583 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10584 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10585 "Scalable flags of index and data do not match");
10587 N->getIndex().getValueType().getVectorElementCount(),
10588 N->getValue().getValueType().getVectorElementCount()) &&
10589 "Vector width mismatch between index and data");
10590 assert(isa<ConstantSDNode>(N->getScale()) &&
10591 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10592 "Scale should be a constant power of 2");
10593
10594 CSEMap.InsertNode(N, IP);
10595 InsertNode(N);
10596 SDValue V(N, 0);
10597 NewSDValueDbgMsg(V, "Creating new node: ", this);
10598 return V;
10599}
10600
10602 const SDLoc &dl, ArrayRef<SDValue> Ops,
10603 MachineMemOperand *MMO,
10604 ISD::MemIndexType IndexType) {
10605 assert(Ops.size() == 7 && "Incompatible number of operands");
10606
10608 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VECTOR_HISTOGRAM, VTs, Ops);
10609 ID.AddInteger(MemVT.getRawBits());
10610 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10611 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10612 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10613 ID.AddInteger(MMO->getFlags());
10614 void *IP = nullptr;
10615 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10616 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10617 return SDValue(E, 0);
10618 }
10619
10620 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10621 VTs, MemVT, MMO, IndexType);
10622 createOperands(N, Ops);
10623
10624 assert(N->getMask().getValueType().getVectorElementCount() ==
10625 N->getIndex().getValueType().getVectorElementCount() &&
10626 "Vector width mismatch between mask and data");
10627 assert(isa<ConstantSDNode>(N->getScale()) &&
10628 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10629 "Scale should be a constant power of 2");
10630 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10631
10632 CSEMap.InsertNode(N, IP);
10633 InsertNode(N);
10634 SDValue V(N, 0);
10635 NewSDValueDbgMsg(V, "Creating new node: ", this);
10636 return V;
10637}
10638
10640 SDValue Ptr, SDValue Mask, SDValue EVL,
10641 MachineMemOperand *MMO) {
10642 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10643 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10645 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10646 ID.AddInteger(VT.getRawBits());
10647 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10648 VTs, VT, MMO));
10649 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10650 ID.AddInteger(MMO->getFlags());
10651 void *IP = nullptr;
10652 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10653 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10654 return SDValue(E, 0);
10655 }
10656 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10657 VT, MMO);
10658 createOperands(N, Ops);
10659
10660 CSEMap.InsertNode(N, IP);
10661 InsertNode(N);
10662 SDValue V(N, 0);
10663 NewSDValueDbgMsg(V, "Creating new node: ", this);
10664 return V;
10665}
10666
10668 EVT MemVT, MachineMemOperand *MMO) {
10669 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10670 SDVTList VTs = getVTList(MVT::Other);
10671 SDValue Ops[] = {Chain, Ptr};
10673 AddNodeIDNode(ID, ISD::GET_FPENV_MEM, VTs, Ops);
10674 ID.AddInteger(MemVT.getRawBits());
10675 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10676 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10677 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10678 ID.AddInteger(MMO->getFlags());
10679 void *IP = nullptr;
10680 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10681 return SDValue(E, 0);
10682
10683 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10684 dl.getDebugLoc(), VTs, MemVT, MMO);
10685 createOperands(N, Ops);
10686
10687 CSEMap.InsertNode(N, IP);
10688 InsertNode(N);
10689 SDValue V(N, 0);
10690 NewSDValueDbgMsg(V, "Creating new node: ", this);
10691 return V;
10692}
10693
10695 EVT MemVT, MachineMemOperand *MMO) {
10696 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10697 SDVTList VTs = getVTList(MVT::Other);
10698 SDValue Ops[] = {Chain, Ptr};
10700 AddNodeIDNode(ID, ISD::SET_FPENV_MEM, VTs, Ops);
10701 ID.AddInteger(MemVT.getRawBits());
10702 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10703 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10704 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10705 ID.AddInteger(MMO->getFlags());
10706 void *IP = nullptr;
10707 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10708 return SDValue(E, 0);
10709
10710 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10711 dl.getDebugLoc(), VTs, MemVT, MMO);
10712 createOperands(N, Ops);
10713
10714 CSEMap.InsertNode(N, IP);
10715 InsertNode(N);
10716 SDValue V(N, 0);
10717 NewSDValueDbgMsg(V, "Creating new node: ", this);
10718 return V;
10719}
10720
10722 // select undef, T, F --> T (if T is a constant), otherwise F
10723 // select, ?, undef, F --> F
10724 // select, ?, T, undef --> T
10725 if (Cond.isUndef())
10726 return isConstantValueOfAnyType(T) ? T : F;
10727 if (T.isUndef())
10728 return F;
10729 if (F.isUndef())
10730 return T;
10731
10732 // select true, T, F --> T
10733 // select false, T, F --> F
10734 if (auto C = isBoolConstant(Cond))
10735 return *C ? T : F;
10736
10737 // select ?, T, T --> T
10738 if (T == F)
10739 return T;
10740
10741 return SDValue();
10742}
10743
10745 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10746 if (X.isUndef())
10747 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10748 // shift X, undef --> undef (because it may shift by the bitwidth)
10749 if (Y.isUndef())
10750 return getUNDEF(X.getValueType());
10751
10752 // shift 0, Y --> 0
10753 // shift X, 0 --> X
10755 return X;
10756
10757 // shift X, C >= bitwidth(X) --> undef
10758 // All vector elements must be too big (or undef) to avoid partial undefs.
10759 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10760 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10761 };
10762 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10763 return getUNDEF(X.getValueType());
10764
10765 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10766 if (X.getValueType().getScalarType() == MVT::i1)
10767 return X;
10768
10769 return SDValue();
10770}
10771
10773 SDNodeFlags Flags) {
10774 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10775 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10776 // operation is poison. That result can be relaxed to undef.
10777 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10778 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10779 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10780 (YC && YC->getValueAPF().isNaN());
10781 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10782 (YC && YC->getValueAPF().isInfinity());
10783
10784 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10785 return getUNDEF(X.getValueType());
10786
10787 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10788 return getUNDEF(X.getValueType());
10789
10790 if (!YC)
10791 return SDValue();
10792
10793 // X + -0.0 --> X
10794 if (Opcode == ISD::FADD)
10795 if (YC->getValueAPF().isNegZero())
10796 return X;
10797
10798 // X - +0.0 --> X
10799 if (Opcode == ISD::FSUB)
10800 if (YC->getValueAPF().isPosZero())
10801 return X;
10802
10803 // X * 1.0 --> X
10804 // X / 1.0 --> X
10805 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10806 if (YC->getValueAPF().isExactlyValue(1.0))
10807 return X;
10808
10809 // X * 0.0 --> 0.0
10810 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10811 if (YC->getValueAPF().isZero())
10812 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10813
10814 return SDValue();
10815}
10816
10818 SDValue Ptr, SDValue SV, unsigned Align) {
10819 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10820 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10821}
10822
10823SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10825 switch (Ops.size()) {
10826 case 0: return getNode(Opcode, DL, VT);
10827 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10828 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10829 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10830 default: break;
10831 }
10832
10833 // Copy from an SDUse array into an SDValue array for use with
10834 // the regular getNode logic.
10836 return getNode(Opcode, DL, VT, NewOps);
10837}
10838
10839SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10841 SDNodeFlags Flags;
10842 if (Inserter)
10843 Flags = Inserter->getFlags();
10844 return getNode(Opcode, DL, VT, Ops, Flags);
10845}
10846
10847SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10848 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10849 unsigned NumOps = Ops.size();
10850 switch (NumOps) {
10851 case 0: return getNode(Opcode, DL, VT);
10852 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10853 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10854 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10855 default: break;
10856 }
10857
10858#ifndef NDEBUG
10859 for (const auto &Op : Ops)
10860 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10861 "Operand is DELETED_NODE!");
10862#endif
10863
10864 switch (Opcode) {
10865 default: break;
10866 case ISD::BUILD_VECTOR:
10867 // Attempt to simplify BUILD_VECTOR.
10868 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10869 return V;
10870 break;
10872 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10873 return V;
10874 break;
10875 case ISD::SELECT_CC:
10876 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10877 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10878 "LHS and RHS of condition must have same type!");
10879 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10880 "True and False arms of SelectCC must have same type!");
10881 assert(Ops[2].getValueType() == VT &&
10882 "select_cc node must be of same type as true and false value!");
10883 assert((!Ops[0].getValueType().isVector() ||
10884 Ops[0].getValueType().getVectorElementCount() ==
10885 VT.getVectorElementCount()) &&
10886 "Expected select_cc with vector result to have the same sized "
10887 "comparison type!");
10888 break;
10889 case ISD::BR_CC:
10890 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10891 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10892 "LHS/RHS of comparison should match types!");
10893 break;
10894 case ISD::VP_ADD:
10895 case ISD::VP_SUB:
10896 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10897 if (VT.getScalarType() == MVT::i1)
10898 Opcode = ISD::VP_XOR;
10899 break;
10900 case ISD::VP_MUL:
10901 // If it is VP_MUL mask operation then turn it to VP_AND
10902 if (VT.getScalarType() == MVT::i1)
10903 Opcode = ISD::VP_AND;
10904 break;
10905 case ISD::VP_REDUCE_MUL:
10906 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10907 if (VT == MVT::i1)
10908 Opcode = ISD::VP_REDUCE_AND;
10909 break;
10910 case ISD::VP_REDUCE_ADD:
10911 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10912 if (VT == MVT::i1)
10913 Opcode = ISD::VP_REDUCE_XOR;
10914 break;
10915 case ISD::VP_REDUCE_SMAX:
10916 case ISD::VP_REDUCE_UMIN:
10917 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10918 // VP_REDUCE_AND.
10919 if (VT == MVT::i1)
10920 Opcode = ISD::VP_REDUCE_AND;
10921 break;
10922 case ISD::VP_REDUCE_SMIN:
10923 case ISD::VP_REDUCE_UMAX:
10924 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10925 // VP_REDUCE_OR.
10926 if (VT == MVT::i1)
10927 Opcode = ISD::VP_REDUCE_OR;
10928 break;
10929 }
10930
10931 // Memoize nodes.
10932 SDNode *N;
10933 SDVTList VTs = getVTList(VT);
10934
10935 if (VT != MVT::Glue) {
10937 AddNodeIDNode(ID, Opcode, VTs, Ops);
10938 void *IP = nullptr;
10939
10940 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10941 E->intersectFlagsWith(Flags);
10942 return SDValue(E, 0);
10943 }
10944
10945 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10946 createOperands(N, Ops);
10947
10948 CSEMap.InsertNode(N, IP);
10949 } else {
10950 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
10951 createOperands(N, Ops);
10952 }
10953
10954 N->setFlags(Flags);
10955 InsertNode(N);
10956 SDValue V(N, 0);
10957 NewSDValueDbgMsg(V, "Creating new node: ", this);
10958 return V;
10959}
10960
10961SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10962 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
10963 SDNodeFlags Flags;
10964 if (Inserter)
10965 Flags = Inserter->getFlags();
10966 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10967}
10968
10969SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
10971 const SDNodeFlags Flags) {
10972 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
10973}
10974
10975SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10977 SDNodeFlags Flags;
10978 if (Inserter)
10979 Flags = Inserter->getFlags();
10980 return getNode(Opcode, DL, VTList, Ops, Flags);
10981}
10982
10983SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
10984 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10985 if (VTList.NumVTs == 1)
10986 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
10987
10988#ifndef NDEBUG
10989 for (const auto &Op : Ops)
10990 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10991 "Operand is DELETED_NODE!");
10992#endif
10993
10994 switch (Opcode) {
10995 case ISD::SADDO:
10996 case ISD::UADDO:
10997 case ISD::SSUBO:
10998 case ISD::USUBO: {
10999 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11000 "Invalid add/sub overflow op!");
11001 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11002 Ops[0].getValueType() == Ops[1].getValueType() &&
11003 Ops[0].getValueType() == VTList.VTs[0] &&
11004 "Binary operator types must match!");
11005 SDValue N1 = Ops[0], N2 = Ops[1];
11006 canonicalizeCommutativeBinop(Opcode, N1, N2);
11007
11008 // (X +- 0) -> X with zero-overflow.
11009 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11010 /*AllowTruncation*/ true);
11011 if (N2CV && N2CV->isZero()) {
11012 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11013 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11014 }
11015
11016 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11017 VTList.VTs[1].getScalarType() == MVT::i1) {
11018 SDValue F1 = getFreeze(N1);
11019 SDValue F2 = getFreeze(N2);
11020 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11021 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11022 return getNode(ISD::MERGE_VALUES, DL, VTList,
11023 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11024 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11025 Flags);
11026 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11027 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11028 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11029 return getNode(ISD::MERGE_VALUES, DL, VTList,
11030 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11031 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11032 Flags);
11033 }
11034 }
11035 break;
11036 }
11037 case ISD::SADDO_CARRY:
11038 case ISD::UADDO_CARRY:
11039 case ISD::SSUBO_CARRY:
11040 case ISD::USUBO_CARRY:
11041 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11042 "Invalid add/sub overflow op!");
11043 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11044 Ops[0].getValueType() == Ops[1].getValueType() &&
11045 Ops[0].getValueType() == VTList.VTs[0] &&
11046 Ops[2].getValueType() == VTList.VTs[1] &&
11047 "Binary operator types must match!");
11048 break;
11049 case ISD::SMUL_LOHI:
11050 case ISD::UMUL_LOHI: {
11051 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11052 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11053 VTList.VTs[0] == Ops[0].getValueType() &&
11054 VTList.VTs[0] == Ops[1].getValueType() &&
11055 "Binary operator types must match!");
11056 // Constant fold.
11059 if (LHS && RHS) {
11060 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11061 unsigned OutWidth = Width * 2;
11062 APInt Val = LHS->getAPIntValue();
11063 APInt Mul = RHS->getAPIntValue();
11064 if (Opcode == ISD::SMUL_LOHI) {
11065 Val = Val.sext(OutWidth);
11066 Mul = Mul.sext(OutWidth);
11067 } else {
11068 Val = Val.zext(OutWidth);
11069 Mul = Mul.zext(OutWidth);
11070 }
11071 Val *= Mul;
11072
11073 SDValue Hi =
11074 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11075 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11076 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11077 }
11078 break;
11079 }
11080 case ISD::FFREXP: {
11081 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11082 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11083 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11084
11086 int FrexpExp;
11087 APFloat FrexpMant =
11088 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11089 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11090 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11091 DL, VTList.VTs[1]);
11092 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11093 }
11094
11095 break;
11096 }
11098 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11099 "Invalid STRICT_FP_EXTEND!");
11100 assert(VTList.VTs[0].isFloatingPoint() &&
11101 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11102 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11103 "STRICT_FP_EXTEND result type should be vector iff the operand "
11104 "type is vector!");
11105 assert((!VTList.VTs[0].isVector() ||
11106 VTList.VTs[0].getVectorElementCount() ==
11107 Ops[1].getValueType().getVectorElementCount()) &&
11108 "Vector element count mismatch!");
11109 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11110 "Invalid fpext node, dst <= src!");
11111 break;
11113 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11114 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11115 "STRICT_FP_ROUND result type should be vector iff the operand "
11116 "type is vector!");
11117 assert((!VTList.VTs[0].isVector() ||
11118 VTList.VTs[0].getVectorElementCount() ==
11119 Ops[1].getValueType().getVectorElementCount()) &&
11120 "Vector element count mismatch!");
11121 assert(VTList.VTs[0].isFloatingPoint() &&
11122 Ops[1].getValueType().isFloatingPoint() &&
11123 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11124 Ops[2].getOpcode() == ISD::TargetConstant &&
11125 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11126 "Invalid STRICT_FP_ROUND!");
11127 break;
11128 }
11129
11130 // Memoize the node unless it returns a glue result.
11131 SDNode *N;
11132 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11134 AddNodeIDNode(ID, Opcode, VTList, Ops);
11135 void *IP = nullptr;
11136 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11137 E->intersectFlagsWith(Flags);
11138 return SDValue(E, 0);
11139 }
11140
11141 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11142 createOperands(N, Ops);
11143 CSEMap.InsertNode(N, IP);
11144 } else {
11145 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11146 createOperands(N, Ops);
11147 }
11148
11149 N->setFlags(Flags);
11150 InsertNode(N);
11151 SDValue V(N, 0);
11152 NewSDValueDbgMsg(V, "Creating new node: ", this);
11153 return V;
11154}
11155
11156SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11157 SDVTList VTList) {
11158 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11159}
11160
11161SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11162 SDValue N1) {
11163 SDValue Ops[] = { N1 };
11164 return getNode(Opcode, DL, VTList, Ops);
11165}
11166
11167SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11168 SDValue N1, SDValue N2) {
11169 SDValue Ops[] = { N1, N2 };
11170 return getNode(Opcode, DL, VTList, Ops);
11171}
11172
11173SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11174 SDValue N1, SDValue N2, SDValue N3) {
11175 SDValue Ops[] = { N1, N2, N3 };
11176 return getNode(Opcode, DL, VTList, Ops);
11177}
11178
11179SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11180 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11181 SDValue Ops[] = { N1, N2, N3, N4 };
11182 return getNode(Opcode, DL, VTList, Ops);
11183}
11184
11185SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11186 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11187 SDValue N5) {
11188 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11189 return getNode(Opcode, DL, VTList, Ops);
11190}
11191
11193 if (!VT.isExtended())
11194 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11195
11196 return makeVTList(&(*EVTs.insert(VT).first), 1);
11197}
11198
11201 ID.AddInteger(2U);
11202 ID.AddInteger(VT1.getRawBits());
11203 ID.AddInteger(VT2.getRawBits());
11204
11205 void *IP = nullptr;
11206 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11207 if (!Result) {
11208 EVT *Array = Allocator.Allocate<EVT>(2);
11209 Array[0] = VT1;
11210 Array[1] = VT2;
11211 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11212 VTListMap.InsertNode(Result, IP);
11213 }
11214 return Result->getSDVTList();
11215}
11216
11219 ID.AddInteger(3U);
11220 ID.AddInteger(VT1.getRawBits());
11221 ID.AddInteger(VT2.getRawBits());
11222 ID.AddInteger(VT3.getRawBits());
11223
11224 void *IP = nullptr;
11225 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11226 if (!Result) {
11227 EVT *Array = Allocator.Allocate<EVT>(3);
11228 Array[0] = VT1;
11229 Array[1] = VT2;
11230 Array[2] = VT3;
11231 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11232 VTListMap.InsertNode(Result, IP);
11233 }
11234 return Result->getSDVTList();
11235}
11236
11239 ID.AddInteger(4U);
11240 ID.AddInteger(VT1.getRawBits());
11241 ID.AddInteger(VT2.getRawBits());
11242 ID.AddInteger(VT3.getRawBits());
11243 ID.AddInteger(VT4.getRawBits());
11244
11245 void *IP = nullptr;
11246 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11247 if (!Result) {
11248 EVT *Array = Allocator.Allocate<EVT>(4);
11249 Array[0] = VT1;
11250 Array[1] = VT2;
11251 Array[2] = VT3;
11252 Array[3] = VT4;
11253 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11254 VTListMap.InsertNode(Result, IP);
11255 }
11256 return Result->getSDVTList();
11257}
11258
11260 unsigned NumVTs = VTs.size();
11262 ID.AddInteger(NumVTs);
11263 for (unsigned index = 0; index < NumVTs; index++) {
11264 ID.AddInteger(VTs[index].getRawBits());
11265 }
11266
11267 void *IP = nullptr;
11268 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11269 if (!Result) {
11270 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11271 llvm::copy(VTs, Array);
11272 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11273 VTListMap.InsertNode(Result, IP);
11274 }
11275 return Result->getSDVTList();
11276}
11277
11278
11279/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11280/// specified operands. If the resultant node already exists in the DAG,
11281/// this does not modify the specified node, instead it returns the node that
11282/// already exists. If the resultant node does not exist in the DAG, the
11283/// input node is returned. As a degenerate case, if you specify the same
11284/// input operands as the node already has, the input node is returned.
11286 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11287
11288 // Check to see if there is no change.
11289 if (Op == N->getOperand(0)) return N;
11290
11291 // See if the modified node already exists.
11292 void *InsertPos = nullptr;
11293 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11294 return Existing;
11295
11296 // Nope it doesn't. Remove the node from its current place in the maps.
11297 if (InsertPos)
11298 if (!RemoveNodeFromCSEMaps(N))
11299 InsertPos = nullptr;
11300
11301 // Now we update the operands.
11302 N->OperandList[0].set(Op);
11303
11305 // If this gets put into a CSE map, add it.
11306 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11307 return N;
11308}
11309
11311 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11312
11313 // Check to see if there is no change.
11314 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11315 return N; // No operands changed, just return the input node.
11316
11317 // See if the modified node already exists.
11318 void *InsertPos = nullptr;
11319 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11320 return Existing;
11321
11322 // Nope it doesn't. Remove the node from its current place in the maps.
11323 if (InsertPos)
11324 if (!RemoveNodeFromCSEMaps(N))
11325 InsertPos = nullptr;
11326
11327 // Now we update the operands.
11328 if (N->OperandList[0] != Op1)
11329 N->OperandList[0].set(Op1);
11330 if (N->OperandList[1] != Op2)
11331 N->OperandList[1].set(Op2);
11332
11334 // If this gets put into a CSE map, add it.
11335 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11336 return N;
11337}
11338
11341 SDValue Ops[] = { Op1, Op2, Op3 };
11342 return UpdateNodeOperands(N, Ops);
11343}
11344
11347 SDValue Op3, SDValue Op4) {
11348 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11349 return UpdateNodeOperands(N, Ops);
11350}
11351
11354 SDValue Op3, SDValue Op4, SDValue Op5) {
11355 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11356 return UpdateNodeOperands(N, Ops);
11357}
11358
11361 unsigned NumOps = Ops.size();
11362 assert(N->getNumOperands() == NumOps &&
11363 "Update with wrong number of operands");
11364
11365 // If no operands changed just return the input node.
11366 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11367 return N;
11368
11369 // See if the modified node already exists.
11370 void *InsertPos = nullptr;
11371 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11372 return Existing;
11373
11374 // Nope it doesn't. Remove the node from its current place in the maps.
11375 if (InsertPos)
11376 if (!RemoveNodeFromCSEMaps(N))
11377 InsertPos = nullptr;
11378
11379 // Now we update the operands.
11380 for (unsigned i = 0; i != NumOps; ++i)
11381 if (N->OperandList[i] != Ops[i])
11382 N->OperandList[i].set(Ops[i]);
11383
11385 // If this gets put into a CSE map, add it.
11386 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11387 return N;
11388}
11389
11390/// DropOperands - Release the operands and set this node to have
11391/// zero operands.
11393 // Unlike the code in MorphNodeTo that does this, we don't need to
11394 // watch for dead nodes here.
11395 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11396 SDUse &Use = *I++;
11397 Use.set(SDValue());
11398 }
11399}
11400
11402 ArrayRef<MachineMemOperand *> NewMemRefs) {
11403 if (NewMemRefs.empty()) {
11404 N->clearMemRefs();
11405 return;
11406 }
11407
11408 // Check if we can avoid allocating by storing a single reference directly.
11409 if (NewMemRefs.size() == 1) {
11410 N->MemRefs = NewMemRefs[0];
11411 N->NumMemRefs = 1;
11412 return;
11413 }
11414
11415 MachineMemOperand **MemRefsBuffer =
11416 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11417 llvm::copy(NewMemRefs, MemRefsBuffer);
11418 N->MemRefs = MemRefsBuffer;
11419 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11420}
11421
11422/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11423/// machine opcode.
11424///
11426 EVT VT) {
11427 SDVTList VTs = getVTList(VT);
11428 return SelectNodeTo(N, MachineOpc, VTs, {});
11429}
11430
11432 EVT VT, SDValue Op1) {
11433 SDVTList VTs = getVTList(VT);
11434 SDValue Ops[] = { Op1 };
11435 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11436}
11437
11439 EVT VT, SDValue Op1,
11440 SDValue Op2) {
11441 SDVTList VTs = getVTList(VT);
11442 SDValue Ops[] = { Op1, Op2 };
11443 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11444}
11445
11447 EVT VT, SDValue Op1,
11448 SDValue Op2, SDValue Op3) {
11449 SDVTList VTs = getVTList(VT);
11450 SDValue Ops[] = { Op1, Op2, Op3 };
11451 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11452}
11453
11456 SDVTList VTs = getVTList(VT);
11457 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11458}
11459
11461 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11462 SDVTList VTs = getVTList(VT1, VT2);
11463 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11464}
11465
11467 EVT VT1, EVT VT2) {
11468 SDVTList VTs = getVTList(VT1, VT2);
11469 return SelectNodeTo(N, MachineOpc, VTs, {});
11470}
11471
11473 EVT VT1, EVT VT2, EVT VT3,
11475 SDVTList VTs = getVTList(VT1, VT2, VT3);
11476 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11477}
11478
11480 EVT VT1, EVT VT2,
11481 SDValue Op1, SDValue Op2) {
11482 SDVTList VTs = getVTList(VT1, VT2);
11483 SDValue Ops[] = { Op1, Op2 };
11484 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11485}
11486
11489 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11490 // Reset the NodeID to -1.
11491 New->setNodeId(-1);
11492 if (New != N) {
11493 ReplaceAllUsesWith(N, New);
11495 }
11496 return New;
11497}
11498
11499/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11500/// the line number information on the merged node since it is not possible to
11501/// preserve the information that operation is associated with multiple lines.
11502/// This will make the debugger working better at -O0, were there is a higher
11503/// probability having other instructions associated with that line.
11504///
11505/// For IROrder, we keep the smaller of the two
11506SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11507 DebugLoc NLoc = N->getDebugLoc();
11508 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11509 N->setDebugLoc(DebugLoc());
11510 }
11511 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11512 N->setIROrder(Order);
11513 return N;
11514}
11515
11516/// MorphNodeTo - This *mutates* the specified node to have the specified
11517/// return type, opcode, and operands.
11518///
11519/// Note that MorphNodeTo returns the resultant node. If there is already a
11520/// node of the specified opcode and operands, it returns that node instead of
11521/// the current one. Note that the SDLoc need not be the same.
11522///
11523/// Using MorphNodeTo is faster than creating a new node and swapping it in
11524/// with ReplaceAllUsesWith both because it often avoids allocating a new
11525/// node, and because it doesn't require CSE recalculation for any of
11526/// the node's users.
11527///
11528/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11529/// As a consequence it isn't appropriate to use from within the DAG combiner or
11530/// the legalizer which maintain worklists that would need to be updated when
11531/// deleting things.
11534 // If an identical node already exists, use it.
11535 void *IP = nullptr;
11536 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11538 AddNodeIDNode(ID, Opc, VTs, Ops);
11539 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11540 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11541 }
11542
11543 if (!RemoveNodeFromCSEMaps(N))
11544 IP = nullptr;
11545
11546 // Start the morphing.
11547 N->NodeType = Opc;
11548 N->ValueList = VTs.VTs;
11549 N->NumValues = VTs.NumVTs;
11550
11551 // Clear the operands list, updating used nodes to remove this from their
11552 // use list. Keep track of any operands that become dead as a result.
11553 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11554 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11555 SDUse &Use = *I++;
11556 SDNode *Used = Use.getNode();
11557 Use.set(SDValue());
11558 if (Used->use_empty())
11559 DeadNodeSet.insert(Used);
11560 }
11561
11562 // For MachineNode, initialize the memory references information.
11564 MN->clearMemRefs();
11565
11566 // Swap for an appropriately sized array from the recycler.
11567 removeOperands(N);
11568 createOperands(N, Ops);
11569
11570 // Delete any nodes that are still dead after adding the uses for the
11571 // new operands.
11572 if (!DeadNodeSet.empty()) {
11573 SmallVector<SDNode *, 16> DeadNodes;
11574 for (SDNode *N : DeadNodeSet)
11575 if (N->use_empty())
11576 DeadNodes.push_back(N);
11577 RemoveDeadNodes(DeadNodes);
11578 }
11579
11580 if (IP)
11581 CSEMap.InsertNode(N, IP); // Memoize the new node.
11582 return N;
11583}
11584
11586 unsigned OrigOpc = Node->getOpcode();
11587 unsigned NewOpc;
11588 switch (OrigOpc) {
11589 default:
11590 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11591#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11592 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11593#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11594 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11595#include "llvm/IR/ConstrainedOps.def"
11596 }
11597
11598 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11599
11600 // We're taking this node out of the chain, so we need to re-link things.
11601 SDValue InputChain = Node->getOperand(0);
11602 SDValue OutputChain = SDValue(Node, 1);
11603 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11604
11606 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11607 Ops.push_back(Node->getOperand(i));
11608
11609 SDVTList VTs = getVTList(Node->getValueType(0));
11610 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11611
11612 // MorphNodeTo can operate in two ways: if an existing node with the
11613 // specified operands exists, it can just return it. Otherwise, it
11614 // updates the node in place to have the requested operands.
11615 if (Res == Node) {
11616 // If we updated the node in place, reset the node ID. To the isel,
11617 // this should be just like a newly allocated machine node.
11618 Res->setNodeId(-1);
11619 } else {
11622 }
11623
11624 return Res;
11625}
11626
11627/// getMachineNode - These are used for target selectors to create a new node
11628/// with specified return type(s), MachineInstr opcode, and operands.
11629///
11630/// Note that getMachineNode returns the resultant node. If there is already a
11631/// node of the specified opcode and operands, it returns that node instead of
11632/// the current one.
11634 EVT VT) {
11635 SDVTList VTs = getVTList(VT);
11636 return getMachineNode(Opcode, dl, VTs, {});
11637}
11638
11640 EVT VT, SDValue Op1) {
11641 SDVTList VTs = getVTList(VT);
11642 SDValue Ops[] = { Op1 };
11643 return getMachineNode(Opcode, dl, VTs, Ops);
11644}
11645
11647 EVT VT, SDValue Op1, SDValue Op2) {
11648 SDVTList VTs = getVTList(VT);
11649 SDValue Ops[] = { Op1, Op2 };
11650 return getMachineNode(Opcode, dl, VTs, Ops);
11651}
11652
11654 EVT VT, SDValue Op1, SDValue Op2,
11655 SDValue Op3) {
11656 SDVTList VTs = getVTList(VT);
11657 SDValue Ops[] = { Op1, Op2, Op3 };
11658 return getMachineNode(Opcode, dl, VTs, Ops);
11659}
11660
11663 SDVTList VTs = getVTList(VT);
11664 return getMachineNode(Opcode, dl, VTs, Ops);
11665}
11666
11668 EVT VT1, EVT VT2, SDValue Op1,
11669 SDValue Op2) {
11670 SDVTList VTs = getVTList(VT1, VT2);
11671 SDValue Ops[] = { Op1, Op2 };
11672 return getMachineNode(Opcode, dl, VTs, Ops);
11673}
11674
11676 EVT VT1, EVT VT2, SDValue Op1,
11677 SDValue Op2, SDValue Op3) {
11678 SDVTList VTs = getVTList(VT1, VT2);
11679 SDValue Ops[] = { Op1, Op2, Op3 };
11680 return getMachineNode(Opcode, dl, VTs, Ops);
11681}
11682
11684 EVT VT1, EVT VT2,
11686 SDVTList VTs = getVTList(VT1, VT2);
11687 return getMachineNode(Opcode, dl, VTs, Ops);
11688}
11689
11691 EVT VT1, EVT VT2, EVT VT3,
11692 SDValue Op1, SDValue Op2) {
11693 SDVTList VTs = getVTList(VT1, VT2, VT3);
11694 SDValue Ops[] = { Op1, Op2 };
11695 return getMachineNode(Opcode, dl, VTs, Ops);
11696}
11697
11699 EVT VT1, EVT VT2, EVT VT3,
11700 SDValue Op1, SDValue Op2,
11701 SDValue Op3) {
11702 SDVTList VTs = getVTList(VT1, VT2, VT3);
11703 SDValue Ops[] = { Op1, Op2, Op3 };
11704 return getMachineNode(Opcode, dl, VTs, Ops);
11705}
11706
11708 EVT VT1, EVT VT2, EVT VT3,
11710 SDVTList VTs = getVTList(VT1, VT2, VT3);
11711 return getMachineNode(Opcode, dl, VTs, Ops);
11712}
11713
11715 ArrayRef<EVT> ResultTys,
11717 SDVTList VTs = getVTList(ResultTys);
11718 return getMachineNode(Opcode, dl, VTs, Ops);
11719}
11720
11722 SDVTList VTs,
11724 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11726 void *IP = nullptr;
11727
11728 if (DoCSE) {
11730 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11731 IP = nullptr;
11732 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11733 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11734 }
11735 }
11736
11737 // Allocate a new MachineSDNode.
11738 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11739 createOperands(N, Ops);
11740
11741 if (DoCSE)
11742 CSEMap.InsertNode(N, IP);
11743
11744 InsertNode(N);
11745 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11746 return N;
11747}
11748
11749/// getTargetExtractSubreg - A convenience function for creating
11750/// TargetOpcode::EXTRACT_SUBREG nodes.
11752 SDValue Operand) {
11753 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11754 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11755 VT, Operand, SRIdxVal);
11756 return SDValue(Subreg, 0);
11757}
11758
11759/// getTargetInsertSubreg - A convenience function for creating
11760/// TargetOpcode::INSERT_SUBREG nodes.
11762 SDValue Operand, SDValue Subreg) {
11763 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11764 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11765 VT, Operand, Subreg, SRIdxVal);
11766 return SDValue(Result, 0);
11767}
11768
11769/// getNodeIfExists - Get the specified node if it's already available, or
11770/// else return NULL.
11773 bool AllowCommute) {
11774 SDNodeFlags Flags;
11775 if (Inserter)
11776 Flags = Inserter->getFlags();
11777 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11778}
11779
11782 const SDNodeFlags Flags,
11783 bool AllowCommute) {
11784 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11785 return nullptr;
11786
11787 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11789 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11790 void *IP = nullptr;
11791 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11792 E->intersectFlagsWith(Flags);
11793 return E;
11794 }
11795 return nullptr;
11796 };
11797
11798 if (SDNode *Existing = Lookup(Ops))
11799 return Existing;
11800
11801 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11802 return Lookup({Ops[1], Ops[0]});
11803
11804 return nullptr;
11805}
11806
11807/// doesNodeExist - Check if a node exists without modifying its flags.
11808bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11810 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11812 AddNodeIDNode(ID, Opcode, VTList, Ops);
11813 void *IP = nullptr;
11814 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11815 return true;
11816 }
11817 return false;
11818}
11819
11820/// getDbgValue - Creates a SDDbgValue node.
11821///
11822/// SDNode
11824 SDNode *N, unsigned R, bool IsIndirect,
11825 const DebugLoc &DL, unsigned O) {
11826 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11827 "Expected inlined-at fields to agree");
11828 return new (DbgInfo->getAlloc())
11829 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11830 {}, IsIndirect, DL, O,
11831 /*IsVariadic=*/false);
11832}
11833
11834/// Constant
11836 DIExpression *Expr,
11837 const Value *C,
11838 const DebugLoc &DL, unsigned O) {
11839 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11840 "Expected inlined-at fields to agree");
11841 return new (DbgInfo->getAlloc())
11842 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11843 /*IsIndirect=*/false, DL, O,
11844 /*IsVariadic=*/false);
11845}
11846
11847/// FrameIndex
11849 DIExpression *Expr, unsigned FI,
11850 bool IsIndirect,
11851 const DebugLoc &DL,
11852 unsigned O) {
11853 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11854 "Expected inlined-at fields to agree");
11855 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11856}
11857
11858/// FrameIndex with dependencies
11860 DIExpression *Expr, unsigned FI,
11861 ArrayRef<SDNode *> Dependencies,
11862 bool IsIndirect,
11863 const DebugLoc &DL,
11864 unsigned O) {
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, SDDbgOperand::fromFrameIdx(FI),
11869 Dependencies, IsIndirect, DL, O,
11870 /*IsVariadic=*/false);
11871}
11872
11873/// VReg
11875 Register VReg, bool IsIndirect,
11876 const DebugLoc &DL, unsigned O) {
11877 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11878 "Expected inlined-at fields to agree");
11879 return new (DbgInfo->getAlloc())
11880 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11881 {}, IsIndirect, DL, O,
11882 /*IsVariadic=*/false);
11883}
11884
11887 ArrayRef<SDNode *> Dependencies,
11888 bool IsIndirect, const DebugLoc &DL,
11889 unsigned O, bool IsVariadic) {
11890 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11891 "Expected inlined-at fields to agree");
11892 return new (DbgInfo->getAlloc())
11893 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11894 DL, O, IsVariadic);
11895}
11896
11898 unsigned OffsetInBits, unsigned SizeInBits,
11899 bool InvalidateDbg) {
11900 SDNode *FromNode = From.getNode();
11901 SDNode *ToNode = To.getNode();
11902 assert(FromNode && ToNode && "Can't modify dbg values");
11903
11904 // PR35338
11905 // TODO: assert(From != To && "Redundant dbg value transfer");
11906 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11907 if (From == To || FromNode == ToNode)
11908 return;
11909
11910 if (!FromNode->getHasDebugValue())
11911 return;
11912
11913 SDDbgOperand FromLocOp =
11914 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11916
11918 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11919 if (Dbg->isInvalidated())
11920 continue;
11921
11922 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11923
11924 // Create a new location ops vector that is equal to the old vector, but
11925 // with each instance of FromLocOp replaced with ToLocOp.
11926 bool Changed = false;
11927 auto NewLocOps = Dbg->copyLocationOps();
11928 std::replace_if(
11929 NewLocOps.begin(), NewLocOps.end(),
11930 [&Changed, FromLocOp](const SDDbgOperand &Op) {
11931 bool Match = Op == FromLocOp;
11932 Changed |= Match;
11933 return Match;
11934 },
11935 ToLocOp);
11936 // Ignore this SDDbgValue if we didn't find a matching location.
11937 if (!Changed)
11938 continue;
11939
11940 DIVariable *Var = Dbg->getVariable();
11941 auto *Expr = Dbg->getExpression();
11942 // If a fragment is requested, update the expression.
11943 if (SizeInBits) {
11944 // When splitting a larger (e.g., sign-extended) value whose
11945 // lower bits are described with an SDDbgValue, do not attempt
11946 // to transfer the SDDbgValue to the upper bits.
11947 if (auto FI = Expr->getFragmentInfo())
11948 if (OffsetInBits + SizeInBits > FI->SizeInBits)
11949 continue;
11950 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
11951 SizeInBits);
11952 if (!Fragment)
11953 continue;
11954 Expr = *Fragment;
11955 }
11956
11957 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
11958 // Clone the SDDbgValue and move it to To.
11959 SDDbgValue *Clone = getDbgValueList(
11960 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
11961 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
11962 Dbg->isVariadic());
11963 ClonedDVs.push_back(Clone);
11964
11965 if (InvalidateDbg) {
11966 // Invalidate value and indicate the SDDbgValue should not be emitted.
11967 Dbg->setIsInvalidated();
11968 Dbg->setIsEmitted();
11969 }
11970 }
11971
11972 for (SDDbgValue *Dbg : ClonedDVs) {
11973 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
11974 "Transferred DbgValues should depend on the new SDNode");
11975 AddDbgValue(Dbg, false);
11976 }
11977}
11978
11980 if (!N.getHasDebugValue())
11981 return;
11982
11983 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
11984 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
11985 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
11986 return SDDbgOperand::fromNode(Node, ResNo);
11987 };
11988
11990 for (auto *DV : GetDbgValues(&N)) {
11991 if (DV->isInvalidated())
11992 continue;
11993 switch (N.getOpcode()) {
11994 default:
11995 break;
11996 case ISD::ADD: {
11997 SDValue N0 = N.getOperand(0);
11998 SDValue N1 = N.getOperand(1);
11999 if (!isa<ConstantSDNode>(N0)) {
12000 bool RHSConstant = isa<ConstantSDNode>(N1);
12002 if (RHSConstant)
12003 Offset = N.getConstantOperandVal(1);
12004 // We are not allowed to turn indirect debug values variadic, so
12005 // don't salvage those.
12006 if (!RHSConstant && DV->isIndirect())
12007 continue;
12008
12009 // Rewrite an ADD constant node into a DIExpression. Since we are
12010 // performing arithmetic to compute the variable's *value* in the
12011 // DIExpression, we need to mark the expression with a
12012 // DW_OP_stack_value.
12013 auto *DIExpr = DV->getExpression();
12014 auto NewLocOps = DV->copyLocationOps();
12015 bool Changed = false;
12016 size_t OrigLocOpsSize = NewLocOps.size();
12017 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12018 // We're not given a ResNo to compare against because the whole
12019 // node is going away. We know that any ISD::ADD only has one
12020 // result, so we can assume any node match is using the result.
12021 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12022 NewLocOps[i].getSDNode() != &N)
12023 continue;
12024 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12025 if (RHSConstant) {
12028 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12029 } else {
12030 // Convert to a variadic expression (if not already).
12031 // convertToVariadicExpression() returns a const pointer, so we use
12032 // a temporary const variable here.
12033 const auto *TmpDIExpr =
12037 ExprOps.push_back(NewLocOps.size());
12038 ExprOps.push_back(dwarf::DW_OP_plus);
12039 SDDbgOperand RHS =
12041 NewLocOps.push_back(RHS);
12042 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12043 }
12044 Changed = true;
12045 }
12046 (void)Changed;
12047 assert(Changed && "Salvage target doesn't use N");
12048
12049 bool IsVariadic =
12050 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12051
12052 auto AdditionalDependencies = DV->getAdditionalDependencies();
12053 SDDbgValue *Clone = getDbgValueList(
12054 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12055 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12056 ClonedDVs.push_back(Clone);
12057 DV->setIsInvalidated();
12058 DV->setIsEmitted();
12059 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12060 N0.getNode()->dumprFull(this);
12061 dbgs() << " into " << *DIExpr << '\n');
12062 }
12063 break;
12064 }
12065 case ISD::TRUNCATE: {
12066 SDValue N0 = N.getOperand(0);
12067 TypeSize FromSize = N0.getValueSizeInBits();
12068 TypeSize ToSize = N.getValueSizeInBits(0);
12069
12070 DIExpression *DbgExpression = DV->getExpression();
12071 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12072 auto NewLocOps = DV->copyLocationOps();
12073 bool Changed = false;
12074 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12075 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12076 NewLocOps[i].getSDNode() != &N)
12077 continue;
12078
12079 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12080 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12081 Changed = true;
12082 }
12083 assert(Changed && "Salvage target doesn't use N");
12084 (void)Changed;
12085
12086 SDDbgValue *Clone =
12087 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12088 DV->getAdditionalDependencies(), DV->isIndirect(),
12089 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12090
12091 ClonedDVs.push_back(Clone);
12092 DV->setIsInvalidated();
12093 DV->setIsEmitted();
12094 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12095 dbgs() << " into " << *DbgExpression << '\n');
12096 break;
12097 }
12098 }
12099 }
12100
12101 for (SDDbgValue *Dbg : ClonedDVs) {
12102 assert((!Dbg->getSDNodes().empty() ||
12103 llvm::any_of(Dbg->getLocationOps(),
12104 [&](const SDDbgOperand &Op) {
12105 return Op.getKind() == SDDbgOperand::FRAMEIX;
12106 })) &&
12107 "Salvaged DbgValue should depend on a new SDNode");
12108 AddDbgValue(Dbg, false);
12109 }
12110}
12111
12112/// Creates a SDDbgLabel node.
12114 const DebugLoc &DL, unsigned O) {
12115 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12116 "Expected inlined-at fields to agree");
12117 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12118}
12119
12120namespace {
12121
12122/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12123/// pointed to by a use iterator is deleted, increment the use iterator
12124/// so that it doesn't dangle.
12125///
12126class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12129
12130 void NodeDeleted(SDNode *N, SDNode *E) override {
12131 // Increment the iterator as needed.
12132 while (UI != UE && N == UI->getUser())
12133 ++UI;
12134 }
12135
12136public:
12137 RAUWUpdateListener(SelectionDAG &d,
12140 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12141};
12142
12143} // end anonymous namespace
12144
12145/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12146/// This can cause recursive merging of nodes in the DAG.
12147///
12148/// This version assumes From has a single result value.
12149///
12151 SDNode *From = FromN.getNode();
12152 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12153 "Cannot replace with this method!");
12154 assert(From != To.getNode() && "Cannot replace uses of with self");
12155
12156 // Preserve Debug Values
12157 transferDbgValues(FromN, To);
12158 // Preserve extra info.
12159 copyExtraInfo(From, To.getNode());
12160
12161 // Iterate over all the existing uses of From. New uses will be added
12162 // to the beginning of the use list, which we avoid visiting.
12163 // This specifically avoids visiting uses of From that arise while the
12164 // replacement is happening, because any such uses would be the result
12165 // of CSE: If an existing node looks like From after one of its operands
12166 // is replaced by To, we don't want to replace of all its users with To
12167 // too. See PR3018 for more info.
12168 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12169 RAUWUpdateListener Listener(*this, UI, UE);
12170 while (UI != UE) {
12171 SDNode *User = UI->getUser();
12172
12173 // This node is about to morph, remove its old self from the CSE maps.
12174 RemoveNodeFromCSEMaps(User);
12175
12176 // A user can appear in a use list multiple times, and when this
12177 // happens the uses are usually next to each other in the list.
12178 // To help reduce the number of CSE recomputations, process all
12179 // the uses of this user that we can find this way.
12180 do {
12181 SDUse &Use = *UI;
12182 ++UI;
12183 Use.set(To);
12184 if (To->isDivergent() != From->isDivergent())
12186 } while (UI != UE && UI->getUser() == User);
12187 // Now that we have modified User, add it back to the CSE maps. If it
12188 // already exists there, recursively merge the results together.
12189 AddModifiedNodeToCSEMaps(User);
12190 }
12191
12192 // If we just RAUW'd the root, take note.
12193 if (FromN == getRoot())
12194 setRoot(To);
12195}
12196
12197/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12198/// This can cause recursive merging of nodes in the DAG.
12199///
12200/// This version assumes that for each value of From, there is a
12201/// corresponding value in To in the same position with the same type.
12202///
12204#ifndef NDEBUG
12205 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12206 assert((!From->hasAnyUseOfValue(i) ||
12207 From->getValueType(i) == To->getValueType(i)) &&
12208 "Cannot use this version of ReplaceAllUsesWith!");
12209#endif
12210
12211 // Handle the trivial case.
12212 if (From == To)
12213 return;
12214
12215 // Preserve Debug Info. Only do this if there's a use.
12216 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12217 if (From->hasAnyUseOfValue(i)) {
12218 assert((i < To->getNumValues()) && "Invalid To location");
12219 transferDbgValues(SDValue(From, i), SDValue(To, i));
12220 }
12221 // Preserve extra info.
12222 copyExtraInfo(From, To);
12223
12224 // Iterate over just the existing users of From. See the comments in
12225 // the ReplaceAllUsesWith above.
12226 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12227 RAUWUpdateListener Listener(*this, UI, UE);
12228 while (UI != UE) {
12229 SDNode *User = UI->getUser();
12230
12231 // This node is about to morph, remove its old self from the CSE maps.
12232 RemoveNodeFromCSEMaps(User);
12233
12234 // A user can appear in a use list multiple times, and when this
12235 // happens the uses are usually next to each other in the list.
12236 // To help reduce the number of CSE recomputations, process all
12237 // the uses of this user that we can find this way.
12238 do {
12239 SDUse &Use = *UI;
12240 ++UI;
12241 Use.setNode(To);
12242 if (To->isDivergent() != From->isDivergent())
12244 } while (UI != UE && UI->getUser() == User);
12245
12246 // Now that we have modified User, add it back to the CSE maps. If it
12247 // already exists there, recursively merge the results together.
12248 AddModifiedNodeToCSEMaps(User);
12249 }
12250
12251 // If we just RAUW'd the root, take note.
12252 if (From == getRoot().getNode())
12253 setRoot(SDValue(To, getRoot().getResNo()));
12254}
12255
12256/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12257/// This can cause recursive merging of nodes in the DAG.
12258///
12259/// This version can replace From with any result values. To must match the
12260/// number and types of values returned by From.
12262 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12263 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12264
12265 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12266 // Preserve Debug Info.
12267 transferDbgValues(SDValue(From, i), To[i]);
12268 // Preserve extra info.
12269 copyExtraInfo(From, To[i].getNode());
12270 }
12271
12272 // Iterate over just the existing users of From. See the comments in
12273 // the ReplaceAllUsesWith above.
12274 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12275 RAUWUpdateListener Listener(*this, UI, UE);
12276 while (UI != UE) {
12277 SDNode *User = UI->getUser();
12278
12279 // This node is about to morph, remove its old self from the CSE maps.
12280 RemoveNodeFromCSEMaps(User);
12281
12282 // A user can appear in a use list multiple times, and when this happens the
12283 // uses are usually next to each other in the list. To help reduce the
12284 // number of CSE and divergence recomputations, process all the uses of this
12285 // user that we can find this way.
12286 bool To_IsDivergent = false;
12287 do {
12288 SDUse &Use = *UI;
12289 const SDValue &ToOp = To[Use.getResNo()];
12290 ++UI;
12291 Use.set(ToOp);
12292 To_IsDivergent |= ToOp->isDivergent();
12293 } while (UI != UE && UI->getUser() == User);
12294
12295 if (To_IsDivergent != From->isDivergent())
12297
12298 // Now that we have modified User, add it back to the CSE maps. If it
12299 // already exists there, recursively merge the results together.
12300 AddModifiedNodeToCSEMaps(User);
12301 }
12302
12303 // If we just RAUW'd the root, take note.
12304 if (From == getRoot().getNode())
12305 setRoot(SDValue(To[getRoot().getResNo()]));
12306}
12307
12308/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12309/// uses of other values produced by From.getNode() alone. The Deleted
12310/// vector is handled the same way as for ReplaceAllUsesWith.
12312 // Handle the really simple, really trivial case efficiently.
12313 if (From == To) return;
12314
12315 // Handle the simple, trivial, case efficiently.
12316 if (From.getNode()->getNumValues() == 1) {
12317 ReplaceAllUsesWith(From, To);
12318 return;
12319 }
12320
12321 // Preserve Debug Info.
12322 transferDbgValues(From, To);
12323 copyExtraInfo(From.getNode(), To.getNode());
12324
12325 // Iterate over just the existing users of From. See the comments in
12326 // the ReplaceAllUsesWith above.
12327 SDNode::use_iterator UI = From.getNode()->use_begin(),
12328 UE = From.getNode()->use_end();
12329 RAUWUpdateListener Listener(*this, UI, UE);
12330 while (UI != UE) {
12331 SDNode *User = UI->getUser();
12332 bool UserRemovedFromCSEMaps = false;
12333
12334 // A user can appear in a use list multiple times, and when this
12335 // happens the uses are usually next to each other in the list.
12336 // To help reduce the number of CSE recomputations, process all
12337 // the uses of this user that we can find this way.
12338 do {
12339 SDUse &Use = *UI;
12340
12341 // Skip uses of different values from the same node.
12342 if (Use.getResNo() != From.getResNo()) {
12343 ++UI;
12344 continue;
12345 }
12346
12347 // If this node hasn't been modified yet, it's still in the CSE maps,
12348 // so remove its old self from the CSE maps.
12349 if (!UserRemovedFromCSEMaps) {
12350 RemoveNodeFromCSEMaps(User);
12351 UserRemovedFromCSEMaps = true;
12352 }
12353
12354 ++UI;
12355 Use.set(To);
12356 if (To->isDivergent() != From->isDivergent())
12358 } while (UI != UE && UI->getUser() == User);
12359 // We are iterating over all uses of the From node, so if a use
12360 // doesn't use the specific value, no changes are made.
12361 if (!UserRemovedFromCSEMaps)
12362 continue;
12363
12364 // Now that we have modified User, add it back to the CSE maps. If it
12365 // already exists there, recursively merge the results together.
12366 AddModifiedNodeToCSEMaps(User);
12367 }
12368
12369 // If we just RAUW'd the root, take note.
12370 if (From == getRoot())
12371 setRoot(To);
12372}
12373
12374namespace {
12375
12376/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12377/// to record information about a use.
12378struct UseMemo {
12379 SDNode *User;
12380 unsigned Index;
12381 SDUse *Use;
12382};
12383
12384/// operator< - Sort Memos by User.
12385bool operator<(const UseMemo &L, const UseMemo &R) {
12386 return (intptr_t)L.User < (intptr_t)R.User;
12387}
12388
12389/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12390/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12391/// the node already has been taken care of recursively.
12392class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12393 SmallVectorImpl<UseMemo> &Uses;
12394
12395 void NodeDeleted(SDNode *N, SDNode *E) override {
12396 for (UseMemo &Memo : Uses)
12397 if (Memo.User == N)
12398 Memo.User = nullptr;
12399 }
12400
12401public:
12402 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12403 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12404};
12405
12406} // end anonymous namespace
12407
12408/// Return true if a glue output should propagate divergence information.
12410 switch (Node->getOpcode()) {
12411 case ISD::CopyFromReg:
12412 case ISD::CopyToReg:
12413 return false;
12414 default:
12415 return true;
12416 }
12417
12418 llvm_unreachable("covered opcode switch");
12419}
12420
12422 if (TLI->isSDNodeAlwaysUniform(N)) {
12423 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12424 "Conflicting divergence information!");
12425 return false;
12426 }
12427 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12428 return true;
12429 for (const auto &Op : N->ops()) {
12430 EVT VT = Op.getValueType();
12431
12432 // Skip Chain. It does not carry divergence.
12433 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12434 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12435 return true;
12436 }
12437 return false;
12438}
12439
12441 SmallVector<SDNode *, 16> Worklist(1, N);
12442 do {
12443 N = Worklist.pop_back_val();
12444 bool IsDivergent = calculateDivergence(N);
12445 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12446 N->SDNodeBits.IsDivergent = IsDivergent;
12447 llvm::append_range(Worklist, N->users());
12448 }
12449 } while (!Worklist.empty());
12450}
12451
12452void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12454 Order.reserve(AllNodes.size());
12455 for (auto &N : allnodes()) {
12456 unsigned NOps = N.getNumOperands();
12457 Degree[&N] = NOps;
12458 if (0 == NOps)
12459 Order.push_back(&N);
12460 }
12461 for (size_t I = 0; I != Order.size(); ++I) {
12462 SDNode *N = Order[I];
12463 for (auto *U : N->users()) {
12464 unsigned &UnsortedOps = Degree[U];
12465 if (0 == --UnsortedOps)
12466 Order.push_back(U);
12467 }
12468 }
12469}
12470
12471#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12472void SelectionDAG::VerifyDAGDivergence() {
12473 std::vector<SDNode *> TopoOrder;
12474 CreateTopologicalOrder(TopoOrder);
12475 for (auto *N : TopoOrder) {
12476 assert(calculateDivergence(N) == N->isDivergent() &&
12477 "Divergence bit inconsistency detected");
12478 }
12479}
12480#endif
12481
12482/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12483/// uses of other values produced by From.getNode() alone. The same value
12484/// may appear in both the From and To list. The Deleted vector is
12485/// handled the same way as for ReplaceAllUsesWith.
12487 const SDValue *To,
12488 unsigned Num){
12489 // Handle the simple, trivial case efficiently.
12490 if (Num == 1)
12491 return ReplaceAllUsesOfValueWith(*From, *To);
12492
12493 transferDbgValues(*From, *To);
12494 copyExtraInfo(From->getNode(), To->getNode());
12495
12496 // Read up all the uses and make records of them. This helps
12497 // processing new uses that are introduced during the
12498 // replacement process.
12500 for (unsigned i = 0; i != Num; ++i) {
12501 unsigned FromResNo = From[i].getResNo();
12502 SDNode *FromNode = From[i].getNode();
12503 for (SDUse &Use : FromNode->uses()) {
12504 if (Use.getResNo() == FromResNo) {
12505 UseMemo Memo = {Use.getUser(), i, &Use};
12506 Uses.push_back(Memo);
12507 }
12508 }
12509 }
12510
12511 // Sort the uses, so that all the uses from a given User are together.
12513 RAUOVWUpdateListener Listener(*this, Uses);
12514
12515 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12516 UseIndex != UseIndexEnd; ) {
12517 // We know that this user uses some value of From. If it is the right
12518 // value, update it.
12519 SDNode *User = Uses[UseIndex].User;
12520 // If the node has been deleted by recursive CSE updates when updating
12521 // another node, then just skip this entry.
12522 if (User == nullptr) {
12523 ++UseIndex;
12524 continue;
12525 }
12526
12527 // This node is about to morph, remove its old self from the CSE maps.
12528 RemoveNodeFromCSEMaps(User);
12529
12530 // The Uses array is sorted, so all the uses for a given User
12531 // are next to each other in the list.
12532 // To help reduce the number of CSE recomputations, process all
12533 // the uses of this user that we can find this way.
12534 do {
12535 unsigned i = Uses[UseIndex].Index;
12536 SDUse &Use = *Uses[UseIndex].Use;
12537 ++UseIndex;
12538
12539 Use.set(To[i]);
12540 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12541
12542 // Now that we have modified User, add it back to the CSE maps. If it
12543 // already exists there, recursively merge the results together.
12544 AddModifiedNodeToCSEMaps(User);
12545 }
12546}
12547
12548/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12549/// based on their topological order. It returns the maximum id and a vector
12550/// of the SDNodes* in assigned order by reference.
12552 unsigned DAGSize = 0;
12553
12554 // SortedPos tracks the progress of the algorithm. Nodes before it are
12555 // sorted, nodes after it are unsorted. When the algorithm completes
12556 // it is at the end of the list.
12557 allnodes_iterator SortedPos = allnodes_begin();
12558
12559 // Visit all the nodes. Move nodes with no operands to the front of
12560 // the list immediately. Annotate nodes that do have operands with their
12561 // operand count. Before we do this, the Node Id fields of the nodes
12562 // may contain arbitrary values. After, the Node Id fields for nodes
12563 // before SortedPos will contain the topological sort index, and the
12564 // Node Id fields for nodes At SortedPos and after will contain the
12565 // count of outstanding operands.
12567 checkForCycles(&N, this);
12568 unsigned Degree = N.getNumOperands();
12569 if (Degree == 0) {
12570 // A node with no uses, add it to the result array immediately.
12571 N.setNodeId(DAGSize++);
12572 allnodes_iterator Q(&N);
12573 if (Q != SortedPos)
12574 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12575 assert(SortedPos != AllNodes.end() && "Overran node list");
12576 ++SortedPos;
12577 } else {
12578 // Temporarily use the Node Id as scratch space for the degree count.
12579 N.setNodeId(Degree);
12580 }
12581 }
12582
12583 // Visit all the nodes. As we iterate, move nodes into sorted order,
12584 // such that by the time the end is reached all nodes will be sorted.
12585 for (SDNode &Node : allnodes()) {
12586 SDNode *N = &Node;
12587 checkForCycles(N, this);
12588 // N is in sorted position, so all its uses have one less operand
12589 // that needs to be sorted.
12590 for (SDNode *P : N->users()) {
12591 unsigned Degree = P->getNodeId();
12592 assert(Degree != 0 && "Invalid node degree");
12593 --Degree;
12594 if (Degree == 0) {
12595 // All of P's operands are sorted, so P may sorted now.
12596 P->setNodeId(DAGSize++);
12597 if (P->getIterator() != SortedPos)
12598 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12599 assert(SortedPos != AllNodes.end() && "Overran node list");
12600 ++SortedPos;
12601 } else {
12602 // Update P's outstanding operand count.
12603 P->setNodeId(Degree);
12604 }
12605 }
12606 if (Node.getIterator() == SortedPos) {
12607#ifndef NDEBUG
12609 SDNode *S = &*++I;
12610 dbgs() << "Overran sorted position:\n";
12611 S->dumprFull(this); dbgs() << "\n";
12612 dbgs() << "Checking if this is due to cycles\n";
12613 checkForCycles(this, true);
12614#endif
12615 llvm_unreachable(nullptr);
12616 }
12617 }
12618
12619 assert(SortedPos == AllNodes.end() &&
12620 "Topological sort incomplete!");
12621 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12622 "First node in topological sort is not the entry token!");
12623 assert(AllNodes.front().getNodeId() == 0 &&
12624 "First node in topological sort has non-zero id!");
12625 assert(AllNodes.front().getNumOperands() == 0 &&
12626 "First node in topological sort has operands!");
12627 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12628 "Last node in topologic sort has unexpected id!");
12629 assert(AllNodes.back().use_empty() &&
12630 "Last node in topologic sort has users!");
12631 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12632 return DAGSize;
12633}
12634
12636 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12637 SortedNodes.clear();
12638 // Node -> remaining number of outstanding operands.
12639 DenseMap<const SDNode *, unsigned> RemainingOperands;
12640
12641 // Put nodes without any operands into SortedNodes first.
12642 for (const SDNode &N : allnodes()) {
12643 checkForCycles(&N, this);
12644 unsigned NumOperands = N.getNumOperands();
12645 if (NumOperands == 0)
12646 SortedNodes.push_back(&N);
12647 else
12648 // Record their total number of outstanding operands.
12649 RemainingOperands[&N] = NumOperands;
12650 }
12651
12652 // A node is pushed into SortedNodes when all of its operands (predecessors in
12653 // the graph) are also in SortedNodes.
12654 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12655 const SDNode *N = SortedNodes[i];
12656 for (const SDNode *U : N->users()) {
12657 // HandleSDNode is never part of a DAG and therefore has no entry in
12658 // RemainingOperands.
12659 if (U->getOpcode() == ISD::HANDLENODE)
12660 continue;
12661 unsigned &NumRemOperands = RemainingOperands[U];
12662 assert(NumRemOperands && "Invalid number of remaining operands");
12663 --NumRemOperands;
12664 if (!NumRemOperands)
12665 SortedNodes.push_back(U);
12666 }
12667 }
12668
12669 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12670 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12671 "First node in topological sort is not the entry token");
12672 assert(SortedNodes.front()->getNumOperands() == 0 &&
12673 "First node in topological sort has operands");
12674}
12675
12676/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12677/// value is produced by SD.
12678void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12679 for (SDNode *SD : DB->getSDNodes()) {
12680 if (!SD)
12681 continue;
12682 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12683 SD->setHasDebugValue(true);
12684 }
12685 DbgInfo->add(DB, isParameter);
12686}
12687
12688void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12689
12691 SDValue NewMemOpChain) {
12692 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12693 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12694 // The new memory operation must have the same position as the old load in
12695 // terms of memory dependency. Create a TokenFactor for the old load and new
12696 // memory operation and update uses of the old load's output chain to use that
12697 // TokenFactor.
12698 if (OldChain == NewMemOpChain || OldChain.use_empty())
12699 return NewMemOpChain;
12700
12701 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12702 OldChain, NewMemOpChain);
12703 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12704 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12705 return TokenFactor;
12706}
12707
12709 SDValue NewMemOp) {
12710 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12711 SDValue OldChain = SDValue(OldLoad, 1);
12712 SDValue NewMemOpChain = NewMemOp.getValue(1);
12713 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12714}
12715
12717 Function **OutFunction) {
12718 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12719
12720 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12721 auto *Module = MF->getFunction().getParent();
12722 auto *Function = Module->getFunction(Symbol);
12723
12724 if (OutFunction != nullptr)
12725 *OutFunction = Function;
12726
12727 if (Function != nullptr) {
12728 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12729 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12730 }
12731
12732 std::string ErrorStr;
12733 raw_string_ostream ErrorFormatter(ErrorStr);
12734 ErrorFormatter << "Undefined external symbol ";
12735 ErrorFormatter << '"' << Symbol << '"';
12736 report_fatal_error(Twine(ErrorStr));
12737}
12738
12739//===----------------------------------------------------------------------===//
12740// SDNode Class
12741//===----------------------------------------------------------------------===//
12742
12745 return Const != nullptr && Const->isZero();
12746}
12747
12749 return V.isUndef() || isNullConstant(V);
12750}
12751
12754 return Const != nullptr && Const->isZero() && !Const->isNegative();
12755}
12756
12759 return Const != nullptr && Const->isAllOnes();
12760}
12761
12764 return Const != nullptr && Const->isOne();
12765}
12766
12769 return Const != nullptr && Const->isMinSignedValue();
12770}
12771
12772bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12773 unsigned OperandNo) {
12774 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12775 // TODO: Target-specific opcodes could be added.
12776 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12777 /*AllowTruncation*/ true)) {
12778 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12779 switch (Opcode) {
12780 case ISD::ADD:
12781 case ISD::OR:
12782 case ISD::XOR:
12783 case ISD::UMAX:
12784 return Const.isZero();
12785 case ISD::MUL:
12786 return Const.isOne();
12787 case ISD::AND:
12788 case ISD::UMIN:
12789 return Const.isAllOnes();
12790 case ISD::SMAX:
12791 return Const.isMinSignedValue();
12792 case ISD::SMIN:
12793 return Const.isMaxSignedValue();
12794 case ISD::SUB:
12795 case ISD::SHL:
12796 case ISD::SRA:
12797 case ISD::SRL:
12798 return OperandNo == 1 && Const.isZero();
12799 case ISD::UDIV:
12800 case ISD::SDIV:
12801 return OperandNo == 1 && Const.isOne();
12802 }
12803 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12804 switch (Opcode) {
12805 case ISD::FADD:
12806 return ConstFP->isZero() &&
12807 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12808 case ISD::FSUB:
12809 return OperandNo == 1 && ConstFP->isZero() &&
12810 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12811 case ISD::FMUL:
12812 return ConstFP->isExactlyValue(1.0);
12813 case ISD::FDIV:
12814 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12815 case ISD::FMINNUM:
12816 case ISD::FMAXNUM: {
12817 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12818 EVT VT = V.getValueType();
12819 const fltSemantics &Semantics = VT.getFltSemantics();
12820 APFloat NeutralAF = !Flags.hasNoNaNs()
12821 ? APFloat::getQNaN(Semantics)
12822 : !Flags.hasNoInfs()
12823 ? APFloat::getInf(Semantics)
12824 : APFloat::getLargest(Semantics);
12825 if (Opcode == ISD::FMAXNUM)
12826 NeutralAF.changeSign();
12827
12828 return ConstFP->isExactlyValue(NeutralAF);
12829 }
12830 }
12831 }
12832 return false;
12833}
12834
12836 while (V.getOpcode() == ISD::BITCAST)
12837 V = V.getOperand(0);
12838 return V;
12839}
12840
12842 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12843 V = V.getOperand(0);
12844 return V;
12845}
12846
12848 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12849 V = V.getOperand(0);
12850 return V;
12851}
12852
12854 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12855 SDValue InVec = V.getOperand(0);
12856 SDValue EltNo = V.getOperand(2);
12857 EVT VT = InVec.getValueType();
12858 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12859 if (IndexC && VT.isFixedLengthVector() &&
12860 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12861 !DemandedElts[IndexC->getZExtValue()]) {
12862 V = InVec;
12863 continue;
12864 }
12865 break;
12866 }
12867 return V;
12868}
12869
12871 while (V.getOpcode() == ISD::TRUNCATE)
12872 V = V.getOperand(0);
12873 return V;
12874}
12875
12876bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12877 if (V.getOpcode() != ISD::XOR)
12878 return false;
12879 V = peekThroughBitcasts(V.getOperand(1));
12880 unsigned NumBits = V.getScalarValueSizeInBits();
12881 ConstantSDNode *C =
12882 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12883 return C && (C->getAPIntValue().countr_one() >= NumBits);
12884}
12885
12887 bool AllowTruncation) {
12888 EVT VT = N.getValueType();
12889 APInt DemandedElts = VT.isFixedLengthVector()
12891 : APInt(1, 1);
12892 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12893}
12894
12896 bool AllowUndefs,
12897 bool AllowTruncation) {
12899 return CN;
12900
12901 // SplatVectors can truncate their operands. Ignore that case here unless
12902 // AllowTruncation is set.
12903 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12904 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12905 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12906 EVT CVT = CN->getValueType(0);
12907 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12908 if (AllowTruncation || CVT == VecEltVT)
12909 return CN;
12910 }
12911 }
12912
12914 BitVector UndefElements;
12915 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12916
12917 // BuildVectors can truncate their operands. Ignore that case here unless
12918 // AllowTruncation is set.
12919 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12920 if (CN && (UndefElements.none() || AllowUndefs)) {
12921 EVT CVT = CN->getValueType(0);
12922 EVT NSVT = N.getValueType().getScalarType();
12923 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12924 if (AllowTruncation || (CVT == NSVT))
12925 return CN;
12926 }
12927 }
12928
12929 return nullptr;
12930}
12931
12933 EVT VT = N.getValueType();
12934 APInt DemandedElts = VT.isFixedLengthVector()
12936 : APInt(1, 1);
12937 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
12938}
12939
12941 const APInt &DemandedElts,
12942 bool AllowUndefs) {
12944 return CN;
12945
12947 BitVector UndefElements;
12948 ConstantFPSDNode *CN =
12949 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
12950 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12951 if (CN && (UndefElements.none() || AllowUndefs))
12952 return CN;
12953 }
12954
12955 if (N.getOpcode() == ISD::SPLAT_VECTOR)
12956 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
12957 return CN;
12958
12959 return nullptr;
12960}
12961
12962bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
12963 // TODO: may want to use peekThroughBitcast() here.
12964 ConstantSDNode *C =
12965 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
12966 return C && C->isZero();
12967}
12968
12969bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
12970 ConstantSDNode *C =
12971 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
12972 return C && C->isOne();
12973}
12974
12975bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
12976 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
12977 return C && C->isExactlyValue(1.0);
12978}
12979
12980bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
12982 unsigned BitWidth = N.getScalarValueSizeInBits();
12983 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12984 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
12985}
12986
12987bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
12988 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
12989 return C && APInt::isSameValue(C->getAPIntValue(),
12990 APInt(C->getAPIntValue().getBitWidth(), 1));
12991}
12992
12993bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
12995 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
12996 return C && C->isZero();
12997}
12998
12999bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13000 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13001 return C && C->isZero();
13002}
13003
13007
13008MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13009 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13010 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13011 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13012 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13013 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13014 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13015
13016 // We check here that the size of the memory operand fits within the size of
13017 // the MMO. This is because the MMO might indicate only a possible address
13018 // range instead of specifying the affected memory addresses precisely.
13019 assert(
13020 (!MMO->getType().isValid() ||
13021 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13022 "Size mismatch!");
13023}
13024
13025/// Profile - Gather unique data for the node.
13026///
13028 AddNodeIDNode(ID, this);
13029}
13030
13031namespace {
13032
13033 struct EVTArray {
13034 std::vector<EVT> VTs;
13035
13036 EVTArray() {
13037 VTs.reserve(MVT::VALUETYPE_SIZE);
13038 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13039 VTs.push_back(MVT((MVT::SimpleValueType)i));
13040 }
13041 };
13042
13043} // end anonymous namespace
13044
13045/// getValueTypeList - Return a pointer to the specified value type.
13046///
13047const EVT *SDNode::getValueTypeList(MVT VT) {
13048 static EVTArray SimpleVTArray;
13049
13050 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13051 return &SimpleVTArray.VTs[VT.SimpleTy];
13052}
13053
13054/// hasAnyUseOfValue - Return true if there are any use of the indicated
13055/// value. This method ignores uses of other values defined by this operation.
13056bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13057 assert(Value < getNumValues() && "Bad value!");
13058
13059 for (SDUse &U : uses())
13060 if (U.getResNo() == Value)
13061 return true;
13062
13063 return false;
13064}
13065
13066/// isOnlyUserOf - Return true if this node is the only use of N.
13067bool SDNode::isOnlyUserOf(const SDNode *N) const {
13068 bool Seen = false;
13069 for (const SDNode *User : N->users()) {
13070 if (User == this)
13071 Seen = true;
13072 else
13073 return false;
13074 }
13075
13076 return Seen;
13077}
13078
13079/// Return true if the only users of N are contained in Nodes.
13081 bool Seen = false;
13082 for (const SDNode *User : N->users()) {
13083 if (llvm::is_contained(Nodes, User))
13084 Seen = true;
13085 else
13086 return false;
13087 }
13088
13089 return Seen;
13090}
13091
13092/// Return true if the referenced return value is an operand of N.
13093bool SDValue::isOperandOf(const SDNode *N) const {
13094 return is_contained(N->op_values(), *this);
13095}
13096
13097bool SDNode::isOperandOf(const SDNode *N) const {
13098 return any_of(N->op_values(),
13099 [this](SDValue Op) { return this == Op.getNode(); });
13100}
13101
13102/// reachesChainWithoutSideEffects - Return true if this operand (which must
13103/// be a chain) reaches the specified operand without crossing any
13104/// side-effecting instructions on any chain path. In practice, this looks
13105/// through token factors and non-volatile loads. In order to remain efficient,
13106/// this only looks a couple of nodes in, it does not do an exhaustive search.
13107///
13108/// Note that we only need to examine chains when we're searching for
13109/// side-effects; SelectionDAG requires that all side-effects are represented
13110/// by chains, even if another operand would force a specific ordering. This
13111/// constraint is necessary to allow transformations like splitting loads.
13113 unsigned Depth) const {
13114 if (*this == Dest) return true;
13115
13116 // Don't search too deeply, we just want to be able to see through
13117 // TokenFactor's etc.
13118 if (Depth == 0) return false;
13119
13120 // If this is a token factor, all inputs to the TF happen in parallel.
13121 if (getOpcode() == ISD::TokenFactor) {
13122 // First, try a shallow search.
13123 if (is_contained((*this)->ops(), Dest)) {
13124 // We found the chain we want as an operand of this TokenFactor.
13125 // Essentially, we reach the chain without side-effects if we could
13126 // serialize the TokenFactor into a simple chain of operations with
13127 // Dest as the last operation. This is automatically true if the
13128 // chain has one use: there are no other ordering constraints.
13129 // If the chain has more than one use, we give up: some other
13130 // use of Dest might force a side-effect between Dest and the current
13131 // node.
13132 if (Dest.hasOneUse())
13133 return true;
13134 }
13135 // Next, try a deep search: check whether every operand of the TokenFactor
13136 // reaches Dest.
13137 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13138 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13139 });
13140 }
13141
13142 // Loads don't have side effects, look through them.
13143 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13144 if (Ld->isUnordered())
13145 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13146 }
13147 return false;
13148}
13149
13150bool SDNode::hasPredecessor(const SDNode *N) const {
13153 Worklist.push_back(this);
13154 return hasPredecessorHelper(N, Visited, Worklist);
13155}
13156
13158 this->Flags &= Flags;
13159}
13160
13161SDValue
13163 ArrayRef<ISD::NodeType> CandidateBinOps,
13164 bool AllowPartials) {
13165 // The pattern must end in an extract from index 0.
13166 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13167 !isNullConstant(Extract->getOperand(1)))
13168 return SDValue();
13169
13170 // Match against one of the candidate binary ops.
13171 SDValue Op = Extract->getOperand(0);
13172 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13173 return Op.getOpcode() == unsigned(BinOp);
13174 }))
13175 return SDValue();
13176
13177 // Floating-point reductions may require relaxed constraints on the final step
13178 // of the reduction because they may reorder intermediate operations.
13179 unsigned CandidateBinOp = Op.getOpcode();
13180 if (Op.getValueType().isFloatingPoint()) {
13181 SDNodeFlags Flags = Op->getFlags();
13182 switch (CandidateBinOp) {
13183 case ISD::FADD:
13184 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13185 return SDValue();
13186 break;
13187 default:
13188 llvm_unreachable("Unhandled FP opcode for binop reduction");
13189 }
13190 }
13191
13192 // Matching failed - attempt to see if we did enough stages that a partial
13193 // reduction from a subvector is possible.
13194 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13195 if (!AllowPartials || !Op)
13196 return SDValue();
13197 EVT OpVT = Op.getValueType();
13198 EVT OpSVT = OpVT.getScalarType();
13199 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13200 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13201 return SDValue();
13202 BinOp = (ISD::NodeType)CandidateBinOp;
13203 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13204 };
13205
13206 // At each stage, we're looking for something that looks like:
13207 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13208 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13209 // i32 undef, i32 undef, i32 undef, i32 undef>
13210 // %a = binop <8 x i32> %op, %s
13211 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13212 // we expect something like:
13213 // <4,5,6,7,u,u,u,u>
13214 // <2,3,u,u,u,u,u,u>
13215 // <1,u,u,u,u,u,u,u>
13216 // While a partial reduction match would be:
13217 // <2,3,u,u,u,u,u,u>
13218 // <1,u,u,u,u,u,u,u>
13219 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13220 SDValue PrevOp;
13221 for (unsigned i = 0; i < Stages; ++i) {
13222 unsigned MaskEnd = (1 << i);
13223
13224 if (Op.getOpcode() != CandidateBinOp)
13225 return PartialReduction(PrevOp, MaskEnd);
13226
13227 SDValue Op0 = Op.getOperand(0);
13228 SDValue Op1 = Op.getOperand(1);
13229
13231 if (Shuffle) {
13232 Op = Op1;
13233 } else {
13234 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13235 Op = Op0;
13236 }
13237
13238 // The first operand of the shuffle should be the same as the other operand
13239 // of the binop.
13240 if (!Shuffle || Shuffle->getOperand(0) != Op)
13241 return PartialReduction(PrevOp, MaskEnd);
13242
13243 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13244 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13245 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13246 return PartialReduction(PrevOp, MaskEnd);
13247
13248 PrevOp = Op;
13249 }
13250
13251 // Handle subvector reductions, which tend to appear after the shuffle
13252 // reduction stages.
13253 while (Op.getOpcode() == CandidateBinOp) {
13254 unsigned NumElts = Op.getValueType().getVectorNumElements();
13255 SDValue Op0 = Op.getOperand(0);
13256 SDValue Op1 = Op.getOperand(1);
13257 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13259 Op0.getOperand(0) != Op1.getOperand(0))
13260 break;
13261 SDValue Src = Op0.getOperand(0);
13262 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13263 if (NumSrcElts != (2 * NumElts))
13264 break;
13265 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13266 Op1.getConstantOperandAPInt(1) == NumElts) &&
13267 !(Op1.getConstantOperandAPInt(1) == 0 &&
13268 Op0.getConstantOperandAPInt(1) == NumElts))
13269 break;
13270 Op = Src;
13271 }
13272
13273 BinOp = (ISD::NodeType)CandidateBinOp;
13274 return Op;
13275}
13276
13278 EVT VT = N->getValueType(0);
13279 EVT EltVT = VT.getVectorElementType();
13280 unsigned NE = VT.getVectorNumElements();
13281
13282 SDLoc dl(N);
13283
13284 // If ResNE is 0, fully unroll the vector op.
13285 if (ResNE == 0)
13286 ResNE = NE;
13287 else if (NE > ResNE)
13288 NE = ResNE;
13289
13290 if (N->getNumValues() == 2) {
13291 SmallVector<SDValue, 8> Scalars0, Scalars1;
13292 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13293 EVT VT1 = N->getValueType(1);
13294 EVT EltVT1 = VT1.getVectorElementType();
13295
13296 unsigned i;
13297 for (i = 0; i != NE; ++i) {
13298 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13299 SDValue Operand = N->getOperand(j);
13300 EVT OperandVT = Operand.getValueType();
13301
13302 // A vector operand; extract a single element.
13303 EVT OperandEltVT = OperandVT.getVectorElementType();
13304 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13305 }
13306
13307 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13308 Scalars0.push_back(EltOp);
13309 Scalars1.push_back(EltOp.getValue(1));
13310 }
13311
13312 for (; i < ResNE; ++i) {
13313 Scalars0.push_back(getUNDEF(EltVT));
13314 Scalars1.push_back(getUNDEF(EltVT1));
13315 }
13316
13317 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13318 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13319 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13320 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13321 return getMergeValues({Vec0, Vec1}, dl);
13322 }
13323
13324 assert(N->getNumValues() == 1 &&
13325 "Can't unroll a vector with multiple results!");
13326
13328 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13329
13330 unsigned i;
13331 for (i= 0; i != NE; ++i) {
13332 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13333 SDValue Operand = N->getOperand(j);
13334 EVT OperandVT = Operand.getValueType();
13335 if (OperandVT.isVector()) {
13336 // A vector operand; extract a single element.
13337 EVT OperandEltVT = OperandVT.getVectorElementType();
13338 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13339 } else {
13340 // A scalar operand; just use it as is.
13341 Operands[j] = Operand;
13342 }
13343 }
13344
13345 switch (N->getOpcode()) {
13346 default: {
13347 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13348 N->getFlags()));
13349 break;
13350 }
13351 case ISD::VSELECT:
13352 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13353 break;
13354 case ISD::SHL:
13355 case ISD::SRA:
13356 case ISD::SRL:
13357 case ISD::ROTL:
13358 case ISD::ROTR:
13359 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13360 getShiftAmountOperand(Operands[0].getValueType(),
13361 Operands[1])));
13362 break;
13364 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13365 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13366 Operands[0],
13367 getValueType(ExtVT)));
13368 break;
13369 }
13370 case ISD::ADDRSPACECAST: {
13371 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13372 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13373 ASC->getSrcAddressSpace(),
13374 ASC->getDestAddressSpace()));
13375 break;
13376 }
13377 }
13378 }
13379
13380 for (; i < ResNE; ++i)
13381 Scalars.push_back(getUNDEF(EltVT));
13382
13383 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13384 return getBuildVector(VecVT, dl, Scalars);
13385}
13386
13387std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13388 SDNode *N, unsigned ResNE) {
13389 unsigned Opcode = N->getOpcode();
13390 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13391 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13392 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13393 "Expected an overflow opcode");
13394
13395 EVT ResVT = N->getValueType(0);
13396 EVT OvVT = N->getValueType(1);
13397 EVT ResEltVT = ResVT.getVectorElementType();
13398 EVT OvEltVT = OvVT.getVectorElementType();
13399 SDLoc dl(N);
13400
13401 // If ResNE is 0, fully unroll the vector op.
13402 unsigned NE = ResVT.getVectorNumElements();
13403 if (ResNE == 0)
13404 ResNE = NE;
13405 else if (NE > ResNE)
13406 NE = ResNE;
13407
13408 SmallVector<SDValue, 8> LHSScalars;
13409 SmallVector<SDValue, 8> RHSScalars;
13410 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13411 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13412
13413 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13414 SDVTList VTs = getVTList(ResEltVT, SVT);
13415 SmallVector<SDValue, 8> ResScalars;
13416 SmallVector<SDValue, 8> OvScalars;
13417 for (unsigned i = 0; i < NE; ++i) {
13418 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13419 SDValue Ov =
13420 getSelect(dl, OvEltVT, Res.getValue(1),
13421 getBoolConstant(true, dl, OvEltVT, ResVT),
13422 getConstant(0, dl, OvEltVT));
13423
13424 ResScalars.push_back(Res);
13425 OvScalars.push_back(Ov);
13426 }
13427
13428 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13429 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13430
13431 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13432 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13433 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13434 getBuildVector(NewOvVT, dl, OvScalars));
13435}
13436
13439 unsigned Bytes,
13440 int Dist) const {
13441 if (LD->isVolatile() || Base->isVolatile())
13442 return false;
13443 // TODO: probably too restrictive for atomics, revisit
13444 if (!LD->isSimple())
13445 return false;
13446 if (LD->isIndexed() || Base->isIndexed())
13447 return false;
13448 if (LD->getChain() != Base->getChain())
13449 return false;
13450 EVT VT = LD->getMemoryVT();
13451 if (VT.getSizeInBits() / 8 != Bytes)
13452 return false;
13453
13454 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13455 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13456
13457 int64_t Offset = 0;
13458 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13459 return (Dist * (int64_t)Bytes == Offset);
13460 return false;
13461}
13462
13463/// InferPtrAlignment - Infer alignment of a load / store address. Return
13464/// std::nullopt if it cannot be inferred.
13466 // If this is a GlobalAddress + cst, return the alignment.
13467 const GlobalValue *GV = nullptr;
13468 int64_t GVOffset = 0;
13469 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13470 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13471 KnownBits Known(PtrWidth);
13473 unsigned AlignBits = Known.countMinTrailingZeros();
13474 if (AlignBits)
13475 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13476 }
13477
13478 // If this is a direct reference to a stack slot, use information about the
13479 // stack slot's alignment.
13480 int FrameIdx = INT_MIN;
13481 int64_t FrameOffset = 0;
13483 FrameIdx = FI->getIndex();
13484 } else if (isBaseWithConstantOffset(Ptr) &&
13486 // Handle FI+Cst
13487 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13488 FrameOffset = Ptr.getConstantOperandVal(1);
13489 }
13490
13491 if (FrameIdx != INT_MIN) {
13493 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13494 }
13495
13496 return std::nullopt;
13497}
13498
13499/// Split the scalar node with EXTRACT_ELEMENT using the provided
13500/// VTs and return the low/high part.
13501std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13502 const SDLoc &DL,
13503 const EVT &LoVT,
13504 const EVT &HiVT) {
13505 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13506 "Split node must be a scalar type");
13507 SDValue Lo =
13509 SDValue Hi =
13511 return std::make_pair(Lo, Hi);
13512}
13513
13514/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13515/// which is split (or expanded) into two not necessarily identical pieces.
13516std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13517 // Currently all types are split in half.
13518 EVT LoVT, HiVT;
13519 if (!VT.isVector())
13520 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13521 else
13522 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13523
13524 return std::make_pair(LoVT, HiVT);
13525}
13526
13527/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13528/// type, dependent on an enveloping VT that has been split into two identical
13529/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13530std::pair<EVT, EVT>
13532 bool *HiIsEmpty) const {
13533 EVT EltTp = VT.getVectorElementType();
13534 // Examples:
13535 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13536 // custom VL=9 with enveloping VL=8/8 yields 8/1
13537 // custom VL=10 with enveloping VL=8/8 yields 8/2
13538 // etc.
13539 ElementCount VTNumElts = VT.getVectorElementCount();
13540 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13541 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13542 "Mixing fixed width and scalable vectors when enveloping a type");
13543 EVT LoVT, HiVT;
13544 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13545 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13546 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13547 *HiIsEmpty = false;
13548 } else {
13549 // Flag that hi type has zero storage size, but return split envelop type
13550 // (this would be easier if vector types with zero elements were allowed).
13551 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13552 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13553 *HiIsEmpty = true;
13554 }
13555 return std::make_pair(LoVT, HiVT);
13556}
13557
13558/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13559/// low/high part.
13560std::pair<SDValue, SDValue>
13561SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13562 const EVT &HiVT) {
13563 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13564 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13565 "Splitting vector with an invalid mixture of fixed and scalable "
13566 "vector types");
13568 N.getValueType().getVectorMinNumElements() &&
13569 "More vector elements requested than available!");
13570 SDValue Lo, Hi;
13571 Lo = getExtractSubvector(DL, LoVT, N, 0);
13572 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13573 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13574 // IDX with the runtime scaling factor of the result vector type. For
13575 // fixed-width result vectors, that runtime scaling factor is 1.
13578 return std::make_pair(Lo, Hi);
13579}
13580
13581std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13582 const SDLoc &DL) {
13583 // Split the vector length parameter.
13584 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13585 EVT VT = N.getValueType();
13587 "Expecting the mask to be an evenly-sized vector");
13588 unsigned HalfMinNumElts = VecVT.getVectorMinNumElements() / 2;
13589 SDValue HalfNumElts =
13590 VecVT.isFixedLengthVector()
13591 ? getConstant(HalfMinNumElts, DL, VT)
13592 : getVScale(DL, VT, APInt(VT.getScalarSizeInBits(), HalfMinNumElts));
13593 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13594 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13595 return std::make_pair(Lo, Hi);
13596}
13597
13598/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13600 EVT VT = N.getValueType();
13603 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13604}
13605
13608 unsigned Start, unsigned Count,
13609 EVT EltVT) {
13610 EVT VT = Op.getValueType();
13611 if (Count == 0)
13613 if (EltVT == EVT())
13614 EltVT = VT.getVectorElementType();
13615 SDLoc SL(Op);
13616 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13617 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13618 }
13619}
13620
13621// getAddressSpace - Return the address space this GlobalAddress belongs to.
13623 return getGlobal()->getType()->getAddressSpace();
13624}
13625
13628 return Val.MachineCPVal->getType();
13629 return Val.ConstVal->getType();
13630}
13631
13632bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13633 unsigned &SplatBitSize,
13634 bool &HasAnyUndefs,
13635 unsigned MinSplatBits,
13636 bool IsBigEndian) const {
13637 EVT VT = getValueType(0);
13638 assert(VT.isVector() && "Expected a vector type");
13639 unsigned VecWidth = VT.getSizeInBits();
13640 if (MinSplatBits > VecWidth)
13641 return false;
13642
13643 // FIXME: The widths are based on this node's type, but build vectors can
13644 // truncate their operands.
13645 SplatValue = APInt(VecWidth, 0);
13646 SplatUndef = APInt(VecWidth, 0);
13647
13648 // Get the bits. Bits with undefined values (when the corresponding element
13649 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13650 // in SplatValue. If any of the values are not constant, give up and return
13651 // false.
13652 unsigned int NumOps = getNumOperands();
13653 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13654 unsigned EltWidth = VT.getScalarSizeInBits();
13655
13656 for (unsigned j = 0; j < NumOps; ++j) {
13657 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13658 SDValue OpVal = getOperand(i);
13659 unsigned BitPos = j * EltWidth;
13660
13661 if (OpVal.isUndef())
13662 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13663 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13664 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13665 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13666 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13667 else
13668 return false;
13669 }
13670
13671 // The build_vector is all constants or undefs. Find the smallest element
13672 // size that splats the vector.
13673 HasAnyUndefs = (SplatUndef != 0);
13674
13675 // FIXME: This does not work for vectors with elements less than 8 bits.
13676 while (VecWidth > 8) {
13677 // If we can't split in half, stop here.
13678 if (VecWidth & 1)
13679 break;
13680
13681 unsigned HalfSize = VecWidth / 2;
13682 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13683 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13684 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13685 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13686
13687 // If the two halves do not match (ignoring undef bits), stop here.
13688 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13689 MinSplatBits > HalfSize)
13690 break;
13691
13692 SplatValue = HighValue | LowValue;
13693 SplatUndef = HighUndef & LowUndef;
13694
13695 VecWidth = HalfSize;
13696 }
13697
13698 // FIXME: The loop above only tries to split in halves. But if the input
13699 // vector for example is <3 x i16> it wouldn't be able to detect a
13700 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13701 // optimizations. I guess that back in the days when this helper was created
13702 // vectors normally was power-of-2 sized.
13703
13704 SplatBitSize = VecWidth;
13705 return true;
13706}
13707
13709 BitVector *UndefElements) const {
13710 unsigned NumOps = getNumOperands();
13711 if (UndefElements) {
13712 UndefElements->clear();
13713 UndefElements->resize(NumOps);
13714 }
13715 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13716 if (!DemandedElts)
13717 return SDValue();
13718 SDValue Splatted;
13719 for (unsigned i = 0; i != NumOps; ++i) {
13720 if (!DemandedElts[i])
13721 continue;
13722 SDValue Op = getOperand(i);
13723 if (Op.isUndef()) {
13724 if (UndefElements)
13725 (*UndefElements)[i] = true;
13726 } else if (!Splatted) {
13727 Splatted = Op;
13728 } else if (Splatted != Op) {
13729 return SDValue();
13730 }
13731 }
13732
13733 if (!Splatted) {
13734 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13735 assert(getOperand(FirstDemandedIdx).isUndef() &&
13736 "Can only have a splat without a constant for all undefs.");
13737 return getOperand(FirstDemandedIdx);
13738 }
13739
13740 return Splatted;
13741}
13742
13744 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13745 return getSplatValue(DemandedElts, UndefElements);
13746}
13747
13749 SmallVectorImpl<SDValue> &Sequence,
13750 BitVector *UndefElements) const {
13751 unsigned NumOps = getNumOperands();
13752 Sequence.clear();
13753 if (UndefElements) {
13754 UndefElements->clear();
13755 UndefElements->resize(NumOps);
13756 }
13757 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13758 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13759 return false;
13760
13761 // Set the undefs even if we don't find a sequence (like getSplatValue).
13762 if (UndefElements)
13763 for (unsigned I = 0; I != NumOps; ++I)
13764 if (DemandedElts[I] && getOperand(I).isUndef())
13765 (*UndefElements)[I] = true;
13766
13767 // Iteratively widen the sequence length looking for repetitions.
13768 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13769 Sequence.append(SeqLen, SDValue());
13770 for (unsigned I = 0; I != NumOps; ++I) {
13771 if (!DemandedElts[I])
13772 continue;
13773 SDValue &SeqOp = Sequence[I % SeqLen];
13775 if (Op.isUndef()) {
13776 if (!SeqOp)
13777 SeqOp = Op;
13778 continue;
13779 }
13780 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13781 Sequence.clear();
13782 break;
13783 }
13784 SeqOp = Op;
13785 }
13786 if (!Sequence.empty())
13787 return true;
13788 }
13789
13790 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13791 return false;
13792}
13793
13795 BitVector *UndefElements) const {
13796 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13797 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13798}
13799
13802 BitVector *UndefElements) const {
13804 getSplatValue(DemandedElts, UndefElements));
13805}
13806
13809 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13810}
13811
13814 BitVector *UndefElements) const {
13816 getSplatValue(DemandedElts, UndefElements));
13817}
13818
13823
13824int32_t
13826 uint32_t BitWidth) const {
13827 if (ConstantFPSDNode *CN =
13829 bool IsExact;
13830 APSInt IntVal(BitWidth);
13831 const APFloat &APF = CN->getValueAPF();
13832 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13833 APFloat::opOK ||
13834 !IsExact)
13835 return -1;
13836
13837 return IntVal.exactLogBase2();
13838 }
13839 return -1;
13840}
13841
13843 bool IsLittleEndian, unsigned DstEltSizeInBits,
13844 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13845 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13846 if (!isConstant())
13847 return false;
13848
13849 unsigned NumSrcOps = getNumOperands();
13850 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13851 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13852 "Invalid bitcast scale");
13853
13854 // Extract raw src bits.
13855 SmallVector<APInt> SrcBitElements(NumSrcOps,
13856 APInt::getZero(SrcEltSizeInBits));
13857 BitVector SrcUndeElements(NumSrcOps, false);
13858
13859 for (unsigned I = 0; I != NumSrcOps; ++I) {
13861 if (Op.isUndef()) {
13862 SrcUndeElements.set(I);
13863 continue;
13864 }
13865 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13866 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13867 assert((CInt || CFP) && "Unknown constant");
13868 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13869 : CFP->getValueAPF().bitcastToAPInt();
13870 }
13871
13872 // Recast to dst width.
13873 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13874 SrcBitElements, UndefElements, SrcUndeElements);
13875 return true;
13876}
13877
13878void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13879 unsigned DstEltSizeInBits,
13880 SmallVectorImpl<APInt> &DstBitElements,
13881 ArrayRef<APInt> SrcBitElements,
13882 BitVector &DstUndefElements,
13883 const BitVector &SrcUndefElements) {
13884 unsigned NumSrcOps = SrcBitElements.size();
13885 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13886 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13887 "Invalid bitcast scale");
13888 assert(NumSrcOps == SrcUndefElements.size() &&
13889 "Vector size mismatch");
13890
13891 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13892 DstUndefElements.clear();
13893 DstUndefElements.resize(NumDstOps, false);
13894 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13895
13896 // Concatenate src elements constant bits together into dst element.
13897 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13898 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13899 for (unsigned I = 0; I != NumDstOps; ++I) {
13900 DstUndefElements.set(I);
13901 APInt &DstBits = DstBitElements[I];
13902 for (unsigned J = 0; J != Scale; ++J) {
13903 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13904 if (SrcUndefElements[Idx])
13905 continue;
13906 DstUndefElements.reset(I);
13907 const APInt &SrcBits = SrcBitElements[Idx];
13908 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13909 "Illegal constant bitwidths");
13910 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13911 }
13912 }
13913 return;
13914 }
13915
13916 // Split src element constant bits into dst elements.
13917 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13918 for (unsigned I = 0; I != NumSrcOps; ++I) {
13919 if (SrcUndefElements[I]) {
13920 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13921 continue;
13922 }
13923 const APInt &SrcBits = SrcBitElements[I];
13924 for (unsigned J = 0; J != Scale; ++J) {
13925 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13926 APInt &DstBits = DstBitElements[Idx];
13927 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13928 }
13929 }
13930}
13931
13933 for (const SDValue &Op : op_values()) {
13934 unsigned Opc = Op.getOpcode();
13935 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
13936 return false;
13937 }
13938 return true;
13939}
13940
13941std::optional<std::pair<APInt, APInt>>
13943 unsigned NumOps = getNumOperands();
13944 if (NumOps < 2)
13945 return std::nullopt;
13946
13949 return std::nullopt;
13950
13951 unsigned EltSize = getValueType(0).getScalarSizeInBits();
13952 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
13953 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
13954
13955 if (Stride.isZero())
13956 return std::nullopt;
13957
13958 for (unsigned i = 2; i < NumOps; ++i) {
13960 return std::nullopt;
13961
13962 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
13963 if (Val != (Start + (Stride * i)))
13964 return std::nullopt;
13965 }
13966
13967 return std::make_pair(Start, Stride);
13968}
13969
13971 // Find the first non-undef value in the shuffle mask.
13972 unsigned i, e;
13973 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
13974 /* search */;
13975
13976 // If all elements are undefined, this shuffle can be considered a splat
13977 // (although it should eventually get simplified away completely).
13978 if (i == e)
13979 return true;
13980
13981 // Make sure all remaining elements are either undef or the same as the first
13982 // non-undef value.
13983 for (int Idx = Mask[i]; i != e; ++i)
13984 if (Mask[i] >= 0 && Mask[i] != Idx)
13985 return false;
13986 return true;
13987}
13988
13989// Returns true if it is a constant integer BuildVector or constant integer,
13990// possibly hidden by a bitcast.
13992 SDValue N, bool AllowOpaques) const {
13994
13995 if (auto *C = dyn_cast<ConstantSDNode>(N))
13996 return AllowOpaques || !C->isOpaque();
13997
13999 return true;
14000
14001 // Treat a GlobalAddress supporting constant offset folding as a
14002 // constant integer.
14003 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14004 if (GA->getOpcode() == ISD::GlobalAddress &&
14005 TLI->isOffsetFoldingLegal(GA))
14006 return true;
14007
14008 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14009 isa<ConstantSDNode>(N.getOperand(0)))
14010 return true;
14011 return false;
14012}
14013
14014// Returns true if it is a constant float BuildVector or constant float.
14017 return true;
14018
14020 return true;
14021
14022 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14023 isa<ConstantFPSDNode>(N.getOperand(0)))
14024 return true;
14025
14026 return false;
14027}
14028
14029std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14030 ConstantSDNode *Const =
14031 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14032 if (!Const)
14033 return std::nullopt;
14034
14035 EVT VT = N->getValueType(0);
14036 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14037 switch (TLI->getBooleanContents(N.getValueType())) {
14039 if (CVal.isOne())
14040 return true;
14041 if (CVal.isZero())
14042 return false;
14043 return std::nullopt;
14045 if (CVal.isAllOnes())
14046 return true;
14047 if (CVal.isZero())
14048 return false;
14049 return std::nullopt;
14051 return CVal[0];
14052 }
14053 llvm_unreachable("Unknown BooleanContent enum");
14054}
14055
14056void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14057 assert(!Node->OperandList && "Node already has operands");
14059 "too many operands to fit into SDNode");
14060 SDUse *Ops = OperandRecycler.allocate(
14061 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14062
14063 bool IsDivergent = false;
14064 for (unsigned I = 0; I != Vals.size(); ++I) {
14065 Ops[I].setUser(Node);
14066 Ops[I].setInitial(Vals[I]);
14067 EVT VT = Ops[I].getValueType();
14068
14069 // Skip Chain. It does not carry divergence.
14070 if (VT != MVT::Other &&
14071 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14072 Ops[I].getNode()->isDivergent()) {
14073 IsDivergent = true;
14074 }
14075 }
14076 Node->NumOperands = Vals.size();
14077 Node->OperandList = Ops;
14078 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14079 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14080 Node->SDNodeBits.IsDivergent = IsDivergent;
14081 }
14082 checkForCycles(Node);
14083}
14084
14087 size_t Limit = SDNode::getMaxNumOperands();
14088 while (Vals.size() > Limit) {
14089 unsigned SliceIdx = Vals.size() - Limit;
14090 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14091 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14092 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14093 Vals.emplace_back(NewTF);
14094 }
14095 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14096}
14097
14099 EVT VT, SDNodeFlags Flags) {
14100 switch (Opcode) {
14101 default:
14102 return SDValue();
14103 case ISD::ADD:
14104 case ISD::OR:
14105 case ISD::XOR:
14106 case ISD::UMAX:
14107 return getConstant(0, DL, VT);
14108 case ISD::MUL:
14109 return getConstant(1, DL, VT);
14110 case ISD::AND:
14111 case ISD::UMIN:
14112 return getAllOnesConstant(DL, VT);
14113 case ISD::SMAX:
14115 case ISD::SMIN:
14117 case ISD::FADD:
14118 // If flags allow, prefer positive zero since it's generally cheaper
14119 // to materialize on most targets.
14120 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14121 case ISD::FMUL:
14122 return getConstantFP(1.0, DL, VT);
14123 case ISD::FMINNUM:
14124 case ISD::FMAXNUM: {
14125 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14126 const fltSemantics &Semantics = VT.getFltSemantics();
14127 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14128 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14129 APFloat::getLargest(Semantics);
14130 if (Opcode == ISD::FMAXNUM)
14131 NeutralAF.changeSign();
14132
14133 return getConstantFP(NeutralAF, DL, VT);
14134 }
14135 case ISD::FMINIMUM:
14136 case ISD::FMAXIMUM: {
14137 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14138 const fltSemantics &Semantics = VT.getFltSemantics();
14139 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14140 : APFloat::getLargest(Semantics);
14141 if (Opcode == ISD::FMAXIMUM)
14142 NeutralAF.changeSign();
14143
14144 return getConstantFP(NeutralAF, DL, VT);
14145 }
14146
14147 }
14148}
14149
14150/// Helper used to make a call to a library function that has one argument of
14151/// pointer type.
14152///
14153/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14154/// used to get or set floating-point state. They have one argument of pointer
14155/// type, which points to the memory region containing bits of the
14156/// floating-point state. The value returned by such function is ignored in the
14157/// created call.
14158///
14159/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14160/// \param Ptr Pointer used to save/load state.
14161/// \param InChain Ingoing token chain.
14162/// \returns Outgoing chain token.
14164 SDValue InChain,
14165 const SDLoc &DLoc) {
14166 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14168 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14169 RTLIB::Libcall LC = static_cast<RTLIB::Libcall>(LibFunc);
14170 SDValue Callee = getExternalSymbol(TLI->getLibcallName(LC),
14171 TLI->getPointerTy(getDataLayout()));
14173 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14174 TLI->getLibcallCallingConv(LC), Type::getVoidTy(*getContext()), Callee,
14175 std::move(Args));
14176 return TLI->LowerCallTo(CLI).second;
14177}
14178
14180 assert(From && To && "Invalid SDNode; empty source SDValue?");
14181 auto I = SDEI.find(From);
14182 if (I == SDEI.end())
14183 return;
14184
14185 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14186 // the iterator, hence the need to make a copy to prevent a use-after-free.
14187 NodeExtraInfo NEI = I->second;
14188 if (LLVM_LIKELY(!NEI.PCSections)) {
14189 // No deep copy required for the types of extra info set.
14190 //
14191 // FIXME: Investigate if other types of extra info also need deep copy. This
14192 // depends on the types of nodes they can be attached to: if some extra info
14193 // is only ever attached to nodes where a replacement To node is always the
14194 // node where later use and propagation of the extra info has the intended
14195 // semantics, no deep copy is required.
14196 SDEI[To] = std::move(NEI);
14197 return;
14198 }
14199
14200 const SDNode *EntrySDN = getEntryNode().getNode();
14201
14202 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14203 // through the replacement of From with To. Otherwise, replacements of a node
14204 // (From) with more complex nodes (To and its operands) may result in lost
14205 // extra info where the root node (To) is insignificant in further propagating
14206 // and using extra info when further lowering to MIR.
14207 //
14208 // In the first step pre-populate the visited set with the nodes reachable
14209 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14210 // DAG that is not new and should be left untouched.
14211 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14212 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14213 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14214 if (MaxDepth == 0) {
14215 // Remember this node in case we need to increase MaxDepth and continue
14216 // populating FromReach from this node.
14217 Leafs.emplace_back(N);
14218 return;
14219 }
14220 if (!FromReach.insert(N).second)
14221 return;
14222 for (const SDValue &Op : N->op_values())
14223 Self(Self, Op.getNode(), MaxDepth - 1);
14224 };
14225
14226 // Copy extra info to To and all its transitive operands (that are new).
14228 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14229 if (FromReach.contains(N))
14230 return true;
14231 if (!Visited.insert(N).second)
14232 return true;
14233 if (EntrySDN == N)
14234 return false;
14235 for (const SDValue &Op : N->op_values()) {
14236 if (N == To && Op.getNode() == EntrySDN) {
14237 // Special case: New node's operand is the entry node; just need to
14238 // copy extra info to new node.
14239 break;
14240 }
14241 if (!Self(Self, Op.getNode()))
14242 return false;
14243 }
14244 // Copy only if entry node was not reached.
14245 SDEI[N] = NEI;
14246 return true;
14247 };
14248
14249 // We first try with a lower MaxDepth, assuming that the path to common
14250 // operands between From and To is relatively short. This significantly
14251 // improves performance in the common case. The initial MaxDepth is big
14252 // enough to avoid retry in the common case; the last MaxDepth is large
14253 // enough to avoid having to use the fallback below (and protects from
14254 // potential stack exhaustion from recursion).
14255 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14256 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14257 // StartFrom is the previous (or initial) set of leafs reachable at the
14258 // previous maximum depth.
14260 std::swap(StartFrom, Leafs);
14261 for (const SDNode *N : StartFrom)
14262 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14263 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14264 return;
14265 // This should happen very rarely (reached the entry node).
14266 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14267 assert(!Leafs.empty());
14268 }
14269
14270 // This should not happen - but if it did, that means the subgraph reachable
14271 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14272 // could not visit all reachable common operands. Consequently, we were able
14273 // to reach the entry node.
14274 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14275 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14276 // Best-effort fallback if assertions disabled.
14277 SDEI[To] = std::move(NEI);
14278}
14279
14280#ifndef NDEBUG
14281static void checkForCyclesHelper(const SDNode *N,
14284 const llvm::SelectionDAG *DAG) {
14285 // If this node has already been checked, don't check it again.
14286 if (Checked.count(N))
14287 return;
14288
14289 // If a node has already been visited on this depth-first walk, reject it as
14290 // a cycle.
14291 if (!Visited.insert(N).second) {
14292 errs() << "Detected cycle in SelectionDAG\n";
14293 dbgs() << "Offending node:\n";
14294 N->dumprFull(DAG); dbgs() << "\n";
14295 abort();
14296 }
14297
14298 for (const SDValue &Op : N->op_values())
14299 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14300
14301 Checked.insert(N);
14302 Visited.erase(N);
14303}
14304#endif
14305
14307 const llvm::SelectionDAG *DAG,
14308 bool force) {
14309#ifndef NDEBUG
14310 bool check = force;
14311#ifdef EXPENSIVE_CHECKS
14312 check = true;
14313#endif // EXPENSIVE_CHECKS
14314 if (check) {
14315 assert(N && "Checking nonexistent SDNode");
14318 checkForCyclesHelper(N, visited, checked, DAG);
14319 }
14320#endif // !NDEBUG
14321}
14322
14323void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14324 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14325}
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 getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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
@ DEACTIVATION_SYMBOL
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:289
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:515
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:259
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ GlobalAddress
Definition ISDOpcodes.h:88
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:868
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:577
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:744
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:898
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:521
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:832
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:712
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:662
@ TargetExternalSymbol
Definition ISDOpcodes.h:185
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:779
@ TargetJumpTable
Definition ISDOpcodes.h:183
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:193
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:815
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:347
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:688
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:534
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:369
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:784
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:228
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:242
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:669
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:225
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:343
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:180
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:701
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:762
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:642
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:607
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:569
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:219
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:838
@ TargetConstantFP
Definition ISDOpcodes.h:175
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:799
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ TargetFrameIndex
Definition ISDOpcodes.h:182
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:887
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:876
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:724
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:793
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:323
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:493
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:914
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:174
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:498
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:736
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:200
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:732
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:707
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:299
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:678
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:236
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:558
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ VECTOR_SPLICE
VECTOR_SPLICE(VEC1, VEC2, IMM) - Returns a subvector of the same type as VEC1/VEC2 from CONCAT_VECTOR...
Definition ISDOpcodes.h:654
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:947
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:696
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:909
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:933
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:821
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:719
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:333
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:181
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h: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)