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");
442 case ISD::VP_REDUCE_FADD:
443 case ISD::VP_REDUCE_SEQ_FADD:
444 return ISD::FADD;
447 case ISD::VP_REDUCE_FMUL:
448 case ISD::VP_REDUCE_SEQ_FMUL:
449 return ISD::FMUL;
451 case ISD::VP_REDUCE_ADD:
452 return ISD::ADD;
454 case ISD::VP_REDUCE_MUL:
455 return ISD::MUL;
457 case ISD::VP_REDUCE_AND:
458 return ISD::AND;
460 case ISD::VP_REDUCE_OR:
461 return ISD::OR;
463 case ISD::VP_REDUCE_XOR:
464 return ISD::XOR;
466 case ISD::VP_REDUCE_SMAX:
467 return ISD::SMAX;
469 case ISD::VP_REDUCE_SMIN:
470 return ISD::SMIN;
472 case ISD::VP_REDUCE_UMAX:
473 return ISD::UMAX;
475 case ISD::VP_REDUCE_UMIN:
476 return ISD::UMIN;
478 case ISD::VP_REDUCE_FMAX:
479 return ISD::FMAXNUM;
481 case ISD::VP_REDUCE_FMIN:
482 return ISD::FMINNUM;
484 case ISD::VP_REDUCE_FMAXIMUM:
485 return ISD::FMAXIMUM;
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;
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 }
919 case ISD::ATOMIC_SWAP:
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;
969 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
970 break;
971 } // end switch (N->getOpcode())
972
973 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
974 // to check.
975 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
976 ID.AddInteger(MN->getRawSubclassData());
977 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
978 ID.AddInteger(MN->getMemOperand()->getFlags());
979 ID.AddInteger(MN->getMemoryVT().getRawBits());
980 }
981}
982
983/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
984/// data.
985static void AddNodeIDNode(FoldingSetNodeID &ID, const SDNode *N) {
986 AddNodeIDOpcode(ID, N->getOpcode());
987 // Add the return value info.
988 AddNodeIDValueTypes(ID, N->getVTList());
989 // Add the operand info.
990 AddNodeIDOperands(ID, N->ops());
991
992 // Handle SDNode leafs with special info.
994}
995
996//===----------------------------------------------------------------------===//
997// SelectionDAG Class
998//===----------------------------------------------------------------------===//
999
1000/// doNotCSE - Return true if CSE should not be performed for this node.
1001static bool doNotCSE(SDNode *N) {
1002 if (N->getValueType(0) == MVT::Glue)
1003 return true; // Never CSE anything that produces a glue result.
1004
1005 switch (N->getOpcode()) {
1006 default: break;
1007 case ISD::HANDLENODE:
1008 case ISD::EH_LABEL:
1009 return true; // Never CSE these nodes.
1010 }
1011
1012 // Check that remaining values produced are not flags.
1013 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1014 if (N->getValueType(i) == MVT::Glue)
1015 return true; // Never CSE anything that produces a glue result.
1016
1017 return false;
1018}
1019
1020/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1021/// SelectionDAG.
1023 // Create a dummy node (which is not added to allnodes), that adds a reference
1024 // to the root node, preventing it from being deleted.
1025 HandleSDNode Dummy(getRoot());
1026
1027 SmallVector<SDNode*, 128> DeadNodes;
1028
1029 // Add all obviously-dead nodes to the DeadNodes worklist.
1030 for (SDNode &Node : allnodes())
1031 if (Node.use_empty())
1032 DeadNodes.push_back(&Node);
1033
1034 RemoveDeadNodes(DeadNodes);
1035
1036 // If the root changed (e.g. it was a dead load, update the root).
1037 setRoot(Dummy.getValue());
1038}
1039
1040/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1041/// given list, and any nodes that become unreachable as a result.
1043
1044 // Process the worklist, deleting the nodes and adding their uses to the
1045 // worklist.
1046 while (!DeadNodes.empty()) {
1047 SDNode *N = DeadNodes.pop_back_val();
1048 // Skip to next node if we've already managed to delete the node. This could
1049 // happen if replacing a node causes a node previously added to the node to
1050 // be deleted.
1051 if (N->getOpcode() == ISD::DELETED_NODE)
1052 continue;
1053
1054 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1055 DUL->NodeDeleted(N, nullptr);
1056
1057 // Take the node out of the appropriate CSE map.
1058 RemoveNodeFromCSEMaps(N);
1059
1060 // Next, brutally remove the operand list. This is safe to do, as there are
1061 // no cycles in the graph.
1062 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1063 SDUse &Use = *I++;
1064 SDNode *Operand = Use.getNode();
1065 Use.set(SDValue());
1066
1067 // Now that we removed this operand, see if there are no uses of it left.
1068 if (Operand->use_empty())
1069 DeadNodes.push_back(Operand);
1070 }
1071
1072 DeallocateNode(N);
1073 }
1074}
1075
1077 SmallVector<SDNode*, 16> DeadNodes(1, N);
1078
1079 // Create a dummy node that adds a reference to the root node, preventing
1080 // it from being deleted. (This matters if the root is an operand of the
1081 // dead node.)
1082 HandleSDNode Dummy(getRoot());
1083
1084 RemoveDeadNodes(DeadNodes);
1085}
1086
1088 // First take this out of the appropriate CSE map.
1089 RemoveNodeFromCSEMaps(N);
1090
1091 // Finally, remove uses due to operands of this node, remove from the
1092 // AllNodes list, and delete the node.
1093 DeleteNodeNotInCSEMaps(N);
1094}
1095
1096void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1097 assert(N->getIterator() != AllNodes.begin() &&
1098 "Cannot delete the entry node!");
1099 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1100
1101 // Drop all of the operands and decrement used node's use counts.
1102 N->DropOperands();
1103
1104 DeallocateNode(N);
1105}
1106
1107void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1108 assert(!(V->isVariadic() && isParameter));
1109 if (isParameter)
1110 ByvalParmDbgValues.push_back(V);
1111 else
1112 DbgValues.push_back(V);
1113 for (const SDNode *Node : V->getSDNodes())
1114 if (Node)
1115 DbgValMap[Node].push_back(V);
1116}
1117
1119 DbgValMapType::iterator I = DbgValMap.find(Node);
1120 if (I == DbgValMap.end())
1121 return;
1122 for (auto &Val: I->second)
1123 Val->setIsInvalidated();
1124 DbgValMap.erase(I);
1125}
1126
1127void SelectionDAG::DeallocateNode(SDNode *N) {
1128 // If we have operands, deallocate them.
1130
1131 NodeAllocator.Deallocate(AllNodes.remove(N));
1132
1133 // Set the opcode to DELETED_NODE to help catch bugs when node
1134 // memory is reallocated.
1135 // FIXME: There are places in SDag that have grown a dependency on the opcode
1136 // value in the released node.
1137 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1138 N->NodeType = ISD::DELETED_NODE;
1139
1140 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1141 // them and forget about that node.
1142 DbgInfo->erase(N);
1143
1144 // Invalidate extra info.
1145 SDEI.erase(N);
1146}
1147
1148#ifndef NDEBUG
1149/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1150void SelectionDAG::verifyNode(SDNode *N) const {
1151 switch (N->getOpcode()) {
1152 default:
1153 if (N->isTargetOpcode())
1155 break;
1156 case ISD::BUILD_PAIR: {
1157 EVT VT = N->getValueType(0);
1158 assert(N->getNumValues() == 1 && "Too many results!");
1159 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1160 "Wrong return type!");
1161 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1162 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1163 "Mismatched operand types!");
1164 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1165 "Wrong operand type!");
1166 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1167 "Wrong return type size");
1168 break;
1169 }
1170 case ISD::BUILD_VECTOR: {
1171 assert(N->getNumValues() == 1 && "Too many results!");
1172 assert(N->getValueType(0).isVector() && "Wrong return type!");
1173 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1174 "Wrong number of operands!");
1175 EVT EltVT = N->getValueType(0).getVectorElementType();
1176 for (const SDUse &Op : N->ops()) {
1177 assert((Op.getValueType() == EltVT ||
1178 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1179 EltVT.bitsLE(Op.getValueType()))) &&
1180 "Wrong operand type!");
1181 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1182 "Operands must all have the same type");
1183 }
1184 break;
1185 }
1186 }
1187}
1188#endif // NDEBUG
1189
1190/// Insert a newly allocated node into the DAG.
1191///
1192/// Handles insertion into the all nodes list and CSE map, as well as
1193/// verification and other common operations when a new node is allocated.
1194void SelectionDAG::InsertNode(SDNode *N) {
1195 AllNodes.push_back(N);
1196#ifndef NDEBUG
1197 N->PersistentId = NextPersistentId++;
1198 verifyNode(N);
1199#endif
1200 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1201 DUL->NodeInserted(N);
1202}
1203
1204/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1205/// correspond to it. This is useful when we're about to delete or repurpose
1206/// the node. We don't want future request for structurally identical nodes
1207/// to return N anymore.
1208bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1209 bool Erased = false;
1210 switch (N->getOpcode()) {
1211 case ISD::HANDLENODE: return false; // noop.
1212 case ISD::CONDCODE:
1213 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1214 "Cond code doesn't exist!");
1215 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1216 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1217 break;
1219 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1220 break;
1222 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1223 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1224 ESN->getSymbol(), ESN->getTargetFlags()));
1225 break;
1226 }
1227 case ISD::MCSymbol: {
1228 auto *MCSN = cast<MCSymbolSDNode>(N);
1229 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1230 break;
1231 }
1232 case ISD::VALUETYPE: {
1233 EVT VT = cast<VTSDNode>(N)->getVT();
1234 if (VT.isExtended()) {
1235 Erased = ExtendedValueTypeNodes.erase(VT);
1236 } else {
1237 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1238 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1239 }
1240 break;
1241 }
1242 default:
1243 // Remove it from the CSE Map.
1244 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1245 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1246 Erased = CSEMap.RemoveNode(N);
1247 break;
1248 }
1249#ifndef NDEBUG
1250 // Verify that the node was actually in one of the CSE maps, unless it has a
1251 // glue result (which cannot be CSE'd) or is one of the special cases that are
1252 // not subject to CSE.
1253 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1254 !N->isMachineOpcode() && !doNotCSE(N)) {
1255 N->dump(this);
1256 dbgs() << "\n";
1257 llvm_unreachable("Node is not in map!");
1258 }
1259#endif
1260 return Erased;
1261}
1262
1263/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1264/// maps and modified in place. Add it back to the CSE maps, unless an identical
1265/// node already exists, in which case transfer all its users to the existing
1266/// node. This transfer can potentially trigger recursive merging.
1267void
1268SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1269 // For node types that aren't CSE'd, just act as if no identical node
1270 // already exists.
1271 if (!doNotCSE(N)) {
1272 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1273 if (Existing != N) {
1274 // If there was already an existing matching node, use ReplaceAllUsesWith
1275 // to replace the dead one with the existing one. This can cause
1276 // recursive merging of other unrelated nodes down the line.
1277 Existing->intersectFlagsWith(N->getFlags());
1278 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1279 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1280 ReplaceAllUsesWith(N, Existing);
1281
1282 // N is now dead. Inform the listeners and delete it.
1283 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1284 DUL->NodeDeleted(N, Existing);
1285 DeleteNodeNotInCSEMaps(N);
1286 return;
1287 }
1288 }
1289
1290 // If the node doesn't already exist, we updated it. Inform listeners.
1291 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1292 DUL->NodeUpdated(N);
1293}
1294
1295/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1296/// were replaced with those specified. If this node is never memoized,
1297/// return null, otherwise return a pointer to the slot it would take. If a
1298/// node already exists with these operands, the slot will be non-null.
1299SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1300 void *&InsertPos) {
1301 if (doNotCSE(N))
1302 return nullptr;
1303
1304 SDValue Ops[] = { Op };
1305 FoldingSetNodeID ID;
1306 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1308 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1309 if (Node)
1310 Node->intersectFlagsWith(N->getFlags());
1311 return Node;
1312}
1313
1314/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1315/// were replaced with those specified. If this node is never memoized,
1316/// return null, otherwise return a pointer to the slot it would take. If a
1317/// node already exists with these operands, the slot will be non-null.
1318SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1319 SDValue Op1, SDValue Op2,
1320 void *&InsertPos) {
1321 if (doNotCSE(N))
1322 return nullptr;
1323
1324 SDValue Ops[] = { Op1, Op2 };
1325 FoldingSetNodeID ID;
1326 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1328 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1329 if (Node)
1330 Node->intersectFlagsWith(N->getFlags());
1331 return Node;
1332}
1333
1334/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1335/// were replaced with those specified. If this node is never memoized,
1336/// return null, otherwise return a pointer to the slot it would take. If a
1337/// node already exists with these operands, the slot will be non-null.
1338SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1339 void *&InsertPos) {
1340 if (doNotCSE(N))
1341 return nullptr;
1342
1343 FoldingSetNodeID ID;
1344 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1346 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1347 if (Node)
1348 Node->intersectFlagsWith(N->getFlags());
1349 return Node;
1350}
1351
1353 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1354 : VT.getTypeForEVT(*getContext());
1355
1356 return getDataLayout().getABITypeAlign(Ty);
1357}
1358
1359// EntryNode could meaningfully have debug info if we can find it...
1361 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1362 getVTList(MVT::Other, MVT::Glue)),
1363 Root(getEntryNode()) {
1364 InsertNode(&EntryNode);
1365 DbgInfo = new SDDbgInfo();
1366}
1367
1369 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1370 const TargetLibraryInfo *LibraryInfo,
1371 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1373 FunctionVarLocs const *VarLocs) {
1374 MF = &NewMF;
1375 SDAGISelPass = PassPtr;
1376 ORE = &NewORE;
1379 LibInfo = LibraryInfo;
1380 Context = &MF->getFunction().getContext();
1381 UA = NewUA;
1382 PSI = PSIin;
1383 BFI = BFIin;
1384 MMI = &MMIin;
1385 FnVarLocs = VarLocs;
1386}
1387
1389 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1390 allnodes_clear();
1391 OperandRecycler.clear(OperandAllocator);
1392 delete DbgInfo;
1393}
1394
1396 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1397}
1398
1399void SelectionDAG::allnodes_clear() {
1400 assert(&*AllNodes.begin() == &EntryNode);
1401 AllNodes.remove(AllNodes.begin());
1402 while (!AllNodes.empty())
1403 DeallocateNode(&AllNodes.front());
1404#ifndef NDEBUG
1405 NextPersistentId = 0;
1406#endif
1407}
1408
1409SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1410 void *&InsertPos) {
1411 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1412 if (N) {
1413 switch (N->getOpcode()) {
1414 default: break;
1415 case ISD::Constant:
1416 case ISD::ConstantFP:
1417 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1418 "debug location. Use another overload.");
1419 }
1420 }
1421 return N;
1422}
1423
1424SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1425 const SDLoc &DL, void *&InsertPos) {
1426 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1427 if (N) {
1428 switch (N->getOpcode()) {
1429 case ISD::Constant:
1430 case ISD::ConstantFP:
1431 // Erase debug location from the node if the node is used at several
1432 // different places. Do not propagate one location to all uses as it
1433 // will cause a worse single stepping debugging experience.
1434 if (N->getDebugLoc() != DL.getDebugLoc())
1435 N->setDebugLoc(DebugLoc());
1436 break;
1437 default:
1438 // When the node's point of use is located earlier in the instruction
1439 // sequence than its prior point of use, update its debug info to the
1440 // earlier location.
1441 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1442 N->setDebugLoc(DL.getDebugLoc());
1443 break;
1444 }
1445 }
1446 return N;
1447}
1448
1450 allnodes_clear();
1451 OperandRecycler.clear(OperandAllocator);
1452 OperandAllocator.Reset();
1453 CSEMap.clear();
1454
1455 ExtendedValueTypeNodes.clear();
1456 ExternalSymbols.clear();
1457 TargetExternalSymbols.clear();
1458 MCSymbols.clear();
1459 SDEI.clear();
1460 llvm::fill(CondCodeNodes, nullptr);
1461 llvm::fill(ValueTypeNodes, nullptr);
1462
1463 EntryNode.UseList = nullptr;
1464 InsertNode(&EntryNode);
1465 Root = getEntryNode();
1466 DbgInfo->clear();
1467}
1468
1470 return VT.bitsGT(Op.getValueType())
1471 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1472 : getNode(ISD::FP_ROUND, DL, VT, Op,
1473 getIntPtrConstant(0, DL, /*isTarget=*/true));
1474}
1475
1476std::pair<SDValue, SDValue>
1478 const SDLoc &DL, EVT VT) {
1479 assert(!VT.bitsEq(Op.getValueType()) &&
1480 "Strict no-op FP extend/round not allowed.");
1481 SDValue Res =
1482 VT.bitsGT(Op.getValueType())
1483 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1484 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1485 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1486
1487 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1488}
1489
1491 return VT.bitsGT(Op.getValueType()) ?
1492 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1493 getNode(ISD::TRUNCATE, DL, VT, Op);
1494}
1495
1497 return VT.bitsGT(Op.getValueType()) ?
1498 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1499 getNode(ISD::TRUNCATE, DL, VT, Op);
1500}
1501
1503 return VT.bitsGT(Op.getValueType()) ?
1504 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1505 getNode(ISD::TRUNCATE, DL, VT, Op);
1506}
1507
1509 EVT VT) {
1510 assert(!VT.isVector());
1511 auto Type = Op.getValueType();
1512 SDValue DestOp;
1513 if (Type == VT)
1514 return Op;
1515 auto Size = Op.getValueSizeInBits();
1516 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1517 if (DestOp.getValueType() == VT)
1518 return DestOp;
1519
1520 return getAnyExtOrTrunc(DestOp, DL, VT);
1521}
1522
1524 EVT VT) {
1525 assert(!VT.isVector());
1526 auto Type = Op.getValueType();
1527 SDValue DestOp;
1528 if (Type == VT)
1529 return Op;
1530 auto Size = Op.getValueSizeInBits();
1531 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1532 if (DestOp.getValueType() == VT)
1533 return DestOp;
1534
1535 return getSExtOrTrunc(DestOp, DL, VT);
1536}
1537
1539 EVT VT) {
1540 assert(!VT.isVector());
1541 auto Type = Op.getValueType();
1542 SDValue DestOp;
1543 if (Type == VT)
1544 return Op;
1545 auto Size = Op.getValueSizeInBits();
1546 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1547 if (DestOp.getValueType() == VT)
1548 return DestOp;
1549
1550 return getZExtOrTrunc(DestOp, DL, VT);
1551}
1552
1554 EVT OpVT) {
1555 if (VT.bitsLE(Op.getValueType()))
1556 return getNode(ISD::TRUNCATE, SL, VT, Op);
1557
1558 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1559 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1560}
1561
1563 EVT OpVT = Op.getValueType();
1564 assert(VT.isInteger() && OpVT.isInteger() &&
1565 "Cannot getZeroExtendInReg FP types");
1566 assert(VT.isVector() == OpVT.isVector() &&
1567 "getZeroExtendInReg type should be vector iff the operand "
1568 "type is vector!");
1569 assert((!VT.isVector() ||
1571 "Vector element counts must match in getZeroExtendInReg");
1572 assert(VT.bitsLE(OpVT) && "Not extending!");
1573 if (OpVT == VT)
1574 return Op;
1576 VT.getScalarSizeInBits());
1577 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1578}
1579
1581 SDValue EVL, const SDLoc &DL,
1582 EVT VT) {
1583 EVT OpVT = Op.getValueType();
1584 assert(VT.isInteger() && OpVT.isInteger() &&
1585 "Cannot getVPZeroExtendInReg FP types");
1586 assert(VT.isVector() && OpVT.isVector() &&
1587 "getVPZeroExtendInReg type and operand type should be vector!");
1589 "Vector element counts must match in getZeroExtendInReg");
1590 assert(VT.bitsLE(OpVT) && "Not extending!");
1591 if (OpVT == VT)
1592 return Op;
1594 VT.getScalarSizeInBits());
1595 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1596 EVL);
1597}
1598
1600 // Only unsigned pointer semantics are supported right now. In the future this
1601 // might delegate to TLI to check pointer signedness.
1602 return getZExtOrTrunc(Op, DL, VT);
1603}
1604
1606 // Only unsigned pointer semantics are supported right now. In the future this
1607 // might delegate to TLI to check pointer signedness.
1608 return getZeroExtendInReg(Op, DL, VT);
1609}
1610
1612 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1613}
1614
1615/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1617 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1618}
1619
1621 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1622 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1623}
1624
1626 SDValue Mask, SDValue EVL, EVT VT) {
1627 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1628 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1629}
1630
1632 SDValue Mask, SDValue EVL) {
1633 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1634}
1635
1637 SDValue Mask, SDValue EVL) {
1638 if (VT.bitsGT(Op.getValueType()))
1639 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1640 if (VT.bitsLT(Op.getValueType()))
1641 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1642 return Op;
1643}
1644
1646 EVT OpVT) {
1647 if (!V)
1648 return getConstant(0, DL, VT);
1649
1650 switch (TLI->getBooleanContents(OpVT)) {
1653 return getConstant(1, DL, VT);
1655 return getAllOnesConstant(DL, VT);
1656 }
1657 llvm_unreachable("Unexpected boolean content enum!");
1658}
1659
1661 bool isT, bool isO) {
1662 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1663 DL, VT, isT, isO);
1664}
1665
1667 bool isT, bool isO) {
1668 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1669}
1670
1672 EVT VT, bool isT, bool isO) {
1673 assert(VT.isInteger() && "Cannot create FP integer constant!");
1674
1675 EVT EltVT = VT.getScalarType();
1676 const ConstantInt *Elt = &Val;
1677
1678 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1679 // to-be-splatted scalar ConstantInt.
1680 if (isa<VectorType>(Elt->getType()))
1681 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1682
1683 // In some cases the vector type is legal but the element type is illegal and
1684 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1685 // inserted value (the type does not need to match the vector element type).
1686 // Any extra bits introduced will be truncated away.
1687 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1689 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1690 APInt NewVal;
1691 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1692 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1693 else
1694 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1695 Elt = ConstantInt::get(*getContext(), NewVal);
1696 }
1697 // In other cases the element type is illegal and needs to be expanded, for
1698 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1699 // the value into n parts and use a vector type with n-times the elements.
1700 // Then bitcast to the type requested.
1701 // Legalizing constants too early makes the DAGCombiner's job harder so we
1702 // only legalize if the DAG tells us we must produce legal types.
1703 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1704 TLI->getTypeAction(*getContext(), EltVT) ==
1706 const APInt &NewVal = Elt->getValue();
1707 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1708 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1709
1710 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1711 if (VT.isScalableVector() ||
1712 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1713 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1714 "Can only handle an even split!");
1715 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1716
1717 SmallVector<SDValue, 2> ScalarParts;
1718 for (unsigned i = 0; i != Parts; ++i)
1719 ScalarParts.push_back(getConstant(
1720 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1721 ViaEltVT, isT, isO));
1722
1723 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1724 }
1725
1726 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1727 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1728
1729 // Check the temporary vector is the correct size. If this fails then
1730 // getTypeToTransformTo() probably returned a type whose size (in bits)
1731 // isn't a power-of-2 factor of the requested type size.
1732 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1733
1734 SmallVector<SDValue, 2> EltParts;
1735 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1736 EltParts.push_back(getConstant(
1737 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1738 ViaEltVT, isT, isO));
1739
1740 // EltParts is currently in little endian order. If we actually want
1741 // big-endian order then reverse it now.
1742 if (getDataLayout().isBigEndian())
1743 std::reverse(EltParts.begin(), EltParts.end());
1744
1745 // The elements must be reversed when the element order is different
1746 // to the endianness of the elements (because the BITCAST is itself a
1747 // vector shuffle in this situation). However, we do not need any code to
1748 // perform this reversal because getConstant() is producing a vector
1749 // splat.
1750 // This situation occurs in MIPS MSA.
1751
1753 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1754 llvm::append_range(Ops, EltParts);
1755
1756 SDValue V =
1757 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1758 return V;
1759 }
1760
1761 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1762 "APInt size does not match type size!");
1763 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1764 SDVTList VTs = getVTList(EltVT);
1766 AddNodeIDNode(ID, Opc, VTs, {});
1767 ID.AddPointer(Elt);
1768 ID.AddBoolean(isO);
1769 void *IP = nullptr;
1770 SDNode *N = nullptr;
1771 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1772 if (!VT.isVector())
1773 return SDValue(N, 0);
1774
1775 if (!N) {
1776 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1777 CSEMap.InsertNode(N, IP);
1778 InsertNode(N);
1779 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1780 }
1781
1782 SDValue Result(N, 0);
1783 if (VT.isVector())
1784 Result = getSplat(VT, DL, Result);
1785 return Result;
1786}
1787
1789 bool isT, bool isO) {
1790 unsigned Size = VT.getScalarSizeInBits();
1791 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1792}
1793
1795 bool IsOpaque) {
1797 IsTarget, IsOpaque);
1798}
1799
1801 bool isTarget) {
1802 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1803}
1804
1806 const SDLoc &DL) {
1807 assert(VT.isInteger() && "Shift amount is not an integer type!");
1808 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1809 return getConstant(Val, DL, ShiftVT);
1810}
1811
1813 const SDLoc &DL) {
1814 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1815 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1816}
1817
1819 bool isTarget) {
1820 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1821}
1822
1824 bool isTarget) {
1825 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1826}
1827
1829 EVT VT, bool isTarget) {
1830 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1831
1832 EVT EltVT = VT.getScalarType();
1833 const ConstantFP *Elt = &V;
1834
1835 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1836 // the to-be-splatted scalar ConstantFP.
1837 if (isa<VectorType>(Elt->getType()))
1838 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1839
1840 // Do the map lookup using the actual bit pattern for the floating point
1841 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1842 // we don't have issues with SNANs.
1843 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1844 SDVTList VTs = getVTList(EltVT);
1846 AddNodeIDNode(ID, Opc, VTs, {});
1847 ID.AddPointer(Elt);
1848 void *IP = nullptr;
1849 SDNode *N = nullptr;
1850 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1851 if (!VT.isVector())
1852 return SDValue(N, 0);
1853
1854 if (!N) {
1855 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1856 CSEMap.InsertNode(N, IP);
1857 InsertNode(N);
1858 }
1859
1860 SDValue Result(N, 0);
1861 if (VT.isVector())
1862 Result = getSplat(VT, DL, Result);
1863 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1864 return Result;
1865}
1866
1868 bool isTarget) {
1869 EVT EltVT = VT.getScalarType();
1870 if (EltVT == MVT::f32)
1871 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1872 if (EltVT == MVT::f64)
1873 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1874 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1875 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1876 bool Ignored;
1877 APFloat APF = APFloat(Val);
1879 &Ignored);
1880 return getConstantFP(APF, DL, VT, isTarget);
1881 }
1882 llvm_unreachable("Unsupported type in getConstantFP");
1883}
1884
1886 EVT VT, int64_t Offset, bool isTargetGA,
1887 unsigned TargetFlags) {
1888 assert((TargetFlags == 0 || isTargetGA) &&
1889 "Cannot set target flags on target-independent globals");
1890
1891 // Truncate (with sign-extension) the offset value to the pointer size.
1893 if (BitWidth < 64)
1895
1896 unsigned Opc;
1897 if (GV->isThreadLocal())
1899 else
1901
1902 SDVTList VTs = getVTList(VT);
1904 AddNodeIDNode(ID, Opc, VTs, {});
1905 ID.AddPointer(GV);
1906 ID.AddInteger(Offset);
1907 ID.AddInteger(TargetFlags);
1908 void *IP = nullptr;
1909 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1910 return SDValue(E, 0);
1911
1912 auto *N = newSDNode<GlobalAddressSDNode>(
1913 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1914 CSEMap.InsertNode(N, IP);
1915 InsertNode(N);
1916 return SDValue(N, 0);
1917}
1918
1920 SDVTList VTs = getVTList(MVT::Untyped);
1923 ID.AddPointer(GV);
1924 void *IP = nullptr;
1925 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1926 return SDValue(E, 0);
1927
1928 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1929 CSEMap.InsertNode(N, IP);
1930 InsertNode(N);
1931 return SDValue(N, 0);
1932}
1933
1934SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1935 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1936 SDVTList VTs = getVTList(VT);
1938 AddNodeIDNode(ID, Opc, VTs, {});
1939 ID.AddInteger(FI);
1940 void *IP = nullptr;
1941 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1942 return SDValue(E, 0);
1943
1944 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1945 CSEMap.InsertNode(N, IP);
1946 InsertNode(N);
1947 return SDValue(N, 0);
1948}
1949
1950SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1951 unsigned TargetFlags) {
1952 assert((TargetFlags == 0 || isTarget) &&
1953 "Cannot set target flags on target-independent jump tables");
1954 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1955 SDVTList VTs = getVTList(VT);
1957 AddNodeIDNode(ID, Opc, VTs, {});
1958 ID.AddInteger(JTI);
1959 ID.AddInteger(TargetFlags);
1960 void *IP = nullptr;
1961 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1962 return SDValue(E, 0);
1963
1964 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1965 CSEMap.InsertNode(N, IP);
1966 InsertNode(N);
1967 return SDValue(N, 0);
1968}
1969
1971 const SDLoc &DL) {
1973 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Glue, Chain,
1974 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1975}
1976
1978 MaybeAlign Alignment, int Offset,
1979 bool isTarget, unsigned TargetFlags) {
1980 assert((TargetFlags == 0 || isTarget) &&
1981 "Cannot set target flags on target-independent globals");
1982 if (!Alignment)
1983 Alignment = shouldOptForSize()
1984 ? getDataLayout().getABITypeAlign(C->getType())
1985 : getDataLayout().getPrefTypeAlign(C->getType());
1986 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
1987 SDVTList VTs = getVTList(VT);
1989 AddNodeIDNode(ID, Opc, VTs, {});
1990 ID.AddInteger(Alignment->value());
1991 ID.AddInteger(Offset);
1992 ID.AddPointer(C);
1993 ID.AddInteger(TargetFlags);
1994 void *IP = nullptr;
1995 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1996 return SDValue(E, 0);
1997
1998 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
1999 TargetFlags);
2000 CSEMap.InsertNode(N, IP);
2001 InsertNode(N);
2002 SDValue V = SDValue(N, 0);
2003 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2004 return V;
2005}
2006
2008 MaybeAlign Alignment, int Offset,
2009 bool isTarget, unsigned TargetFlags) {
2010 assert((TargetFlags == 0 || isTarget) &&
2011 "Cannot set target flags on target-independent globals");
2012 if (!Alignment)
2013 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2014 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2015 SDVTList VTs = getVTList(VT);
2017 AddNodeIDNode(ID, Opc, VTs, {});
2018 ID.AddInteger(Alignment->value());
2019 ID.AddInteger(Offset);
2020 C->addSelectionDAGCSEId(ID);
2021 ID.AddInteger(TargetFlags);
2022 void *IP = nullptr;
2023 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2024 return SDValue(E, 0);
2025
2026 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2027 TargetFlags);
2028 CSEMap.InsertNode(N, IP);
2029 InsertNode(N);
2030 return SDValue(N, 0);
2031}
2032
2035 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2036 ID.AddPointer(MBB);
2037 void *IP = nullptr;
2038 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2039 return SDValue(E, 0);
2040
2041 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2042 CSEMap.InsertNode(N, IP);
2043 InsertNode(N);
2044 return SDValue(N, 0);
2045}
2046
2048 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2049 ValueTypeNodes.size())
2050 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2051
2052 SDNode *&N = VT.isExtended() ?
2053 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2054
2055 if (N) return SDValue(N, 0);
2056 N = newSDNode<VTSDNode>(VT);
2057 InsertNode(N);
2058 return SDValue(N, 0);
2059}
2060
2062 SDNode *&N = ExternalSymbols[Sym];
2063 if (N) return SDValue(N, 0);
2064 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2065 InsertNode(N);
2066 return SDValue(N, 0);
2067}
2068
2069SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2070 StringRef SymName = TLI->getLibcallImplName(Libcall);
2071 return getExternalSymbol(SymName.data(), VT);
2072}
2073
2075 SDNode *&N = MCSymbols[Sym];
2076 if (N)
2077 return SDValue(N, 0);
2078 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2079 InsertNode(N);
2080 return SDValue(N, 0);
2081}
2082
2084 unsigned TargetFlags) {
2085 SDNode *&N =
2086 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2087 if (N) return SDValue(N, 0);
2088 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2089 InsertNode(N);
2090 return SDValue(N, 0);
2091}
2092
2094 EVT VT, unsigned TargetFlags) {
2095 StringRef SymName = TLI->getLibcallImplName(Libcall);
2096 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2097}
2098
2100 if ((unsigned)Cond >= CondCodeNodes.size())
2101 CondCodeNodes.resize(Cond+1);
2102
2103 if (!CondCodeNodes[Cond]) {
2104 auto *N = newSDNode<CondCodeSDNode>(Cond);
2105 CondCodeNodes[Cond] = N;
2106 InsertNode(N);
2107 }
2108
2109 return SDValue(CondCodeNodes[Cond], 0);
2110}
2111
2113 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2114 "APInt size does not match type size!");
2115
2116 if (MulImm == 0)
2117 return getConstant(0, DL, VT);
2118
2119 const MachineFunction &MF = getMachineFunction();
2120 const Function &F = MF.getFunction();
2121 ConstantRange CR = getVScaleRange(&F, 64);
2122 if (const APInt *C = CR.getSingleElement())
2123 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2124
2125 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2126}
2127
2128/// \returns a value of type \p VT that represents the runtime value of \p
2129/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2130/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2131/// or TypeSize.
2132template <typename Ty>
2134 EVT VT, Ty Quantity) {
2135 if (Quantity.isScalable())
2136 return DAG.getVScale(
2137 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2138
2139 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2140}
2141
2143 ElementCount EC) {
2144 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2145}
2146
2148 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2149}
2150
2152 ElementCount EC) {
2153 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2154 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2155 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2156 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2157}
2158
2160 APInt One(ResVT.getScalarSizeInBits(), 1);
2161 return getStepVector(DL, ResVT, One);
2162}
2163
2165 const APInt &StepVal) {
2166 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2167 if (ResVT.isScalableVector())
2168 return getNode(
2169 ISD::STEP_VECTOR, DL, ResVT,
2170 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2171
2172 SmallVector<SDValue, 16> OpsStepConstants;
2173 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2174 OpsStepConstants.push_back(
2175 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2176 return getBuildVector(ResVT, DL, OpsStepConstants);
2177}
2178
2179/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2180/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2185
2187 SDValue N2, ArrayRef<int> Mask) {
2188 assert(VT.getVectorNumElements() == Mask.size() &&
2189 "Must have the same number of vector elements as mask elements!");
2190 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2191 "Invalid VECTOR_SHUFFLE");
2192
2193 // Canonicalize shuffle undef, undef -> undef
2194 if (N1.isUndef() && N2.isUndef())
2195 return getUNDEF(VT);
2196
2197 // Validate that all indices in Mask are within the range of the elements
2198 // input to the shuffle.
2199 int NElts = Mask.size();
2200 assert(llvm::all_of(Mask,
2201 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2202 "Index out of range");
2203
2204 // Copy the mask so we can do any needed cleanup.
2205 SmallVector<int, 8> MaskVec(Mask);
2206
2207 // Canonicalize shuffle v, v -> v, undef
2208 if (N1 == N2) {
2209 N2 = getUNDEF(VT);
2210 for (int i = 0; i != NElts; ++i)
2211 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2212 }
2213
2214 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2215 if (N1.isUndef())
2216 commuteShuffle(N1, N2, MaskVec);
2217
2218 if (TLI->hasVectorBlend()) {
2219 // If shuffling a splat, try to blend the splat instead. We do this here so
2220 // that even when this arises during lowering we don't have to re-handle it.
2221 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2222 BitVector UndefElements;
2223 SDValue Splat = BV->getSplatValue(&UndefElements);
2224 if (!Splat)
2225 return;
2226
2227 for (int i = 0; i < NElts; ++i) {
2228 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2229 continue;
2230
2231 // If this input comes from undef, mark it as such.
2232 if (UndefElements[MaskVec[i] - Offset]) {
2233 MaskVec[i] = -1;
2234 continue;
2235 }
2236
2237 // If we can blend a non-undef lane, use that instead.
2238 if (!UndefElements[i])
2239 MaskVec[i] = i + Offset;
2240 }
2241 };
2242 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2243 BlendSplat(N1BV, 0);
2244 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2245 BlendSplat(N2BV, NElts);
2246 }
2247
2248 // Canonicalize all index into lhs, -> shuffle lhs, undef
2249 // Canonicalize all index into rhs, -> shuffle rhs, undef
2250 bool AllLHS = true, AllRHS = true;
2251 bool N2Undef = N2.isUndef();
2252 for (int i = 0; i != NElts; ++i) {
2253 if (MaskVec[i] >= NElts) {
2254 if (N2Undef)
2255 MaskVec[i] = -1;
2256 else
2257 AllLHS = false;
2258 } else if (MaskVec[i] >= 0) {
2259 AllRHS = false;
2260 }
2261 }
2262 if (AllLHS && AllRHS)
2263 return getUNDEF(VT);
2264 if (AllLHS && !N2Undef)
2265 N2 = getUNDEF(VT);
2266 if (AllRHS) {
2267 N1 = getUNDEF(VT);
2268 commuteShuffle(N1, N2, MaskVec);
2269 }
2270 // Reset our undef status after accounting for the mask.
2271 N2Undef = N2.isUndef();
2272 // Re-check whether both sides ended up undef.
2273 if (N1.isUndef() && N2Undef)
2274 return getUNDEF(VT);
2275
2276 // If Identity shuffle return that node.
2277 bool Identity = true, AllSame = true;
2278 for (int i = 0; i != NElts; ++i) {
2279 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2280 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2281 }
2282 if (Identity && NElts)
2283 return N1;
2284
2285 // Shuffling a constant splat doesn't change the result.
2286 if (N2Undef) {
2287 SDValue V = N1;
2288
2289 // Look through any bitcasts. We check that these don't change the number
2290 // (and size) of elements and just changes their types.
2291 while (V.getOpcode() == ISD::BITCAST)
2292 V = V->getOperand(0);
2293
2294 // A splat should always show up as a build vector node.
2295 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2296 BitVector UndefElements;
2297 SDValue Splat = BV->getSplatValue(&UndefElements);
2298 // If this is a splat of an undef, shuffling it is also undef.
2299 if (Splat && Splat.isUndef())
2300 return getUNDEF(VT);
2301
2302 bool SameNumElts =
2303 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2304
2305 // We only have a splat which can skip shuffles if there is a splatted
2306 // value and no undef lanes rearranged by the shuffle.
2307 if (Splat && UndefElements.none()) {
2308 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2309 // number of elements match or the value splatted is a zero constant.
2310 if (SameNumElts || isNullConstant(Splat))
2311 return N1;
2312 }
2313
2314 // If the shuffle itself creates a splat, build the vector directly.
2315 if (AllSame && SameNumElts) {
2316 EVT BuildVT = BV->getValueType(0);
2317 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2318 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2319
2320 // We may have jumped through bitcasts, so the type of the
2321 // BUILD_VECTOR may not match the type of the shuffle.
2322 if (BuildVT != VT)
2323 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2324 return NewBV;
2325 }
2326 }
2327 }
2328
2329 SDVTList VTs = getVTList(VT);
2331 SDValue Ops[2] = { N1, N2 };
2333 for (int i = 0; i != NElts; ++i)
2334 ID.AddInteger(MaskVec[i]);
2335
2336 void* IP = nullptr;
2337 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2338 return SDValue(E, 0);
2339
2340 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2341 // SDNode doesn't have access to it. This memory will be "leaked" when
2342 // the node is deallocated, but recovered when the NodeAllocator is released.
2343 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2344 llvm::copy(MaskVec, MaskAlloc);
2345
2346 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2347 dl.getDebugLoc(), MaskAlloc);
2348 createOperands(N, Ops);
2349
2350 CSEMap.InsertNode(N, IP);
2351 InsertNode(N);
2352 SDValue V = SDValue(N, 0);
2353 NewSDValueDbgMsg(V, "Creating new node: ", this);
2354 return V;
2355}
2356
2358 EVT VT = SV.getValueType(0);
2359 SmallVector<int, 8> MaskVec(SV.getMask());
2361
2362 SDValue Op0 = SV.getOperand(0);
2363 SDValue Op1 = SV.getOperand(1);
2364 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2365}
2366
2368 SDVTList VTs = getVTList(VT);
2370 AddNodeIDNode(ID, ISD::Register, VTs, {});
2371 ID.AddInteger(Reg.id());
2372 void *IP = nullptr;
2373 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2374 return SDValue(E, 0);
2375
2376 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2377 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2378 CSEMap.InsertNode(N, IP);
2379 InsertNode(N);
2380 return SDValue(N, 0);
2381}
2382
2385 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2386 ID.AddPointer(RegMask);
2387 void *IP = nullptr;
2388 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2389 return SDValue(E, 0);
2390
2391 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2392 CSEMap.InsertNode(N, IP);
2393 InsertNode(N);
2394 return SDValue(N, 0);
2395}
2396
2398 MCSymbol *Label) {
2399 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2400}
2401
2402SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2403 SDValue Root, MCSymbol *Label) {
2405 SDValue Ops[] = { Root };
2406 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2407 ID.AddPointer(Label);
2408 void *IP = nullptr;
2409 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2410 return SDValue(E, 0);
2411
2412 auto *N =
2413 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2414 createOperands(N, Ops);
2415
2416 CSEMap.InsertNode(N, IP);
2417 InsertNode(N);
2418 return SDValue(N, 0);
2419}
2420
2422 int64_t Offset, bool isTarget,
2423 unsigned TargetFlags) {
2424 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2425 SDVTList VTs = getVTList(VT);
2426
2428 AddNodeIDNode(ID, Opc, VTs, {});
2429 ID.AddPointer(BA);
2430 ID.AddInteger(Offset);
2431 ID.AddInteger(TargetFlags);
2432 void *IP = nullptr;
2433 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2434 return SDValue(E, 0);
2435
2436 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2437 CSEMap.InsertNode(N, IP);
2438 InsertNode(N);
2439 return SDValue(N, 0);
2440}
2441
2444 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2445 ID.AddPointer(V);
2446
2447 void *IP = nullptr;
2448 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2449 return SDValue(E, 0);
2450
2451 auto *N = newSDNode<SrcValueSDNode>(V);
2452 CSEMap.InsertNode(N, IP);
2453 InsertNode(N);
2454 return SDValue(N, 0);
2455}
2456
2459 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2460 ID.AddPointer(MD);
2461
2462 void *IP = nullptr;
2463 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2464 return SDValue(E, 0);
2465
2466 auto *N = newSDNode<MDNodeSDNode>(MD);
2467 CSEMap.InsertNode(N, IP);
2468 InsertNode(N);
2469 return SDValue(N, 0);
2470}
2471
2473 if (VT == V.getValueType())
2474 return V;
2475
2476 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2477}
2478
2480 unsigned SrcAS, unsigned DestAS) {
2481 SDVTList VTs = getVTList(VT);
2482 SDValue Ops[] = {Ptr};
2485 ID.AddInteger(SrcAS);
2486 ID.AddInteger(DestAS);
2487
2488 void *IP = nullptr;
2489 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2490 return SDValue(E, 0);
2491
2492 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2493 VTs, SrcAS, DestAS);
2494 createOperands(N, Ops);
2495
2496 CSEMap.InsertNode(N, IP);
2497 InsertNode(N);
2498 return SDValue(N, 0);
2499}
2500
2502 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2503}
2504
2505/// getShiftAmountOperand - Return the specified value casted to
2506/// the target's desired shift amount type.
2508 EVT OpTy = Op.getValueType();
2509 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2510 if (OpTy == ShTy || OpTy.isVector()) return Op;
2511
2512 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2513}
2514
2516 SDLoc dl(Node);
2518 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2519 EVT VT = Node->getValueType(0);
2520 SDValue Tmp1 = Node->getOperand(0);
2521 SDValue Tmp2 = Node->getOperand(1);
2522 const MaybeAlign MA(Node->getConstantOperandVal(3));
2523
2524 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2525 Tmp2, MachinePointerInfo(V));
2526 SDValue VAList = VAListLoad;
2527
2528 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2529 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2530 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2531
2532 VAList = getNode(
2533 ISD::AND, dl, VAList.getValueType(), VAList,
2534 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2535 }
2536
2537 // Increment the pointer, VAList, to the next vaarg
2538 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2539 getConstant(getDataLayout().getTypeAllocSize(
2540 VT.getTypeForEVT(*getContext())),
2541 dl, VAList.getValueType()));
2542 // Store the incremented VAList to the legalized pointer
2543 Tmp1 =
2544 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2545 // Load the actual argument out of the pointer VAList
2546 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2547}
2548
2550 SDLoc dl(Node);
2552 // This defaults to loading a pointer from the input and storing it to the
2553 // output, returning the chain.
2554 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2555 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2556 SDValue Tmp1 =
2557 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2558 Node->getOperand(2), MachinePointerInfo(VS));
2559 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2560 MachinePointerInfo(VD));
2561}
2562
2564 const DataLayout &DL = getDataLayout();
2565 Type *Ty = VT.getTypeForEVT(*getContext());
2566 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2567
2568 if (TLI->isTypeLegal(VT) || !VT.isVector())
2569 return RedAlign;
2570
2571 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2572 const Align StackAlign = TFI->getStackAlign();
2573
2574 // See if we can choose a smaller ABI alignment in cases where it's an
2575 // illegal vector type that will get broken down.
2576 if (RedAlign > StackAlign) {
2577 EVT IntermediateVT;
2578 MVT RegisterVT;
2579 unsigned NumIntermediates;
2580 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2581 NumIntermediates, RegisterVT);
2582 Ty = IntermediateVT.getTypeForEVT(*getContext());
2583 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2584 if (RedAlign2 < RedAlign)
2585 RedAlign = RedAlign2;
2586
2587 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2588 // If the stack is not realignable, the alignment should be limited to the
2589 // StackAlignment
2590 RedAlign = std::min(RedAlign, StackAlign);
2591 }
2592
2593 return RedAlign;
2594}
2595
2597 MachineFrameInfo &MFI = MF->getFrameInfo();
2598 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2599 int StackID = 0;
2600 if (Bytes.isScalable())
2601 StackID = TFI->getStackIDForScalableVectors();
2602 // The stack id gives an indication of whether the object is scalable or
2603 // not, so it's safe to pass in the minimum size here.
2604 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2605 false, nullptr, StackID);
2606 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2607}
2608
2610 Type *Ty = VT.getTypeForEVT(*getContext());
2611 Align StackAlign =
2612 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2613 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2614}
2615
2617 TypeSize VT1Size = VT1.getStoreSize();
2618 TypeSize VT2Size = VT2.getStoreSize();
2619 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2620 "Don't know how to choose the maximum size when creating a stack "
2621 "temporary");
2622 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2623 ? VT1Size
2624 : VT2Size;
2625
2626 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2627 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2628 const DataLayout &DL = getDataLayout();
2629 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2630 return CreateStackTemporary(Bytes, Align);
2631}
2632
2634 ISD::CondCode Cond, const SDLoc &dl) {
2635 EVT OpVT = N1.getValueType();
2636
2637 auto GetUndefBooleanConstant = [&]() {
2638 if (VT.getScalarType() == MVT::i1 ||
2639 TLI->getBooleanContents(OpVT) ==
2641 return getUNDEF(VT);
2642 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2643 // so we cannot use getUNDEF(). Return zero instead.
2644 return getConstant(0, dl, VT);
2645 };
2646
2647 // These setcc operations always fold.
2648 switch (Cond) {
2649 default: break;
2650 case ISD::SETFALSE:
2651 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2652 case ISD::SETTRUE:
2653 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2654
2655 case ISD::SETOEQ:
2656 case ISD::SETOGT:
2657 case ISD::SETOGE:
2658 case ISD::SETOLT:
2659 case ISD::SETOLE:
2660 case ISD::SETONE:
2661 case ISD::SETO:
2662 case ISD::SETUO:
2663 case ISD::SETUEQ:
2664 case ISD::SETUNE:
2665 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2666 break;
2667 }
2668
2669 if (OpVT.isInteger()) {
2670 // For EQ and NE, we can always pick a value for the undef to make the
2671 // predicate pass or fail, so we can return undef.
2672 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2673 // icmp eq/ne X, undef -> undef.
2674 if ((N1.isUndef() || N2.isUndef()) &&
2675 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2676 return GetUndefBooleanConstant();
2677
2678 // If both operands are undef, we can return undef for int comparison.
2679 // icmp undef, undef -> undef.
2680 if (N1.isUndef() && N2.isUndef())
2681 return GetUndefBooleanConstant();
2682
2683 // icmp X, X -> true/false
2684 // icmp X, undef -> true/false because undef could be X.
2685 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2686 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2687 }
2688
2690 const APInt &C2 = N2C->getAPIntValue();
2692 const APInt &C1 = N1C->getAPIntValue();
2693
2695 dl, VT, OpVT);
2696 }
2697 }
2698
2699 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2700 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2701
2702 if (N1CFP && N2CFP) {
2703 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2704 switch (Cond) {
2705 default: break;
2706 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2707 return GetUndefBooleanConstant();
2708 [[fallthrough]];
2709 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2710 OpVT);
2711 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2712 return GetUndefBooleanConstant();
2713 [[fallthrough]];
2715 R==APFloat::cmpLessThan, dl, VT,
2716 OpVT);
2717 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2718 return GetUndefBooleanConstant();
2719 [[fallthrough]];
2720 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2721 OpVT);
2722 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2723 return GetUndefBooleanConstant();
2724 [[fallthrough]];
2726 VT, OpVT);
2727 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2728 return GetUndefBooleanConstant();
2729 [[fallthrough]];
2731 R==APFloat::cmpEqual, dl, VT,
2732 OpVT);
2733 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2734 return GetUndefBooleanConstant();
2735 [[fallthrough]];
2737 R==APFloat::cmpEqual, dl, VT, OpVT);
2738 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2739 OpVT);
2740 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2741 OpVT);
2743 R==APFloat::cmpEqual, dl, VT,
2744 OpVT);
2745 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2746 OpVT);
2748 R==APFloat::cmpLessThan, dl, VT,
2749 OpVT);
2751 R==APFloat::cmpUnordered, dl, VT,
2752 OpVT);
2754 VT, OpVT);
2755 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2756 OpVT);
2757 }
2758 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2759 // Ensure that the constant occurs on the RHS.
2761 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2762 return SDValue();
2763 return getSetCC(dl, VT, N2, N1, SwappedCond);
2764 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2765 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2766 // If an operand is known to be a nan (or undef that could be a nan), we can
2767 // fold it.
2768 // Choosing NaN for the undef will always make unordered comparison succeed
2769 // and ordered comparison fails.
2770 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2771 switch (ISD::getUnorderedFlavor(Cond)) {
2772 default:
2773 llvm_unreachable("Unknown flavor!");
2774 case 0: // Known false.
2775 return getBoolConstant(false, dl, VT, OpVT);
2776 case 1: // Known true.
2777 return getBoolConstant(true, dl, VT, OpVT);
2778 case 2: // Undefined.
2779 return GetUndefBooleanConstant();
2780 }
2781 }
2782
2783 // Could not fold it.
2784 return SDValue();
2785}
2786
2787/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2788/// use this predicate to simplify operations downstream.
2790 unsigned BitWidth = Op.getScalarValueSizeInBits();
2792}
2793
2795 if (Depth >= MaxRecursionDepth)
2796 return false; // Limit search depth.
2797
2798 unsigned Opc = Op.getOpcode();
2799 switch (Opc) {
2800 case ISD::FABS:
2801 return true;
2802 case ISD::AssertNoFPClass: {
2803 FPClassTest NoFPClass =
2804 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2805
2806 const FPClassTest TestMask = fcNan | fcNegative;
2807 return (NoFPClass & TestMask) == TestMask;
2808 }
2809 case ISD::ARITH_FENCE:
2810 return SignBitIsZeroFP(Op, Depth + 1);
2811 case ISD::FEXP:
2812 case ISD::FEXP2:
2813 case ISD::FEXP10:
2814 return Op->getFlags().hasNoNaNs();
2815 default:
2816 return false;
2817 }
2818
2819 llvm_unreachable("covered opcode switch");
2820}
2821
2822/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2823/// this predicate to simplify operations downstream. Mask is known to be zero
2824/// for bits that V cannot have.
2826 unsigned Depth) const {
2827 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2828}
2829
2830/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2831/// DemandedElts. We use this predicate to simplify operations downstream.
2832/// Mask is known to be zero for bits that V cannot have.
2834 const APInt &DemandedElts,
2835 unsigned Depth) const {
2836 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2837}
2838
2839/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2840/// DemandedElts. We use this predicate to simplify operations downstream.
2842 unsigned Depth /* = 0 */) const {
2843 return computeKnownBits(V, DemandedElts, Depth).isZero();
2844}
2845
2846/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2848 unsigned Depth) const {
2849 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2850}
2851
2853 const APInt &DemandedElts,
2854 unsigned Depth) const {
2855 EVT VT = Op.getValueType();
2856 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2857
2858 unsigned NumElts = VT.getVectorNumElements();
2859 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2860
2861 APInt KnownZeroElements = APInt::getZero(NumElts);
2862 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2863 if (!DemandedElts[EltIdx])
2864 continue; // Don't query elements that are not demanded.
2865 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2866 if (MaskedVectorIsZero(Op, Mask, Depth))
2867 KnownZeroElements.setBit(EltIdx);
2868 }
2869 return KnownZeroElements;
2870}
2871
2872/// isSplatValue - Return true if the vector V has the same value
2873/// across all DemandedElts. For scalable vectors, we don't know the
2874/// number of lanes at compile time. Instead, we use a 1 bit APInt
2875/// to represent a conservative value for all lanes; that is, that
2876/// one bit value is implicitly splatted across all lanes.
2877bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2878 APInt &UndefElts, unsigned Depth) const {
2879 unsigned Opcode = V.getOpcode();
2880 EVT VT = V.getValueType();
2881 assert(VT.isVector() && "Vector type expected");
2882 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2883 "scalable demanded bits are ignored");
2884
2885 if (!DemandedElts)
2886 return false; // No demanded elts, better to assume we don't know anything.
2887
2888 if (Depth >= MaxRecursionDepth)
2889 return false; // Limit search depth.
2890
2891 // Deal with some common cases here that work for both fixed and scalable
2892 // vector types.
2893 switch (Opcode) {
2894 case ISD::SPLAT_VECTOR:
2895 UndefElts = V.getOperand(0).isUndef()
2896 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2897 : APInt(DemandedElts.getBitWidth(), 0);
2898 return true;
2899 case ISD::ADD:
2900 case ISD::SUB:
2901 case ISD::AND:
2902 case ISD::XOR:
2903 case ISD::OR: {
2904 APInt UndefLHS, UndefRHS;
2905 SDValue LHS = V.getOperand(0);
2906 SDValue RHS = V.getOperand(1);
2907 // Only recognize splats with the same demanded undef elements for both
2908 // operands, otherwise we might fail to handle binop-specific undef
2909 // handling.
2910 // e.g. (and undef, 0) -> 0 etc.
2911 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2912 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2913 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2914 UndefElts = UndefLHS | UndefRHS;
2915 return true;
2916 }
2917 return false;
2918 }
2919 case ISD::ABS:
2920 case ISD::TRUNCATE:
2921 case ISD::SIGN_EXTEND:
2922 case ISD::ZERO_EXTEND:
2923 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2924 default:
2925 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2926 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2927 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2928 Depth);
2929 break;
2930 }
2931
2932 // We don't support other cases than those above for scalable vectors at
2933 // the moment.
2934 if (VT.isScalableVector())
2935 return false;
2936
2937 unsigned NumElts = VT.getVectorNumElements();
2938 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2939 UndefElts = APInt::getZero(NumElts);
2940
2941 switch (Opcode) {
2942 case ISD::BUILD_VECTOR: {
2943 SDValue Scl;
2944 for (unsigned i = 0; i != NumElts; ++i) {
2945 SDValue Op = V.getOperand(i);
2946 if (Op.isUndef()) {
2947 UndefElts.setBit(i);
2948 continue;
2949 }
2950 if (!DemandedElts[i])
2951 continue;
2952 if (Scl && Scl != Op)
2953 return false;
2954 Scl = Op;
2955 }
2956 return true;
2957 }
2958 case ISD::VECTOR_SHUFFLE: {
2959 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2960 APInt DemandedLHS = APInt::getZero(NumElts);
2961 APInt DemandedRHS = APInt::getZero(NumElts);
2962 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
2963 for (int i = 0; i != (int)NumElts; ++i) {
2964 int M = Mask[i];
2965 if (M < 0) {
2966 UndefElts.setBit(i);
2967 continue;
2968 }
2969 if (!DemandedElts[i])
2970 continue;
2971 if (M < (int)NumElts)
2972 DemandedLHS.setBit(M);
2973 else
2974 DemandedRHS.setBit(M - NumElts);
2975 }
2976
2977 // If we aren't demanding either op, assume there's no splat.
2978 // If we are demanding both ops, assume there's no splat.
2979 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
2980 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
2981 return false;
2982
2983 // See if the demanded elts of the source op is a splat or we only demand
2984 // one element, which should always be a splat.
2985 // TODO: Handle source ops splats with undefs.
2986 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
2987 APInt SrcUndefs;
2988 return (SrcElts.popcount() == 1) ||
2989 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
2990 (SrcElts & SrcUndefs).isZero());
2991 };
2992 if (!DemandedLHS.isZero())
2993 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
2994 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
2995 }
2997 // Offset the demanded elts by the subvector index.
2998 SDValue Src = V.getOperand(0);
2999 // We don't support scalable vectors at the moment.
3000 if (Src.getValueType().isScalableVector())
3001 return false;
3002 uint64_t Idx = V.getConstantOperandVal(1);
3003 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3004 APInt UndefSrcElts;
3005 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3006 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3007 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3008 return true;
3009 }
3010 break;
3011 }
3015 // Widen the demanded elts by the src element count.
3016 SDValue Src = V.getOperand(0);
3017 // We don't support scalable vectors at the moment.
3018 if (Src.getValueType().isScalableVector())
3019 return false;
3020 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3021 APInt UndefSrcElts;
3022 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3023 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3024 UndefElts = UndefSrcElts.trunc(NumElts);
3025 return true;
3026 }
3027 break;
3028 }
3029 case ISD::BITCAST: {
3030 SDValue Src = V.getOperand(0);
3031 EVT SrcVT = Src.getValueType();
3032 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3033 unsigned BitWidth = VT.getScalarSizeInBits();
3034
3035 // Ignore bitcasts from unsupported types.
3036 // TODO: Add fp support?
3037 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3038 break;
3039
3040 // Bitcast 'small element' vector to 'large element' vector.
3041 if ((BitWidth % SrcBitWidth) == 0) {
3042 // See if each sub element is a splat.
3043 unsigned Scale = BitWidth / SrcBitWidth;
3044 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3045 APInt ScaledDemandedElts =
3046 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3047 for (unsigned I = 0; I != Scale; ++I) {
3048 APInt SubUndefElts;
3049 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3050 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3051 SubDemandedElts &= ScaledDemandedElts;
3052 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3053 return false;
3054 // TODO: Add support for merging sub undef elements.
3055 if (!SubUndefElts.isZero())
3056 return false;
3057 }
3058 return true;
3059 }
3060 break;
3061 }
3062 }
3063
3064 return false;
3065}
3066
3067/// Helper wrapper to main isSplatValue function.
3068bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3069 EVT VT = V.getValueType();
3070 assert(VT.isVector() && "Vector type expected");
3071
3072 APInt UndefElts;
3073 // Since the number of lanes in a scalable vector is unknown at compile time,
3074 // we track one bit which is implicitly broadcast to all lanes. This means
3075 // that all lanes in a scalable vector are considered demanded.
3076 APInt DemandedElts
3078 return isSplatValue(V, DemandedElts, UndefElts) &&
3079 (AllowUndefs || !UndefElts);
3080}
3081
3084
3085 EVT VT = V.getValueType();
3086 unsigned Opcode = V.getOpcode();
3087 switch (Opcode) {
3088 default: {
3089 APInt UndefElts;
3090 // Since the number of lanes in a scalable vector is unknown at compile time,
3091 // we track one bit which is implicitly broadcast to all lanes. This means
3092 // that all lanes in a scalable vector are considered demanded.
3093 APInt DemandedElts
3095
3096 if (isSplatValue(V, DemandedElts, UndefElts)) {
3097 if (VT.isScalableVector()) {
3098 // DemandedElts and UndefElts are ignored for scalable vectors, since
3099 // the only supported cases are SPLAT_VECTOR nodes.
3100 SplatIdx = 0;
3101 } else {
3102 // Handle case where all demanded elements are UNDEF.
3103 if (DemandedElts.isSubsetOf(UndefElts)) {
3104 SplatIdx = 0;
3105 return getUNDEF(VT);
3106 }
3107 SplatIdx = (UndefElts & DemandedElts).countr_one();
3108 }
3109 return V;
3110 }
3111 break;
3112 }
3113 case ISD::SPLAT_VECTOR:
3114 SplatIdx = 0;
3115 return V;
3116 case ISD::VECTOR_SHUFFLE: {
3117 assert(!VT.isScalableVector());
3118 // Check if this is a shuffle node doing a splat.
3119 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3120 // getTargetVShiftNode currently struggles without the splat source.
3121 auto *SVN = cast<ShuffleVectorSDNode>(V);
3122 if (!SVN->isSplat())
3123 break;
3124 int Idx = SVN->getSplatIndex();
3125 int NumElts = V.getValueType().getVectorNumElements();
3126 SplatIdx = Idx % NumElts;
3127 return V.getOperand(Idx / NumElts);
3128 }
3129 }
3130
3131 return SDValue();
3132}
3133
3135 int SplatIdx;
3136 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3137 EVT SVT = SrcVector.getValueType().getScalarType();
3138 EVT LegalSVT = SVT;
3139 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3140 if (!SVT.isInteger())
3141 return SDValue();
3142 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3143 if (LegalSVT.bitsLT(SVT))
3144 return SDValue();
3145 }
3146 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3147 }
3148 return SDValue();
3149}
3150
3151std::optional<ConstantRange>
3153 unsigned Depth) const {
3154 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3155 V.getOpcode() == ISD::SRA) &&
3156 "Unknown shift node");
3157 // Shifting more than the bitwidth is not valid.
3158 unsigned BitWidth = V.getScalarValueSizeInBits();
3159
3160 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3161 const APInt &ShAmt = Cst->getAPIntValue();
3162 if (ShAmt.uge(BitWidth))
3163 return std::nullopt;
3164 return ConstantRange(ShAmt);
3165 }
3166
3167 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3168 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3169 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3170 if (!DemandedElts[i])
3171 continue;
3172 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3173 if (!SA) {
3174 MinAmt = MaxAmt = nullptr;
3175 break;
3176 }
3177 const APInt &ShAmt = SA->getAPIntValue();
3178 if (ShAmt.uge(BitWidth))
3179 return std::nullopt;
3180 if (!MinAmt || MinAmt->ugt(ShAmt))
3181 MinAmt = &ShAmt;
3182 if (!MaxAmt || MaxAmt->ult(ShAmt))
3183 MaxAmt = &ShAmt;
3184 }
3185 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3186 "Failed to find matching min/max shift amounts");
3187 if (MinAmt && MaxAmt)
3188 return ConstantRange(*MinAmt, *MaxAmt + 1);
3189 }
3190
3191 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3192 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3193 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3194 if (KnownAmt.getMaxValue().ult(BitWidth))
3195 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3196
3197 return std::nullopt;
3198}
3199
3200std::optional<unsigned>
3202 unsigned Depth) const {
3203 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3204 V.getOpcode() == ISD::SRA) &&
3205 "Unknown shift node");
3206 if (std::optional<ConstantRange> AmtRange =
3207 getValidShiftAmountRange(V, DemandedElts, Depth))
3208 if (const APInt *ShAmt = AmtRange->getSingleElement())
3209 return ShAmt->getZExtValue();
3210 return std::nullopt;
3211}
3212
3213std::optional<unsigned>
3215 EVT VT = V.getValueType();
3216 APInt DemandedElts = VT.isFixedLengthVector()
3218 : APInt(1, 1);
3219 return getValidShiftAmount(V, DemandedElts, Depth);
3220}
3221
3222std::optional<unsigned>
3224 unsigned Depth) const {
3225 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3226 V.getOpcode() == ISD::SRA) &&
3227 "Unknown shift node");
3228 if (std::optional<ConstantRange> AmtRange =
3229 getValidShiftAmountRange(V, DemandedElts, Depth))
3230 return AmtRange->getUnsignedMin().getZExtValue();
3231 return std::nullopt;
3232}
3233
3234std::optional<unsigned>
3236 EVT VT = V.getValueType();
3237 APInt DemandedElts = VT.isFixedLengthVector()
3239 : APInt(1, 1);
3240 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3241}
3242
3243std::optional<unsigned>
3245 unsigned Depth) const {
3246 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3247 V.getOpcode() == ISD::SRA) &&
3248 "Unknown shift node");
3249 if (std::optional<ConstantRange> AmtRange =
3250 getValidShiftAmountRange(V, DemandedElts, Depth))
3251 return AmtRange->getUnsignedMax().getZExtValue();
3252 return std::nullopt;
3253}
3254
3255std::optional<unsigned>
3257 EVT VT = V.getValueType();
3258 APInt DemandedElts = VT.isFixedLengthVector()
3260 : APInt(1, 1);
3261 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3262}
3263
3264/// Determine which bits of Op are known to be either zero or one and return
3265/// them in Known. For vectors, the known bits are those that are shared by
3266/// every vector element.
3268 EVT VT = Op.getValueType();
3269
3270 // Since the number of lanes in a scalable vector is unknown at compile time,
3271 // we track one bit which is implicitly broadcast to all lanes. This means
3272 // that all lanes in a scalable vector are considered demanded.
3273 APInt DemandedElts = VT.isFixedLengthVector()
3275 : APInt(1, 1);
3276 return computeKnownBits(Op, DemandedElts, Depth);
3277}
3278
3279/// Determine which bits of Op are known to be either zero or one and return
3280/// them in Known. The DemandedElts argument allows us to only collect the known
3281/// bits that are shared by the requested vector elements.
3283 unsigned Depth) const {
3284 unsigned BitWidth = Op.getScalarValueSizeInBits();
3285
3286 KnownBits Known(BitWidth); // Don't know anything.
3287
3288 if (auto OptAPInt = Op->bitcastToAPInt()) {
3289 // We know all of the bits for a constant!
3290 return KnownBits::makeConstant(*std::move(OptAPInt));
3291 }
3292
3293 if (Depth >= MaxRecursionDepth)
3294 return Known; // Limit search depth.
3295
3296 KnownBits Known2;
3297 unsigned NumElts = DemandedElts.getBitWidth();
3298 assert((!Op.getValueType().isFixedLengthVector() ||
3299 NumElts == Op.getValueType().getVectorNumElements()) &&
3300 "Unexpected vector size");
3301
3302 if (!DemandedElts)
3303 return Known; // No demanded elts, better to assume we don't know anything.
3304
3305 unsigned Opcode = Op.getOpcode();
3306 switch (Opcode) {
3307 case ISD::MERGE_VALUES:
3308 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3309 Depth + 1);
3310 case ISD::SPLAT_VECTOR: {
3311 SDValue SrcOp = Op.getOperand(0);
3312 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3313 "Expected SPLAT_VECTOR implicit truncation");
3314 // Implicitly truncate the bits to match the official semantics of
3315 // SPLAT_VECTOR.
3316 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3317 break;
3318 }
3320 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3321 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3322 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3323 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3324 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3325 }
3326 break;
3327 }
3328 case ISD::STEP_VECTOR: {
3329 const APInt &Step = Op.getConstantOperandAPInt(0);
3330
3331 if (Step.isPowerOf2())
3332 Known.Zero.setLowBits(Step.logBase2());
3333
3335
3336 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3337 break;
3338 const APInt MinNumElts =
3339 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3340
3341 bool Overflow;
3342 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3344 .umul_ov(MinNumElts, Overflow);
3345 if (Overflow)
3346 break;
3347
3348 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3349 if (Overflow)
3350 break;
3351
3352 Known.Zero.setHighBits(MaxValue.countl_zero());
3353 break;
3354 }
3355 case ISD::BUILD_VECTOR:
3356 assert(!Op.getValueType().isScalableVector());
3357 // Collect the known bits that are shared by every demanded vector element.
3358 Known.setAllConflict();
3359 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3360 if (!DemandedElts[i])
3361 continue;
3362
3363 SDValue SrcOp = Op.getOperand(i);
3364 Known2 = computeKnownBits(SrcOp, Depth + 1);
3365
3366 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3367 if (SrcOp.getValueSizeInBits() != BitWidth) {
3368 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3369 "Expected BUILD_VECTOR implicit truncation");
3370 Known2 = Known2.trunc(BitWidth);
3371 }
3372
3373 // Known bits are the values that are shared by every demanded element.
3374 Known = Known.intersectWith(Known2);
3375
3376 // If we don't know any bits, early out.
3377 if (Known.isUnknown())
3378 break;
3379 }
3380 break;
3381 case ISD::VECTOR_COMPRESS: {
3382 SDValue Vec = Op.getOperand(0);
3383 SDValue PassThru = Op.getOperand(2);
3384 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3385 // If we don't know any bits, early out.
3386 if (Known.isUnknown())
3387 break;
3388 Known2 = computeKnownBits(Vec, Depth + 1);
3389 Known = Known.intersectWith(Known2);
3390 break;
3391 }
3392 case ISD::VECTOR_SHUFFLE: {
3393 assert(!Op.getValueType().isScalableVector());
3394 // Collect the known bits that are shared by every vector element referenced
3395 // by the shuffle.
3396 APInt DemandedLHS, DemandedRHS;
3398 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3399 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3400 DemandedLHS, DemandedRHS))
3401 break;
3402
3403 // Known bits are the values that are shared by every demanded element.
3404 Known.setAllConflict();
3405 if (!!DemandedLHS) {
3406 SDValue LHS = Op.getOperand(0);
3407 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3408 Known = Known.intersectWith(Known2);
3409 }
3410 // If we don't know any bits, early out.
3411 if (Known.isUnknown())
3412 break;
3413 if (!!DemandedRHS) {
3414 SDValue RHS = Op.getOperand(1);
3415 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3416 Known = Known.intersectWith(Known2);
3417 }
3418 break;
3419 }
3420 case ISD::VSCALE: {
3422 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3423 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3424 break;
3425 }
3426 case ISD::CONCAT_VECTORS: {
3427 if (Op.getValueType().isScalableVector())
3428 break;
3429 // Split DemandedElts and test each of the demanded subvectors.
3430 Known.setAllConflict();
3431 EVT SubVectorVT = Op.getOperand(0).getValueType();
3432 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3433 unsigned NumSubVectors = Op.getNumOperands();
3434 for (unsigned i = 0; i != NumSubVectors; ++i) {
3435 APInt DemandedSub =
3436 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3437 if (!!DemandedSub) {
3438 SDValue Sub = Op.getOperand(i);
3439 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3440 Known = Known.intersectWith(Known2);
3441 }
3442 // If we don't know any bits, early out.
3443 if (Known.isUnknown())
3444 break;
3445 }
3446 break;
3447 }
3448 case ISD::INSERT_SUBVECTOR: {
3449 if (Op.getValueType().isScalableVector())
3450 break;
3451 // Demand any elements from the subvector and the remainder from the src its
3452 // inserted into.
3453 SDValue Src = Op.getOperand(0);
3454 SDValue Sub = Op.getOperand(1);
3455 uint64_t Idx = Op.getConstantOperandVal(2);
3456 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3457 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3458 APInt DemandedSrcElts = DemandedElts;
3459 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3460
3461 Known.setAllConflict();
3462 if (!!DemandedSubElts) {
3463 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3464 if (Known.isUnknown())
3465 break; // early-out.
3466 }
3467 if (!!DemandedSrcElts) {
3468 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3469 Known = Known.intersectWith(Known2);
3470 }
3471 break;
3472 }
3474 // Offset the demanded elts by the subvector index.
3475 SDValue Src = Op.getOperand(0);
3476 // Bail until we can represent demanded elements for scalable vectors.
3477 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3478 break;
3479 uint64_t Idx = Op.getConstantOperandVal(1);
3480 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3481 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3482 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3483 break;
3484 }
3485 case ISD::SCALAR_TO_VECTOR: {
3486 if (Op.getValueType().isScalableVector())
3487 break;
3488 // We know about scalar_to_vector as much as we know about it source,
3489 // which becomes the first element of otherwise unknown vector.
3490 if (DemandedElts != 1)
3491 break;
3492
3493 SDValue N0 = Op.getOperand(0);
3494 Known = computeKnownBits(N0, Depth + 1);
3495 if (N0.getValueSizeInBits() != BitWidth)
3496 Known = Known.trunc(BitWidth);
3497
3498 break;
3499 }
3500 case ISD::BITCAST: {
3501 if (Op.getValueType().isScalableVector())
3502 break;
3503
3504 SDValue N0 = Op.getOperand(0);
3505 EVT SubVT = N0.getValueType();
3506 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3507
3508 // Ignore bitcasts from unsupported types.
3509 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3510 break;
3511
3512 // Fast handling of 'identity' bitcasts.
3513 if (BitWidth == SubBitWidth) {
3514 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3515 break;
3516 }
3517
3518 bool IsLE = getDataLayout().isLittleEndian();
3519
3520 // Bitcast 'small element' vector to 'large element' scalar/vector.
3521 if ((BitWidth % SubBitWidth) == 0) {
3522 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3523
3524 // Collect known bits for the (larger) output by collecting the known
3525 // bits from each set of sub elements and shift these into place.
3526 // We need to separately call computeKnownBits for each set of
3527 // sub elements as the knownbits for each is likely to be different.
3528 unsigned SubScale = BitWidth / SubBitWidth;
3529 APInt SubDemandedElts(NumElts * SubScale, 0);
3530 for (unsigned i = 0; i != NumElts; ++i)
3531 if (DemandedElts[i])
3532 SubDemandedElts.setBit(i * SubScale);
3533
3534 for (unsigned i = 0; i != SubScale; ++i) {
3535 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3536 Depth + 1);
3537 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3538 Known.insertBits(Known2, SubBitWidth * Shifts);
3539 }
3540 }
3541
3542 // Bitcast 'large element' scalar/vector to 'small element' vector.
3543 if ((SubBitWidth % BitWidth) == 0) {
3544 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3545
3546 // Collect known bits for the (smaller) output by collecting the known
3547 // bits from the overlapping larger input elements and extracting the
3548 // sub sections we actually care about.
3549 unsigned SubScale = SubBitWidth / BitWidth;
3550 APInt SubDemandedElts =
3551 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3552 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3553
3554 Known.setAllConflict();
3555 for (unsigned i = 0; i != NumElts; ++i)
3556 if (DemandedElts[i]) {
3557 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3558 unsigned Offset = (Shifts % SubScale) * BitWidth;
3559 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3560 // If we don't know any bits, early out.
3561 if (Known.isUnknown())
3562 break;
3563 }
3564 }
3565 break;
3566 }
3567 case ISD::AND:
3568 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3569 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3570
3571 Known &= Known2;
3572 break;
3573 case ISD::OR:
3574 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3575 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3576
3577 Known |= Known2;
3578 break;
3579 case ISD::XOR:
3580 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3581 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3582
3583 Known ^= Known2;
3584 break;
3585 case ISD::MUL: {
3586 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3587 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3588 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3589 // TODO: SelfMultiply can be poison, but not undef.
3590 if (SelfMultiply)
3591 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3592 Op.getOperand(0), DemandedElts, false, Depth + 1);
3593 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3594
3595 // If the multiplication is known not to overflow, the product of a number
3596 // with itself is non-negative. Only do this if we didn't already computed
3597 // the opposite value for the sign bit.
3598 if (Op->getFlags().hasNoSignedWrap() &&
3599 Op.getOperand(0) == Op.getOperand(1) &&
3600 !Known.isNegative())
3601 Known.makeNonNegative();
3602 break;
3603 }
3604 case ISD::MULHU: {
3605 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3606 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3607 Known = KnownBits::mulhu(Known, Known2);
3608 break;
3609 }
3610 case ISD::MULHS: {
3611 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3612 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3613 Known = KnownBits::mulhs(Known, Known2);
3614 break;
3615 }
3616 case ISD::ABDU: {
3617 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3618 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3619 Known = KnownBits::abdu(Known, Known2);
3620 break;
3621 }
3622 case ISD::ABDS: {
3623 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3624 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3625 Known = KnownBits::abds(Known, Known2);
3626 unsigned SignBits1 =
3627 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3628 if (SignBits1 == 1)
3629 break;
3630 unsigned SignBits0 =
3631 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3632 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3633 break;
3634 }
3635 case ISD::UMUL_LOHI: {
3636 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3637 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3638 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3639 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3640 if (Op.getResNo() == 0)
3641 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3642 else
3643 Known = KnownBits::mulhu(Known, Known2);
3644 break;
3645 }
3646 case ISD::SMUL_LOHI: {
3647 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3648 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3649 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3650 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3651 if (Op.getResNo() == 0)
3652 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3653 else
3654 Known = KnownBits::mulhs(Known, Known2);
3655 break;
3656 }
3657 case ISD::AVGFLOORU: {
3658 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3659 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3660 Known = KnownBits::avgFloorU(Known, Known2);
3661 break;
3662 }
3663 case ISD::AVGCEILU: {
3664 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3665 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3666 Known = KnownBits::avgCeilU(Known, Known2);
3667 break;
3668 }
3669 case ISD::AVGFLOORS: {
3670 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3671 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3672 Known = KnownBits::avgFloorS(Known, Known2);
3673 break;
3674 }
3675 case ISD::AVGCEILS: {
3676 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3677 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3678 Known = KnownBits::avgCeilS(Known, Known2);
3679 break;
3680 }
3681 case ISD::SELECT:
3682 case ISD::VSELECT:
3683 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3684 // If we don't know any bits, early out.
3685 if (Known.isUnknown())
3686 break;
3687 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3688
3689 // Only known if known in both the LHS and RHS.
3690 Known = Known.intersectWith(Known2);
3691 break;
3692 case ISD::SELECT_CC:
3693 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3694 // If we don't know any bits, early out.
3695 if (Known.isUnknown())
3696 break;
3697 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3698
3699 // Only known if known in both the LHS and RHS.
3700 Known = Known.intersectWith(Known2);
3701 break;
3702 case ISD::SMULO:
3703 case ISD::UMULO:
3704 if (Op.getResNo() != 1)
3705 break;
3706 // The boolean result conforms to getBooleanContents.
3707 // If we know the result of a setcc has the top bits zero, use this info.
3708 // We know that we have an integer-based boolean since these operations
3709 // are only available for integer.
3710 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3712 BitWidth > 1)
3713 Known.Zero.setBitsFrom(1);
3714 break;
3715 case ISD::SETCC:
3716 case ISD::SETCCCARRY:
3717 case ISD::STRICT_FSETCC:
3718 case ISD::STRICT_FSETCCS: {
3719 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3720 // If we know the result of a setcc has the top bits zero, use this info.
3721 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3723 BitWidth > 1)
3724 Known.Zero.setBitsFrom(1);
3725 break;
3726 }
3727 case ISD::SHL: {
3728 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3729 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3730
3731 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3732 bool NSW = Op->getFlags().hasNoSignedWrap();
3733
3734 bool ShAmtNonZero = Known2.isNonZero();
3735
3736 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3737
3738 // Minimum shift low bits are known zero.
3739 if (std::optional<unsigned> ShMinAmt =
3740 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3741 Known.Zero.setLowBits(*ShMinAmt);
3742 break;
3743 }
3744 case ISD::SRL:
3745 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3746 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3747 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3748 Op->getFlags().hasExact());
3749
3750 // Minimum shift high bits are known zero.
3751 if (std::optional<unsigned> ShMinAmt =
3752 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3753 Known.Zero.setHighBits(*ShMinAmt);
3754 break;
3755 case ISD::SRA:
3756 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3757 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3758 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3759 Op->getFlags().hasExact());
3760 break;
3761 case ISD::ROTL:
3762 case ISD::ROTR:
3763 if (ConstantSDNode *C =
3764 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3765 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3766
3767 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3768
3769 // Canonicalize to ROTR.
3770 if (Opcode == ISD::ROTL && Amt != 0)
3771 Amt = BitWidth - Amt;
3772
3773 Known.Zero = Known.Zero.rotr(Amt);
3774 Known.One = Known.One.rotr(Amt);
3775 }
3776 break;
3777 case ISD::FSHL:
3778 case ISD::FSHR:
3779 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3780 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3781
3782 // For fshl, 0-shift returns the 1st arg.
3783 // For fshr, 0-shift returns the 2nd arg.
3784 if (Amt == 0) {
3785 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3786 DemandedElts, Depth + 1);
3787 break;
3788 }
3789
3790 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3791 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3792 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3793 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3794 if (Opcode == ISD::FSHL) {
3795 Known <<= Amt;
3796 Known2 >>= BitWidth - Amt;
3797 } else {
3798 Known <<= BitWidth - Amt;
3799 Known2 >>= Amt;
3800 }
3801 Known = Known.unionWith(Known2);
3802 }
3803 break;
3804 case ISD::SHL_PARTS:
3805 case ISD::SRA_PARTS:
3806 case ISD::SRL_PARTS: {
3807 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3808
3809 // Collect lo/hi source values and concatenate.
3810 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3811 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3812 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3813 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3814 Known = Known2.concat(Known);
3815
3816 // Collect shift amount.
3817 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3818
3819 if (Opcode == ISD::SHL_PARTS)
3820 Known = KnownBits::shl(Known, Known2);
3821 else if (Opcode == ISD::SRA_PARTS)
3822 Known = KnownBits::ashr(Known, Known2);
3823 else // if (Opcode == ISD::SRL_PARTS)
3824 Known = KnownBits::lshr(Known, Known2);
3825
3826 // TODO: Minimum shift low/high bits are known zero.
3827
3828 if (Op.getResNo() == 0)
3829 Known = Known.extractBits(LoBits, 0);
3830 else
3831 Known = Known.extractBits(HiBits, LoBits);
3832 break;
3833 }
3835 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3836 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3837 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3838 break;
3839 }
3840 case ISD::CTTZ:
3841 case ISD::CTTZ_ZERO_UNDEF: {
3842 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3843 // If we have a known 1, its position is our upper bound.
3844 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3845 unsigned LowBits = llvm::bit_width(PossibleTZ);
3846 Known.Zero.setBitsFrom(LowBits);
3847 break;
3848 }
3849 case ISD::CTLZ:
3850 case ISD::CTLZ_ZERO_UNDEF: {
3851 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3852 // If we have a known 1, its position is our upper bound.
3853 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3854 unsigned LowBits = llvm::bit_width(PossibleLZ);
3855 Known.Zero.setBitsFrom(LowBits);
3856 break;
3857 }
3858 case ISD::CTPOP: {
3859 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3860 // If we know some of the bits are zero, they can't be one.
3861 unsigned PossibleOnes = Known2.countMaxPopulation();
3862 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3863 break;
3864 }
3865 case ISD::PARITY: {
3866 // Parity returns 0 everywhere but the LSB.
3867 Known.Zero.setBitsFrom(1);
3868 break;
3869 }
3870 case ISD::MGATHER:
3871 case ISD::MLOAD: {
3872 ISD::LoadExtType ETy =
3873 (Opcode == ISD::MGATHER)
3874 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3875 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3876 if (ETy == ISD::ZEXTLOAD) {
3877 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3878 KnownBits Known0(MemVT.getScalarSizeInBits());
3879 return Known0.zext(BitWidth);
3880 }
3881 break;
3882 }
3883 case ISD::LOAD: {
3885 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3886 if (ISD::isNON_EXTLoad(LD) && Cst) {
3887 // Determine any common known bits from the loaded constant pool value.
3888 Type *CstTy = Cst->getType();
3889 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3890 !Op.getValueType().isScalableVector()) {
3891 // If its a vector splat, then we can (quickly) reuse the scalar path.
3892 // NOTE: We assume all elements match and none are UNDEF.
3893 if (CstTy->isVectorTy()) {
3894 if (const Constant *Splat = Cst->getSplatValue()) {
3895 Cst = Splat;
3896 CstTy = Cst->getType();
3897 }
3898 }
3899 // TODO - do we need to handle different bitwidths?
3900 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3901 // Iterate across all vector elements finding common known bits.
3902 Known.setAllConflict();
3903 for (unsigned i = 0; i != NumElts; ++i) {
3904 if (!DemandedElts[i])
3905 continue;
3906 if (Constant *Elt = Cst->getAggregateElement(i)) {
3907 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3908 const APInt &Value = CInt->getValue();
3909 Known.One &= Value;
3910 Known.Zero &= ~Value;
3911 continue;
3912 }
3913 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3914 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3915 Known.One &= Value;
3916 Known.Zero &= ~Value;
3917 continue;
3918 }
3919 }
3920 Known.One.clearAllBits();
3921 Known.Zero.clearAllBits();
3922 break;
3923 }
3924 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3925 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3926 Known = KnownBits::makeConstant(CInt->getValue());
3927 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3928 Known =
3929 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3930 }
3931 }
3932 }
3933 } else if (Op.getResNo() == 0) {
3934 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3935 KnownBits KnownScalarMemory(ScalarMemorySize);
3936 if (const MDNode *MD = LD->getRanges())
3937 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3938
3939 // Extend the Known bits from memory to the size of the scalar result.
3940 if (ISD::isZEXTLoad(Op.getNode()))
3941 Known = KnownScalarMemory.zext(BitWidth);
3942 else if (ISD::isSEXTLoad(Op.getNode()))
3943 Known = KnownScalarMemory.sext(BitWidth);
3944 else if (ISD::isEXTLoad(Op.getNode()))
3945 Known = KnownScalarMemory.anyext(BitWidth);
3946 else
3947 Known = KnownScalarMemory;
3948 assert(Known.getBitWidth() == BitWidth);
3949 return Known;
3950 }
3951 break;
3952 }
3954 if (Op.getValueType().isScalableVector())
3955 break;
3956 EVT InVT = Op.getOperand(0).getValueType();
3957 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3958 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3959 Known = Known.zext(BitWidth);
3960 break;
3961 }
3962 case ISD::ZERO_EXTEND: {
3963 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3964 Known = Known.zext(BitWidth);
3965 break;
3966 }
3968 if (Op.getValueType().isScalableVector())
3969 break;
3970 EVT InVT = Op.getOperand(0).getValueType();
3971 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3972 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3973 // If the sign bit is known to be zero or one, then sext will extend
3974 // it to the top bits, else it will just zext.
3975 Known = Known.sext(BitWidth);
3976 break;
3977 }
3978 case ISD::SIGN_EXTEND: {
3979 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3980 // If the sign bit is known to be zero or one, then sext will extend
3981 // it to the top bits, else it will just zext.
3982 Known = Known.sext(BitWidth);
3983 break;
3984 }
3986 if (Op.getValueType().isScalableVector())
3987 break;
3988 EVT InVT = Op.getOperand(0).getValueType();
3989 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
3990 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
3991 Known = Known.anyext(BitWidth);
3992 break;
3993 }
3994 case ISD::ANY_EXTEND: {
3995 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3996 Known = Known.anyext(BitWidth);
3997 break;
3998 }
3999 case ISD::TRUNCATE: {
4000 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4001 Known = Known.trunc(BitWidth);
4002 break;
4003 }
4004 case ISD::AssertZext: {
4005 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4007 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4008 Known.Zero |= (~InMask);
4009 Known.One &= (~Known.Zero);
4010 break;
4011 }
4012 case ISD::AssertAlign: {
4013 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4014 assert(LogOfAlign != 0);
4015
4016 // TODO: Should use maximum with source
4017 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4018 // well as clearing one bits.
4019 Known.Zero.setLowBits(LogOfAlign);
4020 Known.One.clearLowBits(LogOfAlign);
4021 break;
4022 }
4023 case ISD::AssertNoFPClass: {
4024 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4025
4026 FPClassTest NoFPClass =
4027 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4028 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4029 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4030 // Cannot be negative.
4031 Known.makeNonNegative();
4032 }
4033
4034 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4035 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4036 // Cannot be positive.
4037 Known.makeNegative();
4038 }
4039
4040 break;
4041 }
4042 case ISD::FGETSIGN:
4043 // All bits are zero except the low bit.
4044 Known.Zero.setBitsFrom(1);
4045 break;
4046 case ISD::ADD:
4047 case ISD::SUB: {
4048 SDNodeFlags Flags = Op.getNode()->getFlags();
4049 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4050 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4052 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4053 Flags.hasNoUnsignedWrap(), Known, Known2);
4054 break;
4055 }
4056 case ISD::USUBO:
4057 case ISD::SSUBO:
4058 case ISD::USUBO_CARRY:
4059 case ISD::SSUBO_CARRY:
4060 if (Op.getResNo() == 1) {
4061 // If we know the result of a setcc has the top bits zero, use this info.
4062 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4064 BitWidth > 1)
4065 Known.Zero.setBitsFrom(1);
4066 break;
4067 }
4068 [[fallthrough]];
4069 case ISD::SUBC: {
4070 assert(Op.getResNo() == 0 &&
4071 "We only compute knownbits for the difference here.");
4072
4073 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4074 KnownBits Borrow(1);
4075 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4076 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4077 // Borrow has bit width 1
4078 Borrow = Borrow.trunc(1);
4079 } else {
4080 Borrow.setAllZero();
4081 }
4082
4083 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4084 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4085 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4086 break;
4087 }
4088 case ISD::UADDO:
4089 case ISD::SADDO:
4090 case ISD::UADDO_CARRY:
4091 case ISD::SADDO_CARRY:
4092 if (Op.getResNo() == 1) {
4093 // If we know the result of a setcc has the top bits zero, use this info.
4094 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4096 BitWidth > 1)
4097 Known.Zero.setBitsFrom(1);
4098 break;
4099 }
4100 [[fallthrough]];
4101 case ISD::ADDC:
4102 case ISD::ADDE: {
4103 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4104
4105 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4106 KnownBits Carry(1);
4107 if (Opcode == ISD::ADDE)
4108 // Can't track carry from glue, set carry to unknown.
4109 Carry.resetAll();
4110 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4111 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4112 // Carry has bit width 1
4113 Carry = Carry.trunc(1);
4114 } else {
4115 Carry.setAllZero();
4116 }
4117
4118 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4119 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4120 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4121 break;
4122 }
4123 case ISD::UDIV: {
4124 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4125 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4126 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4127 break;
4128 }
4129 case ISD::SDIV: {
4130 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4131 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4132 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4133 break;
4134 }
4135 case ISD::SREM: {
4136 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4137 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4138 Known = KnownBits::srem(Known, Known2);
4139 break;
4140 }
4141 case ISD::UREM: {
4142 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4143 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4144 Known = KnownBits::urem(Known, Known2);
4145 break;
4146 }
4147 case ISD::EXTRACT_ELEMENT: {
4148 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4149 const unsigned Index = Op.getConstantOperandVal(1);
4150 const unsigned EltBitWidth = Op.getValueSizeInBits();
4151
4152 // Remove low part of known bits mask
4153 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4154 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4155
4156 // Remove high part of known bit mask
4157 Known = Known.trunc(EltBitWidth);
4158 break;
4159 }
4161 SDValue InVec = Op.getOperand(0);
4162 SDValue EltNo = Op.getOperand(1);
4163 EVT VecVT = InVec.getValueType();
4164 // computeKnownBits not yet implemented for scalable vectors.
4165 if (VecVT.isScalableVector())
4166 break;
4167 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4168 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4169
4170 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4171 // anything about the extended bits.
4172 if (BitWidth > EltBitWidth)
4173 Known = Known.trunc(EltBitWidth);
4174
4175 // If we know the element index, just demand that vector element, else for
4176 // an unknown element index, ignore DemandedElts and demand them all.
4177 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4178 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4179 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4180 DemandedSrcElts =
4181 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4182
4183 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4184 if (BitWidth > EltBitWidth)
4185 Known = Known.anyext(BitWidth);
4186 break;
4187 }
4189 if (Op.getValueType().isScalableVector())
4190 break;
4191
4192 // If we know the element index, split the demand between the
4193 // source vector and the inserted element, otherwise assume we need
4194 // the original demanded vector elements and the value.
4195 SDValue InVec = Op.getOperand(0);
4196 SDValue InVal = Op.getOperand(1);
4197 SDValue EltNo = Op.getOperand(2);
4198 bool DemandedVal = true;
4199 APInt DemandedVecElts = DemandedElts;
4200 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4201 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4202 unsigned EltIdx = CEltNo->getZExtValue();
4203 DemandedVal = !!DemandedElts[EltIdx];
4204 DemandedVecElts.clearBit(EltIdx);
4205 }
4206 Known.setAllConflict();
4207 if (DemandedVal) {
4208 Known2 = computeKnownBits(InVal, Depth + 1);
4209 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4210 }
4211 if (!!DemandedVecElts) {
4212 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4213 Known = Known.intersectWith(Known2);
4214 }
4215 break;
4216 }
4217 case ISD::BITREVERSE: {
4218 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4219 Known = Known2.reverseBits();
4220 break;
4221 }
4222 case ISD::BSWAP: {
4223 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4224 Known = Known2.byteSwap();
4225 break;
4226 }
4227 case ISD::ABS: {
4228 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4229 Known = Known2.abs();
4230 Known.Zero.setHighBits(
4231 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4232 break;
4233 }
4234 case ISD::USUBSAT: {
4235 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4236 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4237 Known = KnownBits::usub_sat(Known, Known2);
4238 break;
4239 }
4240 case ISD::UMIN: {
4241 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4242 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4243 Known = KnownBits::umin(Known, Known2);
4244 break;
4245 }
4246 case ISD::UMAX: {
4247 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4248 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4249 Known = KnownBits::umax(Known, Known2);
4250 break;
4251 }
4252 case ISD::SMIN:
4253 case ISD::SMAX: {
4254 // If we have a clamp pattern, we know that the number of sign bits will be
4255 // the minimum of the clamp min/max range.
4256 bool IsMax = (Opcode == ISD::SMAX);
4257 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4258 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4259 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4260 CstHigh =
4261 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4262 if (CstLow && CstHigh) {
4263 if (!IsMax)
4264 std::swap(CstLow, CstHigh);
4265
4266 const APInt &ValueLow = CstLow->getAPIntValue();
4267 const APInt &ValueHigh = CstHigh->getAPIntValue();
4268 if (ValueLow.sle(ValueHigh)) {
4269 unsigned LowSignBits = ValueLow.getNumSignBits();
4270 unsigned HighSignBits = ValueHigh.getNumSignBits();
4271 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4272 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4273 Known.One.setHighBits(MinSignBits);
4274 break;
4275 }
4276 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4277 Known.Zero.setHighBits(MinSignBits);
4278 break;
4279 }
4280 }
4281 }
4282
4283 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4284 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4285 if (IsMax)
4286 Known = KnownBits::smax(Known, Known2);
4287 else
4288 Known = KnownBits::smin(Known, Known2);
4289
4290 // For SMAX, if CstLow is non-negative we know the result will be
4291 // non-negative and thus all sign bits are 0.
4292 // TODO: There's an equivalent of this for smin with negative constant for
4293 // known ones.
4294 if (IsMax && CstLow) {
4295 const APInt &ValueLow = CstLow->getAPIntValue();
4296 if (ValueLow.isNonNegative()) {
4297 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4298 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4299 }
4300 }
4301
4302 break;
4303 }
4304 case ISD::UINT_TO_FP: {
4305 Known.makeNonNegative();
4306 break;
4307 }
4308 case ISD::SINT_TO_FP: {
4309 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4310 if (Known2.isNonNegative())
4311 Known.makeNonNegative();
4312 else if (Known2.isNegative())
4313 Known.makeNegative();
4314 break;
4315 }
4316 case ISD::FP_TO_UINT_SAT: {
4317 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4318 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4320 break;
4321 }
4322 case ISD::ATOMIC_LOAD: {
4323 // If we are looking at the loaded value.
4324 if (Op.getResNo() == 0) {
4325 auto *AT = cast<AtomicSDNode>(Op);
4326 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4327 KnownBits KnownScalarMemory(ScalarMemorySize);
4328 if (const MDNode *MD = AT->getRanges())
4329 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4330
4331 switch (AT->getExtensionType()) {
4332 case ISD::ZEXTLOAD:
4333 Known = KnownScalarMemory.zext(BitWidth);
4334 break;
4335 case ISD::SEXTLOAD:
4336 Known = KnownScalarMemory.sext(BitWidth);
4337 break;
4338 case ISD::EXTLOAD:
4339 switch (TLI->getExtendForAtomicOps()) {
4340 case ISD::ZERO_EXTEND:
4341 Known = KnownScalarMemory.zext(BitWidth);
4342 break;
4343 case ISD::SIGN_EXTEND:
4344 Known = KnownScalarMemory.sext(BitWidth);
4345 break;
4346 default:
4347 Known = KnownScalarMemory.anyext(BitWidth);
4348 break;
4349 }
4350 break;
4351 case ISD::NON_EXTLOAD:
4352 Known = KnownScalarMemory;
4353 break;
4354 }
4355 assert(Known.getBitWidth() == BitWidth);
4356 }
4357 break;
4358 }
4360 if (Op.getResNo() == 1) {
4361 // The boolean result conforms to getBooleanContents.
4362 // If we know the result of a setcc has the top bits zero, use this info.
4363 // We know that we have an integer-based boolean since these operations
4364 // are only available for integer.
4365 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4367 BitWidth > 1)
4368 Known.Zero.setBitsFrom(1);
4369 break;
4370 }
4371 [[fallthrough]];
4373 case ISD::ATOMIC_SWAP:
4384 case ISD::ATOMIC_LOAD_UMAX: {
4385 // If we are looking at the loaded value.
4386 if (Op.getResNo() == 0) {
4387 auto *AT = cast<AtomicSDNode>(Op);
4388 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4389
4390 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4391 Known.Zero.setBitsFrom(MemBits);
4392 }
4393 break;
4394 }
4395 case ISD::FrameIndex:
4397 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4398 Known, getMachineFunction());
4399 break;
4400
4401 default:
4402 if (Opcode < ISD::BUILTIN_OP_END)
4403 break;
4404 [[fallthrough]];
4408 // TODO: Probably okay to remove after audit; here to reduce change size
4409 // in initial enablement patch for scalable vectors
4410 if (Op.getValueType().isScalableVector())
4411 break;
4412
4413 // Allow the target to implement this method for its nodes.
4414 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4415 break;
4416 }
4417
4418 return Known;
4419}
4420
4421/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4434
4437 // X + 0 never overflow
4438 if (isNullConstant(N1))
4439 return OFK_Never;
4440
4441 // If both operands each have at least two sign bits, the addition
4442 // cannot overflow.
4443 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4444 return OFK_Never;
4445
4446 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4447 return OFK_Sometime;
4448}
4449
4452 // X + 0 never overflow
4453 if (isNullConstant(N1))
4454 return OFK_Never;
4455
4456 // mulhi + 1 never overflow
4457 KnownBits N1Known = computeKnownBits(N1);
4458 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4459 N1Known.getMaxValue().ult(2))
4460 return OFK_Never;
4461
4462 KnownBits N0Known = computeKnownBits(N0);
4463 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4464 N0Known.getMaxValue().ult(2))
4465 return OFK_Never;
4466
4467 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4468 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4469 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4470 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4471}
4472
4475 // X - 0 never overflow
4476 if (isNullConstant(N1))
4477 return OFK_Never;
4478
4479 // If both operands each have at least two sign bits, the subtraction
4480 // cannot overflow.
4481 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4482 return OFK_Never;
4483
4484 KnownBits N0Known = computeKnownBits(N0);
4485 KnownBits N1Known = computeKnownBits(N1);
4486 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4487 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4488 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4489}
4490
4493 // X - 0 never overflow
4494 if (isNullConstant(N1))
4495 return OFK_Never;
4496
4497 KnownBits N0Known = computeKnownBits(N0);
4498 KnownBits N1Known = computeKnownBits(N1);
4499 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4500 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4501 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4502}
4503
4506 // X * 0 and X * 1 never overflow.
4507 if (isNullConstant(N1) || isOneConstant(N1))
4508 return OFK_Never;
4509
4510 KnownBits N0Known = computeKnownBits(N0);
4511 KnownBits N1Known = computeKnownBits(N1);
4512 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4513 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4514 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4515}
4516
4519 // X * 0 and X * 1 never overflow.
4520 if (isNullConstant(N1) || isOneConstant(N1))
4521 return OFK_Never;
4522
4523 // Get the size of the result.
4524 unsigned BitWidth = N0.getScalarValueSizeInBits();
4525
4526 // Sum of the sign bits.
4527 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4528
4529 // If we have enough sign bits, then there's no overflow.
4530 if (SignBits > BitWidth + 1)
4531 return OFK_Never;
4532
4533 if (SignBits == BitWidth + 1) {
4534 // The overflow occurs when the true multiplication of the
4535 // the operands is the minimum negative number.
4536 KnownBits N0Known = computeKnownBits(N0);
4537 KnownBits N1Known = computeKnownBits(N1);
4538 // If one of the operands is non-negative, then there's no
4539 // overflow.
4540 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4541 return OFK_Never;
4542 }
4543
4544 return OFK_Sometime;
4545}
4546
4548 if (Depth >= MaxRecursionDepth)
4549 return false; // Limit search depth.
4550
4551 EVT OpVT = Val.getValueType();
4552 unsigned BitWidth = OpVT.getScalarSizeInBits();
4553
4554 // Is the constant a known power of 2?
4556 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4557 }))
4558 return true;
4559
4560 // A left-shift of a constant one will have exactly one bit set because
4561 // shifting the bit off the end is undefined.
4562 if (Val.getOpcode() == ISD::SHL) {
4563 auto *C = isConstOrConstSplat(Val.getOperand(0));
4564 if (C && C->getAPIntValue() == 1)
4565 return true;
4566 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4567 isKnownNeverZero(Val, Depth);
4568 }
4569
4570 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4571 // one bit set.
4572 if (Val.getOpcode() == ISD::SRL) {
4573 auto *C = isConstOrConstSplat(Val.getOperand(0));
4574 if (C && C->getAPIntValue().isSignMask())
4575 return true;
4576 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4577 isKnownNeverZero(Val, Depth);
4578 }
4579
4580 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4581 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4582
4583 // Are all operands of a build vector constant powers of two?
4584 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4585 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4586 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4587 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4588 return false;
4589 }))
4590 return true;
4591
4592 // Is the operand of a splat vector a constant power of two?
4593 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4595 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4596 return true;
4597
4598 // vscale(power-of-two) is a power-of-two for some targets
4599 if (Val.getOpcode() == ISD::VSCALE &&
4600 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4602 return true;
4603
4604 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4605 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4606 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4608
4609 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4610 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4612
4613 // Looking for `x & -x` pattern:
4614 // If x == 0:
4615 // x & -x -> 0
4616 // If x != 0:
4617 // x & -x -> non-zero pow2
4618 // so if we find the pattern return whether we know `x` is non-zero.
4619 SDValue X;
4620 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4621 return isKnownNeverZero(X, Depth);
4622
4623 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4624 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4625
4626 // More could be done here, though the above checks are enough
4627 // to handle some common cases.
4628 return false;
4629}
4630
4632 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4633 return C1->getValueAPF().getExactLog2Abs() >= 0;
4634
4635 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4636 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4637
4638 return false;
4639}
4640
4642 EVT VT = Op.getValueType();
4643
4644 // Since the number of lanes in a scalable vector is unknown at compile time,
4645 // we track one bit which is implicitly broadcast to all lanes. This means
4646 // that all lanes in a scalable vector are considered demanded.
4647 APInt DemandedElts = VT.isFixedLengthVector()
4649 : APInt(1, 1);
4650 return ComputeNumSignBits(Op, DemandedElts, Depth);
4651}
4652
4653unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4654 unsigned Depth) const {
4655 EVT VT = Op.getValueType();
4656 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4657 unsigned VTBits = VT.getScalarSizeInBits();
4658 unsigned NumElts = DemandedElts.getBitWidth();
4659 unsigned Tmp, Tmp2;
4660 unsigned FirstAnswer = 1;
4661
4662 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4663 const APInt &Val = C->getAPIntValue();
4664 return Val.getNumSignBits();
4665 }
4666
4667 if (Depth >= MaxRecursionDepth)
4668 return 1; // Limit search depth.
4669
4670 if (!DemandedElts)
4671 return 1; // No demanded elts, better to assume we don't know anything.
4672
4673 unsigned Opcode = Op.getOpcode();
4674 switch (Opcode) {
4675 default: break;
4676 case ISD::AssertSext:
4677 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4678 return VTBits-Tmp+1;
4679 case ISD::AssertZext:
4680 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4681 return VTBits-Tmp;
4682 case ISD::FREEZE:
4683 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4684 /*PoisonOnly=*/false))
4685 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4686 break;
4687 case ISD::MERGE_VALUES:
4688 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4689 Depth + 1);
4690 case ISD::SPLAT_VECTOR: {
4691 // Check if the sign bits of source go down as far as the truncated value.
4692 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4693 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4694 if (NumSrcSignBits > (NumSrcBits - VTBits))
4695 return NumSrcSignBits - (NumSrcBits - VTBits);
4696 break;
4697 }
4698 case ISD::BUILD_VECTOR:
4699 assert(!VT.isScalableVector());
4700 Tmp = VTBits;
4701 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4702 if (!DemandedElts[i])
4703 continue;
4704
4705 SDValue SrcOp = Op.getOperand(i);
4706 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4707 // for constant nodes to ensure we only look at the sign bits.
4709 APInt T = C->getAPIntValue().trunc(VTBits);
4710 Tmp2 = T.getNumSignBits();
4711 } else {
4712 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4713
4714 if (SrcOp.getValueSizeInBits() != VTBits) {
4715 assert(SrcOp.getValueSizeInBits() > VTBits &&
4716 "Expected BUILD_VECTOR implicit truncation");
4717 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4718 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4719 }
4720 }
4721 Tmp = std::min(Tmp, Tmp2);
4722 }
4723 return Tmp;
4724
4725 case ISD::VECTOR_COMPRESS: {
4726 SDValue Vec = Op.getOperand(0);
4727 SDValue PassThru = Op.getOperand(2);
4728 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4729 if (Tmp == 1)
4730 return 1;
4731 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4732 Tmp = std::min(Tmp, Tmp2);
4733 return Tmp;
4734 }
4735
4736 case ISD::VECTOR_SHUFFLE: {
4737 // Collect the minimum number of sign bits that are shared by every vector
4738 // element referenced by the shuffle.
4739 APInt DemandedLHS, DemandedRHS;
4741 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4742 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4743 DemandedLHS, DemandedRHS))
4744 return 1;
4745
4746 Tmp = std::numeric_limits<unsigned>::max();
4747 if (!!DemandedLHS)
4748 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4749 if (!!DemandedRHS) {
4750 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4751 Tmp = std::min(Tmp, Tmp2);
4752 }
4753 // If we don't know anything, early out and try computeKnownBits fall-back.
4754 if (Tmp == 1)
4755 break;
4756 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4757 return Tmp;
4758 }
4759
4760 case ISD::BITCAST: {
4761 if (VT.isScalableVector())
4762 break;
4763 SDValue N0 = Op.getOperand(0);
4764 EVT SrcVT = N0.getValueType();
4765 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4766
4767 // Ignore bitcasts from unsupported types..
4768 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4769 break;
4770
4771 // Fast handling of 'identity' bitcasts.
4772 if (VTBits == SrcBits)
4773 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4774
4775 bool IsLE = getDataLayout().isLittleEndian();
4776
4777 // Bitcast 'large element' scalar/vector to 'small element' vector.
4778 if ((SrcBits % VTBits) == 0) {
4779 assert(VT.isVector() && "Expected bitcast to vector");
4780
4781 unsigned Scale = SrcBits / VTBits;
4782 APInt SrcDemandedElts =
4783 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4784
4785 // Fast case - sign splat can be simply split across the small elements.
4786 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4787 if (Tmp == SrcBits)
4788 return VTBits;
4789
4790 // Slow case - determine how far the sign extends into each sub-element.
4791 Tmp2 = VTBits;
4792 for (unsigned i = 0; i != NumElts; ++i)
4793 if (DemandedElts[i]) {
4794 unsigned SubOffset = i % Scale;
4795 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4796 SubOffset = SubOffset * VTBits;
4797 if (Tmp <= SubOffset)
4798 return 1;
4799 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4800 }
4801 return Tmp2;
4802 }
4803 break;
4804 }
4805
4807 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4808 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4809 return VTBits - Tmp + 1;
4810 case ISD::SIGN_EXTEND:
4811 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4812 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4814 // Max of the input and what this extends.
4815 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4816 Tmp = VTBits-Tmp+1;
4817 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4818 return std::max(Tmp, Tmp2);
4820 if (VT.isScalableVector())
4821 break;
4822 SDValue Src = Op.getOperand(0);
4823 EVT SrcVT = Src.getValueType();
4824 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4825 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4826 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4827 }
4828 case ISD::SRA:
4829 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4830 // SRA X, C -> adds C sign bits.
4831 if (std::optional<unsigned> ShAmt =
4832 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4833 Tmp = std::min(Tmp + *ShAmt, VTBits);
4834 return Tmp;
4835 case ISD::SHL:
4836 if (std::optional<ConstantRange> ShAmtRange =
4837 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4838 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4839 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4840 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4841 // shifted out, then we can compute the number of sign bits for the
4842 // operand being extended. A future improvement could be to pass along the
4843 // "shifted left by" information in the recursive calls to
4844 // ComputeKnownSignBits. Allowing us to handle this more generically.
4845 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4846 SDValue Ext = Op.getOperand(0);
4847 EVT ExtVT = Ext.getValueType();
4848 SDValue Extendee = Ext.getOperand(0);
4849 EVT ExtendeeVT = Extendee.getValueType();
4850 unsigned SizeDifference =
4851 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4852 if (SizeDifference <= MinShAmt) {
4853 Tmp = SizeDifference +
4854 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4855 if (MaxShAmt < Tmp)
4856 return Tmp - MaxShAmt;
4857 }
4858 }
4859 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4860 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4861 if (MaxShAmt < Tmp)
4862 return Tmp - MaxShAmt;
4863 }
4864 break;
4865 case ISD::AND:
4866 case ISD::OR:
4867 case ISD::XOR: // NOT is handled here.
4868 // Logical binary ops preserve the number of sign bits at the worst.
4869 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4870 if (Tmp != 1) {
4871 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4872 FirstAnswer = std::min(Tmp, Tmp2);
4873 // We computed what we know about the sign bits as our first
4874 // answer. Now proceed to the generic code that uses
4875 // computeKnownBits, and pick whichever answer is better.
4876 }
4877 break;
4878
4879 case ISD::SELECT:
4880 case ISD::VSELECT:
4881 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4882 if (Tmp == 1) return 1; // Early out.
4883 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4884 return std::min(Tmp, Tmp2);
4885 case ISD::SELECT_CC:
4886 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4887 if (Tmp == 1) return 1; // Early out.
4888 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4889 return std::min(Tmp, Tmp2);
4890
4891 case ISD::SMIN:
4892 case ISD::SMAX: {
4893 // If we have a clamp pattern, we know that the number of sign bits will be
4894 // the minimum of the clamp min/max range.
4895 bool IsMax = (Opcode == ISD::SMAX);
4896 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4897 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4898 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4899 CstHigh =
4900 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4901 if (CstLow && CstHigh) {
4902 if (!IsMax)
4903 std::swap(CstLow, CstHigh);
4904 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4905 Tmp = CstLow->getAPIntValue().getNumSignBits();
4906 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4907 return std::min(Tmp, Tmp2);
4908 }
4909 }
4910
4911 // Fallback - just get the minimum number of sign bits of the operands.
4912 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4913 if (Tmp == 1)
4914 return 1; // Early out.
4915 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4916 return std::min(Tmp, Tmp2);
4917 }
4918 case ISD::UMIN:
4919 case ISD::UMAX:
4920 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4921 if (Tmp == 1)
4922 return 1; // Early out.
4923 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4924 return std::min(Tmp, Tmp2);
4925 case ISD::SSUBO_CARRY:
4926 case ISD::USUBO_CARRY:
4927 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4928 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4929 return VTBits;
4930 [[fallthrough]];
4931 case ISD::SADDO:
4932 case ISD::UADDO:
4933 case ISD::SADDO_CARRY:
4934 case ISD::UADDO_CARRY:
4935 case ISD::SSUBO:
4936 case ISD::USUBO:
4937 case ISD::SMULO:
4938 case ISD::UMULO:
4939 if (Op.getResNo() != 1)
4940 break;
4941 // The boolean result conforms to getBooleanContents. Fall through.
4942 // If setcc returns 0/-1, all bits are sign bits.
4943 // We know that we have an integer-based boolean since these operations
4944 // are only available for integer.
4945 if (TLI->getBooleanContents(VT.isVector(), false) ==
4947 return VTBits;
4948 break;
4949 case ISD::SETCC:
4950 case ISD::SETCCCARRY:
4951 case ISD::STRICT_FSETCC:
4952 case ISD::STRICT_FSETCCS: {
4953 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
4954 // If setcc returns 0/-1, all bits are sign bits.
4955 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
4957 return VTBits;
4958 break;
4959 }
4960 case ISD::ROTL:
4961 case ISD::ROTR:
4962 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4963
4964 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
4965 if (Tmp == VTBits)
4966 return VTBits;
4967
4968 if (ConstantSDNode *C =
4969 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
4970 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
4971
4972 // Handle rotate right by N like a rotate left by 32-N.
4973 if (Opcode == ISD::ROTR)
4974 RotAmt = (VTBits - RotAmt) % VTBits;
4975
4976 // If we aren't rotating out all of the known-in sign bits, return the
4977 // number that are left. This handles rotl(sext(x), 1) for example.
4978 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
4979 }
4980 break;
4981 case ISD::ADD:
4982 case ISD::ADDC:
4983 // TODO: Move Operand 1 check before Operand 0 check
4984 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4985 if (Tmp == 1) return 1; // Early out.
4986
4987 // Special case decrementing a value (ADD X, -1):
4988 if (ConstantSDNode *CRHS =
4989 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
4990 if (CRHS->isAllOnes()) {
4991 KnownBits Known =
4992 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4993
4994 // If the input is known to be 0 or 1, the output is 0/-1, which is all
4995 // sign bits set.
4996 if ((Known.Zero | 1).isAllOnes())
4997 return VTBits;
4998
4999 // If we are subtracting one from a positive number, there is no carry
5000 // out of the result.
5001 if (Known.isNonNegative())
5002 return Tmp;
5003 }
5004
5005 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5006 if (Tmp2 == 1) return 1; // Early out.
5007
5008 // Add can have at most one carry bit. Thus we know that the output
5009 // is, at worst, one more bit than the inputs.
5010 return std::min(Tmp, Tmp2) - 1;
5011 case ISD::SUB:
5012 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5013 if (Tmp2 == 1) return 1; // Early out.
5014
5015 // Handle NEG.
5016 if (ConstantSDNode *CLHS =
5017 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5018 if (CLHS->isZero()) {
5019 KnownBits Known =
5020 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5021 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5022 // sign bits set.
5023 if ((Known.Zero | 1).isAllOnes())
5024 return VTBits;
5025
5026 // If the input is known to be positive (the sign bit is known clear),
5027 // the output of the NEG has the same number of sign bits as the input.
5028 if (Known.isNonNegative())
5029 return Tmp2;
5030
5031 // Otherwise, we treat this like a SUB.
5032 }
5033
5034 // Sub can have at most one carry bit. Thus we know that the output
5035 // is, at worst, one more bit than the inputs.
5036 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5037 if (Tmp == 1) return 1; // Early out.
5038 return std::min(Tmp, Tmp2) - 1;
5039 case ISD::MUL: {
5040 // The output of the Mul can be at most twice the valid bits in the inputs.
5041 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5042 if (SignBitsOp0 == 1)
5043 break;
5044 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5045 if (SignBitsOp1 == 1)
5046 break;
5047 unsigned OutValidBits =
5048 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5049 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5050 }
5051 case ISD::AVGCEILS:
5052 case ISD::AVGFLOORS:
5053 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5054 if (Tmp == 1)
5055 return 1; // Early out.
5056 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5057 return std::min(Tmp, Tmp2);
5058 case ISD::SREM:
5059 // The sign bit is the LHS's sign bit, except when the result of the
5060 // remainder is zero. The magnitude of the result should be less than or
5061 // equal to the magnitude of the LHS. Therefore, the result should have
5062 // at least as many sign bits as the left hand side.
5063 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5064 case ISD::TRUNCATE: {
5065 // Check if the sign bits of source go down as far as the truncated value.
5066 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5067 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5068 if (NumSrcSignBits > (NumSrcBits - VTBits))
5069 return NumSrcSignBits - (NumSrcBits - VTBits);
5070 break;
5071 }
5072 case ISD::EXTRACT_ELEMENT: {
5073 if (VT.isScalableVector())
5074 break;
5075 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5076 const int BitWidth = Op.getValueSizeInBits();
5077 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5078
5079 // Get reverse index (starting from 1), Op1 value indexes elements from
5080 // little end. Sign starts at big end.
5081 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5082
5083 // If the sign portion ends in our element the subtraction gives correct
5084 // result. Otherwise it gives either negative or > bitwidth result
5085 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5086 }
5088 if (VT.isScalableVector())
5089 break;
5090 // If we know the element index, split the demand between the
5091 // source vector and the inserted element, otherwise assume we need
5092 // the original demanded vector elements and the value.
5093 SDValue InVec = Op.getOperand(0);
5094 SDValue InVal = Op.getOperand(1);
5095 SDValue EltNo = Op.getOperand(2);
5096 bool DemandedVal = true;
5097 APInt DemandedVecElts = DemandedElts;
5098 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5099 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5100 unsigned EltIdx = CEltNo->getZExtValue();
5101 DemandedVal = !!DemandedElts[EltIdx];
5102 DemandedVecElts.clearBit(EltIdx);
5103 }
5104 Tmp = std::numeric_limits<unsigned>::max();
5105 if (DemandedVal) {
5106 // TODO - handle implicit truncation of inserted elements.
5107 if (InVal.getScalarValueSizeInBits() != VTBits)
5108 break;
5109 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5110 Tmp = std::min(Tmp, Tmp2);
5111 }
5112 if (!!DemandedVecElts) {
5113 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5114 Tmp = std::min(Tmp, Tmp2);
5115 }
5116 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5117 return Tmp;
5118 }
5120 assert(!VT.isScalableVector());
5121 SDValue InVec = Op.getOperand(0);
5122 SDValue EltNo = Op.getOperand(1);
5123 EVT VecVT = InVec.getValueType();
5124 // ComputeNumSignBits not yet implemented for scalable vectors.
5125 if (VecVT.isScalableVector())
5126 break;
5127 const unsigned BitWidth = Op.getValueSizeInBits();
5128 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5129 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5130
5131 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5132 // anything about sign bits. But if the sizes match we can derive knowledge
5133 // about sign bits from the vector operand.
5134 if (BitWidth != EltBitWidth)
5135 break;
5136
5137 // If we know the element index, just demand that vector element, else for
5138 // an unknown element index, ignore DemandedElts and demand them all.
5139 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5140 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5141 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5142 DemandedSrcElts =
5143 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5144
5145 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5146 }
5148 // Offset the demanded elts by the subvector index.
5149 SDValue Src = Op.getOperand(0);
5150 // Bail until we can represent demanded elements for scalable vectors.
5151 if (Src.getValueType().isScalableVector())
5152 break;
5153 uint64_t Idx = Op.getConstantOperandVal(1);
5154 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5155 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5156 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5157 }
5158 case ISD::CONCAT_VECTORS: {
5159 if (VT.isScalableVector())
5160 break;
5161 // Determine the minimum number of sign bits across all demanded
5162 // elts of the input vectors. Early out if the result is already 1.
5163 Tmp = std::numeric_limits<unsigned>::max();
5164 EVT SubVectorVT = Op.getOperand(0).getValueType();
5165 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5166 unsigned NumSubVectors = Op.getNumOperands();
5167 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5168 APInt DemandedSub =
5169 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5170 if (!DemandedSub)
5171 continue;
5172 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5173 Tmp = std::min(Tmp, Tmp2);
5174 }
5175 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5176 return Tmp;
5177 }
5178 case ISD::INSERT_SUBVECTOR: {
5179 if (VT.isScalableVector())
5180 break;
5181 // Demand any elements from the subvector and the remainder from the src its
5182 // inserted into.
5183 SDValue Src = Op.getOperand(0);
5184 SDValue Sub = Op.getOperand(1);
5185 uint64_t Idx = Op.getConstantOperandVal(2);
5186 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5187 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5188 APInt DemandedSrcElts = DemandedElts;
5189 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5190
5191 Tmp = std::numeric_limits<unsigned>::max();
5192 if (!!DemandedSubElts) {
5193 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5194 if (Tmp == 1)
5195 return 1; // early-out
5196 }
5197 if (!!DemandedSrcElts) {
5198 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5199 Tmp = std::min(Tmp, Tmp2);
5200 }
5201 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5202 return Tmp;
5203 }
5204 case ISD::LOAD: {
5206 if (const MDNode *Ranges = LD->getRanges()) {
5207 if (DemandedElts != 1)
5208 break;
5209
5211 if (VTBits > CR.getBitWidth()) {
5212 switch (LD->getExtensionType()) {
5213 case ISD::SEXTLOAD:
5214 CR = CR.signExtend(VTBits);
5215 break;
5216 case ISD::ZEXTLOAD:
5217 CR = CR.zeroExtend(VTBits);
5218 break;
5219 default:
5220 break;
5221 }
5222 }
5223
5224 if (VTBits != CR.getBitWidth())
5225 break;
5226 return std::min(CR.getSignedMin().getNumSignBits(),
5228 }
5229
5230 break;
5231 }
5234 case ISD::ATOMIC_SWAP:
5246 case ISD::ATOMIC_LOAD: {
5247 auto *AT = cast<AtomicSDNode>(Op);
5248 // If we are looking at the loaded value.
5249 if (Op.getResNo() == 0) {
5250 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5251 if (Tmp == VTBits)
5252 return 1; // early-out
5253
5254 // For atomic_load, prefer to use the extension type.
5255 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5256 switch (AT->getExtensionType()) {
5257 default:
5258 break;
5259 case ISD::SEXTLOAD:
5260 return VTBits - Tmp + 1;
5261 case ISD::ZEXTLOAD:
5262 return VTBits - Tmp;
5263 }
5264 }
5265
5266 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5267 return VTBits - Tmp + 1;
5268 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5269 return VTBits - Tmp;
5270 }
5271 break;
5272 }
5273 }
5274
5275 // If we are looking at the loaded value of the SDNode.
5276 if (Op.getResNo() == 0) {
5277 // Handle LOADX separately here. EXTLOAD case will fallthrough.
5278 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
5279 unsigned ExtType = LD->getExtensionType();
5280 switch (ExtType) {
5281 default: break;
5282 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5283 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5284 return VTBits - Tmp + 1;
5285 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5286 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5287 return VTBits - Tmp;
5288 case ISD::NON_EXTLOAD:
5289 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5290 // We only need to handle vectors - computeKnownBits should handle
5291 // scalar cases.
5292 Type *CstTy = Cst->getType();
5293 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5294 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5295 VTBits == CstTy->getScalarSizeInBits()) {
5296 Tmp = VTBits;
5297 for (unsigned i = 0; i != NumElts; ++i) {
5298 if (!DemandedElts[i])
5299 continue;
5300 if (Constant *Elt = Cst->getAggregateElement(i)) {
5301 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5302 const APInt &Value = CInt->getValue();
5303 Tmp = std::min(Tmp, Value.getNumSignBits());
5304 continue;
5305 }
5306 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5307 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5308 Tmp = std::min(Tmp, Value.getNumSignBits());
5309 continue;
5310 }
5311 }
5312 // Unknown type. Conservatively assume no bits match sign bit.
5313 return 1;
5314 }
5315 return Tmp;
5316 }
5317 }
5318 break;
5319 }
5320 }
5321 }
5322
5323 // Allow the target to implement this method for its nodes.
5324 if (Opcode >= ISD::BUILTIN_OP_END ||
5325 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5326 Opcode == ISD::INTRINSIC_W_CHAIN ||
5327 Opcode == ISD::INTRINSIC_VOID) {
5328 // TODO: This can probably be removed once target code is audited. This
5329 // is here purely to reduce patch size and review complexity.
5330 if (!VT.isScalableVector()) {
5331 unsigned NumBits =
5332 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5333 if (NumBits > 1)
5334 FirstAnswer = std::max(FirstAnswer, NumBits);
5335 }
5336 }
5337
5338 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5339 // use this information.
5340 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5341 return std::max(FirstAnswer, Known.countMinSignBits());
5342}
5343
5345 unsigned Depth) const {
5346 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5347 return Op.getScalarValueSizeInBits() - SignBits + 1;
5348}
5349
5351 const APInt &DemandedElts,
5352 unsigned Depth) const {
5353 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5354 return Op.getScalarValueSizeInBits() - SignBits + 1;
5355}
5356
5358 unsigned Depth) const {
5359 // Early out for FREEZE.
5360 if (Op.getOpcode() == ISD::FREEZE)
5361 return true;
5362
5363 EVT VT = Op.getValueType();
5364 APInt DemandedElts = VT.isFixedLengthVector()
5366 : APInt(1, 1);
5367 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5368}
5369
5371 const APInt &DemandedElts,
5372 bool PoisonOnly,
5373 unsigned Depth) const {
5374 unsigned Opcode = Op.getOpcode();
5375
5376 // Early out for FREEZE.
5377 if (Opcode == ISD::FREEZE)
5378 return true;
5379
5380 if (Depth >= MaxRecursionDepth)
5381 return false; // Limit search depth.
5382
5383 if (isIntOrFPConstant(Op))
5384 return true;
5385
5386 switch (Opcode) {
5387 case ISD::CONDCODE:
5388 case ISD::VALUETYPE:
5389 case ISD::FrameIndex:
5391 case ISD::CopyFromReg:
5392 return true;
5393
5394 case ISD::POISON:
5395 return false;
5396
5397 case ISD::UNDEF:
5398 return PoisonOnly;
5399
5400 case ISD::BUILD_VECTOR:
5401 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5402 // this shouldn't affect the result.
5403 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5404 if (!DemandedElts[i])
5405 continue;
5407 Depth + 1))
5408 return false;
5409 }
5410 return true;
5411
5413 SDValue Src = Op.getOperand(0);
5414 if (Src.getValueType().isScalableVector())
5415 break;
5416 uint64_t Idx = Op.getConstantOperandVal(1);
5417 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5418 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5419 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5420 Depth + 1);
5421 }
5422
5423 case ISD::INSERT_SUBVECTOR: {
5424 if (Op.getValueType().isScalableVector())
5425 break;
5426 SDValue Src = Op.getOperand(0);
5427 SDValue Sub = Op.getOperand(1);
5428 uint64_t Idx = Op.getConstantOperandVal(2);
5429 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5430 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5431 APInt DemandedSrcElts = DemandedElts;
5432 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5433
5434 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5435 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5436 return false;
5437 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5438 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5439 return false;
5440 return true;
5441 }
5442
5444 SDValue Src = Op.getOperand(0);
5445 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5446 EVT SrcVT = Src.getValueType();
5447 if (SrcVT.isFixedLengthVector() && IndexC &&
5448 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5449 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5450 IndexC->getZExtValue());
5451 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5452 Depth + 1);
5453 }
5454 break;
5455 }
5456
5458 SDValue InVec = Op.getOperand(0);
5459 SDValue InVal = Op.getOperand(1);
5460 SDValue EltNo = Op.getOperand(2);
5461 EVT VT = InVec.getValueType();
5462 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5463 if (IndexC && VT.isFixedLengthVector() &&
5464 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5465 if (DemandedElts[IndexC->getZExtValue()] &&
5467 return false;
5468 APInt InVecDemandedElts = DemandedElts;
5469 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5470 if (!!InVecDemandedElts &&
5472 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5473 InVecDemandedElts, PoisonOnly, Depth + 1))
5474 return false;
5475 return true;
5476 }
5477 break;
5478 }
5479
5481 // Check upper (known undef) elements.
5482 if (DemandedElts.ugt(1) && !PoisonOnly)
5483 return false;
5484 // Check element zero.
5485 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5486 Op.getOperand(0), PoisonOnly, Depth + 1))
5487 return false;
5488 return true;
5489
5490 case ISD::SPLAT_VECTOR:
5491 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5492 Depth + 1);
5493
5494 case ISD::VECTOR_SHUFFLE: {
5495 APInt DemandedLHS, DemandedRHS;
5496 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5497 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5498 DemandedElts, DemandedLHS, DemandedRHS,
5499 /*AllowUndefElts=*/false))
5500 return false;
5501 if (!DemandedLHS.isZero() &&
5502 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5503 PoisonOnly, Depth + 1))
5504 return false;
5505 if (!DemandedRHS.isZero() &&
5506 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5507 PoisonOnly, Depth + 1))
5508 return false;
5509 return true;
5510 }
5511
5512 case ISD::SHL:
5513 case ISD::SRL:
5514 case ISD::SRA:
5515 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5516 // enough to check operand 0 if Op can't create undef/poison.
5517 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5518 /*ConsiderFlags*/ true, Depth) &&
5519 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5520 PoisonOnly, Depth + 1);
5521
5522 case ISD::BSWAP:
5523 case ISD::CTPOP:
5524 case ISD::BITREVERSE:
5525 case ISD::AND:
5526 case ISD::OR:
5527 case ISD::XOR:
5528 case ISD::ADD:
5529 case ISD::SUB:
5530 case ISD::MUL:
5531 case ISD::SADDSAT:
5532 case ISD::UADDSAT:
5533 case ISD::SSUBSAT:
5534 case ISD::USUBSAT:
5535 case ISD::SSHLSAT:
5536 case ISD::USHLSAT:
5537 case ISD::SMIN:
5538 case ISD::SMAX:
5539 case ISD::UMIN:
5540 case ISD::UMAX:
5541 case ISD::ZERO_EXTEND:
5542 case ISD::SIGN_EXTEND:
5543 case ISD::ANY_EXTEND:
5544 case ISD::TRUNCATE:
5545 case ISD::VSELECT: {
5546 // If Op can't create undef/poison and none of its operands are undef/poison
5547 // then Op is never undef/poison. A difference from the more common check
5548 // below, outside the switch, is that we handle elementwise operations for
5549 // which the DemandedElts mask is valid for all operands here.
5550 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5551 /*ConsiderFlags*/ true, Depth) &&
5552 all_of(Op->ops(), [&](SDValue V) {
5553 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5554 PoisonOnly, Depth + 1);
5555 });
5556 }
5557
5558 // TODO: Search for noundef attributes from library functions.
5559
5560 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5561
5562 default:
5563 // Allow the target to implement this method for its nodes.
5564 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5565 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5566 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5567 Op, DemandedElts, *this, PoisonOnly, Depth);
5568 break;
5569 }
5570
5571 // If Op can't create undef/poison and none of its operands are undef/poison
5572 // then Op is never undef/poison.
5573 // NOTE: TargetNodes can handle this in themselves in
5574 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5575 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5576 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5577 Depth) &&
5578 all_of(Op->ops(), [&](SDValue V) {
5579 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5580 });
5581}
5582
5584 bool ConsiderFlags,
5585 unsigned Depth) const {
5586 EVT VT = Op.getValueType();
5587 APInt DemandedElts = VT.isFixedLengthVector()
5589 : APInt(1, 1);
5590 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5591 Depth);
5592}
5593
5595 bool PoisonOnly, bool ConsiderFlags,
5596 unsigned Depth) const {
5597 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5598 return true;
5599
5600 unsigned Opcode = Op.getOpcode();
5601 switch (Opcode) {
5602 case ISD::AssertSext:
5603 case ISD::AssertZext:
5604 case ISD::AssertAlign:
5606 // Assertion nodes can create poison if the assertion fails.
5607 return true;
5608
5609 case ISD::FREEZE:
5613 case ISD::SADDSAT:
5614 case ISD::UADDSAT:
5615 case ISD::SSUBSAT:
5616 case ISD::USUBSAT:
5617 case ISD::MULHU:
5618 case ISD::MULHS:
5619 case ISD::AVGFLOORS:
5620 case ISD::AVGFLOORU:
5621 case ISD::AVGCEILS:
5622 case ISD::AVGCEILU:
5623 case ISD::ABDU:
5624 case ISD::ABDS:
5625 case ISD::SMIN:
5626 case ISD::SMAX:
5627 case ISD::SCMP:
5628 case ISD::UMIN:
5629 case ISD::UMAX:
5630 case ISD::UCMP:
5631 case ISD::AND:
5632 case ISD::XOR:
5633 case ISD::ROTL:
5634 case ISD::ROTR:
5635 case ISD::FSHL:
5636 case ISD::FSHR:
5637 case ISD::BSWAP:
5638 case ISD::CTTZ:
5639 case ISD::CTLZ:
5640 case ISD::CTPOP:
5641 case ISD::BITREVERSE:
5642 case ISD::PARITY:
5643 case ISD::SIGN_EXTEND:
5644 case ISD::TRUNCATE:
5648 case ISD::BITCAST:
5649 case ISD::BUILD_VECTOR:
5650 case ISD::BUILD_PAIR:
5651 case ISD::SPLAT_VECTOR:
5652 case ISD::FABS:
5653 return false;
5654
5655 case ISD::ABS:
5656 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5657 // Different to Intrinsic::abs.
5658 return false;
5659
5660 case ISD::ADDC:
5661 case ISD::SUBC:
5662 case ISD::ADDE:
5663 case ISD::SUBE:
5664 case ISD::SADDO:
5665 case ISD::SSUBO:
5666 case ISD::SMULO:
5667 case ISD::SADDO_CARRY:
5668 case ISD::SSUBO_CARRY:
5669 case ISD::UADDO:
5670 case ISD::USUBO:
5671 case ISD::UMULO:
5672 case ISD::UADDO_CARRY:
5673 case ISD::USUBO_CARRY:
5674 // No poison on result or overflow flags.
5675 return false;
5676
5677 case ISD::SELECT_CC:
5678 case ISD::SETCC: {
5679 // Integer setcc cannot create undef or poison.
5680 if (Op.getOperand(0).getValueType().isInteger())
5681 return false;
5682
5683 // FP compares are more complicated. They can create poison for nan/infinity
5684 // based on options and flags. The options and flags also cause special
5685 // nonan condition codes to be used. Those condition codes may be preserved
5686 // even if the nonan flag is dropped somewhere.
5687 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5688 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5689 return (unsigned)CCCode & 0x10U;
5690 }
5691
5692 case ISD::OR:
5693 case ISD::ZERO_EXTEND:
5694 case ISD::SELECT:
5695 case ISD::VSELECT:
5696 case ISD::ADD:
5697 case ISD::SUB:
5698 case ISD::MUL:
5699 case ISD::FNEG:
5700 case ISD::FADD:
5701 case ISD::FSUB:
5702 case ISD::FMUL:
5703 case ISD::FDIV:
5704 case ISD::FREM:
5705 case ISD::FCOPYSIGN:
5706 case ISD::FMA:
5707 case ISD::FMAD:
5708 case ISD::FMULADD:
5709 case ISD::FP_EXTEND:
5712 // No poison except from flags (which is handled above)
5713 return false;
5714
5715 case ISD::SHL:
5716 case ISD::SRL:
5717 case ISD::SRA:
5718 // If the max shift amount isn't in range, then the shift can
5719 // create poison.
5720 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5721
5724 // If the amount is zero then the result will be poison.
5725 // TODO: Add isKnownNeverZero DemandedElts handling.
5726 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5727
5729 // Check if we demand any upper (undef) elements.
5730 return !PoisonOnly && DemandedElts.ugt(1);
5731
5734 // Ensure that the element index is in bounds.
5735 EVT VecVT = Op.getOperand(0).getValueType();
5736 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5737 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5738 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5739 }
5740
5741 case ISD::VECTOR_SHUFFLE: {
5742 // Check for any demanded shuffle element that is undef.
5743 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5744 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5745 if (Elt < 0 && DemandedElts[Idx])
5746 return true;
5747 return false;
5748 }
5749
5751 return false;
5752
5753 default:
5754 // Allow the target to implement this method for its nodes.
5755 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5756 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5757 return TLI->canCreateUndefOrPoisonForTargetNode(
5758 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5759 break;
5760 }
5761
5762 // Be conservative and return true.
5763 return true;
5764}
5765
5766bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5767 unsigned Opcode = Op.getOpcode();
5768 if (Opcode == ISD::OR)
5769 return Op->getFlags().hasDisjoint() ||
5770 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5771 if (Opcode == ISD::XOR)
5772 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5773 return false;
5774}
5775
5777 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5778 (Op.isAnyAdd() || isADDLike(Op));
5779}
5780
5782 unsigned Depth) const {
5783 EVT VT = Op.getValueType();
5784
5785 // Since the number of lanes in a scalable vector is unknown at compile time,
5786 // we track one bit which is implicitly broadcast to all lanes. This means
5787 // that all lanes in a scalable vector are considered demanded.
5788 APInt DemandedElts = VT.isFixedLengthVector()
5790 : APInt(1, 1);
5791
5792 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5793}
5794
5796 bool SNaN, unsigned Depth) const {
5797 assert(!DemandedElts.isZero() && "No demanded elements");
5798
5799 // If we're told that NaNs won't happen, assume they won't.
5800 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5801 return true;
5802
5803 if (Depth >= MaxRecursionDepth)
5804 return false; // Limit search depth.
5805
5806 // If the value is a constant, we can obviously see if it is a NaN or not.
5808 return !C->getValueAPF().isNaN() ||
5809 (SNaN && !C->getValueAPF().isSignaling());
5810 }
5811
5812 unsigned Opcode = Op.getOpcode();
5813 switch (Opcode) {
5814 case ISD::FADD:
5815 case ISD::FSUB:
5816 case ISD::FMUL:
5817 case ISD::FDIV:
5818 case ISD::FREM:
5819 case ISD::FSIN:
5820 case ISD::FCOS:
5821 case ISD::FTAN:
5822 case ISD::FASIN:
5823 case ISD::FACOS:
5824 case ISD::FATAN:
5825 case ISD::FATAN2:
5826 case ISD::FSINH:
5827 case ISD::FCOSH:
5828 case ISD::FTANH:
5829 case ISD::FMA:
5830 case ISD::FMULADD:
5831 case ISD::FMAD: {
5832 if (SNaN)
5833 return true;
5834 // TODO: Need isKnownNeverInfinity
5835 return false;
5836 }
5837 case ISD::FCANONICALIZE:
5838 case ISD::FEXP:
5839 case ISD::FEXP2:
5840 case ISD::FEXP10:
5841 case ISD::FTRUNC:
5842 case ISD::FFLOOR:
5843 case ISD::FCEIL:
5844 case ISD::FROUND:
5845 case ISD::FROUNDEVEN:
5846 case ISD::LROUND:
5847 case ISD::LLROUND:
5848 case ISD::FRINT:
5849 case ISD::LRINT:
5850 case ISD::LLRINT:
5851 case ISD::FNEARBYINT:
5852 case ISD::FLDEXP: {
5853 if (SNaN)
5854 return true;
5855 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5856 }
5857 case ISD::FABS:
5858 case ISD::FNEG:
5859 case ISD::FCOPYSIGN: {
5860 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5861 }
5862 case ISD::SELECT:
5863 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5864 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5865 case ISD::FP_EXTEND:
5866 case ISD::FP_ROUND: {
5867 if (SNaN)
5868 return true;
5869 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5870 }
5871 case ISD::SINT_TO_FP:
5872 case ISD::UINT_TO_FP:
5873 return true;
5874 case ISD::FSQRT: // Need is known positive
5875 case ISD::FLOG:
5876 case ISD::FLOG2:
5877 case ISD::FLOG10:
5878 case ISD::FPOWI:
5879 case ISD::FPOW: {
5880 if (SNaN)
5881 return true;
5882 // TODO: Refine on operand
5883 return false;
5884 }
5885 case ISD::FMINNUM:
5886 case ISD::FMAXNUM:
5887 case ISD::FMINIMUMNUM:
5888 case ISD::FMAXIMUMNUM: {
5889 // Only one needs to be known not-nan, since it will be returned if the
5890 // other ends up being one.
5891 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5892 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5893 }
5894 case ISD::FMINNUM_IEEE:
5895 case ISD::FMAXNUM_IEEE: {
5896 if (SNaN)
5897 return true;
5898 // This can return a NaN if either operand is an sNaN, or if both operands
5899 // are NaN.
5900 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5901 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5902 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5903 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5904 }
5905 case ISD::FMINIMUM:
5906 case ISD::FMAXIMUM: {
5907 // TODO: Does this quiet or return the origina NaN as-is?
5908 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5909 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5910 }
5912 SDValue Src = Op.getOperand(0);
5913 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5914 EVT SrcVT = Src.getValueType();
5915 if (SrcVT.isFixedLengthVector() && Idx &&
5916 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5917 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5918 Idx->getZExtValue());
5919 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5920 }
5921 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5922 }
5924 SDValue Src = Op.getOperand(0);
5925 if (Src.getValueType().isFixedLengthVector()) {
5926 unsigned Idx = Op.getConstantOperandVal(1);
5927 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5928 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5929 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5930 }
5931 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5932 }
5933 case ISD::INSERT_SUBVECTOR: {
5934 SDValue BaseVector = Op.getOperand(0);
5935 SDValue SubVector = Op.getOperand(1);
5936 EVT BaseVectorVT = BaseVector.getValueType();
5937 if (BaseVectorVT.isFixedLengthVector()) {
5938 unsigned Idx = Op.getConstantOperandVal(2);
5939 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
5940 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
5941
5942 // Clear/Extract the bits at the position where the subvector will be
5943 // inserted.
5944 APInt DemandedMask =
5945 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
5946 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
5947 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5948
5949 bool NeverNaN = true;
5950 if (!DemandedSrcElts.isZero())
5951 NeverNaN &=
5952 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
5953 if (NeverNaN && !DemandedSubElts.isZero())
5954 NeverNaN &=
5955 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
5956 return NeverNaN;
5957 }
5958 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
5959 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
5960 }
5961 case ISD::BUILD_VECTOR: {
5962 unsigned NumElts = Op.getNumOperands();
5963 for (unsigned I = 0; I != NumElts; ++I)
5964 if (DemandedElts[I] &&
5965 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
5966 return false;
5967 return true;
5968 }
5969 case ISD::AssertNoFPClass: {
5970 FPClassTest NoFPClass =
5971 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
5972 if ((NoFPClass & fcNan) == fcNan)
5973 return true;
5974 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
5975 return true;
5976 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5977 }
5978 default:
5979 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5980 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
5981 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
5982 Depth);
5983 }
5984
5985 return false;
5986 }
5987}
5988
5990 assert(Op.getValueType().isFloatingPoint() &&
5991 "Floating point type expected");
5992
5993 // If the value is a constant, we can obviously see if it is a zero or not.
5995 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
5996}
5997
5999 if (Depth >= MaxRecursionDepth)
6000 return false; // Limit search depth.
6001
6002 assert(!Op.getValueType().isFloatingPoint() &&
6003 "Floating point types unsupported - use isKnownNeverZeroFloat");
6004
6005 // If the value is a constant, we can obviously see if it is a zero or not.
6007 [](ConstantSDNode *C) { return !C->isZero(); }))
6008 return true;
6009
6010 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6011 // some degree.
6012 switch (Op.getOpcode()) {
6013 default:
6014 break;
6015
6016 case ISD::OR:
6017 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6018 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6019
6020 case ISD::VSELECT:
6021 case ISD::SELECT:
6022 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6023 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6024
6025 case ISD::SHL: {
6026 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6027 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6028 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6029 // 1 << X is never zero.
6030 if (ValKnown.One[0])
6031 return true;
6032 // If max shift cnt of known ones is non-zero, result is non-zero.
6033 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6034 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6035 !ValKnown.One.shl(MaxCnt).isZero())
6036 return true;
6037 break;
6038 }
6039 case ISD::UADDSAT:
6040 case ISD::UMAX:
6041 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6042 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6043
6044 // For smin/smax: If either operand is known negative/positive
6045 // respectively we don't need the other to be known at all.
6046 case ISD::SMAX: {
6047 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6048 if (Op1.isStrictlyPositive())
6049 return true;
6050
6051 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6052 if (Op0.isStrictlyPositive())
6053 return true;
6054
6055 if (Op1.isNonZero() && Op0.isNonZero())
6056 return true;
6057
6058 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6059 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6060 }
6061 case ISD::SMIN: {
6062 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6063 if (Op1.isNegative())
6064 return true;
6065
6066 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6067 if (Op0.isNegative())
6068 return true;
6069
6070 if (Op1.isNonZero() && Op0.isNonZero())
6071 return true;
6072
6073 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6074 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6075 }
6076 case ISD::UMIN:
6077 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6078 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6079
6080 case ISD::ROTL:
6081 case ISD::ROTR:
6082 case ISD::BITREVERSE:
6083 case ISD::BSWAP:
6084 case ISD::CTPOP:
6085 case ISD::ABS:
6086 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6087
6088 case ISD::SRA:
6089 case ISD::SRL: {
6090 if (Op->getFlags().hasExact())
6091 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6092 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6093 if (ValKnown.isNegative())
6094 return true;
6095 // If max shift cnt of known ones is non-zero, result is non-zero.
6096 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6097 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6098 !ValKnown.One.lshr(MaxCnt).isZero())
6099 return true;
6100 break;
6101 }
6102 case ISD::UDIV:
6103 case ISD::SDIV:
6104 // div exact can only produce a zero if the dividend is zero.
6105 // TODO: For udiv this is also true if Op1 u<= Op0
6106 if (Op->getFlags().hasExact())
6107 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6108 break;
6109
6110 case ISD::ADD:
6111 if (Op->getFlags().hasNoUnsignedWrap())
6112 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6113 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6114 return true;
6115 // TODO: There are a lot more cases we can prove for add.
6116 break;
6117
6118 case ISD::SUB: {
6119 if (isNullConstant(Op.getOperand(0)))
6120 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6121
6122 std::optional<bool> ne =
6123 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6124 computeKnownBits(Op.getOperand(1), Depth + 1));
6125 return ne && *ne;
6126 }
6127
6128 case ISD::MUL:
6129 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6130 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6131 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6132 return true;
6133 break;
6134
6135 case ISD::ZERO_EXTEND:
6136 case ISD::SIGN_EXTEND:
6137 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6138 case ISD::VSCALE: {
6140 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6141 ConstantRange CR =
6142 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6143 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6144 return true;
6145 break;
6146 }
6147 }
6148
6150}
6151
6153 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6154 return !C1->isNegative();
6155
6156 switch (Op.getOpcode()) {
6157 case ISD::FABS:
6158 case ISD::FEXP:
6159 case ISD::FEXP2:
6160 case ISD::FEXP10:
6161 return true;
6162 default:
6163 return false;
6164 }
6165
6166 llvm_unreachable("covered opcode switch");
6167}
6168
6170 assert(Use.getValueType().isFloatingPoint());
6171 const SDNode *User = Use.getUser();
6172 unsigned OperandNo = Use.getOperandNo();
6173 // Check if this use is insensitive to the sign of zero
6174 switch (User->getOpcode()) {
6175 case ISD::SETCC:
6176 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6177 case ISD::FABS:
6178 // fabs always produces +0.0.
6179 return true;
6180 case ISD::FCOPYSIGN:
6181 // copysign overwrites the sign bit of the first operand.
6182 return OperandNo == 0;
6183 case ISD::FADD:
6184 case ISD::FSUB: {
6185 // Arithmetic with non-zero constants fixes the uncertainty around the
6186 // sign bit.
6187 SDValue Other = User->getOperand(1 - OperandNo);
6189 }
6190 case ISD::FP_TO_SINT:
6191 case ISD::FP_TO_UINT:
6192 // fp-to-int conversions normalize signed zeros.
6193 return true;
6194 default:
6195 return false;
6196 }
6197}
6198
6200 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6201 // regression. Ideally, this should be implemented as a demanded-bits
6202 // optimization that stems from the users.
6203 if (Op->use_size() > 2)
6204 return false;
6205 return all_of(Op->uses(),
6206 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6207}
6208
6210 // Check the obvious case.
6211 if (A == B) return true;
6212
6213 // For negative and positive zero.
6216 if (CA->isZero() && CB->isZero()) return true;
6217
6218 // Otherwise they may not be equal.
6219 return false;
6220}
6221
6222// Only bits set in Mask must be negated, other bits may be arbitrary.
6224 if (isBitwiseNot(V, AllowUndefs))
6225 return V.getOperand(0);
6226
6227 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6228 // bits in the non-extended part.
6229 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6230 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6231 return SDValue();
6232 SDValue ExtArg = V.getOperand(0);
6233 if (ExtArg.getScalarValueSizeInBits() >=
6234 MaskC->getAPIntValue().getActiveBits() &&
6235 isBitwiseNot(ExtArg, AllowUndefs) &&
6236 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6237 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6238 return ExtArg.getOperand(0).getOperand(0);
6239 return SDValue();
6240}
6241
6243 // Match masked merge pattern (X & ~M) op (Y & M)
6244 // Including degenerate case (X & ~M) op M
6245 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6246 SDValue Other) {
6247 if (SDValue NotOperand =
6248 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6249 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6250 NotOperand->getOpcode() == ISD::TRUNCATE)
6251 NotOperand = NotOperand->getOperand(0);
6252
6253 if (Other == NotOperand)
6254 return true;
6255 if (Other->getOpcode() == ISD::AND)
6256 return NotOperand == Other->getOperand(0) ||
6257 NotOperand == Other->getOperand(1);
6258 }
6259 return false;
6260 };
6261
6262 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6263 A = A->getOperand(0);
6264
6265 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6266 B = B->getOperand(0);
6267
6268 if (A->getOpcode() == ISD::AND)
6269 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6270 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6271 return false;
6272}
6273
6274// FIXME: unify with llvm::haveNoCommonBitsSet.
6276 assert(A.getValueType() == B.getValueType() &&
6277 "Values must have the same type");
6280 return true;
6283}
6284
6285static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6286 SelectionDAG &DAG) {
6287 if (cast<ConstantSDNode>(Step)->isZero())
6288 return DAG.getConstant(0, DL, VT);
6289
6290 return SDValue();
6291}
6292
6295 SelectionDAG &DAG) {
6296 int NumOps = Ops.size();
6297 assert(NumOps != 0 && "Can't build an empty vector!");
6298 assert(!VT.isScalableVector() &&
6299 "BUILD_VECTOR cannot be used with scalable types");
6300 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6301 "Incorrect element count in BUILD_VECTOR!");
6302
6303 // BUILD_VECTOR of UNDEFs is UNDEF.
6304 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6305 return DAG.getUNDEF(VT);
6306
6307 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6308 SDValue IdentitySrc;
6309 bool IsIdentity = true;
6310 for (int i = 0; i != NumOps; ++i) {
6312 Ops[i].getOperand(0).getValueType() != VT ||
6313 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6314 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6315 Ops[i].getConstantOperandAPInt(1) != i) {
6316 IsIdentity = false;
6317 break;
6318 }
6319 IdentitySrc = Ops[i].getOperand(0);
6320 }
6321 if (IsIdentity)
6322 return IdentitySrc;
6323
6324 return SDValue();
6325}
6326
6327/// Try to simplify vector concatenation to an input value, undef, or build
6328/// vector.
6331 SelectionDAG &DAG) {
6332 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6334 [Ops](SDValue Op) {
6335 return Ops[0].getValueType() == Op.getValueType();
6336 }) &&
6337 "Concatenation of vectors with inconsistent value types!");
6338 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6339 VT.getVectorElementCount() &&
6340 "Incorrect element count in vector concatenation!");
6341
6342 if (Ops.size() == 1)
6343 return Ops[0];
6344
6345 // Concat of UNDEFs is UNDEF.
6346 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6347 return DAG.getUNDEF(VT);
6348
6349 // Scan the operands and look for extract operations from a single source
6350 // that correspond to insertion at the same location via this concatenation:
6351 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6352 SDValue IdentitySrc;
6353 bool IsIdentity = true;
6354 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6355 SDValue Op = Ops[i];
6356 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6357 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6358 Op.getOperand(0).getValueType() != VT ||
6359 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6360 Op.getConstantOperandVal(1) != IdentityIndex) {
6361 IsIdentity = false;
6362 break;
6363 }
6364 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6365 "Unexpected identity source vector for concat of extracts");
6366 IdentitySrc = Op.getOperand(0);
6367 }
6368 if (IsIdentity) {
6369 assert(IdentitySrc && "Failed to set source vector of extracts");
6370 return IdentitySrc;
6371 }
6372
6373 // The code below this point is only designed to work for fixed width
6374 // vectors, so we bail out for now.
6375 if (VT.isScalableVector())
6376 return SDValue();
6377
6378 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6379 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6380 // BUILD_VECTOR.
6381 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6382 EVT SVT = VT.getScalarType();
6384 for (SDValue Op : Ops) {
6385 EVT OpVT = Op.getValueType();
6386 if (Op.isUndef())
6387 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6388 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6389 Elts.append(Op->op_begin(), Op->op_end());
6390 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6391 OpVT.getVectorNumElements() == 1 &&
6392 isNullConstant(Op.getOperand(2)))
6393 Elts.push_back(Op.getOperand(1));
6394 else
6395 return SDValue();
6396 }
6397
6398 // BUILD_VECTOR requires all inputs to be of the same type, find the
6399 // maximum type and extend them all.
6400 for (SDValue Op : Elts)
6401 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6402
6403 if (SVT.bitsGT(VT.getScalarType())) {
6404 for (SDValue &Op : Elts) {
6405 if (Op.isUndef())
6406 Op = DAG.getUNDEF(SVT);
6407 else
6408 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6409 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6410 : DAG.getSExtOrTrunc(Op, DL, SVT);
6411 }
6412 }
6413
6414 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6415 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6416 return V;
6417}
6418
6419/// Gets or creates the specified node.
6420SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6421 SDVTList VTs = getVTList(VT);
6423 AddNodeIDNode(ID, Opcode, VTs, {});
6424 void *IP = nullptr;
6425 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6426 return SDValue(E, 0);
6427
6428 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6429 CSEMap.InsertNode(N, IP);
6430
6431 InsertNode(N);
6432 SDValue V = SDValue(N, 0);
6433 NewSDValueDbgMsg(V, "Creating new node: ", this);
6434 return V;
6435}
6436
6437SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6438 SDValue N1) {
6439 SDNodeFlags Flags;
6440 if (Inserter)
6441 Flags = Inserter->getFlags();
6442 return getNode(Opcode, DL, VT, N1, Flags);
6443}
6444
6445SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6446 SDValue N1, const SDNodeFlags Flags) {
6447 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6448
6449 // Constant fold unary operations with a vector integer or float operand.
6450 switch (Opcode) {
6451 default:
6452 // FIXME: Entirely reasonable to perform folding of other unary
6453 // operations here as the need arises.
6454 break;
6455 case ISD::FNEG:
6456 case ISD::FABS:
6457 case ISD::FCEIL:
6458 case ISD::FTRUNC:
6459 case ISD::FFLOOR:
6460 case ISD::FP_EXTEND:
6461 case ISD::FP_TO_SINT:
6462 case ISD::FP_TO_UINT:
6463 case ISD::FP_TO_FP16:
6464 case ISD::FP_TO_BF16:
6465 case ISD::TRUNCATE:
6466 case ISD::ANY_EXTEND:
6467 case ISD::ZERO_EXTEND:
6468 case ISD::SIGN_EXTEND:
6469 case ISD::UINT_TO_FP:
6470 case ISD::SINT_TO_FP:
6471 case ISD::FP16_TO_FP:
6472 case ISD::BF16_TO_FP:
6473 case ISD::BITCAST:
6474 case ISD::ABS:
6475 case ISD::BITREVERSE:
6476 case ISD::BSWAP:
6477 case ISD::CTLZ:
6479 case ISD::CTTZ:
6481 case ISD::CTPOP:
6482 case ISD::STEP_VECTOR: {
6483 SDValue Ops = {N1};
6484 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6485 return Fold;
6486 }
6487 }
6488
6489 unsigned OpOpcode = N1.getNode()->getOpcode();
6490 switch (Opcode) {
6491 case ISD::STEP_VECTOR:
6492 assert(VT.isScalableVector() &&
6493 "STEP_VECTOR can only be used with scalable types");
6494 assert(OpOpcode == ISD::TargetConstant &&
6495 VT.getVectorElementType() == N1.getValueType() &&
6496 "Unexpected step operand");
6497 break;
6498 case ISD::FREEZE:
6499 assert(VT == N1.getValueType() && "Unexpected VT!");
6500 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6501 return N1;
6502 break;
6503 case ISD::TokenFactor:
6504 case ISD::MERGE_VALUES:
6506 return N1; // Factor, merge or concat of one node? No need.
6507 case ISD::BUILD_VECTOR: {
6508 // Attempt to simplify BUILD_VECTOR.
6509 SDValue Ops[] = {N1};
6510 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6511 return V;
6512 break;
6513 }
6514 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6515 case ISD::FP_EXTEND:
6517 "Invalid FP cast!");
6518 if (N1.getValueType() == VT) return N1; // noop conversion.
6519 assert((!VT.isVector() || VT.getVectorElementCount() ==
6521 "Vector element count mismatch!");
6522 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6523 if (N1.isUndef())
6524 return getUNDEF(VT);
6525 break;
6526 case ISD::FP_TO_SINT:
6527 case ISD::FP_TO_UINT:
6528 if (N1.isUndef())
6529 return getUNDEF(VT);
6530 break;
6531 case ISD::SINT_TO_FP:
6532 case ISD::UINT_TO_FP:
6533 // [us]itofp(undef) = 0, because the result value is bounded.
6534 if (N1.isUndef())
6535 return getConstantFP(0.0, DL, VT);
6536 break;
6537 case ISD::SIGN_EXTEND:
6538 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6539 "Invalid SIGN_EXTEND!");
6540 assert(VT.isVector() == N1.getValueType().isVector() &&
6541 "SIGN_EXTEND result type type should be vector iff the operand "
6542 "type is vector!");
6543 if (N1.getValueType() == VT) return N1; // noop extension
6544 assert((!VT.isVector() || VT.getVectorElementCount() ==
6546 "Vector element count mismatch!");
6547 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6548 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6549 SDNodeFlags Flags;
6550 if (OpOpcode == ISD::ZERO_EXTEND)
6551 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6552 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6553 transferDbgValues(N1, NewVal);
6554 return NewVal;
6555 }
6556
6557 if (OpOpcode == ISD::POISON)
6558 return getPOISON(VT);
6559
6560 if (N1.isUndef())
6561 // sext(undef) = 0, because the top bits will all be the same.
6562 return getConstant(0, DL, VT);
6563
6564 // Skip unnecessary sext_inreg pattern:
6565 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6566 if (OpOpcode == ISD::TRUNCATE) {
6567 SDValue OpOp = N1.getOperand(0);
6568 if (OpOp.getValueType() == VT) {
6569 unsigned NumSignExtBits =
6571 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6572 transferDbgValues(N1, OpOp);
6573 return OpOp;
6574 }
6575 }
6576 }
6577 break;
6578 case ISD::ZERO_EXTEND:
6579 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6580 "Invalid ZERO_EXTEND!");
6581 assert(VT.isVector() == N1.getValueType().isVector() &&
6582 "ZERO_EXTEND result type type should be vector iff the operand "
6583 "type is vector!");
6584 if (N1.getValueType() == VT) return N1; // noop extension
6585 assert((!VT.isVector() || VT.getVectorElementCount() ==
6587 "Vector element count mismatch!");
6588 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6589 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6590 SDNodeFlags Flags;
6591 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6592 SDValue NewVal =
6593 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6594 transferDbgValues(N1, NewVal);
6595 return NewVal;
6596 }
6597
6598 if (OpOpcode == ISD::POISON)
6599 return getPOISON(VT);
6600
6601 if (N1.isUndef())
6602 // zext(undef) = 0, because the top bits will be zero.
6603 return getConstant(0, DL, VT);
6604
6605 // Skip unnecessary zext_inreg pattern:
6606 // (zext (trunc x)) -> x iff the upper bits are known zero.
6607 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6608 // use to recognise zext_inreg patterns.
6609 if (OpOpcode == ISD::TRUNCATE) {
6610 SDValue OpOp = N1.getOperand(0);
6611 if (OpOp.getValueType() == VT) {
6612 if (OpOp.getOpcode() != ISD::AND) {
6615 if (MaskedValueIsZero(OpOp, HiBits)) {
6616 transferDbgValues(N1, OpOp);
6617 return OpOp;
6618 }
6619 }
6620 }
6621 }
6622 break;
6623 case ISD::ANY_EXTEND:
6624 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6625 "Invalid ANY_EXTEND!");
6626 assert(VT.isVector() == N1.getValueType().isVector() &&
6627 "ANY_EXTEND result type type should be vector iff the operand "
6628 "type is vector!");
6629 if (N1.getValueType() == VT) return N1; // noop extension
6630 assert((!VT.isVector() || VT.getVectorElementCount() ==
6632 "Vector element count mismatch!");
6633 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6634
6635 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6636 OpOpcode == ISD::ANY_EXTEND) {
6637 SDNodeFlags Flags;
6638 if (OpOpcode == ISD::ZERO_EXTEND)
6639 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6640 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6641 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6642 }
6643 if (N1.isUndef())
6644 return getUNDEF(VT);
6645
6646 // (ext (trunc x)) -> x
6647 if (OpOpcode == ISD::TRUNCATE) {
6648 SDValue OpOp = N1.getOperand(0);
6649 if (OpOp.getValueType() == VT) {
6650 transferDbgValues(N1, OpOp);
6651 return OpOp;
6652 }
6653 }
6654 break;
6655 case ISD::TRUNCATE:
6656 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6657 "Invalid TRUNCATE!");
6658 assert(VT.isVector() == N1.getValueType().isVector() &&
6659 "TRUNCATE result type type should be vector iff the operand "
6660 "type is vector!");
6661 if (N1.getValueType() == VT) return N1; // noop truncate
6662 assert((!VT.isVector() || VT.getVectorElementCount() ==
6664 "Vector element count mismatch!");
6665 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6666 if (OpOpcode == ISD::TRUNCATE)
6667 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6668 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6669 OpOpcode == ISD::ANY_EXTEND) {
6670 // If the source is smaller than the dest, we still need an extend.
6672 VT.getScalarType())) {
6673 SDNodeFlags Flags;
6674 if (OpOpcode == ISD::ZERO_EXTEND)
6675 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6676 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6677 }
6678 if (N1.getOperand(0).getValueType().bitsGT(VT))
6679 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6680 return N1.getOperand(0);
6681 }
6682 if (N1.isUndef())
6683 return getUNDEF(VT);
6684 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6685 return getVScale(DL, VT,
6687 break;
6691 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6692 assert(N1.getValueType().bitsLE(VT) &&
6693 "The input must be the same size or smaller than the result.");
6696 "The destination vector type must have fewer lanes than the input.");
6697 break;
6698 case ISD::ABS:
6699 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6700 if (N1.isUndef())
6701 return getConstant(0, DL, VT);
6702 break;
6703 case ISD::BSWAP:
6704 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6705 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6706 "BSWAP types must be a multiple of 16 bits!");
6707 if (N1.isUndef())
6708 return getUNDEF(VT);
6709 // bswap(bswap(X)) -> X.
6710 if (OpOpcode == ISD::BSWAP)
6711 return N1.getOperand(0);
6712 break;
6713 case ISD::BITREVERSE:
6714 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6715 if (N1.isUndef())
6716 return getUNDEF(VT);
6717 break;
6718 case ISD::BITCAST:
6720 "Cannot BITCAST between types of different sizes!");
6721 if (VT == N1.getValueType()) return N1; // noop conversion.
6722 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6723 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6724 if (N1.isUndef())
6725 return getUNDEF(VT);
6726 break;
6728 assert(VT.isVector() && !N1.getValueType().isVector() &&
6729 (VT.getVectorElementType() == N1.getValueType() ||
6731 N1.getValueType().isInteger() &&
6733 "Illegal SCALAR_TO_VECTOR node!");
6734 if (N1.isUndef())
6735 return getUNDEF(VT);
6736 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6737 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6739 N1.getConstantOperandVal(1) == 0 &&
6740 N1.getOperand(0).getValueType() == VT)
6741 return N1.getOperand(0);
6742 break;
6743 case ISD::FNEG:
6744 // Negation of an unknown bag of bits is still completely undefined.
6745 if (N1.isUndef())
6746 return getUNDEF(VT);
6747
6748 if (OpOpcode == ISD::FNEG) // --X -> X
6749 return N1.getOperand(0);
6750 break;
6751 case ISD::FABS:
6752 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6753 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6754 break;
6755 case ISD::VSCALE:
6756 assert(VT == N1.getValueType() && "Unexpected VT!");
6757 break;
6758 case ISD::CTPOP:
6759 if (N1.getValueType().getScalarType() == MVT::i1)
6760 return N1;
6761 break;
6762 case ISD::CTLZ:
6763 case ISD::CTTZ:
6764 if (N1.getValueType().getScalarType() == MVT::i1)
6765 return getNOT(DL, N1, N1.getValueType());
6766 break;
6767 case ISD::VECREDUCE_ADD:
6768 if (N1.getValueType().getScalarType() == MVT::i1)
6769 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6770 break;
6773 if (N1.getValueType().getScalarType() == MVT::i1)
6774 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6775 break;
6778 if (N1.getValueType().getScalarType() == MVT::i1)
6779 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6780 break;
6781 case ISD::SPLAT_VECTOR:
6782 assert(VT.isVector() && "Wrong return type!");
6783 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6784 // that for now.
6786 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6788 N1.getValueType().isInteger() &&
6790 "Wrong operand type!");
6791 break;
6792 }
6793
6794 SDNode *N;
6795 SDVTList VTs = getVTList(VT);
6796 SDValue Ops[] = {N1};
6797 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6799 AddNodeIDNode(ID, Opcode, VTs, Ops);
6800 void *IP = nullptr;
6801 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6802 E->intersectFlagsWith(Flags);
6803 return SDValue(E, 0);
6804 }
6805
6806 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6807 N->setFlags(Flags);
6808 createOperands(N, Ops);
6809 CSEMap.InsertNode(N, IP);
6810 } else {
6811 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6812 createOperands(N, Ops);
6813 }
6814
6815 InsertNode(N);
6816 SDValue V = SDValue(N, 0);
6817 NewSDValueDbgMsg(V, "Creating new node: ", this);
6818 return V;
6819}
6820
6821static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6822 const APInt &C2) {
6823 switch (Opcode) {
6824 case ISD::ADD: return C1 + C2;
6825 case ISD::SUB: return C1 - C2;
6826 case ISD::MUL: return C1 * C2;
6827 case ISD::AND: return C1 & C2;
6828 case ISD::OR: return C1 | C2;
6829 case ISD::XOR: return C1 ^ C2;
6830 case ISD::SHL: return C1 << C2;
6831 case ISD::SRL: return C1.lshr(C2);
6832 case ISD::SRA: return C1.ashr(C2);
6833 case ISD::ROTL: return C1.rotl(C2);
6834 case ISD::ROTR: return C1.rotr(C2);
6835 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6836 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6837 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6838 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6839 case ISD::SADDSAT: return C1.sadd_sat(C2);
6840 case ISD::UADDSAT: return C1.uadd_sat(C2);
6841 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6842 case ISD::USUBSAT: return C1.usub_sat(C2);
6843 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6844 case ISD::USHLSAT: return C1.ushl_sat(C2);
6845 case ISD::UDIV:
6846 if (!C2.getBoolValue())
6847 break;
6848 return C1.udiv(C2);
6849 case ISD::UREM:
6850 if (!C2.getBoolValue())
6851 break;
6852 return C1.urem(C2);
6853 case ISD::SDIV:
6854 if (!C2.getBoolValue())
6855 break;
6856 return C1.sdiv(C2);
6857 case ISD::SREM:
6858 if (!C2.getBoolValue())
6859 break;
6860 return C1.srem(C2);
6861 case ISD::AVGFLOORS:
6862 return APIntOps::avgFloorS(C1, C2);
6863 case ISD::AVGFLOORU:
6864 return APIntOps::avgFloorU(C1, C2);
6865 case ISD::AVGCEILS:
6866 return APIntOps::avgCeilS(C1, C2);
6867 case ISD::AVGCEILU:
6868 return APIntOps::avgCeilU(C1, C2);
6869 case ISD::ABDS:
6870 return APIntOps::abds(C1, C2);
6871 case ISD::ABDU:
6872 return APIntOps::abdu(C1, C2);
6873 case ISD::MULHS:
6874 return APIntOps::mulhs(C1, C2);
6875 case ISD::MULHU:
6876 return APIntOps::mulhu(C1, C2);
6877 }
6878 return std::nullopt;
6879}
6880// Handle constant folding with UNDEF.
6881// TODO: Handle more cases.
6882static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6883 bool IsUndef1, const APInt &C2,
6884 bool IsUndef2) {
6885 if (!(IsUndef1 || IsUndef2))
6886 return FoldValue(Opcode, C1, C2);
6887
6888 // Fold and(x, undef) -> 0
6889 // Fold mul(x, undef) -> 0
6890 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6891 return APInt::getZero(C1.getBitWidth());
6892
6893 return std::nullopt;
6894}
6895
6897 const GlobalAddressSDNode *GA,
6898 const SDNode *N2) {
6899 if (GA->getOpcode() != ISD::GlobalAddress)
6900 return SDValue();
6901 if (!TLI->isOffsetFoldingLegal(GA))
6902 return SDValue();
6903 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6904 if (!C2)
6905 return SDValue();
6906 int64_t Offset = C2->getSExtValue();
6907 switch (Opcode) {
6908 case ISD::ADD:
6909 case ISD::PTRADD:
6910 break;
6911 case ISD::SUB: Offset = -uint64_t(Offset); break;
6912 default: return SDValue();
6913 }
6914 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6915 GA->getOffset() + uint64_t(Offset));
6916}
6917
6919 switch (Opcode) {
6920 case ISD::SDIV:
6921 case ISD::UDIV:
6922 case ISD::SREM:
6923 case ISD::UREM: {
6924 // If a divisor is zero/undef or any element of a divisor vector is
6925 // zero/undef, the whole op is undef.
6926 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6927 SDValue Divisor = Ops[1];
6928 if (Divisor.isUndef() || isNullConstant(Divisor))
6929 return true;
6930
6931 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
6932 llvm::any_of(Divisor->op_values(),
6933 [](SDValue V) { return V.isUndef() ||
6934 isNullConstant(V); });
6935 // TODO: Handle signed overflow.
6936 }
6937 // TODO: Handle oversized shifts.
6938 default:
6939 return false;
6940 }
6941}
6942
6945 SDNodeFlags Flags) {
6946 // If the opcode is a target-specific ISD node, there's nothing we can
6947 // do here and the operand rules may not line up with the below, so
6948 // bail early.
6949 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
6950 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
6951 // foldCONCAT_VECTORS in getNode before this is called.
6952 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
6953 return SDValue();
6954
6955 unsigned NumOps = Ops.size();
6956 if (NumOps == 0)
6957 return SDValue();
6958
6959 if (isUndef(Opcode, Ops))
6960 return getUNDEF(VT);
6961
6962 // Handle unary special cases.
6963 if (NumOps == 1) {
6964 SDValue N1 = Ops[0];
6965
6966 // Constant fold unary operations with an integer constant operand. Even
6967 // opaque constant will be folded, because the folding of unary operations
6968 // doesn't create new constants with different values. Nevertheless, the
6969 // opaque flag is preserved during folding to prevent future folding with
6970 // other constants.
6971 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
6972 const APInt &Val = C->getAPIntValue();
6973 switch (Opcode) {
6974 case ISD::SIGN_EXTEND:
6975 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6976 C->isTargetOpcode(), C->isOpaque());
6977 case ISD::TRUNCATE:
6978 if (C->isOpaque())
6979 break;
6980 [[fallthrough]];
6981 case ISD::ZERO_EXTEND:
6982 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6983 C->isTargetOpcode(), C->isOpaque());
6984 case ISD::ANY_EXTEND:
6985 // Some targets like RISCV prefer to sign extend some types.
6986 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
6987 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
6988 C->isTargetOpcode(), C->isOpaque());
6989 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
6990 C->isTargetOpcode(), C->isOpaque());
6991 case ISD::ABS:
6992 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
6993 C->isOpaque());
6994 case ISD::BITREVERSE:
6995 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
6996 C->isOpaque());
6997 case ISD::BSWAP:
6998 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
6999 C->isOpaque());
7000 case ISD::CTPOP:
7001 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7002 C->isOpaque());
7003 case ISD::CTLZ:
7005 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7006 C->isOpaque());
7007 case ISD::CTTZ:
7009 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7010 C->isOpaque());
7011 case ISD::UINT_TO_FP:
7012 case ISD::SINT_TO_FP: {
7014 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7016 return getConstantFP(FPV, DL, VT);
7017 }
7018 case ISD::FP16_TO_FP:
7019 case ISD::BF16_TO_FP: {
7020 bool Ignored;
7021 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7022 : APFloat::BFloat(),
7023 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7024
7025 // This can return overflow, underflow, or inexact; we don't care.
7026 // FIXME need to be more flexible about rounding mode.
7028 &Ignored);
7029 return getConstantFP(FPV, DL, VT);
7030 }
7031 case ISD::STEP_VECTOR:
7032 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7033 return V;
7034 break;
7035 case ISD::BITCAST:
7036 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7037 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7038 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7039 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7040 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7041 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7042 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7043 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7044 break;
7045 }
7046 }
7047
7048 // Constant fold unary operations with a floating point constant operand.
7049 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7050 APFloat V = C->getValueAPF(); // make copy
7051 switch (Opcode) {
7052 case ISD::FNEG:
7053 V.changeSign();
7054 return getConstantFP(V, DL, VT);
7055 case ISD::FABS:
7056 V.clearSign();
7057 return getConstantFP(V, DL, VT);
7058 case ISD::FCEIL: {
7059 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7061 return getConstantFP(V, DL, VT);
7062 return SDValue();
7063 }
7064 case ISD::FTRUNC: {
7065 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7067 return getConstantFP(V, DL, VT);
7068 return SDValue();
7069 }
7070 case ISD::FFLOOR: {
7071 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7073 return getConstantFP(V, DL, VT);
7074 return SDValue();
7075 }
7076 case ISD::FP_EXTEND: {
7077 bool ignored;
7078 // This can return overflow, underflow, or inexact; we don't care.
7079 // FIXME need to be more flexible about rounding mode.
7080 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7081 &ignored);
7082 return getConstantFP(V, DL, VT);
7083 }
7084 case ISD::FP_TO_SINT:
7085 case ISD::FP_TO_UINT: {
7086 bool ignored;
7087 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7088 // FIXME need to be more flexible about rounding mode.
7090 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7091 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7092 break;
7093 return getConstant(IntVal, DL, VT);
7094 }
7095 case ISD::FP_TO_FP16:
7096 case ISD::FP_TO_BF16: {
7097 bool Ignored;
7098 // This can return overflow, underflow, or inexact; we don't care.
7099 // FIXME need to be more flexible about rounding mode.
7100 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7101 : APFloat::BFloat(),
7103 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7104 }
7105 case ISD::BITCAST:
7106 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7107 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7108 VT);
7109 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7110 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7111 VT);
7112 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7113 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7114 VT);
7115 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7116 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7117 break;
7118 }
7119 }
7120
7121 // Early-out if we failed to constant fold a bitcast.
7122 if (Opcode == ISD::BITCAST)
7123 return SDValue();
7124 }
7125
7126 // Handle binops special cases.
7127 if (NumOps == 2) {
7128 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7129 return CFP;
7130
7131 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7132 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7133 if (C1->isOpaque() || C2->isOpaque())
7134 return SDValue();
7135
7136 std::optional<APInt> FoldAttempt =
7137 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7138 if (!FoldAttempt)
7139 return SDValue();
7140
7141 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7142 assert((!Folded || !VT.isVector()) &&
7143 "Can't fold vectors ops with scalar operands");
7144 return Folded;
7145 }
7146 }
7147
7148 // fold (add Sym, c) -> Sym+c
7150 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7151 if (TLI->isCommutativeBinOp(Opcode))
7153 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7154
7155 // fold (sext_in_reg c1) -> c2
7156 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7157 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7158
7159 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7160 unsigned FromBits = EVT.getScalarSizeInBits();
7161 Val <<= Val.getBitWidth() - FromBits;
7162 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7163 return getConstant(Val, DL, ConstantVT);
7164 };
7165
7166 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7167 const APInt &Val = C1->getAPIntValue();
7168 return SignExtendInReg(Val, VT);
7169 }
7170
7172 SmallVector<SDValue, 8> ScalarOps;
7173 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7174 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7175 SDValue Op = Ops[0].getOperand(I);
7176 if (Op.isUndef()) {
7177 ScalarOps.push_back(getUNDEF(OpVT));
7178 continue;
7179 }
7180 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7181 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7182 }
7183 return getBuildVector(VT, DL, ScalarOps);
7184 }
7185
7186 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7187 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7188 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7189 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7190 Ops[0].getOperand(0).getValueType()));
7191 }
7192 }
7193
7194 // Handle fshl/fshr special cases.
7195 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7196 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7197 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7198 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7199
7200 if (C1 && C2 && C3) {
7201 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7202 return SDValue();
7203 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7204 &V3 = C3->getAPIntValue();
7205
7206 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7207 : APIntOps::fshr(V1, V2, V3);
7208 return getConstant(FoldedVal, DL, VT);
7209 }
7210 }
7211
7212 // Handle fma/fmad special cases.
7213 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7214 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7215 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7216 Ops[2].getValueType() == VT && "FMA types must match!");
7220 if (C1 && C2 && C3) {
7221 APFloat V1 = C1->getValueAPF();
7222 const APFloat &V2 = C2->getValueAPF();
7223 const APFloat &V3 = C3->getValueAPF();
7224 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7227 } else
7229 return getConstantFP(V1, DL, VT);
7230 }
7231 }
7232
7233 // This is for vector folding only from here on.
7234 if (!VT.isVector())
7235 return SDValue();
7236
7237 ElementCount NumElts = VT.getVectorElementCount();
7238
7239 // See if we can fold through any bitcasted integer ops.
7240 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7241 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7242 (Ops[0].getOpcode() == ISD::BITCAST ||
7243 Ops[1].getOpcode() == ISD::BITCAST)) {
7246 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7247 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7248 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7249 N2.getValueType().isInteger()) {
7250 bool IsLE = getDataLayout().isLittleEndian();
7251 unsigned EltBits = VT.getScalarSizeInBits();
7252 SmallVector<APInt> RawBits1, RawBits2;
7253 BitVector UndefElts1, UndefElts2;
7254 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7255 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7256 SmallVector<APInt> RawBits;
7257 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7258 std::optional<APInt> Fold = FoldValueWithUndef(
7259 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7260 if (!Fold)
7261 break;
7262 RawBits.push_back(*Fold);
7263 }
7264 if (RawBits.size() == NumElts.getFixedValue()) {
7265 // We have constant folded, but we might need to cast this again back
7266 // to the original (possibly legalized) type.
7267 EVT BVVT, BVEltVT;
7268 if (N1.getValueType() == VT) {
7269 BVVT = N1.getValueType();
7270 BVEltVT = BV1->getOperand(0).getValueType();
7271 } else {
7272 BVVT = N2.getValueType();
7273 BVEltVT = BV2->getOperand(0).getValueType();
7274 }
7275 unsigned BVEltBits = BVEltVT.getSizeInBits();
7276 SmallVector<APInt> DstBits;
7277 BitVector DstUndefs;
7279 DstBits, RawBits, DstUndefs,
7280 BitVector(RawBits.size(), false));
7281 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7282 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7283 if (DstUndefs[I])
7284 continue;
7285 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7286 }
7287 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7288 }
7289 }
7290 }
7291 }
7292
7293 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7294 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7295 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7296 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7297 APInt RHSVal;
7298 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7299 APInt NewStep = Opcode == ISD::MUL
7300 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7301 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7302 return getStepVector(DL, VT, NewStep);
7303 }
7304 }
7305
7306 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7307 return !Op.getValueType().isVector() ||
7308 Op.getValueType().getVectorElementCount() == NumElts;
7309 };
7310
7311 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7312 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7313 Op.getOpcode() == ISD::BUILD_VECTOR ||
7314 Op.getOpcode() == ISD::SPLAT_VECTOR;
7315 };
7316
7317 // All operands must be vector types with the same number of elements as
7318 // the result type and must be either UNDEF or a build/splat vector
7319 // or UNDEF scalars.
7320 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7321 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7322 return SDValue();
7323
7324 // If we are comparing vectors, then the result needs to be a i1 boolean that
7325 // is then extended back to the legal result type depending on how booleans
7326 // are represented.
7327 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7328 ISD::NodeType ExtendCode =
7329 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7330 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7332
7333 // Find legal integer scalar type for constant promotion and
7334 // ensure that its scalar size is at least as large as source.
7335 EVT LegalSVT = VT.getScalarType();
7336 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7337 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7338 if (LegalSVT.bitsLT(VT.getScalarType()))
7339 return SDValue();
7340 }
7341
7342 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7343 // only have one operand to check. For fixed-length vector types we may have
7344 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7345 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7346
7347 // Constant fold each scalar lane separately.
7348 SmallVector<SDValue, 4> ScalarResults;
7349 for (unsigned I = 0; I != NumVectorElts; I++) {
7350 SmallVector<SDValue, 4> ScalarOps;
7351 for (SDValue Op : Ops) {
7352 EVT InSVT = Op.getValueType().getScalarType();
7353 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7354 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7355 if (Op.isUndef())
7356 ScalarOps.push_back(getUNDEF(InSVT));
7357 else
7358 ScalarOps.push_back(Op);
7359 continue;
7360 }
7361
7362 SDValue ScalarOp =
7363 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7364 EVT ScalarVT = ScalarOp.getValueType();
7365
7366 // Build vector (integer) scalar operands may need implicit
7367 // truncation - do this before constant folding.
7368 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7369 // Don't create illegally-typed nodes unless they're constants or undef
7370 // - if we fail to constant fold we can't guarantee the (dead) nodes
7371 // we're creating will be cleaned up before being visited for
7372 // legalization.
7373 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7374 !isa<ConstantSDNode>(ScalarOp) &&
7375 TLI->getTypeAction(*getContext(), InSVT) !=
7377 return SDValue();
7378 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7379 }
7380
7381 ScalarOps.push_back(ScalarOp);
7382 }
7383
7384 // Constant fold the scalar operands.
7385 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7386
7387 // Scalar folding only succeeded if the result is a constant or UNDEF.
7388 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7389 ScalarResult.getOpcode() != ISD::ConstantFP)
7390 return SDValue();
7391
7392 // Legalize the (integer) scalar constant if necessary. We only do
7393 // this once we know the folding succeeded, since otherwise we would
7394 // get a node with illegal type which has a user.
7395 if (LegalSVT != SVT)
7396 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7397
7398 ScalarResults.push_back(ScalarResult);
7399 }
7400
7401 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7402 : getBuildVector(VT, DL, ScalarResults);
7403 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7404 return V;
7405}
7406
7409 // TODO: Add support for unary/ternary fp opcodes.
7410 if (Ops.size() != 2)
7411 return SDValue();
7412
7413 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7414 // should. That will require dealing with a potentially non-default
7415 // rounding mode, checking the "opStatus" return value from the APFloat
7416 // math calculations, and possibly other variations.
7417 SDValue N1 = Ops[0];
7418 SDValue N2 = Ops[1];
7419 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7420 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7421 if (N1CFP && N2CFP) {
7422 APFloat C1 = N1CFP->getValueAPF(); // make copy
7423 const APFloat &C2 = N2CFP->getValueAPF();
7424 switch (Opcode) {
7425 case ISD::FADD:
7427 return getConstantFP(C1, DL, VT);
7428 case ISD::FSUB:
7430 return getConstantFP(C1, DL, VT);
7431 case ISD::FMUL:
7433 return getConstantFP(C1, DL, VT);
7434 case ISD::FDIV:
7436 return getConstantFP(C1, DL, VT);
7437 case ISD::FREM:
7438 C1.mod(C2);
7439 return getConstantFP(C1, DL, VT);
7440 case ISD::FCOPYSIGN:
7441 C1.copySign(C2);
7442 return getConstantFP(C1, DL, VT);
7443 case ISD::FMINNUM:
7444 if (C1.isSignaling() || C2.isSignaling())
7445 return SDValue();
7446 return getConstantFP(minnum(C1, C2), DL, VT);
7447 case ISD::FMAXNUM:
7448 if (C1.isSignaling() || C2.isSignaling())
7449 return SDValue();
7450 return getConstantFP(maxnum(C1, C2), DL, VT);
7451 case ISD::FMINIMUM:
7452 return getConstantFP(minimum(C1, C2), DL, VT);
7453 case ISD::FMAXIMUM:
7454 return getConstantFP(maximum(C1, C2), DL, VT);
7455 case ISD::FMINIMUMNUM:
7456 return getConstantFP(minimumnum(C1, C2), DL, VT);
7457 case ISD::FMAXIMUMNUM:
7458 return getConstantFP(maximumnum(C1, C2), DL, VT);
7459 default: break;
7460 }
7461 }
7462 if (N1CFP && Opcode == ISD::FP_ROUND) {
7463 APFloat C1 = N1CFP->getValueAPF(); // make copy
7464 bool Unused;
7465 // This can return overflow, underflow, or inexact; we don't care.
7466 // FIXME need to be more flexible about rounding mode.
7468 &Unused);
7469 return getConstantFP(C1, DL, VT);
7470 }
7471
7472 switch (Opcode) {
7473 case ISD::FSUB:
7474 // -0.0 - undef --> undef (consistent with "fneg undef")
7475 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7476 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7477 return getUNDEF(VT);
7478 [[fallthrough]];
7479
7480 case ISD::FADD:
7481 case ISD::FMUL:
7482 case ISD::FDIV:
7483 case ISD::FREM:
7484 // If both operands are undef, the result is undef. If 1 operand is undef,
7485 // the result is NaN. This should match the behavior of the IR optimizer.
7486 if (N1.isUndef() && N2.isUndef())
7487 return getUNDEF(VT);
7488 if (N1.isUndef() || N2.isUndef())
7490 }
7491 return SDValue();
7492}
7493
7495 const SDLoc &DL, EVT DstEltVT) {
7496 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7497
7498 // If this is already the right type, we're done.
7499 if (SrcEltVT == DstEltVT)
7500 return SDValue(BV, 0);
7501
7502 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7503 unsigned DstBitSize = DstEltVT.getSizeInBits();
7504
7505 // If this is a conversion of N elements of one type to N elements of another
7506 // type, convert each element. This handles FP<->INT cases.
7507 if (SrcBitSize == DstBitSize) {
7509 for (SDValue Op : BV->op_values()) {
7510 // If the vector element type is not legal, the BUILD_VECTOR operands
7511 // are promoted and implicitly truncated. Make that explicit here.
7512 if (Op.getValueType() != SrcEltVT)
7513 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7514 Ops.push_back(getBitcast(DstEltVT, Op));
7515 }
7516 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7518 return getBuildVector(VT, DL, Ops);
7519 }
7520
7521 // Otherwise, we're growing or shrinking the elements. To avoid having to
7522 // handle annoying details of growing/shrinking FP values, we convert them to
7523 // int first.
7524 if (SrcEltVT.isFloatingPoint()) {
7525 // Convert the input float vector to a int vector where the elements are the
7526 // same sizes.
7527 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7528 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7530 DstEltVT);
7531 return SDValue();
7532 }
7533
7534 // Now we know the input is an integer vector. If the output is a FP type,
7535 // convert to integer first, then to FP of the right size.
7536 if (DstEltVT.isFloatingPoint()) {
7537 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7538 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7540 DstEltVT);
7541 return SDValue();
7542 }
7543
7544 // Okay, we know the src/dst types are both integers of differing types.
7545 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7546
7547 // Extract the constant raw bit data.
7548 BitVector UndefElements;
7549 SmallVector<APInt> RawBits;
7550 bool IsLE = getDataLayout().isLittleEndian();
7551 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7552 return SDValue();
7553
7555 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7556 if (UndefElements[I])
7557 Ops.push_back(getUNDEF(DstEltVT));
7558 else
7559 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7560 }
7561
7562 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7563 return getBuildVector(VT, DL, Ops);
7564}
7565
7567 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7568
7569 // There's no need to assert on a byte-aligned pointer. All pointers are at
7570 // least byte aligned.
7571 if (A == Align(1))
7572 return Val;
7573
7574 SDVTList VTs = getVTList(Val.getValueType());
7576 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7577 ID.AddInteger(A.value());
7578
7579 void *IP = nullptr;
7580 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7581 return SDValue(E, 0);
7582
7583 auto *N =
7584 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7585 createOperands(N, {Val});
7586
7587 CSEMap.InsertNode(N, IP);
7588 InsertNode(N);
7589
7590 SDValue V(N, 0);
7591 NewSDValueDbgMsg(V, "Creating new node: ", this);
7592 return V;
7593}
7594
7595SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7596 SDValue N1, SDValue N2) {
7597 SDNodeFlags Flags;
7598 if (Inserter)
7599 Flags = Inserter->getFlags();
7600 return getNode(Opcode, DL, VT, N1, N2, Flags);
7601}
7602
7604 SDValue &N2) const {
7605 if (!TLI->isCommutativeBinOp(Opcode))
7606 return;
7607
7608 // Canonicalize:
7609 // binop(const, nonconst) -> binop(nonconst, const)
7612 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7613 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7614 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7615 std::swap(N1, N2);
7616
7617 // Canonicalize:
7618 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7619 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7621 std::swap(N1, N2);
7622}
7623
7624SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7625 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7627 N2.getOpcode() != ISD::DELETED_NODE &&
7628 "Operand is DELETED_NODE!");
7629
7630 canonicalizeCommutativeBinop(Opcode, N1, N2);
7631
7632 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7633 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7634
7635 // Don't allow undefs in vector splats - we might be returning N2 when folding
7636 // to zero etc.
7637 ConstantSDNode *N2CV =
7638 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7639
7640 switch (Opcode) {
7641 default: break;
7642 case ISD::TokenFactor:
7643 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7644 N2.getValueType() == MVT::Other && "Invalid token factor!");
7645 // Fold trivial token factors.
7646 if (N1.getOpcode() == ISD::EntryToken) return N2;
7647 if (N2.getOpcode() == ISD::EntryToken) return N1;
7648 if (N1 == N2) return N1;
7649 break;
7650 case ISD::BUILD_VECTOR: {
7651 // Attempt to simplify BUILD_VECTOR.
7652 SDValue Ops[] = {N1, N2};
7653 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7654 return V;
7655 break;
7656 }
7657 case ISD::CONCAT_VECTORS: {
7658 SDValue Ops[] = {N1, N2};
7659 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7660 return V;
7661 break;
7662 }
7663 case ISD::AND:
7664 assert(VT.isInteger() && "This operator does not apply to FP types!");
7665 assert(N1.getValueType() == N2.getValueType() &&
7666 N1.getValueType() == VT && "Binary operator types must match!");
7667 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7668 // worth handling here.
7669 if (N2CV && N2CV->isZero())
7670 return N2;
7671 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7672 return N1;
7673 break;
7674 case ISD::OR:
7675 case ISD::XOR:
7676 case ISD::ADD:
7677 case ISD::PTRADD:
7678 case ISD::SUB:
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 // The equal operand types requirement is unnecessarily strong for PTRADD.
7683 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7684 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7685 // logic everywhere where PTRADDs may be folded or combined to properly
7686 // support them. If/when we introduce pointer types to the SDAG, we will
7687 // need to relax this constraint.
7688
7689 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7690 // it's worth handling here.
7691 if (N2CV && N2CV->isZero())
7692 return N1;
7693 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7694 VT.getScalarType() == MVT::i1)
7695 return getNode(ISD::XOR, DL, VT, N1, N2);
7696 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7697 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7698 N2.getOpcode() == ISD::VSCALE) {
7699 const APInt &C1 = N1->getConstantOperandAPInt(0);
7700 const APInt &C2 = N2->getConstantOperandAPInt(0);
7701 return getVScale(DL, VT, C1 + C2);
7702 }
7703 break;
7704 case ISD::MUL:
7705 assert(VT.isInteger() && "This operator does not apply to FP types!");
7706 assert(N1.getValueType() == N2.getValueType() &&
7707 N1.getValueType() == VT && "Binary operator types must match!");
7708 if (VT.getScalarType() == MVT::i1)
7709 return getNode(ISD::AND, DL, VT, N1, N2);
7710 if (N2CV && N2CV->isZero())
7711 return N2;
7712 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7713 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7714 const APInt &N2CImm = N2C->getAPIntValue();
7715 return getVScale(DL, VT, MulImm * N2CImm);
7716 }
7717 break;
7718 case ISD::UDIV:
7719 case ISD::UREM:
7720 case ISD::MULHU:
7721 case ISD::MULHS:
7722 case ISD::SDIV:
7723 case ISD::SREM:
7724 case ISD::SADDSAT:
7725 case ISD::SSUBSAT:
7726 case ISD::UADDSAT:
7727 case ISD::USUBSAT:
7728 assert(VT.isInteger() && "This operator does not apply to FP types!");
7729 assert(N1.getValueType() == N2.getValueType() &&
7730 N1.getValueType() == VT && "Binary operator types must match!");
7731 if (VT.getScalarType() == MVT::i1) {
7732 // fold (add_sat x, y) -> (or x, y) for bool types.
7733 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7734 return getNode(ISD::OR, DL, VT, N1, N2);
7735 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7736 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7737 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7738 }
7739 break;
7740 case ISD::SCMP:
7741 case ISD::UCMP:
7742 assert(N1.getValueType() == N2.getValueType() &&
7743 "Types of operands of UCMP/SCMP must match");
7744 assert(N1.getValueType().isVector() == VT.isVector() &&
7745 "Operands and return type of must both be scalars or vectors");
7746 if (VT.isVector())
7749 "Result and operands must have the same number of elements");
7750 break;
7751 case ISD::AVGFLOORS:
7752 case ISD::AVGFLOORU:
7753 case ISD::AVGCEILS:
7754 case ISD::AVGCEILU:
7755 assert(VT.isInteger() && "This operator does not apply to FP types!");
7756 assert(N1.getValueType() == N2.getValueType() &&
7757 N1.getValueType() == VT && "Binary operator types must match!");
7758 break;
7759 case ISD::ABDS:
7760 case ISD::ABDU:
7761 assert(VT.isInteger() && "This operator does not apply to FP types!");
7762 assert(N1.getValueType() == N2.getValueType() &&
7763 N1.getValueType() == VT && "Binary operator types must match!");
7764 if (VT.getScalarType() == MVT::i1)
7765 return getNode(ISD::XOR, DL, VT, N1, N2);
7766 break;
7767 case ISD::SMIN:
7768 case ISD::UMAX:
7769 assert(VT.isInteger() && "This operator does not apply to FP types!");
7770 assert(N1.getValueType() == N2.getValueType() &&
7771 N1.getValueType() == VT && "Binary operator types must match!");
7772 if (VT.getScalarType() == MVT::i1)
7773 return getNode(ISD::OR, DL, VT, N1, N2);
7774 break;
7775 case ISD::SMAX:
7776 case ISD::UMIN:
7777 assert(VT.isInteger() && "This operator does not apply to FP types!");
7778 assert(N1.getValueType() == N2.getValueType() &&
7779 N1.getValueType() == VT && "Binary operator types must match!");
7780 if (VT.getScalarType() == MVT::i1)
7781 return getNode(ISD::AND, DL, VT, N1, N2);
7782 break;
7783 case ISD::FADD:
7784 case ISD::FSUB:
7785 case ISD::FMUL:
7786 case ISD::FDIV:
7787 case ISD::FREM:
7788 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7789 assert(N1.getValueType() == N2.getValueType() &&
7790 N1.getValueType() == VT && "Binary operator types must match!");
7791 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7792 return V;
7793 break;
7794 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7795 assert(N1.getValueType() == VT &&
7798 "Invalid FCOPYSIGN!");
7799 break;
7800 case ISD::SHL:
7801 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7802 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7803 const APInt &ShiftImm = N2C->getAPIntValue();
7804 return getVScale(DL, VT, MulImm << ShiftImm);
7805 }
7806 [[fallthrough]];
7807 case ISD::SRA:
7808 case ISD::SRL:
7809 if (SDValue V = simplifyShift(N1, N2))
7810 return V;
7811 [[fallthrough]];
7812 case ISD::ROTL:
7813 case ISD::ROTR:
7814 assert(VT == N1.getValueType() &&
7815 "Shift operators return type must be the same as their first arg");
7816 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7817 "Shifts only work on integers");
7818 assert((!VT.isVector() || VT == N2.getValueType()) &&
7819 "Vector shift amounts must be in the same as their first arg");
7820 // Verify that the shift amount VT is big enough to hold valid shift
7821 // amounts. This catches things like trying to shift an i1024 value by an
7822 // i8, which is easy to fall into in generic code that uses
7823 // TLI.getShiftAmount().
7826 "Invalid use of small shift amount with oversized value!");
7827
7828 // Always fold shifts of i1 values so the code generator doesn't need to
7829 // handle them. Since we know the size of the shift has to be less than the
7830 // size of the value, the shift/rotate count is guaranteed to be zero.
7831 if (VT == MVT::i1)
7832 return N1;
7833 if (N2CV && N2CV->isZero())
7834 return N1;
7835 break;
7836 case ISD::FP_ROUND:
7838 VT.bitsLE(N1.getValueType()) && N2C &&
7839 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7840 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7841 if (N1.getValueType() == VT) return N1; // noop conversion.
7842 break;
7843 case ISD::AssertNoFPClass: {
7845 "AssertNoFPClass is used for a non-floating type");
7846 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7847 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7848 assert(llvm::to_underlying(NoFPClass) <=
7850 "FPClassTest value too large");
7851 (void)NoFPClass;
7852 break;
7853 }
7854 case ISD::AssertSext:
7855 case ISD::AssertZext: {
7856 EVT EVT = cast<VTSDNode>(N2)->getVT();
7857 assert(VT == N1.getValueType() && "Not an inreg extend!");
7858 assert(VT.isInteger() && EVT.isInteger() &&
7859 "Cannot *_EXTEND_INREG FP types");
7860 assert(!EVT.isVector() &&
7861 "AssertSExt/AssertZExt type should be the vector element type "
7862 "rather than the vector type!");
7863 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7864 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7865 break;
7866 }
7868 EVT EVT = cast<VTSDNode>(N2)->getVT();
7869 assert(VT == N1.getValueType() && "Not an inreg extend!");
7870 assert(VT.isInteger() && EVT.isInteger() &&
7871 "Cannot *_EXTEND_INREG FP types");
7872 assert(EVT.isVector() == VT.isVector() &&
7873 "SIGN_EXTEND_INREG type should be vector iff the operand "
7874 "type is vector!");
7875 assert((!EVT.isVector() ||
7877 "Vector element counts must match in SIGN_EXTEND_INREG");
7878 assert(EVT.bitsLE(VT) && "Not extending!");
7879 if (EVT == VT) return N1; // Not actually extending
7880 break;
7881 }
7883 case ISD::FP_TO_UINT_SAT: {
7884 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7885 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7886 assert(N1.getValueType().isVector() == VT.isVector() &&
7887 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7888 "vector!");
7889 assert((!VT.isVector() || VT.getVectorElementCount() ==
7891 "Vector element counts must match in FP_TO_*INT_SAT");
7892 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7893 "Type to saturate to must be a scalar.");
7894 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7895 "Not extending!");
7896 break;
7897 }
7900 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7901 element type of the vector.");
7902
7903 // Extract from an undefined value or using an undefined index is undefined.
7904 if (N1.isUndef() || N2.isUndef())
7905 return getUNDEF(VT);
7906
7907 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7908 // vectors. For scalable vectors we will provide appropriate support for
7909 // dealing with arbitrary indices.
7910 if (N2C && N1.getValueType().isFixedLengthVector() &&
7911 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7912 return getUNDEF(VT);
7913
7914 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7915 // expanding copies of large vectors from registers. This only works for
7916 // fixed length vectors, since we need to know the exact number of
7917 // elements.
7918 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7920 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7921 return getExtractVectorElt(DL, VT,
7922 N1.getOperand(N2C->getZExtValue() / Factor),
7923 N2C->getZExtValue() % Factor);
7924 }
7925
7926 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7927 // lowering is expanding large vector constants.
7928 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
7929 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
7932 "BUILD_VECTOR used for scalable vectors");
7933 unsigned Index =
7934 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
7935 SDValue Elt = N1.getOperand(Index);
7936
7937 if (VT != Elt.getValueType())
7938 // If the vector element type is not legal, the BUILD_VECTOR operands
7939 // are promoted and implicitly truncated, and the result implicitly
7940 // extended. Make that explicit here.
7941 Elt = getAnyExtOrTrunc(Elt, DL, VT);
7942
7943 return Elt;
7944 }
7945
7946 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
7947 // operations are lowered to scalars.
7948 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
7949 // If the indices are the same, return the inserted element else
7950 // if the indices are known different, extract the element from
7951 // the original vector.
7952 SDValue N1Op2 = N1.getOperand(2);
7954
7955 if (N1Op2C && N2C) {
7956 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
7957 if (VT == N1.getOperand(1).getValueType())
7958 return N1.getOperand(1);
7959 if (VT.isFloatingPoint()) {
7961 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
7962 }
7963 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
7964 }
7965 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
7966 }
7967 }
7968
7969 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
7970 // when vector types are scalarized and v1iX is legal.
7971 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
7972 // Here we are completely ignoring the extract element index (N2),
7973 // which is fine for fixed width vectors, since any index other than 0
7974 // is undefined anyway. However, this cannot be ignored for scalable
7975 // vectors - in theory we could support this, but we don't want to do this
7976 // without a profitability check.
7977 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
7979 N1.getValueType().getVectorNumElements() == 1) {
7980 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
7981 N1.getOperand(1));
7982 }
7983 break;
7985 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
7986 assert(!N1.getValueType().isVector() && !VT.isVector() &&
7987 (N1.getValueType().isInteger() == VT.isInteger()) &&
7988 N1.getValueType() != VT &&
7989 "Wrong types for EXTRACT_ELEMENT!");
7990
7991 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
7992 // 64-bit integers into 32-bit parts. Instead of building the extract of
7993 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
7994 if (N1.getOpcode() == ISD::BUILD_PAIR)
7995 return N1.getOperand(N2C->getZExtValue());
7996
7997 // EXTRACT_ELEMENT of a constant int is also very common.
7998 if (N1C) {
7999 unsigned ElementSize = VT.getSizeInBits();
8000 unsigned Shift = ElementSize * N2C->getZExtValue();
8001 const APInt &Val = N1C->getAPIntValue();
8002 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8003 }
8004 break;
8006 EVT N1VT = N1.getValueType();
8007 assert(VT.isVector() && N1VT.isVector() &&
8008 "Extract subvector VTs must be vectors!");
8010 "Extract subvector VTs must have the same element type!");
8011 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8012 "Cannot extract a scalable vector from a fixed length vector!");
8013 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8015 "Extract subvector must be from larger vector to smaller vector!");
8016 assert(N2C && "Extract subvector index must be a constant");
8017 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8018 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8019 N1VT.getVectorMinNumElements()) &&
8020 "Extract subvector overflow!");
8021 assert(N2C->getAPIntValue().getBitWidth() ==
8022 TLI->getVectorIdxWidth(getDataLayout()) &&
8023 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8024 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8025 "Extract index is not a multiple of the output vector length");
8026
8027 // Trivial extraction.
8028 if (VT == N1VT)
8029 return N1;
8030
8031 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8032 if (N1.isUndef())
8033 return getUNDEF(VT);
8034
8035 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8036 // the concat have the same type as the extract.
8037 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8038 VT == N1.getOperand(0).getValueType()) {
8039 unsigned Factor = VT.getVectorMinNumElements();
8040 return N1.getOperand(N2C->getZExtValue() / Factor);
8041 }
8042
8043 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8044 // during shuffle legalization.
8045 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8046 VT == N1.getOperand(1).getValueType())
8047 return N1.getOperand(1);
8048 break;
8049 }
8050 }
8051
8052 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8053 switch (Opcode) {
8054 case ISD::XOR:
8055 case ISD::ADD:
8056 case ISD::PTRADD:
8057 case ISD::SUB:
8059 case ISD::UDIV:
8060 case ISD::SDIV:
8061 case ISD::UREM:
8062 case ISD::SREM:
8063 case ISD::MUL:
8064 case ISD::AND:
8065 case ISD::SSUBSAT:
8066 case ISD::USUBSAT:
8067 case ISD::UMIN:
8068 case ISD::OR:
8069 case ISD::SADDSAT:
8070 case ISD::UADDSAT:
8071 case ISD::UMAX:
8072 case ISD::SMAX:
8073 case ISD::SMIN:
8074 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8075 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8076 }
8077 }
8078
8079 // Canonicalize an UNDEF to the RHS, even over a constant.
8080 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8081 if (TLI->isCommutativeBinOp(Opcode)) {
8082 std::swap(N1, N2);
8083 } else {
8084 switch (Opcode) {
8085 case ISD::PTRADD:
8086 case ISD::SUB:
8087 // fold op(undef, non_undef_arg2) -> undef.
8088 return N1;
8090 case ISD::UDIV:
8091 case ISD::SDIV:
8092 case ISD::UREM:
8093 case ISD::SREM:
8094 case ISD::SSUBSAT:
8095 case ISD::USUBSAT:
8096 // fold op(undef, non_undef_arg2) -> 0.
8097 return getConstant(0, DL, VT);
8098 }
8099 }
8100 }
8101
8102 // Fold a bunch of operators when the RHS is undef.
8103 if (N2.getOpcode() == ISD::UNDEF) {
8104 switch (Opcode) {
8105 case ISD::XOR:
8106 if (N1.getOpcode() == ISD::UNDEF)
8107 // Handle undef ^ undef -> 0 special case. This is a common
8108 // idiom (misuse).
8109 return getConstant(0, DL, VT);
8110 [[fallthrough]];
8111 case ISD::ADD:
8112 case ISD::PTRADD:
8113 case ISD::SUB:
8114 // fold op(arg1, undef) -> undef.
8115 return N2;
8116 case ISD::UDIV:
8117 case ISD::SDIV:
8118 case ISD::UREM:
8119 case ISD::SREM:
8120 // fold op(arg1, undef) -> poison.
8121 return getPOISON(VT);
8122 case ISD::MUL:
8123 case ISD::AND:
8124 case ISD::SSUBSAT:
8125 case ISD::USUBSAT:
8126 case ISD::UMIN:
8127 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8128 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8129 case ISD::OR:
8130 case ISD::SADDSAT:
8131 case ISD::UADDSAT:
8132 case ISD::UMAX:
8133 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8134 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8135 case ISD::SMAX:
8136 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8137 return N1.getOpcode() == ISD::UNDEF
8138 ? N2
8139 : getConstant(
8141 VT);
8142 case ISD::SMIN:
8143 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8144 return N1.getOpcode() == ISD::UNDEF
8145 ? N2
8146 : getConstant(
8148 VT);
8149 }
8150 }
8151
8152 // Perform trivial constant folding.
8153 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8154 return SV;
8155
8156 // Memoize this node if possible.
8157 SDNode *N;
8158 SDVTList VTs = getVTList(VT);
8159 SDValue Ops[] = {N1, N2};
8160 if (VT != MVT::Glue) {
8162 AddNodeIDNode(ID, Opcode, VTs, Ops);
8163 void *IP = nullptr;
8164 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8165 E->intersectFlagsWith(Flags);
8166 return SDValue(E, 0);
8167 }
8168
8169 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8170 N->setFlags(Flags);
8171 createOperands(N, Ops);
8172 CSEMap.InsertNode(N, IP);
8173 } else {
8174 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8175 createOperands(N, Ops);
8176 }
8177
8178 InsertNode(N);
8179 SDValue V = SDValue(N, 0);
8180 NewSDValueDbgMsg(V, "Creating new node: ", this);
8181 return V;
8182}
8183
8184SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8185 SDValue N1, SDValue N2, SDValue N3) {
8186 SDNodeFlags Flags;
8187 if (Inserter)
8188 Flags = Inserter->getFlags();
8189 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8190}
8191
8192SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8193 SDValue N1, SDValue N2, SDValue N3,
8194 const SDNodeFlags Flags) {
8196 N2.getOpcode() != ISD::DELETED_NODE &&
8197 N3.getOpcode() != ISD::DELETED_NODE &&
8198 "Operand is DELETED_NODE!");
8199 // Perform various simplifications.
8200 switch (Opcode) {
8201 case ISD::BUILD_VECTOR: {
8202 // Attempt to simplify BUILD_VECTOR.
8203 SDValue Ops[] = {N1, N2, N3};
8204 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8205 return V;
8206 break;
8207 }
8208 case ISD::CONCAT_VECTORS: {
8209 SDValue Ops[] = {N1, N2, N3};
8210 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8211 return V;
8212 break;
8213 }
8214 case ISD::SETCC: {
8215 assert(VT.isInteger() && "SETCC result type must be an integer!");
8216 assert(N1.getValueType() == N2.getValueType() &&
8217 "SETCC operands must have the same type!");
8218 assert(VT.isVector() == N1.getValueType().isVector() &&
8219 "SETCC type should be vector iff the operand type is vector!");
8220 assert((!VT.isVector() || VT.getVectorElementCount() ==
8222 "SETCC vector element counts must match!");
8223 // Use FoldSetCC to simplify SETCC's.
8224 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8225 return V;
8226 break;
8227 }
8228 case ISD::SELECT:
8229 case ISD::VSELECT:
8230 if (SDValue V = simplifySelect(N1, N2, N3))
8231 return V;
8232 break;
8234 llvm_unreachable("should use getVectorShuffle constructor!");
8235 case ISD::VECTOR_SPLICE: {
8236 if (cast<ConstantSDNode>(N3)->isZero())
8237 return N1;
8238 break;
8239 }
8241 assert(VT.isVector() && VT == N1.getValueType() &&
8242 "INSERT_VECTOR_ELT vector type mismatch");
8244 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8245 assert((!VT.isFloatingPoint() ||
8246 VT.getVectorElementType() == N2.getValueType()) &&
8247 "INSERT_VECTOR_ELT fp scalar type mismatch");
8248 assert((!VT.isInteger() ||
8250 "INSERT_VECTOR_ELT int scalar size mismatch");
8251
8252 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8253 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8254 // for scalable vectors where we will generate appropriate code to
8255 // deal with out-of-bounds cases correctly.
8256 if (N3C && VT.isFixedLengthVector() &&
8257 N3C->getZExtValue() >= VT.getVectorNumElements())
8258 return getUNDEF(VT);
8259
8260 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8261 if (N3.isUndef())
8262 return getUNDEF(VT);
8263
8264 // If inserting poison, just use the input vector.
8265 if (N2.getOpcode() == ISD::POISON)
8266 return N1;
8267
8268 // Inserting undef into undef/poison is still undef.
8269 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8270 return getUNDEF(VT);
8271
8272 // If the inserted element is an UNDEF, just use the input vector.
8273 // But not if skipping the insert could make the result more poisonous.
8274 if (N2.isUndef()) {
8275 if (N3C && VT.isFixedLengthVector()) {
8276 APInt EltMask =
8277 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8278 if (isGuaranteedNotToBePoison(N1, EltMask))
8279 return N1;
8280 } else if (isGuaranteedNotToBePoison(N1))
8281 return N1;
8282 }
8283 break;
8284 }
8285 case ISD::INSERT_SUBVECTOR: {
8286 // If inserting poison, just use the input vector,
8287 if (N2.getOpcode() == ISD::POISON)
8288 return N1;
8289
8290 // Inserting undef into undef/poison is still undef.
8291 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8292 return getUNDEF(VT);
8293
8294 EVT N2VT = N2.getValueType();
8295 assert(VT == N1.getValueType() &&
8296 "Dest and insert subvector source types must match!");
8297 assert(VT.isVector() && N2VT.isVector() &&
8298 "Insert subvector VTs must be vectors!");
8300 "Insert subvector VTs must have the same element type!");
8301 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8302 "Cannot insert a scalable vector into a fixed length vector!");
8303 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8305 "Insert subvector must be from smaller vector to larger vector!");
8307 "Insert subvector index must be constant");
8308 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8309 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8311 "Insert subvector overflow!");
8313 TLI->getVectorIdxWidth(getDataLayout()) &&
8314 "Constant index for INSERT_SUBVECTOR has an invalid size");
8315
8316 // Trivial insertion.
8317 if (VT == N2VT)
8318 return N2;
8319
8320 // If this is an insert of an extracted vector into an undef/poison vector,
8321 // we can just use the input to the extract. But not if skipping the
8322 // extract+insert could make the result more poisonous.
8323 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8324 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8325 if (N1.getOpcode() == ISD::POISON)
8326 return N2.getOperand(0);
8327 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8328 unsigned LoBit = N3->getAsZExtVal();
8329 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8330 APInt EltMask =
8331 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8332 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8333 return N2.getOperand(0);
8334 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8335 return N2.getOperand(0);
8336 }
8337
8338 // If the inserted subvector is UNDEF, just use the input vector.
8339 // But not if skipping the insert could make the result more poisonous.
8340 if (N2.isUndef()) {
8341 if (VT.isFixedLengthVector()) {
8342 unsigned LoBit = N3->getAsZExtVal();
8343 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8344 APInt EltMask =
8345 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8346 if (isGuaranteedNotToBePoison(N1, EltMask))
8347 return N1;
8348 } else if (isGuaranteedNotToBePoison(N1))
8349 return N1;
8350 }
8351 break;
8352 }
8353 case ISD::BITCAST:
8354 // Fold bit_convert nodes from a type to themselves.
8355 if (N1.getValueType() == VT)
8356 return N1;
8357 break;
8358 case ISD::VP_TRUNCATE:
8359 case ISD::VP_SIGN_EXTEND:
8360 case ISD::VP_ZERO_EXTEND:
8361 // Don't create noop casts.
8362 if (N1.getValueType() == VT)
8363 return N1;
8364 break;
8365 case ISD::VECTOR_COMPRESS: {
8366 [[maybe_unused]] EVT VecVT = N1.getValueType();
8367 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8368 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8369 assert(VT == VecVT && "Vector and result type don't match.");
8370 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8371 "All inputs must be vectors.");
8372 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8374 "Vector and mask must have same number of elements.");
8375
8376 if (N1.isUndef() || N2.isUndef())
8377 return N3;
8378
8379 break;
8380 }
8385 [[maybe_unused]] EVT AccVT = N1.getValueType();
8386 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8387 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8388 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8389 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8390 "node to have the same type!");
8391 assert(VT.isVector() && VT == AccVT &&
8392 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8393 "the same type as its result!");
8395 AccVT.getVectorElementCount()) &&
8396 "Expected the element count of the second and third operands of the "
8397 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8398 "element count of the first operand and the result!");
8400 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8401 "node to have an element type which is the same as or smaller than "
8402 "the element type of the first operand and result!");
8403 break;
8404 }
8405 }
8406
8407 // Perform trivial constant folding for arithmetic operators.
8408 switch (Opcode) {
8409 case ISD::FMA:
8410 case ISD::FMAD:
8411 case ISD::SETCC:
8412 case ISD::FSHL:
8413 case ISD::FSHR:
8414 if (SDValue SV =
8415 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8416 return SV;
8417 break;
8418 }
8419
8420 // Memoize node if it doesn't produce a glue result.
8421 SDNode *N;
8422 SDVTList VTs = getVTList(VT);
8423 SDValue Ops[] = {N1, N2, N3};
8424 if (VT != MVT::Glue) {
8426 AddNodeIDNode(ID, Opcode, VTs, Ops);
8427 void *IP = nullptr;
8428 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8429 E->intersectFlagsWith(Flags);
8430 return SDValue(E, 0);
8431 }
8432
8433 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8434 N->setFlags(Flags);
8435 createOperands(N, Ops);
8436 CSEMap.InsertNode(N, IP);
8437 } else {
8438 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8439 createOperands(N, Ops);
8440 }
8441
8442 InsertNode(N);
8443 SDValue V = SDValue(N, 0);
8444 NewSDValueDbgMsg(V, "Creating new node: ", this);
8445 return V;
8446}
8447
8448SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8449 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8450 const SDNodeFlags Flags) {
8451 SDValue Ops[] = { N1, N2, N3, N4 };
8452 return getNode(Opcode, DL, VT, Ops, Flags);
8453}
8454
8455SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8456 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8457 SDNodeFlags Flags;
8458 if (Inserter)
8459 Flags = Inserter->getFlags();
8460 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8461}
8462
8463SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8464 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8465 SDValue N5, const SDNodeFlags Flags) {
8466 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8467 return getNode(Opcode, DL, VT, Ops, Flags);
8468}
8469
8470SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8471 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8472 SDValue N5) {
8473 SDNodeFlags Flags;
8474 if (Inserter)
8475 Flags = Inserter->getFlags();
8476 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8477}
8478
8479/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8480/// the incoming stack arguments to be loaded from the stack.
8482 SmallVector<SDValue, 8> ArgChains;
8483
8484 // Include the original chain at the beginning of the list. When this is
8485 // used by target LowerCall hooks, this helps legalize find the
8486 // CALLSEQ_BEGIN node.
8487 ArgChains.push_back(Chain);
8488
8489 // Add a chain value for each stack argument.
8490 for (SDNode *U : getEntryNode().getNode()->users())
8491 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8492 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8493 if (FI->getIndex() < 0)
8494 ArgChains.push_back(SDValue(L, 1));
8495
8496 // Build a tokenfactor for all the chains.
8497 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8498}
8499
8500/// getMemsetValue - Vectorized representation of the memset value
8501/// operand.
8503 const SDLoc &dl) {
8504 assert(!Value.isUndef());
8505
8506 unsigned NumBits = VT.getScalarSizeInBits();
8508 assert(C->getAPIntValue().getBitWidth() == 8);
8509 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8510 if (VT.isInteger()) {
8511 bool IsOpaque = VT.getSizeInBits() > 64 ||
8512 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8513 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8514 }
8515 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8516 }
8517
8518 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8519 EVT IntVT = VT.getScalarType();
8520 if (!IntVT.isInteger())
8521 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8522
8523 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8524 if (NumBits > 8) {
8525 // Use a multiplication with 0x010101... to extend the input to the
8526 // required length.
8527 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8528 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8529 DAG.getConstant(Magic, dl, IntVT));
8530 }
8531
8532 if (VT != Value.getValueType() && !VT.isInteger())
8533 Value = DAG.getBitcast(VT.getScalarType(), Value);
8534 if (VT != Value.getValueType())
8535 Value = DAG.getSplatBuildVector(VT, dl, Value);
8536
8537 return Value;
8538}
8539
8540/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8541/// used when a memcpy is turned into a memset when the source is a constant
8542/// string ptr.
8544 const TargetLowering &TLI,
8545 const ConstantDataArraySlice &Slice) {
8546 // Handle vector with all elements zero.
8547 if (Slice.Array == nullptr) {
8548 if (VT.isInteger())
8549 return DAG.getConstant(0, dl, VT);
8550 return DAG.getNode(ISD::BITCAST, dl, VT,
8551 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8552 }
8553
8554 assert(!VT.isVector() && "Can't handle vector type here!");
8555 unsigned NumVTBits = VT.getSizeInBits();
8556 unsigned NumVTBytes = NumVTBits / 8;
8557 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8558
8559 APInt Val(NumVTBits, 0);
8560 if (DAG.getDataLayout().isLittleEndian()) {
8561 for (unsigned i = 0; i != NumBytes; ++i)
8562 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8563 } else {
8564 for (unsigned i = 0; i != NumBytes; ++i)
8565 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8566 }
8567
8568 // If the "cost" of materializing the integer immediate is less than the cost
8569 // of a load, then it is cost effective to turn the load into the immediate.
8570 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8571 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8572 return DAG.getConstant(Val, dl, VT);
8573 return SDValue();
8574}
8575
8577 const SDLoc &DL,
8578 const SDNodeFlags Flags) {
8579 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8580 return getMemBasePlusOffset(Base, Index, DL, Flags);
8581}
8582
8584 const SDLoc &DL,
8585 const SDNodeFlags Flags) {
8586 assert(Offset.getValueType().isInteger());
8587 EVT BasePtrVT = Ptr.getValueType();
8588 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8589 BasePtrVT))
8590 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8591 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8592 SDNodeFlags AddFlags = Flags;
8593 AddFlags.setInBounds(false);
8594 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8595}
8596
8597/// Returns true if memcpy source is constant data.
8599 uint64_t SrcDelta = 0;
8600 GlobalAddressSDNode *G = nullptr;
8601 if (Src.getOpcode() == ISD::GlobalAddress)
8603 else if (Src->isAnyAdd() &&
8604 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8605 Src.getOperand(1).getOpcode() == ISD::Constant) {
8606 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8607 SrcDelta = Src.getConstantOperandVal(1);
8608 }
8609 if (!G)
8610 return false;
8611
8612 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8613 SrcDelta + G->getOffset());
8614}
8615
8617 SelectionDAG &DAG) {
8618 // On Darwin, -Os means optimize for size without hurting performance, so
8619 // only really optimize for size when -Oz (MinSize) is used.
8621 return MF.getFunction().hasMinSize();
8622 return DAG.shouldOptForSize();
8623}
8624
8626 SmallVector<SDValue, 32> &OutChains, unsigned From,
8627 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8628 SmallVector<SDValue, 16> &OutStoreChains) {
8629 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8630 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8631 SmallVector<SDValue, 16> GluedLoadChains;
8632 for (unsigned i = From; i < To; ++i) {
8633 OutChains.push_back(OutLoadChains[i]);
8634 GluedLoadChains.push_back(OutLoadChains[i]);
8635 }
8636
8637 // Chain for all loads.
8638 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8639 GluedLoadChains);
8640
8641 for (unsigned i = From; i < To; ++i) {
8642 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8643 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8644 ST->getBasePtr(), ST->getMemoryVT(),
8645 ST->getMemOperand());
8646 OutChains.push_back(NewStore);
8647 }
8648}
8649
8651 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8652 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8653 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8654 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8655 // Turn a memcpy of undef to nop.
8656 // FIXME: We need to honor volatile even is Src is undef.
8657 if (Src.isUndef())
8658 return Chain;
8659
8660 // Expand memcpy to a series of load and store ops if the size operand falls
8661 // below a certain threshold.
8662 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8663 // rather than maybe a humongous number of loads and stores.
8664 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8665 const DataLayout &DL = DAG.getDataLayout();
8666 LLVMContext &C = *DAG.getContext();
8667 std::vector<EVT> MemOps;
8668 bool DstAlignCanChange = false;
8670 MachineFrameInfo &MFI = MF.getFrameInfo();
8671 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8673 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8674 DstAlignCanChange = true;
8675 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8676 if (!SrcAlign || Alignment > *SrcAlign)
8677 SrcAlign = Alignment;
8678 assert(SrcAlign && "SrcAlign must be set");
8680 // If marked as volatile, perform a copy even when marked as constant.
8681 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8682 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8683 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8684 const MemOp Op = isZeroConstant
8685 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8686 /*IsZeroMemset*/ true, isVol)
8687 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8688 *SrcAlign, isVol, CopyFromConstant);
8689 if (!TLI.findOptimalMemOpLowering(
8690 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8691 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8692 return SDValue();
8693
8694 if (DstAlignCanChange) {
8695 Type *Ty = MemOps[0].getTypeForEVT(C);
8696 Align NewAlign = DL.getABITypeAlign(Ty);
8697
8698 // Don't promote to an alignment that would require dynamic stack
8699 // realignment which may conflict with optimizations such as tail call
8700 // optimization.
8702 if (!TRI->hasStackRealignment(MF))
8703 if (MaybeAlign StackAlign = DL.getStackAlignment())
8704 NewAlign = std::min(NewAlign, *StackAlign);
8705
8706 if (NewAlign > Alignment) {
8707 // Give the stack frame object a larger alignment if needed.
8708 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8709 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8710 Alignment = NewAlign;
8711 }
8712 }
8713
8714 // Prepare AAInfo for loads/stores after lowering this memcpy.
8715 AAMDNodes NewAAInfo = AAInfo;
8716 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8717
8718 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8719 bool isConstant =
8720 BatchAA && SrcVal &&
8721 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8722
8723 MachineMemOperand::Flags MMOFlags =
8725 SmallVector<SDValue, 16> OutLoadChains;
8726 SmallVector<SDValue, 16> OutStoreChains;
8727 SmallVector<SDValue, 32> OutChains;
8728 unsigned NumMemOps = MemOps.size();
8729 uint64_t SrcOff = 0, DstOff = 0;
8730 for (unsigned i = 0; i != NumMemOps; ++i) {
8731 EVT VT = MemOps[i];
8732 unsigned VTSize = VT.getSizeInBits() / 8;
8733 SDValue Value, Store;
8734
8735 if (VTSize > Size) {
8736 // Issuing an unaligned load / store pair that overlaps with the previous
8737 // pair. Adjust the offset accordingly.
8738 assert(i == NumMemOps-1 && i != 0);
8739 SrcOff -= VTSize - Size;
8740 DstOff -= VTSize - Size;
8741 }
8742
8743 if (CopyFromConstant &&
8744 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8745 // It's unlikely a store of a vector immediate can be done in a single
8746 // instruction. It would require a load from a constantpool first.
8747 // We only handle zero vectors here.
8748 // FIXME: Handle other cases where store of vector immediate is done in
8749 // a single instruction.
8750 ConstantDataArraySlice SubSlice;
8751 if (SrcOff < Slice.Length) {
8752 SubSlice = Slice;
8753 SubSlice.move(SrcOff);
8754 } else {
8755 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8756 SubSlice.Array = nullptr;
8757 SubSlice.Offset = 0;
8758 SubSlice.Length = VTSize;
8759 }
8760 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8761 if (Value.getNode()) {
8762 Store = DAG.getStore(
8763 Chain, dl, Value,
8764 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8765 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8766 OutChains.push_back(Store);
8767 }
8768 }
8769
8770 if (!Store.getNode()) {
8771 // The type might not be legal for the target. This should only happen
8772 // if the type is smaller than a legal type, as on PPC, so the right
8773 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8774 // to Load/Store if NVT==VT.
8775 // FIXME does the case above also need this?
8776 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8777 assert(NVT.bitsGE(VT));
8778
8779 bool isDereferenceable =
8780 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8781 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8782 if (isDereferenceable)
8784 if (isConstant)
8785 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8786
8787 Value = DAG.getExtLoad(
8788 ISD::EXTLOAD, dl, NVT, Chain,
8789 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8790 SrcPtrInfo.getWithOffset(SrcOff), VT,
8791 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8792 OutLoadChains.push_back(Value.getValue(1));
8793
8794 Store = DAG.getTruncStore(
8795 Chain, dl, Value,
8796 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8797 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8798 OutStoreChains.push_back(Store);
8799 }
8800 SrcOff += VTSize;
8801 DstOff += VTSize;
8802 Size -= VTSize;
8803 }
8804
8805 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8807 unsigned NumLdStInMemcpy = OutStoreChains.size();
8808
8809 if (NumLdStInMemcpy) {
8810 // It may be that memcpy might be converted to memset if it's memcpy
8811 // of constants. In such a case, we won't have loads and stores, but
8812 // just stores. In the absence of loads, there is nothing to gang up.
8813 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8814 // If target does not care, just leave as it.
8815 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8816 OutChains.push_back(OutLoadChains[i]);
8817 OutChains.push_back(OutStoreChains[i]);
8818 }
8819 } else {
8820 // Ld/St less than/equal limit set by target.
8821 if (NumLdStInMemcpy <= GluedLdStLimit) {
8822 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8823 NumLdStInMemcpy, OutLoadChains,
8824 OutStoreChains);
8825 } else {
8826 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8827 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8828 unsigned GlueIter = 0;
8829
8830 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8831 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8832 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8833
8834 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8835 OutLoadChains, OutStoreChains);
8836 GlueIter += GluedLdStLimit;
8837 }
8838
8839 // Residual ld/st.
8840 if (RemainingLdStInMemcpy) {
8841 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8842 RemainingLdStInMemcpy, OutLoadChains,
8843 OutStoreChains);
8844 }
8845 }
8846 }
8847 }
8848 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8849}
8850
8852 SDValue Chain, SDValue Dst, SDValue Src,
8853 uint64_t Size, Align Alignment,
8854 bool isVol, bool AlwaysInline,
8855 MachinePointerInfo DstPtrInfo,
8856 MachinePointerInfo SrcPtrInfo,
8857 const AAMDNodes &AAInfo) {
8858 // Turn a memmove of undef to nop.
8859 // FIXME: We need to honor volatile even is Src is undef.
8860 if (Src.isUndef())
8861 return Chain;
8862
8863 // Expand memmove to a series of load and store ops if the size operand falls
8864 // below a certain threshold.
8865 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8866 const DataLayout &DL = DAG.getDataLayout();
8867 LLVMContext &C = *DAG.getContext();
8868 std::vector<EVT> MemOps;
8869 bool DstAlignCanChange = false;
8871 MachineFrameInfo &MFI = MF.getFrameInfo();
8872 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8874 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8875 DstAlignCanChange = true;
8876 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8877 if (!SrcAlign || Alignment > *SrcAlign)
8878 SrcAlign = Alignment;
8879 assert(SrcAlign && "SrcAlign must be set");
8880 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8881 if (!TLI.findOptimalMemOpLowering(
8882 C, MemOps, Limit,
8883 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8884 /*IsVolatile*/ true),
8885 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8886 MF.getFunction().getAttributes()))
8887 return SDValue();
8888
8889 if (DstAlignCanChange) {
8890 Type *Ty = MemOps[0].getTypeForEVT(C);
8891 Align NewAlign = DL.getABITypeAlign(Ty);
8892
8893 // Don't promote to an alignment that would require dynamic stack
8894 // realignment which may conflict with optimizations such as tail call
8895 // optimization.
8897 if (!TRI->hasStackRealignment(MF))
8898 if (MaybeAlign StackAlign = DL.getStackAlignment())
8899 NewAlign = std::min(NewAlign, *StackAlign);
8900
8901 if (NewAlign > Alignment) {
8902 // Give the stack frame object a larger alignment if needed.
8903 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8904 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8905 Alignment = NewAlign;
8906 }
8907 }
8908
8909 // Prepare AAInfo for loads/stores after lowering this memmove.
8910 AAMDNodes NewAAInfo = AAInfo;
8911 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8912
8913 MachineMemOperand::Flags MMOFlags =
8915 uint64_t SrcOff = 0, DstOff = 0;
8916 SmallVector<SDValue, 8> LoadValues;
8917 SmallVector<SDValue, 8> LoadChains;
8918 SmallVector<SDValue, 8> OutChains;
8919 unsigned NumMemOps = MemOps.size();
8920 for (unsigned i = 0; i < NumMemOps; i++) {
8921 EVT VT = MemOps[i];
8922 unsigned VTSize = VT.getSizeInBits() / 8;
8923 SDValue Value;
8924
8925 bool isDereferenceable =
8926 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8927 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8928 if (isDereferenceable)
8930
8931 Value = DAG.getLoad(
8932 VT, dl, Chain,
8933 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8934 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
8935 LoadValues.push_back(Value);
8936 LoadChains.push_back(Value.getValue(1));
8937 SrcOff += VTSize;
8938 }
8939 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
8940 OutChains.clear();
8941 for (unsigned i = 0; i < NumMemOps; i++) {
8942 EVT VT = MemOps[i];
8943 unsigned VTSize = VT.getSizeInBits() / 8;
8944 SDValue Store;
8945
8946 Store = DAG.getStore(
8947 Chain, dl, LoadValues[i],
8948 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8949 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8950 OutChains.push_back(Store);
8951 DstOff += VTSize;
8952 }
8953
8954 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8955}
8956
8957/// Lower the call to 'memset' intrinsic function into a series of store
8958/// operations.
8959///
8960/// \param DAG Selection DAG where lowered code is placed.
8961/// \param dl Link to corresponding IR location.
8962/// \param Chain Control flow dependency.
8963/// \param Dst Pointer to destination memory location.
8964/// \param Src Value of byte to write into the memory.
8965/// \param Size Number of bytes to write.
8966/// \param Alignment Alignment of the destination in bytes.
8967/// \param isVol True if destination is volatile.
8968/// \param AlwaysInline Makes sure no function call is generated.
8969/// \param DstPtrInfo IR information on the memory pointer.
8970/// \returns New head in the control flow, if lowering was successful, empty
8971/// SDValue otherwise.
8972///
8973/// The function tries to replace 'llvm.memset' intrinsic with several store
8974/// operations and value calculation code. This is usually profitable for small
8975/// memory size or when the semantic requires inlining.
8977 SDValue Chain, SDValue Dst, SDValue Src,
8978 uint64_t Size, Align Alignment, bool isVol,
8979 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
8980 const AAMDNodes &AAInfo) {
8981 // Turn a memset of undef to nop.
8982 // FIXME: We need to honor volatile even is Src is undef.
8983 if (Src.isUndef())
8984 return Chain;
8985
8986 // Expand memset to a series of load/store ops if the size operand
8987 // falls below a certain threshold.
8988 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8989 std::vector<EVT> MemOps;
8990 bool DstAlignCanChange = false;
8991 LLVMContext &C = *DAG.getContext();
8993 MachineFrameInfo &MFI = MF.getFrameInfo();
8994 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8996 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8997 DstAlignCanChange = true;
8998 bool IsZeroVal = isNullConstant(Src);
8999 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9000
9001 if (!TLI.findOptimalMemOpLowering(
9002 C, MemOps, Limit,
9003 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9004 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9005 return SDValue();
9006
9007 if (DstAlignCanChange) {
9008 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9009 const DataLayout &DL = DAG.getDataLayout();
9010 Align NewAlign = DL.getABITypeAlign(Ty);
9011
9012 // Don't promote to an alignment that would require dynamic stack
9013 // realignment which may conflict with optimizations such as tail call
9014 // optimization.
9016 if (!TRI->hasStackRealignment(MF))
9017 if (MaybeAlign StackAlign = DL.getStackAlignment())
9018 NewAlign = std::min(NewAlign, *StackAlign);
9019
9020 if (NewAlign > Alignment) {
9021 // Give the stack frame object a larger alignment if needed.
9022 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9023 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9024 Alignment = NewAlign;
9025 }
9026 }
9027
9028 SmallVector<SDValue, 8> OutChains;
9029 uint64_t DstOff = 0;
9030 unsigned NumMemOps = MemOps.size();
9031
9032 // Find the largest store and generate the bit pattern for it.
9033 EVT LargestVT = MemOps[0];
9034 for (unsigned i = 1; i < NumMemOps; i++)
9035 if (MemOps[i].bitsGT(LargestVT))
9036 LargestVT = MemOps[i];
9037 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9038
9039 // Prepare AAInfo for loads/stores after lowering this memset.
9040 AAMDNodes NewAAInfo = AAInfo;
9041 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9042
9043 for (unsigned i = 0; i < NumMemOps; i++) {
9044 EVT VT = MemOps[i];
9045 unsigned VTSize = VT.getSizeInBits() / 8;
9046 if (VTSize > Size) {
9047 // Issuing an unaligned load / store pair that overlaps with the previous
9048 // pair. Adjust the offset accordingly.
9049 assert(i == NumMemOps-1 && i != 0);
9050 DstOff -= VTSize - Size;
9051 }
9052
9053 // If this store is smaller than the largest store see whether we can get
9054 // the smaller value for free with a truncate or extract vector element and
9055 // then store.
9056 SDValue Value = MemSetValue;
9057 if (VT.bitsLT(LargestVT)) {
9058 unsigned Index;
9059 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9060 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9061 if (!LargestVT.isVector() && !VT.isVector() &&
9062 TLI.isTruncateFree(LargestVT, VT))
9063 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9064 else if (LargestVT.isVector() && !VT.isVector() &&
9066 LargestVT.getTypeForEVT(*DAG.getContext()),
9067 VT.getSizeInBits(), Index) &&
9068 TLI.isTypeLegal(SVT) &&
9069 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9070 // Target which can combine store(extractelement VectorTy, Idx) can get
9071 // the smaller value for free.
9072 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9073 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9074 } else
9075 Value = getMemsetValue(Src, VT, DAG, dl);
9076 }
9077 assert(Value.getValueType() == VT && "Value with wrong type.");
9078 SDValue Store = DAG.getStore(
9079 Chain, dl, Value,
9080 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9081 DstPtrInfo.getWithOffset(DstOff), Alignment,
9083 NewAAInfo);
9084 OutChains.push_back(Store);
9085 DstOff += VT.getSizeInBits() / 8;
9086 Size -= VTSize;
9087 }
9088
9089 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9090}
9091
9093 unsigned AS) {
9094 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9095 // pointer operands can be losslessly bitcasted to pointers of address space 0
9096 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9097 report_fatal_error("cannot lower memory intrinsic in address space " +
9098 Twine(AS));
9099 }
9100}
9101
9103 const SelectionDAG *SelDAG,
9104 bool AllowReturnsFirstArg) {
9105 if (!CI || !CI->isTailCall())
9106 return false;
9107 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9108 // helper symbol we lower to.
9109 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9110 AllowReturnsFirstArg &&
9112}
9113
9114std::pair<SDValue, SDValue>
9116 SDValue Mem1, SDValue Size, const CallInst *CI) {
9117 RTLIB::LibcallImpl MemcmpImpl = TLI->getLibcallImpl(RTLIB::MEMCMP);
9118 if (MemcmpImpl == RTLIB::Unsupported)
9119 return {};
9120
9123 {Mem0, PT},
9124 {Mem1, PT},
9126
9128 bool IsTailCall =
9129 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9130
9131 CLI.setDebugLoc(dl)
9132 .setChain(Chain)
9133 .setLibCallee(
9134 TLI->getLibcallImplCallingConv(MemcmpImpl),
9136 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9137 std::move(Args))
9138 .setTailCall(IsTailCall);
9139
9140 return TLI->LowerCallTo(CLI);
9141}
9142
9143std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9144 const SDLoc &dl,
9145 SDValue Src,
9146 const CallInst *CI) {
9147 RTLIB::LibcallImpl StrlenImpl = TLI->getLibcallImpl(RTLIB::STRLEN);
9148 if (StrlenImpl == RTLIB::Unsupported)
9149 return {};
9150
9151 // Emit a library call.
9154
9156 bool IsTailCall =
9157 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9158
9159 CLI.setDebugLoc(dl)
9160 .setChain(Chain)
9161 .setLibCallee(TLI->getLibcallImplCallingConv(StrlenImpl), CI->getType(),
9163 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9164 std::move(Args))
9165 .setTailCall(IsTailCall);
9166
9167 return TLI->LowerCallTo(CLI);
9168}
9169
9171 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9172 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9173 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9174 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9175 BatchAAResults *BatchAA) {
9176 // Check to see if we should lower the memcpy to loads and stores first.
9177 // For cases within the target-specified limits, this is the best choice.
9179 if (ConstantSize) {
9180 // Memcpy with size zero? Just return the original chain.
9181 if (ConstantSize->isZero())
9182 return Chain;
9183
9185 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9186 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9187 if (Result.getNode())
9188 return Result;
9189 }
9190
9191 // Then check to see if we should lower the memcpy with target-specific
9192 // code. If the target chooses to do this, this is the next best.
9193 if (TSI) {
9194 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9195 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9196 DstPtrInfo, SrcPtrInfo);
9197 if (Result.getNode())
9198 return Result;
9199 }
9200
9201 // If we really need inline code and the target declined to provide it,
9202 // use a (potentially long) sequence of loads and stores.
9203 if (AlwaysInline) {
9204 assert(ConstantSize && "AlwaysInline requires a constant size!");
9206 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9207 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9208 }
9209
9212
9213 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9214 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9215 // respect volatile, so they may do things like read or write memory
9216 // beyond the given memory regions. But fixing this isn't easy, and most
9217 // people don't care.
9218
9219 // Emit a library call.
9222 Args.emplace_back(Dst, PtrTy);
9223 Args.emplace_back(Src, PtrTy);
9224 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9225 // FIXME: pass in SDLoc
9227 bool IsTailCall = false;
9228 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9229
9230 if (OverrideTailCall.has_value()) {
9231 IsTailCall = *OverrideTailCall;
9232 } else {
9233 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9234 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9235 }
9236
9237 CLI.setDebugLoc(dl)
9238 .setChain(Chain)
9239 .setLibCallee(
9240 TLI->getLibcallImplCallingConv(MemCpyImpl),
9241 Dst.getValueType().getTypeForEVT(*getContext()),
9242 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9243 std::move(Args))
9245 .setTailCall(IsTailCall);
9246
9247 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9248 return CallResult.second;
9249}
9250
9252 SDValue Dst, SDValue Src, SDValue Size,
9253 Type *SizeTy, unsigned ElemSz,
9254 bool isTailCall,
9255 MachinePointerInfo DstPtrInfo,
9256 MachinePointerInfo SrcPtrInfo) {
9257 // Emit a library call.
9260 Args.emplace_back(Dst, ArgTy);
9261 Args.emplace_back(Src, ArgTy);
9262 Args.emplace_back(Size, SizeTy);
9263
9264 RTLIB::Libcall LibraryCall =
9266 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9267 if (LibcallImpl == RTLIB::Unsupported)
9268 report_fatal_error("Unsupported element size");
9269
9271 CLI.setDebugLoc(dl)
9272 .setChain(Chain)
9273 .setLibCallee(
9274 TLI->getLibcallImplCallingConv(LibcallImpl),
9276 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9277 std::move(Args))
9279 .setTailCall(isTailCall);
9280
9281 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9282 return CallResult.second;
9283}
9284
9286 SDValue Src, SDValue Size, Align Alignment,
9287 bool isVol, const CallInst *CI,
9288 std::optional<bool> OverrideTailCall,
9289 MachinePointerInfo DstPtrInfo,
9290 MachinePointerInfo SrcPtrInfo,
9291 const AAMDNodes &AAInfo,
9292 BatchAAResults *BatchAA) {
9293 // Check to see if we should lower the memmove to loads and stores first.
9294 // For cases within the target-specified limits, this is the best choice.
9296 if (ConstantSize) {
9297 // Memmove with size zero? Just return the original chain.
9298 if (ConstantSize->isZero())
9299 return Chain;
9300
9302 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9303 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9304 if (Result.getNode())
9305 return Result;
9306 }
9307
9308 // Then check to see if we should lower the memmove with target-specific
9309 // code. If the target chooses to do this, this is the next best.
9310 if (TSI) {
9311 SDValue Result =
9312 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9313 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9314 if (Result.getNode())
9315 return Result;
9316 }
9317
9320
9321 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9322 // not be safe. See memcpy above for more details.
9323
9324 // Emit a library call.
9327 Args.emplace_back(Dst, PtrTy);
9328 Args.emplace_back(Src, PtrTy);
9329 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9330 // FIXME: pass in SDLoc
9332
9333 RTLIB::LibcallImpl MemmoveImpl = TLI->getLibcallImpl(RTLIB::MEMMOVE);
9334
9335 bool IsTailCall = false;
9336 if (OverrideTailCall.has_value()) {
9337 IsTailCall = *OverrideTailCall;
9338 } else {
9339 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9340 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9341 }
9342
9343 CLI.setDebugLoc(dl)
9344 .setChain(Chain)
9345 .setLibCallee(
9346 TLI->getLibcallImplCallingConv(MemmoveImpl),
9347 Dst.getValueType().getTypeForEVT(*getContext()),
9348 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9349 std::move(Args))
9351 .setTailCall(IsTailCall);
9352
9353 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9354 return CallResult.second;
9355}
9356
9358 SDValue Dst, SDValue Src, SDValue Size,
9359 Type *SizeTy, unsigned ElemSz,
9360 bool isTailCall,
9361 MachinePointerInfo DstPtrInfo,
9362 MachinePointerInfo SrcPtrInfo) {
9363 // Emit a library call.
9365 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9366 Args.emplace_back(Dst, IntPtrTy);
9367 Args.emplace_back(Src, IntPtrTy);
9368 Args.emplace_back(Size, SizeTy);
9369
9370 RTLIB::Libcall LibraryCall =
9372 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9373 if (LibcallImpl == RTLIB::Unsupported)
9374 report_fatal_error("Unsupported element size");
9375
9377 CLI.setDebugLoc(dl)
9378 .setChain(Chain)
9379 .setLibCallee(
9380 TLI->getLibcallImplCallingConv(LibcallImpl),
9382 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9383 std::move(Args))
9385 .setTailCall(isTailCall);
9386
9387 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9388 return CallResult.second;
9389}
9390
9392 SDValue Src, SDValue Size, Align Alignment,
9393 bool isVol, bool AlwaysInline,
9394 const CallInst *CI,
9395 MachinePointerInfo DstPtrInfo,
9396 const AAMDNodes &AAInfo) {
9397 // Check to see if we should lower the memset to stores first.
9398 // For cases within the target-specified limits, this is the best choice.
9400 if (ConstantSize) {
9401 // Memset with size zero? Just return the original chain.
9402 if (ConstantSize->isZero())
9403 return Chain;
9404
9405 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9406 ConstantSize->getZExtValue(), Alignment,
9407 isVol, false, DstPtrInfo, AAInfo);
9408
9409 if (Result.getNode())
9410 return Result;
9411 }
9412
9413 // Then check to see if we should lower the memset with target-specific
9414 // code. If the target chooses to do this, this is the next best.
9415 if (TSI) {
9416 SDValue Result = TSI->EmitTargetCodeForMemset(
9417 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9418 if (Result.getNode())
9419 return Result;
9420 }
9421
9422 // If we really need inline code and the target declined to provide it,
9423 // use a (potentially long) sequence of loads and stores.
9424 if (AlwaysInline) {
9425 assert(ConstantSize && "AlwaysInline requires a constant size!");
9426 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9427 ConstantSize->getZExtValue(), Alignment,
9428 isVol, true, DstPtrInfo, AAInfo);
9429 assert(Result &&
9430 "getMemsetStores must return a valid sequence when AlwaysInline");
9431 return Result;
9432 }
9433
9435
9436 // Emit a library call.
9437 auto &Ctx = *getContext();
9438 const auto& DL = getDataLayout();
9439
9441 // FIXME: pass in SDLoc
9442 CLI.setDebugLoc(dl).setChain(Chain);
9443
9444 RTLIB::LibcallImpl BzeroImpl = TLI->getLibcallImpl(RTLIB::BZERO);
9445 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9446
9447 // If zeroing out and bzero is present, use it.
9448 if (UseBZero) {
9450 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9451 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9452 CLI.setLibCallee(
9453 TLI->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9454 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9455 } else {
9456 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9457
9459 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9460 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9461 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9462 CLI.setLibCallee(TLI->getLibcallImplCallingConv(MemsetImpl),
9463 Dst.getValueType().getTypeForEVT(Ctx),
9464 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9465 std::move(Args));
9466 }
9467
9468 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9469 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9470
9471 // If we're going to use bzero, make sure not to tail call unless the
9472 // subsequent return doesn't need a value, as bzero doesn't return the first
9473 // arg unlike memset.
9474 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9475 bool IsTailCall =
9476 CI && CI->isTailCall() &&
9477 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9478 CLI.setDiscardResult().setTailCall(IsTailCall);
9479
9480 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9481 return CallResult.second;
9482}
9483
9486 Type *SizeTy, unsigned ElemSz,
9487 bool isTailCall,
9488 MachinePointerInfo DstPtrInfo) {
9489 // Emit a library call.
9491 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9492 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9493 Args.emplace_back(Size, SizeTy);
9494
9495 RTLIB::Libcall LibraryCall =
9497 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9498 if (LibcallImpl == RTLIB::Unsupported)
9499 report_fatal_error("Unsupported element size");
9500
9502 CLI.setDebugLoc(dl)
9503 .setChain(Chain)
9504 .setLibCallee(
9505 TLI->getLibcallImplCallingConv(LibcallImpl),
9507 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9508 std::move(Args))
9510 .setTailCall(isTailCall);
9511
9512 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9513 return CallResult.second;
9514}
9515
9516SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9518 MachineMemOperand *MMO,
9519 ISD::LoadExtType ExtType) {
9521 AddNodeIDNode(ID, Opcode, VTList, Ops);
9522 ID.AddInteger(MemVT.getRawBits());
9523 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9524 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9525 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9526 ID.AddInteger(MMO->getFlags());
9527 void* IP = nullptr;
9528 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9529 E->refineAlignment(MMO);
9530 E->refineRanges(MMO);
9531 return SDValue(E, 0);
9532 }
9533
9534 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9535 VTList, MemVT, MMO, ExtType);
9536 createOperands(N, Ops);
9537
9538 CSEMap.InsertNode(N, IP);
9539 InsertNode(N);
9540 SDValue V(N, 0);
9541 NewSDValueDbgMsg(V, "Creating new node: ", this);
9542 return V;
9543}
9544
9546 EVT MemVT, SDVTList VTs, SDValue Chain,
9547 SDValue Ptr, SDValue Cmp, SDValue Swp,
9548 MachineMemOperand *MMO) {
9549 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9551 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9552
9553 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9554 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9555}
9556
9557SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9558 SDValue Chain, SDValue Ptr, SDValue Val,
9559 MachineMemOperand *MMO) {
9560 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9561 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9562 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9563 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9564 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9565 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9566 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9567 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9568 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9569 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9570 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9571 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9572 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9573 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9574 Opcode == ISD::ATOMIC_STORE) &&
9575 "Invalid Atomic Op");
9576
9577 EVT VT = Val.getValueType();
9578
9579 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9580 getVTList(VT, MVT::Other);
9581 SDValue Ops[] = {Chain, Ptr, Val};
9582 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9583}
9584
9586 EVT MemVT, EVT VT, SDValue Chain,
9587 SDValue Ptr, MachineMemOperand *MMO) {
9588 SDVTList VTs = getVTList(VT, MVT::Other);
9589 SDValue Ops[] = {Chain, Ptr};
9590 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9591}
9592
9593/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9595 if (Ops.size() == 1)
9596 return Ops[0];
9597
9599 VTs.reserve(Ops.size());
9600 for (const SDValue &Op : Ops)
9601 VTs.push_back(Op.getValueType());
9602 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9603}
9604
9606 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9607 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9609 const AAMDNodes &AAInfo) {
9610 if (Size.hasValue() && !Size.getValue())
9612
9614 MachineMemOperand *MMO =
9615 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9616
9617 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9618}
9619
9621 SDVTList VTList,
9622 ArrayRef<SDValue> Ops, EVT MemVT,
9623 MachineMemOperand *MMO) {
9624 assert(
9625 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9626 Opcode == ISD::PREFETCH ||
9627 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9628 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9629 "Opcode is not a memory-accessing opcode!");
9630
9631 // Memoize the node unless it returns a glue result.
9633 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9635 AddNodeIDNode(ID, Opcode, VTList, Ops);
9636 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9637 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9638 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9639 ID.AddInteger(MMO->getFlags());
9640 ID.AddInteger(MemVT.getRawBits());
9641 void *IP = nullptr;
9642 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9643 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9644 return SDValue(E, 0);
9645 }
9646
9647 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9648 VTList, MemVT, MMO);
9649 createOperands(N, Ops);
9650
9651 CSEMap.InsertNode(N, IP);
9652 } else {
9653 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9654 VTList, MemVT, MMO);
9655 createOperands(N, Ops);
9656 }
9657 InsertNode(N);
9658 SDValue V(N, 0);
9659 NewSDValueDbgMsg(V, "Creating new node: ", this);
9660 return V;
9661}
9662
9664 SDValue Chain, int FrameIndex) {
9665 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9666 const auto VTs = getVTList(MVT::Other);
9667 SDValue Ops[2] = {
9668 Chain,
9669 getFrameIndex(FrameIndex,
9670 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9671 true)};
9672
9674 AddNodeIDNode(ID, Opcode, VTs, Ops);
9675 ID.AddInteger(FrameIndex);
9676 void *IP = nullptr;
9677 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9678 return SDValue(E, 0);
9679
9680 LifetimeSDNode *N =
9681 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9682 createOperands(N, Ops);
9683 CSEMap.InsertNode(N, IP);
9684 InsertNode(N);
9685 SDValue V(N, 0);
9686 NewSDValueDbgMsg(V, "Creating new node: ", this);
9687 return V;
9688}
9689
9691 uint64_t Guid, uint64_t Index,
9692 uint32_t Attr) {
9693 const unsigned Opcode = ISD::PSEUDO_PROBE;
9694 const auto VTs = getVTList(MVT::Other);
9695 SDValue Ops[] = {Chain};
9697 AddNodeIDNode(ID, Opcode, VTs, Ops);
9698 ID.AddInteger(Guid);
9699 ID.AddInteger(Index);
9700 void *IP = nullptr;
9701 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9702 return SDValue(E, 0);
9703
9704 auto *N = newSDNode<PseudoProbeSDNode>(
9705 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9706 createOperands(N, Ops);
9707 CSEMap.InsertNode(N, IP);
9708 InsertNode(N);
9709 SDValue V(N, 0);
9710 NewSDValueDbgMsg(V, "Creating new node: ", this);
9711 return V;
9712}
9713
9714/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9715/// MachinePointerInfo record from it. This is particularly useful because the
9716/// code generator has many cases where it doesn't bother passing in a
9717/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9719 SelectionDAG &DAG, SDValue Ptr,
9720 int64_t Offset = 0) {
9721 // If this is FI+Offset, we can model it.
9722 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9724 FI->getIndex(), Offset);
9725
9726 // If this is (FI+Offset1)+Offset2, we can model it.
9727 if (Ptr.getOpcode() != ISD::ADD ||
9730 return Info;
9731
9732 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9734 DAG.getMachineFunction(), FI,
9735 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9736}
9737
9738/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9739/// MachinePointerInfo record from it. This is particularly useful because the
9740/// code generator has many cases where it doesn't bother passing in a
9741/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9743 SelectionDAG &DAG, SDValue Ptr,
9744 SDValue OffsetOp) {
9745 // If the 'Offset' value isn't a constant, we can't handle this.
9747 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9748 if (OffsetOp.isUndef())
9749 return InferPointerInfo(Info, DAG, Ptr);
9750 return Info;
9751}
9752
9754 EVT VT, const SDLoc &dl, SDValue Chain,
9755 SDValue Ptr, SDValue Offset,
9756 MachinePointerInfo PtrInfo, EVT MemVT,
9757 Align Alignment,
9758 MachineMemOperand::Flags MMOFlags,
9759 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9760 assert(Chain.getValueType() == MVT::Other &&
9761 "Invalid chain type");
9762
9763 MMOFlags |= MachineMemOperand::MOLoad;
9764 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9765 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9766 // clients.
9767 if (PtrInfo.V.isNull())
9768 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9769
9770 TypeSize Size = MemVT.getStoreSize();
9772 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9773 Alignment, AAInfo, Ranges);
9774 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9775}
9776
9778 EVT VT, const SDLoc &dl, SDValue Chain,
9779 SDValue Ptr, SDValue Offset, EVT MemVT,
9780 MachineMemOperand *MMO) {
9781 if (VT == MemVT) {
9782 ExtType = ISD::NON_EXTLOAD;
9783 } else if (ExtType == ISD::NON_EXTLOAD) {
9784 assert(VT == MemVT && "Non-extending load from different memory type!");
9785 } else {
9786 // Extending load.
9787 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9788 "Should only be an extending load, not truncating!");
9789 assert(VT.isInteger() == MemVT.isInteger() &&
9790 "Cannot convert from FP to Int or Int -> FP!");
9791 assert(VT.isVector() == MemVT.isVector() &&
9792 "Cannot use an ext load to convert to or from a vector!");
9793 assert((!VT.isVector() ||
9795 "Cannot use an ext load to change the number of vector elements!");
9796 }
9797
9798 assert((!MMO->getRanges() ||
9800 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9801 MemVT.isInteger())) &&
9802 "Range metadata and load type must match!");
9803
9804 bool Indexed = AM != ISD::UNINDEXED;
9805 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9806
9807 SDVTList VTs = Indexed ?
9808 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9809 SDValue Ops[] = { Chain, Ptr, Offset };
9811 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9812 ID.AddInteger(MemVT.getRawBits());
9813 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9814 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9815 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9816 ID.AddInteger(MMO->getFlags());
9817 void *IP = nullptr;
9818 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9819 E->refineAlignment(MMO);
9820 E->refineRanges(MMO);
9821 return SDValue(E, 0);
9822 }
9823 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9824 ExtType, MemVT, MMO);
9825 createOperands(N, Ops);
9826
9827 CSEMap.InsertNode(N, IP);
9828 InsertNode(N);
9829 SDValue V(N, 0);
9830 NewSDValueDbgMsg(V, "Creating new node: ", this);
9831 return V;
9832}
9833
9835 SDValue Ptr, MachinePointerInfo PtrInfo,
9836 MaybeAlign Alignment,
9837 MachineMemOperand::Flags MMOFlags,
9838 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9839 SDValue Undef = getUNDEF(Ptr.getValueType());
9840 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9841 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9842}
9843
9845 SDValue Ptr, MachineMemOperand *MMO) {
9846 SDValue Undef = getUNDEF(Ptr.getValueType());
9847 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9848 VT, MMO);
9849}
9850
9852 EVT VT, SDValue Chain, SDValue Ptr,
9853 MachinePointerInfo PtrInfo, EVT MemVT,
9854 MaybeAlign Alignment,
9855 MachineMemOperand::Flags MMOFlags,
9856 const AAMDNodes &AAInfo) {
9857 SDValue Undef = getUNDEF(Ptr.getValueType());
9858 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9859 MemVT, Alignment, MMOFlags, AAInfo);
9860}
9861
9863 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9864 MachineMemOperand *MMO) {
9865 SDValue Undef = getUNDEF(Ptr.getValueType());
9866 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
9867 MemVT, MMO);
9868}
9869
9873 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
9874 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
9875 // Don't propagate the invariant or dereferenceable flags.
9876 auto MMOFlags =
9877 LD->getMemOperand()->getFlags() &
9879 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
9880 LD->getChain(), Base, Offset, LD->getPointerInfo(),
9881 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
9882}
9883
9885 SDValue Ptr, MachinePointerInfo PtrInfo,
9886 Align Alignment,
9887 MachineMemOperand::Flags MMOFlags,
9888 const AAMDNodes &AAInfo) {
9889 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9890
9891 MMOFlags |= MachineMemOperand::MOStore;
9892 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9893
9894 if (PtrInfo.V.isNull())
9895 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9896
9899 MachineMemOperand *MMO =
9900 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
9901 return getStore(Chain, dl, Val, Ptr, MMO);
9902}
9903
9905 SDValue Ptr, MachineMemOperand *MMO) {
9906 SDValue Undef = getUNDEF(Ptr.getValueType());
9907 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
9909}
9910
9912 SDValue Ptr, SDValue Offset, EVT SVT,
9914 bool IsTruncating) {
9915 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
9916 EVT VT = Val.getValueType();
9917 if (VT == SVT) {
9918 IsTruncating = false;
9919 } else if (!IsTruncating) {
9920 assert(VT == SVT && "No-truncating store from different memory type!");
9921 } else {
9923 "Should only be a truncating store, not extending!");
9924 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
9925 assert(VT.isVector() == SVT.isVector() &&
9926 "Cannot use trunc store to convert to or from a vector!");
9927 assert((!VT.isVector() ||
9929 "Cannot use trunc store to change the number of vector elements!");
9930 }
9931
9932 bool Indexed = AM != ISD::UNINDEXED;
9933 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
9934 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
9935 : getVTList(MVT::Other);
9936 SDValue Ops[] = {Chain, Val, Ptr, Offset};
9939 ID.AddInteger(SVT.getRawBits());
9940 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
9941 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
9942 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9943 ID.AddInteger(MMO->getFlags());
9944 void *IP = nullptr;
9945 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9946 cast<StoreSDNode>(E)->refineAlignment(MMO);
9947 return SDValue(E, 0);
9948 }
9949 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9950 IsTruncating, SVT, MMO);
9951 createOperands(N, Ops);
9952
9953 CSEMap.InsertNode(N, IP);
9954 InsertNode(N);
9955 SDValue V(N, 0);
9956 NewSDValueDbgMsg(V, "Creating new node: ", this);
9957 return V;
9958}
9959
9961 SDValue Ptr, MachinePointerInfo PtrInfo,
9962 EVT SVT, Align Alignment,
9963 MachineMemOperand::Flags MMOFlags,
9964 const AAMDNodes &AAInfo) {
9965 assert(Chain.getValueType() == MVT::Other &&
9966 "Invalid chain type");
9967
9968 MMOFlags |= MachineMemOperand::MOStore;
9969 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
9970
9971 if (PtrInfo.V.isNull())
9972 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
9973
9975 MachineMemOperand *MMO = MF.getMachineMemOperand(
9976 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
9977 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
9978}
9979
9981 SDValue Ptr, EVT SVT,
9982 MachineMemOperand *MMO) {
9983 SDValue Undef = getUNDEF(Ptr.getValueType());
9984 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
9985}
9986
9990 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
9991 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
9992 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
9993 ST->getMemoryVT(), ST->getMemOperand(), AM,
9994 ST->isTruncatingStore());
9995}
9996
9998 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
9999 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10000 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10001 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10002 const MDNode *Ranges, bool IsExpanding) {
10003 MMOFlags |= MachineMemOperand::MOLoad;
10004 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10005 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10006 // clients.
10007 if (PtrInfo.V.isNull())
10008 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10009
10010 TypeSize Size = MemVT.getStoreSize();
10012 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10013 Alignment, AAInfo, Ranges);
10014 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10015 MMO, IsExpanding);
10016}
10017
10019 ISD::LoadExtType ExtType, EVT VT,
10020 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10021 SDValue Offset, SDValue Mask, SDValue EVL,
10022 EVT MemVT, MachineMemOperand *MMO,
10023 bool IsExpanding) {
10024 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10025 assert(Mask.getValueType().getVectorElementCount() ==
10026 VT.getVectorElementCount() &&
10027 "Vector width mismatch between mask and data");
10028
10029 bool Indexed = AM != ISD::UNINDEXED;
10030 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10031
10032 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10033 : getVTList(VT, MVT::Other);
10034 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10036 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10037 ID.AddInteger(MemVT.getRawBits());
10038 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10039 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10040 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10041 ID.AddInteger(MMO->getFlags());
10042 void *IP = nullptr;
10043 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10044 E->refineAlignment(MMO);
10045 E->refineRanges(MMO);
10046 return SDValue(E, 0);
10047 }
10048 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10049 ExtType, IsExpanding, MemVT, MMO);
10050 createOperands(N, Ops);
10051
10052 CSEMap.InsertNode(N, IP);
10053 InsertNode(N);
10054 SDValue V(N, 0);
10055 NewSDValueDbgMsg(V, "Creating new node: ", this);
10056 return V;
10057}
10058
10060 SDValue Ptr, SDValue Mask, SDValue EVL,
10061 MachinePointerInfo PtrInfo,
10062 MaybeAlign Alignment,
10063 MachineMemOperand::Flags MMOFlags,
10064 const AAMDNodes &AAInfo, const MDNode *Ranges,
10065 bool IsExpanding) {
10066 SDValue Undef = getUNDEF(Ptr.getValueType());
10067 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10068 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10069 IsExpanding);
10070}
10071
10073 SDValue Ptr, SDValue Mask, SDValue EVL,
10074 MachineMemOperand *MMO, bool IsExpanding) {
10075 SDValue Undef = getUNDEF(Ptr.getValueType());
10076 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10077 Mask, EVL, VT, MMO, IsExpanding);
10078}
10079
10081 EVT VT, SDValue Chain, SDValue Ptr,
10082 SDValue Mask, SDValue EVL,
10083 MachinePointerInfo PtrInfo, EVT MemVT,
10084 MaybeAlign Alignment,
10085 MachineMemOperand::Flags MMOFlags,
10086 const AAMDNodes &AAInfo, bool IsExpanding) {
10087 SDValue Undef = getUNDEF(Ptr.getValueType());
10088 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10089 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10090 IsExpanding);
10091}
10092
10094 EVT VT, SDValue Chain, SDValue Ptr,
10095 SDValue Mask, SDValue EVL, EVT MemVT,
10096 MachineMemOperand *MMO, bool IsExpanding) {
10097 SDValue Undef = getUNDEF(Ptr.getValueType());
10098 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10099 EVL, MemVT, MMO, IsExpanding);
10100}
10101
10105 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10106 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10107 // Don't propagate the invariant or dereferenceable flags.
10108 auto MMOFlags =
10109 LD->getMemOperand()->getFlags() &
10111 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10112 LD->getChain(), Base, Offset, LD->getMask(),
10113 LD->getVectorLength(), LD->getPointerInfo(),
10114 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10115 nullptr, LD->isExpandingLoad());
10116}
10117
10119 SDValue Ptr, SDValue Offset, SDValue Mask,
10120 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10121 ISD::MemIndexedMode AM, bool IsTruncating,
10122 bool IsCompressing) {
10123 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10124 assert(Mask.getValueType().getVectorElementCount() ==
10126 "Vector width mismatch between mask and data");
10127
10128 bool Indexed = AM != ISD::UNINDEXED;
10129 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10130 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10131 : getVTList(MVT::Other);
10132 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10134 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10135 ID.AddInteger(MemVT.getRawBits());
10136 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10137 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10138 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10139 ID.AddInteger(MMO->getFlags());
10140 void *IP = nullptr;
10141 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10142 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10143 return SDValue(E, 0);
10144 }
10145 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10146 IsTruncating, IsCompressing, MemVT, MMO);
10147 createOperands(N, Ops);
10148
10149 CSEMap.InsertNode(N, IP);
10150 InsertNode(N);
10151 SDValue V(N, 0);
10152 NewSDValueDbgMsg(V, "Creating new node: ", this);
10153 return V;
10154}
10155
10157 SDValue Val, SDValue Ptr, SDValue Mask,
10158 SDValue EVL, MachinePointerInfo PtrInfo,
10159 EVT SVT, Align Alignment,
10160 MachineMemOperand::Flags MMOFlags,
10161 const AAMDNodes &AAInfo,
10162 bool IsCompressing) {
10163 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10164
10165 MMOFlags |= MachineMemOperand::MOStore;
10166 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10167
10168 if (PtrInfo.V.isNull())
10169 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10170
10172 MachineMemOperand *MMO = MF.getMachineMemOperand(
10173 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10174 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10175 IsCompressing);
10176}
10177
10179 SDValue Val, SDValue Ptr, SDValue Mask,
10180 SDValue EVL, EVT SVT,
10181 MachineMemOperand *MMO,
10182 bool IsCompressing) {
10183 EVT VT = Val.getValueType();
10184
10185 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10186 if (VT == SVT)
10187 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10188 EVL, VT, MMO, ISD::UNINDEXED,
10189 /*IsTruncating*/ false, IsCompressing);
10190
10192 "Should only be a truncating store, not extending!");
10193 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10194 assert(VT.isVector() == SVT.isVector() &&
10195 "Cannot use trunc store to convert to or from a vector!");
10196 assert((!VT.isVector() ||
10198 "Cannot use trunc store to change the number of vector elements!");
10199
10200 SDVTList VTs = getVTList(MVT::Other);
10201 SDValue Undef = getUNDEF(Ptr.getValueType());
10202 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10204 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10205 ID.AddInteger(SVT.getRawBits());
10206 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10207 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10208 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10209 ID.AddInteger(MMO->getFlags());
10210 void *IP = nullptr;
10211 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10212 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10213 return SDValue(E, 0);
10214 }
10215 auto *N =
10216 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10217 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10218 createOperands(N, Ops);
10219
10220 CSEMap.InsertNode(N, IP);
10221 InsertNode(N);
10222 SDValue V(N, 0);
10223 NewSDValueDbgMsg(V, "Creating new node: ", this);
10224 return V;
10225}
10226
10230 auto *ST = cast<VPStoreSDNode>(OrigStore);
10231 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10232 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10233 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10234 Offset, ST->getMask(), ST->getVectorLength()};
10236 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10237 ID.AddInteger(ST->getMemoryVT().getRawBits());
10238 ID.AddInteger(ST->getRawSubclassData());
10239 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10240 ID.AddInteger(ST->getMemOperand()->getFlags());
10241 void *IP = nullptr;
10242 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10243 return SDValue(E, 0);
10244
10245 auto *N = newSDNode<VPStoreSDNode>(
10246 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10247 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10248 createOperands(N, Ops);
10249
10250 CSEMap.InsertNode(N, IP);
10251 InsertNode(N);
10252 SDValue V(N, 0);
10253 NewSDValueDbgMsg(V, "Creating new node: ", this);
10254 return V;
10255}
10256
10258 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10259 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10260 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10261 bool Indexed = AM != ISD::UNINDEXED;
10262 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10263
10264 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10265 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10266 : getVTList(VT, MVT::Other);
10268 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10269 ID.AddInteger(VT.getRawBits());
10270 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10271 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10272 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10273
10274 void *IP = nullptr;
10275 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10276 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10277 return SDValue(E, 0);
10278 }
10279
10280 auto *N =
10281 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10282 ExtType, IsExpanding, MemVT, MMO);
10283 createOperands(N, Ops);
10284 CSEMap.InsertNode(N, IP);
10285 InsertNode(N);
10286 SDValue V(N, 0);
10287 NewSDValueDbgMsg(V, "Creating new node: ", this);
10288 return V;
10289}
10290
10292 SDValue Ptr, SDValue Stride,
10293 SDValue Mask, SDValue EVL,
10294 MachineMemOperand *MMO,
10295 bool IsExpanding) {
10296 SDValue Undef = getUNDEF(Ptr.getValueType());
10297 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10298 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10299}
10300
10302 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10303 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10304 MachineMemOperand *MMO, bool IsExpanding) {
10305 SDValue Undef = getUNDEF(Ptr.getValueType());
10306 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10307 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10308}
10309
10311 SDValue Val, SDValue Ptr,
10312 SDValue Offset, SDValue Stride,
10313 SDValue Mask, SDValue EVL, EVT MemVT,
10314 MachineMemOperand *MMO,
10316 bool IsTruncating, bool IsCompressing) {
10317 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10318 bool Indexed = AM != ISD::UNINDEXED;
10319 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10320 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10321 : getVTList(MVT::Other);
10322 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10324 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10325 ID.AddInteger(MemVT.getRawBits());
10326 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10327 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10328 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10329 void *IP = nullptr;
10330 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10331 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10332 return SDValue(E, 0);
10333 }
10334 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10335 VTs, AM, IsTruncating,
10336 IsCompressing, MemVT, MMO);
10337 createOperands(N, Ops);
10338
10339 CSEMap.InsertNode(N, IP);
10340 InsertNode(N);
10341 SDValue V(N, 0);
10342 NewSDValueDbgMsg(V, "Creating new node: ", this);
10343 return V;
10344}
10345
10347 SDValue Val, SDValue Ptr,
10348 SDValue Stride, SDValue Mask,
10349 SDValue EVL, EVT SVT,
10350 MachineMemOperand *MMO,
10351 bool IsCompressing) {
10352 EVT VT = Val.getValueType();
10353
10354 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10355 if (VT == SVT)
10356 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10357 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10358 /*IsTruncating*/ false, IsCompressing);
10359
10361 "Should only be a truncating store, not extending!");
10362 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10363 assert(VT.isVector() == SVT.isVector() &&
10364 "Cannot use trunc store to convert to or from a vector!");
10365 assert((!VT.isVector() ||
10367 "Cannot use trunc store to change the number of vector elements!");
10368
10369 SDVTList VTs = getVTList(MVT::Other);
10370 SDValue Undef = getUNDEF(Ptr.getValueType());
10371 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10373 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10374 ID.AddInteger(SVT.getRawBits());
10375 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10376 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10377 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10378 void *IP = nullptr;
10379 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10380 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10381 return SDValue(E, 0);
10382 }
10383 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10384 VTs, ISD::UNINDEXED, true,
10385 IsCompressing, SVT, MMO);
10386 createOperands(N, Ops);
10387
10388 CSEMap.InsertNode(N, IP);
10389 InsertNode(N);
10390 SDValue V(N, 0);
10391 NewSDValueDbgMsg(V, "Creating new node: ", this);
10392 return V;
10393}
10394
10397 ISD::MemIndexType IndexType) {
10398 assert(Ops.size() == 6 && "Incompatible number of operands");
10399
10401 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10402 ID.AddInteger(VT.getRawBits());
10403 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10404 dl.getIROrder(), VTs, VT, MMO, IndexType));
10405 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10406 ID.AddInteger(MMO->getFlags());
10407 void *IP = nullptr;
10408 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10409 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10410 return SDValue(E, 0);
10411 }
10412
10413 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10414 VT, MMO, IndexType);
10415 createOperands(N, Ops);
10416
10417 assert(N->getMask().getValueType().getVectorElementCount() ==
10418 N->getValueType(0).getVectorElementCount() &&
10419 "Vector width mismatch between mask and data");
10420 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10421 N->getValueType(0).getVectorElementCount().isScalable() &&
10422 "Scalable flags of index and data do not match");
10424 N->getIndex().getValueType().getVectorElementCount(),
10425 N->getValueType(0).getVectorElementCount()) &&
10426 "Vector width mismatch between index and data");
10427 assert(isa<ConstantSDNode>(N->getScale()) &&
10428 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10429 "Scale should be a constant power of 2");
10430
10431 CSEMap.InsertNode(N, IP);
10432 InsertNode(N);
10433 SDValue V(N, 0);
10434 NewSDValueDbgMsg(V, "Creating new node: ", this);
10435 return V;
10436}
10437
10440 MachineMemOperand *MMO,
10441 ISD::MemIndexType IndexType) {
10442 assert(Ops.size() == 7 && "Incompatible number of operands");
10443
10445 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10446 ID.AddInteger(VT.getRawBits());
10447 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10448 dl.getIROrder(), VTs, VT, MMO, IndexType));
10449 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10450 ID.AddInteger(MMO->getFlags());
10451 void *IP = nullptr;
10452 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10453 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10454 return SDValue(E, 0);
10455 }
10456 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10457 VT, MMO, IndexType);
10458 createOperands(N, Ops);
10459
10460 assert(N->getMask().getValueType().getVectorElementCount() ==
10461 N->getValue().getValueType().getVectorElementCount() &&
10462 "Vector width mismatch between mask and data");
10463 assert(
10464 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10465 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10466 "Scalable flags of index and data do not match");
10468 N->getIndex().getValueType().getVectorElementCount(),
10469 N->getValue().getValueType().getVectorElementCount()) &&
10470 "Vector width mismatch between index and data");
10471 assert(isa<ConstantSDNode>(N->getScale()) &&
10472 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10473 "Scale should be a constant power of 2");
10474
10475 CSEMap.InsertNode(N, IP);
10476 InsertNode(N);
10477 SDValue V(N, 0);
10478 NewSDValueDbgMsg(V, "Creating new node: ", this);
10479 return V;
10480}
10481
10484 SDValue PassThru, EVT MemVT,
10485 MachineMemOperand *MMO,
10487 ISD::LoadExtType ExtTy, bool isExpanding) {
10488 bool Indexed = AM != ISD::UNINDEXED;
10489 assert((Indexed || Offset.isUndef()) &&
10490 "Unindexed masked load with an offset!");
10491 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10492 : getVTList(VT, MVT::Other);
10493 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10496 ID.AddInteger(MemVT.getRawBits());
10497 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10498 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10499 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10500 ID.AddInteger(MMO->getFlags());
10501 void *IP = nullptr;
10502 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10503 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10504 return SDValue(E, 0);
10505 }
10506 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10507 AM, ExtTy, isExpanding, MemVT, MMO);
10508 createOperands(N, Ops);
10509
10510 CSEMap.InsertNode(N, IP);
10511 InsertNode(N);
10512 SDValue V(N, 0);
10513 NewSDValueDbgMsg(V, "Creating new node: ", this);
10514 return V;
10515}
10516
10521 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10522 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10523 Offset, LD->getMask(), LD->getPassThru(),
10524 LD->getMemoryVT(), LD->getMemOperand(), AM,
10525 LD->getExtensionType(), LD->isExpandingLoad());
10526}
10527
10530 SDValue Mask, EVT MemVT,
10531 MachineMemOperand *MMO,
10532 ISD::MemIndexedMode AM, bool IsTruncating,
10533 bool IsCompressing) {
10534 assert(Chain.getValueType() == MVT::Other &&
10535 "Invalid chain type");
10536 bool Indexed = AM != ISD::UNINDEXED;
10537 assert((Indexed || Offset.isUndef()) &&
10538 "Unindexed masked store with an offset!");
10539 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10540 : getVTList(MVT::Other);
10541 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10544 ID.AddInteger(MemVT.getRawBits());
10545 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10546 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10547 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10548 ID.AddInteger(MMO->getFlags());
10549 void *IP = nullptr;
10550 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10551 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10552 return SDValue(E, 0);
10553 }
10554 auto *N =
10555 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10556 IsTruncating, IsCompressing, MemVT, MMO);
10557 createOperands(N, Ops);
10558
10559 CSEMap.InsertNode(N, IP);
10560 InsertNode(N);
10561 SDValue V(N, 0);
10562 NewSDValueDbgMsg(V, "Creating new node: ", this);
10563 return V;
10564}
10565
10570 assert(ST->getOffset().isUndef() &&
10571 "Masked store is already a indexed store!");
10572 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10573 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10574 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10575}
10576
10579 MachineMemOperand *MMO,
10580 ISD::MemIndexType IndexType,
10581 ISD::LoadExtType ExtTy) {
10582 assert(Ops.size() == 6 && "Incompatible number of operands");
10583
10586 ID.AddInteger(MemVT.getRawBits());
10587 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10588 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10589 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10590 ID.AddInteger(MMO->getFlags());
10591 void *IP = nullptr;
10592 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10593 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10594 return SDValue(E, 0);
10595 }
10596
10597 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10598 VTs, MemVT, MMO, IndexType, ExtTy);
10599 createOperands(N, Ops);
10600
10601 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10602 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10603 assert(N->getMask().getValueType().getVectorElementCount() ==
10604 N->getValueType(0).getVectorElementCount() &&
10605 "Vector width mismatch between mask and data");
10606 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10607 N->getValueType(0).getVectorElementCount().isScalable() &&
10608 "Scalable flags of index and data do not match");
10610 N->getIndex().getValueType().getVectorElementCount(),
10611 N->getValueType(0).getVectorElementCount()) &&
10612 "Vector width mismatch between index and data");
10613 assert(isa<ConstantSDNode>(N->getScale()) &&
10614 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10615 "Scale should be a constant power of 2");
10616
10617 CSEMap.InsertNode(N, IP);
10618 InsertNode(N);
10619 SDValue V(N, 0);
10620 NewSDValueDbgMsg(V, "Creating new node: ", this);
10621 return V;
10622}
10623
10626 MachineMemOperand *MMO,
10627 ISD::MemIndexType IndexType,
10628 bool IsTrunc) {
10629 assert(Ops.size() == 6 && "Incompatible number of operands");
10630
10633 ID.AddInteger(MemVT.getRawBits());
10634 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10635 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10636 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10637 ID.AddInteger(MMO->getFlags());
10638 void *IP = nullptr;
10639 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10640 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10641 return SDValue(E, 0);
10642 }
10643
10644 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10645 VTs, MemVT, MMO, IndexType, IsTrunc);
10646 createOperands(N, Ops);
10647
10648 assert(N->getMask().getValueType().getVectorElementCount() ==
10649 N->getValue().getValueType().getVectorElementCount() &&
10650 "Vector width mismatch between mask and data");
10651 assert(
10652 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10653 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10654 "Scalable flags of index and data do not match");
10656 N->getIndex().getValueType().getVectorElementCount(),
10657 N->getValue().getValueType().getVectorElementCount()) &&
10658 "Vector width mismatch between index and data");
10659 assert(isa<ConstantSDNode>(N->getScale()) &&
10660 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10661 "Scale should be a constant power of 2");
10662
10663 CSEMap.InsertNode(N, IP);
10664 InsertNode(N);
10665 SDValue V(N, 0);
10666 NewSDValueDbgMsg(V, "Creating new node: ", this);
10667 return V;
10668}
10669
10671 const SDLoc &dl, ArrayRef<SDValue> Ops,
10672 MachineMemOperand *MMO,
10673 ISD::MemIndexType IndexType) {
10674 assert(Ops.size() == 7 && "Incompatible number of operands");
10675
10678 ID.AddInteger(MemVT.getRawBits());
10679 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10680 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10681 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10682 ID.AddInteger(MMO->getFlags());
10683 void *IP = nullptr;
10684 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10685 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10686 return SDValue(E, 0);
10687 }
10688
10689 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10690 VTs, MemVT, MMO, IndexType);
10691 createOperands(N, Ops);
10692
10693 assert(N->getMask().getValueType().getVectorElementCount() ==
10694 N->getIndex().getValueType().getVectorElementCount() &&
10695 "Vector width mismatch between mask and data");
10696 assert(isa<ConstantSDNode>(N->getScale()) &&
10697 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10698 "Scale should be a constant power of 2");
10699 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10700
10701 CSEMap.InsertNode(N, IP);
10702 InsertNode(N);
10703 SDValue V(N, 0);
10704 NewSDValueDbgMsg(V, "Creating new node: ", this);
10705 return V;
10706}
10707
10709 SDValue Ptr, SDValue Mask, SDValue EVL,
10710 MachineMemOperand *MMO) {
10711 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10712 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10714 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10715 ID.AddInteger(VT.getRawBits());
10716 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10717 VTs, VT, MMO));
10718 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10719 ID.AddInteger(MMO->getFlags());
10720 void *IP = nullptr;
10721 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10722 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10723 return SDValue(E, 0);
10724 }
10725 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10726 VT, MMO);
10727 createOperands(N, Ops);
10728
10729 CSEMap.InsertNode(N, IP);
10730 InsertNode(N);
10731 SDValue V(N, 0);
10732 NewSDValueDbgMsg(V, "Creating new node: ", this);
10733 return V;
10734}
10735
10737 EVT MemVT, MachineMemOperand *MMO) {
10738 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10739 SDVTList VTs = getVTList(MVT::Other);
10740 SDValue Ops[] = {Chain, Ptr};
10743 ID.AddInteger(MemVT.getRawBits());
10744 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10745 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10746 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10747 ID.AddInteger(MMO->getFlags());
10748 void *IP = nullptr;
10749 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10750 return SDValue(E, 0);
10751
10752 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10753 dl.getDebugLoc(), VTs, MemVT, MMO);
10754 createOperands(N, Ops);
10755
10756 CSEMap.InsertNode(N, IP);
10757 InsertNode(N);
10758 SDValue V(N, 0);
10759 NewSDValueDbgMsg(V, "Creating new node: ", this);
10760 return V;
10761}
10762
10764 EVT MemVT, MachineMemOperand *MMO) {
10765 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10766 SDVTList VTs = getVTList(MVT::Other);
10767 SDValue Ops[] = {Chain, Ptr};
10770 ID.AddInteger(MemVT.getRawBits());
10771 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10772 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10773 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10774 ID.AddInteger(MMO->getFlags());
10775 void *IP = nullptr;
10776 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10777 return SDValue(E, 0);
10778
10779 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10780 dl.getDebugLoc(), VTs, MemVT, MMO);
10781 createOperands(N, Ops);
10782
10783 CSEMap.InsertNode(N, IP);
10784 InsertNode(N);
10785 SDValue V(N, 0);
10786 NewSDValueDbgMsg(V, "Creating new node: ", this);
10787 return V;
10788}
10789
10791 // select undef, T, F --> T (if T is a constant), otherwise F
10792 // select, ?, undef, F --> F
10793 // select, ?, T, undef --> T
10794 if (Cond.isUndef())
10795 return isConstantValueOfAnyType(T) ? T : F;
10796 if (T.isUndef())
10797 return F;
10798 if (F.isUndef())
10799 return T;
10800
10801 // select true, T, F --> T
10802 // select false, T, F --> F
10803 if (auto C = isBoolConstant(Cond))
10804 return *C ? T : F;
10805
10806 // select ?, T, T --> T
10807 if (T == F)
10808 return T;
10809
10810 return SDValue();
10811}
10812
10814 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10815 if (X.isUndef())
10816 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10817 // shift X, undef --> undef (because it may shift by the bitwidth)
10818 if (Y.isUndef())
10819 return getUNDEF(X.getValueType());
10820
10821 // shift 0, Y --> 0
10822 // shift X, 0 --> X
10824 return X;
10825
10826 // shift X, C >= bitwidth(X) --> undef
10827 // All vector elements must be too big (or undef) to avoid partial undefs.
10828 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10829 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10830 };
10831 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10832 return getUNDEF(X.getValueType());
10833
10834 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10835 if (X.getValueType().getScalarType() == MVT::i1)
10836 return X;
10837
10838 return SDValue();
10839}
10840
10842 SDNodeFlags Flags) {
10843 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10844 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10845 // operation is poison. That result can be relaxed to undef.
10846 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10847 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10848 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10849 (YC && YC->getValueAPF().isNaN());
10850 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10851 (YC && YC->getValueAPF().isInfinity());
10852
10853 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10854 return getUNDEF(X.getValueType());
10855
10856 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10857 return getUNDEF(X.getValueType());
10858
10859 if (!YC)
10860 return SDValue();
10861
10862 // X + -0.0 --> X
10863 if (Opcode == ISD::FADD)
10864 if (YC->getValueAPF().isNegZero())
10865 return X;
10866
10867 // X - +0.0 --> X
10868 if (Opcode == ISD::FSUB)
10869 if (YC->getValueAPF().isPosZero())
10870 return X;
10871
10872 // X * 1.0 --> X
10873 // X / 1.0 --> X
10874 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
10875 if (YC->getValueAPF().isExactlyValue(1.0))
10876 return X;
10877
10878 // X * 0.0 --> 0.0
10879 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
10880 if (YC->getValueAPF().isZero())
10881 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
10882
10883 return SDValue();
10884}
10885
10887 SDValue Ptr, SDValue SV, unsigned Align) {
10888 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
10889 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
10890}
10891
10892SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10894 switch (Ops.size()) {
10895 case 0: return getNode(Opcode, DL, VT);
10896 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
10897 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
10898 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
10899 default: break;
10900 }
10901
10902 // Copy from an SDUse array into an SDValue array for use with
10903 // the regular getNode logic.
10905 return getNode(Opcode, DL, VT, NewOps);
10906}
10907
10908SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10910 SDNodeFlags Flags;
10911 if (Inserter)
10912 Flags = Inserter->getFlags();
10913 return getNode(Opcode, DL, VT, Ops, Flags);
10914}
10915
10916SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
10917 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
10918 unsigned NumOps = Ops.size();
10919 switch (NumOps) {
10920 case 0: return getNode(Opcode, DL, VT);
10921 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
10922 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
10923 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
10924 default: break;
10925 }
10926
10927#ifndef NDEBUG
10928 for (const auto &Op : Ops)
10929 assert(Op.getOpcode() != ISD::DELETED_NODE &&
10930 "Operand is DELETED_NODE!");
10931#endif
10932
10933 switch (Opcode) {
10934 default: break;
10935 case ISD::BUILD_VECTOR:
10936 // Attempt to simplify BUILD_VECTOR.
10937 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
10938 return V;
10939 break;
10941 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
10942 return V;
10943 break;
10944 case ISD::SELECT_CC:
10945 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
10946 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
10947 "LHS and RHS of condition must have same type!");
10948 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10949 "True and False arms of SelectCC must have same type!");
10950 assert(Ops[2].getValueType() == VT &&
10951 "select_cc node must be of same type as true and false value!");
10952 assert((!Ops[0].getValueType().isVector() ||
10953 Ops[0].getValueType().getVectorElementCount() ==
10954 VT.getVectorElementCount()) &&
10955 "Expected select_cc with vector result to have the same sized "
10956 "comparison type!");
10957 break;
10958 case ISD::BR_CC:
10959 assert(NumOps == 5 && "BR_CC takes 5 operands!");
10960 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
10961 "LHS/RHS of comparison should match types!");
10962 break;
10963 case ISD::VP_ADD:
10964 case ISD::VP_SUB:
10965 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
10966 if (VT.getScalarType() == MVT::i1)
10967 Opcode = ISD::VP_XOR;
10968 break;
10969 case ISD::VP_MUL:
10970 // If it is VP_MUL mask operation then turn it to VP_AND
10971 if (VT.getScalarType() == MVT::i1)
10972 Opcode = ISD::VP_AND;
10973 break;
10974 case ISD::VP_REDUCE_MUL:
10975 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
10976 if (VT == MVT::i1)
10977 Opcode = ISD::VP_REDUCE_AND;
10978 break;
10979 case ISD::VP_REDUCE_ADD:
10980 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
10981 if (VT == MVT::i1)
10982 Opcode = ISD::VP_REDUCE_XOR;
10983 break;
10984 case ISD::VP_REDUCE_SMAX:
10985 case ISD::VP_REDUCE_UMIN:
10986 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
10987 // VP_REDUCE_AND.
10988 if (VT == MVT::i1)
10989 Opcode = ISD::VP_REDUCE_AND;
10990 break;
10991 case ISD::VP_REDUCE_SMIN:
10992 case ISD::VP_REDUCE_UMAX:
10993 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
10994 // VP_REDUCE_OR.
10995 if (VT == MVT::i1)
10996 Opcode = ISD::VP_REDUCE_OR;
10997 break;
10998 }
10999
11000 // Memoize nodes.
11001 SDNode *N;
11002 SDVTList VTs = getVTList(VT);
11003
11004 if (VT != MVT::Glue) {
11006 AddNodeIDNode(ID, Opcode, VTs, Ops);
11007 void *IP = nullptr;
11008
11009 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11010 E->intersectFlagsWith(Flags);
11011 return SDValue(E, 0);
11012 }
11013
11014 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11015 createOperands(N, Ops);
11016
11017 CSEMap.InsertNode(N, IP);
11018 } else {
11019 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11020 createOperands(N, Ops);
11021 }
11022
11023 N->setFlags(Flags);
11024 InsertNode(N);
11025 SDValue V(N, 0);
11026 NewSDValueDbgMsg(V, "Creating new node: ", this);
11027 return V;
11028}
11029
11030SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11031 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11032 SDNodeFlags Flags;
11033 if (Inserter)
11034 Flags = Inserter->getFlags();
11035 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11036}
11037
11038SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11040 const SDNodeFlags Flags) {
11041 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11042}
11043
11044SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11046 SDNodeFlags Flags;
11047 if (Inserter)
11048 Flags = Inserter->getFlags();
11049 return getNode(Opcode, DL, VTList, Ops, Flags);
11050}
11051
11052SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11053 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11054 if (VTList.NumVTs == 1)
11055 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11056
11057#ifndef NDEBUG
11058 for (const auto &Op : Ops)
11059 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11060 "Operand is DELETED_NODE!");
11061#endif
11062
11063 switch (Opcode) {
11064 case ISD::SADDO:
11065 case ISD::UADDO:
11066 case ISD::SSUBO:
11067 case ISD::USUBO: {
11068 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11069 "Invalid add/sub overflow op!");
11070 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11071 Ops[0].getValueType() == Ops[1].getValueType() &&
11072 Ops[0].getValueType() == VTList.VTs[0] &&
11073 "Binary operator types must match!");
11074 SDValue N1 = Ops[0], N2 = Ops[1];
11075 canonicalizeCommutativeBinop(Opcode, N1, N2);
11076
11077 // (X +- 0) -> X with zero-overflow.
11078 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11079 /*AllowTruncation*/ true);
11080 if (N2CV && N2CV->isZero()) {
11081 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11082 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11083 }
11084
11085 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11086 VTList.VTs[1].getScalarType() == MVT::i1) {
11087 SDValue F1 = getFreeze(N1);
11088 SDValue F2 = getFreeze(N2);
11089 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11090 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11091 return getNode(ISD::MERGE_VALUES, DL, VTList,
11092 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11093 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11094 Flags);
11095 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11096 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11097 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11098 return getNode(ISD::MERGE_VALUES, DL, VTList,
11099 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11100 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11101 Flags);
11102 }
11103 }
11104 break;
11105 }
11106 case ISD::SADDO_CARRY:
11107 case ISD::UADDO_CARRY:
11108 case ISD::SSUBO_CARRY:
11109 case ISD::USUBO_CARRY:
11110 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11111 "Invalid add/sub overflow op!");
11112 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11113 Ops[0].getValueType() == Ops[1].getValueType() &&
11114 Ops[0].getValueType() == VTList.VTs[0] &&
11115 Ops[2].getValueType() == VTList.VTs[1] &&
11116 "Binary operator types must match!");
11117 break;
11118 case ISD::SMUL_LOHI:
11119 case ISD::UMUL_LOHI: {
11120 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11121 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11122 VTList.VTs[0] == Ops[0].getValueType() &&
11123 VTList.VTs[0] == Ops[1].getValueType() &&
11124 "Binary operator types must match!");
11125 // Constant fold.
11128 if (LHS && RHS) {
11129 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11130 unsigned OutWidth = Width * 2;
11131 APInt Val = LHS->getAPIntValue();
11132 APInt Mul = RHS->getAPIntValue();
11133 if (Opcode == ISD::SMUL_LOHI) {
11134 Val = Val.sext(OutWidth);
11135 Mul = Mul.sext(OutWidth);
11136 } else {
11137 Val = Val.zext(OutWidth);
11138 Mul = Mul.zext(OutWidth);
11139 }
11140 Val *= Mul;
11141
11142 SDValue Hi =
11143 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11144 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11145 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11146 }
11147 break;
11148 }
11149 case ISD::FFREXP: {
11150 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11151 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11152 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11153
11155 int FrexpExp;
11156 APFloat FrexpMant =
11157 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11158 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11159 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11160 DL, VTList.VTs[1]);
11161 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11162 }
11163
11164 break;
11165 }
11167 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11168 "Invalid STRICT_FP_EXTEND!");
11169 assert(VTList.VTs[0].isFloatingPoint() &&
11170 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11171 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11172 "STRICT_FP_EXTEND result type should be vector iff the operand "
11173 "type is vector!");
11174 assert((!VTList.VTs[0].isVector() ||
11175 VTList.VTs[0].getVectorElementCount() ==
11176 Ops[1].getValueType().getVectorElementCount()) &&
11177 "Vector element count mismatch!");
11178 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11179 "Invalid fpext node, dst <= src!");
11180 break;
11182 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11183 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11184 "STRICT_FP_ROUND result type should be vector iff the operand "
11185 "type is vector!");
11186 assert((!VTList.VTs[0].isVector() ||
11187 VTList.VTs[0].getVectorElementCount() ==
11188 Ops[1].getValueType().getVectorElementCount()) &&
11189 "Vector element count mismatch!");
11190 assert(VTList.VTs[0].isFloatingPoint() &&
11191 Ops[1].getValueType().isFloatingPoint() &&
11192 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11193 Ops[2].getOpcode() == ISD::TargetConstant &&
11194 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11195 "Invalid STRICT_FP_ROUND!");
11196 break;
11197 }
11198
11199 // Memoize the node unless it returns a glue result.
11200 SDNode *N;
11201 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11203 AddNodeIDNode(ID, Opcode, VTList, Ops);
11204 void *IP = nullptr;
11205 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11206 E->intersectFlagsWith(Flags);
11207 return SDValue(E, 0);
11208 }
11209
11210 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11211 createOperands(N, Ops);
11212 CSEMap.InsertNode(N, IP);
11213 } else {
11214 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11215 createOperands(N, Ops);
11216 }
11217
11218 N->setFlags(Flags);
11219 InsertNode(N);
11220 SDValue V(N, 0);
11221 NewSDValueDbgMsg(V, "Creating new node: ", this);
11222 return V;
11223}
11224
11225SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11226 SDVTList VTList) {
11227 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11228}
11229
11230SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11231 SDValue N1) {
11232 SDValue Ops[] = { N1 };
11233 return getNode(Opcode, DL, VTList, Ops);
11234}
11235
11236SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11237 SDValue N1, SDValue N2) {
11238 SDValue Ops[] = { N1, N2 };
11239 return getNode(Opcode, DL, VTList, Ops);
11240}
11241
11242SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11243 SDValue N1, SDValue N2, SDValue N3) {
11244 SDValue Ops[] = { N1, N2, N3 };
11245 return getNode(Opcode, DL, VTList, Ops);
11246}
11247
11248SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11249 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11250 SDValue Ops[] = { N1, N2, N3, N4 };
11251 return getNode(Opcode, DL, VTList, Ops);
11252}
11253
11254SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11255 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11256 SDValue N5) {
11257 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11258 return getNode(Opcode, DL, VTList, Ops);
11259}
11260
11262 if (!VT.isExtended())
11263 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11264
11265 return makeVTList(&(*EVTs.insert(VT).first), 1);
11266}
11267
11270 ID.AddInteger(2U);
11271 ID.AddInteger(VT1.getRawBits());
11272 ID.AddInteger(VT2.getRawBits());
11273
11274 void *IP = nullptr;
11275 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11276 if (!Result) {
11277 EVT *Array = Allocator.Allocate<EVT>(2);
11278 Array[0] = VT1;
11279 Array[1] = VT2;
11280 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11281 VTListMap.InsertNode(Result, IP);
11282 }
11283 return Result->getSDVTList();
11284}
11285
11288 ID.AddInteger(3U);
11289 ID.AddInteger(VT1.getRawBits());
11290 ID.AddInteger(VT2.getRawBits());
11291 ID.AddInteger(VT3.getRawBits());
11292
11293 void *IP = nullptr;
11294 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11295 if (!Result) {
11296 EVT *Array = Allocator.Allocate<EVT>(3);
11297 Array[0] = VT1;
11298 Array[1] = VT2;
11299 Array[2] = VT3;
11300 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11301 VTListMap.InsertNode(Result, IP);
11302 }
11303 return Result->getSDVTList();
11304}
11305
11308 ID.AddInteger(4U);
11309 ID.AddInteger(VT1.getRawBits());
11310 ID.AddInteger(VT2.getRawBits());
11311 ID.AddInteger(VT3.getRawBits());
11312 ID.AddInteger(VT4.getRawBits());
11313
11314 void *IP = nullptr;
11315 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11316 if (!Result) {
11317 EVT *Array = Allocator.Allocate<EVT>(4);
11318 Array[0] = VT1;
11319 Array[1] = VT2;
11320 Array[2] = VT3;
11321 Array[3] = VT4;
11322 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11323 VTListMap.InsertNode(Result, IP);
11324 }
11325 return Result->getSDVTList();
11326}
11327
11329 unsigned NumVTs = VTs.size();
11331 ID.AddInteger(NumVTs);
11332 for (unsigned index = 0; index < NumVTs; index++) {
11333 ID.AddInteger(VTs[index].getRawBits());
11334 }
11335
11336 void *IP = nullptr;
11337 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11338 if (!Result) {
11339 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11340 llvm::copy(VTs, Array);
11341 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11342 VTListMap.InsertNode(Result, IP);
11343 }
11344 return Result->getSDVTList();
11345}
11346
11347
11348/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11349/// specified operands. If the resultant node already exists in the DAG,
11350/// this does not modify the specified node, instead it returns the node that
11351/// already exists. If the resultant node does not exist in the DAG, the
11352/// input node is returned. As a degenerate case, if you specify the same
11353/// input operands as the node already has, the input node is returned.
11355 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11356
11357 // Check to see if there is no change.
11358 if (Op == N->getOperand(0)) return N;
11359
11360 // See if the modified node already exists.
11361 void *InsertPos = nullptr;
11362 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11363 return Existing;
11364
11365 // Nope it doesn't. Remove the node from its current place in the maps.
11366 if (InsertPos)
11367 if (!RemoveNodeFromCSEMaps(N))
11368 InsertPos = nullptr;
11369
11370 // Now we update the operands.
11371 N->OperandList[0].set(Op);
11372
11374 // If this gets put into a CSE map, add it.
11375 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11376 return N;
11377}
11378
11380 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11381
11382 // Check to see if there is no change.
11383 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11384 return N; // No operands changed, just return the input node.
11385
11386 // See if the modified node already exists.
11387 void *InsertPos = nullptr;
11388 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11389 return Existing;
11390
11391 // Nope it doesn't. Remove the node from its current place in the maps.
11392 if (InsertPos)
11393 if (!RemoveNodeFromCSEMaps(N))
11394 InsertPos = nullptr;
11395
11396 // Now we update the operands.
11397 if (N->OperandList[0] != Op1)
11398 N->OperandList[0].set(Op1);
11399 if (N->OperandList[1] != Op2)
11400 N->OperandList[1].set(Op2);
11401
11403 // If this gets put into a CSE map, add it.
11404 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11405 return N;
11406}
11407
11410 SDValue Ops[] = { Op1, Op2, Op3 };
11411 return UpdateNodeOperands(N, Ops);
11412}
11413
11416 SDValue Op3, SDValue Op4) {
11417 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11418 return UpdateNodeOperands(N, Ops);
11419}
11420
11423 SDValue Op3, SDValue Op4, SDValue Op5) {
11424 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11425 return UpdateNodeOperands(N, Ops);
11426}
11427
11430 unsigned NumOps = Ops.size();
11431 assert(N->getNumOperands() == NumOps &&
11432 "Update with wrong number of operands");
11433
11434 // If no operands changed just return the input node.
11435 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11436 return N;
11437
11438 // See if the modified node already exists.
11439 void *InsertPos = nullptr;
11440 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11441 return Existing;
11442
11443 // Nope it doesn't. Remove the node from its current place in the maps.
11444 if (InsertPos)
11445 if (!RemoveNodeFromCSEMaps(N))
11446 InsertPos = nullptr;
11447
11448 // Now we update the operands.
11449 for (unsigned i = 0; i != NumOps; ++i)
11450 if (N->OperandList[i] != Ops[i])
11451 N->OperandList[i].set(Ops[i]);
11452
11454 // If this gets put into a CSE map, add it.
11455 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11456 return N;
11457}
11458
11459/// DropOperands - Release the operands and set this node to have
11460/// zero operands.
11462 // Unlike the code in MorphNodeTo that does this, we don't need to
11463 // watch for dead nodes here.
11464 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11465 SDUse &Use = *I++;
11466 Use.set(SDValue());
11467 }
11468}
11469
11471 ArrayRef<MachineMemOperand *> NewMemRefs) {
11472 if (NewMemRefs.empty()) {
11473 N->clearMemRefs();
11474 return;
11475 }
11476
11477 // Check if we can avoid allocating by storing a single reference directly.
11478 if (NewMemRefs.size() == 1) {
11479 N->MemRefs = NewMemRefs[0];
11480 N->NumMemRefs = 1;
11481 return;
11482 }
11483
11484 MachineMemOperand **MemRefsBuffer =
11485 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11486 llvm::copy(NewMemRefs, MemRefsBuffer);
11487 N->MemRefs = MemRefsBuffer;
11488 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11489}
11490
11491/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11492/// machine opcode.
11493///
11495 EVT VT) {
11496 SDVTList VTs = getVTList(VT);
11497 return SelectNodeTo(N, MachineOpc, VTs, {});
11498}
11499
11501 EVT VT, SDValue Op1) {
11502 SDVTList VTs = getVTList(VT);
11503 SDValue Ops[] = { Op1 };
11504 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11505}
11506
11508 EVT VT, SDValue Op1,
11509 SDValue Op2) {
11510 SDVTList VTs = getVTList(VT);
11511 SDValue Ops[] = { Op1, Op2 };
11512 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11513}
11514
11516 EVT VT, SDValue Op1,
11517 SDValue Op2, SDValue Op3) {
11518 SDVTList VTs = getVTList(VT);
11519 SDValue Ops[] = { Op1, Op2, Op3 };
11520 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11521}
11522
11525 SDVTList VTs = getVTList(VT);
11526 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11527}
11528
11530 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11531 SDVTList VTs = getVTList(VT1, VT2);
11532 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11533}
11534
11536 EVT VT1, EVT VT2) {
11537 SDVTList VTs = getVTList(VT1, VT2);
11538 return SelectNodeTo(N, MachineOpc, VTs, {});
11539}
11540
11542 EVT VT1, EVT VT2, EVT VT3,
11544 SDVTList VTs = getVTList(VT1, VT2, VT3);
11545 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11546}
11547
11549 EVT VT1, EVT VT2,
11550 SDValue Op1, SDValue Op2) {
11551 SDVTList VTs = getVTList(VT1, VT2);
11552 SDValue Ops[] = { Op1, Op2 };
11553 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11554}
11555
11558 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11559 // Reset the NodeID to -1.
11560 New->setNodeId(-1);
11561 if (New != N) {
11562 ReplaceAllUsesWith(N, New);
11564 }
11565 return New;
11566}
11567
11568/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11569/// the line number information on the merged node since it is not possible to
11570/// preserve the information that operation is associated with multiple lines.
11571/// This will make the debugger working better at -O0, were there is a higher
11572/// probability having other instructions associated with that line.
11573///
11574/// For IROrder, we keep the smaller of the two
11575SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11576 DebugLoc NLoc = N->getDebugLoc();
11577 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11578 N->setDebugLoc(DebugLoc());
11579 }
11580 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11581 N->setIROrder(Order);
11582 return N;
11583}
11584
11585/// MorphNodeTo - This *mutates* the specified node to have the specified
11586/// return type, opcode, and operands.
11587///
11588/// Note that MorphNodeTo returns the resultant node. If there is already a
11589/// node of the specified opcode and operands, it returns that node instead of
11590/// the current one. Note that the SDLoc need not be the same.
11591///
11592/// Using MorphNodeTo is faster than creating a new node and swapping it in
11593/// with ReplaceAllUsesWith both because it often avoids allocating a new
11594/// node, and because it doesn't require CSE recalculation for any of
11595/// the node's users.
11596///
11597/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11598/// As a consequence it isn't appropriate to use from within the DAG combiner or
11599/// the legalizer which maintain worklists that would need to be updated when
11600/// deleting things.
11603 // If an identical node already exists, use it.
11604 void *IP = nullptr;
11605 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11607 AddNodeIDNode(ID, Opc, VTs, Ops);
11608 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11609 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11610 }
11611
11612 if (!RemoveNodeFromCSEMaps(N))
11613 IP = nullptr;
11614
11615 // Start the morphing.
11616 N->NodeType = Opc;
11617 N->ValueList = VTs.VTs;
11618 N->NumValues = VTs.NumVTs;
11619
11620 // Clear the operands list, updating used nodes to remove this from their
11621 // use list. Keep track of any operands that become dead as a result.
11622 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11623 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11624 SDUse &Use = *I++;
11625 SDNode *Used = Use.getNode();
11626 Use.set(SDValue());
11627 if (Used->use_empty())
11628 DeadNodeSet.insert(Used);
11629 }
11630
11631 // For MachineNode, initialize the memory references information.
11633 MN->clearMemRefs();
11634
11635 // Swap for an appropriately sized array from the recycler.
11636 removeOperands(N);
11637 createOperands(N, Ops);
11638
11639 // Delete any nodes that are still dead after adding the uses for the
11640 // new operands.
11641 if (!DeadNodeSet.empty()) {
11642 SmallVector<SDNode *, 16> DeadNodes;
11643 for (SDNode *N : DeadNodeSet)
11644 if (N->use_empty())
11645 DeadNodes.push_back(N);
11646 RemoveDeadNodes(DeadNodes);
11647 }
11648
11649 if (IP)
11650 CSEMap.InsertNode(N, IP); // Memoize the new node.
11651 return N;
11652}
11653
11655 unsigned OrigOpc = Node->getOpcode();
11656 unsigned NewOpc;
11657 switch (OrigOpc) {
11658 default:
11659 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11660#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11661 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11662#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11663 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11664#include "llvm/IR/ConstrainedOps.def"
11665 }
11666
11667 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11668
11669 // We're taking this node out of the chain, so we need to re-link things.
11670 SDValue InputChain = Node->getOperand(0);
11671 SDValue OutputChain = SDValue(Node, 1);
11672 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11673
11675 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11676 Ops.push_back(Node->getOperand(i));
11677
11678 SDVTList VTs = getVTList(Node->getValueType(0));
11679 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11680
11681 // MorphNodeTo can operate in two ways: if an existing node with the
11682 // specified operands exists, it can just return it. Otherwise, it
11683 // updates the node in place to have the requested operands.
11684 if (Res == Node) {
11685 // If we updated the node in place, reset the node ID. To the isel,
11686 // this should be just like a newly allocated machine node.
11687 Res->setNodeId(-1);
11688 } else {
11691 }
11692
11693 return Res;
11694}
11695
11696/// getMachineNode - These are used for target selectors to create a new node
11697/// with specified return type(s), MachineInstr opcode, and operands.
11698///
11699/// Note that getMachineNode returns the resultant node. If there is already a
11700/// node of the specified opcode and operands, it returns that node instead of
11701/// the current one.
11703 EVT VT) {
11704 SDVTList VTs = getVTList(VT);
11705 return getMachineNode(Opcode, dl, VTs, {});
11706}
11707
11709 EVT VT, SDValue Op1) {
11710 SDVTList VTs = getVTList(VT);
11711 SDValue Ops[] = { Op1 };
11712 return getMachineNode(Opcode, dl, VTs, Ops);
11713}
11714
11716 EVT VT, SDValue Op1, SDValue Op2) {
11717 SDVTList VTs = getVTList(VT);
11718 SDValue Ops[] = { Op1, Op2 };
11719 return getMachineNode(Opcode, dl, VTs, Ops);
11720}
11721
11723 EVT VT, SDValue Op1, SDValue Op2,
11724 SDValue Op3) {
11725 SDVTList VTs = getVTList(VT);
11726 SDValue Ops[] = { Op1, Op2, Op3 };
11727 return getMachineNode(Opcode, dl, VTs, Ops);
11728}
11729
11732 SDVTList VTs = getVTList(VT);
11733 return getMachineNode(Opcode, dl, VTs, Ops);
11734}
11735
11737 EVT VT1, EVT VT2, SDValue Op1,
11738 SDValue Op2) {
11739 SDVTList VTs = getVTList(VT1, VT2);
11740 SDValue Ops[] = { Op1, Op2 };
11741 return getMachineNode(Opcode, dl, VTs, Ops);
11742}
11743
11745 EVT VT1, EVT VT2, SDValue Op1,
11746 SDValue Op2, SDValue Op3) {
11747 SDVTList VTs = getVTList(VT1, VT2);
11748 SDValue Ops[] = { Op1, Op2, Op3 };
11749 return getMachineNode(Opcode, dl, VTs, Ops);
11750}
11751
11753 EVT VT1, EVT VT2,
11755 SDVTList VTs = getVTList(VT1, VT2);
11756 return getMachineNode(Opcode, dl, VTs, Ops);
11757}
11758
11760 EVT VT1, EVT VT2, EVT VT3,
11761 SDValue Op1, SDValue Op2) {
11762 SDVTList VTs = getVTList(VT1, VT2, VT3);
11763 SDValue Ops[] = { Op1, Op2 };
11764 return getMachineNode(Opcode, dl, VTs, Ops);
11765}
11766
11768 EVT VT1, EVT VT2, EVT VT3,
11769 SDValue Op1, SDValue Op2,
11770 SDValue Op3) {
11771 SDVTList VTs = getVTList(VT1, VT2, VT3);
11772 SDValue Ops[] = { Op1, Op2, Op3 };
11773 return getMachineNode(Opcode, dl, VTs, Ops);
11774}
11775
11777 EVT VT1, EVT VT2, EVT VT3,
11779 SDVTList VTs = getVTList(VT1, VT2, VT3);
11780 return getMachineNode(Opcode, dl, VTs, Ops);
11781}
11782
11784 ArrayRef<EVT> ResultTys,
11786 SDVTList VTs = getVTList(ResultTys);
11787 return getMachineNode(Opcode, dl, VTs, Ops);
11788}
11789
11791 SDVTList VTs,
11793 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11795 void *IP = nullptr;
11796
11797 if (DoCSE) {
11799 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11800 IP = nullptr;
11801 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11802 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11803 }
11804 }
11805
11806 // Allocate a new MachineSDNode.
11807 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11808 createOperands(N, Ops);
11809
11810 if (DoCSE)
11811 CSEMap.InsertNode(N, IP);
11812
11813 InsertNode(N);
11814 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11815 return N;
11816}
11817
11818/// getTargetExtractSubreg - A convenience function for creating
11819/// TargetOpcode::EXTRACT_SUBREG nodes.
11821 SDValue Operand) {
11822 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11823 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11824 VT, Operand, SRIdxVal);
11825 return SDValue(Subreg, 0);
11826}
11827
11828/// getTargetInsertSubreg - A convenience function for creating
11829/// TargetOpcode::INSERT_SUBREG nodes.
11831 SDValue Operand, SDValue Subreg) {
11832 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11833 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11834 VT, Operand, Subreg, SRIdxVal);
11835 return SDValue(Result, 0);
11836}
11837
11838/// getNodeIfExists - Get the specified node if it's already available, or
11839/// else return NULL.
11842 bool AllowCommute) {
11843 SDNodeFlags Flags;
11844 if (Inserter)
11845 Flags = Inserter->getFlags();
11846 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11847}
11848
11851 const SDNodeFlags Flags,
11852 bool AllowCommute) {
11853 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11854 return nullptr;
11855
11856 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11858 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11859 void *IP = nullptr;
11860 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11861 E->intersectFlagsWith(Flags);
11862 return E;
11863 }
11864 return nullptr;
11865 };
11866
11867 if (SDNode *Existing = Lookup(Ops))
11868 return Existing;
11869
11870 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
11871 return Lookup({Ops[1], Ops[0]});
11872
11873 return nullptr;
11874}
11875
11876/// doesNodeExist - Check if a node exists without modifying its flags.
11877bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
11879 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
11881 AddNodeIDNode(ID, Opcode, VTList, Ops);
11882 void *IP = nullptr;
11883 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
11884 return true;
11885 }
11886 return false;
11887}
11888
11889/// getDbgValue - Creates a SDDbgValue node.
11890///
11891/// SDNode
11893 SDNode *N, unsigned R, bool IsIndirect,
11894 const DebugLoc &DL, unsigned O) {
11895 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11896 "Expected inlined-at fields to agree");
11897 return new (DbgInfo->getAlloc())
11898 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
11899 {}, IsIndirect, DL, O,
11900 /*IsVariadic=*/false);
11901}
11902
11903/// Constant
11905 DIExpression *Expr,
11906 const Value *C,
11907 const DebugLoc &DL, unsigned O) {
11908 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11909 "Expected inlined-at fields to agree");
11910 return new (DbgInfo->getAlloc())
11911 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
11912 /*IsIndirect=*/false, DL, O,
11913 /*IsVariadic=*/false);
11914}
11915
11916/// FrameIndex
11918 DIExpression *Expr, unsigned FI,
11919 bool IsIndirect,
11920 const DebugLoc &DL,
11921 unsigned O) {
11922 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11923 "Expected inlined-at fields to agree");
11924 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
11925}
11926
11927/// FrameIndex with dependencies
11929 DIExpression *Expr, unsigned FI,
11930 ArrayRef<SDNode *> Dependencies,
11931 bool IsIndirect,
11932 const DebugLoc &DL,
11933 unsigned O) {
11934 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11935 "Expected inlined-at fields to agree");
11936 return new (DbgInfo->getAlloc())
11937 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
11938 Dependencies, IsIndirect, DL, O,
11939 /*IsVariadic=*/false);
11940}
11941
11942/// VReg
11944 Register VReg, bool IsIndirect,
11945 const DebugLoc &DL, unsigned O) {
11946 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11947 "Expected inlined-at fields to agree");
11948 return new (DbgInfo->getAlloc())
11949 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
11950 {}, IsIndirect, DL, O,
11951 /*IsVariadic=*/false);
11952}
11953
11956 ArrayRef<SDNode *> Dependencies,
11957 bool IsIndirect, const DebugLoc &DL,
11958 unsigned O, bool IsVariadic) {
11959 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
11960 "Expected inlined-at fields to agree");
11961 return new (DbgInfo->getAlloc())
11962 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
11963 DL, O, IsVariadic);
11964}
11965
11967 unsigned OffsetInBits, unsigned SizeInBits,
11968 bool InvalidateDbg) {
11969 SDNode *FromNode = From.getNode();
11970 SDNode *ToNode = To.getNode();
11971 assert(FromNode && ToNode && "Can't modify dbg values");
11972
11973 // PR35338
11974 // TODO: assert(From != To && "Redundant dbg value transfer");
11975 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
11976 if (From == To || FromNode == ToNode)
11977 return;
11978
11979 if (!FromNode->getHasDebugValue())
11980 return;
11981
11982 SDDbgOperand FromLocOp =
11983 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
11985
11987 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
11988 if (Dbg->isInvalidated())
11989 continue;
11990
11991 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
11992
11993 // Create a new location ops vector that is equal to the old vector, but
11994 // with each instance of FromLocOp replaced with ToLocOp.
11995 bool Changed = false;
11996 auto NewLocOps = Dbg->copyLocationOps();
11997 std::replace_if(
11998 NewLocOps.begin(), NewLocOps.end(),
11999 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12000 bool Match = Op == FromLocOp;
12001 Changed |= Match;
12002 return Match;
12003 },
12004 ToLocOp);
12005 // Ignore this SDDbgValue if we didn't find a matching location.
12006 if (!Changed)
12007 continue;
12008
12009 DIVariable *Var = Dbg->getVariable();
12010 auto *Expr = Dbg->getExpression();
12011 // If a fragment is requested, update the expression.
12012 if (SizeInBits) {
12013 // When splitting a larger (e.g., sign-extended) value whose
12014 // lower bits are described with an SDDbgValue, do not attempt
12015 // to transfer the SDDbgValue to the upper bits.
12016 if (auto FI = Expr->getFragmentInfo())
12017 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12018 continue;
12019 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12020 SizeInBits);
12021 if (!Fragment)
12022 continue;
12023 Expr = *Fragment;
12024 }
12025
12026 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12027 // Clone the SDDbgValue and move it to To.
12028 SDDbgValue *Clone = getDbgValueList(
12029 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12030 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12031 Dbg->isVariadic());
12032 ClonedDVs.push_back(Clone);
12033
12034 if (InvalidateDbg) {
12035 // Invalidate value and indicate the SDDbgValue should not be emitted.
12036 Dbg->setIsInvalidated();
12037 Dbg->setIsEmitted();
12038 }
12039 }
12040
12041 for (SDDbgValue *Dbg : ClonedDVs) {
12042 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12043 "Transferred DbgValues should depend on the new SDNode");
12044 AddDbgValue(Dbg, false);
12045 }
12046}
12047
12049 if (!N.getHasDebugValue())
12050 return;
12051
12052 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12053 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12054 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12055 return SDDbgOperand::fromNode(Node, ResNo);
12056 };
12057
12059 for (auto *DV : GetDbgValues(&N)) {
12060 if (DV->isInvalidated())
12061 continue;
12062 switch (N.getOpcode()) {
12063 default:
12064 break;
12065 case ISD::ADD: {
12066 SDValue N0 = N.getOperand(0);
12067 SDValue N1 = N.getOperand(1);
12068 if (!isa<ConstantSDNode>(N0)) {
12069 bool RHSConstant = isa<ConstantSDNode>(N1);
12071 if (RHSConstant)
12072 Offset = N.getConstantOperandVal(1);
12073 // We are not allowed to turn indirect debug values variadic, so
12074 // don't salvage those.
12075 if (!RHSConstant && DV->isIndirect())
12076 continue;
12077
12078 // Rewrite an ADD constant node into a DIExpression. Since we are
12079 // performing arithmetic to compute the variable's *value* in the
12080 // DIExpression, we need to mark the expression with a
12081 // DW_OP_stack_value.
12082 auto *DIExpr = DV->getExpression();
12083 auto NewLocOps = DV->copyLocationOps();
12084 bool Changed = false;
12085 size_t OrigLocOpsSize = NewLocOps.size();
12086 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12087 // We're not given a ResNo to compare against because the whole
12088 // node is going away. We know that any ISD::ADD only has one
12089 // result, so we can assume any node match is using the result.
12090 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12091 NewLocOps[i].getSDNode() != &N)
12092 continue;
12093 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12094 if (RHSConstant) {
12097 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12098 } else {
12099 // Convert to a variadic expression (if not already).
12100 // convertToVariadicExpression() returns a const pointer, so we use
12101 // a temporary const variable here.
12102 const auto *TmpDIExpr =
12106 ExprOps.push_back(NewLocOps.size());
12107 ExprOps.push_back(dwarf::DW_OP_plus);
12108 SDDbgOperand RHS =
12110 NewLocOps.push_back(RHS);
12111 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12112 }
12113 Changed = true;
12114 }
12115 (void)Changed;
12116 assert(Changed && "Salvage target doesn't use N");
12117
12118 bool IsVariadic =
12119 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12120
12121 auto AdditionalDependencies = DV->getAdditionalDependencies();
12122 SDDbgValue *Clone = getDbgValueList(
12123 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12124 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12125 ClonedDVs.push_back(Clone);
12126 DV->setIsInvalidated();
12127 DV->setIsEmitted();
12128 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12129 N0.getNode()->dumprFull(this);
12130 dbgs() << " into " << *DIExpr << '\n');
12131 }
12132 break;
12133 }
12134 case ISD::TRUNCATE: {
12135 SDValue N0 = N.getOperand(0);
12136 TypeSize FromSize = N0.getValueSizeInBits();
12137 TypeSize ToSize = N.getValueSizeInBits(0);
12138
12139 DIExpression *DbgExpression = DV->getExpression();
12140 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12141 auto NewLocOps = DV->copyLocationOps();
12142 bool Changed = false;
12143 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12144 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12145 NewLocOps[i].getSDNode() != &N)
12146 continue;
12147
12148 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12149 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12150 Changed = true;
12151 }
12152 assert(Changed && "Salvage target doesn't use N");
12153 (void)Changed;
12154
12155 SDDbgValue *Clone =
12156 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12157 DV->getAdditionalDependencies(), DV->isIndirect(),
12158 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12159
12160 ClonedDVs.push_back(Clone);
12161 DV->setIsInvalidated();
12162 DV->setIsEmitted();
12163 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12164 dbgs() << " into " << *DbgExpression << '\n');
12165 break;
12166 }
12167 }
12168 }
12169
12170 for (SDDbgValue *Dbg : ClonedDVs) {
12171 assert((!Dbg->getSDNodes().empty() ||
12172 llvm::any_of(Dbg->getLocationOps(),
12173 [&](const SDDbgOperand &Op) {
12174 return Op.getKind() == SDDbgOperand::FRAMEIX;
12175 })) &&
12176 "Salvaged DbgValue should depend on a new SDNode");
12177 AddDbgValue(Dbg, false);
12178 }
12179}
12180
12181/// Creates a SDDbgLabel node.
12183 const DebugLoc &DL, unsigned O) {
12184 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12185 "Expected inlined-at fields to agree");
12186 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12187}
12188
12189namespace {
12190
12191/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12192/// pointed to by a use iterator is deleted, increment the use iterator
12193/// so that it doesn't dangle.
12194///
12195class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12198
12199 void NodeDeleted(SDNode *N, SDNode *E) override {
12200 // Increment the iterator as needed.
12201 while (UI != UE && N == UI->getUser())
12202 ++UI;
12203 }
12204
12205public:
12206 RAUWUpdateListener(SelectionDAG &d,
12209 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12210};
12211
12212} // end anonymous namespace
12213
12214/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12215/// This can cause recursive merging of nodes in the DAG.
12216///
12217/// This version assumes From has a single result value.
12218///
12220 SDNode *From = FromN.getNode();
12221 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12222 "Cannot replace with this method!");
12223 assert(From != To.getNode() && "Cannot replace uses of with self");
12224
12225 // Preserve Debug Values
12226 transferDbgValues(FromN, To);
12227 // Preserve extra info.
12228 copyExtraInfo(From, To.getNode());
12229
12230 // Iterate over all the existing uses of From. New uses will be added
12231 // to the beginning of the use list, which we avoid visiting.
12232 // This specifically avoids visiting uses of From that arise while the
12233 // replacement is happening, because any such uses would be the result
12234 // of CSE: If an existing node looks like From after one of its operands
12235 // is replaced by To, we don't want to replace of all its users with To
12236 // too. See PR3018 for more info.
12237 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12238 RAUWUpdateListener Listener(*this, UI, UE);
12239 while (UI != UE) {
12240 SDNode *User = UI->getUser();
12241
12242 // This node is about to morph, remove its old self from the CSE maps.
12243 RemoveNodeFromCSEMaps(User);
12244
12245 // A user can appear in a use list multiple times, and when this
12246 // happens the uses are usually next to each other in the list.
12247 // To help reduce the number of CSE recomputations, process all
12248 // the uses of this user that we can find this way.
12249 do {
12250 SDUse &Use = *UI;
12251 ++UI;
12252 Use.set(To);
12253 if (To->isDivergent() != From->isDivergent())
12255 } while (UI != UE && UI->getUser() == User);
12256 // Now that we have modified User, add it back to the CSE maps. If it
12257 // already exists there, recursively merge the results together.
12258 AddModifiedNodeToCSEMaps(User);
12259 }
12260
12261 // If we just RAUW'd the root, take note.
12262 if (FromN == getRoot())
12263 setRoot(To);
12264}
12265
12266/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12267/// This can cause recursive merging of nodes in the DAG.
12268///
12269/// This version assumes that for each value of From, there is a
12270/// corresponding value in To in the same position with the same type.
12271///
12273#ifndef NDEBUG
12274 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12275 assert((!From->hasAnyUseOfValue(i) ||
12276 From->getValueType(i) == To->getValueType(i)) &&
12277 "Cannot use this version of ReplaceAllUsesWith!");
12278#endif
12279
12280 // Handle the trivial case.
12281 if (From == To)
12282 return;
12283
12284 // Preserve Debug Info. Only do this if there's a use.
12285 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12286 if (From->hasAnyUseOfValue(i)) {
12287 assert((i < To->getNumValues()) && "Invalid To location");
12288 transferDbgValues(SDValue(From, i), SDValue(To, i));
12289 }
12290 // Preserve extra info.
12291 copyExtraInfo(From, To);
12292
12293 // Iterate over just the existing users of From. See the comments in
12294 // the ReplaceAllUsesWith above.
12295 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12296 RAUWUpdateListener Listener(*this, UI, UE);
12297 while (UI != UE) {
12298 SDNode *User = UI->getUser();
12299
12300 // This node is about to morph, remove its old self from the CSE maps.
12301 RemoveNodeFromCSEMaps(User);
12302
12303 // A user can appear in a use list multiple times, and when this
12304 // happens the uses are usually next to each other in the list.
12305 // To help reduce the number of CSE recomputations, process all
12306 // the uses of this user that we can find this way.
12307 do {
12308 SDUse &Use = *UI;
12309 ++UI;
12310 Use.setNode(To);
12311 if (To->isDivergent() != From->isDivergent())
12313 } while (UI != UE && UI->getUser() == User);
12314
12315 // Now that we have modified User, add it back to the CSE maps. If it
12316 // already exists there, recursively merge the results together.
12317 AddModifiedNodeToCSEMaps(User);
12318 }
12319
12320 // If we just RAUW'd the root, take note.
12321 if (From == getRoot().getNode())
12322 setRoot(SDValue(To, getRoot().getResNo()));
12323}
12324
12325/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12326/// This can cause recursive merging of nodes in the DAG.
12327///
12328/// This version can replace From with any result values. To must match the
12329/// number and types of values returned by From.
12331 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12332 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12333
12334 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12335 // Preserve Debug Info.
12336 transferDbgValues(SDValue(From, i), To[i]);
12337 // Preserve extra info.
12338 copyExtraInfo(From, To[i].getNode());
12339 }
12340
12341 // Iterate over just the existing users of From. See the comments in
12342 // the ReplaceAllUsesWith above.
12343 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12344 RAUWUpdateListener Listener(*this, UI, UE);
12345 while (UI != UE) {
12346 SDNode *User = UI->getUser();
12347
12348 // This node is about to morph, remove its old self from the CSE maps.
12349 RemoveNodeFromCSEMaps(User);
12350
12351 // A user can appear in a use list multiple times, and when this happens the
12352 // uses are usually next to each other in the list. To help reduce the
12353 // number of CSE and divergence recomputations, process all the uses of this
12354 // user that we can find this way.
12355 bool To_IsDivergent = false;
12356 do {
12357 SDUse &Use = *UI;
12358 const SDValue &ToOp = To[Use.getResNo()];
12359 ++UI;
12360 Use.set(ToOp);
12361 To_IsDivergent |= ToOp->isDivergent();
12362 } while (UI != UE && UI->getUser() == User);
12363
12364 if (To_IsDivergent != From->isDivergent())
12366
12367 // Now that we have modified User, add it back to the CSE maps. If it
12368 // already exists there, recursively merge the results together.
12369 AddModifiedNodeToCSEMaps(User);
12370 }
12371
12372 // If we just RAUW'd the root, take note.
12373 if (From == getRoot().getNode())
12374 setRoot(SDValue(To[getRoot().getResNo()]));
12375}
12376
12377/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12378/// uses of other values produced by From.getNode() alone. The Deleted
12379/// vector is handled the same way as for ReplaceAllUsesWith.
12381 // Handle the really simple, really trivial case efficiently.
12382 if (From == To) return;
12383
12384 // Handle the simple, trivial, case efficiently.
12385 if (From.getNode()->getNumValues() == 1) {
12386 ReplaceAllUsesWith(From, To);
12387 return;
12388 }
12389
12390 // Preserve Debug Info.
12391 transferDbgValues(From, To);
12392 copyExtraInfo(From.getNode(), To.getNode());
12393
12394 // Iterate over just the existing users of From. See the comments in
12395 // the ReplaceAllUsesWith above.
12396 SDNode::use_iterator UI = From.getNode()->use_begin(),
12397 UE = From.getNode()->use_end();
12398 RAUWUpdateListener Listener(*this, UI, UE);
12399 while (UI != UE) {
12400 SDNode *User = UI->getUser();
12401 bool UserRemovedFromCSEMaps = false;
12402
12403 // A user can appear in a use list multiple times, and when this
12404 // happens the uses are usually next to each other in the list.
12405 // To help reduce the number of CSE recomputations, process all
12406 // the uses of this user that we can find this way.
12407 do {
12408 SDUse &Use = *UI;
12409
12410 // Skip uses of different values from the same node.
12411 if (Use.getResNo() != From.getResNo()) {
12412 ++UI;
12413 continue;
12414 }
12415
12416 // If this node hasn't been modified yet, it's still in the CSE maps,
12417 // so remove its old self from the CSE maps.
12418 if (!UserRemovedFromCSEMaps) {
12419 RemoveNodeFromCSEMaps(User);
12420 UserRemovedFromCSEMaps = true;
12421 }
12422
12423 ++UI;
12424 Use.set(To);
12425 if (To->isDivergent() != From->isDivergent())
12427 } while (UI != UE && UI->getUser() == User);
12428 // We are iterating over all uses of the From node, so if a use
12429 // doesn't use the specific value, no changes are made.
12430 if (!UserRemovedFromCSEMaps)
12431 continue;
12432
12433 // Now that we have modified User, add it back to the CSE maps. If it
12434 // already exists there, recursively merge the results together.
12435 AddModifiedNodeToCSEMaps(User);
12436 }
12437
12438 // If we just RAUW'd the root, take note.
12439 if (From == getRoot())
12440 setRoot(To);
12441}
12442
12443namespace {
12444
12445/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12446/// to record information about a use.
12447struct UseMemo {
12448 SDNode *User;
12449 unsigned Index;
12450 SDUse *Use;
12451};
12452
12453/// operator< - Sort Memos by User.
12454bool operator<(const UseMemo &L, const UseMemo &R) {
12455 return (intptr_t)L.User < (intptr_t)R.User;
12456}
12457
12458/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12459/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12460/// the node already has been taken care of recursively.
12461class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12462 SmallVectorImpl<UseMemo> &Uses;
12463
12464 void NodeDeleted(SDNode *N, SDNode *E) override {
12465 for (UseMemo &Memo : Uses)
12466 if (Memo.User == N)
12467 Memo.User = nullptr;
12468 }
12469
12470public:
12471 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12472 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12473};
12474
12475} // end anonymous namespace
12476
12477/// Return true if a glue output should propagate divergence information.
12479 switch (Node->getOpcode()) {
12480 case ISD::CopyFromReg:
12481 case ISD::CopyToReg:
12482 return false;
12483 default:
12484 return true;
12485 }
12486
12487 llvm_unreachable("covered opcode switch");
12488}
12489
12491 if (TLI->isSDNodeAlwaysUniform(N)) {
12492 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12493 "Conflicting divergence information!");
12494 return false;
12495 }
12496 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12497 return true;
12498 for (const auto &Op : N->ops()) {
12499 EVT VT = Op.getValueType();
12500
12501 // Skip Chain. It does not carry divergence.
12502 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12503 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12504 return true;
12505 }
12506 return false;
12507}
12508
12510 SmallVector<SDNode *, 16> Worklist(1, N);
12511 do {
12512 N = Worklist.pop_back_val();
12513 bool IsDivergent = calculateDivergence(N);
12514 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12515 N->SDNodeBits.IsDivergent = IsDivergent;
12516 llvm::append_range(Worklist, N->users());
12517 }
12518 } while (!Worklist.empty());
12519}
12520
12521void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12523 Order.reserve(AllNodes.size());
12524 for (auto &N : allnodes()) {
12525 unsigned NOps = N.getNumOperands();
12526 Degree[&N] = NOps;
12527 if (0 == NOps)
12528 Order.push_back(&N);
12529 }
12530 for (size_t I = 0; I != Order.size(); ++I) {
12531 SDNode *N = Order[I];
12532 for (auto *U : N->users()) {
12533 unsigned &UnsortedOps = Degree[U];
12534 if (0 == --UnsortedOps)
12535 Order.push_back(U);
12536 }
12537 }
12538}
12539
12540#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12541void SelectionDAG::VerifyDAGDivergence() {
12542 std::vector<SDNode *> TopoOrder;
12543 CreateTopologicalOrder(TopoOrder);
12544 for (auto *N : TopoOrder) {
12545 assert(calculateDivergence(N) == N->isDivergent() &&
12546 "Divergence bit inconsistency detected");
12547 }
12548}
12549#endif
12550
12551/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12552/// uses of other values produced by From.getNode() alone. The same value
12553/// may appear in both the From and To list. The Deleted vector is
12554/// handled the same way as for ReplaceAllUsesWith.
12556 const SDValue *To,
12557 unsigned Num){
12558 // Handle the simple, trivial case efficiently.
12559 if (Num == 1)
12560 return ReplaceAllUsesOfValueWith(*From, *To);
12561
12562 transferDbgValues(*From, *To);
12563 copyExtraInfo(From->getNode(), To->getNode());
12564
12565 // Read up all the uses and make records of them. This helps
12566 // processing new uses that are introduced during the
12567 // replacement process.
12569 for (unsigned i = 0; i != Num; ++i) {
12570 unsigned FromResNo = From[i].getResNo();
12571 SDNode *FromNode = From[i].getNode();
12572 for (SDUse &Use : FromNode->uses()) {
12573 if (Use.getResNo() == FromResNo) {
12574 UseMemo Memo = {Use.getUser(), i, &Use};
12575 Uses.push_back(Memo);
12576 }
12577 }
12578 }
12579
12580 // Sort the uses, so that all the uses from a given User are together.
12582 RAUOVWUpdateListener Listener(*this, Uses);
12583
12584 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12585 UseIndex != UseIndexEnd; ) {
12586 // We know that this user uses some value of From. If it is the right
12587 // value, update it.
12588 SDNode *User = Uses[UseIndex].User;
12589 // If the node has been deleted by recursive CSE updates when updating
12590 // another node, then just skip this entry.
12591 if (User == nullptr) {
12592 ++UseIndex;
12593 continue;
12594 }
12595
12596 // This node is about to morph, remove its old self from the CSE maps.
12597 RemoveNodeFromCSEMaps(User);
12598
12599 // The Uses array is sorted, so all the uses for a given User
12600 // are next to each other in the list.
12601 // To help reduce the number of CSE recomputations, process all
12602 // the uses of this user that we can find this way.
12603 do {
12604 unsigned i = Uses[UseIndex].Index;
12605 SDUse &Use = *Uses[UseIndex].Use;
12606 ++UseIndex;
12607
12608 Use.set(To[i]);
12609 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12610
12611 // Now that we have modified User, add it back to the CSE maps. If it
12612 // already exists there, recursively merge the results together.
12613 AddModifiedNodeToCSEMaps(User);
12614 }
12615}
12616
12617/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12618/// based on their topological order. It returns the maximum id and a vector
12619/// of the SDNodes* in assigned order by reference.
12621 unsigned DAGSize = 0;
12622
12623 // SortedPos tracks the progress of the algorithm. Nodes before it are
12624 // sorted, nodes after it are unsorted. When the algorithm completes
12625 // it is at the end of the list.
12626 allnodes_iterator SortedPos = allnodes_begin();
12627
12628 // Visit all the nodes. Move nodes with no operands to the front of
12629 // the list immediately. Annotate nodes that do have operands with their
12630 // operand count. Before we do this, the Node Id fields of the nodes
12631 // may contain arbitrary values. After, the Node Id fields for nodes
12632 // before SortedPos will contain the topological sort index, and the
12633 // Node Id fields for nodes At SortedPos and after will contain the
12634 // count of outstanding operands.
12636 checkForCycles(&N, this);
12637 unsigned Degree = N.getNumOperands();
12638 if (Degree == 0) {
12639 // A node with no uses, add it to the result array immediately.
12640 N.setNodeId(DAGSize++);
12641 allnodes_iterator Q(&N);
12642 if (Q != SortedPos)
12643 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12644 assert(SortedPos != AllNodes.end() && "Overran node list");
12645 ++SortedPos;
12646 } else {
12647 // Temporarily use the Node Id as scratch space for the degree count.
12648 N.setNodeId(Degree);
12649 }
12650 }
12651
12652 // Visit all the nodes. As we iterate, move nodes into sorted order,
12653 // such that by the time the end is reached all nodes will be sorted.
12654 for (SDNode &Node : allnodes()) {
12655 SDNode *N = &Node;
12656 checkForCycles(N, this);
12657 // N is in sorted position, so all its uses have one less operand
12658 // that needs to be sorted.
12659 for (SDNode *P : N->users()) {
12660 unsigned Degree = P->getNodeId();
12661 assert(Degree != 0 && "Invalid node degree");
12662 --Degree;
12663 if (Degree == 0) {
12664 // All of P's operands are sorted, so P may sorted now.
12665 P->setNodeId(DAGSize++);
12666 if (P->getIterator() != SortedPos)
12667 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12668 assert(SortedPos != AllNodes.end() && "Overran node list");
12669 ++SortedPos;
12670 } else {
12671 // Update P's outstanding operand count.
12672 P->setNodeId(Degree);
12673 }
12674 }
12675 if (Node.getIterator() == SortedPos) {
12676#ifndef NDEBUG
12678 SDNode *S = &*++I;
12679 dbgs() << "Overran sorted position:\n";
12680 S->dumprFull(this); dbgs() << "\n";
12681 dbgs() << "Checking if this is due to cycles\n";
12682 checkForCycles(this, true);
12683#endif
12684 llvm_unreachable(nullptr);
12685 }
12686 }
12687
12688 assert(SortedPos == AllNodes.end() &&
12689 "Topological sort incomplete!");
12690 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12691 "First node in topological sort is not the entry token!");
12692 assert(AllNodes.front().getNodeId() == 0 &&
12693 "First node in topological sort has non-zero id!");
12694 assert(AllNodes.front().getNumOperands() == 0 &&
12695 "First node in topological sort has operands!");
12696 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12697 "Last node in topologic sort has unexpected id!");
12698 assert(AllNodes.back().use_empty() &&
12699 "Last node in topologic sort has users!");
12700 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12701 return DAGSize;
12702}
12703
12705 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12706 SortedNodes.clear();
12707 // Node -> remaining number of outstanding operands.
12708 DenseMap<const SDNode *, unsigned> RemainingOperands;
12709
12710 // Put nodes without any operands into SortedNodes first.
12711 for (const SDNode &N : allnodes()) {
12712 checkForCycles(&N, this);
12713 unsigned NumOperands = N.getNumOperands();
12714 if (NumOperands == 0)
12715 SortedNodes.push_back(&N);
12716 else
12717 // Record their total number of outstanding operands.
12718 RemainingOperands[&N] = NumOperands;
12719 }
12720
12721 // A node is pushed into SortedNodes when all of its operands (predecessors in
12722 // the graph) are also in SortedNodes.
12723 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12724 const SDNode *N = SortedNodes[i];
12725 for (const SDNode *U : N->users()) {
12726 // HandleSDNode is never part of a DAG and therefore has no entry in
12727 // RemainingOperands.
12728 if (U->getOpcode() == ISD::HANDLENODE)
12729 continue;
12730 unsigned &NumRemOperands = RemainingOperands[U];
12731 assert(NumRemOperands && "Invalid number of remaining operands");
12732 --NumRemOperands;
12733 if (!NumRemOperands)
12734 SortedNodes.push_back(U);
12735 }
12736 }
12737
12738 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12739 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12740 "First node in topological sort is not the entry token");
12741 assert(SortedNodes.front()->getNumOperands() == 0 &&
12742 "First node in topological sort has operands");
12743}
12744
12745/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12746/// value is produced by SD.
12747void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12748 for (SDNode *SD : DB->getSDNodes()) {
12749 if (!SD)
12750 continue;
12751 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12752 SD->setHasDebugValue(true);
12753 }
12754 DbgInfo->add(DB, isParameter);
12755}
12756
12757void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12758
12760 SDValue NewMemOpChain) {
12761 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12762 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12763 // The new memory operation must have the same position as the old load in
12764 // terms of memory dependency. Create a TokenFactor for the old load and new
12765 // memory operation and update uses of the old load's output chain to use that
12766 // TokenFactor.
12767 if (OldChain == NewMemOpChain || OldChain.use_empty())
12768 return NewMemOpChain;
12769
12770 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12771 OldChain, NewMemOpChain);
12772 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12773 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12774 return TokenFactor;
12775}
12776
12778 SDValue NewMemOp) {
12779 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12780 SDValue OldChain = SDValue(OldLoad, 1);
12781 SDValue NewMemOpChain = NewMemOp.getValue(1);
12782 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12783}
12784
12786 Function **OutFunction) {
12787 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12788
12789 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12790 auto *Module = MF->getFunction().getParent();
12791 auto *Function = Module->getFunction(Symbol);
12792
12793 if (OutFunction != nullptr)
12794 *OutFunction = Function;
12795
12796 if (Function != nullptr) {
12797 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12798 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12799 }
12800
12801 std::string ErrorStr;
12802 raw_string_ostream ErrorFormatter(ErrorStr);
12803 ErrorFormatter << "Undefined external symbol ";
12804 ErrorFormatter << '"' << Symbol << '"';
12805 report_fatal_error(Twine(ErrorStr));
12806}
12807
12808//===----------------------------------------------------------------------===//
12809// SDNode Class
12810//===----------------------------------------------------------------------===//
12811
12814 return Const != nullptr && Const->isZero();
12815}
12816
12818 return V.isUndef() || isNullConstant(V);
12819}
12820
12823 return Const != nullptr && Const->isZero() && !Const->isNegative();
12824}
12825
12828 return Const != nullptr && Const->isAllOnes();
12829}
12830
12833 return Const != nullptr && Const->isOne();
12834}
12835
12838 return Const != nullptr && Const->isMinSignedValue();
12839}
12840
12841bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12842 unsigned OperandNo) {
12843 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12844 // TODO: Target-specific opcodes could be added.
12845 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12846 /*AllowTruncation*/ true)) {
12847 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12848 switch (Opcode) {
12849 case ISD::ADD:
12850 case ISD::OR:
12851 case ISD::XOR:
12852 case ISD::UMAX:
12853 return Const.isZero();
12854 case ISD::MUL:
12855 return Const.isOne();
12856 case ISD::AND:
12857 case ISD::UMIN:
12858 return Const.isAllOnes();
12859 case ISD::SMAX:
12860 return Const.isMinSignedValue();
12861 case ISD::SMIN:
12862 return Const.isMaxSignedValue();
12863 case ISD::SUB:
12864 case ISD::SHL:
12865 case ISD::SRA:
12866 case ISD::SRL:
12867 return OperandNo == 1 && Const.isZero();
12868 case ISD::UDIV:
12869 case ISD::SDIV:
12870 return OperandNo == 1 && Const.isOne();
12871 }
12872 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
12873 switch (Opcode) {
12874 case ISD::FADD:
12875 return ConstFP->isZero() &&
12876 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
12877 case ISD::FSUB:
12878 return OperandNo == 1 && ConstFP->isZero() &&
12879 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
12880 case ISD::FMUL:
12881 return ConstFP->isExactlyValue(1.0);
12882 case ISD::FDIV:
12883 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
12884 case ISD::FMINNUM:
12885 case ISD::FMAXNUM: {
12886 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
12887 EVT VT = V.getValueType();
12888 const fltSemantics &Semantics = VT.getFltSemantics();
12889 APFloat NeutralAF = !Flags.hasNoNaNs()
12890 ? APFloat::getQNaN(Semantics)
12891 : !Flags.hasNoInfs()
12892 ? APFloat::getInf(Semantics)
12893 : APFloat::getLargest(Semantics);
12894 if (Opcode == ISD::FMAXNUM)
12895 NeutralAF.changeSign();
12896
12897 return ConstFP->isExactlyValue(NeutralAF);
12898 }
12899 }
12900 }
12901 return false;
12902}
12903
12905 while (V.getOpcode() == ISD::BITCAST)
12906 V = V.getOperand(0);
12907 return V;
12908}
12909
12911 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
12912 V = V.getOperand(0);
12913 return V;
12914}
12915
12917 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
12918 V = V.getOperand(0);
12919 return V;
12920}
12921
12923 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
12924 SDValue InVec = V.getOperand(0);
12925 SDValue EltNo = V.getOperand(2);
12926 EVT VT = InVec.getValueType();
12927 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
12928 if (IndexC && VT.isFixedLengthVector() &&
12929 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
12930 !DemandedElts[IndexC->getZExtValue()]) {
12931 V = InVec;
12932 continue;
12933 }
12934 break;
12935 }
12936 return V;
12937}
12938
12940 while (V.getOpcode() == ISD::TRUNCATE)
12941 V = V.getOperand(0);
12942 return V;
12943}
12944
12945bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
12946 if (V.getOpcode() != ISD::XOR)
12947 return false;
12948 V = peekThroughBitcasts(V.getOperand(1));
12949 unsigned NumBits = V.getScalarValueSizeInBits();
12950 ConstantSDNode *C =
12951 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
12952 return C && (C->getAPIntValue().countr_one() >= NumBits);
12953}
12954
12956 bool AllowTruncation) {
12957 EVT VT = N.getValueType();
12958 APInt DemandedElts = VT.isFixedLengthVector()
12960 : APInt(1, 1);
12961 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
12962}
12963
12965 bool AllowUndefs,
12966 bool AllowTruncation) {
12968 return CN;
12969
12970 // SplatVectors can truncate their operands. Ignore that case here unless
12971 // AllowTruncation is set.
12972 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
12973 EVT VecEltVT = N->getValueType(0).getVectorElementType();
12974 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
12975 EVT CVT = CN->getValueType(0);
12976 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
12977 if (AllowTruncation || CVT == VecEltVT)
12978 return CN;
12979 }
12980 }
12981
12983 BitVector UndefElements;
12984 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
12985
12986 // BuildVectors can truncate their operands. Ignore that case here unless
12987 // AllowTruncation is set.
12988 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
12989 if (CN && (UndefElements.none() || AllowUndefs)) {
12990 EVT CVT = CN->getValueType(0);
12991 EVT NSVT = N.getValueType().getScalarType();
12992 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
12993 if (AllowTruncation || (CVT == NSVT))
12994 return CN;
12995 }
12996 }
12997
12998 return nullptr;
12999}
13000
13002 EVT VT = N.getValueType();
13003 APInt DemandedElts = VT.isFixedLengthVector()
13005 : APInt(1, 1);
13006 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13007}
13008
13010 const APInt &DemandedElts,
13011 bool AllowUndefs) {
13013 return CN;
13014
13016 BitVector UndefElements;
13017 ConstantFPSDNode *CN =
13018 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13019 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13020 if (CN && (UndefElements.none() || AllowUndefs))
13021 return CN;
13022 }
13023
13024 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13025 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13026 return CN;
13027
13028 return nullptr;
13029}
13030
13031bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13032 // TODO: may want to use peekThroughBitcast() here.
13033 ConstantSDNode *C =
13034 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13035 return C && C->isZero();
13036}
13037
13038bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13039 ConstantSDNode *C =
13040 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13041 return C && C->isOne();
13042}
13043
13044bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13045 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13046 return C && C->isExactlyValue(1.0);
13047}
13048
13049bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13051 unsigned BitWidth = N.getScalarValueSizeInBits();
13052 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13053 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13054}
13055
13056bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13057 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13058 return C && APInt::isSameValue(C->getAPIntValue(),
13059 APInt(C->getAPIntValue().getBitWidth(), 1));
13060}
13061
13062bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13064 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13065 return C && C->isZero();
13066}
13067
13068bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13069 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13070 return C && C->isZero();
13071}
13072
13076
13077MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13078 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13079 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13080 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13081 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13082 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13083 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13084
13085 // We check here that the size of the memory operand fits within the size of
13086 // the MMO. This is because the MMO might indicate only a possible address
13087 // range instead of specifying the affected memory addresses precisely.
13088 assert(
13089 (!MMO->getType().isValid() ||
13090 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13091 "Size mismatch!");
13092}
13093
13094/// Profile - Gather unique data for the node.
13095///
13097 AddNodeIDNode(ID, this);
13098}
13099
13100namespace {
13101
13102 struct EVTArray {
13103 std::vector<EVT> VTs;
13104
13105 EVTArray() {
13106 VTs.reserve(MVT::VALUETYPE_SIZE);
13107 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13108 VTs.push_back(MVT((MVT::SimpleValueType)i));
13109 }
13110 };
13111
13112} // end anonymous namespace
13113
13114/// getValueTypeList - Return a pointer to the specified value type.
13115///
13116const EVT *SDNode::getValueTypeList(MVT VT) {
13117 static EVTArray SimpleVTArray;
13118
13119 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13120 return &SimpleVTArray.VTs[VT.SimpleTy];
13121}
13122
13123/// hasAnyUseOfValue - Return true if there are any use of the indicated
13124/// value. This method ignores uses of other values defined by this operation.
13125bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13126 assert(Value < getNumValues() && "Bad value!");
13127
13128 for (SDUse &U : uses())
13129 if (U.getResNo() == Value)
13130 return true;
13131
13132 return false;
13133}
13134
13135/// isOnlyUserOf - Return true if this node is the only use of N.
13136bool SDNode::isOnlyUserOf(const SDNode *N) const {
13137 bool Seen = false;
13138 for (const SDNode *User : N->users()) {
13139 if (User == this)
13140 Seen = true;
13141 else
13142 return false;
13143 }
13144
13145 return Seen;
13146}
13147
13148/// Return true if the only users of N are contained in Nodes.
13150 bool Seen = false;
13151 for (const SDNode *User : N->users()) {
13152 if (llvm::is_contained(Nodes, User))
13153 Seen = true;
13154 else
13155 return false;
13156 }
13157
13158 return Seen;
13159}
13160
13161/// Return true if the referenced return value is an operand of N.
13162bool SDValue::isOperandOf(const SDNode *N) const {
13163 return is_contained(N->op_values(), *this);
13164}
13165
13166bool SDNode::isOperandOf(const SDNode *N) const {
13167 return any_of(N->op_values(),
13168 [this](SDValue Op) { return this == Op.getNode(); });
13169}
13170
13171/// reachesChainWithoutSideEffects - Return true if this operand (which must
13172/// be a chain) reaches the specified operand without crossing any
13173/// side-effecting instructions on any chain path. In practice, this looks
13174/// through token factors and non-volatile loads. In order to remain efficient,
13175/// this only looks a couple of nodes in, it does not do an exhaustive search.
13176///
13177/// Note that we only need to examine chains when we're searching for
13178/// side-effects; SelectionDAG requires that all side-effects are represented
13179/// by chains, even if another operand would force a specific ordering. This
13180/// constraint is necessary to allow transformations like splitting loads.
13182 unsigned Depth) const {
13183 if (*this == Dest) return true;
13184
13185 // Don't search too deeply, we just want to be able to see through
13186 // TokenFactor's etc.
13187 if (Depth == 0) return false;
13188
13189 // If this is a token factor, all inputs to the TF happen in parallel.
13190 if (getOpcode() == ISD::TokenFactor) {
13191 // First, try a shallow search.
13192 if (is_contained((*this)->ops(), Dest)) {
13193 // We found the chain we want as an operand of this TokenFactor.
13194 // Essentially, we reach the chain without side-effects if we could
13195 // serialize the TokenFactor into a simple chain of operations with
13196 // Dest as the last operation. This is automatically true if the
13197 // chain has one use: there are no other ordering constraints.
13198 // If the chain has more than one use, we give up: some other
13199 // use of Dest might force a side-effect between Dest and the current
13200 // node.
13201 if (Dest.hasOneUse())
13202 return true;
13203 }
13204 // Next, try a deep search: check whether every operand of the TokenFactor
13205 // reaches Dest.
13206 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13207 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13208 });
13209 }
13210
13211 // Loads don't have side effects, look through them.
13212 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13213 if (Ld->isUnordered())
13214 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13215 }
13216 return false;
13217}
13218
13219bool SDNode::hasPredecessor(const SDNode *N) const {
13222 Worklist.push_back(this);
13223 return hasPredecessorHelper(N, Visited, Worklist);
13224}
13225
13227 this->Flags &= Flags;
13228}
13229
13230SDValue
13232 ArrayRef<ISD::NodeType> CandidateBinOps,
13233 bool AllowPartials) {
13234 // The pattern must end in an extract from index 0.
13235 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13236 !isNullConstant(Extract->getOperand(1)))
13237 return SDValue();
13238
13239 // Match against one of the candidate binary ops.
13240 SDValue Op = Extract->getOperand(0);
13241 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13242 return Op.getOpcode() == unsigned(BinOp);
13243 }))
13244 return SDValue();
13245
13246 // Floating-point reductions may require relaxed constraints on the final step
13247 // of the reduction because they may reorder intermediate operations.
13248 unsigned CandidateBinOp = Op.getOpcode();
13249 if (Op.getValueType().isFloatingPoint()) {
13250 SDNodeFlags Flags = Op->getFlags();
13251 switch (CandidateBinOp) {
13252 case ISD::FADD:
13253 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13254 return SDValue();
13255 break;
13256 default:
13257 llvm_unreachable("Unhandled FP opcode for binop reduction");
13258 }
13259 }
13260
13261 // Matching failed - attempt to see if we did enough stages that a partial
13262 // reduction from a subvector is possible.
13263 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13264 if (!AllowPartials || !Op)
13265 return SDValue();
13266 EVT OpVT = Op.getValueType();
13267 EVT OpSVT = OpVT.getScalarType();
13268 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13269 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13270 return SDValue();
13271 BinOp = (ISD::NodeType)CandidateBinOp;
13272 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13273 };
13274
13275 // At each stage, we're looking for something that looks like:
13276 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13277 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13278 // i32 undef, i32 undef, i32 undef, i32 undef>
13279 // %a = binop <8 x i32> %op, %s
13280 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13281 // we expect something like:
13282 // <4,5,6,7,u,u,u,u>
13283 // <2,3,u,u,u,u,u,u>
13284 // <1,u,u,u,u,u,u,u>
13285 // While a partial reduction match would be:
13286 // <2,3,u,u,u,u,u,u>
13287 // <1,u,u,u,u,u,u,u>
13288 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13289 SDValue PrevOp;
13290 for (unsigned i = 0; i < Stages; ++i) {
13291 unsigned MaskEnd = (1 << i);
13292
13293 if (Op.getOpcode() != CandidateBinOp)
13294 return PartialReduction(PrevOp, MaskEnd);
13295
13296 SDValue Op0 = Op.getOperand(0);
13297 SDValue Op1 = Op.getOperand(1);
13298
13300 if (Shuffle) {
13301 Op = Op1;
13302 } else {
13303 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13304 Op = Op0;
13305 }
13306
13307 // The first operand of the shuffle should be the same as the other operand
13308 // of the binop.
13309 if (!Shuffle || Shuffle->getOperand(0) != Op)
13310 return PartialReduction(PrevOp, MaskEnd);
13311
13312 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13313 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13314 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13315 return PartialReduction(PrevOp, MaskEnd);
13316
13317 PrevOp = Op;
13318 }
13319
13320 // Handle subvector reductions, which tend to appear after the shuffle
13321 // reduction stages.
13322 while (Op.getOpcode() == CandidateBinOp) {
13323 unsigned NumElts = Op.getValueType().getVectorNumElements();
13324 SDValue Op0 = Op.getOperand(0);
13325 SDValue Op1 = Op.getOperand(1);
13326 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13328 Op0.getOperand(0) != Op1.getOperand(0))
13329 break;
13330 SDValue Src = Op0.getOperand(0);
13331 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13332 if (NumSrcElts != (2 * NumElts))
13333 break;
13334 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13335 Op1.getConstantOperandAPInt(1) == NumElts) &&
13336 !(Op1.getConstantOperandAPInt(1) == 0 &&
13337 Op0.getConstantOperandAPInt(1) == NumElts))
13338 break;
13339 Op = Src;
13340 }
13341
13342 BinOp = (ISD::NodeType)CandidateBinOp;
13343 return Op;
13344}
13345
13347 EVT VT = N->getValueType(0);
13348 EVT EltVT = VT.getVectorElementType();
13349 unsigned NE = VT.getVectorNumElements();
13350
13351 SDLoc dl(N);
13352
13353 // If ResNE is 0, fully unroll the vector op.
13354 if (ResNE == 0)
13355 ResNE = NE;
13356 else if (NE > ResNE)
13357 NE = ResNE;
13358
13359 if (N->getNumValues() == 2) {
13360 SmallVector<SDValue, 8> Scalars0, Scalars1;
13361 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13362 EVT VT1 = N->getValueType(1);
13363 EVT EltVT1 = VT1.getVectorElementType();
13364
13365 unsigned i;
13366 for (i = 0; i != NE; ++i) {
13367 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13368 SDValue Operand = N->getOperand(j);
13369 EVT OperandVT = Operand.getValueType();
13370
13371 // A vector operand; extract a single element.
13372 EVT OperandEltVT = OperandVT.getVectorElementType();
13373 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13374 }
13375
13376 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13377 Scalars0.push_back(EltOp);
13378 Scalars1.push_back(EltOp.getValue(1));
13379 }
13380
13381 for (; i < ResNE; ++i) {
13382 Scalars0.push_back(getUNDEF(EltVT));
13383 Scalars1.push_back(getUNDEF(EltVT1));
13384 }
13385
13386 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13387 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13388 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13389 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13390 return getMergeValues({Vec0, Vec1}, dl);
13391 }
13392
13393 assert(N->getNumValues() == 1 &&
13394 "Can't unroll a vector with multiple results!");
13395
13397 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13398
13399 unsigned i;
13400 for (i= 0; i != NE; ++i) {
13401 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13402 SDValue Operand = N->getOperand(j);
13403 EVT OperandVT = Operand.getValueType();
13404 if (OperandVT.isVector()) {
13405 // A vector operand; extract a single element.
13406 EVT OperandEltVT = OperandVT.getVectorElementType();
13407 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13408 } else {
13409 // A scalar operand; just use it as is.
13410 Operands[j] = Operand;
13411 }
13412 }
13413
13414 switch (N->getOpcode()) {
13415 default: {
13416 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13417 N->getFlags()));
13418 break;
13419 }
13420 case ISD::VSELECT:
13421 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13422 break;
13423 case ISD::SHL:
13424 case ISD::SRA:
13425 case ISD::SRL:
13426 case ISD::ROTL:
13427 case ISD::ROTR:
13428 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13429 getShiftAmountOperand(Operands[0].getValueType(),
13430 Operands[1])));
13431 break;
13433 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13434 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13435 Operands[0],
13436 getValueType(ExtVT)));
13437 break;
13438 }
13439 case ISD::ADDRSPACECAST: {
13440 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13441 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13442 ASC->getSrcAddressSpace(),
13443 ASC->getDestAddressSpace()));
13444 break;
13445 }
13446 }
13447 }
13448
13449 for (; i < ResNE; ++i)
13450 Scalars.push_back(getUNDEF(EltVT));
13451
13452 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13453 return getBuildVector(VecVT, dl, Scalars);
13454}
13455
13456std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13457 SDNode *N, unsigned ResNE) {
13458 unsigned Opcode = N->getOpcode();
13459 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13460 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13461 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13462 "Expected an overflow opcode");
13463
13464 EVT ResVT = N->getValueType(0);
13465 EVT OvVT = N->getValueType(1);
13466 EVT ResEltVT = ResVT.getVectorElementType();
13467 EVT OvEltVT = OvVT.getVectorElementType();
13468 SDLoc dl(N);
13469
13470 // If ResNE is 0, fully unroll the vector op.
13471 unsigned NE = ResVT.getVectorNumElements();
13472 if (ResNE == 0)
13473 ResNE = NE;
13474 else if (NE > ResNE)
13475 NE = ResNE;
13476
13477 SmallVector<SDValue, 8> LHSScalars;
13478 SmallVector<SDValue, 8> RHSScalars;
13479 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13480 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13481
13482 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13483 SDVTList VTs = getVTList(ResEltVT, SVT);
13484 SmallVector<SDValue, 8> ResScalars;
13485 SmallVector<SDValue, 8> OvScalars;
13486 for (unsigned i = 0; i < NE; ++i) {
13487 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13488 SDValue Ov =
13489 getSelect(dl, OvEltVT, Res.getValue(1),
13490 getBoolConstant(true, dl, OvEltVT, ResVT),
13491 getConstant(0, dl, OvEltVT));
13492
13493 ResScalars.push_back(Res);
13494 OvScalars.push_back(Ov);
13495 }
13496
13497 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13498 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13499
13500 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13501 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13502 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13503 getBuildVector(NewOvVT, dl, OvScalars));
13504}
13505
13508 unsigned Bytes,
13509 int Dist) const {
13510 if (LD->isVolatile() || Base->isVolatile())
13511 return false;
13512 // TODO: probably too restrictive for atomics, revisit
13513 if (!LD->isSimple())
13514 return false;
13515 if (LD->isIndexed() || Base->isIndexed())
13516 return false;
13517 if (LD->getChain() != Base->getChain())
13518 return false;
13519 EVT VT = LD->getMemoryVT();
13520 if (VT.getSizeInBits() / 8 != Bytes)
13521 return false;
13522
13523 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13524 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13525
13526 int64_t Offset = 0;
13527 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13528 return (Dist * (int64_t)Bytes == Offset);
13529 return false;
13530}
13531
13532/// InferPtrAlignment - Infer alignment of a load / store address. Return
13533/// std::nullopt if it cannot be inferred.
13535 // If this is a GlobalAddress + cst, return the alignment.
13536 const GlobalValue *GV = nullptr;
13537 int64_t GVOffset = 0;
13538 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13539 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13540 KnownBits Known(PtrWidth);
13542 unsigned AlignBits = Known.countMinTrailingZeros();
13543 if (AlignBits)
13544 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13545 }
13546
13547 // If this is a direct reference to a stack slot, use information about the
13548 // stack slot's alignment.
13549 int FrameIdx = INT_MIN;
13550 int64_t FrameOffset = 0;
13552 FrameIdx = FI->getIndex();
13553 } else if (isBaseWithConstantOffset(Ptr) &&
13555 // Handle FI+Cst
13556 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13557 FrameOffset = Ptr.getConstantOperandVal(1);
13558 }
13559
13560 if (FrameIdx != INT_MIN) {
13562 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13563 }
13564
13565 return std::nullopt;
13566}
13567
13568/// Split the scalar node with EXTRACT_ELEMENT using the provided
13569/// VTs and return the low/high part.
13570std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13571 const SDLoc &DL,
13572 const EVT &LoVT,
13573 const EVT &HiVT) {
13574 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13575 "Split node must be a scalar type");
13576 SDValue Lo =
13578 SDValue Hi =
13580 return std::make_pair(Lo, Hi);
13581}
13582
13583/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13584/// which is split (or expanded) into two not necessarily identical pieces.
13585std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13586 // Currently all types are split in half.
13587 EVT LoVT, HiVT;
13588 if (!VT.isVector())
13589 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13590 else
13591 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13592
13593 return std::make_pair(LoVT, HiVT);
13594}
13595
13596/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13597/// type, dependent on an enveloping VT that has been split into two identical
13598/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13599std::pair<EVT, EVT>
13601 bool *HiIsEmpty) const {
13602 EVT EltTp = VT.getVectorElementType();
13603 // Examples:
13604 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13605 // custom VL=9 with enveloping VL=8/8 yields 8/1
13606 // custom VL=10 with enveloping VL=8/8 yields 8/2
13607 // etc.
13608 ElementCount VTNumElts = VT.getVectorElementCount();
13609 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13610 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13611 "Mixing fixed width and scalable vectors when enveloping a type");
13612 EVT LoVT, HiVT;
13613 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13614 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13615 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13616 *HiIsEmpty = false;
13617 } else {
13618 // Flag that hi type has zero storage size, but return split envelop type
13619 // (this would be easier if vector types with zero elements were allowed).
13620 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13621 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13622 *HiIsEmpty = true;
13623 }
13624 return std::make_pair(LoVT, HiVT);
13625}
13626
13627/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13628/// low/high part.
13629std::pair<SDValue, SDValue>
13630SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13631 const EVT &HiVT) {
13632 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13633 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13634 "Splitting vector with an invalid mixture of fixed and scalable "
13635 "vector types");
13637 N.getValueType().getVectorMinNumElements() &&
13638 "More vector elements requested than available!");
13639 SDValue Lo, Hi;
13640 Lo = getExtractSubvector(DL, LoVT, N, 0);
13641 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13642 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13643 // IDX with the runtime scaling factor of the result vector type. For
13644 // fixed-width result vectors, that runtime scaling factor is 1.
13647 return std::make_pair(Lo, Hi);
13648}
13649
13650std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13651 const SDLoc &DL) {
13652 // Split the vector length parameter.
13653 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13654 EVT VT = N.getValueType();
13656 "Expecting the mask to be an evenly-sized vector");
13657 SDValue HalfNumElts = getElementCount(
13659 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13660 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13661 return std::make_pair(Lo, Hi);
13662}
13663
13664/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13666 EVT VT = N.getValueType();
13669 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13670}
13671
13674 unsigned Start, unsigned Count,
13675 EVT EltVT) {
13676 EVT VT = Op.getValueType();
13677 if (Count == 0)
13679 if (EltVT == EVT())
13680 EltVT = VT.getVectorElementType();
13681 SDLoc SL(Op);
13682 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13683 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13684 }
13685}
13686
13687// getAddressSpace - Return the address space this GlobalAddress belongs to.
13689 return getGlobal()->getType()->getAddressSpace();
13690}
13691
13694 return Val.MachineCPVal->getType();
13695 return Val.ConstVal->getType();
13696}
13697
13698bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13699 unsigned &SplatBitSize,
13700 bool &HasAnyUndefs,
13701 unsigned MinSplatBits,
13702 bool IsBigEndian) const {
13703 EVT VT = getValueType(0);
13704 assert(VT.isVector() && "Expected a vector type");
13705 unsigned VecWidth = VT.getSizeInBits();
13706 if (MinSplatBits > VecWidth)
13707 return false;
13708
13709 // FIXME: The widths are based on this node's type, but build vectors can
13710 // truncate their operands.
13711 SplatValue = APInt(VecWidth, 0);
13712 SplatUndef = APInt(VecWidth, 0);
13713
13714 // Get the bits. Bits with undefined values (when the corresponding element
13715 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13716 // in SplatValue. If any of the values are not constant, give up and return
13717 // false.
13718 unsigned int NumOps = getNumOperands();
13719 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13720 unsigned EltWidth = VT.getScalarSizeInBits();
13721
13722 for (unsigned j = 0; j < NumOps; ++j) {
13723 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13724 SDValue OpVal = getOperand(i);
13725 unsigned BitPos = j * EltWidth;
13726
13727 if (OpVal.isUndef())
13728 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13729 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13730 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13731 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13732 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13733 else
13734 return false;
13735 }
13736
13737 // The build_vector is all constants or undefs. Find the smallest element
13738 // size that splats the vector.
13739 HasAnyUndefs = (SplatUndef != 0);
13740
13741 // FIXME: This does not work for vectors with elements less than 8 bits.
13742 while (VecWidth > 8) {
13743 // If we can't split in half, stop here.
13744 if (VecWidth & 1)
13745 break;
13746
13747 unsigned HalfSize = VecWidth / 2;
13748 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13749 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13750 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13751 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13752
13753 // If the two halves do not match (ignoring undef bits), stop here.
13754 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13755 MinSplatBits > HalfSize)
13756 break;
13757
13758 SplatValue = HighValue | LowValue;
13759 SplatUndef = HighUndef & LowUndef;
13760
13761 VecWidth = HalfSize;
13762 }
13763
13764 // FIXME: The loop above only tries to split in halves. But if the input
13765 // vector for example is <3 x i16> it wouldn't be able to detect a
13766 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13767 // optimizations. I guess that back in the days when this helper was created
13768 // vectors normally was power-of-2 sized.
13769
13770 SplatBitSize = VecWidth;
13771 return true;
13772}
13773
13775 BitVector *UndefElements) const {
13776 unsigned NumOps = getNumOperands();
13777 if (UndefElements) {
13778 UndefElements->clear();
13779 UndefElements->resize(NumOps);
13780 }
13781 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13782 if (!DemandedElts)
13783 return SDValue();
13784 SDValue Splatted;
13785 for (unsigned i = 0; i != NumOps; ++i) {
13786 if (!DemandedElts[i])
13787 continue;
13788 SDValue Op = getOperand(i);
13789 if (Op.isUndef()) {
13790 if (UndefElements)
13791 (*UndefElements)[i] = true;
13792 } else if (!Splatted) {
13793 Splatted = Op;
13794 } else if (Splatted != Op) {
13795 return SDValue();
13796 }
13797 }
13798
13799 if (!Splatted) {
13800 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13801 assert(getOperand(FirstDemandedIdx).isUndef() &&
13802 "Can only have a splat without a constant for all undefs.");
13803 return getOperand(FirstDemandedIdx);
13804 }
13805
13806 return Splatted;
13807}
13808
13810 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13811 return getSplatValue(DemandedElts, UndefElements);
13812}
13813
13815 SmallVectorImpl<SDValue> &Sequence,
13816 BitVector *UndefElements) const {
13817 unsigned NumOps = getNumOperands();
13818 Sequence.clear();
13819 if (UndefElements) {
13820 UndefElements->clear();
13821 UndefElements->resize(NumOps);
13822 }
13823 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13824 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13825 return false;
13826
13827 // Set the undefs even if we don't find a sequence (like getSplatValue).
13828 if (UndefElements)
13829 for (unsigned I = 0; I != NumOps; ++I)
13830 if (DemandedElts[I] && getOperand(I).isUndef())
13831 (*UndefElements)[I] = true;
13832
13833 // Iteratively widen the sequence length looking for repetitions.
13834 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13835 Sequence.append(SeqLen, SDValue());
13836 for (unsigned I = 0; I != NumOps; ++I) {
13837 if (!DemandedElts[I])
13838 continue;
13839 SDValue &SeqOp = Sequence[I % SeqLen];
13841 if (Op.isUndef()) {
13842 if (!SeqOp)
13843 SeqOp = Op;
13844 continue;
13845 }
13846 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13847 Sequence.clear();
13848 break;
13849 }
13850 SeqOp = Op;
13851 }
13852 if (!Sequence.empty())
13853 return true;
13854 }
13855
13856 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13857 return false;
13858}
13859
13861 BitVector *UndefElements) const {
13862 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13863 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
13864}
13865
13868 BitVector *UndefElements) const {
13870 getSplatValue(DemandedElts, UndefElements));
13871}
13872
13875 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
13876}
13877
13880 BitVector *UndefElements) const {
13882 getSplatValue(DemandedElts, UndefElements));
13883}
13884
13889
13890int32_t
13892 uint32_t BitWidth) const {
13893 if (ConstantFPSDNode *CN =
13895 bool IsExact;
13896 APSInt IntVal(BitWidth);
13897 const APFloat &APF = CN->getValueAPF();
13898 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
13899 APFloat::opOK ||
13900 !IsExact)
13901 return -1;
13902
13903 return IntVal.exactLogBase2();
13904 }
13905 return -1;
13906}
13907
13909 bool IsLittleEndian, unsigned DstEltSizeInBits,
13910 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
13911 // Early-out if this contains anything but Undef/Constant/ConstantFP.
13912 if (!isConstant())
13913 return false;
13914
13915 unsigned NumSrcOps = getNumOperands();
13916 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
13917 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13918 "Invalid bitcast scale");
13919
13920 // Extract raw src bits.
13921 SmallVector<APInt> SrcBitElements(NumSrcOps,
13922 APInt::getZero(SrcEltSizeInBits));
13923 BitVector SrcUndeElements(NumSrcOps, false);
13924
13925 for (unsigned I = 0; I != NumSrcOps; ++I) {
13927 if (Op.isUndef()) {
13928 SrcUndeElements.set(I);
13929 continue;
13930 }
13931 auto *CInt = dyn_cast<ConstantSDNode>(Op);
13932 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
13933 assert((CInt || CFP) && "Unknown constant");
13934 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
13935 : CFP->getValueAPF().bitcastToAPInt();
13936 }
13937
13938 // Recast to dst width.
13939 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
13940 SrcBitElements, UndefElements, SrcUndeElements);
13941 return true;
13942}
13943
13944void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
13945 unsigned DstEltSizeInBits,
13946 SmallVectorImpl<APInt> &DstBitElements,
13947 ArrayRef<APInt> SrcBitElements,
13948 BitVector &DstUndefElements,
13949 const BitVector &SrcUndefElements) {
13950 unsigned NumSrcOps = SrcBitElements.size();
13951 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
13952 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
13953 "Invalid bitcast scale");
13954 assert(NumSrcOps == SrcUndefElements.size() &&
13955 "Vector size mismatch");
13956
13957 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
13958 DstUndefElements.clear();
13959 DstUndefElements.resize(NumDstOps, false);
13960 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
13961
13962 // Concatenate src elements constant bits together into dst element.
13963 if (SrcEltSizeInBits <= DstEltSizeInBits) {
13964 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
13965 for (unsigned I = 0; I != NumDstOps; ++I) {
13966 DstUndefElements.set(I);
13967 APInt &DstBits = DstBitElements[I];
13968 for (unsigned J = 0; J != Scale; ++J) {
13969 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13970 if (SrcUndefElements[Idx])
13971 continue;
13972 DstUndefElements.reset(I);
13973 const APInt &SrcBits = SrcBitElements[Idx];
13974 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
13975 "Illegal constant bitwidths");
13976 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
13977 }
13978 }
13979 return;
13980 }
13981
13982 // Split src element constant bits into dst elements.
13983 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
13984 for (unsigned I = 0; I != NumSrcOps; ++I) {
13985 if (SrcUndefElements[I]) {
13986 DstUndefElements.set(I * Scale, (I + 1) * Scale);
13987 continue;
13988 }
13989 const APInt &SrcBits = SrcBitElements[I];
13990 for (unsigned J = 0; J != Scale; ++J) {
13991 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
13992 APInt &DstBits = DstBitElements[Idx];
13993 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
13994 }
13995 }
13996}
13997
13999 for (const SDValue &Op : op_values()) {
14000 unsigned Opc = Op.getOpcode();
14001 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14002 return false;
14003 }
14004 return true;
14005}
14006
14007std::optional<std::pair<APInt, APInt>>
14009 unsigned NumOps = getNumOperands();
14010 if (NumOps < 2)
14011 return std::nullopt;
14012
14015 return std::nullopt;
14016
14017 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14018 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14019 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14020
14021 if (Stride.isZero())
14022 return std::nullopt;
14023
14024 for (unsigned i = 2; i < NumOps; ++i) {
14026 return std::nullopt;
14027
14028 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14029 if (Val != (Start + (Stride * i)))
14030 return std::nullopt;
14031 }
14032
14033 return std::make_pair(Start, Stride);
14034}
14035
14037 // Find the first non-undef value in the shuffle mask.
14038 unsigned i, e;
14039 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14040 /* search */;
14041
14042 // If all elements are undefined, this shuffle can be considered a splat
14043 // (although it should eventually get simplified away completely).
14044 if (i == e)
14045 return true;
14046
14047 // Make sure all remaining elements are either undef or the same as the first
14048 // non-undef value.
14049 for (int Idx = Mask[i]; i != e; ++i)
14050 if (Mask[i] >= 0 && Mask[i] != Idx)
14051 return false;
14052 return true;
14053}
14054
14055// Returns true if it is a constant integer BuildVector or constant integer,
14056// possibly hidden by a bitcast.
14058 SDValue N, bool AllowOpaques) const {
14060
14061 if (auto *C = dyn_cast<ConstantSDNode>(N))
14062 return AllowOpaques || !C->isOpaque();
14063
14065 return true;
14066
14067 // Treat a GlobalAddress supporting constant offset folding as a
14068 // constant integer.
14069 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14070 if (GA->getOpcode() == ISD::GlobalAddress &&
14071 TLI->isOffsetFoldingLegal(GA))
14072 return true;
14073
14074 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14075 isa<ConstantSDNode>(N.getOperand(0)))
14076 return true;
14077 return false;
14078}
14079
14080// Returns true if it is a constant float BuildVector or constant float.
14083 return true;
14084
14086 return true;
14087
14088 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14089 isa<ConstantFPSDNode>(N.getOperand(0)))
14090 return true;
14091
14092 return false;
14093}
14094
14095std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14096 ConstantSDNode *Const =
14097 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14098 if (!Const)
14099 return std::nullopt;
14100
14101 EVT VT = N->getValueType(0);
14102 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14103 switch (TLI->getBooleanContents(N.getValueType())) {
14105 if (CVal.isOne())
14106 return true;
14107 if (CVal.isZero())
14108 return false;
14109 return std::nullopt;
14111 if (CVal.isAllOnes())
14112 return true;
14113 if (CVal.isZero())
14114 return false;
14115 return std::nullopt;
14117 return CVal[0];
14118 }
14119 llvm_unreachable("Unknown BooleanContent enum");
14120}
14121
14122void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14123 assert(!Node->OperandList && "Node already has operands");
14125 "too many operands to fit into SDNode");
14126 SDUse *Ops = OperandRecycler.allocate(
14127 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14128
14129 bool IsDivergent = false;
14130 for (unsigned I = 0; I != Vals.size(); ++I) {
14131 Ops[I].setUser(Node);
14132 Ops[I].setInitial(Vals[I]);
14133 EVT VT = Ops[I].getValueType();
14134
14135 // Skip Chain. It does not carry divergence.
14136 if (VT != MVT::Other &&
14137 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14138 Ops[I].getNode()->isDivergent()) {
14139 IsDivergent = true;
14140 }
14141 }
14142 Node->NumOperands = Vals.size();
14143 Node->OperandList = Ops;
14144 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14145 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14146 Node->SDNodeBits.IsDivergent = IsDivergent;
14147 }
14148 checkForCycles(Node);
14149}
14150
14153 size_t Limit = SDNode::getMaxNumOperands();
14154 while (Vals.size() > Limit) {
14155 unsigned SliceIdx = Vals.size() - Limit;
14156 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14157 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14158 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14159 Vals.emplace_back(NewTF);
14160 }
14161 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14162}
14163
14165 EVT VT, SDNodeFlags Flags) {
14166 switch (Opcode) {
14167 default:
14168 return SDValue();
14169 case ISD::ADD:
14170 case ISD::OR:
14171 case ISD::XOR:
14172 case ISD::UMAX:
14173 return getConstant(0, DL, VT);
14174 case ISD::MUL:
14175 return getConstant(1, DL, VT);
14176 case ISD::AND:
14177 case ISD::UMIN:
14178 return getAllOnesConstant(DL, VT);
14179 case ISD::SMAX:
14181 case ISD::SMIN:
14183 case ISD::FADD:
14184 // If flags allow, prefer positive zero since it's generally cheaper
14185 // to materialize on most targets.
14186 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14187 case ISD::FMUL:
14188 return getConstantFP(1.0, DL, VT);
14189 case ISD::FMINNUM:
14190 case ISD::FMAXNUM: {
14191 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14192 const fltSemantics &Semantics = VT.getFltSemantics();
14193 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14194 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14195 APFloat::getLargest(Semantics);
14196 if (Opcode == ISD::FMAXNUM)
14197 NeutralAF.changeSign();
14198
14199 return getConstantFP(NeutralAF, DL, VT);
14200 }
14201 case ISD::FMINIMUM:
14202 case ISD::FMAXIMUM: {
14203 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14204 const fltSemantics &Semantics = VT.getFltSemantics();
14205 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14206 : APFloat::getLargest(Semantics);
14207 if (Opcode == ISD::FMAXIMUM)
14208 NeutralAF.changeSign();
14209
14210 return getConstantFP(NeutralAF, DL, VT);
14211 }
14212
14213 }
14214}
14215
14216/// Helper used to make a call to a library function that has one argument of
14217/// pointer type.
14218///
14219/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14220/// used to get or set floating-point state. They have one argument of pointer
14221/// type, which points to the memory region containing bits of the
14222/// floating-point state. The value returned by such function is ignored in the
14223/// created call.
14224///
14225/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14226/// \param Ptr Pointer used to save/load state.
14227/// \param InChain Ingoing token chain.
14228/// \returns Outgoing chain token.
14230 SDValue InChain,
14231 const SDLoc &DLoc) {
14232 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14234 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14235 RTLIB::LibcallImpl LibcallImpl =
14236 TLI->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14237 if (LibcallImpl == RTLIB::Unsupported)
14238 reportFatalUsageError("emitting call to unsupported libcall");
14239
14240 SDValue Callee =
14241 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14243 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14244 TLI->getLibcallImplCallingConv(LibcallImpl),
14245 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14246 return TLI->LowerCallTo(CLI).second;
14247}
14248
14250 assert(From && To && "Invalid SDNode; empty source SDValue?");
14251 auto I = SDEI.find(From);
14252 if (I == SDEI.end())
14253 return;
14254
14255 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14256 // the iterator, hence the need to make a copy to prevent a use-after-free.
14257 NodeExtraInfo NEI = I->second;
14258 if (LLVM_LIKELY(!NEI.PCSections)) {
14259 // No deep copy required for the types of extra info set.
14260 //
14261 // FIXME: Investigate if other types of extra info also need deep copy. This
14262 // depends on the types of nodes they can be attached to: if some extra info
14263 // is only ever attached to nodes where a replacement To node is always the
14264 // node where later use and propagation of the extra info has the intended
14265 // semantics, no deep copy is required.
14266 SDEI[To] = std::move(NEI);
14267 return;
14268 }
14269
14270 const SDNode *EntrySDN = getEntryNode().getNode();
14271
14272 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14273 // through the replacement of From with To. Otherwise, replacements of a node
14274 // (From) with more complex nodes (To and its operands) may result in lost
14275 // extra info where the root node (To) is insignificant in further propagating
14276 // and using extra info when further lowering to MIR.
14277 //
14278 // In the first step pre-populate the visited set with the nodes reachable
14279 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14280 // DAG that is not new and should be left untouched.
14281 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14282 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14283 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14284 if (MaxDepth == 0) {
14285 // Remember this node in case we need to increase MaxDepth and continue
14286 // populating FromReach from this node.
14287 Leafs.emplace_back(N);
14288 return;
14289 }
14290 if (!FromReach.insert(N).second)
14291 return;
14292 for (const SDValue &Op : N->op_values())
14293 Self(Self, Op.getNode(), MaxDepth - 1);
14294 };
14295
14296 // Copy extra info to To and all its transitive operands (that are new).
14298 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14299 if (FromReach.contains(N))
14300 return true;
14301 if (!Visited.insert(N).second)
14302 return true;
14303 if (EntrySDN == N)
14304 return false;
14305 for (const SDValue &Op : N->op_values()) {
14306 if (N == To && Op.getNode() == EntrySDN) {
14307 // Special case: New node's operand is the entry node; just need to
14308 // copy extra info to new node.
14309 break;
14310 }
14311 if (!Self(Self, Op.getNode()))
14312 return false;
14313 }
14314 // Copy only if entry node was not reached.
14315 SDEI[N] = NEI;
14316 return true;
14317 };
14318
14319 // We first try with a lower MaxDepth, assuming that the path to common
14320 // operands between From and To is relatively short. This significantly
14321 // improves performance in the common case. The initial MaxDepth is big
14322 // enough to avoid retry in the common case; the last MaxDepth is large
14323 // enough to avoid having to use the fallback below (and protects from
14324 // potential stack exhaustion from recursion).
14325 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14326 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14327 // StartFrom is the previous (or initial) set of leafs reachable at the
14328 // previous maximum depth.
14330 std::swap(StartFrom, Leafs);
14331 for (const SDNode *N : StartFrom)
14332 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14333 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14334 return;
14335 // This should happen very rarely (reached the entry node).
14336 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14337 assert(!Leafs.empty());
14338 }
14339
14340 // This should not happen - but if it did, that means the subgraph reachable
14341 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14342 // could not visit all reachable common operands. Consequently, we were able
14343 // to reach the entry node.
14344 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14345 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14346 // Best-effort fallback if assertions disabled.
14347 SDEI[To] = std::move(NEI);
14348}
14349
14350#ifndef NDEBUG
14351static void checkForCyclesHelper(const SDNode *N,
14354 const llvm::SelectionDAG *DAG) {
14355 // If this node has already been checked, don't check it again.
14356 if (Checked.count(N))
14357 return;
14358
14359 // If a node has already been visited on this depth-first walk, reject it as
14360 // a cycle.
14361 if (!Visited.insert(N).second) {
14362 errs() << "Detected cycle in SelectionDAG\n";
14363 dbgs() << "Offending node:\n";
14364 N->dumprFull(DAG); dbgs() << "\n";
14365 abort();
14366 }
14367
14368 for (const SDValue &Op : N->op_values())
14369 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14370
14371 Checked.insert(N);
14372 Visited.erase(N);
14373}
14374#endif
14375
14377 const llvm::SelectionDAG *DAG,
14378 bool force) {
14379#ifndef NDEBUG
14380 bool check = force;
14381#ifdef EXPENSIVE_CHECKS
14382 check = true;
14383#endif // EXPENSIVE_CHECKS
14384 if (check) {
14385 assert(N && "Checking nonexistent SDNode");
14388 checkForCyclesHelper(N, visited, checked, DAG);
14389 }
14390#endif // !NDEBUG
14391}
14392
14393void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14394 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14395}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:569
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue getFixedOrScalableQuantity(SelectionDAG &DAG, const SDLoc &DL, EVT VT, Ty Quantity)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1102
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1190
void copySign(const APFloat &RHS)
Definition APFloat.h:1284
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6053
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1172
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition APFloat.h:1414
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1163
bool isFinite() const
Definition APFloat.h:1436
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1329
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1181
bool isSignaling() const
Definition APFloat.h:1433
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1217
bool isZero() const
Definition APFloat.h:1427
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1314
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1208
bool isPosZero() const
Definition APFloat.h:1442
bool isNegZero() const
Definition APFloat.h:1443
void changeSign()
Definition APFloat.h:1279
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1091
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1971
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2055
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1407
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1012
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1541
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1392
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1671
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1386
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1033
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1513
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1331
APInt abs() const
Get the absolute value.
Definition APInt.h:1796
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2026
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1489
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1397
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1154
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:835
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1640
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1629
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1599
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2086
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2100
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1141
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1436
unsigned logBase2() const
Definition APInt.h:1762
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2036
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1736
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:985
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1368
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1418
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1389
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2045
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:904
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
const APFloat & getValue() const
Definition Constants.h:326
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:330
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This SDNode is used for LIFETIME_START/LIFETIME_END values.
This class is used to represent ISD::LOAD nodes.
static LocationSize precise(uint64_t Value)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:230
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI void dump(bool Sorted=false) const
Dump the textual format of this DAG.
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
virtual bool shallExtractConstSplatVectorElementToStore(Type *VectorTy, unsigned ElemSizeInBits, unsigned &Index) const
Return true if the target shall perform extract vector element and store given that the vector is kno...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
BooleanContent
Enum that describes how the target represents true/false values.
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
virtual bool isLegalStoreImmediate(int64_t Value) const
Return true if the specified immediate is legal for the value input of a store instruction.
static ISD::NodeType getExtendForContent(BooleanContent Content)
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool findOptimalMemOpLowering(LLVMContext &Context, std::vector< EVT > &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS, unsigned SrcAS, const AttributeList &FuncAttributes) const
Determines the optimal series of memory ops to replace the memset / memcpy.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:627
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:232
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3131
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3118
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3108
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3182
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3123
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2269
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3173
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3009
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2274
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3103
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3113
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:807
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:256
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:780
@ TargetConstantPool
Definition ISDOpcodes.h:184
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
@ 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
@ PARTIAL_REDUCE_SMLA
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ 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
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:771
@ TargetBlockAddress
Definition ISDOpcodes.h:186
@ DEACTIVATION_SYMBOL
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ 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
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:841
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:511
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:215
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ 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
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:410
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ 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
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
Definition ISDOpcodes.h:991
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:521
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:981
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:249
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ PARTIAL_REDUCE_UMLA
@ 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
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ 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
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ 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
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ 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
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ 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
@ GET_ACTIVE_LANE_MASK
@ 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
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:701
@ ATOMIC_LOAD_FMAXIMUM
@ 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
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ 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
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:379
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:351
@ ATOMIC_LOAD_FMINIMUM
@ 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
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:966
@ 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
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ ATOMIC_LOAD_UDEC_WRAP
@ 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
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ 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
@ GET_FPENV_MEM
Gets the current floating-point environment.
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ 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
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ 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
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:985
@ EXPERIMENTAL_VECTOR_HISTOGRAM
@ 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
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:844
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ VECREDUCE_SEQ_FMUL
@ 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
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:527
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:360
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:719
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:333
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:208
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:181
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:549
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:241
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1757
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1611
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2494
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
LLVM_ABI bool isOneOrOneSplatFP(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant floating-point value, or a splatted vector of a constant float...
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1625
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2148
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1593
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1537
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1580
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1611
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1751
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1561
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:543
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1847
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:723
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1909
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1598
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1638
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:761
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:781
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:778
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
intptr_t getRawBits() const
Definition ValueTypes.h:512
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:142
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:308
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:453
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:301
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:124
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:274
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:119
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:161
KnownBits byteSwap() const
Definition KnownBits.h:514
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:233
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:172
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:321
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:326
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:167
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)