LLVM 23.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 (MinMaxOpc) {
438 default:
439 llvm_unreachable("unrecognized min/max opcode");
440 case ISD::SMIN:
441 return ISD::UMIN;
442 case ISD::SMAX:
443 return ISD::UMAX;
444 case ISD::UMIN:
445 return ISD::SMIN;
446 case ISD::UMAX:
447 return ISD::SMAX;
448 }
449}
450
452 switch (VecReduceOpcode) {
453 default:
454 llvm_unreachable("Expected VECREDUCE opcode");
457 case ISD::VP_REDUCE_FADD:
458 case ISD::VP_REDUCE_SEQ_FADD:
459 return ISD::FADD;
462 case ISD::VP_REDUCE_FMUL:
463 case ISD::VP_REDUCE_SEQ_FMUL:
464 return ISD::FMUL;
466 case ISD::VP_REDUCE_ADD:
467 return ISD::ADD;
469 case ISD::VP_REDUCE_MUL:
470 return ISD::MUL;
472 case ISD::VP_REDUCE_AND:
473 return ISD::AND;
475 case ISD::VP_REDUCE_OR:
476 return ISD::OR;
478 case ISD::VP_REDUCE_XOR:
479 return ISD::XOR;
481 case ISD::VP_REDUCE_SMAX:
482 return ISD::SMAX;
484 case ISD::VP_REDUCE_SMIN:
485 return ISD::SMIN;
487 case ISD::VP_REDUCE_UMAX:
488 return ISD::UMAX;
490 case ISD::VP_REDUCE_UMIN:
491 return ISD::UMIN;
493 case ISD::VP_REDUCE_FMAX:
494 return ISD::FMAXNUM;
496 case ISD::VP_REDUCE_FMIN:
497 return ISD::FMINNUM;
499 case ISD::VP_REDUCE_FMAXIMUM:
500 return ISD::FMAXIMUM;
502 case ISD::VP_REDUCE_FMINIMUM:
503 return ISD::FMINIMUM;
504 }
505}
506
507bool ISD::isVPOpcode(unsigned Opcode) {
508 switch (Opcode) {
509 default:
510 return false;
511#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
512 case ISD::VPSD: \
513 return true;
514#include "llvm/IR/VPIntrinsics.def"
515 }
516}
517
518bool ISD::isVPBinaryOp(unsigned Opcode) {
519 switch (Opcode) {
520 default:
521 break;
522#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
523#define VP_PROPERTY_BINARYOP return true;
524#define END_REGISTER_VP_SDNODE(VPSD) break;
525#include "llvm/IR/VPIntrinsics.def"
526 }
527 return false;
528}
529
530bool ISD::isVPReduction(unsigned Opcode) {
531 switch (Opcode) {
532 default:
533 return false;
534 case ISD::VP_REDUCE_ADD:
535 case ISD::VP_REDUCE_MUL:
536 case ISD::VP_REDUCE_AND:
537 case ISD::VP_REDUCE_OR:
538 case ISD::VP_REDUCE_XOR:
539 case ISD::VP_REDUCE_SMAX:
540 case ISD::VP_REDUCE_SMIN:
541 case ISD::VP_REDUCE_UMAX:
542 case ISD::VP_REDUCE_UMIN:
543 case ISD::VP_REDUCE_FMAX:
544 case ISD::VP_REDUCE_FMIN:
545 case ISD::VP_REDUCE_FMAXIMUM:
546 case ISD::VP_REDUCE_FMINIMUM:
547 case ISD::VP_REDUCE_FADD:
548 case ISD::VP_REDUCE_FMUL:
549 case ISD::VP_REDUCE_SEQ_FADD:
550 case ISD::VP_REDUCE_SEQ_FMUL:
551 return true;
552 }
553}
554
555/// The operand position of the vector mask.
556std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
557 switch (Opcode) {
558 default:
559 return std::nullopt;
560#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
561 case ISD::VPSD: \
562 return MASKPOS;
563#include "llvm/IR/VPIntrinsics.def"
564 }
565}
566
567/// The operand position of the explicit vector length parameter.
568std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
569 switch (Opcode) {
570 default:
571 return std::nullopt;
572#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
573 case ISD::VPSD: \
574 return EVLPOS;
575#include "llvm/IR/VPIntrinsics.def"
576 }
577}
578
579std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
580 bool hasFPExcept) {
581 // FIXME: Return strict opcodes in case of fp exceptions.
582 switch (VPOpcode) {
583 default:
584 return std::nullopt;
585#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
586#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
587#define END_REGISTER_VP_SDNODE(VPOPC) break;
588#include "llvm/IR/VPIntrinsics.def"
589 }
590 return std::nullopt;
591}
592
593std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
594 switch (Opcode) {
595 default:
596 return std::nullopt;
597#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
598#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
599#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
600#include "llvm/IR/VPIntrinsics.def"
601 }
602}
603
605 switch (ExtType) {
606 case ISD::EXTLOAD:
607 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
608 case ISD::SEXTLOAD:
609 return ISD::SIGN_EXTEND;
610 case ISD::ZEXTLOAD:
611 return ISD::ZERO_EXTEND;
612 default:
613 break;
614 }
615
616 llvm_unreachable("Invalid LoadExtType");
617}
618
620 // To perform this operation, we just need to swap the L and G bits of the
621 // operation.
622 unsigned OldL = (Operation >> 2) & 1;
623 unsigned OldG = (Operation >> 1) & 1;
624 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
625 (OldL << 1) | // New G bit
626 (OldG << 2)); // New L bit.
627}
628
630 unsigned Operation = Op;
631 if (isIntegerLike)
632 Operation ^= 7; // Flip L, G, E bits, but not U.
633 else
634 Operation ^= 15; // Flip all of the condition bits.
635
637 Operation &= ~8; // Don't let N and U bits get set.
638
639 return ISD::CondCode(Operation);
640}
641
645
647 bool isIntegerLike) {
648 return getSetCCInverseImpl(Op, isIntegerLike);
649}
650
651/// For an integer comparison, return 1 if the comparison is a signed operation
652/// and 2 if the result is an unsigned comparison. Return zero if the operation
653/// does not depend on the sign of the input (setne and seteq).
654static int isSignedOp(ISD::CondCode Opcode) {
655 switch (Opcode) {
656 default: llvm_unreachable("Illegal integer setcc operation!");
657 case ISD::SETEQ:
658 case ISD::SETNE: return 0;
659 case ISD::SETLT:
660 case ISD::SETLE:
661 case ISD::SETGT:
662 case ISD::SETGE: return 1;
663 case ISD::SETULT:
664 case ISD::SETULE:
665 case ISD::SETUGT:
666 case ISD::SETUGE: return 2;
667 }
668}
669
671 EVT Type) {
672 bool IsInteger = Type.isInteger();
673 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
674 // Cannot fold a signed integer setcc with an unsigned integer setcc.
675 return ISD::SETCC_INVALID;
676
677 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
678
679 // If the N and U bits get set, then the resultant comparison DOES suddenly
680 // care about orderedness, and it is true when ordered.
681 if (Op > ISD::SETTRUE2)
682 Op &= ~16; // Clear the U bit if the N bit is set.
683
684 // Canonicalize illegal integer setcc's.
685 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
686 Op = ISD::SETNE;
687
688 return ISD::CondCode(Op);
689}
690
692 EVT Type) {
693 bool IsInteger = Type.isInteger();
694 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
695 // Cannot fold a signed setcc with an unsigned setcc.
696 return ISD::SETCC_INVALID;
697
698 // Combine all of the condition bits.
699 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
700
701 // Canonicalize illegal integer setcc's.
702 if (IsInteger) {
703 switch (Result) {
704 default: break;
705 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
706 case ISD::SETOEQ: // SETEQ & SETU[LG]E
707 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
708 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
709 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
710 }
711 }
712
713 return Result;
714}
715
716//===----------------------------------------------------------------------===//
717// SDNode Profile Support
718//===----------------------------------------------------------------------===//
719
720/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
721static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
722 ID.AddInteger(OpC);
723}
724
725/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
726/// solely with their pointer.
728 ID.AddPointer(VTList.VTs);
729}
730
731/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
734 for (const auto &Op : Ops) {
735 ID.AddPointer(Op.getNode());
736 ID.AddInteger(Op.getResNo());
737 }
738}
739
740/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
743 for (const auto &Op : Ops) {
744 ID.AddPointer(Op.getNode());
745 ID.AddInteger(Op.getResNo());
746 }
747}
748
749static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
750 SDVTList VTList, ArrayRef<SDValue> OpList) {
751 AddNodeIDOpcode(ID, OpC);
752 AddNodeIDValueTypes(ID, VTList);
753 AddNodeIDOperands(ID, OpList);
754}
755
756/// If this is an SDNode with special info, add this info to the NodeID data.
758 switch (N->getOpcode()) {
761 case ISD::MCSymbol:
762 llvm_unreachable("Should only be used on nodes with operands");
763 default: break; // Normal nodes don't need extra info.
765 case ISD::Constant: {
767 ID.AddPointer(C->getConstantIntValue());
768 ID.AddBoolean(C->isOpaque());
769 break;
770 }
772 case ISD::ConstantFP:
773 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
774 break;
780 ID.AddPointer(GA->getGlobal());
781 ID.AddInteger(GA->getOffset());
782 ID.AddInteger(GA->getTargetFlags());
783 break;
784 }
785 case ISD::BasicBlock:
787 break;
788 case ISD::Register:
789 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
790 break;
792 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
793 break;
794 case ISD::SRCVALUE:
795 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
796 break;
797 case ISD::FrameIndex:
799 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
800 break;
802 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
803 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
804 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
805 break;
806 case ISD::JumpTable:
808 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
809 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
810 break;
814 ID.AddInteger(CP->getAlign().value());
815 ID.AddInteger(CP->getOffset());
818 else
819 ID.AddPointer(CP->getConstVal());
820 ID.AddInteger(CP->getTargetFlags());
821 break;
822 }
823 case ISD::TargetIndex: {
825 ID.AddInteger(TI->getIndex());
826 ID.AddInteger(TI->getOffset());
827 ID.AddInteger(TI->getTargetFlags());
828 break;
829 }
830 case ISD::LOAD: {
831 const LoadSDNode *LD = cast<LoadSDNode>(N);
832 ID.AddInteger(LD->getMemoryVT().getRawBits());
833 ID.AddInteger(LD->getRawSubclassData());
834 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
835 ID.AddInteger(LD->getMemOperand()->getFlags());
836 break;
837 }
838 case ISD::STORE: {
839 const StoreSDNode *ST = cast<StoreSDNode>(N);
840 ID.AddInteger(ST->getMemoryVT().getRawBits());
841 ID.AddInteger(ST->getRawSubclassData());
842 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
843 ID.AddInteger(ST->getMemOperand()->getFlags());
844 break;
845 }
846 case ISD::VP_LOAD: {
847 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
848 ID.AddInteger(ELD->getMemoryVT().getRawBits());
849 ID.AddInteger(ELD->getRawSubclassData());
850 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
851 ID.AddInteger(ELD->getMemOperand()->getFlags());
852 break;
853 }
854 case ISD::VP_LOAD_FF: {
855 const auto *LD = cast<VPLoadFFSDNode>(N);
856 ID.AddInteger(LD->getMemoryVT().getRawBits());
857 ID.AddInteger(LD->getRawSubclassData());
858 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
859 ID.AddInteger(LD->getMemOperand()->getFlags());
860 break;
861 }
862 case ISD::VP_STORE: {
863 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
864 ID.AddInteger(EST->getMemoryVT().getRawBits());
865 ID.AddInteger(EST->getRawSubclassData());
866 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
867 ID.AddInteger(EST->getMemOperand()->getFlags());
868 break;
869 }
870 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
872 ID.AddInteger(SLD->getMemoryVT().getRawBits());
873 ID.AddInteger(SLD->getRawSubclassData());
874 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
875 break;
876 }
877 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
879 ID.AddInteger(SST->getMemoryVT().getRawBits());
880 ID.AddInteger(SST->getRawSubclassData());
881 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
882 break;
883 }
884 case ISD::VP_GATHER: {
886 ID.AddInteger(EG->getMemoryVT().getRawBits());
887 ID.AddInteger(EG->getRawSubclassData());
888 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
889 ID.AddInteger(EG->getMemOperand()->getFlags());
890 break;
891 }
892 case ISD::VP_SCATTER: {
894 ID.AddInteger(ES->getMemoryVT().getRawBits());
895 ID.AddInteger(ES->getRawSubclassData());
896 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
897 ID.AddInteger(ES->getMemOperand()->getFlags());
898 break;
899 }
900 case ISD::MLOAD: {
902 ID.AddInteger(MLD->getMemoryVT().getRawBits());
903 ID.AddInteger(MLD->getRawSubclassData());
904 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
905 ID.AddInteger(MLD->getMemOperand()->getFlags());
906 break;
907 }
908 case ISD::MSTORE: {
910 ID.AddInteger(MST->getMemoryVT().getRawBits());
911 ID.AddInteger(MST->getRawSubclassData());
912 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
913 ID.AddInteger(MST->getMemOperand()->getFlags());
914 break;
915 }
916 case ISD::MGATHER: {
918 ID.AddInteger(MG->getMemoryVT().getRawBits());
919 ID.AddInteger(MG->getRawSubclassData());
920 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
921 ID.AddInteger(MG->getMemOperand()->getFlags());
922 break;
923 }
924 case ISD::MSCATTER: {
926 ID.AddInteger(MS->getMemoryVT().getRawBits());
927 ID.AddInteger(MS->getRawSubclassData());
928 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
929 ID.AddInteger(MS->getMemOperand()->getFlags());
930 break;
931 }
934 case ISD::ATOMIC_SWAP:
946 case ISD::ATOMIC_LOAD:
947 case ISD::ATOMIC_STORE: {
948 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
949 ID.AddInteger(AT->getMemoryVT().getRawBits());
950 ID.AddInteger(AT->getRawSubclassData());
951 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
952 ID.AddInteger(AT->getMemOperand()->getFlags());
953 break;
954 }
955 case ISD::VECTOR_SHUFFLE: {
956 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
957 for (int M : Mask)
958 ID.AddInteger(M);
959 break;
960 }
961 case ISD::ADDRSPACECAST: {
963 ID.AddInteger(ASC->getSrcAddressSpace());
964 ID.AddInteger(ASC->getDestAddressSpace());
965 break;
966 }
968 case ISD::BlockAddress: {
970 ID.AddPointer(BA->getBlockAddress());
971 ID.AddInteger(BA->getOffset());
972 ID.AddInteger(BA->getTargetFlags());
973 break;
974 }
975 case ISD::AssertAlign:
976 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
977 break;
978 case ISD::PREFETCH:
981 // Handled by MemIntrinsicSDNode check after the switch.
982 break;
984 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
985 break;
986 } // end switch (N->getOpcode())
987
988 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
989 // to check.
990 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
991 ID.AddInteger(MN->getRawSubclassData());
992 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
993 ID.AddInteger(MN->getMemOperand()->getFlags());
994 ID.AddInteger(MN->getMemoryVT().getRawBits());
995 }
996}
997
998/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
999/// data.
1001 AddNodeIDOpcode(ID, N->getOpcode());
1002 // Add the return value info.
1003 AddNodeIDValueTypes(ID, N->getVTList());
1004 // Add the operand info.
1005 AddNodeIDOperands(ID, N->ops());
1006
1007 // Handle SDNode leafs with special info.
1009}
1010
1011//===----------------------------------------------------------------------===//
1012// SelectionDAG Class
1013//===----------------------------------------------------------------------===//
1014
1015/// doNotCSE - Return true if CSE should not be performed for this node.
1016static bool doNotCSE(SDNode *N) {
1017 if (N->getValueType(0) == MVT::Glue)
1018 return true; // Never CSE anything that produces a glue result.
1019
1020 switch (N->getOpcode()) {
1021 default: break;
1022 case ISD::HANDLENODE:
1023 case ISD::EH_LABEL:
1024 return true; // Never CSE these nodes.
1025 }
1026
1027 // Check that remaining values produced are not flags.
1028 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1029 if (N->getValueType(i) == MVT::Glue)
1030 return true; // Never CSE anything that produces a glue result.
1031
1032 return false;
1033}
1034
1035/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1036/// SelectionDAG.
1038 // Create a dummy node (which is not added to allnodes), that adds a reference
1039 // to the root node, preventing it from being deleted.
1040 HandleSDNode Dummy(getRoot());
1041
1042 SmallVector<SDNode*, 128> DeadNodes;
1043
1044 // Add all obviously-dead nodes to the DeadNodes worklist.
1045 for (SDNode &Node : allnodes())
1046 if (Node.use_empty())
1047 DeadNodes.push_back(&Node);
1048
1049 RemoveDeadNodes(DeadNodes);
1050
1051 // If the root changed (e.g. it was a dead load, update the root).
1052 setRoot(Dummy.getValue());
1053}
1054
1055/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1056/// given list, and any nodes that become unreachable as a result.
1058
1059 // Process the worklist, deleting the nodes and adding their uses to the
1060 // worklist.
1061 while (!DeadNodes.empty()) {
1062 SDNode *N = DeadNodes.pop_back_val();
1063 // Skip to next node if we've already managed to delete the node. This could
1064 // happen if replacing a node causes a node previously added to the node to
1065 // be deleted.
1066 if (N->getOpcode() == ISD::DELETED_NODE)
1067 continue;
1068
1069 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1070 DUL->NodeDeleted(N, nullptr);
1071
1072 // Take the node out of the appropriate CSE map.
1073 RemoveNodeFromCSEMaps(N);
1074
1075 // Next, brutally remove the operand list. This is safe to do, as there are
1076 // no cycles in the graph.
1077 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1078 SDUse &Use = *I++;
1079 SDNode *Operand = Use.getNode();
1080 Use.set(SDValue());
1081
1082 // Now that we removed this operand, see if there are no uses of it left.
1083 if (Operand->use_empty())
1084 DeadNodes.push_back(Operand);
1085 }
1086
1087 DeallocateNode(N);
1088 }
1089}
1090
1092 SmallVector<SDNode*, 16> DeadNodes(1, N);
1093
1094 // Create a dummy node that adds a reference to the root node, preventing
1095 // it from being deleted. (This matters if the root is an operand of the
1096 // dead node.)
1097 HandleSDNode Dummy(getRoot());
1098
1099 RemoveDeadNodes(DeadNodes);
1100}
1101
1103 // First take this out of the appropriate CSE map.
1104 RemoveNodeFromCSEMaps(N);
1105
1106 // Finally, remove uses due to operands of this node, remove from the
1107 // AllNodes list, and delete the node.
1108 DeleteNodeNotInCSEMaps(N);
1109}
1110
1111void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1112 assert(N->getIterator() != AllNodes.begin() &&
1113 "Cannot delete the entry node!");
1114 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1115
1116 // Drop all of the operands and decrement used node's use counts.
1117 N->DropOperands();
1118
1119 DeallocateNode(N);
1120}
1121
1122void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1123 assert(!(V->isVariadic() && isParameter));
1124 if (isParameter)
1125 ByvalParmDbgValues.push_back(V);
1126 else
1127 DbgValues.push_back(V);
1128 for (const SDNode *Node : V->getSDNodes())
1129 if (Node)
1130 DbgValMap[Node].push_back(V);
1131}
1132
1134 DbgValMapType::iterator I = DbgValMap.find(Node);
1135 if (I == DbgValMap.end())
1136 return;
1137 for (auto &Val: I->second)
1138 Val->setIsInvalidated();
1139 DbgValMap.erase(I);
1140}
1141
1142void SelectionDAG::DeallocateNode(SDNode *N) {
1143 // If we have operands, deallocate them.
1145
1146 NodeAllocator.Deallocate(AllNodes.remove(N));
1147
1148 // Set the opcode to DELETED_NODE to help catch bugs when node
1149 // memory is reallocated.
1150 // FIXME: There are places in SDag that have grown a dependency on the opcode
1151 // value in the released node.
1152 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1153 N->NodeType = ISD::DELETED_NODE;
1154
1155 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1156 // them and forget about that node.
1157 DbgInfo->erase(N);
1158
1159 // Invalidate extra info.
1160 SDEI.erase(N);
1161}
1162
1163#ifndef NDEBUG
1164/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1165void SelectionDAG::verifyNode(SDNode *N) const {
1166 switch (N->getOpcode()) {
1167 default:
1168 if (N->isTargetOpcode())
1170 break;
1171 case ISD::BUILD_PAIR: {
1172 EVT VT = N->getValueType(0);
1173 assert(N->getNumValues() == 1 && "Too many results!");
1174 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1175 "Wrong return type!");
1176 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1177 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1178 "Mismatched operand types!");
1179 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1180 "Wrong operand type!");
1181 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1182 "Wrong return type size");
1183 break;
1184 }
1185 case ISD::BUILD_VECTOR: {
1186 assert(N->getNumValues() == 1 && "Too many results!");
1187 assert(N->getValueType(0).isVector() && "Wrong return type!");
1188 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1189 "Wrong number of operands!");
1190 EVT EltVT = N->getValueType(0).getVectorElementType();
1191 for (const SDUse &Op : N->ops()) {
1192 assert((Op.getValueType() == EltVT ||
1193 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1194 EltVT.bitsLE(Op.getValueType()))) &&
1195 "Wrong operand type!");
1196 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1197 "Operands must all have the same type");
1198 }
1199 break;
1200 }
1201 }
1202}
1203#endif // NDEBUG
1204
1205/// Insert a newly allocated node into the DAG.
1206///
1207/// Handles insertion into the all nodes list and CSE map, as well as
1208/// verification and other common operations when a new node is allocated.
1209void SelectionDAG::InsertNode(SDNode *N) {
1210 AllNodes.push_back(N);
1211#ifndef NDEBUG
1212 N->PersistentId = NextPersistentId++;
1213 verifyNode(N);
1214#endif
1215 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1216 DUL->NodeInserted(N);
1217}
1218
1219/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1220/// correspond to it. This is useful when we're about to delete or repurpose
1221/// the node. We don't want future request for structurally identical nodes
1222/// to return N anymore.
1223bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1224 bool Erased = false;
1225 switch (N->getOpcode()) {
1226 case ISD::HANDLENODE: return false; // noop.
1227 case ISD::CONDCODE:
1228 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1229 "Cond code doesn't exist!");
1230 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1231 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1232 break;
1234 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1235 break;
1237 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1238 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1239 ESN->getSymbol(), ESN->getTargetFlags()));
1240 break;
1241 }
1242 case ISD::MCSymbol: {
1243 auto *MCSN = cast<MCSymbolSDNode>(N);
1244 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1245 break;
1246 }
1247 case ISD::VALUETYPE: {
1248 EVT VT = cast<VTSDNode>(N)->getVT();
1249 if (VT.isExtended()) {
1250 Erased = ExtendedValueTypeNodes.erase(VT);
1251 } else {
1252 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1253 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1254 }
1255 break;
1256 }
1257 default:
1258 // Remove it from the CSE Map.
1259 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1260 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1261 Erased = CSEMap.RemoveNode(N);
1262 break;
1263 }
1264#ifndef NDEBUG
1265 // Verify that the node was actually in one of the CSE maps, unless it has a
1266 // glue result (which cannot be CSE'd) or is one of the special cases that are
1267 // not subject to CSE.
1268 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1269 !N->isMachineOpcode() && !doNotCSE(N)) {
1270 N->dump(this);
1271 dbgs() << "\n";
1272 llvm_unreachable("Node is not in map!");
1273 }
1274#endif
1275 return Erased;
1276}
1277
1278/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1279/// maps and modified in place. Add it back to the CSE maps, unless an identical
1280/// node already exists, in which case transfer all its users to the existing
1281/// node. This transfer can potentially trigger recursive merging.
1282void
1283SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1284 // For node types that aren't CSE'd, just act as if no identical node
1285 // already exists.
1286 if (!doNotCSE(N)) {
1287 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1288 if (Existing != N) {
1289 // If there was already an existing matching node, use ReplaceAllUsesWith
1290 // to replace the dead one with the existing one. This can cause
1291 // recursive merging of other unrelated nodes down the line.
1292 Existing->intersectFlagsWith(N->getFlags());
1293 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1294 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1295 ReplaceAllUsesWith(N, Existing);
1296
1297 // N is now dead. Inform the listeners and delete it.
1298 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1299 DUL->NodeDeleted(N, Existing);
1300 DeleteNodeNotInCSEMaps(N);
1301 return;
1302 }
1303 }
1304
1305 // If the node doesn't already exist, we updated it. Inform listeners.
1306 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1307 DUL->NodeUpdated(N);
1308}
1309
1310/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1311/// were replaced with those specified. If this node is never memoized,
1312/// return null, otherwise return a pointer to the slot it would take. If a
1313/// node already exists with these operands, the slot will be non-null.
1314SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1315 void *&InsertPos) {
1316 if (doNotCSE(N))
1317 return nullptr;
1318
1319 SDValue Ops[] = { Op };
1320 FoldingSetNodeID ID;
1321 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1323 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1324 if (Node)
1325 Node->intersectFlagsWith(N->getFlags());
1326 return Node;
1327}
1328
1329/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1330/// were replaced with those specified. If this node is never memoized,
1331/// return null, otherwise return a pointer to the slot it would take. If a
1332/// node already exists with these operands, the slot will be non-null.
1333SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1334 SDValue Op1, SDValue Op2,
1335 void *&InsertPos) {
1336 if (doNotCSE(N))
1337 return nullptr;
1338
1339 SDValue Ops[] = { Op1, Op2 };
1340 FoldingSetNodeID ID;
1341 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1343 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1344 if (Node)
1345 Node->intersectFlagsWith(N->getFlags());
1346 return Node;
1347}
1348
1349/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1350/// were replaced with those specified. If this node is never memoized,
1351/// return null, otherwise return a pointer to the slot it would take. If a
1352/// node already exists with these operands, the slot will be non-null.
1353SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1354 void *&InsertPos) {
1355 if (doNotCSE(N))
1356 return nullptr;
1357
1358 FoldingSetNodeID ID;
1359 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1361 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1362 if (Node)
1363 Node->intersectFlagsWith(N->getFlags());
1364 return Node;
1365}
1366
1368 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1369 : VT.getTypeForEVT(*getContext());
1370
1371 return getDataLayout().getABITypeAlign(Ty);
1372}
1373
1374// EntryNode could meaningfully have debug info if we can find it...
1376 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1377 getVTList(MVT::Other, MVT::Glue)),
1378 Root(getEntryNode()) {
1379 InsertNode(&EntryNode);
1380 DbgInfo = new SDDbgInfo();
1381}
1382
1384 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1385 const TargetLibraryInfo *LibraryInfo,
1386 const LibcallLoweringInfo *LibcallsInfo,
1387 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1389 FunctionVarLocs const *VarLocs) {
1390 MF = &NewMF;
1391 SDAGISelPass = PassPtr;
1392 ORE = &NewORE;
1395 LibInfo = LibraryInfo;
1396 Libcalls = LibcallsInfo;
1397 Context = &MF->getFunction().getContext();
1398 UA = NewUA;
1399 PSI = PSIin;
1400 BFI = BFIin;
1401 MMI = &MMIin;
1402 FnVarLocs = VarLocs;
1403}
1404
1406 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1407 allnodes_clear();
1408 OperandRecycler.clear(OperandAllocator);
1409 delete DbgInfo;
1410}
1411
1413 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1414}
1415
1416void SelectionDAG::allnodes_clear() {
1417 assert(&*AllNodes.begin() == &EntryNode);
1418 AllNodes.remove(AllNodes.begin());
1419 while (!AllNodes.empty())
1420 DeallocateNode(&AllNodes.front());
1421#ifndef NDEBUG
1422 NextPersistentId = 0;
1423#endif
1424}
1425
1426SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1427 void *&InsertPos) {
1428 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1429 if (N) {
1430 switch (N->getOpcode()) {
1431 default: break;
1432 case ISD::Constant:
1433 case ISD::ConstantFP:
1434 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1435 "debug location. Use another overload.");
1436 }
1437 }
1438 return N;
1439}
1440
1441SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1442 const SDLoc &DL, void *&InsertPos) {
1443 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1444 if (N) {
1445 switch (N->getOpcode()) {
1446 case ISD::Constant:
1447 case ISD::ConstantFP:
1448 // Erase debug location from the node if the node is used at several
1449 // different places. Do not propagate one location to all uses as it
1450 // will cause a worse single stepping debugging experience.
1451 if (N->getDebugLoc() != DL.getDebugLoc())
1452 N->setDebugLoc(DebugLoc());
1453 break;
1454 default:
1455 // When the node's point of use is located earlier in the instruction
1456 // sequence than its prior point of use, update its debug info to the
1457 // earlier location.
1458 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1459 N->setDebugLoc(DL.getDebugLoc());
1460 break;
1461 }
1462 }
1463 return N;
1464}
1465
1467 allnodes_clear();
1468 OperandRecycler.clear(OperandAllocator);
1469 OperandAllocator.Reset();
1470 CSEMap.clear();
1471
1472 ExtendedValueTypeNodes.clear();
1473 ExternalSymbols.clear();
1474 TargetExternalSymbols.clear();
1475 MCSymbols.clear();
1476 SDEI.clear();
1477 llvm::fill(CondCodeNodes, nullptr);
1478 llvm::fill(ValueTypeNodes, nullptr);
1479
1480 EntryNode.UseList = nullptr;
1481 InsertNode(&EntryNode);
1482 Root = getEntryNode();
1483 DbgInfo->clear();
1484}
1485
1487 return VT.bitsGT(Op.getValueType())
1488 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1489 : getNode(ISD::FP_ROUND, DL, VT, Op,
1490 getIntPtrConstant(0, DL, /*isTarget=*/true));
1491}
1492
1493std::pair<SDValue, SDValue>
1495 const SDLoc &DL, EVT VT) {
1496 assert(!VT.bitsEq(Op.getValueType()) &&
1497 "Strict no-op FP extend/round not allowed.");
1498 SDValue Res =
1499 VT.bitsGT(Op.getValueType())
1500 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1501 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1502 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1503
1504 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1505}
1506
1508 return VT.bitsGT(Op.getValueType()) ?
1509 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1510 getNode(ISD::TRUNCATE, DL, VT, Op);
1511}
1512
1514 return VT.bitsGT(Op.getValueType()) ?
1515 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1516 getNode(ISD::TRUNCATE, DL, VT, Op);
1517}
1518
1520 return VT.bitsGT(Op.getValueType()) ?
1521 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1522 getNode(ISD::TRUNCATE, DL, VT, Op);
1523}
1524
1526 EVT VT) {
1527 assert(!VT.isVector());
1528 auto Type = Op.getValueType();
1529 SDValue DestOp;
1530 if (Type == VT)
1531 return Op;
1532 auto Size = Op.getValueSizeInBits();
1533 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1534 if (DestOp.getValueType() == VT)
1535 return DestOp;
1536
1537 return getAnyExtOrTrunc(DestOp, DL, VT);
1538}
1539
1541 EVT VT) {
1542 assert(!VT.isVector());
1543 auto Type = Op.getValueType();
1544 SDValue DestOp;
1545 if (Type == VT)
1546 return Op;
1547 auto Size = Op.getValueSizeInBits();
1548 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1549 if (DestOp.getValueType() == VT)
1550 return DestOp;
1551
1552 return getSExtOrTrunc(DestOp, DL, VT);
1553}
1554
1556 EVT VT) {
1557 assert(!VT.isVector());
1558 auto Type = Op.getValueType();
1559 SDValue DestOp;
1560 if (Type == VT)
1561 return Op;
1562 auto Size = Op.getValueSizeInBits();
1563 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1564 if (DestOp.getValueType() == VT)
1565 return DestOp;
1566
1567 return getZExtOrTrunc(DestOp, DL, VT);
1568}
1569
1571 EVT OpVT) {
1572 if (VT.bitsLE(Op.getValueType()))
1573 return getNode(ISD::TRUNCATE, SL, VT, Op);
1574
1575 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1576 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1577}
1578
1580 EVT OpVT = Op.getValueType();
1581 assert(VT.isInteger() && OpVT.isInteger() &&
1582 "Cannot getZeroExtendInReg FP types");
1583 assert(VT.isVector() == OpVT.isVector() &&
1584 "getZeroExtendInReg type should be vector iff the operand "
1585 "type is vector!");
1586 assert((!VT.isVector() ||
1588 "Vector element counts must match in getZeroExtendInReg");
1589 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1590 if (OpVT == VT)
1591 return Op;
1592 // TODO: Use computeKnownBits instead of AssertZext.
1593 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1594 ->getVT()
1595 .getScalarType()
1596 .bitsLE(VT.getScalarType()))
1597 return Op;
1599 VT.getScalarSizeInBits());
1600 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1601}
1602
1604 SDValue EVL, const SDLoc &DL,
1605 EVT VT) {
1606 EVT OpVT = Op.getValueType();
1607 assert(VT.isInteger() && OpVT.isInteger() &&
1608 "Cannot getVPZeroExtendInReg FP types");
1609 assert(VT.isVector() && OpVT.isVector() &&
1610 "getVPZeroExtendInReg type and operand type should be vector!");
1612 "Vector element counts must match in getZeroExtendInReg");
1613 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1614 if (OpVT == VT)
1615 return Op;
1617 VT.getScalarSizeInBits());
1618 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1619 EVL);
1620}
1621
1623 // Only unsigned pointer semantics are supported right now. In the future this
1624 // might delegate to TLI to check pointer signedness.
1625 return getZExtOrTrunc(Op, DL, VT);
1626}
1627
1629 // Only unsigned pointer semantics are supported right now. In the future this
1630 // might delegate to TLI to check pointer signedness.
1631 return getZeroExtendInReg(Op, DL, VT);
1632}
1633
1635 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1636}
1637
1638/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1640 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1641}
1642
1644 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1645 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1646}
1647
1649 SDValue Mask, SDValue EVL, EVT VT) {
1650 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1651 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1652}
1653
1655 SDValue Mask, SDValue EVL) {
1656 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1657}
1658
1660 SDValue Mask, SDValue EVL) {
1661 if (VT.bitsGT(Op.getValueType()))
1662 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1663 if (VT.bitsLT(Op.getValueType()))
1664 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1665 return Op;
1666}
1667
1669 EVT OpVT) {
1670 if (!V)
1671 return getConstant(0, DL, VT);
1672
1673 switch (TLI->getBooleanContents(OpVT)) {
1676 return getConstant(1, DL, VT);
1678 return getAllOnesConstant(DL, VT);
1679 }
1680 llvm_unreachable("Unexpected boolean content enum!");
1681}
1682
1684 bool isT, bool isO) {
1685 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1686 DL, VT, isT, isO);
1687}
1688
1690 bool isT, bool isO) {
1691 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1692}
1693
1695 EVT VT, bool isT, bool isO) {
1696 assert(VT.isInteger() && "Cannot create FP integer constant!");
1697
1698 EVT EltVT = VT.getScalarType();
1699 const ConstantInt *Elt = &Val;
1700
1701 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1702 // to-be-splatted scalar ConstantInt.
1703 if (isa<VectorType>(Elt->getType()))
1704 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1705
1706 // In some cases the vector type is legal but the element type is illegal and
1707 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1708 // inserted value (the type does not need to match the vector element type).
1709 // Any extra bits introduced will be truncated away.
1710 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1712 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1713 APInt NewVal;
1714 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1715 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1716 else
1717 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1718 Elt = ConstantInt::get(*getContext(), NewVal);
1719 }
1720 // In other cases the element type is illegal and needs to be expanded, for
1721 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1722 // the value into n parts and use a vector type with n-times the elements.
1723 // Then bitcast to the type requested.
1724 // Legalizing constants too early makes the DAGCombiner's job harder so we
1725 // only legalize if the DAG tells us we must produce legal types.
1726 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1727 TLI->getTypeAction(*getContext(), EltVT) ==
1729 const APInt &NewVal = Elt->getValue();
1730 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1731 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1732
1733 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1734 if (VT.isScalableVector() ||
1735 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1736 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1737 "Can only handle an even split!");
1738 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1739
1740 SmallVector<SDValue, 2> ScalarParts;
1741 for (unsigned i = 0; i != Parts; ++i)
1742 ScalarParts.push_back(getConstant(
1743 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1744 ViaEltVT, isT, isO));
1745
1746 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1747 }
1748
1749 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1750 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1751
1752 // Check the temporary vector is the correct size. If this fails then
1753 // getTypeToTransformTo() probably returned a type whose size (in bits)
1754 // isn't a power-of-2 factor of the requested type size.
1755 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1756
1757 SmallVector<SDValue, 2> EltParts;
1758 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1759 EltParts.push_back(getConstant(
1760 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1761 ViaEltVT, isT, isO));
1762
1763 // EltParts is currently in little endian order. If we actually want
1764 // big-endian order then reverse it now.
1765 if (getDataLayout().isBigEndian())
1766 std::reverse(EltParts.begin(), EltParts.end());
1767
1768 // The elements must be reversed when the element order is different
1769 // to the endianness of the elements (because the BITCAST is itself a
1770 // vector shuffle in this situation). However, we do not need any code to
1771 // perform this reversal because getConstant() is producing a vector
1772 // splat.
1773 // This situation occurs in MIPS MSA.
1774
1776 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1777 llvm::append_range(Ops, EltParts);
1778
1779 SDValue V =
1780 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1781 return V;
1782 }
1783
1784 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1785 "APInt size does not match type size!");
1786 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1787 SDVTList VTs = getVTList(EltVT);
1789 AddNodeIDNode(ID, Opc, VTs, {});
1790 ID.AddPointer(Elt);
1791 ID.AddBoolean(isO);
1792 void *IP = nullptr;
1793 SDNode *N = nullptr;
1794 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1795 if (!VT.isVector())
1796 return SDValue(N, 0);
1797
1798 if (!N) {
1799 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1800 CSEMap.InsertNode(N, IP);
1801 InsertNode(N);
1802 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1803 }
1804
1805 SDValue Result(N, 0);
1806 if (VT.isVector())
1807 Result = getSplat(VT, DL, Result);
1808 return Result;
1809}
1810
1812 bool isT, bool isO) {
1813 unsigned Size = VT.getScalarSizeInBits();
1814 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1815}
1816
1818 bool IsOpaque) {
1820 IsTarget, IsOpaque);
1821}
1822
1824 bool isTarget) {
1825 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1826}
1827
1829 const SDLoc &DL) {
1830 assert(VT.isInteger() && "Shift amount is not an integer type!");
1831 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1832 return getConstant(Val, DL, ShiftVT);
1833}
1834
1836 const SDLoc &DL) {
1837 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1838 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1839}
1840
1842 bool isTarget) {
1843 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1844}
1845
1847 bool isTarget) {
1848 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1849}
1850
1852 EVT VT, bool isTarget) {
1853 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1854
1855 EVT EltVT = VT.getScalarType();
1856 const ConstantFP *Elt = &V;
1857
1858 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1859 // the to-be-splatted scalar ConstantFP.
1860 if (isa<VectorType>(Elt->getType()))
1861 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1862
1863 // Do the map lookup using the actual bit pattern for the floating point
1864 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1865 // we don't have issues with SNANs.
1866 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1867 SDVTList VTs = getVTList(EltVT);
1869 AddNodeIDNode(ID, Opc, VTs, {});
1870 ID.AddPointer(Elt);
1871 void *IP = nullptr;
1872 SDNode *N = nullptr;
1873 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1874 if (!VT.isVector())
1875 return SDValue(N, 0);
1876
1877 if (!N) {
1878 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1879 CSEMap.InsertNode(N, IP);
1880 InsertNode(N);
1881 }
1882
1883 SDValue Result(N, 0);
1884 if (VT.isVector())
1885 Result = getSplat(VT, DL, Result);
1886 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1887 return Result;
1888}
1889
1891 bool isTarget) {
1892 EVT EltVT = VT.getScalarType();
1893 if (EltVT == MVT::f32)
1894 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1895 if (EltVT == MVT::f64)
1896 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1897 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1898 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1899 bool Ignored;
1900 APFloat APF = APFloat(Val);
1902 &Ignored);
1903 return getConstantFP(APF, DL, VT, isTarget);
1904 }
1905 llvm_unreachable("Unsupported type in getConstantFP");
1906}
1907
1909 EVT VT, int64_t Offset, bool isTargetGA,
1910 unsigned TargetFlags) {
1911 assert((TargetFlags == 0 || isTargetGA) &&
1912 "Cannot set target flags on target-independent globals");
1913
1914 // Truncate (with sign-extension) the offset value to the pointer size.
1916 if (BitWidth < 64)
1918
1919 unsigned Opc;
1920 if (GV->isThreadLocal())
1922 else
1924
1925 SDVTList VTs = getVTList(VT);
1927 AddNodeIDNode(ID, Opc, VTs, {});
1928 ID.AddPointer(GV);
1929 ID.AddInteger(Offset);
1930 ID.AddInteger(TargetFlags);
1931 void *IP = nullptr;
1932 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1933 return SDValue(E, 0);
1934
1935 auto *N = newSDNode<GlobalAddressSDNode>(
1936 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1937 CSEMap.InsertNode(N, IP);
1938 InsertNode(N);
1939 return SDValue(N, 0);
1940}
1941
1943 SDVTList VTs = getVTList(MVT::Untyped);
1946 ID.AddPointer(GV);
1947 void *IP = nullptr;
1948 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1949 return SDValue(E, 0);
1950
1951 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1952 CSEMap.InsertNode(N, IP);
1953 InsertNode(N);
1954 return SDValue(N, 0);
1955}
1956
1957SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1958 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1959 SDVTList VTs = getVTList(VT);
1961 AddNodeIDNode(ID, Opc, VTs, {});
1962 ID.AddInteger(FI);
1963 void *IP = nullptr;
1964 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1965 return SDValue(E, 0);
1966
1967 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1968 CSEMap.InsertNode(N, IP);
1969 InsertNode(N);
1970 return SDValue(N, 0);
1971}
1972
1973SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1974 unsigned TargetFlags) {
1975 assert((TargetFlags == 0 || isTarget) &&
1976 "Cannot set target flags on target-independent jump tables");
1977 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1978 SDVTList VTs = getVTList(VT);
1980 AddNodeIDNode(ID, Opc, VTs, {});
1981 ID.AddInteger(JTI);
1982 ID.AddInteger(TargetFlags);
1983 void *IP = nullptr;
1984 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1985 return SDValue(E, 0);
1986
1987 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1988 CSEMap.InsertNode(N, IP);
1989 InsertNode(N);
1990 return SDValue(N, 0);
1991}
1992
1994 const SDLoc &DL) {
1996 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
1997 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1998}
1999
2001 MaybeAlign Alignment, int Offset,
2002 bool isTarget, unsigned TargetFlags) {
2003 assert((TargetFlags == 0 || isTarget) &&
2004 "Cannot set target flags on target-independent globals");
2005 if (!Alignment)
2006 Alignment = shouldOptForSize()
2007 ? getDataLayout().getABITypeAlign(C->getType())
2008 : getDataLayout().getPrefTypeAlign(C->getType());
2009 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2010 SDVTList VTs = getVTList(VT);
2012 AddNodeIDNode(ID, Opc, VTs, {});
2013 ID.AddInteger(Alignment->value());
2014 ID.AddInteger(Offset);
2015 ID.AddPointer(C);
2016 ID.AddInteger(TargetFlags);
2017 void *IP = nullptr;
2018 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2019 return SDValue(E, 0);
2020
2021 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2022 TargetFlags);
2023 CSEMap.InsertNode(N, IP);
2024 InsertNode(N);
2025 SDValue V = SDValue(N, 0);
2026 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2027 return V;
2028}
2029
2031 MaybeAlign Alignment, int Offset,
2032 bool isTarget, unsigned TargetFlags) {
2033 assert((TargetFlags == 0 || isTarget) &&
2034 "Cannot set target flags on target-independent globals");
2035 if (!Alignment)
2036 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2037 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2038 SDVTList VTs = getVTList(VT);
2040 AddNodeIDNode(ID, Opc, VTs, {});
2041 ID.AddInteger(Alignment->value());
2042 ID.AddInteger(Offset);
2043 C->addSelectionDAGCSEId(ID);
2044 ID.AddInteger(TargetFlags);
2045 void *IP = nullptr;
2046 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2047 return SDValue(E, 0);
2048
2049 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2050 TargetFlags);
2051 CSEMap.InsertNode(N, IP);
2052 InsertNode(N);
2053 return SDValue(N, 0);
2054}
2055
2058 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2059 ID.AddPointer(MBB);
2060 void *IP = nullptr;
2061 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2062 return SDValue(E, 0);
2063
2064 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2065 CSEMap.InsertNode(N, IP);
2066 InsertNode(N);
2067 return SDValue(N, 0);
2068}
2069
2071 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2072 ValueTypeNodes.size())
2073 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2074
2075 SDNode *&N = VT.isExtended() ?
2076 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2077
2078 if (N) return SDValue(N, 0);
2079 N = newSDNode<VTSDNode>(VT);
2080 InsertNode(N);
2081 return SDValue(N, 0);
2082}
2083
2085 SDNode *&N = ExternalSymbols[Sym];
2086 if (N) return SDValue(N, 0);
2087 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2088 InsertNode(N);
2089 return SDValue(N, 0);
2090}
2091
2092SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2094 return getExternalSymbol(SymName.data(), VT);
2095}
2096
2098 SDNode *&N = MCSymbols[Sym];
2099 if (N)
2100 return SDValue(N, 0);
2101 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2102 InsertNode(N);
2103 return SDValue(N, 0);
2104}
2105
2107 unsigned TargetFlags) {
2108 SDNode *&N =
2109 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2110 if (N) return SDValue(N, 0);
2111 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2112 InsertNode(N);
2113 return SDValue(N, 0);
2114}
2115
2117 EVT VT, unsigned TargetFlags) {
2119 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2120}
2121
2123 if ((unsigned)Cond >= CondCodeNodes.size())
2124 CondCodeNodes.resize(Cond+1);
2125
2126 if (!CondCodeNodes[Cond]) {
2127 auto *N = newSDNode<CondCodeSDNode>(Cond);
2128 CondCodeNodes[Cond] = N;
2129 InsertNode(N);
2130 }
2131
2132 return SDValue(CondCodeNodes[Cond], 0);
2133}
2134
2136 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2137 "APInt size does not match type size!");
2138
2139 if (MulImm == 0)
2140 return getConstant(0, DL, VT);
2141
2142 const MachineFunction &MF = getMachineFunction();
2143 const Function &F = MF.getFunction();
2144 ConstantRange CR = getVScaleRange(&F, 64);
2145 if (const APInt *C = CR.getSingleElement())
2146 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2147
2148 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2149}
2150
2151/// \returns a value of type \p VT that represents the runtime value of \p
2152/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2153/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2154/// or TypeSize.
2155template <typename Ty>
2157 EVT VT, Ty Quantity) {
2158 if (Quantity.isScalable())
2159 return DAG.getVScale(
2160 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2161
2162 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2163}
2164
2166 ElementCount EC) {
2167 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2168}
2169
2171 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2172}
2173
2175 ElementCount EC) {
2176 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2177 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2178 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2179 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2180}
2181
2183 APInt One(ResVT.getScalarSizeInBits(), 1);
2184 return getStepVector(DL, ResVT, One);
2185}
2186
2188 const APInt &StepVal) {
2189 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2190 if (ResVT.isScalableVector())
2191 return getNode(
2192 ISD::STEP_VECTOR, DL, ResVT,
2193 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2194
2195 SmallVector<SDValue, 16> OpsStepConstants;
2196 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2197 OpsStepConstants.push_back(
2198 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2199 return getBuildVector(ResVT, DL, OpsStepConstants);
2200}
2201
2202/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2203/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2208
2210 SDValue N2, ArrayRef<int> Mask) {
2211 assert(VT.getVectorNumElements() == Mask.size() &&
2212 "Must have the same number of vector elements as mask elements!");
2213 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2214 "Invalid VECTOR_SHUFFLE");
2215
2216 // Canonicalize shuffle undef, undef -> undef
2217 if (N1.isUndef() && N2.isUndef())
2218 return getUNDEF(VT);
2219
2220 // Validate that all indices in Mask are within the range of the elements
2221 // input to the shuffle.
2222 int NElts = Mask.size();
2223 assert(llvm::all_of(Mask,
2224 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2225 "Index out of range");
2226
2227 // Copy the mask so we can do any needed cleanup.
2228 SmallVector<int, 8> MaskVec(Mask);
2229
2230 // Canonicalize shuffle v, v -> v, undef
2231 if (N1 == N2) {
2232 N2 = getUNDEF(VT);
2233 for (int i = 0; i != NElts; ++i)
2234 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2235 }
2236
2237 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2238 if (N1.isUndef())
2239 commuteShuffle(N1, N2, MaskVec);
2240
2241 if (TLI->hasVectorBlend()) {
2242 // If shuffling a splat, try to blend the splat instead. We do this here so
2243 // that even when this arises during lowering we don't have to re-handle it.
2244 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2245 BitVector UndefElements;
2246 SDValue Splat = BV->getSplatValue(&UndefElements);
2247 if (!Splat)
2248 return;
2249
2250 for (int i = 0; i < NElts; ++i) {
2251 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2252 continue;
2253
2254 // If this input comes from undef, mark it as such.
2255 if (UndefElements[MaskVec[i] - Offset]) {
2256 MaskVec[i] = -1;
2257 continue;
2258 }
2259
2260 // If we can blend a non-undef lane, use that instead.
2261 if (!UndefElements[i])
2262 MaskVec[i] = i + Offset;
2263 }
2264 };
2265 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2266 BlendSplat(N1BV, 0);
2267 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2268 BlendSplat(N2BV, NElts);
2269 }
2270
2271 // Canonicalize all index into lhs, -> shuffle lhs, undef
2272 // Canonicalize all index into rhs, -> shuffle rhs, undef
2273 bool AllLHS = true, AllRHS = true;
2274 bool N2Undef = N2.isUndef();
2275 for (int i = 0; i != NElts; ++i) {
2276 if (MaskVec[i] >= NElts) {
2277 if (N2Undef)
2278 MaskVec[i] = -1;
2279 else
2280 AllLHS = false;
2281 } else if (MaskVec[i] >= 0) {
2282 AllRHS = false;
2283 }
2284 }
2285 if (AllLHS && AllRHS)
2286 return getUNDEF(VT);
2287 if (AllLHS && !N2Undef)
2288 N2 = getUNDEF(VT);
2289 if (AllRHS) {
2290 N1 = getUNDEF(VT);
2291 commuteShuffle(N1, N2, MaskVec);
2292 }
2293 // Reset our undef status after accounting for the mask.
2294 N2Undef = N2.isUndef();
2295 // Re-check whether both sides ended up undef.
2296 if (N1.isUndef() && N2Undef)
2297 return getUNDEF(VT);
2298
2299 // If Identity shuffle return that node.
2300 bool Identity = true, AllSame = true;
2301 for (int i = 0; i != NElts; ++i) {
2302 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2303 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2304 }
2305 if (Identity && NElts)
2306 return N1;
2307
2308 // Shuffling a constant splat doesn't change the result.
2309 if (N2Undef) {
2310 SDValue V = N1;
2311
2312 // Look through any bitcasts. We check that these don't change the number
2313 // (and size) of elements and just changes their types.
2314 while (V.getOpcode() == ISD::BITCAST)
2315 V = V->getOperand(0);
2316
2317 // A splat should always show up as a build vector node.
2318 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2319 BitVector UndefElements;
2320 SDValue Splat = BV->getSplatValue(&UndefElements);
2321 // If this is a splat of an undef, shuffling it is also undef.
2322 if (Splat && Splat.isUndef())
2323 return getUNDEF(VT);
2324
2325 bool SameNumElts =
2326 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2327
2328 // We only have a splat which can skip shuffles if there is a splatted
2329 // value and no undef lanes rearranged by the shuffle.
2330 if (Splat && UndefElements.none()) {
2331 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2332 // number of elements match or the value splatted is a zero constant.
2333 if (SameNumElts || isNullConstant(Splat))
2334 return N1;
2335 }
2336
2337 // If the shuffle itself creates a splat, build the vector directly.
2338 if (AllSame && SameNumElts) {
2339 EVT BuildVT = BV->getValueType(0);
2340 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2341 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2342
2343 // We may have jumped through bitcasts, so the type of the
2344 // BUILD_VECTOR may not match the type of the shuffle.
2345 if (BuildVT != VT)
2346 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2347 return NewBV;
2348 }
2349 }
2350 }
2351
2352 SDVTList VTs = getVTList(VT);
2354 SDValue Ops[2] = { N1, N2 };
2356 for (int i = 0; i != NElts; ++i)
2357 ID.AddInteger(MaskVec[i]);
2358
2359 void* IP = nullptr;
2360 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2361 return SDValue(E, 0);
2362
2363 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2364 // SDNode doesn't have access to it. This memory will be "leaked" when
2365 // the node is deallocated, but recovered when the NodeAllocator is released.
2366 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2367 llvm::copy(MaskVec, MaskAlloc);
2368
2369 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2370 dl.getDebugLoc(), MaskAlloc);
2371 createOperands(N, Ops);
2372
2373 CSEMap.InsertNode(N, IP);
2374 InsertNode(N);
2375 SDValue V = SDValue(N, 0);
2376 NewSDValueDbgMsg(V, "Creating new node: ", this);
2377 return V;
2378}
2379
2381 EVT VT = SV.getValueType(0);
2382 SmallVector<int, 8> MaskVec(SV.getMask());
2384
2385 SDValue Op0 = SV.getOperand(0);
2386 SDValue Op1 = SV.getOperand(1);
2387 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2388}
2389
2391 SDVTList VTs = getVTList(VT);
2393 AddNodeIDNode(ID, ISD::Register, VTs, {});
2394 ID.AddInteger(Reg.id());
2395 void *IP = nullptr;
2396 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2397 return SDValue(E, 0);
2398
2399 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2400 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2401 CSEMap.InsertNode(N, IP);
2402 InsertNode(N);
2403 return SDValue(N, 0);
2404}
2405
2408 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2409 ID.AddPointer(RegMask);
2410 void *IP = nullptr;
2411 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2412 return SDValue(E, 0);
2413
2414 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2415 CSEMap.InsertNode(N, IP);
2416 InsertNode(N);
2417 return SDValue(N, 0);
2418}
2419
2421 MCSymbol *Label) {
2422 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2423}
2424
2425SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2426 SDValue Root, MCSymbol *Label) {
2428 SDValue Ops[] = { Root };
2429 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2430 ID.AddPointer(Label);
2431 void *IP = nullptr;
2432 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2433 return SDValue(E, 0);
2434
2435 auto *N =
2436 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2437 createOperands(N, Ops);
2438
2439 CSEMap.InsertNode(N, IP);
2440 InsertNode(N);
2441 return SDValue(N, 0);
2442}
2443
2445 int64_t Offset, bool isTarget,
2446 unsigned TargetFlags) {
2447 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2448 SDVTList VTs = getVTList(VT);
2449
2451 AddNodeIDNode(ID, Opc, VTs, {});
2452 ID.AddPointer(BA);
2453 ID.AddInteger(Offset);
2454 ID.AddInteger(TargetFlags);
2455 void *IP = nullptr;
2456 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2457 return SDValue(E, 0);
2458
2459 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2460 CSEMap.InsertNode(N, IP);
2461 InsertNode(N);
2462 return SDValue(N, 0);
2463}
2464
2467 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2468 ID.AddPointer(V);
2469
2470 void *IP = nullptr;
2471 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2472 return SDValue(E, 0);
2473
2474 auto *N = newSDNode<SrcValueSDNode>(V);
2475 CSEMap.InsertNode(N, IP);
2476 InsertNode(N);
2477 return SDValue(N, 0);
2478}
2479
2482 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2483 ID.AddPointer(MD);
2484
2485 void *IP = nullptr;
2486 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2487 return SDValue(E, 0);
2488
2489 auto *N = newSDNode<MDNodeSDNode>(MD);
2490 CSEMap.InsertNode(N, IP);
2491 InsertNode(N);
2492 return SDValue(N, 0);
2493}
2494
2496 if (VT == V.getValueType())
2497 return V;
2498
2499 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2500}
2501
2503 unsigned SrcAS, unsigned DestAS) {
2504 SDVTList VTs = getVTList(VT);
2505 SDValue Ops[] = {Ptr};
2508 ID.AddInteger(SrcAS);
2509 ID.AddInteger(DestAS);
2510
2511 void *IP = nullptr;
2512 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2513 return SDValue(E, 0);
2514
2515 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2516 VTs, SrcAS, DestAS);
2517 createOperands(N, Ops);
2518
2519 CSEMap.InsertNode(N, IP);
2520 InsertNode(N);
2521 return SDValue(N, 0);
2522}
2523
2525 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2526}
2527
2528/// getShiftAmountOperand - Return the specified value casted to
2529/// the target's desired shift amount type.
2531 EVT OpTy = Op.getValueType();
2532 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2533 if (OpTy == ShTy || OpTy.isVector()) return Op;
2534
2535 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2536}
2537
2539 SDLoc dl(Node);
2541 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2542 EVT VT = Node->getValueType(0);
2543 SDValue Tmp1 = Node->getOperand(0);
2544 SDValue Tmp2 = Node->getOperand(1);
2545 const MaybeAlign MA(Node->getConstantOperandVal(3));
2546
2547 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2548 Tmp2, MachinePointerInfo(V));
2549 SDValue VAList = VAListLoad;
2550
2551 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2552 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2553 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2554
2555 VAList = getNode(
2556 ISD::AND, dl, VAList.getValueType(), VAList,
2557 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2558 }
2559
2560 // Increment the pointer, VAList, to the next vaarg
2561 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2562 getConstant(getDataLayout().getTypeAllocSize(
2563 VT.getTypeForEVT(*getContext())),
2564 dl, VAList.getValueType()));
2565 // Store the incremented VAList to the legalized pointer
2566 Tmp1 =
2567 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2568 // Load the actual argument out of the pointer VAList
2569 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2570}
2571
2573 SDLoc dl(Node);
2575 // This defaults to loading a pointer from the input and storing it to the
2576 // output, returning the chain.
2577 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2578 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2579 SDValue Tmp1 =
2580 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2581 Node->getOperand(2), MachinePointerInfo(VS));
2582 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2583 MachinePointerInfo(VD));
2584}
2585
2587 const DataLayout &DL = getDataLayout();
2588 Type *Ty = VT.getTypeForEVT(*getContext());
2589 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2590
2591 if (TLI->isTypeLegal(VT) || !VT.isVector())
2592 return RedAlign;
2593
2594 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2595 const Align StackAlign = TFI->getStackAlign();
2596
2597 // See if we can choose a smaller ABI alignment in cases where it's an
2598 // illegal vector type that will get broken down.
2599 if (RedAlign > StackAlign) {
2600 EVT IntermediateVT;
2601 MVT RegisterVT;
2602 unsigned NumIntermediates;
2603 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2604 NumIntermediates, RegisterVT);
2605 Ty = IntermediateVT.getTypeForEVT(*getContext());
2606 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2607 if (RedAlign2 < RedAlign)
2608 RedAlign = RedAlign2;
2609
2610 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2611 // If the stack is not realignable, the alignment should be limited to the
2612 // StackAlignment
2613 RedAlign = std::min(RedAlign, StackAlign);
2614 }
2615
2616 return RedAlign;
2617}
2618
2620 MachineFrameInfo &MFI = MF->getFrameInfo();
2621 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2622 int StackID = 0;
2623 if (Bytes.isScalable())
2624 StackID = TFI->getStackIDForScalableVectors();
2625 // The stack id gives an indication of whether the object is scalable or
2626 // not, so it's safe to pass in the minimum size here.
2627 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2628 false, nullptr, StackID);
2629 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2630}
2631
2633 Type *Ty = VT.getTypeForEVT(*getContext());
2634 Align StackAlign =
2635 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2636 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2637}
2638
2640 TypeSize VT1Size = VT1.getStoreSize();
2641 TypeSize VT2Size = VT2.getStoreSize();
2642 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2643 "Don't know how to choose the maximum size when creating a stack "
2644 "temporary");
2645 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2646 ? VT1Size
2647 : VT2Size;
2648
2649 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2650 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2651 const DataLayout &DL = getDataLayout();
2652 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2653 return CreateStackTemporary(Bytes, Align);
2654}
2655
2657 ISD::CondCode Cond, const SDLoc &dl) {
2658 EVT OpVT = N1.getValueType();
2659
2660 auto GetUndefBooleanConstant = [&]() {
2661 if (VT.getScalarType() == MVT::i1 ||
2662 TLI->getBooleanContents(OpVT) ==
2664 return getUNDEF(VT);
2665 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2666 // so we cannot use getUNDEF(). Return zero instead.
2667 return getConstant(0, dl, VT);
2668 };
2669
2670 // These setcc operations always fold.
2671 switch (Cond) {
2672 default: break;
2673 case ISD::SETFALSE:
2674 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2675 case ISD::SETTRUE:
2676 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2677
2678 case ISD::SETOEQ:
2679 case ISD::SETOGT:
2680 case ISD::SETOGE:
2681 case ISD::SETOLT:
2682 case ISD::SETOLE:
2683 case ISD::SETONE:
2684 case ISD::SETO:
2685 case ISD::SETUO:
2686 case ISD::SETUEQ:
2687 case ISD::SETUNE:
2688 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2689 break;
2690 }
2691
2692 if (OpVT.isInteger()) {
2693 // For EQ and NE, we can always pick a value for the undef to make the
2694 // predicate pass or fail, so we can return undef.
2695 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2696 // icmp eq/ne X, undef -> undef.
2697 if ((N1.isUndef() || N2.isUndef()) &&
2698 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2699 return GetUndefBooleanConstant();
2700
2701 // If both operands are undef, we can return undef for int comparison.
2702 // icmp undef, undef -> undef.
2703 if (N1.isUndef() && N2.isUndef())
2704 return GetUndefBooleanConstant();
2705
2706 // icmp X, X -> true/false
2707 // icmp X, undef -> true/false because undef could be X.
2708 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2709 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2710 }
2711
2713 const APInt &C2 = N2C->getAPIntValue();
2715 const APInt &C1 = N1C->getAPIntValue();
2716
2718 dl, VT, OpVT);
2719 }
2720 }
2721
2722 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2723 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2724
2725 if (N1CFP && N2CFP) {
2726 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2727 switch (Cond) {
2728 default: break;
2729 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2730 return GetUndefBooleanConstant();
2731 [[fallthrough]];
2732 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2733 OpVT);
2734 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2735 return GetUndefBooleanConstant();
2736 [[fallthrough]];
2738 R==APFloat::cmpLessThan, dl, VT,
2739 OpVT);
2740 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2741 return GetUndefBooleanConstant();
2742 [[fallthrough]];
2743 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2744 OpVT);
2745 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2746 return GetUndefBooleanConstant();
2747 [[fallthrough]];
2749 VT, OpVT);
2750 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2751 return GetUndefBooleanConstant();
2752 [[fallthrough]];
2754 R==APFloat::cmpEqual, dl, VT,
2755 OpVT);
2756 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2757 return GetUndefBooleanConstant();
2758 [[fallthrough]];
2760 R==APFloat::cmpEqual, dl, VT, OpVT);
2761 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2762 OpVT);
2763 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2764 OpVT);
2766 R==APFloat::cmpEqual, dl, VT,
2767 OpVT);
2768 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2769 OpVT);
2771 R==APFloat::cmpLessThan, dl, VT,
2772 OpVT);
2774 R==APFloat::cmpUnordered, dl, VT,
2775 OpVT);
2777 VT, OpVT);
2778 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2779 OpVT);
2780 }
2781 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2782 // Ensure that the constant occurs on the RHS.
2784 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2785 return SDValue();
2786 return getSetCC(dl, VT, N2, N1, SwappedCond);
2787 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2788 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2789 // If an operand is known to be a nan (or undef that could be a nan), we can
2790 // fold it.
2791 // Choosing NaN for the undef will always make unordered comparison succeed
2792 // and ordered comparison fails.
2793 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2794 switch (ISD::getUnorderedFlavor(Cond)) {
2795 default:
2796 llvm_unreachable("Unknown flavor!");
2797 case 0: // Known false.
2798 return getBoolConstant(false, dl, VT, OpVT);
2799 case 1: // Known true.
2800 return getBoolConstant(true, dl, VT, OpVT);
2801 case 2: // Undefined.
2802 return GetUndefBooleanConstant();
2803 }
2804 }
2805
2806 // Could not fold it.
2807 return SDValue();
2808}
2809
2810/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2811/// use this predicate to simplify operations downstream.
2813 unsigned BitWidth = Op.getScalarValueSizeInBits();
2815}
2816
2817// TODO: Should have argument to specify if sign bit of nan is ignorable.
2819 if (Depth >= MaxRecursionDepth)
2820 return false; // Limit search depth.
2821
2822 unsigned Opc = Op.getOpcode();
2823 switch (Opc) {
2824 case ISD::FABS:
2825 return true;
2826 case ISD::AssertNoFPClass: {
2827 FPClassTest NoFPClass =
2828 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2829
2830 const FPClassTest TestMask = fcNan | fcNegative;
2831 return (NoFPClass & TestMask) == TestMask;
2832 }
2833 case ISD::ARITH_FENCE:
2834 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2835 case ISD::FEXP:
2836 case ISD::FEXP2:
2837 case ISD::FEXP10:
2838 return Op->getFlags().hasNoNaNs();
2839 case ISD::FMINNUM:
2840 case ISD::FMINNUM_IEEE:
2841 case ISD::FMINIMUM:
2842 case ISD::FMINIMUMNUM:
2843 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2844 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2845 case ISD::FMAXNUM:
2846 case ISD::FMAXNUM_IEEE:
2847 case ISD::FMAXIMUM:
2848 case ISD::FMAXIMUMNUM:
2849 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2850 // is sufficient.
2851 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2852 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2853 default:
2854 return false;
2855 }
2856
2857 llvm_unreachable("covered opcode switch");
2858}
2859
2860/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2861/// this predicate to simplify operations downstream. Mask is known to be zero
2862/// for bits that V cannot have.
2864 unsigned Depth) const {
2865 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2866}
2867
2868/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2869/// DemandedElts. We use this predicate to simplify operations downstream.
2870/// Mask is known to be zero for bits that V cannot have.
2872 const APInt &DemandedElts,
2873 unsigned Depth) const {
2874 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2875}
2876
2877/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2878/// DemandedElts. We use this predicate to simplify operations downstream.
2880 unsigned Depth /* = 0 */) const {
2881 return computeKnownBits(V, DemandedElts, Depth).isZero();
2882}
2883
2884/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2886 unsigned Depth) const {
2887 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2888}
2889
2891 const APInt &DemandedElts,
2892 unsigned Depth) const {
2893 EVT VT = Op.getValueType();
2894 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2895
2896 unsigned NumElts = VT.getVectorNumElements();
2897 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2898
2899 APInt KnownZeroElements = APInt::getZero(NumElts);
2900 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2901 if (!DemandedElts[EltIdx])
2902 continue; // Don't query elements that are not demanded.
2903 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2904 if (MaskedVectorIsZero(Op, Mask, Depth))
2905 KnownZeroElements.setBit(EltIdx);
2906 }
2907 return KnownZeroElements;
2908}
2909
2910/// isSplatValue - Return true if the vector V has the same value
2911/// across all DemandedElts. For scalable vectors, we don't know the
2912/// number of lanes at compile time. Instead, we use a 1 bit APInt
2913/// to represent a conservative value for all lanes; that is, that
2914/// one bit value is implicitly splatted across all lanes.
2915bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2916 APInt &UndefElts, unsigned Depth) const {
2917 unsigned Opcode = V.getOpcode();
2918 EVT VT = V.getValueType();
2919 assert(VT.isVector() && "Vector type expected");
2920 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2921 "scalable demanded bits are ignored");
2922
2923 if (!DemandedElts)
2924 return false; // No demanded elts, better to assume we don't know anything.
2925
2926 if (Depth >= MaxRecursionDepth)
2927 return false; // Limit search depth.
2928
2929 // Deal with some common cases here that work for both fixed and scalable
2930 // vector types.
2931 switch (Opcode) {
2932 case ISD::SPLAT_VECTOR:
2933 UndefElts = V.getOperand(0).isUndef()
2934 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2935 : APInt(DemandedElts.getBitWidth(), 0);
2936 return true;
2937 case ISD::ADD:
2938 case ISD::SUB:
2939 case ISD::AND:
2940 case ISD::XOR:
2941 case ISD::OR: {
2942 APInt UndefLHS, UndefRHS;
2943 SDValue LHS = V.getOperand(0);
2944 SDValue RHS = V.getOperand(1);
2945 // Only recognize splats with the same demanded undef elements for both
2946 // operands, otherwise we might fail to handle binop-specific undef
2947 // handling.
2948 // e.g. (and undef, 0) -> 0 etc.
2949 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2950 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2951 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2952 UndefElts = UndefLHS | UndefRHS;
2953 return true;
2954 }
2955 return false;
2956 }
2957 case ISD::ABS:
2958 case ISD::TRUNCATE:
2959 case ISD::SIGN_EXTEND:
2960 case ISD::ZERO_EXTEND:
2961 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2962 default:
2963 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2964 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2965 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2966 Depth);
2967 break;
2968 }
2969
2970 // We don't support other cases than those above for scalable vectors at
2971 // the moment.
2972 if (VT.isScalableVector())
2973 return false;
2974
2975 unsigned NumElts = VT.getVectorNumElements();
2976 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2977 UndefElts = APInt::getZero(NumElts);
2978
2979 switch (Opcode) {
2980 case ISD::BUILD_VECTOR: {
2981 SDValue Scl;
2982 for (unsigned i = 0; i != NumElts; ++i) {
2983 SDValue Op = V.getOperand(i);
2984 if (Op.isUndef()) {
2985 UndefElts.setBit(i);
2986 continue;
2987 }
2988 if (!DemandedElts[i])
2989 continue;
2990 if (Scl && Scl != Op)
2991 return false;
2992 Scl = Op;
2993 }
2994 return true;
2995 }
2996 case ISD::VECTOR_SHUFFLE: {
2997 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2998 APInt DemandedLHS = APInt::getZero(NumElts);
2999 APInt DemandedRHS = APInt::getZero(NumElts);
3000 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3001 for (int i = 0; i != (int)NumElts; ++i) {
3002 int M = Mask[i];
3003 if (M < 0) {
3004 UndefElts.setBit(i);
3005 continue;
3006 }
3007 if (!DemandedElts[i])
3008 continue;
3009 if (M < (int)NumElts)
3010 DemandedLHS.setBit(M);
3011 else
3012 DemandedRHS.setBit(M - NumElts);
3013 }
3014
3015 // If we aren't demanding either op, assume there's no splat.
3016 // If we are demanding both ops, assume there's no splat.
3017 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3018 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3019 return false;
3020
3021 // See if the demanded elts of the source op is a splat or we only demand
3022 // one element, which should always be a splat.
3023 // TODO: Handle source ops splats with undefs.
3024 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3025 APInt SrcUndefs;
3026 return (SrcElts.popcount() == 1) ||
3027 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3028 (SrcElts & SrcUndefs).isZero());
3029 };
3030 if (!DemandedLHS.isZero())
3031 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3032 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3033 }
3035 // Offset the demanded elts by the subvector index.
3036 SDValue Src = V.getOperand(0);
3037 // We don't support scalable vectors at the moment.
3038 if (Src.getValueType().isScalableVector())
3039 return false;
3040 uint64_t Idx = V.getConstantOperandVal(1);
3041 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3042 APInt UndefSrcElts;
3043 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3044 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3045 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3046 return true;
3047 }
3048 break;
3049 }
3053 // Widen the demanded elts by the src element count.
3054 SDValue Src = V.getOperand(0);
3055 // We don't support scalable vectors at the moment.
3056 if (Src.getValueType().isScalableVector())
3057 return false;
3058 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3059 APInt UndefSrcElts;
3060 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3061 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3062 UndefElts = UndefSrcElts.trunc(NumElts);
3063 return true;
3064 }
3065 break;
3066 }
3067 case ISD::BITCAST: {
3068 SDValue Src = V.getOperand(0);
3069 EVT SrcVT = Src.getValueType();
3070 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3071 unsigned BitWidth = VT.getScalarSizeInBits();
3072
3073 // Ignore bitcasts from unsupported types.
3074 // TODO: Add fp support?
3075 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3076 break;
3077
3078 // Bitcast 'small element' vector to 'large element' vector.
3079 if ((BitWidth % SrcBitWidth) == 0) {
3080 // See if each sub element is a splat.
3081 unsigned Scale = BitWidth / SrcBitWidth;
3082 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3083 APInt ScaledDemandedElts =
3084 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3085 for (unsigned I = 0; I != Scale; ++I) {
3086 APInt SubUndefElts;
3087 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3088 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3089 SubDemandedElts &= ScaledDemandedElts;
3090 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3091 return false;
3092 // TODO: Add support for merging sub undef elements.
3093 if (!SubUndefElts.isZero())
3094 return false;
3095 }
3096 return true;
3097 }
3098 break;
3099 }
3100 }
3101
3102 return false;
3103}
3104
3105/// Helper wrapper to main isSplatValue function.
3106bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3107 EVT VT = V.getValueType();
3108 assert(VT.isVector() && "Vector type expected");
3109
3110 APInt UndefElts;
3111 // Since the number of lanes in a scalable vector is unknown at compile time,
3112 // we track one bit which is implicitly broadcast to all lanes. This means
3113 // that all lanes in a scalable vector are considered demanded.
3114 APInt DemandedElts
3116 return isSplatValue(V, DemandedElts, UndefElts) &&
3117 (AllowUndefs || !UndefElts);
3118}
3119
3122
3123 EVT VT = V.getValueType();
3124 unsigned Opcode = V.getOpcode();
3125 switch (Opcode) {
3126 default: {
3127 APInt UndefElts;
3128 // Since the number of lanes in a scalable vector is unknown at compile time,
3129 // we track one bit which is implicitly broadcast to all lanes. This means
3130 // that all lanes in a scalable vector are considered demanded.
3131 APInt DemandedElts
3133
3134 if (isSplatValue(V, DemandedElts, UndefElts)) {
3135 if (VT.isScalableVector()) {
3136 // DemandedElts and UndefElts are ignored for scalable vectors, since
3137 // the only supported cases are SPLAT_VECTOR nodes.
3138 SplatIdx = 0;
3139 } else {
3140 // Handle case where all demanded elements are UNDEF.
3141 if (DemandedElts.isSubsetOf(UndefElts)) {
3142 SplatIdx = 0;
3143 return getUNDEF(VT);
3144 }
3145 SplatIdx = (UndefElts & DemandedElts).countr_one();
3146 }
3147 return V;
3148 }
3149 break;
3150 }
3151 case ISD::SPLAT_VECTOR:
3152 SplatIdx = 0;
3153 return V;
3154 case ISD::VECTOR_SHUFFLE: {
3155 assert(!VT.isScalableVector());
3156 // Check if this is a shuffle node doing a splat.
3157 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3158 // getTargetVShiftNode currently struggles without the splat source.
3159 auto *SVN = cast<ShuffleVectorSDNode>(V);
3160 if (!SVN->isSplat())
3161 break;
3162 int Idx = SVN->getSplatIndex();
3163 int NumElts = V.getValueType().getVectorNumElements();
3164 SplatIdx = Idx % NumElts;
3165 return V.getOperand(Idx / NumElts);
3166 }
3167 }
3168
3169 return SDValue();
3170}
3171
3173 int SplatIdx;
3174 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3175 EVT SVT = SrcVector.getValueType().getScalarType();
3176 EVT LegalSVT = SVT;
3177 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3178 if (!SVT.isInteger())
3179 return SDValue();
3180 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3181 if (LegalSVT.bitsLT(SVT))
3182 return SDValue();
3183 }
3184 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3185 }
3186 return SDValue();
3187}
3188
3189std::optional<ConstantRange>
3191 unsigned Depth) const {
3192 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3193 V.getOpcode() == ISD::SRA) &&
3194 "Unknown shift node");
3195 // Shifting more than the bitwidth is not valid.
3196 unsigned BitWidth = V.getScalarValueSizeInBits();
3197
3198 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3199 const APInt &ShAmt = Cst->getAPIntValue();
3200 if (ShAmt.uge(BitWidth))
3201 return std::nullopt;
3202 return ConstantRange(ShAmt);
3203 }
3204
3205 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3206 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3207 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3208 if (!DemandedElts[i])
3209 continue;
3210 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3211 if (!SA) {
3212 MinAmt = MaxAmt = nullptr;
3213 break;
3214 }
3215 const APInt &ShAmt = SA->getAPIntValue();
3216 if (ShAmt.uge(BitWidth))
3217 return std::nullopt;
3218 if (!MinAmt || MinAmt->ugt(ShAmt))
3219 MinAmt = &ShAmt;
3220 if (!MaxAmt || MaxAmt->ult(ShAmt))
3221 MaxAmt = &ShAmt;
3222 }
3223 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3224 "Failed to find matching min/max shift amounts");
3225 if (MinAmt && MaxAmt)
3226 return ConstantRange(*MinAmt, *MaxAmt + 1);
3227 }
3228
3229 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3230 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3231 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3232 if (KnownAmt.getMaxValue().ult(BitWidth))
3233 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3234
3235 return std::nullopt;
3236}
3237
3238std::optional<unsigned>
3240 unsigned Depth) const {
3241 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3242 V.getOpcode() == ISD::SRA) &&
3243 "Unknown shift node");
3244 if (std::optional<ConstantRange> AmtRange =
3245 getValidShiftAmountRange(V, DemandedElts, Depth))
3246 if (const APInt *ShAmt = AmtRange->getSingleElement())
3247 return ShAmt->getZExtValue();
3248 return std::nullopt;
3249}
3250
3251std::optional<unsigned>
3253 EVT VT = V.getValueType();
3254 APInt DemandedElts = VT.isFixedLengthVector()
3256 : APInt(1, 1);
3257 return getValidShiftAmount(V, DemandedElts, Depth);
3258}
3259
3260std::optional<unsigned>
3262 unsigned Depth) const {
3263 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3264 V.getOpcode() == ISD::SRA) &&
3265 "Unknown shift node");
3266 if (std::optional<ConstantRange> AmtRange =
3267 getValidShiftAmountRange(V, DemandedElts, Depth))
3268 return AmtRange->getUnsignedMin().getZExtValue();
3269 return std::nullopt;
3270}
3271
3272std::optional<unsigned>
3274 EVT VT = V.getValueType();
3275 APInt DemandedElts = VT.isFixedLengthVector()
3277 : APInt(1, 1);
3278 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3279}
3280
3281std::optional<unsigned>
3283 unsigned Depth) const {
3284 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3285 V.getOpcode() == ISD::SRA) &&
3286 "Unknown shift node");
3287 if (std::optional<ConstantRange> AmtRange =
3288 getValidShiftAmountRange(V, DemandedElts, Depth))
3289 return AmtRange->getUnsignedMax().getZExtValue();
3290 return std::nullopt;
3291}
3292
3293std::optional<unsigned>
3295 EVT VT = V.getValueType();
3296 APInt DemandedElts = VT.isFixedLengthVector()
3298 : APInt(1, 1);
3299 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3300}
3301
3302/// Determine which bits of Op are known to be either zero or one and return
3303/// them in Known. For vectors, the known bits are those that are shared by
3304/// every vector element.
3306 EVT VT = Op.getValueType();
3307
3308 // Since the number of lanes in a scalable vector is unknown at compile time,
3309 // we track one bit which is implicitly broadcast to all lanes. This means
3310 // that all lanes in a scalable vector are considered demanded.
3311 APInt DemandedElts = VT.isFixedLengthVector()
3313 : APInt(1, 1);
3314 return computeKnownBits(Op, DemandedElts, Depth);
3315}
3316
3317/// Determine which bits of Op are known to be either zero or one and return
3318/// them in Known. The DemandedElts argument allows us to only collect the known
3319/// bits that are shared by the requested vector elements.
3321 unsigned Depth) const {
3322 unsigned BitWidth = Op.getScalarValueSizeInBits();
3323
3324 KnownBits Known(BitWidth); // Don't know anything.
3325
3326 if (auto OptAPInt = Op->bitcastToAPInt()) {
3327 // We know all of the bits for a constant!
3328 return KnownBits::makeConstant(*std::move(OptAPInt));
3329 }
3330
3331 if (Depth >= MaxRecursionDepth)
3332 return Known; // Limit search depth.
3333
3334 KnownBits Known2;
3335 unsigned NumElts = DemandedElts.getBitWidth();
3336 assert((!Op.getValueType().isFixedLengthVector() ||
3337 NumElts == Op.getValueType().getVectorNumElements()) &&
3338 "Unexpected vector size");
3339
3340 if (!DemandedElts)
3341 return Known; // No demanded elts, better to assume we don't know anything.
3342
3343 unsigned Opcode = Op.getOpcode();
3344 switch (Opcode) {
3345 case ISD::MERGE_VALUES:
3346 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3347 Depth + 1);
3348 case ISD::SPLAT_VECTOR: {
3349 SDValue SrcOp = Op.getOperand(0);
3350 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3351 "Expected SPLAT_VECTOR implicit truncation");
3352 // Implicitly truncate the bits to match the official semantics of
3353 // SPLAT_VECTOR.
3354 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3355 break;
3356 }
3358 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3359 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3360 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3361 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3362 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3363 }
3364 break;
3365 }
3366 case ISD::STEP_VECTOR: {
3367 const APInt &Step = Op.getConstantOperandAPInt(0);
3368
3369 if (Step.isPowerOf2())
3370 Known.Zero.setLowBits(Step.logBase2());
3371
3373
3374 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3375 break;
3376 const APInt MinNumElts =
3377 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3378
3379 bool Overflow;
3380 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3382 .umul_ov(MinNumElts, Overflow);
3383 if (Overflow)
3384 break;
3385
3386 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3387 if (Overflow)
3388 break;
3389
3390 Known.Zero.setHighBits(MaxValue.countl_zero());
3391 break;
3392 }
3393 case ISD::BUILD_VECTOR:
3394 assert(!Op.getValueType().isScalableVector());
3395 // Collect the known bits that are shared by every demanded vector element.
3396 Known.setAllConflict();
3397 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3398 if (!DemandedElts[i])
3399 continue;
3400
3401 SDValue SrcOp = Op.getOperand(i);
3402 Known2 = computeKnownBits(SrcOp, Depth + 1);
3403
3404 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3405 if (SrcOp.getValueSizeInBits() != BitWidth) {
3406 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3407 "Expected BUILD_VECTOR implicit truncation");
3408 Known2 = Known2.trunc(BitWidth);
3409 }
3410
3411 // Known bits are the values that are shared by every demanded element.
3412 Known = Known.intersectWith(Known2);
3413
3414 // If we don't know any bits, early out.
3415 if (Known.isUnknown())
3416 break;
3417 }
3418 break;
3419 case ISD::VECTOR_COMPRESS: {
3420 SDValue Vec = Op.getOperand(0);
3421 SDValue PassThru = Op.getOperand(2);
3422 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3423 // If we don't know any bits, early out.
3424 if (Known.isUnknown())
3425 break;
3426 Known2 = computeKnownBits(Vec, Depth + 1);
3427 Known = Known.intersectWith(Known2);
3428 break;
3429 }
3430 case ISD::VECTOR_SHUFFLE: {
3431 assert(!Op.getValueType().isScalableVector());
3432 // Collect the known bits that are shared by every vector element referenced
3433 // by the shuffle.
3434 APInt DemandedLHS, DemandedRHS;
3436 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3437 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3438 DemandedLHS, DemandedRHS))
3439 break;
3440
3441 // Known bits are the values that are shared by every demanded element.
3442 Known.setAllConflict();
3443 if (!!DemandedLHS) {
3444 SDValue LHS = Op.getOperand(0);
3445 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3446 Known = Known.intersectWith(Known2);
3447 }
3448 // If we don't know any bits, early out.
3449 if (Known.isUnknown())
3450 break;
3451 if (!!DemandedRHS) {
3452 SDValue RHS = Op.getOperand(1);
3453 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3454 Known = Known.intersectWith(Known2);
3455 }
3456 break;
3457 }
3458 case ISD::VSCALE: {
3460 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3461 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3462 break;
3463 }
3464 case ISD::CONCAT_VECTORS: {
3465 if (Op.getValueType().isScalableVector())
3466 break;
3467 // Split DemandedElts and test each of the demanded subvectors.
3468 Known.setAllConflict();
3469 EVT SubVectorVT = Op.getOperand(0).getValueType();
3470 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3471 unsigned NumSubVectors = Op.getNumOperands();
3472 for (unsigned i = 0; i != NumSubVectors; ++i) {
3473 APInt DemandedSub =
3474 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3475 if (!!DemandedSub) {
3476 SDValue Sub = Op.getOperand(i);
3477 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3478 Known = Known.intersectWith(Known2);
3479 }
3480 // If we don't know any bits, early out.
3481 if (Known.isUnknown())
3482 break;
3483 }
3484 break;
3485 }
3486 case ISD::INSERT_SUBVECTOR: {
3487 if (Op.getValueType().isScalableVector())
3488 break;
3489 // Demand any elements from the subvector and the remainder from the src its
3490 // inserted into.
3491 SDValue Src = Op.getOperand(0);
3492 SDValue Sub = Op.getOperand(1);
3493 uint64_t Idx = Op.getConstantOperandVal(2);
3494 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3495 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3496 APInt DemandedSrcElts = DemandedElts;
3497 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3498
3499 Known.setAllConflict();
3500 if (!!DemandedSubElts) {
3501 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3502 if (Known.isUnknown())
3503 break; // early-out.
3504 }
3505 if (!!DemandedSrcElts) {
3506 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3507 Known = Known.intersectWith(Known2);
3508 }
3509 break;
3510 }
3512 // Offset the demanded elts by the subvector index.
3513 SDValue Src = Op.getOperand(0);
3514 // Bail until we can represent demanded elements for scalable vectors.
3515 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3516 break;
3517 uint64_t Idx = Op.getConstantOperandVal(1);
3518 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3519 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3520 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3521 break;
3522 }
3523 case ISD::SCALAR_TO_VECTOR: {
3524 if (Op.getValueType().isScalableVector())
3525 break;
3526 // We know about scalar_to_vector as much as we know about it source,
3527 // which becomes the first element of otherwise unknown vector.
3528 if (DemandedElts != 1)
3529 break;
3530
3531 SDValue N0 = Op.getOperand(0);
3532 Known = computeKnownBits(N0, Depth + 1);
3533 if (N0.getValueSizeInBits() != BitWidth)
3534 Known = Known.trunc(BitWidth);
3535
3536 break;
3537 }
3538 case ISD::BITCAST: {
3539 if (Op.getValueType().isScalableVector())
3540 break;
3541
3542 SDValue N0 = Op.getOperand(0);
3543 EVT SubVT = N0.getValueType();
3544 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3545
3546 // Ignore bitcasts from unsupported types.
3547 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3548 break;
3549
3550 // Fast handling of 'identity' bitcasts.
3551 if (BitWidth == SubBitWidth) {
3552 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3553 break;
3554 }
3555
3556 bool IsLE = getDataLayout().isLittleEndian();
3557
3558 // Bitcast 'small element' vector to 'large element' scalar/vector.
3559 if ((BitWidth % SubBitWidth) == 0) {
3560 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3561
3562 // Collect known bits for the (larger) output by collecting the known
3563 // bits from each set of sub elements and shift these into place.
3564 // We need to separately call computeKnownBits for each set of
3565 // sub elements as the knownbits for each is likely to be different.
3566 unsigned SubScale = BitWidth / SubBitWidth;
3567 APInt SubDemandedElts(NumElts * SubScale, 0);
3568 for (unsigned i = 0; i != NumElts; ++i)
3569 if (DemandedElts[i])
3570 SubDemandedElts.setBit(i * SubScale);
3571
3572 for (unsigned i = 0; i != SubScale; ++i) {
3573 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3574 Depth + 1);
3575 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3576 Known.insertBits(Known2, SubBitWidth * Shifts);
3577 }
3578 }
3579
3580 // Bitcast 'large element' scalar/vector to 'small element' vector.
3581 if ((SubBitWidth % BitWidth) == 0) {
3582 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3583
3584 // Collect known bits for the (smaller) output by collecting the known
3585 // bits from the overlapping larger input elements and extracting the
3586 // sub sections we actually care about.
3587 unsigned SubScale = SubBitWidth / BitWidth;
3588 APInt SubDemandedElts =
3589 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3590 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3591
3592 Known.setAllConflict();
3593 for (unsigned i = 0; i != NumElts; ++i)
3594 if (DemandedElts[i]) {
3595 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3596 unsigned Offset = (Shifts % SubScale) * BitWidth;
3597 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3598 // If we don't know any bits, early out.
3599 if (Known.isUnknown())
3600 break;
3601 }
3602 }
3603 break;
3604 }
3605 case ISD::AND:
3606 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3607 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3608
3609 Known &= Known2;
3610 break;
3611 case ISD::OR:
3612 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3613 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3614
3615 Known |= Known2;
3616 break;
3617 case ISD::XOR:
3618 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3619 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3620
3621 Known ^= Known2;
3622 break;
3623 case ISD::MUL: {
3624 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3625 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3626 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3627 // TODO: SelfMultiply can be poison, but not undef.
3628 if (SelfMultiply)
3629 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3630 Op.getOperand(0), DemandedElts, false, Depth + 1);
3631 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3632
3633 // If the multiplication is known not to overflow, the product of a number
3634 // with itself is non-negative. Only do this if we didn't already computed
3635 // the opposite value for the sign bit.
3636 if (Op->getFlags().hasNoSignedWrap() &&
3637 Op.getOperand(0) == Op.getOperand(1) &&
3638 !Known.isNegative())
3639 Known.makeNonNegative();
3640 break;
3641 }
3642 case ISD::MULHU: {
3643 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3644 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3645 Known = KnownBits::mulhu(Known, Known2);
3646 break;
3647 }
3648 case ISD::MULHS: {
3649 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3650 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651 Known = KnownBits::mulhs(Known, Known2);
3652 break;
3653 }
3654 case ISD::ABDU: {
3655 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3656 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3657 Known = KnownBits::abdu(Known, Known2);
3658 break;
3659 }
3660 case ISD::ABDS: {
3661 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3662 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3663 Known = KnownBits::abds(Known, Known2);
3664 unsigned SignBits1 =
3665 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3666 if (SignBits1 == 1)
3667 break;
3668 unsigned SignBits0 =
3669 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3670 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3671 break;
3672 }
3673 case ISD::UMUL_LOHI: {
3674 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3675 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3676 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3677 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3678 if (Op.getResNo() == 0)
3679 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3680 else
3681 Known = KnownBits::mulhu(Known, Known2);
3682 break;
3683 }
3684 case ISD::SMUL_LOHI: {
3685 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3686 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3687 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3688 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3689 if (Op.getResNo() == 0)
3690 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3691 else
3692 Known = KnownBits::mulhs(Known, Known2);
3693 break;
3694 }
3695 case ISD::AVGFLOORU: {
3696 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3697 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3698 Known = KnownBits::avgFloorU(Known, Known2);
3699 break;
3700 }
3701 case ISD::AVGCEILU: {
3702 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3703 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3704 Known = KnownBits::avgCeilU(Known, Known2);
3705 break;
3706 }
3707 case ISD::AVGFLOORS: {
3708 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3709 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3710 Known = KnownBits::avgFloorS(Known, Known2);
3711 break;
3712 }
3713 case ISD::AVGCEILS: {
3714 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3715 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known = KnownBits::avgCeilS(Known, Known2);
3717 break;
3718 }
3719 case ISD::SELECT:
3720 case ISD::VSELECT:
3721 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3722 // If we don't know any bits, early out.
3723 if (Known.isUnknown())
3724 break;
3725 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3726
3727 // Only known if known in both the LHS and RHS.
3728 Known = Known.intersectWith(Known2);
3729 break;
3730 case ISD::SELECT_CC:
3731 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3732 // If we don't know any bits, early out.
3733 if (Known.isUnknown())
3734 break;
3735 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3736
3737 // Only known if known in both the LHS and RHS.
3738 Known = Known.intersectWith(Known2);
3739 break;
3740 case ISD::SMULO:
3741 case ISD::UMULO:
3742 if (Op.getResNo() != 1)
3743 break;
3744 // The boolean result conforms to getBooleanContents.
3745 // If we know the result of a setcc has the top bits zero, use this info.
3746 // We know that we have an integer-based boolean since these operations
3747 // are only available for integer.
3748 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3750 BitWidth > 1)
3751 Known.Zero.setBitsFrom(1);
3752 break;
3753 case ISD::SETCC:
3754 case ISD::SETCCCARRY:
3755 case ISD::STRICT_FSETCC:
3756 case ISD::STRICT_FSETCCS: {
3757 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3758 // If we know the result of a setcc has the top bits zero, use this info.
3759 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3761 BitWidth > 1)
3762 Known.Zero.setBitsFrom(1);
3763 break;
3764 }
3765 case ISD::SHL: {
3766 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3767 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3768
3769 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3770 bool NSW = Op->getFlags().hasNoSignedWrap();
3771
3772 bool ShAmtNonZero = Known2.isNonZero();
3773
3774 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3775
3776 // Minimum shift low bits are known zero.
3777 if (std::optional<unsigned> ShMinAmt =
3778 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3779 Known.Zero.setLowBits(*ShMinAmt);
3780 break;
3781 }
3782 case ISD::SRL:
3783 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3784 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3785 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3786 Op->getFlags().hasExact());
3787
3788 // Minimum shift high bits are known zero.
3789 if (std::optional<unsigned> ShMinAmt =
3790 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3791 Known.Zero.setHighBits(*ShMinAmt);
3792 break;
3793 case ISD::SRA:
3794 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3795 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3796 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3797 Op->getFlags().hasExact());
3798 break;
3799 case ISD::ROTL:
3800 case ISD::ROTR:
3801 if (ConstantSDNode *C =
3802 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3803 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3804
3805 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3806
3807 // Canonicalize to ROTR.
3808 if (Opcode == ISD::ROTL && Amt != 0)
3809 Amt = BitWidth - Amt;
3810
3811 Known.Zero = Known.Zero.rotr(Amt);
3812 Known.One = Known.One.rotr(Amt);
3813 }
3814 break;
3815 case ISD::FSHL:
3816 case ISD::FSHR:
3817 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3818 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3819
3820 // For fshl, 0-shift returns the 1st arg.
3821 // For fshr, 0-shift returns the 2nd arg.
3822 if (Amt == 0) {
3823 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3824 DemandedElts, Depth + 1);
3825 break;
3826 }
3827
3828 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3829 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3830 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3831 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3832 if (Opcode == ISD::FSHL) {
3833 Known <<= Amt;
3834 Known2 >>= BitWidth - Amt;
3835 } else {
3836 Known <<= BitWidth - Amt;
3837 Known2 >>= Amt;
3838 }
3839 Known = Known.unionWith(Known2);
3840 }
3841 break;
3842 case ISD::SHL_PARTS:
3843 case ISD::SRA_PARTS:
3844 case ISD::SRL_PARTS: {
3845 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3846
3847 // Collect lo/hi source values and concatenate.
3848 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3849 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3850 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3851 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3852 Known = Known2.concat(Known);
3853
3854 // Collect shift amount.
3855 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3856
3857 if (Opcode == ISD::SHL_PARTS)
3858 Known = KnownBits::shl(Known, Known2);
3859 else if (Opcode == ISD::SRA_PARTS)
3860 Known = KnownBits::ashr(Known, Known2);
3861 else // if (Opcode == ISD::SRL_PARTS)
3862 Known = KnownBits::lshr(Known, Known2);
3863
3864 // TODO: Minimum shift low/high bits are known zero.
3865
3866 if (Op.getResNo() == 0)
3867 Known = Known.extractBits(LoBits, 0);
3868 else
3869 Known = Known.extractBits(HiBits, LoBits);
3870 break;
3871 }
3873 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3874 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3875 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3876 break;
3877 }
3878 case ISD::CTTZ:
3879 case ISD::CTTZ_ZERO_UNDEF: {
3880 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3881 // If we have a known 1, its position is our upper bound.
3882 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3883 unsigned LowBits = llvm::bit_width(PossibleTZ);
3884 Known.Zero.setBitsFrom(LowBits);
3885 break;
3886 }
3887 case ISD::CTLZ:
3888 case ISD::CTLZ_ZERO_UNDEF: {
3889 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3890 // If we have a known 1, its position is our upper bound.
3891 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3892 unsigned LowBits = llvm::bit_width(PossibleLZ);
3893 Known.Zero.setBitsFrom(LowBits);
3894 break;
3895 }
3896 case ISD::CTLS: {
3897 unsigned MinRedundantSignBits =
3898 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3899 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3901 Known = Range.toKnownBits();
3902 break;
3903 }
3904 case ISD::CTPOP: {
3905 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3906 // If we know some of the bits are zero, they can't be one.
3907 unsigned PossibleOnes = Known2.countMaxPopulation();
3908 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3909 break;
3910 }
3911 case ISD::PARITY: {
3912 // Parity returns 0 everywhere but the LSB.
3913 Known.Zero.setBitsFrom(1);
3914 break;
3915 }
3916 case ISD::MGATHER:
3917 case ISD::MLOAD: {
3918 ISD::LoadExtType ETy =
3919 (Opcode == ISD::MGATHER)
3920 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3921 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3922 if (ETy == ISD::ZEXTLOAD) {
3923 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3924 KnownBits Known0(MemVT.getScalarSizeInBits());
3925 return Known0.zext(BitWidth);
3926 }
3927 break;
3928 }
3929 case ISD::LOAD: {
3931 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3932 if (ISD::isNON_EXTLoad(LD) && Cst) {
3933 // Determine any common known bits from the loaded constant pool value.
3934 Type *CstTy = Cst->getType();
3935 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3936 !Op.getValueType().isScalableVector()) {
3937 // If its a vector splat, then we can (quickly) reuse the scalar path.
3938 // NOTE: We assume all elements match and none are UNDEF.
3939 if (CstTy->isVectorTy()) {
3940 if (const Constant *Splat = Cst->getSplatValue()) {
3941 Cst = Splat;
3942 CstTy = Cst->getType();
3943 }
3944 }
3945 // TODO - do we need to handle different bitwidths?
3946 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3947 // Iterate across all vector elements finding common known bits.
3948 Known.setAllConflict();
3949 for (unsigned i = 0; i != NumElts; ++i) {
3950 if (!DemandedElts[i])
3951 continue;
3952 if (Constant *Elt = Cst->getAggregateElement(i)) {
3953 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3954 const APInt &Value = CInt->getValue();
3955 Known.One &= Value;
3956 Known.Zero &= ~Value;
3957 continue;
3958 }
3959 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3960 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3961 Known.One &= Value;
3962 Known.Zero &= ~Value;
3963 continue;
3964 }
3965 }
3966 Known.One.clearAllBits();
3967 Known.Zero.clearAllBits();
3968 break;
3969 }
3970 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3971 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3972 Known = KnownBits::makeConstant(CInt->getValue());
3973 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3974 Known =
3975 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3976 }
3977 }
3978 }
3979 } else if (Op.getResNo() == 0) {
3980 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3981 KnownBits KnownScalarMemory(ScalarMemorySize);
3982 if (const MDNode *MD = LD->getRanges())
3983 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3984
3985 // Extend the Known bits from memory to the size of the scalar result.
3986 if (ISD::isZEXTLoad(Op.getNode()))
3987 Known = KnownScalarMemory.zext(BitWidth);
3988 else if (ISD::isSEXTLoad(Op.getNode()))
3989 Known = KnownScalarMemory.sext(BitWidth);
3990 else if (ISD::isEXTLoad(Op.getNode()))
3991 Known = KnownScalarMemory.anyext(BitWidth);
3992 else
3993 Known = KnownScalarMemory;
3994 assert(Known.getBitWidth() == BitWidth);
3995 return Known;
3996 }
3997 break;
3998 }
4000 if (Op.getValueType().isScalableVector())
4001 break;
4002 EVT InVT = Op.getOperand(0).getValueType();
4003 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4004 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4005 Known = Known.zext(BitWidth);
4006 break;
4007 }
4008 case ISD::ZERO_EXTEND: {
4009 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4010 Known = Known.zext(BitWidth);
4011 break;
4012 }
4014 if (Op.getValueType().isScalableVector())
4015 break;
4016 EVT InVT = Op.getOperand(0).getValueType();
4017 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4018 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4019 // If the sign bit is known to be zero or one, then sext will extend
4020 // it to the top bits, else it will just zext.
4021 Known = Known.sext(BitWidth);
4022 break;
4023 }
4024 case ISD::SIGN_EXTEND: {
4025 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4026 // If the sign bit is known to be zero or one, then sext will extend
4027 // it to the top bits, else it will just zext.
4028 Known = Known.sext(BitWidth);
4029 break;
4030 }
4032 if (Op.getValueType().isScalableVector())
4033 break;
4034 EVT InVT = Op.getOperand(0).getValueType();
4035 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4036 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4037 Known = Known.anyext(BitWidth);
4038 break;
4039 }
4040 case ISD::ANY_EXTEND: {
4041 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4042 Known = Known.anyext(BitWidth);
4043 break;
4044 }
4045 case ISD::TRUNCATE: {
4046 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4047 Known = Known.trunc(BitWidth);
4048 break;
4049 }
4050 case ISD::TRUNCATE_SSAT_S: {
4051 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4052 Known = Known.truncSSat(BitWidth);
4053 break;
4054 }
4055 case ISD::TRUNCATE_SSAT_U: {
4056 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4057 Known = Known.truncSSatU(BitWidth);
4058 break;
4059 }
4060 case ISD::TRUNCATE_USAT_U: {
4061 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4062 Known = Known.truncUSat(BitWidth);
4063 break;
4064 }
4065 case ISD::AssertZext: {
4066 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4068 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4069 Known.Zero |= (~InMask);
4070 Known.One &= (~Known.Zero);
4071 break;
4072 }
4073 case ISD::AssertAlign: {
4074 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4075 assert(LogOfAlign != 0);
4076
4077 // TODO: Should use maximum with source
4078 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4079 // well as clearing one bits.
4080 Known.Zero.setLowBits(LogOfAlign);
4081 Known.One.clearLowBits(LogOfAlign);
4082 break;
4083 }
4084 case ISD::AssertNoFPClass: {
4085 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4086
4087 FPClassTest NoFPClass =
4088 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4089 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4090 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4091 // Cannot be negative.
4092 Known.makeNonNegative();
4093 }
4094
4095 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4096 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4097 // Cannot be positive.
4098 Known.makeNegative();
4099 }
4100
4101 break;
4102 }
4103 case ISD::FGETSIGN:
4104 // All bits are zero except the low bit.
4105 Known.Zero.setBitsFrom(1);
4106 break;
4107 case ISD::ADD:
4108 case ISD::SUB: {
4109 SDNodeFlags Flags = Op.getNode()->getFlags();
4110 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4111 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4113 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4114 Flags.hasNoUnsignedWrap(), Known, Known2);
4115 break;
4116 }
4117 case ISD::USUBO:
4118 case ISD::SSUBO:
4119 case ISD::USUBO_CARRY:
4120 case ISD::SSUBO_CARRY:
4121 if (Op.getResNo() == 1) {
4122 // If we know the result of a setcc has the top bits zero, use this info.
4123 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4125 BitWidth > 1)
4126 Known.Zero.setBitsFrom(1);
4127 break;
4128 }
4129 [[fallthrough]];
4130 case ISD::SUBC: {
4131 assert(Op.getResNo() == 0 &&
4132 "We only compute knownbits for the difference here.");
4133
4134 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4135 KnownBits Borrow(1);
4136 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4137 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4138 // Borrow has bit width 1
4139 Borrow = Borrow.trunc(1);
4140 } else {
4141 Borrow.setAllZero();
4142 }
4143
4144 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4145 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4146 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4147 break;
4148 }
4149 case ISD::UADDO:
4150 case ISD::SADDO:
4151 case ISD::UADDO_CARRY:
4152 case ISD::SADDO_CARRY:
4153 if (Op.getResNo() == 1) {
4154 // If we know the result of a setcc has the top bits zero, use this info.
4155 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4157 BitWidth > 1)
4158 Known.Zero.setBitsFrom(1);
4159 break;
4160 }
4161 [[fallthrough]];
4162 case ISD::ADDC:
4163 case ISD::ADDE: {
4164 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4165
4166 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4167 KnownBits Carry(1);
4168 if (Opcode == ISD::ADDE)
4169 // Can't track carry from glue, set carry to unknown.
4170 Carry.resetAll();
4171 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4172 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4173 // Carry has bit width 1
4174 Carry = Carry.trunc(1);
4175 } else {
4176 Carry.setAllZero();
4177 }
4178
4179 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4180 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4181 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4182 break;
4183 }
4184 case ISD::UDIV: {
4185 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4186 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4187 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4188 break;
4189 }
4190 case ISD::SDIV: {
4191 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4192 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4193 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4194 break;
4195 }
4196 case ISD::SREM: {
4197 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4198 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4199 Known = KnownBits::srem(Known, Known2);
4200 break;
4201 }
4202 case ISD::UREM: {
4203 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4204 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4205 Known = KnownBits::urem(Known, Known2);
4206 break;
4207 }
4208 case ISD::EXTRACT_ELEMENT: {
4209 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4210 const unsigned Index = Op.getConstantOperandVal(1);
4211 const unsigned EltBitWidth = Op.getValueSizeInBits();
4212
4213 // Remove low part of known bits mask
4214 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4215 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4216
4217 // Remove high part of known bit mask
4218 Known = Known.trunc(EltBitWidth);
4219 break;
4220 }
4222 SDValue InVec = Op.getOperand(0);
4223 SDValue EltNo = Op.getOperand(1);
4224 EVT VecVT = InVec.getValueType();
4225 // computeKnownBits not yet implemented for scalable vectors.
4226 if (VecVT.isScalableVector())
4227 break;
4228 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4229 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4230
4231 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4232 // anything about the extended bits.
4233 if (BitWidth > EltBitWidth)
4234 Known = Known.trunc(EltBitWidth);
4235
4236 // If we know the element index, just demand that vector element, else for
4237 // an unknown element index, ignore DemandedElts and demand them all.
4238 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4239 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4240 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4241 DemandedSrcElts =
4242 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4243
4244 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4245 if (BitWidth > EltBitWidth)
4246 Known = Known.anyext(BitWidth);
4247 break;
4248 }
4250 if (Op.getValueType().isScalableVector())
4251 break;
4252
4253 // If we know the element index, split the demand between the
4254 // source vector and the inserted element, otherwise assume we need
4255 // the original demanded vector elements and the value.
4256 SDValue InVec = Op.getOperand(0);
4257 SDValue InVal = Op.getOperand(1);
4258 SDValue EltNo = Op.getOperand(2);
4259 bool DemandedVal = true;
4260 APInt DemandedVecElts = DemandedElts;
4261 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4262 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4263 unsigned EltIdx = CEltNo->getZExtValue();
4264 DemandedVal = !!DemandedElts[EltIdx];
4265 DemandedVecElts.clearBit(EltIdx);
4266 }
4267 Known.setAllConflict();
4268 if (DemandedVal) {
4269 Known2 = computeKnownBits(InVal, Depth + 1);
4270 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4271 }
4272 if (!!DemandedVecElts) {
4273 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4274 Known = Known.intersectWith(Known2);
4275 }
4276 break;
4277 }
4278 case ISD::BITREVERSE: {
4279 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4280 Known = Known2.reverseBits();
4281 break;
4282 }
4283 case ISD::BSWAP: {
4284 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4285 Known = Known2.byteSwap();
4286 break;
4287 }
4288 case ISD::ABS: {
4289 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4290 Known = Known2.abs();
4291 Known.Zero.setHighBits(
4292 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4293 break;
4294 }
4295 case ISD::USUBSAT: {
4296 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4297 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4298 Known = KnownBits::usub_sat(Known, Known2);
4299 break;
4300 }
4301 case ISD::UMIN: {
4302 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4303 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4304 Known = KnownBits::umin(Known, Known2);
4305 break;
4306 }
4307 case ISD::UMAX: {
4308 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4309 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4310 Known = KnownBits::umax(Known, Known2);
4311 break;
4312 }
4313 case ISD::SMIN:
4314 case ISD::SMAX: {
4315 // If we have a clamp pattern, we know that the number of sign bits will be
4316 // the minimum of the clamp min/max range.
4317 bool IsMax = (Opcode == ISD::SMAX);
4318 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4319 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4320 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4321 CstHigh =
4322 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4323 if (CstLow && CstHigh) {
4324 if (!IsMax)
4325 std::swap(CstLow, CstHigh);
4326
4327 const APInt &ValueLow = CstLow->getAPIntValue();
4328 const APInt &ValueHigh = CstHigh->getAPIntValue();
4329 if (ValueLow.sle(ValueHigh)) {
4330 unsigned LowSignBits = ValueLow.getNumSignBits();
4331 unsigned HighSignBits = ValueHigh.getNumSignBits();
4332 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4333 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4334 Known.One.setHighBits(MinSignBits);
4335 break;
4336 }
4337 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4338 Known.Zero.setHighBits(MinSignBits);
4339 break;
4340 }
4341 }
4342 }
4343
4344 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4345 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4346 if (IsMax)
4347 Known = KnownBits::smax(Known, Known2);
4348 else
4349 Known = KnownBits::smin(Known, Known2);
4350
4351 // For SMAX, if CstLow is non-negative we know the result will be
4352 // non-negative and thus all sign bits are 0.
4353 // TODO: There's an equivalent of this for smin with negative constant for
4354 // known ones.
4355 if (IsMax && CstLow) {
4356 const APInt &ValueLow = CstLow->getAPIntValue();
4357 if (ValueLow.isNonNegative()) {
4358 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4359 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4360 }
4361 }
4362
4363 break;
4364 }
4365 case ISD::UINT_TO_FP: {
4366 Known.makeNonNegative();
4367 break;
4368 }
4369 case ISD::SINT_TO_FP: {
4370 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4371 if (Known2.isNonNegative())
4372 Known.makeNonNegative();
4373 else if (Known2.isNegative())
4374 Known.makeNegative();
4375 break;
4376 }
4377 case ISD::FP_TO_UINT_SAT: {
4378 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4379 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4381 break;
4382 }
4383 case ISD::ATOMIC_LOAD: {
4384 // If we are looking at the loaded value.
4385 if (Op.getResNo() == 0) {
4386 auto *AT = cast<AtomicSDNode>(Op);
4387 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4388 KnownBits KnownScalarMemory(ScalarMemorySize);
4389 if (const MDNode *MD = AT->getRanges())
4390 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4391
4392 switch (AT->getExtensionType()) {
4393 case ISD::ZEXTLOAD:
4394 Known = KnownScalarMemory.zext(BitWidth);
4395 break;
4396 case ISD::SEXTLOAD:
4397 Known = KnownScalarMemory.sext(BitWidth);
4398 break;
4399 case ISD::EXTLOAD:
4400 switch (TLI->getExtendForAtomicOps()) {
4401 case ISD::ZERO_EXTEND:
4402 Known = KnownScalarMemory.zext(BitWidth);
4403 break;
4404 case ISD::SIGN_EXTEND:
4405 Known = KnownScalarMemory.sext(BitWidth);
4406 break;
4407 default:
4408 Known = KnownScalarMemory.anyext(BitWidth);
4409 break;
4410 }
4411 break;
4412 case ISD::NON_EXTLOAD:
4413 Known = KnownScalarMemory;
4414 break;
4415 }
4416 assert(Known.getBitWidth() == BitWidth);
4417 }
4418 break;
4419 }
4421 if (Op.getResNo() == 1) {
4422 // The boolean result conforms to getBooleanContents.
4423 // If we know the result of a setcc has the top bits zero, use this info.
4424 // We know that we have an integer-based boolean since these operations
4425 // are only available for integer.
4426 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4428 BitWidth > 1)
4429 Known.Zero.setBitsFrom(1);
4430 break;
4431 }
4432 [[fallthrough]];
4434 case ISD::ATOMIC_SWAP:
4445 case ISD::ATOMIC_LOAD_UMAX: {
4446 // If we are looking at the loaded value.
4447 if (Op.getResNo() == 0) {
4448 auto *AT = cast<AtomicSDNode>(Op);
4449 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4450
4451 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4452 Known.Zero.setBitsFrom(MemBits);
4453 }
4454 break;
4455 }
4456 case ISD::FrameIndex:
4458 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4459 Known, getMachineFunction());
4460 break;
4461
4462 default:
4463 if (Opcode < ISD::BUILTIN_OP_END)
4464 break;
4465 [[fallthrough]];
4469 // TODO: Probably okay to remove after audit; here to reduce change size
4470 // in initial enablement patch for scalable vectors
4471 if (Op.getValueType().isScalableVector())
4472 break;
4473
4474 // Allow the target to implement this method for its nodes.
4475 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4476 break;
4477 }
4478
4479 return Known;
4480}
4481
4482/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4495
4498 // X + 0 never overflow
4499 if (isNullConstant(N1))
4500 return OFK_Never;
4501
4502 // If both operands each have at least two sign bits, the addition
4503 // cannot overflow.
4504 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4505 return OFK_Never;
4506
4507 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4508 return OFK_Sometime;
4509}
4510
4513 // X + 0 never overflow
4514 if (isNullConstant(N1))
4515 return OFK_Never;
4516
4517 // mulhi + 1 never overflow
4518 KnownBits N1Known = computeKnownBits(N1);
4519 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4520 N1Known.getMaxValue().ult(2))
4521 return OFK_Never;
4522
4523 KnownBits N0Known = computeKnownBits(N0);
4524 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4525 N0Known.getMaxValue().ult(2))
4526 return OFK_Never;
4527
4528 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4529 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4530 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4531 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4532}
4533
4536 // X - 0 never overflow
4537 if (isNullConstant(N1))
4538 return OFK_Never;
4539
4540 // If both operands each have at least two sign bits, the subtraction
4541 // cannot overflow.
4542 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4543 return OFK_Never;
4544
4545 KnownBits N0Known = computeKnownBits(N0);
4546 KnownBits N1Known = computeKnownBits(N1);
4547 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4548 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4549 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4550}
4551
4554 // X - 0 never overflow
4555 if (isNullConstant(N1))
4556 return OFK_Never;
4557
4558 KnownBits N0Known = computeKnownBits(N0);
4559 KnownBits N1Known = computeKnownBits(N1);
4560 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4561 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4562 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4563}
4564
4567 // X * 0 and X * 1 never overflow.
4568 if (isNullConstant(N1) || isOneConstant(N1))
4569 return OFK_Never;
4570
4571 KnownBits N0Known = computeKnownBits(N0);
4572 KnownBits N1Known = computeKnownBits(N1);
4573 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4574 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4575 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4576}
4577
4580 // X * 0 and X * 1 never overflow.
4581 if (isNullConstant(N1) || isOneConstant(N1))
4582 return OFK_Never;
4583
4584 // Get the size of the result.
4585 unsigned BitWidth = N0.getScalarValueSizeInBits();
4586
4587 // Sum of the sign bits.
4588 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4589
4590 // If we have enough sign bits, then there's no overflow.
4591 if (SignBits > BitWidth + 1)
4592 return OFK_Never;
4593
4594 if (SignBits == BitWidth + 1) {
4595 // The overflow occurs when the true multiplication of the
4596 // the operands is the minimum negative number.
4597 KnownBits N0Known = computeKnownBits(N0);
4598 KnownBits N1Known = computeKnownBits(N1);
4599 // If one of the operands is non-negative, then there's no
4600 // overflow.
4601 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4602 return OFK_Never;
4603 }
4604
4605 return OFK_Sometime;
4606}
4607
4609 if (Depth >= MaxRecursionDepth)
4610 return false; // Limit search depth.
4611
4612 EVT OpVT = Val.getValueType();
4613 unsigned BitWidth = OpVT.getScalarSizeInBits();
4614
4615 // Is the constant a known power of 2?
4617 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4618 }))
4619 return true;
4620
4621 // A left-shift of a constant one will have exactly one bit set because
4622 // shifting the bit off the end is undefined.
4623 if (Val.getOpcode() == ISD::SHL) {
4624 auto *C = isConstOrConstSplat(Val.getOperand(0));
4625 if (C && C->getAPIntValue() == 1)
4626 return true;
4627 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4628 isKnownNeverZero(Val, Depth);
4629 }
4630
4631 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4632 // one bit set.
4633 if (Val.getOpcode() == ISD::SRL) {
4634 auto *C = isConstOrConstSplat(Val.getOperand(0));
4635 if (C && C->getAPIntValue().isSignMask())
4636 return true;
4637 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4638 isKnownNeverZero(Val, Depth);
4639 }
4640
4641 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4642 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4643
4644 // Are all operands of a build vector constant powers of two?
4645 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4646 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4648 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4649 return false;
4650 }))
4651 return true;
4652
4653 // Is the operand of a splat vector a constant power of two?
4654 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4656 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4657 return true;
4658
4659 // vscale(power-of-two) is a power-of-two for some targets
4660 if (Val.getOpcode() == ISD::VSCALE &&
4661 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4663 return true;
4664
4665 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4666 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4667 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4669
4670 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4671 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4673
4674 // Looking for `x & -x` pattern:
4675 // If x == 0:
4676 // x & -x -> 0
4677 // If x != 0:
4678 // x & -x -> non-zero pow2
4679 // so if we find the pattern return whether we know `x` is non-zero.
4680 SDValue X;
4681 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4682 return isKnownNeverZero(X, Depth);
4683
4684 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4685 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4686
4687 // More could be done here, though the above checks are enough
4688 // to handle some common cases.
4689 return false;
4690}
4691
4693 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4694 return C1->getValueAPF().getExactLog2Abs() >= 0;
4695
4696 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4697 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4698
4699 return false;
4700}
4701
4703 EVT VT = Op.getValueType();
4704
4705 // Since the number of lanes in a scalable vector is unknown at compile time,
4706 // we track one bit which is implicitly broadcast to all lanes. This means
4707 // that all lanes in a scalable vector are considered demanded.
4708 APInt DemandedElts = VT.isFixedLengthVector()
4710 : APInt(1, 1);
4711 return ComputeNumSignBits(Op, DemandedElts, Depth);
4712}
4713
4714unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4715 unsigned Depth) const {
4716 EVT VT = Op.getValueType();
4717 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4718 unsigned VTBits = VT.getScalarSizeInBits();
4719 unsigned NumElts = DemandedElts.getBitWidth();
4720 unsigned Tmp, Tmp2;
4721 unsigned FirstAnswer = 1;
4722
4723 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4724 const APInt &Val = C->getAPIntValue();
4725 return Val.getNumSignBits();
4726 }
4727
4728 if (Depth >= MaxRecursionDepth)
4729 return 1; // Limit search depth.
4730
4731 if (!DemandedElts)
4732 return 1; // No demanded elts, better to assume we don't know anything.
4733
4734 unsigned Opcode = Op.getOpcode();
4735 switch (Opcode) {
4736 default: break;
4737 case ISD::AssertSext:
4738 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4739 return VTBits-Tmp+1;
4740 case ISD::AssertZext:
4741 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4742 return VTBits-Tmp;
4743 case ISD::FREEZE:
4744 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4745 /*PoisonOnly=*/false))
4746 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4747 break;
4748 case ISD::MERGE_VALUES:
4749 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4750 Depth + 1);
4751 case ISD::SPLAT_VECTOR: {
4752 // Check if the sign bits of source go down as far as the truncated value.
4753 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4754 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4755 if (NumSrcSignBits > (NumSrcBits - VTBits))
4756 return NumSrcSignBits - (NumSrcBits - VTBits);
4757 break;
4758 }
4759 case ISD::BUILD_VECTOR:
4760 assert(!VT.isScalableVector());
4761 Tmp = VTBits;
4762 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4763 if (!DemandedElts[i])
4764 continue;
4765
4766 SDValue SrcOp = Op.getOperand(i);
4767 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4768 // for constant nodes to ensure we only look at the sign bits.
4770 APInt T = C->getAPIntValue().trunc(VTBits);
4771 Tmp2 = T.getNumSignBits();
4772 } else {
4773 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4774
4775 if (SrcOp.getValueSizeInBits() != VTBits) {
4776 assert(SrcOp.getValueSizeInBits() > VTBits &&
4777 "Expected BUILD_VECTOR implicit truncation");
4778 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4779 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4780 }
4781 }
4782 Tmp = std::min(Tmp, Tmp2);
4783 }
4784 return Tmp;
4785
4786 case ISD::VECTOR_COMPRESS: {
4787 SDValue Vec = Op.getOperand(0);
4788 SDValue PassThru = Op.getOperand(2);
4789 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4790 if (Tmp == 1)
4791 return 1;
4792 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4793 Tmp = std::min(Tmp, Tmp2);
4794 return Tmp;
4795 }
4796
4797 case ISD::VECTOR_SHUFFLE: {
4798 // Collect the minimum number of sign bits that are shared by every vector
4799 // element referenced by the shuffle.
4800 APInt DemandedLHS, DemandedRHS;
4802 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4803 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4804 DemandedLHS, DemandedRHS))
4805 return 1;
4806
4807 Tmp = std::numeric_limits<unsigned>::max();
4808 if (!!DemandedLHS)
4809 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4810 if (!!DemandedRHS) {
4811 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4812 Tmp = std::min(Tmp, Tmp2);
4813 }
4814 // If we don't know anything, early out and try computeKnownBits fall-back.
4815 if (Tmp == 1)
4816 break;
4817 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4818 return Tmp;
4819 }
4820
4821 case ISD::BITCAST: {
4822 if (VT.isScalableVector())
4823 break;
4824 SDValue N0 = Op.getOperand(0);
4825 EVT SrcVT = N0.getValueType();
4826 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4827
4828 // Ignore bitcasts from unsupported types..
4829 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4830 break;
4831
4832 // Fast handling of 'identity' bitcasts.
4833 if (VTBits == SrcBits)
4834 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4835
4836 bool IsLE = getDataLayout().isLittleEndian();
4837
4838 // Bitcast 'large element' scalar/vector to 'small element' vector.
4839 if ((SrcBits % VTBits) == 0) {
4840 assert(VT.isVector() && "Expected bitcast to vector");
4841
4842 unsigned Scale = SrcBits / VTBits;
4843 APInt SrcDemandedElts =
4844 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4845
4846 // Fast case - sign splat can be simply split across the small elements.
4847 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4848 if (Tmp == SrcBits)
4849 return VTBits;
4850
4851 // Slow case - determine how far the sign extends into each sub-element.
4852 Tmp2 = VTBits;
4853 for (unsigned i = 0; i != NumElts; ++i)
4854 if (DemandedElts[i]) {
4855 unsigned SubOffset = i % Scale;
4856 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4857 SubOffset = SubOffset * VTBits;
4858 if (Tmp <= SubOffset)
4859 return 1;
4860 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4861 }
4862 return Tmp2;
4863 }
4864 break;
4865 }
4866
4868 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4869 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4870 return VTBits - Tmp + 1;
4871 case ISD::SIGN_EXTEND:
4872 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4873 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4875 // Max of the input and what this extends.
4876 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4877 Tmp = VTBits-Tmp+1;
4878 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4879 return std::max(Tmp, Tmp2);
4881 if (VT.isScalableVector())
4882 break;
4883 SDValue Src = Op.getOperand(0);
4884 EVT SrcVT = Src.getValueType();
4885 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4886 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4887 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4888 }
4889 case ISD::SRA:
4890 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4891 // SRA X, C -> adds C sign bits.
4892 if (std::optional<unsigned> ShAmt =
4893 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4894 Tmp = std::min(Tmp + *ShAmt, VTBits);
4895 return Tmp;
4896 case ISD::SHL:
4897 if (std::optional<ConstantRange> ShAmtRange =
4898 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4899 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4900 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4901 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4902 // shifted out, then we can compute the number of sign bits for the
4903 // operand being extended. A future improvement could be to pass along the
4904 // "shifted left by" information in the recursive calls to
4905 // ComputeKnownSignBits. Allowing us to handle this more generically.
4906 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4907 SDValue Ext = Op.getOperand(0);
4908 EVT ExtVT = Ext.getValueType();
4909 SDValue Extendee = Ext.getOperand(0);
4910 EVT ExtendeeVT = Extendee.getValueType();
4911 unsigned SizeDifference =
4912 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4913 if (SizeDifference <= MinShAmt) {
4914 Tmp = SizeDifference +
4915 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4916 if (MaxShAmt < Tmp)
4917 return Tmp - MaxShAmt;
4918 }
4919 }
4920 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4921 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4922 if (MaxShAmt < Tmp)
4923 return Tmp - MaxShAmt;
4924 }
4925 break;
4926 case ISD::AND:
4927 case ISD::OR:
4928 case ISD::XOR: // NOT is handled here.
4929 // Logical binary ops preserve the number of sign bits at the worst.
4930 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4931 if (Tmp != 1) {
4932 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4933 FirstAnswer = std::min(Tmp, Tmp2);
4934 // We computed what we know about the sign bits as our first
4935 // answer. Now proceed to the generic code that uses
4936 // computeKnownBits, and pick whichever answer is better.
4937 }
4938 break;
4939
4940 case ISD::SELECT:
4941 case ISD::VSELECT:
4942 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4943 if (Tmp == 1) return 1; // Early out.
4944 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4945 return std::min(Tmp, Tmp2);
4946 case ISD::SELECT_CC:
4947 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4948 if (Tmp == 1) return 1; // Early out.
4949 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4950 return std::min(Tmp, Tmp2);
4951
4952 case ISD::SMIN:
4953 case ISD::SMAX: {
4954 // If we have a clamp pattern, we know that the number of sign bits will be
4955 // the minimum of the clamp min/max range.
4956 bool IsMax = (Opcode == ISD::SMAX);
4957 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4958 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4959 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4960 CstHigh =
4961 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4962 if (CstLow && CstHigh) {
4963 if (!IsMax)
4964 std::swap(CstLow, CstHigh);
4965 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4966 Tmp = CstLow->getAPIntValue().getNumSignBits();
4967 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4968 return std::min(Tmp, Tmp2);
4969 }
4970 }
4971
4972 // Fallback - just get the minimum number of sign bits of the operands.
4973 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4974 if (Tmp == 1)
4975 return 1; // Early out.
4976 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4977 return std::min(Tmp, Tmp2);
4978 }
4979 case ISD::UMIN:
4980 case ISD::UMAX:
4981 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4982 if (Tmp == 1)
4983 return 1; // Early out.
4984 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4985 return std::min(Tmp, Tmp2);
4986 case ISD::SSUBO_CARRY:
4987 case ISD::USUBO_CARRY:
4988 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4989 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4990 return VTBits;
4991 [[fallthrough]];
4992 case ISD::SADDO:
4993 case ISD::UADDO:
4994 case ISD::SADDO_CARRY:
4995 case ISD::UADDO_CARRY:
4996 case ISD::SSUBO:
4997 case ISD::USUBO:
4998 case ISD::SMULO:
4999 case ISD::UMULO:
5000 if (Op.getResNo() != 1)
5001 break;
5002 // The boolean result conforms to getBooleanContents. Fall through.
5003 // If setcc returns 0/-1, all bits are sign bits.
5004 // We know that we have an integer-based boolean since these operations
5005 // are only available for integer.
5006 if (TLI->getBooleanContents(VT.isVector(), false) ==
5008 return VTBits;
5009 break;
5010 case ISD::SETCC:
5011 case ISD::SETCCCARRY:
5012 case ISD::STRICT_FSETCC:
5013 case ISD::STRICT_FSETCCS: {
5014 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5015 // If setcc returns 0/-1, all bits are sign bits.
5016 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5018 return VTBits;
5019 break;
5020 }
5021 case ISD::ROTL:
5022 case ISD::ROTR:
5023 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5024
5025 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5026 if (Tmp == VTBits)
5027 return VTBits;
5028
5029 if (ConstantSDNode *C =
5030 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5031 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5032
5033 // Handle rotate right by N like a rotate left by 32-N.
5034 if (Opcode == ISD::ROTR)
5035 RotAmt = (VTBits - RotAmt) % VTBits;
5036
5037 // If we aren't rotating out all of the known-in sign bits, return the
5038 // number that are left. This handles rotl(sext(x), 1) for example.
5039 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5040 }
5041 break;
5042 case ISD::ADD:
5043 case ISD::ADDC:
5044 // TODO: Move Operand 1 check before Operand 0 check
5045 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5046 if (Tmp == 1) return 1; // Early out.
5047
5048 // Special case decrementing a value (ADD X, -1):
5049 if (ConstantSDNode *CRHS =
5050 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5051 if (CRHS->isAllOnes()) {
5052 KnownBits Known =
5053 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5054
5055 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5056 // sign bits set.
5057 if ((Known.Zero | 1).isAllOnes())
5058 return VTBits;
5059
5060 // If we are subtracting one from a positive number, there is no carry
5061 // out of the result.
5062 if (Known.isNonNegative())
5063 return Tmp;
5064 }
5065
5066 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5067 if (Tmp2 == 1) return 1; // Early out.
5068
5069 // Add can have at most one carry bit. Thus we know that the output
5070 // is, at worst, one more bit than the inputs.
5071 return std::min(Tmp, Tmp2) - 1;
5072 case ISD::SUB:
5073 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5074 if (Tmp2 == 1) return 1; // Early out.
5075
5076 // Handle NEG.
5077 if (ConstantSDNode *CLHS =
5078 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5079 if (CLHS->isZero()) {
5080 KnownBits Known =
5081 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5082 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5083 // sign bits set.
5084 if ((Known.Zero | 1).isAllOnes())
5085 return VTBits;
5086
5087 // If the input is known to be positive (the sign bit is known clear),
5088 // the output of the NEG has the same number of sign bits as the input.
5089 if (Known.isNonNegative())
5090 return Tmp2;
5091
5092 // Otherwise, we treat this like a SUB.
5093 }
5094
5095 // Sub can have at most one carry bit. Thus we know that the output
5096 // is, at worst, one more bit than the inputs.
5097 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5098 if (Tmp == 1) return 1; // Early out.
5099 return std::min(Tmp, Tmp2) - 1;
5100 case ISD::MUL: {
5101 // The output of the Mul can be at most twice the valid bits in the inputs.
5102 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5103 if (SignBitsOp0 == 1)
5104 break;
5105 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5106 if (SignBitsOp1 == 1)
5107 break;
5108 unsigned OutValidBits =
5109 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5110 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5111 }
5112 case ISD::AVGCEILS:
5113 case ISD::AVGFLOORS:
5114 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5115 if (Tmp == 1)
5116 return 1; // Early out.
5117 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5118 return std::min(Tmp, Tmp2);
5119 case ISD::SREM:
5120 // The sign bit is the LHS's sign bit, except when the result of the
5121 // remainder is zero. The magnitude of the result should be less than or
5122 // equal to the magnitude of the LHS. Therefore, the result should have
5123 // at least as many sign bits as the left hand side.
5124 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5125 case ISD::TRUNCATE: {
5126 // Check if the sign bits of source go down as far as the truncated value.
5127 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5128 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5129 if (NumSrcSignBits > (NumSrcBits - VTBits))
5130 return NumSrcSignBits - (NumSrcBits - VTBits);
5131 break;
5132 }
5133 case ISD::EXTRACT_ELEMENT: {
5134 if (VT.isScalableVector())
5135 break;
5136 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5137 const int BitWidth = Op.getValueSizeInBits();
5138 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5139
5140 // Get reverse index (starting from 1), Op1 value indexes elements from
5141 // little end. Sign starts at big end.
5142 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5143
5144 // If the sign portion ends in our element the subtraction gives correct
5145 // result. Otherwise it gives either negative or > bitwidth result
5146 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5147 }
5149 if (VT.isScalableVector())
5150 break;
5151 // If we know the element index, split the demand between the
5152 // source vector and the inserted element, otherwise assume we need
5153 // the original demanded vector elements and the value.
5154 SDValue InVec = Op.getOperand(0);
5155 SDValue InVal = Op.getOperand(1);
5156 SDValue EltNo = Op.getOperand(2);
5157 bool DemandedVal = true;
5158 APInt DemandedVecElts = DemandedElts;
5159 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5160 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5161 unsigned EltIdx = CEltNo->getZExtValue();
5162 DemandedVal = !!DemandedElts[EltIdx];
5163 DemandedVecElts.clearBit(EltIdx);
5164 }
5165 Tmp = std::numeric_limits<unsigned>::max();
5166 if (DemandedVal) {
5167 // TODO - handle implicit truncation of inserted elements.
5168 if (InVal.getScalarValueSizeInBits() != VTBits)
5169 break;
5170 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5171 Tmp = std::min(Tmp, Tmp2);
5172 }
5173 if (!!DemandedVecElts) {
5174 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5175 Tmp = std::min(Tmp, Tmp2);
5176 }
5177 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5178 return Tmp;
5179 }
5181 SDValue InVec = Op.getOperand(0);
5182 SDValue EltNo = Op.getOperand(1);
5183 EVT VecVT = InVec.getValueType();
5184 // ComputeNumSignBits not yet implemented for scalable vectors.
5185 if (VecVT.isScalableVector())
5186 break;
5187 const unsigned BitWidth = Op.getValueSizeInBits();
5188 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5189 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5190
5191 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5192 // anything about sign bits. But if the sizes match we can derive knowledge
5193 // about sign bits from the vector operand.
5194 if (BitWidth != EltBitWidth)
5195 break;
5196
5197 // If we know the element index, just demand that vector element, else for
5198 // an unknown element index, ignore DemandedElts and demand them all.
5199 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5200 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5201 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5202 DemandedSrcElts =
5203 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5204
5205 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5206 }
5208 // Offset the demanded elts by the subvector index.
5209 SDValue Src = Op.getOperand(0);
5210 // Bail until we can represent demanded elements for scalable vectors.
5211 if (Src.getValueType().isScalableVector())
5212 break;
5213 uint64_t Idx = Op.getConstantOperandVal(1);
5214 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5215 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5216 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5217 }
5218 case ISD::CONCAT_VECTORS: {
5219 if (VT.isScalableVector())
5220 break;
5221 // Determine the minimum number of sign bits across all demanded
5222 // elts of the input vectors. Early out if the result is already 1.
5223 Tmp = std::numeric_limits<unsigned>::max();
5224 EVT SubVectorVT = Op.getOperand(0).getValueType();
5225 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5226 unsigned NumSubVectors = Op.getNumOperands();
5227 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5228 APInt DemandedSub =
5229 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5230 if (!DemandedSub)
5231 continue;
5232 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5233 Tmp = std::min(Tmp, Tmp2);
5234 }
5235 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5236 return Tmp;
5237 }
5238 case ISD::INSERT_SUBVECTOR: {
5239 if (VT.isScalableVector())
5240 break;
5241 // Demand any elements from the subvector and the remainder from the src its
5242 // inserted into.
5243 SDValue Src = Op.getOperand(0);
5244 SDValue Sub = Op.getOperand(1);
5245 uint64_t Idx = Op.getConstantOperandVal(2);
5246 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5247 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5248 APInt DemandedSrcElts = DemandedElts;
5249 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5250
5251 Tmp = std::numeric_limits<unsigned>::max();
5252 if (!!DemandedSubElts) {
5253 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5254 if (Tmp == 1)
5255 return 1; // early-out
5256 }
5257 if (!!DemandedSrcElts) {
5258 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5259 Tmp = std::min(Tmp, Tmp2);
5260 }
5261 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5262 return Tmp;
5263 }
5264 case ISD::LOAD: {
5265 // If we are looking at the loaded value of the SDNode.
5266 if (Op.getResNo() != 0)
5267 break;
5268
5270 if (const MDNode *Ranges = LD->getRanges()) {
5271 if (DemandedElts != 1)
5272 break;
5273
5275 if (VTBits > CR.getBitWidth()) {
5276 switch (LD->getExtensionType()) {
5277 case ISD::SEXTLOAD:
5278 CR = CR.signExtend(VTBits);
5279 break;
5280 case ISD::ZEXTLOAD:
5281 CR = CR.zeroExtend(VTBits);
5282 break;
5283 default:
5284 break;
5285 }
5286 }
5287
5288 if (VTBits != CR.getBitWidth())
5289 break;
5290 return std::min(CR.getSignedMin().getNumSignBits(),
5292 }
5293
5294 unsigned ExtType = LD->getExtensionType();
5295 switch (ExtType) {
5296 default:
5297 break;
5298 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5299 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5300 return VTBits - Tmp + 1;
5301 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5302 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5303 return VTBits - Tmp;
5304 case ISD::NON_EXTLOAD:
5305 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5306 // We only need to handle vectors - computeKnownBits should handle
5307 // scalar cases.
5308 Type *CstTy = Cst->getType();
5309 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5310 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5311 VTBits == CstTy->getScalarSizeInBits()) {
5312 Tmp = VTBits;
5313 for (unsigned i = 0; i != NumElts; ++i) {
5314 if (!DemandedElts[i])
5315 continue;
5316 if (Constant *Elt = Cst->getAggregateElement(i)) {
5317 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5318 const APInt &Value = CInt->getValue();
5319 Tmp = std::min(Tmp, Value.getNumSignBits());
5320 continue;
5321 }
5322 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5323 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5324 Tmp = std::min(Tmp, Value.getNumSignBits());
5325 continue;
5326 }
5327 }
5328 // Unknown type. Conservatively assume no bits match sign bit.
5329 return 1;
5330 }
5331 return Tmp;
5332 }
5333 }
5334 break;
5335 }
5336
5337 break;
5338 }
5341 case ISD::ATOMIC_SWAP:
5353 case ISD::ATOMIC_LOAD: {
5354 auto *AT = cast<AtomicSDNode>(Op);
5355 // If we are looking at the loaded value.
5356 if (Op.getResNo() == 0) {
5357 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5358 if (Tmp == VTBits)
5359 return 1; // early-out
5360
5361 // For atomic_load, prefer to use the extension type.
5362 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5363 switch (AT->getExtensionType()) {
5364 default:
5365 break;
5366 case ISD::SEXTLOAD:
5367 return VTBits - Tmp + 1;
5368 case ISD::ZEXTLOAD:
5369 return VTBits - Tmp;
5370 }
5371 }
5372
5373 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5374 return VTBits - Tmp + 1;
5375 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5376 return VTBits - Tmp;
5377 }
5378 break;
5379 }
5380 }
5381
5382 // Allow the target to implement this method for its nodes.
5383 if (Opcode >= ISD::BUILTIN_OP_END ||
5384 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5385 Opcode == ISD::INTRINSIC_W_CHAIN ||
5386 Opcode == ISD::INTRINSIC_VOID) {
5387 // TODO: This can probably be removed once target code is audited. This
5388 // is here purely to reduce patch size and review complexity.
5389 if (!VT.isScalableVector()) {
5390 unsigned NumBits =
5391 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5392 if (NumBits > 1)
5393 FirstAnswer = std::max(FirstAnswer, NumBits);
5394 }
5395 }
5396
5397 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5398 // use this information.
5399 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5400 return std::max(FirstAnswer, Known.countMinSignBits());
5401}
5402
5404 unsigned Depth) const {
5405 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5406 return Op.getScalarValueSizeInBits() - SignBits + 1;
5407}
5408
5410 const APInt &DemandedElts,
5411 unsigned Depth) const {
5412 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5413 return Op.getScalarValueSizeInBits() - SignBits + 1;
5414}
5415
5417 unsigned Depth) const {
5418 // Early out for FREEZE.
5419 if (Op.getOpcode() == ISD::FREEZE)
5420 return true;
5421
5422 EVT VT = Op.getValueType();
5423 APInt DemandedElts = VT.isFixedLengthVector()
5425 : APInt(1, 1);
5426 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5427}
5428
5430 const APInt &DemandedElts,
5431 bool PoisonOnly,
5432 unsigned Depth) const {
5433 unsigned Opcode = Op.getOpcode();
5434
5435 // Early out for FREEZE.
5436 if (Opcode == ISD::FREEZE)
5437 return true;
5438
5439 if (Depth >= MaxRecursionDepth)
5440 return false; // Limit search depth.
5441
5442 if (isIntOrFPConstant(Op))
5443 return true;
5444
5445 switch (Opcode) {
5446 case ISD::CONDCODE:
5447 case ISD::VALUETYPE:
5448 case ISD::FrameIndex:
5450 case ISD::CopyFromReg:
5451 return true;
5452
5453 case ISD::POISON:
5454 return false;
5455
5456 case ISD::UNDEF:
5457 return PoisonOnly;
5458
5459 case ISD::BUILD_VECTOR:
5460 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5461 // this shouldn't affect the result.
5462 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5463 if (!DemandedElts[i])
5464 continue;
5466 Depth + 1))
5467 return false;
5468 }
5469 return true;
5470
5472 SDValue Src = Op.getOperand(0);
5473 if (Src.getValueType().isScalableVector())
5474 break;
5475 uint64_t Idx = Op.getConstantOperandVal(1);
5476 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5477 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5478 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5479 Depth + 1);
5480 }
5481
5482 case ISD::INSERT_SUBVECTOR: {
5483 if (Op.getValueType().isScalableVector())
5484 break;
5485 SDValue Src = Op.getOperand(0);
5486 SDValue Sub = Op.getOperand(1);
5487 uint64_t Idx = Op.getConstantOperandVal(2);
5488 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5489 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5490 APInt DemandedSrcElts = DemandedElts;
5491 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5492
5493 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5494 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5495 return false;
5496 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5497 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5498 return false;
5499 return true;
5500 }
5501
5503 SDValue Src = Op.getOperand(0);
5504 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5505 EVT SrcVT = Src.getValueType();
5506 if (SrcVT.isFixedLengthVector() && IndexC &&
5507 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5508 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5509 IndexC->getZExtValue());
5510 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5511 Depth + 1);
5512 }
5513 break;
5514 }
5515
5517 SDValue InVec = Op.getOperand(0);
5518 SDValue InVal = Op.getOperand(1);
5519 SDValue EltNo = Op.getOperand(2);
5520 EVT VT = InVec.getValueType();
5521 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5522 if (IndexC && VT.isFixedLengthVector() &&
5523 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5524 if (DemandedElts[IndexC->getZExtValue()] &&
5526 return false;
5527 APInt InVecDemandedElts = DemandedElts;
5528 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5529 if (!!InVecDemandedElts &&
5531 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5532 InVecDemandedElts, PoisonOnly, Depth + 1))
5533 return false;
5534 return true;
5535 }
5536 break;
5537 }
5538
5540 // Check upper (known undef) elements.
5541 if (DemandedElts.ugt(1) && !PoisonOnly)
5542 return false;
5543 // Check element zero.
5544 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5545 Op.getOperand(0), PoisonOnly, Depth + 1))
5546 return false;
5547 return true;
5548
5549 case ISD::SPLAT_VECTOR:
5550 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5551 Depth + 1);
5552
5553 case ISD::VECTOR_SHUFFLE: {
5554 APInt DemandedLHS, DemandedRHS;
5555 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5556 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5557 DemandedElts, DemandedLHS, DemandedRHS,
5558 /*AllowUndefElts=*/false))
5559 return false;
5560 if (!DemandedLHS.isZero() &&
5561 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5562 PoisonOnly, Depth + 1))
5563 return false;
5564 if (!DemandedRHS.isZero() &&
5565 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5566 PoisonOnly, Depth + 1))
5567 return false;
5568 return true;
5569 }
5570
5571 case ISD::SHL:
5572 case ISD::SRL:
5573 case ISD::SRA:
5574 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5575 // enough to check operand 0 if Op can't create undef/poison.
5576 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5577 /*ConsiderFlags*/ true, Depth) &&
5578 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5579 PoisonOnly, Depth + 1);
5580
5581 case ISD::BSWAP:
5582 case ISD::CTPOP:
5583 case ISD::BITREVERSE:
5584 case ISD::AND:
5585 case ISD::OR:
5586 case ISD::XOR:
5587 case ISD::ADD:
5588 case ISD::SUB:
5589 case ISD::MUL:
5590 case ISD::SADDSAT:
5591 case ISD::UADDSAT:
5592 case ISD::SSUBSAT:
5593 case ISD::USUBSAT:
5594 case ISD::SSHLSAT:
5595 case ISD::USHLSAT:
5596 case ISD::SMIN:
5597 case ISD::SMAX:
5598 case ISD::UMIN:
5599 case ISD::UMAX:
5600 case ISD::ZERO_EXTEND:
5601 case ISD::SIGN_EXTEND:
5602 case ISD::ANY_EXTEND:
5603 case ISD::TRUNCATE:
5604 case ISD::VSELECT: {
5605 // If Op can't create undef/poison and none of its operands are undef/poison
5606 // then Op is never undef/poison. A difference from the more common check
5607 // below, outside the switch, is that we handle elementwise operations for
5608 // which the DemandedElts mask is valid for all operands here.
5609 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5610 /*ConsiderFlags*/ true, Depth) &&
5611 all_of(Op->ops(), [&](SDValue V) {
5612 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5613 PoisonOnly, Depth + 1);
5614 });
5615 }
5616
5617 // TODO: Search for noundef attributes from library functions.
5618
5619 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5620
5621 default:
5622 // Allow the target to implement this method for its nodes.
5623 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5624 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5625 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5626 Op, DemandedElts, *this, PoisonOnly, Depth);
5627 break;
5628 }
5629
5630 // If Op can't create undef/poison and none of its operands are undef/poison
5631 // then Op is never undef/poison.
5632 // NOTE: TargetNodes can handle this in themselves in
5633 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5634 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5635 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5636 Depth) &&
5637 all_of(Op->ops(), [&](SDValue V) {
5638 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5639 });
5640}
5641
5643 bool ConsiderFlags,
5644 unsigned Depth) const {
5645 EVT VT = Op.getValueType();
5646 APInt DemandedElts = VT.isFixedLengthVector()
5648 : APInt(1, 1);
5649 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5650 Depth);
5651}
5652
5654 bool PoisonOnly, bool ConsiderFlags,
5655 unsigned Depth) const {
5656 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5657 return true;
5658
5659 unsigned Opcode = Op.getOpcode();
5660 switch (Opcode) {
5661 case ISD::AssertSext:
5662 case ISD::AssertZext:
5663 case ISD::AssertAlign:
5665 // Assertion nodes can create poison if the assertion fails.
5666 return true;
5667
5668 case ISD::FREEZE:
5672 case ISD::SADDSAT:
5673 case ISD::UADDSAT:
5674 case ISD::SSUBSAT:
5675 case ISD::USUBSAT:
5676 case ISD::MULHU:
5677 case ISD::MULHS:
5678 case ISD::AVGFLOORS:
5679 case ISD::AVGFLOORU:
5680 case ISD::AVGCEILS:
5681 case ISD::AVGCEILU:
5682 case ISD::ABDU:
5683 case ISD::ABDS:
5684 case ISD::SMIN:
5685 case ISD::SMAX:
5686 case ISD::SCMP:
5687 case ISD::UMIN:
5688 case ISD::UMAX:
5689 case ISD::UCMP:
5690 case ISD::AND:
5691 case ISD::XOR:
5692 case ISD::ROTL:
5693 case ISD::ROTR:
5694 case ISD::FSHL:
5695 case ISD::FSHR:
5696 case ISD::BSWAP:
5697 case ISD::CTTZ:
5698 case ISD::CTLZ:
5699 case ISD::CTLS:
5700 case ISD::CTPOP:
5701 case ISD::BITREVERSE:
5702 case ISD::PARITY:
5703 case ISD::SIGN_EXTEND:
5704 case ISD::TRUNCATE:
5708 case ISD::BITCAST:
5709 case ISD::BUILD_VECTOR:
5710 case ISD::BUILD_PAIR:
5711 case ISD::SPLAT_VECTOR:
5712 case ISD::FABS:
5713 return false;
5714
5715 case ISD::ABS:
5716 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5717 // Different to Intrinsic::abs.
5718 return false;
5719
5720 case ISD::ADDC:
5721 case ISD::SUBC:
5722 case ISD::ADDE:
5723 case ISD::SUBE:
5724 case ISD::SADDO:
5725 case ISD::SSUBO:
5726 case ISD::SMULO:
5727 case ISD::SADDO_CARRY:
5728 case ISD::SSUBO_CARRY:
5729 case ISD::UADDO:
5730 case ISD::USUBO:
5731 case ISD::UMULO:
5732 case ISD::UADDO_CARRY:
5733 case ISD::USUBO_CARRY:
5734 // No poison on result or overflow flags.
5735 return false;
5736
5737 case ISD::SELECT_CC:
5738 case ISD::SETCC: {
5739 // Integer setcc cannot create undef or poison.
5740 if (Op.getOperand(0).getValueType().isInteger())
5741 return false;
5742
5743 // FP compares are more complicated. They can create poison for nan/infinity
5744 // based on options and flags. The options and flags also cause special
5745 // nonan condition codes to be used. Those condition codes may be preserved
5746 // even if the nonan flag is dropped somewhere.
5747 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5748 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5749 return (unsigned)CCCode & 0x10U;
5750 }
5751
5752 case ISD::OR:
5753 case ISD::ZERO_EXTEND:
5754 case ISD::SELECT:
5755 case ISD::VSELECT:
5756 case ISD::ADD:
5757 case ISD::SUB:
5758 case ISD::MUL:
5759 case ISD::FNEG:
5760 case ISD::FADD:
5761 case ISD::FSUB:
5762 case ISD::FMUL:
5763 case ISD::FDIV:
5764 case ISD::FREM:
5765 case ISD::FCOPYSIGN:
5766 case ISD::FMA:
5767 case ISD::FMAD:
5768 case ISD::FMULADD:
5769 case ISD::FP_EXTEND:
5775 // No poison except from flags (which is handled above)
5776 return false;
5777
5778 case ISD::SHL:
5779 case ISD::SRL:
5780 case ISD::SRA:
5781 // If the max shift amount isn't in range, then the shift can
5782 // create poison.
5783 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5784
5787 // If the amount is zero then the result will be poison.
5788 // TODO: Add isKnownNeverZero DemandedElts handling.
5789 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5790
5792 // Check if we demand any upper (undef) elements.
5793 return !PoisonOnly && DemandedElts.ugt(1);
5794
5797 // Ensure that the element index is in bounds.
5798 EVT VecVT = Op.getOperand(0).getValueType();
5799 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5800 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5801 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5802 }
5803
5804 case ISD::VECTOR_SHUFFLE: {
5805 // Check for any demanded shuffle element that is undef.
5806 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5807 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5808 if (Elt < 0 && DemandedElts[Idx])
5809 return true;
5810 return false;
5811 }
5812
5814 return false;
5815
5816 default:
5817 // Allow the target to implement this method for its nodes.
5818 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5819 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5820 return TLI->canCreateUndefOrPoisonForTargetNode(
5821 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5822 break;
5823 }
5824
5825 // Be conservative and return true.
5826 return true;
5827}
5828
5829bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5830 unsigned Opcode = Op.getOpcode();
5831 if (Opcode == ISD::OR)
5832 return Op->getFlags().hasDisjoint() ||
5833 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5834 if (Opcode == ISD::XOR)
5835 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5836 return false;
5837}
5838
5840 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5841 (Op.isAnyAdd() || isADDLike(Op));
5842}
5843
5845 unsigned Depth) const {
5846 EVT VT = Op.getValueType();
5847
5848 // Since the number of lanes in a scalable vector is unknown at compile time,
5849 // we track one bit which is implicitly broadcast to all lanes. This means
5850 // that all lanes in a scalable vector are considered demanded.
5851 APInt DemandedElts = VT.isFixedLengthVector()
5853 : APInt(1, 1);
5854
5855 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5856}
5857
5859 bool SNaN, unsigned Depth) const {
5860 assert(!DemandedElts.isZero() && "No demanded elements");
5861
5862 // If we're told that NaNs won't happen, assume they won't.
5863 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5864 return true;
5865
5866 if (Depth >= MaxRecursionDepth)
5867 return false; // Limit search depth.
5868
5869 // If the value is a constant, we can obviously see if it is a NaN or not.
5871 return !C->getValueAPF().isNaN() ||
5872 (SNaN && !C->getValueAPF().isSignaling());
5873 }
5874
5875 unsigned Opcode = Op.getOpcode();
5876 switch (Opcode) {
5877 case ISD::FADD:
5878 case ISD::FSUB:
5879 case ISD::FMUL:
5880 case ISD::FDIV:
5881 case ISD::FREM:
5882 case ISD::FSIN:
5883 case ISD::FCOS:
5884 case ISD::FTAN:
5885 case ISD::FASIN:
5886 case ISD::FACOS:
5887 case ISD::FATAN:
5888 case ISD::FATAN2:
5889 case ISD::FSINH:
5890 case ISD::FCOSH:
5891 case ISD::FTANH:
5892 case ISD::FMA:
5893 case ISD::FMULADD:
5894 case ISD::FMAD: {
5895 if (SNaN)
5896 return true;
5897 // TODO: Need isKnownNeverInfinity
5898 return false;
5899 }
5900 case ISD::FCANONICALIZE:
5901 case ISD::FEXP:
5902 case ISD::FEXP2:
5903 case ISD::FEXP10:
5904 case ISD::FTRUNC:
5905 case ISD::FFLOOR:
5906 case ISD::FCEIL:
5907 case ISD::FROUND:
5908 case ISD::FROUNDEVEN:
5909 case ISD::LROUND:
5910 case ISD::LLROUND:
5911 case ISD::FRINT:
5912 case ISD::LRINT:
5913 case ISD::LLRINT:
5914 case ISD::FNEARBYINT:
5915 case ISD::FLDEXP: {
5916 if (SNaN)
5917 return true;
5918 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5919 }
5920 case ISD::FABS:
5921 case ISD::FNEG:
5922 case ISD::FCOPYSIGN: {
5923 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5924 }
5925 case ISD::SELECT:
5926 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5927 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5928 case ISD::FP_EXTEND:
5929 case ISD::FP_ROUND: {
5930 if (SNaN)
5931 return true;
5932 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5933 }
5934 case ISD::SINT_TO_FP:
5935 case ISD::UINT_TO_FP:
5936 return true;
5937 case ISD::FSQRT: // Need is known positive
5938 case ISD::FLOG:
5939 case ISD::FLOG2:
5940 case ISD::FLOG10:
5941 case ISD::FPOWI:
5942 case ISD::FPOW: {
5943 if (SNaN)
5944 return true;
5945 // TODO: Refine on operand
5946 return false;
5947 }
5948 case ISD::FMINNUM:
5949 case ISD::FMAXNUM:
5950 case ISD::FMINIMUMNUM:
5951 case ISD::FMAXIMUMNUM: {
5952 // Only one needs to be known not-nan, since it will be returned if the
5953 // other ends up being one.
5954 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5955 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5956 }
5957 case ISD::FMINNUM_IEEE:
5958 case ISD::FMAXNUM_IEEE: {
5959 if (SNaN)
5960 return true;
5961 // This can return a NaN if either operand is an sNaN, or if both operands
5962 // are NaN.
5963 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5964 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5965 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5966 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5967 }
5968 case ISD::FMINIMUM:
5969 case ISD::FMAXIMUM: {
5970 // TODO: Does this quiet or return the origina NaN as-is?
5971 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5972 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5973 }
5975 SDValue Src = Op.getOperand(0);
5976 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5977 EVT SrcVT = Src.getValueType();
5978 if (SrcVT.isFixedLengthVector() && Idx &&
5979 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5980 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5981 Idx->getZExtValue());
5982 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5983 }
5984 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5985 }
5987 SDValue Src = Op.getOperand(0);
5988 if (Src.getValueType().isFixedLengthVector()) {
5989 unsigned Idx = Op.getConstantOperandVal(1);
5990 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5991 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5992 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5993 }
5994 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5995 }
5996 case ISD::INSERT_SUBVECTOR: {
5997 SDValue BaseVector = Op.getOperand(0);
5998 SDValue SubVector = Op.getOperand(1);
5999 EVT BaseVectorVT = BaseVector.getValueType();
6000 if (BaseVectorVT.isFixedLengthVector()) {
6001 unsigned Idx = Op.getConstantOperandVal(2);
6002 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6003 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6004
6005 // Clear/Extract the bits at the position where the subvector will be
6006 // inserted.
6007 APInt DemandedMask =
6008 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6009 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6010 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6011
6012 bool NeverNaN = true;
6013 if (!DemandedSrcElts.isZero())
6014 NeverNaN &=
6015 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6016 if (NeverNaN && !DemandedSubElts.isZero())
6017 NeverNaN &=
6018 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6019 return NeverNaN;
6020 }
6021 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6022 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6023 }
6024 case ISD::BUILD_VECTOR: {
6025 unsigned NumElts = Op.getNumOperands();
6026 for (unsigned I = 0; I != NumElts; ++I)
6027 if (DemandedElts[I] &&
6028 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6029 return false;
6030 return true;
6031 }
6032 case ISD::AssertNoFPClass: {
6033 FPClassTest NoFPClass =
6034 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6035 if ((NoFPClass & fcNan) == fcNan)
6036 return true;
6037 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6038 return true;
6039 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6040 }
6041 default:
6042 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6043 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6044 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6045 Depth);
6046 }
6047
6048 return false;
6049 }
6050}
6051
6053 assert(Op.getValueType().isFloatingPoint() &&
6054 "Floating point type expected");
6055
6056 // If the value is a constant, we can obviously see if it is a zero or not.
6058 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6059}
6060
6062 if (Depth >= MaxRecursionDepth)
6063 return false; // Limit search depth.
6064
6065 assert(!Op.getValueType().isFloatingPoint() &&
6066 "Floating point types unsupported - use isKnownNeverZeroFloat");
6067
6068 // If the value is a constant, we can obviously see if it is a zero or not.
6070 [](ConstantSDNode *C) { return !C->isZero(); }))
6071 return true;
6072
6073 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6074 // some degree.
6075 switch (Op.getOpcode()) {
6076 default:
6077 break;
6078
6079 case ISD::OR:
6080 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6081 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6082
6083 case ISD::VSELECT:
6084 case ISD::SELECT:
6085 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6086 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6087
6088 case ISD::SHL: {
6089 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6090 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6091 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6092 // 1 << X is never zero.
6093 if (ValKnown.One[0])
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.shl(MaxCnt).isZero())
6099 return true;
6100 break;
6101 }
6102 case ISD::UADDSAT:
6103 case ISD::UMAX:
6104 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6105 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6106
6107 // For smin/smax: If either operand is known negative/positive
6108 // respectively we don't need the other to be known at all.
6109 case ISD::SMAX: {
6110 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6111 if (Op1.isStrictlyPositive())
6112 return true;
6113
6114 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6115 if (Op0.isStrictlyPositive())
6116 return true;
6117
6118 if (Op1.isNonZero() && Op0.isNonZero())
6119 return true;
6120
6121 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6122 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6123 }
6124 case ISD::SMIN: {
6125 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6126 if (Op1.isNegative())
6127 return true;
6128
6129 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6130 if (Op0.isNegative())
6131 return true;
6132
6133 if (Op1.isNonZero() && Op0.isNonZero())
6134 return true;
6135
6136 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6137 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6138 }
6139 case ISD::UMIN:
6140 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6141 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6142
6143 case ISD::ROTL:
6144 case ISD::ROTR:
6145 case ISD::BITREVERSE:
6146 case ISD::BSWAP:
6147 case ISD::CTPOP:
6148 case ISD::ABS:
6149 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6150
6151 case ISD::SRA:
6152 case ISD::SRL: {
6153 if (Op->getFlags().hasExact())
6154 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6155 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6156 if (ValKnown.isNegative())
6157 return true;
6158 // If max shift cnt of known ones is non-zero, result is non-zero.
6159 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6160 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6161 !ValKnown.One.lshr(MaxCnt).isZero())
6162 return true;
6163 break;
6164 }
6165 case ISD::UDIV:
6166 case ISD::SDIV:
6167 // div exact can only produce a zero if the dividend is zero.
6168 // TODO: For udiv this is also true if Op1 u<= Op0
6169 if (Op->getFlags().hasExact())
6170 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6171 break;
6172
6173 case ISD::ADD:
6174 if (Op->getFlags().hasNoUnsignedWrap())
6175 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6176 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6177 return true;
6178 // TODO: There are a lot more cases we can prove for add.
6179 break;
6180
6181 case ISD::SUB: {
6182 if (isNullConstant(Op.getOperand(0)))
6183 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6184
6185 std::optional<bool> ne =
6186 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6187 computeKnownBits(Op.getOperand(1), Depth + 1));
6188 return ne && *ne;
6189 }
6190
6191 case ISD::MUL:
6192 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6193 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6194 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6195 return true;
6196 break;
6197
6198 case ISD::ZERO_EXTEND:
6199 case ISD::SIGN_EXTEND:
6200 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6201 case ISD::VSCALE: {
6203 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6204 ConstantRange CR =
6205 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6206 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6207 return true;
6208 break;
6209 }
6210 }
6211
6213}
6214
6216 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6217 return !C1->isNegative();
6218
6219 switch (Op.getOpcode()) {
6220 case ISD::FABS:
6221 case ISD::FEXP:
6222 case ISD::FEXP2:
6223 case ISD::FEXP10:
6224 return true;
6225 default:
6226 return false;
6227 }
6228
6229 llvm_unreachable("covered opcode switch");
6230}
6231
6233 assert(Use.getValueType().isFloatingPoint());
6234 const SDNode *User = Use.getUser();
6235 unsigned OperandNo = Use.getOperandNo();
6236 // Check if this use is insensitive to the sign of zero
6237 switch (User->getOpcode()) {
6238 case ISD::SETCC:
6239 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6240 case ISD::FABS:
6241 // fabs always produces +0.0.
6242 return true;
6243 case ISD::FCOPYSIGN:
6244 // copysign overwrites the sign bit of the first operand.
6245 return OperandNo == 0;
6246 case ISD::FADD:
6247 case ISD::FSUB: {
6248 // Arithmetic with non-zero constants fixes the uncertainty around the
6249 // sign bit.
6250 SDValue Other = User->getOperand(1 - OperandNo);
6252 }
6253 case ISD::FP_TO_SINT:
6254 case ISD::FP_TO_UINT:
6255 // fp-to-int conversions normalize signed zeros.
6256 return true;
6257 default:
6258 return false;
6259 }
6260}
6261
6263 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6264 // regression. Ideally, this should be implemented as a demanded-bits
6265 // optimization that stems from the users.
6266 if (Op->use_size() > 2)
6267 return false;
6268 return all_of(Op->uses(),
6269 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6270}
6271
6273 // Check the obvious case.
6274 if (A == B) return true;
6275
6276 // For negative and positive zero.
6279 if (CA->isZero() && CB->isZero()) return true;
6280
6281 // Otherwise they may not be equal.
6282 return false;
6283}
6284
6285// Only bits set in Mask must be negated, other bits may be arbitrary.
6287 if (isBitwiseNot(V, AllowUndefs))
6288 return V.getOperand(0);
6289
6290 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6291 // bits in the non-extended part.
6292 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6293 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6294 return SDValue();
6295 SDValue ExtArg = V.getOperand(0);
6296 if (ExtArg.getScalarValueSizeInBits() >=
6297 MaskC->getAPIntValue().getActiveBits() &&
6298 isBitwiseNot(ExtArg, AllowUndefs) &&
6299 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6300 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6301 return ExtArg.getOperand(0).getOperand(0);
6302 return SDValue();
6303}
6304
6306 // Match masked merge pattern (X & ~M) op (Y & M)
6307 // Including degenerate case (X & ~M) op M
6308 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6309 SDValue Other) {
6310 if (SDValue NotOperand =
6311 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6312 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6313 NotOperand->getOpcode() == ISD::TRUNCATE)
6314 NotOperand = NotOperand->getOperand(0);
6315
6316 if (Other == NotOperand)
6317 return true;
6318 if (Other->getOpcode() == ISD::AND)
6319 return NotOperand == Other->getOperand(0) ||
6320 NotOperand == Other->getOperand(1);
6321 }
6322 return false;
6323 };
6324
6325 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6326 A = A->getOperand(0);
6327
6328 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6329 B = B->getOperand(0);
6330
6331 if (A->getOpcode() == ISD::AND)
6332 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6333 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6334 return false;
6335}
6336
6337// FIXME: unify with llvm::haveNoCommonBitsSet.
6339 assert(A.getValueType() == B.getValueType() &&
6340 "Values must have the same type");
6343 return true;
6346}
6347
6348static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6349 SelectionDAG &DAG) {
6350 if (cast<ConstantSDNode>(Step)->isZero())
6351 return DAG.getConstant(0, DL, VT);
6352
6353 return SDValue();
6354}
6355
6358 SelectionDAG &DAG) {
6359 int NumOps = Ops.size();
6360 assert(NumOps != 0 && "Can't build an empty vector!");
6361 assert(!VT.isScalableVector() &&
6362 "BUILD_VECTOR cannot be used with scalable types");
6363 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6364 "Incorrect element count in BUILD_VECTOR!");
6365
6366 // BUILD_VECTOR of UNDEFs is UNDEF.
6367 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6368 return DAG.getUNDEF(VT);
6369
6370 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6371 SDValue IdentitySrc;
6372 bool IsIdentity = true;
6373 for (int i = 0; i != NumOps; ++i) {
6375 Ops[i].getOperand(0).getValueType() != VT ||
6376 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6377 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6378 Ops[i].getConstantOperandAPInt(1) != i) {
6379 IsIdentity = false;
6380 break;
6381 }
6382 IdentitySrc = Ops[i].getOperand(0);
6383 }
6384 if (IsIdentity)
6385 return IdentitySrc;
6386
6387 return SDValue();
6388}
6389
6390/// Try to simplify vector concatenation to an input value, undef, or build
6391/// vector.
6394 SelectionDAG &DAG) {
6395 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6397 [Ops](SDValue Op) {
6398 return Ops[0].getValueType() == Op.getValueType();
6399 }) &&
6400 "Concatenation of vectors with inconsistent value types!");
6401 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6402 VT.getVectorElementCount() &&
6403 "Incorrect element count in vector concatenation!");
6404
6405 if (Ops.size() == 1)
6406 return Ops[0];
6407
6408 // Concat of UNDEFs is UNDEF.
6409 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6410 return DAG.getUNDEF(VT);
6411
6412 // Scan the operands and look for extract operations from a single source
6413 // that correspond to insertion at the same location via this concatenation:
6414 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6415 SDValue IdentitySrc;
6416 bool IsIdentity = true;
6417 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6418 SDValue Op = Ops[i];
6419 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6420 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6421 Op.getOperand(0).getValueType() != VT ||
6422 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6423 Op.getConstantOperandVal(1) != IdentityIndex) {
6424 IsIdentity = false;
6425 break;
6426 }
6427 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6428 "Unexpected identity source vector for concat of extracts");
6429 IdentitySrc = Op.getOperand(0);
6430 }
6431 if (IsIdentity) {
6432 assert(IdentitySrc && "Failed to set source vector of extracts");
6433 return IdentitySrc;
6434 }
6435
6436 // The code below this point is only designed to work for fixed width
6437 // vectors, so we bail out for now.
6438 if (VT.isScalableVector())
6439 return SDValue();
6440
6441 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6442 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6443 // BUILD_VECTOR.
6444 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6445 EVT SVT = VT.getScalarType();
6447 for (SDValue Op : Ops) {
6448 EVT OpVT = Op.getValueType();
6449 if (Op.isUndef())
6450 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6451 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6452 Elts.append(Op->op_begin(), Op->op_end());
6453 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6454 OpVT.getVectorNumElements() == 1 &&
6455 isNullConstant(Op.getOperand(2)))
6456 Elts.push_back(Op.getOperand(1));
6457 else
6458 return SDValue();
6459 }
6460
6461 // BUILD_VECTOR requires all inputs to be of the same type, find the
6462 // maximum type and extend them all.
6463 for (SDValue Op : Elts)
6464 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6465
6466 if (SVT.bitsGT(VT.getScalarType())) {
6467 for (SDValue &Op : Elts) {
6468 if (Op.isUndef())
6469 Op = DAG.getUNDEF(SVT);
6470 else
6471 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6472 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6473 : DAG.getSExtOrTrunc(Op, DL, SVT);
6474 }
6475 }
6476
6477 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6478 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6479 return V;
6480}
6481
6482/// Gets or creates the specified node.
6483SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6484 SDVTList VTs = getVTList(VT);
6486 AddNodeIDNode(ID, Opcode, VTs, {});
6487 void *IP = nullptr;
6488 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6489 return SDValue(E, 0);
6490
6491 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6492 CSEMap.InsertNode(N, IP);
6493
6494 InsertNode(N);
6495 SDValue V = SDValue(N, 0);
6496 NewSDValueDbgMsg(V, "Creating new node: ", this);
6497 return V;
6498}
6499
6500SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6501 SDValue N1) {
6502 SDNodeFlags Flags;
6503 if (Inserter)
6504 Flags = Inserter->getFlags();
6505 return getNode(Opcode, DL, VT, N1, Flags);
6506}
6507
6508SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6509 SDValue N1, const SDNodeFlags Flags) {
6510 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6511
6512 // Constant fold unary operations with a vector integer or float operand.
6513 switch (Opcode) {
6514 default:
6515 // FIXME: Entirely reasonable to perform folding of other unary
6516 // operations here as the need arises.
6517 break;
6518 case ISD::FNEG:
6519 case ISD::FABS:
6520 case ISD::FCEIL:
6521 case ISD::FTRUNC:
6522 case ISD::FFLOOR:
6523 case ISD::FP_EXTEND:
6524 case ISD::FP_TO_SINT:
6525 case ISD::FP_TO_UINT:
6526 case ISD::FP_TO_FP16:
6527 case ISD::FP_TO_BF16:
6528 case ISD::TRUNCATE:
6529 case ISD::ANY_EXTEND:
6530 case ISD::ZERO_EXTEND:
6531 case ISD::SIGN_EXTEND:
6532 case ISD::UINT_TO_FP:
6533 case ISD::SINT_TO_FP:
6534 case ISD::FP16_TO_FP:
6535 case ISD::BF16_TO_FP:
6536 case ISD::BITCAST:
6537 case ISD::ABS:
6538 case ISD::BITREVERSE:
6539 case ISD::BSWAP:
6540 case ISD::CTLZ:
6542 case ISD::CTTZ:
6544 case ISD::CTPOP:
6545 case ISD::STEP_VECTOR: {
6546 SDValue Ops = {N1};
6547 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6548 return Fold;
6549 }
6550 }
6551
6552 unsigned OpOpcode = N1.getNode()->getOpcode();
6553 switch (Opcode) {
6554 case ISD::STEP_VECTOR:
6555 assert(VT.isScalableVector() &&
6556 "STEP_VECTOR can only be used with scalable types");
6557 assert(OpOpcode == ISD::TargetConstant &&
6558 VT.getVectorElementType() == N1.getValueType() &&
6559 "Unexpected step operand");
6560 break;
6561 case ISD::FREEZE:
6562 assert(VT == N1.getValueType() && "Unexpected VT!");
6563 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6564 return N1;
6565 break;
6566 case ISD::TokenFactor:
6567 case ISD::MERGE_VALUES:
6569 return N1; // Factor, merge or concat of one node? No need.
6570 case ISD::BUILD_VECTOR: {
6571 // Attempt to simplify BUILD_VECTOR.
6572 SDValue Ops[] = {N1};
6573 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6574 return V;
6575 break;
6576 }
6577 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6578 case ISD::FP_EXTEND:
6580 "Invalid FP cast!");
6581 if (N1.getValueType() == VT) return N1; // noop conversion.
6582 assert((!VT.isVector() || VT.getVectorElementCount() ==
6584 "Vector element count mismatch!");
6585 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6586 if (N1.isUndef())
6587 return getUNDEF(VT);
6588 break;
6589 case ISD::FP_TO_SINT:
6590 case ISD::FP_TO_UINT:
6591 if (N1.isUndef())
6592 return getUNDEF(VT);
6593 break;
6594 case ISD::SINT_TO_FP:
6595 case ISD::UINT_TO_FP:
6596 // [us]itofp(undef) = 0, because the result value is bounded.
6597 if (N1.isUndef())
6598 return getConstantFP(0.0, DL, VT);
6599 break;
6600 case ISD::SIGN_EXTEND:
6601 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6602 "Invalid SIGN_EXTEND!");
6603 assert(VT.isVector() == N1.getValueType().isVector() &&
6604 "SIGN_EXTEND result type type should be vector iff the operand "
6605 "type is vector!");
6606 if (N1.getValueType() == VT) return N1; // noop extension
6607 assert((!VT.isVector() || VT.getVectorElementCount() ==
6609 "Vector element count mismatch!");
6610 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6611 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6612 SDNodeFlags Flags;
6613 if (OpOpcode == ISD::ZERO_EXTEND)
6614 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6615 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6616 transferDbgValues(N1, NewVal);
6617 return NewVal;
6618 }
6619
6620 if (OpOpcode == ISD::POISON)
6621 return getPOISON(VT);
6622
6623 if (N1.isUndef())
6624 // sext(undef) = 0, because the top bits will all be the same.
6625 return getConstant(0, DL, VT);
6626
6627 // Skip unnecessary sext_inreg pattern:
6628 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6629 if (OpOpcode == ISD::TRUNCATE) {
6630 SDValue OpOp = N1.getOperand(0);
6631 if (OpOp.getValueType() == VT) {
6632 unsigned NumSignExtBits =
6634 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6635 transferDbgValues(N1, OpOp);
6636 return OpOp;
6637 }
6638 }
6639 }
6640 break;
6641 case ISD::ZERO_EXTEND:
6642 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6643 "Invalid ZERO_EXTEND!");
6644 assert(VT.isVector() == N1.getValueType().isVector() &&
6645 "ZERO_EXTEND result type type should be vector iff the operand "
6646 "type is vector!");
6647 if (N1.getValueType() == VT) return N1; // noop extension
6648 assert((!VT.isVector() || VT.getVectorElementCount() ==
6650 "Vector element count mismatch!");
6651 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6652 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6653 SDNodeFlags Flags;
6654 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6655 SDValue NewVal =
6656 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6657 transferDbgValues(N1, NewVal);
6658 return NewVal;
6659 }
6660
6661 if (OpOpcode == ISD::POISON)
6662 return getPOISON(VT);
6663
6664 if (N1.isUndef())
6665 // zext(undef) = 0, because the top bits will be zero.
6666 return getConstant(0, DL, VT);
6667
6668 // Skip unnecessary zext_inreg pattern:
6669 // (zext (trunc x)) -> x iff the upper bits are known zero.
6670 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6671 // use to recognise zext_inreg patterns.
6672 if (OpOpcode == ISD::TRUNCATE) {
6673 SDValue OpOp = N1.getOperand(0);
6674 if (OpOp.getValueType() == VT) {
6675 if (OpOp.getOpcode() != ISD::AND) {
6678 if (MaskedValueIsZero(OpOp, HiBits)) {
6679 transferDbgValues(N1, OpOp);
6680 return OpOp;
6681 }
6682 }
6683 }
6684 }
6685 break;
6686 case ISD::ANY_EXTEND:
6687 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6688 "Invalid ANY_EXTEND!");
6689 assert(VT.isVector() == N1.getValueType().isVector() &&
6690 "ANY_EXTEND result type type should be vector iff the operand "
6691 "type is vector!");
6692 if (N1.getValueType() == VT) return N1; // noop extension
6693 assert((!VT.isVector() || VT.getVectorElementCount() ==
6695 "Vector element count mismatch!");
6696 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6697
6698 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6699 OpOpcode == ISD::ANY_EXTEND) {
6700 SDNodeFlags Flags;
6701 if (OpOpcode == ISD::ZERO_EXTEND)
6702 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6703 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6704 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6705 }
6706 if (N1.isUndef())
6707 return getUNDEF(VT);
6708
6709 // (ext (trunc x)) -> x
6710 if (OpOpcode == ISD::TRUNCATE) {
6711 SDValue OpOp = N1.getOperand(0);
6712 if (OpOp.getValueType() == VT) {
6713 transferDbgValues(N1, OpOp);
6714 return OpOp;
6715 }
6716 }
6717 break;
6718 case ISD::TRUNCATE:
6719 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6720 "Invalid TRUNCATE!");
6721 assert(VT.isVector() == N1.getValueType().isVector() &&
6722 "TRUNCATE result type type should be vector iff the operand "
6723 "type is vector!");
6724 if (N1.getValueType() == VT) return N1; // noop truncate
6725 assert((!VT.isVector() || VT.getVectorElementCount() ==
6727 "Vector element count mismatch!");
6728 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6729 if (OpOpcode == ISD::TRUNCATE)
6730 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6731 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6732 OpOpcode == ISD::ANY_EXTEND) {
6733 // If the source is smaller than the dest, we still need an extend.
6735 VT.getScalarType())) {
6736 SDNodeFlags Flags;
6737 if (OpOpcode == ISD::ZERO_EXTEND)
6738 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6739 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6740 }
6741 if (N1.getOperand(0).getValueType().bitsGT(VT))
6742 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6743 return N1.getOperand(0);
6744 }
6745 if (N1.isUndef())
6746 return getUNDEF(VT);
6747 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6748 return getVScale(DL, VT,
6750 break;
6754 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6755 assert(N1.getValueType().bitsLE(VT) &&
6756 "The input must be the same size or smaller than the result.");
6759 "The destination vector type must have fewer lanes than the input.");
6760 break;
6761 case ISD::ABS:
6762 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6763 if (N1.isUndef())
6764 return getConstant(0, DL, VT);
6765 break;
6766 case ISD::BSWAP:
6767 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6768 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6769 "BSWAP types must be a multiple of 16 bits!");
6770 if (N1.isUndef())
6771 return getUNDEF(VT);
6772 // bswap(bswap(X)) -> X.
6773 if (OpOpcode == ISD::BSWAP)
6774 return N1.getOperand(0);
6775 break;
6776 case ISD::BITREVERSE:
6777 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6778 if (N1.isUndef())
6779 return getUNDEF(VT);
6780 break;
6781 case ISD::BITCAST:
6783 "Cannot BITCAST between types of different sizes!");
6784 if (VT == N1.getValueType()) return N1; // noop conversion.
6785 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6786 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6787 if (N1.isUndef())
6788 return getUNDEF(VT);
6789 break;
6791 assert(VT.isVector() && !N1.getValueType().isVector() &&
6792 (VT.getVectorElementType() == N1.getValueType() ||
6794 N1.getValueType().isInteger() &&
6796 "Illegal SCALAR_TO_VECTOR node!");
6797 if (N1.isUndef())
6798 return getUNDEF(VT);
6799 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6800 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6802 N1.getConstantOperandVal(1) == 0 &&
6803 N1.getOperand(0).getValueType() == VT)
6804 return N1.getOperand(0);
6805 break;
6806 case ISD::FNEG:
6807 // Negation of an unknown bag of bits is still completely undefined.
6808 if (N1.isUndef())
6809 return getUNDEF(VT);
6810
6811 if (OpOpcode == ISD::FNEG) // --X -> X
6812 return N1.getOperand(0);
6813 break;
6814 case ISD::FABS:
6815 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6816 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6817 break;
6818 case ISD::VSCALE:
6819 assert(VT == N1.getValueType() && "Unexpected VT!");
6820 break;
6821 case ISD::CTPOP:
6822 if (N1.getValueType().getScalarType() == MVT::i1)
6823 return N1;
6824 break;
6825 case ISD::CTLZ:
6826 case ISD::CTTZ:
6827 if (N1.getValueType().getScalarType() == MVT::i1)
6828 return getNOT(DL, N1, N1.getValueType());
6829 break;
6830 case ISD::VECREDUCE_ADD:
6831 if (N1.getValueType().getScalarType() == MVT::i1)
6832 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6833 break;
6836 if (N1.getValueType().getScalarType() == MVT::i1)
6837 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6838 break;
6841 if (N1.getValueType().getScalarType() == MVT::i1)
6842 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6843 break;
6844 case ISD::SPLAT_VECTOR:
6845 assert(VT.isVector() && "Wrong return type!");
6846 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6847 // that for now.
6849 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6851 N1.getValueType().isInteger() &&
6853 "Wrong operand type!");
6854 break;
6855 }
6856
6857 SDNode *N;
6858 SDVTList VTs = getVTList(VT);
6859 SDValue Ops[] = {N1};
6860 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6862 AddNodeIDNode(ID, Opcode, VTs, Ops);
6863 void *IP = nullptr;
6864 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6865 E->intersectFlagsWith(Flags);
6866 return SDValue(E, 0);
6867 }
6868
6869 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6870 N->setFlags(Flags);
6871 createOperands(N, Ops);
6872 CSEMap.InsertNode(N, IP);
6873 } else {
6874 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6875 createOperands(N, Ops);
6876 }
6877
6878 InsertNode(N);
6879 SDValue V = SDValue(N, 0);
6880 NewSDValueDbgMsg(V, "Creating new node: ", this);
6881 return V;
6882}
6883
6884static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6885 const APInt &C2) {
6886 switch (Opcode) {
6887 case ISD::ADD: return C1 + C2;
6888 case ISD::SUB: return C1 - C2;
6889 case ISD::MUL: return C1 * C2;
6890 case ISD::AND: return C1 & C2;
6891 case ISD::OR: return C1 | C2;
6892 case ISD::XOR: return C1 ^ C2;
6893 case ISD::SHL: return C1 << C2;
6894 case ISD::SRL: return C1.lshr(C2);
6895 case ISD::SRA: return C1.ashr(C2);
6896 case ISD::ROTL: return C1.rotl(C2);
6897 case ISD::ROTR: return C1.rotr(C2);
6898 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6899 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6900 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6901 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6902 case ISD::SADDSAT: return C1.sadd_sat(C2);
6903 case ISD::UADDSAT: return C1.uadd_sat(C2);
6904 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6905 case ISD::USUBSAT: return C1.usub_sat(C2);
6906 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6907 case ISD::USHLSAT: return C1.ushl_sat(C2);
6908 case ISD::UDIV:
6909 if (!C2.getBoolValue())
6910 break;
6911 return C1.udiv(C2);
6912 case ISD::UREM:
6913 if (!C2.getBoolValue())
6914 break;
6915 return C1.urem(C2);
6916 case ISD::SDIV:
6917 if (!C2.getBoolValue())
6918 break;
6919 return C1.sdiv(C2);
6920 case ISD::SREM:
6921 if (!C2.getBoolValue())
6922 break;
6923 return C1.srem(C2);
6924 case ISD::AVGFLOORS:
6925 return APIntOps::avgFloorS(C1, C2);
6926 case ISD::AVGFLOORU:
6927 return APIntOps::avgFloorU(C1, C2);
6928 case ISD::AVGCEILS:
6929 return APIntOps::avgCeilS(C1, C2);
6930 case ISD::AVGCEILU:
6931 return APIntOps::avgCeilU(C1, C2);
6932 case ISD::ABDS:
6933 return APIntOps::abds(C1, C2);
6934 case ISD::ABDU:
6935 return APIntOps::abdu(C1, C2);
6936 case ISD::MULHS:
6937 return APIntOps::mulhs(C1, C2);
6938 case ISD::MULHU:
6939 return APIntOps::mulhu(C1, C2);
6940 case ISD::CLMUL:
6941 return APIntOps::clmul(C1, C2);
6942 case ISD::CLMULR:
6943 return APIntOps::clmulr(C1, C2);
6944 case ISD::CLMULH:
6945 return APIntOps::clmulh(C1, C2);
6946 }
6947 return std::nullopt;
6948}
6949// Handle constant folding with UNDEF.
6950// TODO: Handle more cases.
6951static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6952 bool IsUndef1, const APInt &C2,
6953 bool IsUndef2) {
6954 if (!(IsUndef1 || IsUndef2))
6955 return FoldValue(Opcode, C1, C2);
6956
6957 // Fold and(x, undef) -> 0
6958 // Fold mul(x, undef) -> 0
6959 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6960 return APInt::getZero(C1.getBitWidth());
6961
6962 return std::nullopt;
6963}
6964
6966 const GlobalAddressSDNode *GA,
6967 const SDNode *N2) {
6968 if (GA->getOpcode() != ISD::GlobalAddress)
6969 return SDValue();
6970 if (!TLI->isOffsetFoldingLegal(GA))
6971 return SDValue();
6972 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6973 if (!C2)
6974 return SDValue();
6975 int64_t Offset = C2->getSExtValue();
6976 switch (Opcode) {
6977 case ISD::ADD:
6978 case ISD::PTRADD:
6979 break;
6980 case ISD::SUB: Offset = -uint64_t(Offset); break;
6981 default: return SDValue();
6982 }
6983 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6984 GA->getOffset() + uint64_t(Offset));
6985}
6986
6988 switch (Opcode) {
6989 case ISD::SDIV:
6990 case ISD::UDIV:
6991 case ISD::SREM:
6992 case ISD::UREM: {
6993 // If a divisor is zero/undef or any element of a divisor vector is
6994 // zero/undef, the whole op is undef.
6995 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6996 SDValue Divisor = Ops[1];
6997 if (Divisor.isUndef() || isNullConstant(Divisor))
6998 return true;
6999
7000 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7001 llvm::any_of(Divisor->op_values(),
7002 [](SDValue V) { return V.isUndef() ||
7003 isNullConstant(V); });
7004 // TODO: Handle signed overflow.
7005 }
7006 // TODO: Handle oversized shifts.
7007 default:
7008 return false;
7009 }
7010}
7011
7014 SDNodeFlags Flags) {
7015 // If the opcode is a target-specific ISD node, there's nothing we can
7016 // do here and the operand rules may not line up with the below, so
7017 // bail early.
7018 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7019 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7020 // foldCONCAT_VECTORS in getNode before this is called.
7021 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7022 return SDValue();
7023
7024 unsigned NumOps = Ops.size();
7025 if (NumOps == 0)
7026 return SDValue();
7027
7028 if (isUndef(Opcode, Ops))
7029 return getUNDEF(VT);
7030
7031 // Handle unary special cases.
7032 if (NumOps == 1) {
7033 SDValue N1 = Ops[0];
7034
7035 // Constant fold unary operations with an integer constant operand. Even
7036 // opaque constant will be folded, because the folding of unary operations
7037 // doesn't create new constants with different values. Nevertheless, the
7038 // opaque flag is preserved during folding to prevent future folding with
7039 // other constants.
7040 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7041 const APInt &Val = C->getAPIntValue();
7042 switch (Opcode) {
7043 case ISD::SIGN_EXTEND:
7044 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7045 C->isTargetOpcode(), C->isOpaque());
7046 case ISD::TRUNCATE:
7047 if (C->isOpaque())
7048 break;
7049 [[fallthrough]];
7050 case ISD::ZERO_EXTEND:
7051 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7052 C->isTargetOpcode(), C->isOpaque());
7053 case ISD::ANY_EXTEND:
7054 // Some targets like RISCV prefer to sign extend some types.
7055 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7056 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7057 C->isTargetOpcode(), C->isOpaque());
7058 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7059 C->isTargetOpcode(), C->isOpaque());
7060 case ISD::ABS:
7061 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7062 C->isOpaque());
7063 case ISD::BITREVERSE:
7064 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7065 C->isOpaque());
7066 case ISD::BSWAP:
7067 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7068 C->isOpaque());
7069 case ISD::CTPOP:
7070 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7071 C->isOpaque());
7072 case ISD::CTLZ:
7074 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7075 C->isOpaque());
7076 case ISD::CTTZ:
7078 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7079 C->isOpaque());
7080 case ISD::UINT_TO_FP:
7081 case ISD::SINT_TO_FP: {
7083 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7085 return getConstantFP(FPV, DL, VT);
7086 }
7087 case ISD::FP16_TO_FP:
7088 case ISD::BF16_TO_FP: {
7089 bool Ignored;
7090 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7091 : APFloat::BFloat(),
7092 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7093
7094 // This can return overflow, underflow, or inexact; we don't care.
7095 // FIXME need to be more flexible about rounding mode.
7097 &Ignored);
7098 return getConstantFP(FPV, DL, VT);
7099 }
7100 case ISD::STEP_VECTOR:
7101 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7102 return V;
7103 break;
7104 case ISD::BITCAST:
7105 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7106 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7107 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7108 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7109 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7110 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7111 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7112 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7113 break;
7114 }
7115 }
7116
7117 // Constant fold unary operations with a floating point constant operand.
7118 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7119 APFloat V = C->getValueAPF(); // make copy
7120 switch (Opcode) {
7121 case ISD::FNEG:
7122 V.changeSign();
7123 return getConstantFP(V, DL, VT);
7124 case ISD::FABS:
7125 V.clearSign();
7126 return getConstantFP(V, DL, VT);
7127 case ISD::FCEIL: {
7128 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7130 return getConstantFP(V, DL, VT);
7131 return SDValue();
7132 }
7133 case ISD::FTRUNC: {
7134 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7136 return getConstantFP(V, DL, VT);
7137 return SDValue();
7138 }
7139 case ISD::FFLOOR: {
7140 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7142 return getConstantFP(V, DL, VT);
7143 return SDValue();
7144 }
7145 case ISD::FP_EXTEND: {
7146 bool ignored;
7147 // This can return overflow, underflow, or inexact; we don't care.
7148 // FIXME need to be more flexible about rounding mode.
7149 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7150 &ignored);
7151 return getConstantFP(V, DL, VT);
7152 }
7153 case ISD::FP_TO_SINT:
7154 case ISD::FP_TO_UINT: {
7155 bool ignored;
7156 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7157 // FIXME need to be more flexible about rounding mode.
7159 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7160 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7161 break;
7162 return getConstant(IntVal, DL, VT);
7163 }
7164 case ISD::FP_TO_FP16:
7165 case ISD::FP_TO_BF16: {
7166 bool Ignored;
7167 // This can return overflow, underflow, or inexact; we don't care.
7168 // FIXME need to be more flexible about rounding mode.
7169 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7170 : APFloat::BFloat(),
7172 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7173 }
7174 case ISD::BITCAST:
7175 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7176 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7177 VT);
7178 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7179 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7180 VT);
7181 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7182 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7183 VT);
7184 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7185 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7186 break;
7187 }
7188 }
7189
7190 // Early-out if we failed to constant fold a bitcast.
7191 if (Opcode == ISD::BITCAST)
7192 return SDValue();
7193 }
7194
7195 // Handle binops special cases.
7196 if (NumOps == 2) {
7197 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7198 return CFP;
7199
7200 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7201 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7202 if (C1->isOpaque() || C2->isOpaque())
7203 return SDValue();
7204
7205 std::optional<APInt> FoldAttempt =
7206 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7207 if (!FoldAttempt)
7208 return SDValue();
7209
7210 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7211 assert((!Folded || !VT.isVector()) &&
7212 "Can't fold vectors ops with scalar operands");
7213 return Folded;
7214 }
7215 }
7216
7217 // fold (add Sym, c) -> Sym+c
7219 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7220 if (TLI->isCommutativeBinOp(Opcode))
7222 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7223
7224 // fold (sext_in_reg c1) -> c2
7225 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7226 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7227
7228 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7229 unsigned FromBits = EVT.getScalarSizeInBits();
7230 Val <<= Val.getBitWidth() - FromBits;
7231 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7232 return getConstant(Val, DL, ConstantVT);
7233 };
7234
7235 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7236 const APInt &Val = C1->getAPIntValue();
7237 return SignExtendInReg(Val, VT);
7238 }
7239
7241 SmallVector<SDValue, 8> ScalarOps;
7242 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7243 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7244 SDValue Op = Ops[0].getOperand(I);
7245 if (Op.isUndef()) {
7246 ScalarOps.push_back(getUNDEF(OpVT));
7247 continue;
7248 }
7249 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7250 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7251 }
7252 return getBuildVector(VT, DL, ScalarOps);
7253 }
7254
7255 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7256 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7257 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7258 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7259 Ops[0].getOperand(0).getValueType()));
7260 }
7261 }
7262
7263 // Handle fshl/fshr special cases.
7264 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7265 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7266 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7267 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7268
7269 if (C1 && C2 && C3) {
7270 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7271 return SDValue();
7272 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7273 &V3 = C3->getAPIntValue();
7274
7275 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7276 : APIntOps::fshr(V1, V2, V3);
7277 return getConstant(FoldedVal, DL, VT);
7278 }
7279 }
7280
7281 // Handle fma/fmad special cases.
7282 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7283 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7284 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7285 Ops[2].getValueType() == VT && "FMA types must match!");
7289 if (C1 && C2 && C3) {
7290 APFloat V1 = C1->getValueAPF();
7291 const APFloat &V2 = C2->getValueAPF();
7292 const APFloat &V3 = C3->getValueAPF();
7293 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7296 } else
7298 return getConstantFP(V1, DL, VT);
7299 }
7300 }
7301
7302 // This is for vector folding only from here on.
7303 if (!VT.isVector())
7304 return SDValue();
7305
7306 ElementCount NumElts = VT.getVectorElementCount();
7307
7308 // See if we can fold through any bitcasted integer ops.
7309 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7310 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7311 (Ops[0].getOpcode() == ISD::BITCAST ||
7312 Ops[1].getOpcode() == ISD::BITCAST)) {
7315 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7316 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7317 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7318 N2.getValueType().isInteger()) {
7319 bool IsLE = getDataLayout().isLittleEndian();
7320 unsigned EltBits = VT.getScalarSizeInBits();
7321 SmallVector<APInt> RawBits1, RawBits2;
7322 BitVector UndefElts1, UndefElts2;
7323 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7324 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7325 SmallVector<APInt> RawBits;
7326 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7327 std::optional<APInt> Fold = FoldValueWithUndef(
7328 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7329 if (!Fold)
7330 break;
7331 RawBits.push_back(*Fold);
7332 }
7333 if (RawBits.size() == NumElts.getFixedValue()) {
7334 // We have constant folded, but we might need to cast this again back
7335 // to the original (possibly legalized) type.
7336 EVT BVVT, BVEltVT;
7337 if (N1.getValueType() == VT) {
7338 BVVT = N1.getValueType();
7339 BVEltVT = BV1->getOperand(0).getValueType();
7340 } else {
7341 BVVT = N2.getValueType();
7342 BVEltVT = BV2->getOperand(0).getValueType();
7343 }
7344 unsigned BVEltBits = BVEltVT.getSizeInBits();
7345 SmallVector<APInt> DstBits;
7346 BitVector DstUndefs;
7348 DstBits, RawBits, DstUndefs,
7349 BitVector(RawBits.size(), false));
7350 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7351 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7352 if (DstUndefs[I])
7353 continue;
7354 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7355 }
7356 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7357 }
7358 }
7359 }
7360 }
7361
7362 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7363 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7364 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7365 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7366 APInt RHSVal;
7367 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7368 APInt NewStep = Opcode == ISD::MUL
7369 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7370 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7371 return getStepVector(DL, VT, NewStep);
7372 }
7373 }
7374
7375 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7376 return !Op.getValueType().isVector() ||
7377 Op.getValueType().getVectorElementCount() == NumElts;
7378 };
7379
7380 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7381 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7382 Op.getOpcode() == ISD::BUILD_VECTOR ||
7383 Op.getOpcode() == ISD::SPLAT_VECTOR;
7384 };
7385
7386 // All operands must be vector types with the same number of elements as
7387 // the result type and must be either UNDEF or a build/splat vector
7388 // or UNDEF scalars.
7389 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7390 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7391 return SDValue();
7392
7393 // If we are comparing vectors, then the result needs to be a i1 boolean that
7394 // is then extended back to the legal result type depending on how booleans
7395 // are represented.
7396 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7397 ISD::NodeType ExtendCode =
7398 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7399 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7401
7402 // Find legal integer scalar type for constant promotion and
7403 // ensure that its scalar size is at least as large as source.
7404 EVT LegalSVT = VT.getScalarType();
7405 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7406 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7407 if (LegalSVT.bitsLT(VT.getScalarType()))
7408 return SDValue();
7409 }
7410
7411 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7412 // only have one operand to check. For fixed-length vector types we may have
7413 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7414 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7415
7416 // Constant fold each scalar lane separately.
7417 SmallVector<SDValue, 4> ScalarResults;
7418 for (unsigned I = 0; I != NumVectorElts; I++) {
7419 SmallVector<SDValue, 4> ScalarOps;
7420 for (SDValue Op : Ops) {
7421 EVT InSVT = Op.getValueType().getScalarType();
7422 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7423 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7424 if (Op.isUndef())
7425 ScalarOps.push_back(getUNDEF(InSVT));
7426 else
7427 ScalarOps.push_back(Op);
7428 continue;
7429 }
7430
7431 SDValue ScalarOp =
7432 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7433 EVT ScalarVT = ScalarOp.getValueType();
7434
7435 // Build vector (integer) scalar operands may need implicit
7436 // truncation - do this before constant folding.
7437 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7438 // Don't create illegally-typed nodes unless they're constants or undef
7439 // - if we fail to constant fold we can't guarantee the (dead) nodes
7440 // we're creating will be cleaned up before being visited for
7441 // legalization.
7442 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7443 !isa<ConstantSDNode>(ScalarOp) &&
7444 TLI->getTypeAction(*getContext(), InSVT) !=
7446 return SDValue();
7447 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7448 }
7449
7450 ScalarOps.push_back(ScalarOp);
7451 }
7452
7453 // Constant fold the scalar operands.
7454 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7455
7456 // Scalar folding only succeeded if the result is a constant or UNDEF.
7457 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7458 ScalarResult.getOpcode() != ISD::ConstantFP)
7459 return SDValue();
7460
7461 // Legalize the (integer) scalar constant if necessary. We only do
7462 // this once we know the folding succeeded, since otherwise we would
7463 // get a node with illegal type which has a user.
7464 if (LegalSVT != SVT)
7465 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7466
7467 ScalarResults.push_back(ScalarResult);
7468 }
7469
7470 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7471 : getBuildVector(VT, DL, ScalarResults);
7472 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7473 return V;
7474}
7475
7478 // TODO: Add support for unary/ternary fp opcodes.
7479 if (Ops.size() != 2)
7480 return SDValue();
7481
7482 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7483 // should. That will require dealing with a potentially non-default
7484 // rounding mode, checking the "opStatus" return value from the APFloat
7485 // math calculations, and possibly other variations.
7486 SDValue N1 = Ops[0];
7487 SDValue N2 = Ops[1];
7488 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7489 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7490 if (N1CFP && N2CFP) {
7491 APFloat C1 = N1CFP->getValueAPF(); // make copy
7492 const APFloat &C2 = N2CFP->getValueAPF();
7493 switch (Opcode) {
7494 case ISD::FADD:
7496 return getConstantFP(C1, DL, VT);
7497 case ISD::FSUB:
7499 return getConstantFP(C1, DL, VT);
7500 case ISD::FMUL:
7502 return getConstantFP(C1, DL, VT);
7503 case ISD::FDIV:
7505 return getConstantFP(C1, DL, VT);
7506 case ISD::FREM:
7507 C1.mod(C2);
7508 return getConstantFP(C1, DL, VT);
7509 case ISD::FCOPYSIGN:
7510 C1.copySign(C2);
7511 return getConstantFP(C1, DL, VT);
7512 case ISD::FMINNUM:
7513 if (C1.isSignaling() || C2.isSignaling())
7514 return SDValue();
7515 return getConstantFP(minnum(C1, C2), DL, VT);
7516 case ISD::FMAXNUM:
7517 if (C1.isSignaling() || C2.isSignaling())
7518 return SDValue();
7519 return getConstantFP(maxnum(C1, C2), DL, VT);
7520 case ISD::FMINIMUM:
7521 return getConstantFP(minimum(C1, C2), DL, VT);
7522 case ISD::FMAXIMUM:
7523 return getConstantFP(maximum(C1, C2), DL, VT);
7524 case ISD::FMINIMUMNUM:
7525 return getConstantFP(minimumnum(C1, C2), DL, VT);
7526 case ISD::FMAXIMUMNUM:
7527 return getConstantFP(maximumnum(C1, C2), DL, VT);
7528 default: break;
7529 }
7530 }
7531 if (N1CFP && Opcode == ISD::FP_ROUND) {
7532 APFloat C1 = N1CFP->getValueAPF(); // make copy
7533 bool Unused;
7534 // This can return overflow, underflow, or inexact; we don't care.
7535 // FIXME need to be more flexible about rounding mode.
7537 &Unused);
7538 return getConstantFP(C1, DL, VT);
7539 }
7540
7541 switch (Opcode) {
7542 case ISD::FSUB:
7543 // -0.0 - undef --> undef (consistent with "fneg undef")
7544 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7545 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7546 return getUNDEF(VT);
7547 [[fallthrough]];
7548
7549 case ISD::FADD:
7550 case ISD::FMUL:
7551 case ISD::FDIV:
7552 case ISD::FREM:
7553 // If both operands are undef, the result is undef. If 1 operand is undef,
7554 // the result is NaN. This should match the behavior of the IR optimizer.
7555 if (N1.isUndef() && N2.isUndef())
7556 return getUNDEF(VT);
7557 if (N1.isUndef() || N2.isUndef())
7559 }
7560 return SDValue();
7561}
7562
7564 const SDLoc &DL, EVT DstEltVT) {
7565 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7566
7567 // If this is already the right type, we're done.
7568 if (SrcEltVT == DstEltVT)
7569 return SDValue(BV, 0);
7570
7571 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7572 unsigned DstBitSize = DstEltVT.getSizeInBits();
7573
7574 // If this is a conversion of N elements of one type to N elements of another
7575 // type, convert each element. This handles FP<->INT cases.
7576 if (SrcBitSize == DstBitSize) {
7578 for (SDValue Op : BV->op_values()) {
7579 // If the vector element type is not legal, the BUILD_VECTOR operands
7580 // are promoted and implicitly truncated. Make that explicit here.
7581 if (Op.getValueType() != SrcEltVT)
7582 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7583 Ops.push_back(getBitcast(DstEltVT, Op));
7584 }
7585 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7587 return getBuildVector(VT, DL, Ops);
7588 }
7589
7590 // Otherwise, we're growing or shrinking the elements. To avoid having to
7591 // handle annoying details of growing/shrinking FP values, we convert them to
7592 // int first.
7593 if (SrcEltVT.isFloatingPoint()) {
7594 // Convert the input float vector to a int vector where the elements are the
7595 // same sizes.
7596 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7597 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7599 DstEltVT);
7600 return SDValue();
7601 }
7602
7603 // Now we know the input is an integer vector. If the output is a FP type,
7604 // convert to integer first, then to FP of the right size.
7605 if (DstEltVT.isFloatingPoint()) {
7606 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7607 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7609 DstEltVT);
7610 return SDValue();
7611 }
7612
7613 // Okay, we know the src/dst types are both integers of differing types.
7614 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7615
7616 // Extract the constant raw bit data.
7617 BitVector UndefElements;
7618 SmallVector<APInt> RawBits;
7619 bool IsLE = getDataLayout().isLittleEndian();
7620 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7621 return SDValue();
7622
7624 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7625 if (UndefElements[I])
7626 Ops.push_back(getUNDEF(DstEltVT));
7627 else
7628 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7629 }
7630
7631 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7632 return getBuildVector(VT, DL, Ops);
7633}
7634
7636 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7637
7638 // There's no need to assert on a byte-aligned pointer. All pointers are at
7639 // least byte aligned.
7640 if (A == Align(1))
7641 return Val;
7642
7643 SDVTList VTs = getVTList(Val.getValueType());
7645 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7646 ID.AddInteger(A.value());
7647
7648 void *IP = nullptr;
7649 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7650 return SDValue(E, 0);
7651
7652 auto *N =
7653 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7654 createOperands(N, {Val});
7655
7656 CSEMap.InsertNode(N, IP);
7657 InsertNode(N);
7658
7659 SDValue V(N, 0);
7660 NewSDValueDbgMsg(V, "Creating new node: ", this);
7661 return V;
7662}
7663
7664SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7665 SDValue N1, SDValue N2) {
7666 SDNodeFlags Flags;
7667 if (Inserter)
7668 Flags = Inserter->getFlags();
7669 return getNode(Opcode, DL, VT, N1, N2, Flags);
7670}
7671
7673 SDValue &N2) const {
7674 if (!TLI->isCommutativeBinOp(Opcode))
7675 return;
7676
7677 // Canonicalize:
7678 // binop(const, nonconst) -> binop(nonconst, const)
7681 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7682 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7683 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7684 std::swap(N1, N2);
7685
7686 // Canonicalize:
7687 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7688 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7690 std::swap(N1, N2);
7691}
7692
7693SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7694 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7696 N2.getOpcode() != ISD::DELETED_NODE &&
7697 "Operand is DELETED_NODE!");
7698
7699 canonicalizeCommutativeBinop(Opcode, N1, N2);
7700
7701 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7702 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7703
7704 // Don't allow undefs in vector splats - we might be returning N2 when folding
7705 // to zero etc.
7706 ConstantSDNode *N2CV =
7707 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7708
7709 switch (Opcode) {
7710 default: break;
7711 case ISD::TokenFactor:
7712 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7713 N2.getValueType() == MVT::Other && "Invalid token factor!");
7714 // Fold trivial token factors.
7715 if (N1.getOpcode() == ISD::EntryToken) return N2;
7716 if (N2.getOpcode() == ISD::EntryToken) return N1;
7717 if (N1 == N2) return N1;
7718 break;
7719 case ISD::BUILD_VECTOR: {
7720 // Attempt to simplify BUILD_VECTOR.
7721 SDValue Ops[] = {N1, N2};
7722 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7723 return V;
7724 break;
7725 }
7726 case ISD::CONCAT_VECTORS: {
7727 SDValue Ops[] = {N1, N2};
7728 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7729 return V;
7730 break;
7731 }
7732 case ISD::AND:
7733 assert(VT.isInteger() && "This operator does not apply to FP types!");
7734 assert(N1.getValueType() == N2.getValueType() &&
7735 N1.getValueType() == VT && "Binary operator types must match!");
7736 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7737 // worth handling here.
7738 if (N2CV && N2CV->isZero())
7739 return N2;
7740 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7741 return N1;
7742 break;
7743 case ISD::OR:
7744 case ISD::XOR:
7745 case ISD::ADD:
7746 case ISD::PTRADD:
7747 case ISD::SUB:
7748 assert(VT.isInteger() && "This operator does not apply to FP types!");
7749 assert(N1.getValueType() == N2.getValueType() &&
7750 N1.getValueType() == VT && "Binary operator types must match!");
7751 // The equal operand types requirement is unnecessarily strong for PTRADD.
7752 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7753 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7754 // logic everywhere where PTRADDs may be folded or combined to properly
7755 // support them. If/when we introduce pointer types to the SDAG, we will
7756 // need to relax this constraint.
7757
7758 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7759 // it's worth handling here.
7760 if (N2CV && N2CV->isZero())
7761 return N1;
7762 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7763 VT.getScalarType() == MVT::i1)
7764 return getNode(ISD::XOR, DL, VT, N1, N2);
7765 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7766 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7767 N2.getOpcode() == ISD::VSCALE) {
7768 const APInt &C1 = N1->getConstantOperandAPInt(0);
7769 const APInt &C2 = N2->getConstantOperandAPInt(0);
7770 return getVScale(DL, VT, C1 + C2);
7771 }
7772 break;
7773 case ISD::MUL:
7774 assert(VT.isInteger() && "This operator does not apply to FP types!");
7775 assert(N1.getValueType() == N2.getValueType() &&
7776 N1.getValueType() == VT && "Binary operator types must match!");
7777 if (VT.getScalarType() == MVT::i1)
7778 return getNode(ISD::AND, DL, VT, N1, N2);
7779 if (N2CV && N2CV->isZero())
7780 return N2;
7781 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7782 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7783 const APInt &N2CImm = N2C->getAPIntValue();
7784 return getVScale(DL, VT, MulImm * N2CImm);
7785 }
7786 break;
7787 case ISD::UDIV:
7788 case ISD::UREM:
7789 case ISD::MULHU:
7790 case ISD::MULHS:
7791 case ISD::SDIV:
7792 case ISD::SREM:
7793 case ISD::SADDSAT:
7794 case ISD::SSUBSAT:
7795 case ISD::UADDSAT:
7796 case ISD::USUBSAT:
7797 assert(VT.isInteger() && "This operator does not apply to FP types!");
7798 assert(N1.getValueType() == N2.getValueType() &&
7799 N1.getValueType() == VT && "Binary operator types must match!");
7800 if (VT.getScalarType() == MVT::i1) {
7801 // fold (add_sat x, y) -> (or x, y) for bool types.
7802 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7803 return getNode(ISD::OR, DL, VT, N1, N2);
7804 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7805 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7806 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7807 }
7808 break;
7809 case ISD::SCMP:
7810 case ISD::UCMP:
7811 assert(N1.getValueType() == N2.getValueType() &&
7812 "Types of operands of UCMP/SCMP must match");
7813 assert(N1.getValueType().isVector() == VT.isVector() &&
7814 "Operands and return type of must both be scalars or vectors");
7815 if (VT.isVector())
7818 "Result and operands must have the same number of elements");
7819 break;
7820 case ISD::AVGFLOORS:
7821 case ISD::AVGFLOORU:
7822 case ISD::AVGCEILS:
7823 case ISD::AVGCEILU:
7824 assert(VT.isInteger() && "This operator does not apply to FP types!");
7825 assert(N1.getValueType() == N2.getValueType() &&
7826 N1.getValueType() == VT && "Binary operator types must match!");
7827 break;
7828 case ISD::ABDS:
7829 case ISD::ABDU:
7830 assert(VT.isInteger() && "This operator does not apply to FP types!");
7831 assert(N1.getValueType() == N2.getValueType() &&
7832 N1.getValueType() == VT && "Binary operator types must match!");
7833 if (VT.getScalarType() == MVT::i1)
7834 return getNode(ISD::XOR, DL, VT, N1, N2);
7835 break;
7836 case ISD::SMIN:
7837 case ISD::UMAX:
7838 assert(VT.isInteger() && "This operator does not apply to FP types!");
7839 assert(N1.getValueType() == N2.getValueType() &&
7840 N1.getValueType() == VT && "Binary operator types must match!");
7841 if (VT.getScalarType() == MVT::i1)
7842 return getNode(ISD::OR, DL, VT, N1, N2);
7843 break;
7844 case ISD::SMAX:
7845 case ISD::UMIN:
7846 assert(VT.isInteger() && "This operator does not apply to FP types!");
7847 assert(N1.getValueType() == N2.getValueType() &&
7848 N1.getValueType() == VT && "Binary operator types must match!");
7849 if (VT.getScalarType() == MVT::i1)
7850 return getNode(ISD::AND, DL, VT, N1, N2);
7851 break;
7852 case ISD::FADD:
7853 case ISD::FSUB:
7854 case ISD::FMUL:
7855 case ISD::FDIV:
7856 case ISD::FREM:
7857 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7858 assert(N1.getValueType() == N2.getValueType() &&
7859 N1.getValueType() == VT && "Binary operator types must match!");
7860 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7861 return V;
7862 break;
7863 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7864 assert(N1.getValueType() == VT &&
7867 "Invalid FCOPYSIGN!");
7868 break;
7869 case ISD::SHL:
7870 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7871 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7872 const APInt &ShiftImm = N2C->getAPIntValue();
7873 return getVScale(DL, VT, MulImm << ShiftImm);
7874 }
7875 [[fallthrough]];
7876 case ISD::SRA:
7877 case ISD::SRL:
7878 if (SDValue V = simplifyShift(N1, N2))
7879 return V;
7880 [[fallthrough]];
7881 case ISD::ROTL:
7882 case ISD::ROTR:
7883 case ISD::SSHLSAT:
7884 case ISD::USHLSAT:
7885 assert(VT == N1.getValueType() &&
7886 "Shift operators return type must be the same as their first arg");
7887 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7888 "Shifts only work on integers");
7889 assert((!VT.isVector() || VT == N2.getValueType()) &&
7890 "Vector shift amounts must be in the same as their first arg");
7891 // Verify that the shift amount VT is big enough to hold valid shift
7892 // amounts. This catches things like trying to shift an i1024 value by an
7893 // i8, which is easy to fall into in generic code that uses
7894 // TLI.getShiftAmount().
7897 "Invalid use of small shift amount with oversized value!");
7898
7899 // Always fold shifts of i1 values so the code generator doesn't need to
7900 // handle them. Since we know the size of the shift has to be less than the
7901 // size of the value, the shift/rotate count is guaranteed to be zero.
7902 if (VT == MVT::i1)
7903 return N1;
7904 if (N2CV && N2CV->isZero())
7905 return N1;
7906 break;
7907 case ISD::FP_ROUND:
7909 VT.bitsLE(N1.getValueType()) && N2C &&
7910 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7911 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7912 if (N1.getValueType() == VT) return N1; // noop conversion.
7913 break;
7914 case ISD::AssertNoFPClass: {
7916 "AssertNoFPClass is used for a non-floating type");
7917 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7918 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7919 assert(llvm::to_underlying(NoFPClass) <=
7921 "FPClassTest value too large");
7922 (void)NoFPClass;
7923 break;
7924 }
7925 case ISD::AssertSext:
7926 case ISD::AssertZext: {
7927 EVT EVT = cast<VTSDNode>(N2)->getVT();
7928 assert(VT == N1.getValueType() && "Not an inreg extend!");
7929 assert(VT.isInteger() && EVT.isInteger() &&
7930 "Cannot *_EXTEND_INREG FP types");
7931 assert(!EVT.isVector() &&
7932 "AssertSExt/AssertZExt type should be the vector element type "
7933 "rather than the vector type!");
7934 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7935 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7936 break;
7937 }
7939 EVT EVT = cast<VTSDNode>(N2)->getVT();
7940 assert(VT == N1.getValueType() && "Not an inreg extend!");
7941 assert(VT.isInteger() && EVT.isInteger() &&
7942 "Cannot *_EXTEND_INREG FP types");
7943 assert(EVT.isVector() == VT.isVector() &&
7944 "SIGN_EXTEND_INREG type should be vector iff the operand "
7945 "type is vector!");
7946 assert((!EVT.isVector() ||
7948 "Vector element counts must match in SIGN_EXTEND_INREG");
7949 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
7950 if (EVT == VT) return N1; // Not actually extending
7951 break;
7952 }
7954 case ISD::FP_TO_UINT_SAT: {
7955 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7956 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7957 assert(N1.getValueType().isVector() == VT.isVector() &&
7958 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7959 "vector!");
7960 assert((!VT.isVector() || VT.getVectorElementCount() ==
7962 "Vector element counts must match in FP_TO_*INT_SAT");
7963 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7964 "Type to saturate to must be a scalar.");
7965 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7966 "Not extending!");
7967 break;
7968 }
7971 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7972 element type of the vector.");
7973
7974 // Extract from an undefined value or using an undefined index is undefined.
7975 if (N1.isUndef() || N2.isUndef())
7976 return getUNDEF(VT);
7977
7978 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7979 // vectors. For scalable vectors we will provide appropriate support for
7980 // dealing with arbitrary indices.
7981 if (N2C && N1.getValueType().isFixedLengthVector() &&
7982 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7983 return getUNDEF(VT);
7984
7985 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7986 // expanding copies of large vectors from registers. This only works for
7987 // fixed length vectors, since we need to know the exact number of
7988 // elements.
7989 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7991 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7992 return getExtractVectorElt(DL, VT,
7993 N1.getOperand(N2C->getZExtValue() / Factor),
7994 N2C->getZExtValue() % Factor);
7995 }
7996
7997 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7998 // lowering is expanding large vector constants.
7999 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8000 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8003 "BUILD_VECTOR used for scalable vectors");
8004 unsigned Index =
8005 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8006 SDValue Elt = N1.getOperand(Index);
8007
8008 if (VT != Elt.getValueType())
8009 // If the vector element type is not legal, the BUILD_VECTOR operands
8010 // are promoted and implicitly truncated, and the result implicitly
8011 // extended. Make that explicit here.
8012 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8013
8014 return Elt;
8015 }
8016
8017 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8018 // operations are lowered to scalars.
8019 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8020 // If the indices are the same, return the inserted element else
8021 // if the indices are known different, extract the element from
8022 // the original vector.
8023 SDValue N1Op2 = N1.getOperand(2);
8025
8026 if (N1Op2C && N2C) {
8027 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8028 if (VT == N1.getOperand(1).getValueType())
8029 return N1.getOperand(1);
8030 if (VT.isFloatingPoint()) {
8032 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8033 }
8034 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8035 }
8036 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8037 }
8038 }
8039
8040 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8041 // when vector types are scalarized and v1iX is legal.
8042 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8043 // Here we are completely ignoring the extract element index (N2),
8044 // which is fine for fixed width vectors, since any index other than 0
8045 // is undefined anyway. However, this cannot be ignored for scalable
8046 // vectors - in theory we could support this, but we don't want to do this
8047 // without a profitability check.
8048 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8050 N1.getValueType().getVectorNumElements() == 1) {
8051 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8052 N1.getOperand(1));
8053 }
8054 break;
8056 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8057 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8058 (N1.getValueType().isInteger() == VT.isInteger()) &&
8059 N1.getValueType() != VT &&
8060 "Wrong types for EXTRACT_ELEMENT!");
8061
8062 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8063 // 64-bit integers into 32-bit parts. Instead of building the extract of
8064 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8065 if (N1.getOpcode() == ISD::BUILD_PAIR)
8066 return N1.getOperand(N2C->getZExtValue());
8067
8068 // EXTRACT_ELEMENT of a constant int is also very common.
8069 if (N1C) {
8070 unsigned ElementSize = VT.getSizeInBits();
8071 unsigned Shift = ElementSize * N2C->getZExtValue();
8072 const APInt &Val = N1C->getAPIntValue();
8073 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8074 }
8075 break;
8077 EVT N1VT = N1.getValueType();
8078 assert(VT.isVector() && N1VT.isVector() &&
8079 "Extract subvector VTs must be vectors!");
8081 "Extract subvector VTs must have the same element type!");
8082 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8083 "Cannot extract a scalable vector from a fixed length vector!");
8084 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8086 "Extract subvector must be from larger vector to smaller vector!");
8087 assert(N2C && "Extract subvector index must be a constant");
8088 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8089 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8090 N1VT.getVectorMinNumElements()) &&
8091 "Extract subvector overflow!");
8092 assert(N2C->getAPIntValue().getBitWidth() ==
8093 TLI->getVectorIdxWidth(getDataLayout()) &&
8094 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8095 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8096 "Extract index is not a multiple of the output vector length");
8097
8098 // Trivial extraction.
8099 if (VT == N1VT)
8100 return N1;
8101
8102 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8103 if (N1.isUndef())
8104 return getUNDEF(VT);
8105
8106 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8107 // the concat have the same type as the extract.
8108 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8109 VT == N1.getOperand(0).getValueType()) {
8110 unsigned Factor = VT.getVectorMinNumElements();
8111 return N1.getOperand(N2C->getZExtValue() / Factor);
8112 }
8113
8114 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8115 // during shuffle legalization.
8116 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8117 VT == N1.getOperand(1).getValueType())
8118 return N1.getOperand(1);
8119 break;
8120 }
8121 }
8122
8123 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8124 switch (Opcode) {
8125 case ISD::XOR:
8126 case ISD::ADD:
8127 case ISD::PTRADD:
8128 case ISD::SUB:
8130 case ISD::UDIV:
8131 case ISD::SDIV:
8132 case ISD::UREM:
8133 case ISD::SREM:
8134 case ISD::MUL:
8135 case ISD::AND:
8136 case ISD::SSUBSAT:
8137 case ISD::USUBSAT:
8138 case ISD::UMIN:
8139 case ISD::OR:
8140 case ISD::SADDSAT:
8141 case ISD::UADDSAT:
8142 case ISD::UMAX:
8143 case ISD::SMAX:
8144 case ISD::SMIN:
8145 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8146 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8147 }
8148 }
8149
8150 // Canonicalize an UNDEF to the RHS, even over a constant.
8151 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8152 if (TLI->isCommutativeBinOp(Opcode)) {
8153 std::swap(N1, N2);
8154 } else {
8155 switch (Opcode) {
8156 case ISD::PTRADD:
8157 case ISD::SUB:
8158 // fold op(undef, non_undef_arg2) -> undef.
8159 return N1;
8161 case ISD::UDIV:
8162 case ISD::SDIV:
8163 case ISD::UREM:
8164 case ISD::SREM:
8165 case ISD::SSUBSAT:
8166 case ISD::USUBSAT:
8167 // fold op(undef, non_undef_arg2) -> 0.
8168 return getConstant(0, DL, VT);
8169 }
8170 }
8171 }
8172
8173 // Fold a bunch of operators when the RHS is undef.
8174 if (N2.getOpcode() == ISD::UNDEF) {
8175 switch (Opcode) {
8176 case ISD::XOR:
8177 if (N1.getOpcode() == ISD::UNDEF)
8178 // Handle undef ^ undef -> 0 special case. This is a common
8179 // idiom (misuse).
8180 return getConstant(0, DL, VT);
8181 [[fallthrough]];
8182 case ISD::ADD:
8183 case ISD::PTRADD:
8184 case ISD::SUB:
8185 // fold op(arg1, undef) -> undef.
8186 return N2;
8187 case ISD::UDIV:
8188 case ISD::SDIV:
8189 case ISD::UREM:
8190 case ISD::SREM:
8191 // fold op(arg1, undef) -> poison.
8192 return getPOISON(VT);
8193 case ISD::MUL:
8194 case ISD::AND:
8195 case ISD::SSUBSAT:
8196 case ISD::USUBSAT:
8197 case ISD::UMIN:
8198 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8199 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8200 case ISD::OR:
8201 case ISD::SADDSAT:
8202 case ISD::UADDSAT:
8203 case ISD::UMAX:
8204 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8205 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8206 case ISD::SMAX:
8207 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8208 return N1.getOpcode() == ISD::UNDEF
8209 ? N2
8210 : getConstant(
8212 VT);
8213 case ISD::SMIN:
8214 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8215 return N1.getOpcode() == ISD::UNDEF
8216 ? N2
8217 : getConstant(
8219 VT);
8220 }
8221 }
8222
8223 // Perform trivial constant folding.
8224 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8225 return SV;
8226
8227 // Memoize this node if possible.
8228 SDNode *N;
8229 SDVTList VTs = getVTList(VT);
8230 SDValue Ops[] = {N1, N2};
8231 if (VT != MVT::Glue) {
8233 AddNodeIDNode(ID, Opcode, VTs, Ops);
8234 void *IP = nullptr;
8235 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8236 E->intersectFlagsWith(Flags);
8237 return SDValue(E, 0);
8238 }
8239
8240 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8241 N->setFlags(Flags);
8242 createOperands(N, Ops);
8243 CSEMap.InsertNode(N, IP);
8244 } else {
8245 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8246 createOperands(N, Ops);
8247 }
8248
8249 InsertNode(N);
8250 SDValue V = SDValue(N, 0);
8251 NewSDValueDbgMsg(V, "Creating new node: ", this);
8252 return V;
8253}
8254
8255SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8256 SDValue N1, SDValue N2, SDValue N3) {
8257 SDNodeFlags Flags;
8258 if (Inserter)
8259 Flags = Inserter->getFlags();
8260 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8261}
8262
8263SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8264 SDValue N1, SDValue N2, SDValue N3,
8265 const SDNodeFlags Flags) {
8267 N2.getOpcode() != ISD::DELETED_NODE &&
8268 N3.getOpcode() != ISD::DELETED_NODE &&
8269 "Operand is DELETED_NODE!");
8270 // Perform various simplifications.
8271 switch (Opcode) {
8272 case ISD::BUILD_VECTOR: {
8273 // Attempt to simplify BUILD_VECTOR.
8274 SDValue Ops[] = {N1, N2, N3};
8275 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8276 return V;
8277 break;
8278 }
8279 case ISD::CONCAT_VECTORS: {
8280 SDValue Ops[] = {N1, N2, N3};
8281 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8282 return V;
8283 break;
8284 }
8285 case ISD::SETCC: {
8286 assert(VT.isInteger() && "SETCC result type must be an integer!");
8287 assert(N1.getValueType() == N2.getValueType() &&
8288 "SETCC operands must have the same type!");
8289 assert(VT.isVector() == N1.getValueType().isVector() &&
8290 "SETCC type should be vector iff the operand type is vector!");
8291 assert((!VT.isVector() || VT.getVectorElementCount() ==
8293 "SETCC vector element counts must match!");
8294 // Use FoldSetCC to simplify SETCC's.
8295 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8296 return V;
8297 break;
8298 }
8299 case ISD::SELECT:
8300 case ISD::VSELECT:
8301 if (SDValue V = simplifySelect(N1, N2, N3))
8302 return V;
8303 break;
8305 llvm_unreachable("should use getVectorShuffle constructor!");
8307 if (isNullConstant(N3))
8308 return N1;
8309 break;
8311 if (isNullConstant(N3))
8312 return N2;
8313 break;
8315 assert(VT.isVector() && VT == N1.getValueType() &&
8316 "INSERT_VECTOR_ELT vector type mismatch");
8318 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8319 assert((!VT.isFloatingPoint() ||
8320 VT.getVectorElementType() == N2.getValueType()) &&
8321 "INSERT_VECTOR_ELT fp scalar type mismatch");
8322 assert((!VT.isInteger() ||
8324 "INSERT_VECTOR_ELT int scalar size mismatch");
8325
8326 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8327 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8328 // for scalable vectors where we will generate appropriate code to
8329 // deal with out-of-bounds cases correctly.
8330 if (N3C && VT.isFixedLengthVector() &&
8331 N3C->getZExtValue() >= VT.getVectorNumElements())
8332 return getUNDEF(VT);
8333
8334 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8335 if (N3.isUndef())
8336 return getUNDEF(VT);
8337
8338 // If inserting poison, just use the input vector.
8339 if (N2.getOpcode() == ISD::POISON)
8340 return N1;
8341
8342 // Inserting undef into undef/poison is still undef.
8343 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8344 return getUNDEF(VT);
8345
8346 // If the inserted element is an UNDEF, just use the input vector.
8347 // But not if skipping the insert could make the result more poisonous.
8348 if (N2.isUndef()) {
8349 if (N3C && VT.isFixedLengthVector()) {
8350 APInt EltMask =
8351 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8352 if (isGuaranteedNotToBePoison(N1, EltMask))
8353 return N1;
8354 } else if (isGuaranteedNotToBePoison(N1))
8355 return N1;
8356 }
8357 break;
8358 }
8359 case ISD::INSERT_SUBVECTOR: {
8360 // If inserting poison, just use the input vector,
8361 if (N2.getOpcode() == ISD::POISON)
8362 return N1;
8363
8364 // Inserting undef into undef/poison is still undef.
8365 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8366 return getUNDEF(VT);
8367
8368 EVT N2VT = N2.getValueType();
8369 assert(VT == N1.getValueType() &&
8370 "Dest and insert subvector source types must match!");
8371 assert(VT.isVector() && N2VT.isVector() &&
8372 "Insert subvector VTs must be vectors!");
8374 "Insert subvector VTs must have the same element type!");
8375 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8376 "Cannot insert a scalable vector into a fixed length vector!");
8377 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8379 "Insert subvector must be from smaller vector to larger vector!");
8381 "Insert subvector index must be constant");
8382 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8383 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8385 "Insert subvector overflow!");
8387 TLI->getVectorIdxWidth(getDataLayout()) &&
8388 "Constant index for INSERT_SUBVECTOR has an invalid size");
8389
8390 // Trivial insertion.
8391 if (VT == N2VT)
8392 return N2;
8393
8394 // If this is an insert of an extracted vector into an undef/poison vector,
8395 // we can just use the input to the extract. But not if skipping the
8396 // extract+insert could make the result more poisonous.
8397 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8398 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8399 if (N1.getOpcode() == ISD::POISON)
8400 return N2.getOperand(0);
8401 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8402 unsigned LoBit = N3->getAsZExtVal();
8403 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8404 APInt EltMask =
8405 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8406 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8407 return N2.getOperand(0);
8408 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8409 return N2.getOperand(0);
8410 }
8411
8412 // If the inserted subvector is UNDEF, just use the input vector.
8413 // But not if skipping the insert could make the result more poisonous.
8414 if (N2.isUndef()) {
8415 if (VT.isFixedLengthVector()) {
8416 unsigned LoBit = N3->getAsZExtVal();
8417 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8418 APInt EltMask =
8419 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8420 if (isGuaranteedNotToBePoison(N1, EltMask))
8421 return N1;
8422 } else if (isGuaranteedNotToBePoison(N1))
8423 return N1;
8424 }
8425 break;
8426 }
8427 case ISD::BITCAST:
8428 // Fold bit_convert nodes from a type to themselves.
8429 if (N1.getValueType() == VT)
8430 return N1;
8431 break;
8432 case ISD::VP_TRUNCATE:
8433 case ISD::VP_SIGN_EXTEND:
8434 case ISD::VP_ZERO_EXTEND:
8435 // Don't create noop casts.
8436 if (N1.getValueType() == VT)
8437 return N1;
8438 break;
8439 case ISD::VECTOR_COMPRESS: {
8440 [[maybe_unused]] EVT VecVT = N1.getValueType();
8441 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8442 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8443 assert(VT == VecVT && "Vector and result type don't match.");
8444 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8445 "All inputs must be vectors.");
8446 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8448 "Vector and mask must have same number of elements.");
8449
8450 if (N1.isUndef() || N2.isUndef())
8451 return N3;
8452
8453 break;
8454 }
8459 [[maybe_unused]] EVT AccVT = N1.getValueType();
8460 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8461 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8462 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8463 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8464 "node to have the same type!");
8465 assert(VT.isVector() && VT == AccVT &&
8466 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8467 "the same type as its result!");
8469 AccVT.getVectorElementCount()) &&
8470 "Expected the element count of the second and third operands of the "
8471 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8472 "element count of the first operand and the result!");
8474 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8475 "node to have an element type which is the same as or smaller than "
8476 "the element type of the first operand and result!");
8477 break;
8478 }
8479 }
8480
8481 // Perform trivial constant folding for arithmetic operators.
8482 switch (Opcode) {
8483 case ISD::FMA:
8484 case ISD::FMAD:
8485 case ISD::SETCC:
8486 case ISD::FSHL:
8487 case ISD::FSHR:
8488 if (SDValue SV =
8489 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8490 return SV;
8491 break;
8492 }
8493
8494 // Memoize node if it doesn't produce a glue result.
8495 SDNode *N;
8496 SDVTList VTs = getVTList(VT);
8497 SDValue Ops[] = {N1, N2, N3};
8498 if (VT != MVT::Glue) {
8500 AddNodeIDNode(ID, Opcode, VTs, Ops);
8501 void *IP = nullptr;
8502 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8503 E->intersectFlagsWith(Flags);
8504 return SDValue(E, 0);
8505 }
8506
8507 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8508 N->setFlags(Flags);
8509 createOperands(N, Ops);
8510 CSEMap.InsertNode(N, IP);
8511 } else {
8512 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8513 createOperands(N, Ops);
8514 }
8515
8516 InsertNode(N);
8517 SDValue V = SDValue(N, 0);
8518 NewSDValueDbgMsg(V, "Creating new node: ", this);
8519 return V;
8520}
8521
8522SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8523 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8524 const SDNodeFlags Flags) {
8525 SDValue Ops[] = { N1, N2, N3, N4 };
8526 return getNode(Opcode, DL, VT, Ops, Flags);
8527}
8528
8529SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8530 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8531 SDNodeFlags Flags;
8532 if (Inserter)
8533 Flags = Inserter->getFlags();
8534 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8535}
8536
8537SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8538 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8539 SDValue N5, const SDNodeFlags Flags) {
8540 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8541 return getNode(Opcode, DL, VT, Ops, Flags);
8542}
8543
8544SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8545 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8546 SDValue N5) {
8547 SDNodeFlags Flags;
8548 if (Inserter)
8549 Flags = Inserter->getFlags();
8550 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8551}
8552
8553/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8554/// the incoming stack arguments to be loaded from the stack.
8556 SmallVector<SDValue, 8> ArgChains;
8557
8558 // Include the original chain at the beginning of the list. When this is
8559 // used by target LowerCall hooks, this helps legalize find the
8560 // CALLSEQ_BEGIN node.
8561 ArgChains.push_back(Chain);
8562
8563 // Add a chain value for each stack argument.
8564 for (SDNode *U : getEntryNode().getNode()->users())
8565 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8566 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8567 if (FI->getIndex() < 0)
8568 ArgChains.push_back(SDValue(L, 1));
8569
8570 // Build a tokenfactor for all the chains.
8571 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8572}
8573
8574/// getMemsetValue - Vectorized representation of the memset value
8575/// operand.
8577 const SDLoc &dl) {
8578 assert(!Value.isUndef());
8579
8580 unsigned NumBits = VT.getScalarSizeInBits();
8582 assert(C->getAPIntValue().getBitWidth() == 8);
8583 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8584 if (VT.isInteger()) {
8585 bool IsOpaque = VT.getSizeInBits() > 64 ||
8586 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8587 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8588 }
8589 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8590 }
8591
8592 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8593 EVT IntVT = VT.getScalarType();
8594 if (!IntVT.isInteger())
8595 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8596
8597 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8598 if (NumBits > 8) {
8599 // Use a multiplication with 0x010101... to extend the input to the
8600 // required length.
8601 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8602 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8603 DAG.getConstant(Magic, dl, IntVT));
8604 }
8605
8606 if (VT != Value.getValueType() && !VT.isInteger())
8607 Value = DAG.getBitcast(VT.getScalarType(), Value);
8608 if (VT != Value.getValueType())
8609 Value = DAG.getSplatBuildVector(VT, dl, Value);
8610
8611 return Value;
8612}
8613
8614/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8615/// used when a memcpy is turned into a memset when the source is a constant
8616/// string ptr.
8618 const TargetLowering &TLI,
8619 const ConstantDataArraySlice &Slice) {
8620 // Handle vector with all elements zero.
8621 if (Slice.Array == nullptr) {
8622 if (VT.isInteger())
8623 return DAG.getConstant(0, dl, VT);
8624 return DAG.getNode(ISD::BITCAST, dl, VT,
8625 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8626 }
8627
8628 assert(!VT.isVector() && "Can't handle vector type here!");
8629 unsigned NumVTBits = VT.getSizeInBits();
8630 unsigned NumVTBytes = NumVTBits / 8;
8631 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8632
8633 APInt Val(NumVTBits, 0);
8634 if (DAG.getDataLayout().isLittleEndian()) {
8635 for (unsigned i = 0; i != NumBytes; ++i)
8636 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8637 } else {
8638 for (unsigned i = 0; i != NumBytes; ++i)
8639 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8640 }
8641
8642 // If the "cost" of materializing the integer immediate is less than the cost
8643 // of a load, then it is cost effective to turn the load into the immediate.
8644 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8645 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8646 return DAG.getConstant(Val, dl, VT);
8647 return SDValue();
8648}
8649
8651 const SDLoc &DL,
8652 const SDNodeFlags Flags) {
8653 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8654 return getMemBasePlusOffset(Base, Index, DL, Flags);
8655}
8656
8658 const SDLoc &DL,
8659 const SDNodeFlags Flags) {
8660 assert(Offset.getValueType().isInteger());
8661 EVT BasePtrVT = Ptr.getValueType();
8662 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8663 BasePtrVT))
8664 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8665 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8666 SDNodeFlags AddFlags = Flags;
8667 AddFlags.setInBounds(false);
8668 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8669}
8670
8671/// Returns true if memcpy source is constant data.
8673 uint64_t SrcDelta = 0;
8674 GlobalAddressSDNode *G = nullptr;
8675 if (Src.getOpcode() == ISD::GlobalAddress)
8677 else if (Src->isAnyAdd() &&
8678 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8679 Src.getOperand(1).getOpcode() == ISD::Constant) {
8680 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8681 SrcDelta = Src.getConstantOperandVal(1);
8682 }
8683 if (!G)
8684 return false;
8685
8686 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8687 SrcDelta + G->getOffset());
8688}
8689
8691 SelectionDAG &DAG) {
8692 // On Darwin, -Os means optimize for size without hurting performance, so
8693 // only really optimize for size when -Oz (MinSize) is used.
8695 return MF.getFunction().hasMinSize();
8696 return DAG.shouldOptForSize();
8697}
8698
8700 SmallVector<SDValue, 32> &OutChains, unsigned From,
8701 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8702 SmallVector<SDValue, 16> &OutStoreChains) {
8703 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8704 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8705 SmallVector<SDValue, 16> GluedLoadChains;
8706 for (unsigned i = From; i < To; ++i) {
8707 OutChains.push_back(OutLoadChains[i]);
8708 GluedLoadChains.push_back(OutLoadChains[i]);
8709 }
8710
8711 // Chain for all loads.
8712 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8713 GluedLoadChains);
8714
8715 for (unsigned i = From; i < To; ++i) {
8716 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8717 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8718 ST->getBasePtr(), ST->getMemoryVT(),
8719 ST->getMemOperand());
8720 OutChains.push_back(NewStore);
8721 }
8722}
8723
8725 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8726 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8727 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8728 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8729 // Turn a memcpy of undef to nop.
8730 // FIXME: We need to honor volatile even is Src is undef.
8731 if (Src.isUndef())
8732 return Chain;
8733
8734 // Expand memcpy to a series of load and store ops if the size operand falls
8735 // below a certain threshold.
8736 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8737 // rather than maybe a humongous number of loads and stores.
8738 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8739 const DataLayout &DL = DAG.getDataLayout();
8740 LLVMContext &C = *DAG.getContext();
8741 std::vector<EVT> MemOps;
8742 bool DstAlignCanChange = false;
8744 MachineFrameInfo &MFI = MF.getFrameInfo();
8745 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8747 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8748 DstAlignCanChange = true;
8749 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8750 if (!SrcAlign || Alignment > *SrcAlign)
8751 SrcAlign = Alignment;
8752 assert(SrcAlign && "SrcAlign must be set");
8754 // If marked as volatile, perform a copy even when marked as constant.
8755 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8756 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8757 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8758 const MemOp Op = isZeroConstant
8759 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8760 /*IsZeroMemset*/ true, isVol)
8761 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8762 *SrcAlign, isVol, CopyFromConstant);
8763 if (!TLI.findOptimalMemOpLowering(
8764 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8765 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8766 return SDValue();
8767
8768 if (DstAlignCanChange) {
8769 Type *Ty = MemOps[0].getTypeForEVT(C);
8770 Align NewAlign = DL.getABITypeAlign(Ty);
8771
8772 // Don't promote to an alignment that would require dynamic stack
8773 // realignment which may conflict with optimizations such as tail call
8774 // optimization.
8776 if (!TRI->hasStackRealignment(MF))
8777 if (MaybeAlign StackAlign = DL.getStackAlignment())
8778 NewAlign = std::min(NewAlign, *StackAlign);
8779
8780 if (NewAlign > Alignment) {
8781 // Give the stack frame object a larger alignment if needed.
8782 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8783 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8784 Alignment = NewAlign;
8785 }
8786 }
8787
8788 // Prepare AAInfo for loads/stores after lowering this memcpy.
8789 AAMDNodes NewAAInfo = AAInfo;
8790 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8791
8792 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8793 bool isConstant =
8794 BatchAA && SrcVal &&
8795 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8796
8797 MachineMemOperand::Flags MMOFlags =
8799 SmallVector<SDValue, 16> OutLoadChains;
8800 SmallVector<SDValue, 16> OutStoreChains;
8801 SmallVector<SDValue, 32> OutChains;
8802 unsigned NumMemOps = MemOps.size();
8803 uint64_t SrcOff = 0, DstOff = 0;
8804 for (unsigned i = 0; i != NumMemOps; ++i) {
8805 EVT VT = MemOps[i];
8806 unsigned VTSize = VT.getSizeInBits() / 8;
8807 SDValue Value, Store;
8808
8809 if (VTSize > Size) {
8810 // Issuing an unaligned load / store pair that overlaps with the previous
8811 // pair. Adjust the offset accordingly.
8812 assert(i == NumMemOps-1 && i != 0);
8813 SrcOff -= VTSize - Size;
8814 DstOff -= VTSize - Size;
8815 }
8816
8817 if (CopyFromConstant &&
8818 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8819 // It's unlikely a store of a vector immediate can be done in a single
8820 // instruction. It would require a load from a constantpool first.
8821 // We only handle zero vectors here.
8822 // FIXME: Handle other cases where store of vector immediate is done in
8823 // a single instruction.
8824 ConstantDataArraySlice SubSlice;
8825 if (SrcOff < Slice.Length) {
8826 SubSlice = Slice;
8827 SubSlice.move(SrcOff);
8828 } else {
8829 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8830 SubSlice.Array = nullptr;
8831 SubSlice.Offset = 0;
8832 SubSlice.Length = VTSize;
8833 }
8834 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8835 if (Value.getNode()) {
8836 Store = DAG.getStore(
8837 Chain, dl, Value,
8838 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8839 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8840 OutChains.push_back(Store);
8841 }
8842 }
8843
8844 if (!Store.getNode()) {
8845 // The type might not be legal for the target. This should only happen
8846 // if the type is smaller than a legal type, as on PPC, so the right
8847 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8848 // to Load/Store if NVT==VT.
8849 // FIXME does the case above also need this?
8850 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8851 assert(NVT.bitsGE(VT));
8852
8853 bool isDereferenceable =
8854 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8855 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8856 if (isDereferenceable)
8858 if (isConstant)
8859 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8860
8861 Value = DAG.getExtLoad(
8862 ISD::EXTLOAD, dl, NVT, Chain,
8863 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8864 SrcPtrInfo.getWithOffset(SrcOff), VT,
8865 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8866 OutLoadChains.push_back(Value.getValue(1));
8867
8868 Store = DAG.getTruncStore(
8869 Chain, dl, Value,
8870 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8871 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8872 OutStoreChains.push_back(Store);
8873 }
8874 SrcOff += VTSize;
8875 DstOff += VTSize;
8876 Size -= VTSize;
8877 }
8878
8879 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8881 unsigned NumLdStInMemcpy = OutStoreChains.size();
8882
8883 if (NumLdStInMemcpy) {
8884 // It may be that memcpy might be converted to memset if it's memcpy
8885 // of constants. In such a case, we won't have loads and stores, but
8886 // just stores. In the absence of loads, there is nothing to gang up.
8887 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8888 // If target does not care, just leave as it.
8889 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8890 OutChains.push_back(OutLoadChains[i]);
8891 OutChains.push_back(OutStoreChains[i]);
8892 }
8893 } else {
8894 // Ld/St less than/equal limit set by target.
8895 if (NumLdStInMemcpy <= GluedLdStLimit) {
8896 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8897 NumLdStInMemcpy, OutLoadChains,
8898 OutStoreChains);
8899 } else {
8900 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8901 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8902 unsigned GlueIter = 0;
8903
8904 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8905 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8906 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8907
8908 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8909 OutLoadChains, OutStoreChains);
8910 GlueIter += GluedLdStLimit;
8911 }
8912
8913 // Residual ld/st.
8914 if (RemainingLdStInMemcpy) {
8915 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8916 RemainingLdStInMemcpy, OutLoadChains,
8917 OutStoreChains);
8918 }
8919 }
8920 }
8921 }
8922 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8923}
8924
8926 SDValue Chain, SDValue Dst, SDValue Src,
8927 uint64_t Size, Align Alignment,
8928 bool isVol, bool AlwaysInline,
8929 MachinePointerInfo DstPtrInfo,
8930 MachinePointerInfo SrcPtrInfo,
8931 const AAMDNodes &AAInfo) {
8932 // Turn a memmove of undef to nop.
8933 // FIXME: We need to honor volatile even is Src is undef.
8934 if (Src.isUndef())
8935 return Chain;
8936
8937 // Expand memmove to a series of load and store ops if the size operand falls
8938 // below a certain threshold.
8939 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8940 const DataLayout &DL = DAG.getDataLayout();
8941 LLVMContext &C = *DAG.getContext();
8942 std::vector<EVT> MemOps;
8943 bool DstAlignCanChange = false;
8945 MachineFrameInfo &MFI = MF.getFrameInfo();
8946 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8948 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8949 DstAlignCanChange = true;
8950 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8951 if (!SrcAlign || Alignment > *SrcAlign)
8952 SrcAlign = Alignment;
8953 assert(SrcAlign && "SrcAlign must be set");
8954 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8955 if (!TLI.findOptimalMemOpLowering(
8956 C, MemOps, Limit,
8957 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8958 /*IsVolatile*/ true),
8959 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8960 MF.getFunction().getAttributes()))
8961 return SDValue();
8962
8963 if (DstAlignCanChange) {
8964 Type *Ty = MemOps[0].getTypeForEVT(C);
8965 Align NewAlign = DL.getABITypeAlign(Ty);
8966
8967 // Don't promote to an alignment that would require dynamic stack
8968 // realignment which may conflict with optimizations such as tail call
8969 // optimization.
8971 if (!TRI->hasStackRealignment(MF))
8972 if (MaybeAlign StackAlign = DL.getStackAlignment())
8973 NewAlign = std::min(NewAlign, *StackAlign);
8974
8975 if (NewAlign > Alignment) {
8976 // Give the stack frame object a larger alignment if needed.
8977 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8978 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8979 Alignment = NewAlign;
8980 }
8981 }
8982
8983 // Prepare AAInfo for loads/stores after lowering this memmove.
8984 AAMDNodes NewAAInfo = AAInfo;
8985 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8986
8987 MachineMemOperand::Flags MMOFlags =
8989 uint64_t SrcOff = 0, DstOff = 0;
8990 SmallVector<SDValue, 8> LoadValues;
8991 SmallVector<SDValue, 8> LoadChains;
8992 SmallVector<SDValue, 8> OutChains;
8993 unsigned NumMemOps = MemOps.size();
8994 for (unsigned i = 0; i < NumMemOps; i++) {
8995 EVT VT = MemOps[i];
8996 unsigned VTSize = VT.getSizeInBits() / 8;
8997 SDValue Value;
8998
8999 bool isDereferenceable =
9000 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9001 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9002 if (isDereferenceable)
9004
9005 Value = DAG.getLoad(
9006 VT, dl, Chain,
9007 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9008 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
9009 LoadValues.push_back(Value);
9010 LoadChains.push_back(Value.getValue(1));
9011 SrcOff += VTSize;
9012 }
9013 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9014 OutChains.clear();
9015 for (unsigned i = 0; i < NumMemOps; i++) {
9016 EVT VT = MemOps[i];
9017 unsigned VTSize = VT.getSizeInBits() / 8;
9018 SDValue Store;
9019
9020 Store = DAG.getStore(
9021 Chain, dl, LoadValues[i],
9022 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9023 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9024 OutChains.push_back(Store);
9025 DstOff += VTSize;
9026 }
9027
9028 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9029}
9030
9031/// Lower the call to 'memset' intrinsic function into a series of store
9032/// operations.
9033///
9034/// \param DAG Selection DAG where lowered code is placed.
9035/// \param dl Link to corresponding IR location.
9036/// \param Chain Control flow dependency.
9037/// \param Dst Pointer to destination memory location.
9038/// \param Src Value of byte to write into the memory.
9039/// \param Size Number of bytes to write.
9040/// \param Alignment Alignment of the destination in bytes.
9041/// \param isVol True if destination is volatile.
9042/// \param AlwaysInline Makes sure no function call is generated.
9043/// \param DstPtrInfo IR information on the memory pointer.
9044/// \returns New head in the control flow, if lowering was successful, empty
9045/// SDValue otherwise.
9046///
9047/// The function tries to replace 'llvm.memset' intrinsic with several store
9048/// operations and value calculation code. This is usually profitable for small
9049/// memory size or when the semantic requires inlining.
9051 SDValue Chain, SDValue Dst, SDValue Src,
9052 uint64_t Size, Align Alignment, bool isVol,
9053 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9054 const AAMDNodes &AAInfo) {
9055 // Turn a memset of undef to nop.
9056 // FIXME: We need to honor volatile even is Src is undef.
9057 if (Src.isUndef())
9058 return Chain;
9059
9060 // Expand memset to a series of load/store ops if the size operand
9061 // falls below a certain threshold.
9062 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9063 std::vector<EVT> MemOps;
9064 bool DstAlignCanChange = false;
9065 LLVMContext &C = *DAG.getContext();
9067 MachineFrameInfo &MFI = MF.getFrameInfo();
9068 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9070 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9071 DstAlignCanChange = true;
9072 bool IsZeroVal = isNullConstant(Src);
9073 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9074
9075 if (!TLI.findOptimalMemOpLowering(
9076 C, MemOps, Limit,
9077 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9078 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9079 return SDValue();
9080
9081 if (DstAlignCanChange) {
9082 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9083 const DataLayout &DL = DAG.getDataLayout();
9084 Align NewAlign = DL.getABITypeAlign(Ty);
9085
9086 // Don't promote to an alignment that would require dynamic stack
9087 // realignment which may conflict with optimizations such as tail call
9088 // optimization.
9090 if (!TRI->hasStackRealignment(MF))
9091 if (MaybeAlign StackAlign = DL.getStackAlignment())
9092 NewAlign = std::min(NewAlign, *StackAlign);
9093
9094 if (NewAlign > Alignment) {
9095 // Give the stack frame object a larger alignment if needed.
9096 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9097 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9098 Alignment = NewAlign;
9099 }
9100 }
9101
9102 SmallVector<SDValue, 8> OutChains;
9103 uint64_t DstOff = 0;
9104 unsigned NumMemOps = MemOps.size();
9105
9106 // Find the largest store and generate the bit pattern for it.
9107 EVT LargestVT = MemOps[0];
9108 for (unsigned i = 1; i < NumMemOps; i++)
9109 if (MemOps[i].bitsGT(LargestVT))
9110 LargestVT = MemOps[i];
9111 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9112
9113 // Prepare AAInfo for loads/stores after lowering this memset.
9114 AAMDNodes NewAAInfo = AAInfo;
9115 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9116
9117 for (unsigned i = 0; i < NumMemOps; i++) {
9118 EVT VT = MemOps[i];
9119 unsigned VTSize = VT.getSizeInBits() / 8;
9120 if (VTSize > Size) {
9121 // Issuing an unaligned load / store pair that overlaps with the previous
9122 // pair. Adjust the offset accordingly.
9123 assert(i == NumMemOps-1 && i != 0);
9124 DstOff -= VTSize - Size;
9125 }
9126
9127 // If this store is smaller than the largest store see whether we can get
9128 // the smaller value for free with a truncate or extract vector element and
9129 // then store.
9130 SDValue Value = MemSetValue;
9131 if (VT.bitsLT(LargestVT)) {
9132 unsigned Index;
9133 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9134 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9135 if (!LargestVT.isVector() && !VT.isVector() &&
9136 TLI.isTruncateFree(LargestVT, VT))
9137 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9138 else if (LargestVT.isVector() && !VT.isVector() &&
9140 LargestVT.getTypeForEVT(*DAG.getContext()),
9141 VT.getSizeInBits(), Index) &&
9142 TLI.isTypeLegal(SVT) &&
9143 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9144 // Target which can combine store(extractelement VectorTy, Idx) can get
9145 // the smaller value for free.
9146 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9147 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9148 } else
9149 Value = getMemsetValue(Src, VT, DAG, dl);
9150 }
9151 assert(Value.getValueType() == VT && "Value with wrong type.");
9152 SDValue Store = DAG.getStore(
9153 Chain, dl, Value,
9154 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9155 DstPtrInfo.getWithOffset(DstOff), Alignment,
9157 NewAAInfo);
9158 OutChains.push_back(Store);
9159 DstOff += VT.getSizeInBits() / 8;
9160 Size -= VTSize;
9161 }
9162
9163 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9164}
9165
9167 unsigned AS) {
9168 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9169 // pointer operands can be losslessly bitcasted to pointers of address space 0
9170 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9171 report_fatal_error("cannot lower memory intrinsic in address space " +
9172 Twine(AS));
9173 }
9174}
9175
9177 const SelectionDAG *SelDAG,
9178 bool AllowReturnsFirstArg) {
9179 if (!CI || !CI->isTailCall())
9180 return false;
9181 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9182 // helper symbol we lower to.
9183 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9184 AllowReturnsFirstArg &&
9186}
9187
9188static std::pair<SDValue, SDValue>
9191 const CallInst *CI, RTLIB::Libcall Call,
9192 SelectionDAG *DAG, const TargetLowering *TLI) {
9193 RTLIB::LibcallImpl LCImpl = TLI->getLibcallImpl(Call);
9194
9195 if (LCImpl == RTLIB::Unsupported)
9196 return {};
9197
9199 bool IsTailCall =
9200 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9201 SDValue Callee =
9202 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9203
9204 CLI.setDebugLoc(dl)
9205 .setChain(Chain)
9206 .setLibCallee(TLI->getLibcallImplCallingConv(LCImpl), CI->getType(),
9207 Callee, std::move(Args))
9208 .setTailCall(IsTailCall);
9209
9210 return TLI->LowerCallTo(CLI);
9211}
9212
9213std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9214 const SDLoc &dl, SDValue S1,
9215 SDValue S2,
9216 const CallInst *CI) {
9218 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9219 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9220 RTLIB::STRSTR, this, TLI);
9221}
9222
9223std::pair<SDValue, SDValue>
9225 SDValue Mem1, SDValue Size, const CallInst *CI) {
9226 RTLIB::LibcallImpl MemcmpImpl = getLibcalls().getLibcallImpl(RTLIB::MEMCMP);
9227 if (MemcmpImpl == RTLIB::Unsupported)
9228 return {};
9229
9232 {Mem0, PT},
9233 {Mem1, PT},
9235
9237 bool IsTailCall =
9238 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9239
9240 CLI.setDebugLoc(dl)
9241 .setChain(Chain)
9242 .setLibCallee(
9243 getLibcalls().getLibcallImplCallingConv(MemcmpImpl),
9245 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9246 std::move(Args))
9247 .setTailCall(IsTailCall);
9248
9249 return TLI->LowerCallTo(CLI);
9250}
9251
9252std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9253 const SDLoc &dl,
9254 SDValue Dst, SDValue Src,
9255 const CallInst *CI) {
9256 RTLIB::LibcallImpl LCImpl = TLI->getLibcallImpl(RTLIB::STRCPY);
9257 if (LCImpl == RTLIB::Unsupported)
9258 return {};
9259
9261 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9262
9264 bool IsTailCall =
9265 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9266
9267 CLI.setDebugLoc(dl)
9268 .setChain(Chain)
9269 .setLibCallee(
9270 TLI->getLibcallImplCallingConv(LCImpl), CI->getType(),
9271 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9272 std::move(Args))
9273 .setTailCall(IsTailCall);
9274
9275 return TLI->LowerCallTo(CLI);
9276}
9277
9278std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9279 const SDLoc &dl,
9280 SDValue Src,
9281 const CallInst *CI) {
9282 RTLIB::LibcallImpl StrlenImpl = getLibcalls().getLibcallImpl(RTLIB::STRLEN);
9283 if (StrlenImpl == RTLIB::Unsupported)
9284 return {};
9285
9286 // Emit a library call.
9289
9291 bool IsTailCall =
9292 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9293
9294 CLI.setDebugLoc(dl)
9295 .setChain(Chain)
9296 .setLibCallee(TLI->getLibcallImplCallingConv(StrlenImpl), CI->getType(),
9298 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9299 std::move(Args))
9300 .setTailCall(IsTailCall);
9301
9302 return TLI->LowerCallTo(CLI);
9303}
9304
9306 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9307 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9308 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9309 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9310 BatchAAResults *BatchAA) {
9311 // Check to see if we should lower the memcpy to loads and stores first.
9312 // For cases within the target-specified limits, this is the best choice.
9314 if (ConstantSize) {
9315 // Memcpy with size zero? Just return the original chain.
9316 if (ConstantSize->isZero())
9317 return Chain;
9318
9320 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9321 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9322 if (Result.getNode())
9323 return Result;
9324 }
9325
9326 // Then check to see if we should lower the memcpy with target-specific
9327 // code. If the target chooses to do this, this is the next best.
9328 if (TSI) {
9329 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9330 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9331 DstPtrInfo, SrcPtrInfo);
9332 if (Result.getNode())
9333 return Result;
9334 }
9335
9336 // If we really need inline code and the target declined to provide it,
9337 // use a (potentially long) sequence of loads and stores.
9338 if (AlwaysInline) {
9339 assert(ConstantSize && "AlwaysInline requires a constant size!");
9341 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9342 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9343 }
9344
9347
9348 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9349 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9350 // respect volatile, so they may do things like read or write memory
9351 // beyond the given memory regions. But fixing this isn't easy, and most
9352 // people don't care.
9353
9354 // Emit a library call.
9357 Args.emplace_back(Dst, PtrTy);
9358 Args.emplace_back(Src, PtrTy);
9359 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9360 // FIXME: pass in SDLoc
9362 bool IsTailCall = false;
9363 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9364
9365 if (OverrideTailCall.has_value()) {
9366 IsTailCall = *OverrideTailCall;
9367 } else {
9368 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9369 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9370 }
9371
9372 CLI.setDebugLoc(dl)
9373 .setChain(Chain)
9374 .setLibCallee(
9375 getLibcalls().getLibcallImplCallingConv(MemCpyImpl),
9376 Dst.getValueType().getTypeForEVT(*getContext()),
9377 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9378 std::move(Args))
9380 .setTailCall(IsTailCall);
9381
9382 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9383 return CallResult.second;
9384}
9385
9387 SDValue Dst, SDValue Src, SDValue Size,
9388 Type *SizeTy, unsigned ElemSz,
9389 bool isTailCall,
9390 MachinePointerInfo DstPtrInfo,
9391 MachinePointerInfo SrcPtrInfo) {
9392 // Emit a library call.
9395 Args.emplace_back(Dst, ArgTy);
9396 Args.emplace_back(Src, ArgTy);
9397 Args.emplace_back(Size, SizeTy);
9398
9399 RTLIB::Libcall LibraryCall =
9401 RTLIB::LibcallImpl LibcallImpl = getLibcalls().getLibcallImpl(LibraryCall);
9402 if (LibcallImpl == RTLIB::Unsupported)
9403 report_fatal_error("Unsupported element size");
9404
9406 CLI.setDebugLoc(dl)
9407 .setChain(Chain)
9408 .setLibCallee(
9409 getLibcalls().getLibcallImplCallingConv(LibcallImpl),
9411 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9412 std::move(Args))
9414 .setTailCall(isTailCall);
9415
9416 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9417 return CallResult.second;
9418}
9419
9421 SDValue Src, SDValue Size, Align Alignment,
9422 bool isVol, const CallInst *CI,
9423 std::optional<bool> OverrideTailCall,
9424 MachinePointerInfo DstPtrInfo,
9425 MachinePointerInfo SrcPtrInfo,
9426 const AAMDNodes &AAInfo,
9427 BatchAAResults *BatchAA) {
9428 // Check to see if we should lower the memmove to loads and stores first.
9429 // For cases within the target-specified limits, this is the best choice.
9431 if (ConstantSize) {
9432 // Memmove with size zero? Just return the original chain.
9433 if (ConstantSize->isZero())
9434 return Chain;
9435
9437 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9438 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9439 if (Result.getNode())
9440 return Result;
9441 }
9442
9443 // Then check to see if we should lower the memmove with target-specific
9444 // code. If the target chooses to do this, this is the next best.
9445 if (TSI) {
9446 SDValue Result =
9447 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9448 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9449 if (Result.getNode())
9450 return Result;
9451 }
9452
9455
9456 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9457 // not be safe. See memcpy above for more details.
9458
9459 // Emit a library call.
9462 Args.emplace_back(Dst, PtrTy);
9463 Args.emplace_back(Src, PtrTy);
9464 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9465 // FIXME: pass in SDLoc
9467
9468 RTLIB::LibcallImpl MemmoveImpl = getLibcalls().getLibcallImpl(RTLIB::MEMMOVE);
9469
9470 bool IsTailCall = false;
9471 if (OverrideTailCall.has_value()) {
9472 IsTailCall = *OverrideTailCall;
9473 } else {
9474 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9475 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9476 }
9477
9478 CLI.setDebugLoc(dl)
9479 .setChain(Chain)
9480 .setLibCallee(
9481 getLibcalls().getLibcallImplCallingConv(MemmoveImpl),
9482 Dst.getValueType().getTypeForEVT(*getContext()),
9483 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9484 std::move(Args))
9486 .setTailCall(IsTailCall);
9487
9488 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9489 return CallResult.second;
9490}
9491
9493 SDValue Dst, SDValue Src, SDValue Size,
9494 Type *SizeTy, unsigned ElemSz,
9495 bool isTailCall,
9496 MachinePointerInfo DstPtrInfo,
9497 MachinePointerInfo SrcPtrInfo) {
9498 // Emit a library call.
9500 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9501 Args.emplace_back(Dst, IntPtrTy);
9502 Args.emplace_back(Src, IntPtrTy);
9503 Args.emplace_back(Size, SizeTy);
9504
9505 RTLIB::Libcall LibraryCall =
9507 RTLIB::LibcallImpl LibcallImpl = getLibcalls().getLibcallImpl(LibraryCall);
9508 if (LibcallImpl == RTLIB::Unsupported)
9509 report_fatal_error("Unsupported element size");
9510
9512 CLI.setDebugLoc(dl)
9513 .setChain(Chain)
9514 .setLibCallee(
9515 getLibcalls().getLibcallImplCallingConv(LibcallImpl),
9517 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9518 std::move(Args))
9520 .setTailCall(isTailCall);
9521
9522 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9523 return CallResult.second;
9524}
9525
9527 SDValue Src, SDValue Size, Align Alignment,
9528 bool isVol, bool AlwaysInline,
9529 const CallInst *CI,
9530 MachinePointerInfo DstPtrInfo,
9531 const AAMDNodes &AAInfo) {
9532 // Check to see if we should lower the memset to stores first.
9533 // For cases within the target-specified limits, this is the best choice.
9535 if (ConstantSize) {
9536 // Memset with size zero? Just return the original chain.
9537 if (ConstantSize->isZero())
9538 return Chain;
9539
9540 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9541 ConstantSize->getZExtValue(), Alignment,
9542 isVol, false, DstPtrInfo, AAInfo);
9543
9544 if (Result.getNode())
9545 return Result;
9546 }
9547
9548 // Then check to see if we should lower the memset with target-specific
9549 // code. If the target chooses to do this, this is the next best.
9550 if (TSI) {
9551 SDValue Result = TSI->EmitTargetCodeForMemset(
9552 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9553 if (Result.getNode())
9554 return Result;
9555 }
9556
9557 // If we really need inline code and the target declined to provide it,
9558 // use a (potentially long) sequence of loads and stores.
9559 if (AlwaysInline) {
9560 assert(ConstantSize && "AlwaysInline requires a constant size!");
9561 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9562 ConstantSize->getZExtValue(), Alignment,
9563 isVol, true, DstPtrInfo, AAInfo);
9564 assert(Result &&
9565 "getMemsetStores must return a valid sequence when AlwaysInline");
9566 return Result;
9567 }
9568
9570
9571 // Emit a library call.
9572 auto &Ctx = *getContext();
9573 const auto& DL = getDataLayout();
9574
9576 // FIXME: pass in SDLoc
9577 CLI.setDebugLoc(dl).setChain(Chain);
9578
9579 RTLIB::LibcallImpl BzeroImpl = TLI->getLibcallImpl(RTLIB::BZERO);
9580 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9581
9582 // If zeroing out and bzero is present, use it.
9583 if (UseBZero) {
9585 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9586 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9587 CLI.setLibCallee(
9588 TLI->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9589 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9590 } else {
9591 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9592
9594 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9595 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9596 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9597 CLI.setLibCallee(TLI->getLibcallImplCallingConv(MemsetImpl),
9598 Dst.getValueType().getTypeForEVT(Ctx),
9599 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9600 std::move(Args));
9601 }
9602
9603 RTLIB::LibcallImpl MemsetImpl = TLI->getLibcallImpl(RTLIB::MEMSET);
9604 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9605
9606 // If we're going to use bzero, make sure not to tail call unless the
9607 // subsequent return doesn't need a value, as bzero doesn't return the first
9608 // arg unlike memset.
9609 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9610 bool IsTailCall =
9611 CI && CI->isTailCall() &&
9612 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9613 CLI.setDiscardResult().setTailCall(IsTailCall);
9614
9615 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9616 return CallResult.second;
9617}
9618
9621 Type *SizeTy, unsigned ElemSz,
9622 bool isTailCall,
9623 MachinePointerInfo DstPtrInfo) {
9624 // Emit a library call.
9626 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9627 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9628 Args.emplace_back(Size, SizeTy);
9629
9630 RTLIB::Libcall LibraryCall =
9632 RTLIB::LibcallImpl LibcallImpl = TLI->getLibcallImpl(LibraryCall);
9633 if (LibcallImpl == RTLIB::Unsupported)
9634 report_fatal_error("Unsupported element size");
9635
9637 CLI.setDebugLoc(dl)
9638 .setChain(Chain)
9639 .setLibCallee(
9640 TLI->getLibcallImplCallingConv(LibcallImpl),
9642 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9643 std::move(Args))
9645 .setTailCall(isTailCall);
9646
9647 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9648 return CallResult.second;
9649}
9650
9651SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9653 MachineMemOperand *MMO,
9654 ISD::LoadExtType ExtType) {
9656 AddNodeIDNode(ID, Opcode, VTList, Ops);
9657 ID.AddInteger(MemVT.getRawBits());
9658 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9659 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9660 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9661 ID.AddInteger(MMO->getFlags());
9662 void* IP = nullptr;
9663 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9664 E->refineAlignment(MMO);
9665 E->refineRanges(MMO);
9666 return SDValue(E, 0);
9667 }
9668
9669 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9670 VTList, MemVT, MMO, ExtType);
9671 createOperands(N, Ops);
9672
9673 CSEMap.InsertNode(N, IP);
9674 InsertNode(N);
9675 SDValue V(N, 0);
9676 NewSDValueDbgMsg(V, "Creating new node: ", this);
9677 return V;
9678}
9679
9681 EVT MemVT, SDVTList VTs, SDValue Chain,
9682 SDValue Ptr, SDValue Cmp, SDValue Swp,
9683 MachineMemOperand *MMO) {
9684 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9686 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9687
9688 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9689 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9690}
9691
9692SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9693 SDValue Chain, SDValue Ptr, SDValue Val,
9694 MachineMemOperand *MMO) {
9695 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9696 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9697 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9698 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9699 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9700 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9701 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9702 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9703 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9704 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9705 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9706 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9707 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9708 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9709 Opcode == ISD::ATOMIC_STORE) &&
9710 "Invalid Atomic Op");
9711
9712 EVT VT = Val.getValueType();
9713
9714 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9715 getVTList(VT, MVT::Other);
9716 SDValue Ops[] = {Chain, Ptr, Val};
9717 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9718}
9719
9721 EVT MemVT, EVT VT, SDValue Chain,
9722 SDValue Ptr, MachineMemOperand *MMO) {
9723 SDVTList VTs = getVTList(VT, MVT::Other);
9724 SDValue Ops[] = {Chain, Ptr};
9725 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9726}
9727
9728/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9730 if (Ops.size() == 1)
9731 return Ops[0];
9732
9734 VTs.reserve(Ops.size());
9735 for (const SDValue &Op : Ops)
9736 VTs.push_back(Op.getValueType());
9737 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9738}
9739
9741 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9742 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9744 const AAMDNodes &AAInfo) {
9745 if (Size.hasValue() && !Size.getValue())
9747
9749 MachineMemOperand *MMO =
9750 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9751
9752 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9753}
9754
9756 SDVTList VTList,
9757 ArrayRef<SDValue> Ops, EVT MemVT,
9758 MachineMemOperand *MMO) {
9759 assert(
9760 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9761 Opcode == ISD::PREFETCH ||
9762 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9763 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9764 "Opcode is not a memory-accessing opcode!");
9765
9766 // Memoize the node unless it returns a glue result.
9768 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9770 AddNodeIDNode(ID, Opcode, VTList, Ops);
9771 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9772 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9773 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9774 ID.AddInteger(MMO->getFlags());
9775 ID.AddInteger(MemVT.getRawBits());
9776 void *IP = nullptr;
9777 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9778 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9779 return SDValue(E, 0);
9780 }
9781
9782 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9783 VTList, MemVT, MMO);
9784 createOperands(N, Ops);
9785
9786 CSEMap.InsertNode(N, IP);
9787 } else {
9788 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9789 VTList, MemVT, MMO);
9790 createOperands(N, Ops);
9791 }
9792 InsertNode(N);
9793 SDValue V(N, 0);
9794 NewSDValueDbgMsg(V, "Creating new node: ", this);
9795 return V;
9796}
9797
9799 SDValue Chain, int FrameIndex) {
9800 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9801 const auto VTs = getVTList(MVT::Other);
9802 SDValue Ops[2] = {
9803 Chain,
9804 getFrameIndex(FrameIndex,
9805 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9806 true)};
9807
9809 AddNodeIDNode(ID, Opcode, VTs, Ops);
9810 ID.AddInteger(FrameIndex);
9811 void *IP = nullptr;
9812 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9813 return SDValue(E, 0);
9814
9815 LifetimeSDNode *N =
9816 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9817 createOperands(N, Ops);
9818 CSEMap.InsertNode(N, IP);
9819 InsertNode(N);
9820 SDValue V(N, 0);
9821 NewSDValueDbgMsg(V, "Creating new node: ", this);
9822 return V;
9823}
9824
9826 uint64_t Guid, uint64_t Index,
9827 uint32_t Attr) {
9828 const unsigned Opcode = ISD::PSEUDO_PROBE;
9829 const auto VTs = getVTList(MVT::Other);
9830 SDValue Ops[] = {Chain};
9832 AddNodeIDNode(ID, Opcode, VTs, Ops);
9833 ID.AddInteger(Guid);
9834 ID.AddInteger(Index);
9835 void *IP = nullptr;
9836 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9837 return SDValue(E, 0);
9838
9839 auto *N = newSDNode<PseudoProbeSDNode>(
9840 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9841 createOperands(N, Ops);
9842 CSEMap.InsertNode(N, IP);
9843 InsertNode(N);
9844 SDValue V(N, 0);
9845 NewSDValueDbgMsg(V, "Creating new node: ", this);
9846 return V;
9847}
9848
9849/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9850/// MachinePointerInfo record from it. This is particularly useful because the
9851/// code generator has many cases where it doesn't bother passing in a
9852/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9854 SelectionDAG &DAG, SDValue Ptr,
9855 int64_t Offset = 0) {
9856 // If this is FI+Offset, we can model it.
9857 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9859 FI->getIndex(), Offset);
9860
9861 // If this is (FI+Offset1)+Offset2, we can model it.
9862 if (Ptr.getOpcode() != ISD::ADD ||
9865 return Info;
9866
9867 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9869 DAG.getMachineFunction(), FI,
9870 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9871}
9872
9873/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9874/// MachinePointerInfo record from it. This is particularly useful because the
9875/// code generator has many cases where it doesn't bother passing in a
9876/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9878 SelectionDAG &DAG, SDValue Ptr,
9879 SDValue OffsetOp) {
9880 // If the 'Offset' value isn't a constant, we can't handle this.
9882 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9883 if (OffsetOp.isUndef())
9884 return InferPointerInfo(Info, DAG, Ptr);
9885 return Info;
9886}
9887
9889 EVT VT, const SDLoc &dl, SDValue Chain,
9890 SDValue Ptr, SDValue Offset,
9891 MachinePointerInfo PtrInfo, EVT MemVT,
9892 Align Alignment,
9893 MachineMemOperand::Flags MMOFlags,
9894 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9895 assert(Chain.getValueType() == MVT::Other &&
9896 "Invalid chain type");
9897
9898 MMOFlags |= MachineMemOperand::MOLoad;
9899 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9900 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9901 // clients.
9902 if (PtrInfo.V.isNull())
9903 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9904
9905 TypeSize Size = MemVT.getStoreSize();
9907 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9908 Alignment, AAInfo, Ranges);
9909 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9910}
9911
9913 EVT VT, const SDLoc &dl, SDValue Chain,
9914 SDValue Ptr, SDValue Offset, EVT MemVT,
9915 MachineMemOperand *MMO) {
9916 if (VT == MemVT) {
9917 ExtType = ISD::NON_EXTLOAD;
9918 } else if (ExtType == ISD::NON_EXTLOAD) {
9919 assert(VT == MemVT && "Non-extending load from different memory type!");
9920 } else {
9921 // Extending load.
9922 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9923 "Should only be an extending load, not truncating!");
9924 assert(VT.isInteger() == MemVT.isInteger() &&
9925 "Cannot convert from FP to Int or Int -> FP!");
9926 assert(VT.isVector() == MemVT.isVector() &&
9927 "Cannot use an ext load to convert to or from a vector!");
9928 assert((!VT.isVector() ||
9930 "Cannot use an ext load to change the number of vector elements!");
9931 }
9932
9933 assert((!MMO->getRanges() ||
9935 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9936 MemVT.isInteger())) &&
9937 "Range metadata and load type must match!");
9938
9939 bool Indexed = AM != ISD::UNINDEXED;
9940 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9941
9942 SDVTList VTs = Indexed ?
9943 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9944 SDValue Ops[] = { Chain, Ptr, Offset };
9946 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9947 ID.AddInteger(MemVT.getRawBits());
9948 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9949 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9950 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9951 ID.AddInteger(MMO->getFlags());
9952 void *IP = nullptr;
9953 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9954 E->refineAlignment(MMO);
9955 E->refineRanges(MMO);
9956 return SDValue(E, 0);
9957 }
9958 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9959 ExtType, MemVT, MMO);
9960 createOperands(N, Ops);
9961
9962 CSEMap.InsertNode(N, IP);
9963 InsertNode(N);
9964 SDValue V(N, 0);
9965 NewSDValueDbgMsg(V, "Creating new node: ", this);
9966 return V;
9967}
9968
9970 SDValue Ptr, MachinePointerInfo PtrInfo,
9971 MaybeAlign Alignment,
9972 MachineMemOperand::Flags MMOFlags,
9973 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9974 SDValue Undef = getUNDEF(Ptr.getValueType());
9975 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9976 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9977}
9978
9980 SDValue Ptr, MachineMemOperand *MMO) {
9981 SDValue Undef = getUNDEF(Ptr.getValueType());
9982 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9983 VT, MMO);
9984}
9985
9987 EVT VT, SDValue Chain, SDValue Ptr,
9988 MachinePointerInfo PtrInfo, EVT MemVT,
9989 MaybeAlign Alignment,
9990 MachineMemOperand::Flags MMOFlags,
9991 const AAMDNodes &AAInfo) {
9992 SDValue Undef = getUNDEF(Ptr.getValueType());
9993 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9994 MemVT, Alignment, MMOFlags, AAInfo);
9995}
9996
9998 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
9999 MachineMemOperand *MMO) {
10000 SDValue Undef = getUNDEF(Ptr.getValueType());
10001 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10002 MemVT, MMO);
10003}
10004
10008 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10009 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10010 // Don't propagate the invariant or dereferenceable flags.
10011 auto MMOFlags =
10012 LD->getMemOperand()->getFlags() &
10014 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10015 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10016 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10017}
10018
10020 SDValue Ptr, MachinePointerInfo PtrInfo,
10021 Align Alignment,
10022 MachineMemOperand::Flags MMOFlags,
10023 const AAMDNodes &AAInfo) {
10024 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10025
10026 MMOFlags |= MachineMemOperand::MOStore;
10027 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10028
10029 if (PtrInfo.V.isNull())
10030 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10031
10034 MachineMemOperand *MMO =
10035 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10036 return getStore(Chain, dl, Val, Ptr, MMO);
10037}
10038
10040 SDValue Ptr, MachineMemOperand *MMO) {
10041 SDValue Undef = getUNDEF(Ptr.getValueType());
10042 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10044}
10045
10047 SDValue Ptr, SDValue Offset, EVT SVT,
10049 bool IsTruncating) {
10050 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10051 EVT VT = Val.getValueType();
10052 if (VT == SVT) {
10053 IsTruncating = false;
10054 } else if (!IsTruncating) {
10055 assert(VT == SVT && "No-truncating store from different memory type!");
10056 } else {
10058 "Should only be a truncating store, not extending!");
10059 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10060 assert(VT.isVector() == SVT.isVector() &&
10061 "Cannot use trunc store to convert to or from a vector!");
10062 assert((!VT.isVector() ||
10064 "Cannot use trunc store to change the number of vector elements!");
10065 }
10066
10067 bool Indexed = AM != ISD::UNINDEXED;
10068 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10069 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10070 : getVTList(MVT::Other);
10071 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10074 ID.AddInteger(SVT.getRawBits());
10075 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10076 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10077 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10078 ID.AddInteger(MMO->getFlags());
10079 void *IP = nullptr;
10080 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10081 cast<StoreSDNode>(E)->refineAlignment(MMO);
10082 return SDValue(E, 0);
10083 }
10084 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10085 IsTruncating, SVT, MMO);
10086 createOperands(N, Ops);
10087
10088 CSEMap.InsertNode(N, IP);
10089 InsertNode(N);
10090 SDValue V(N, 0);
10091 NewSDValueDbgMsg(V, "Creating new node: ", this);
10092 return V;
10093}
10094
10096 SDValue Ptr, MachinePointerInfo PtrInfo,
10097 EVT SVT, Align Alignment,
10098 MachineMemOperand::Flags MMOFlags,
10099 const AAMDNodes &AAInfo) {
10100 assert(Chain.getValueType() == MVT::Other &&
10101 "Invalid chain type");
10102
10103 MMOFlags |= MachineMemOperand::MOStore;
10104 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10105
10106 if (PtrInfo.V.isNull())
10107 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10108
10110 MachineMemOperand *MMO = MF.getMachineMemOperand(
10111 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10112 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10113}
10114
10116 SDValue Ptr, EVT SVT,
10117 MachineMemOperand *MMO) {
10118 SDValue Undef = getUNDEF(Ptr.getValueType());
10119 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10120}
10121
10125 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10126 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10127 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10128 ST->getMemoryVT(), ST->getMemOperand(), AM,
10129 ST->isTruncatingStore());
10130}
10131
10133 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10134 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10135 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10136 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10137 const MDNode *Ranges, bool IsExpanding) {
10138 MMOFlags |= MachineMemOperand::MOLoad;
10139 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10140 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10141 // clients.
10142 if (PtrInfo.V.isNull())
10143 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10144
10145 TypeSize Size = MemVT.getStoreSize();
10147 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10148 Alignment, AAInfo, Ranges);
10149 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10150 MMO, IsExpanding);
10151}
10152
10154 ISD::LoadExtType ExtType, EVT VT,
10155 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10156 SDValue Offset, SDValue Mask, SDValue EVL,
10157 EVT MemVT, MachineMemOperand *MMO,
10158 bool IsExpanding) {
10159 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10160 assert(Mask.getValueType().getVectorElementCount() ==
10161 VT.getVectorElementCount() &&
10162 "Vector width mismatch between mask and data");
10163
10164 bool Indexed = AM != ISD::UNINDEXED;
10165 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10166
10167 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10168 : getVTList(VT, MVT::Other);
10169 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10171 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10172 ID.AddInteger(MemVT.getRawBits());
10173 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10174 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10175 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10176 ID.AddInteger(MMO->getFlags());
10177 void *IP = nullptr;
10178 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10179 E->refineAlignment(MMO);
10180 E->refineRanges(MMO);
10181 return SDValue(E, 0);
10182 }
10183 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10184 ExtType, IsExpanding, MemVT, MMO);
10185 createOperands(N, Ops);
10186
10187 CSEMap.InsertNode(N, IP);
10188 InsertNode(N);
10189 SDValue V(N, 0);
10190 NewSDValueDbgMsg(V, "Creating new node: ", this);
10191 return V;
10192}
10193
10195 SDValue Ptr, SDValue Mask, SDValue EVL,
10196 MachinePointerInfo PtrInfo,
10197 MaybeAlign Alignment,
10198 MachineMemOperand::Flags MMOFlags,
10199 const AAMDNodes &AAInfo, const MDNode *Ranges,
10200 bool IsExpanding) {
10201 SDValue Undef = getUNDEF(Ptr.getValueType());
10202 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10203 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10204 IsExpanding);
10205}
10206
10208 SDValue Ptr, SDValue Mask, SDValue EVL,
10209 MachineMemOperand *MMO, bool IsExpanding) {
10210 SDValue Undef = getUNDEF(Ptr.getValueType());
10211 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10212 Mask, EVL, VT, MMO, IsExpanding);
10213}
10214
10216 EVT VT, SDValue Chain, SDValue Ptr,
10217 SDValue Mask, SDValue EVL,
10218 MachinePointerInfo PtrInfo, EVT MemVT,
10219 MaybeAlign Alignment,
10220 MachineMemOperand::Flags MMOFlags,
10221 const AAMDNodes &AAInfo, bool IsExpanding) {
10222 SDValue Undef = getUNDEF(Ptr.getValueType());
10223 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10224 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10225 IsExpanding);
10226}
10227
10229 EVT VT, SDValue Chain, SDValue Ptr,
10230 SDValue Mask, SDValue EVL, EVT MemVT,
10231 MachineMemOperand *MMO, bool IsExpanding) {
10232 SDValue Undef = getUNDEF(Ptr.getValueType());
10233 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10234 EVL, MemVT, MMO, IsExpanding);
10235}
10236
10240 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10241 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10242 // Don't propagate the invariant or dereferenceable flags.
10243 auto MMOFlags =
10244 LD->getMemOperand()->getFlags() &
10246 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10247 LD->getChain(), Base, Offset, LD->getMask(),
10248 LD->getVectorLength(), LD->getPointerInfo(),
10249 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10250 nullptr, LD->isExpandingLoad());
10251}
10252
10254 SDValue Ptr, SDValue Offset, SDValue Mask,
10255 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10256 ISD::MemIndexedMode AM, bool IsTruncating,
10257 bool IsCompressing) {
10258 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10259 assert(Mask.getValueType().getVectorElementCount() ==
10261 "Vector width mismatch between mask and data");
10262
10263 bool Indexed = AM != ISD::UNINDEXED;
10264 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10265 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10266 : getVTList(MVT::Other);
10267 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10269 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10270 ID.AddInteger(MemVT.getRawBits());
10271 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10272 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10273 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10274 ID.AddInteger(MMO->getFlags());
10275 void *IP = nullptr;
10276 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10277 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10278 return SDValue(E, 0);
10279 }
10280 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10281 IsTruncating, IsCompressing, MemVT, MMO);
10282 createOperands(N, Ops);
10283
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 Val, SDValue Ptr, SDValue Mask,
10293 SDValue EVL, MachinePointerInfo PtrInfo,
10294 EVT SVT, Align Alignment,
10295 MachineMemOperand::Flags MMOFlags,
10296 const AAMDNodes &AAInfo,
10297 bool IsCompressing) {
10298 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10299
10300 MMOFlags |= MachineMemOperand::MOStore;
10301 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10302
10303 if (PtrInfo.V.isNull())
10304 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10305
10307 MachineMemOperand *MMO = MF.getMachineMemOperand(
10308 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10309 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10310 IsCompressing);
10311}
10312
10314 SDValue Val, SDValue Ptr, SDValue Mask,
10315 SDValue EVL, EVT SVT,
10316 MachineMemOperand *MMO,
10317 bool IsCompressing) {
10318 EVT VT = Val.getValueType();
10319
10320 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10321 if (VT == SVT)
10322 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10323 EVL, VT, MMO, ISD::UNINDEXED,
10324 /*IsTruncating*/ false, IsCompressing);
10325
10327 "Should only be a truncating store, not extending!");
10328 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10329 assert(VT.isVector() == SVT.isVector() &&
10330 "Cannot use trunc store to convert to or from a vector!");
10331 assert((!VT.isVector() ||
10333 "Cannot use trunc store to change the number of vector elements!");
10334
10335 SDVTList VTs = getVTList(MVT::Other);
10336 SDValue Undef = getUNDEF(Ptr.getValueType());
10337 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10339 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10340 ID.AddInteger(SVT.getRawBits());
10341 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10342 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10343 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10344 ID.AddInteger(MMO->getFlags());
10345 void *IP = nullptr;
10346 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10347 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10348 return SDValue(E, 0);
10349 }
10350 auto *N =
10351 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10352 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10353 createOperands(N, Ops);
10354
10355 CSEMap.InsertNode(N, IP);
10356 InsertNode(N);
10357 SDValue V(N, 0);
10358 NewSDValueDbgMsg(V, "Creating new node: ", this);
10359 return V;
10360}
10361
10365 auto *ST = cast<VPStoreSDNode>(OrigStore);
10366 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10367 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10368 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10369 Offset, ST->getMask(), ST->getVectorLength()};
10371 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10372 ID.AddInteger(ST->getMemoryVT().getRawBits());
10373 ID.AddInteger(ST->getRawSubclassData());
10374 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10375 ID.AddInteger(ST->getMemOperand()->getFlags());
10376 void *IP = nullptr;
10377 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10378 return SDValue(E, 0);
10379
10380 auto *N = newSDNode<VPStoreSDNode>(
10381 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10382 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10383 createOperands(N, Ops);
10384
10385 CSEMap.InsertNode(N, IP);
10386 InsertNode(N);
10387 SDValue V(N, 0);
10388 NewSDValueDbgMsg(V, "Creating new node: ", this);
10389 return V;
10390}
10391
10393 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10394 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10395 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10396 bool Indexed = AM != ISD::UNINDEXED;
10397 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10398
10399 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10400 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10401 : getVTList(VT, MVT::Other);
10403 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10404 ID.AddInteger(VT.getRawBits());
10405 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10406 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10407 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10408
10409 void *IP = nullptr;
10410 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10411 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10412 return SDValue(E, 0);
10413 }
10414
10415 auto *N =
10416 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10417 ExtType, IsExpanding, MemVT, MMO);
10418 createOperands(N, Ops);
10419 CSEMap.InsertNode(N, IP);
10420 InsertNode(N);
10421 SDValue V(N, 0);
10422 NewSDValueDbgMsg(V, "Creating new node: ", this);
10423 return V;
10424}
10425
10427 SDValue Ptr, SDValue Stride,
10428 SDValue Mask, SDValue EVL,
10429 MachineMemOperand *MMO,
10430 bool IsExpanding) {
10431 SDValue Undef = getUNDEF(Ptr.getValueType());
10432 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10433 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10434}
10435
10437 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10438 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10439 MachineMemOperand *MMO, bool IsExpanding) {
10440 SDValue Undef = getUNDEF(Ptr.getValueType());
10441 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10442 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10443}
10444
10446 SDValue Val, SDValue Ptr,
10447 SDValue Offset, SDValue Stride,
10448 SDValue Mask, SDValue EVL, EVT MemVT,
10449 MachineMemOperand *MMO,
10451 bool IsTruncating, bool IsCompressing) {
10452 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10453 bool Indexed = AM != ISD::UNINDEXED;
10454 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10455 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10456 : getVTList(MVT::Other);
10457 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10459 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10460 ID.AddInteger(MemVT.getRawBits());
10461 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10462 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10463 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10464 void *IP = nullptr;
10465 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10466 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10467 return SDValue(E, 0);
10468 }
10469 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10470 VTs, AM, IsTruncating,
10471 IsCompressing, MemVT, MMO);
10472 createOperands(N, Ops);
10473
10474 CSEMap.InsertNode(N, IP);
10475 InsertNode(N);
10476 SDValue V(N, 0);
10477 NewSDValueDbgMsg(V, "Creating new node: ", this);
10478 return V;
10479}
10480
10482 SDValue Val, SDValue Ptr,
10483 SDValue Stride, SDValue Mask,
10484 SDValue EVL, EVT SVT,
10485 MachineMemOperand *MMO,
10486 bool IsCompressing) {
10487 EVT VT = Val.getValueType();
10488
10489 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10490 if (VT == SVT)
10491 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10492 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10493 /*IsTruncating*/ false, IsCompressing);
10494
10496 "Should only be a truncating store, not extending!");
10497 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10498 assert(VT.isVector() == SVT.isVector() &&
10499 "Cannot use trunc store to convert to or from a vector!");
10500 assert((!VT.isVector() ||
10502 "Cannot use trunc store to change the number of vector elements!");
10503
10504 SDVTList VTs = getVTList(MVT::Other);
10505 SDValue Undef = getUNDEF(Ptr.getValueType());
10506 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10508 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10509 ID.AddInteger(SVT.getRawBits());
10510 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10511 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10512 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10513 void *IP = nullptr;
10514 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10515 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10516 return SDValue(E, 0);
10517 }
10518 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10519 VTs, ISD::UNINDEXED, true,
10520 IsCompressing, SVT, MMO);
10521 createOperands(N, Ops);
10522
10523 CSEMap.InsertNode(N, IP);
10524 InsertNode(N);
10525 SDValue V(N, 0);
10526 NewSDValueDbgMsg(V, "Creating new node: ", this);
10527 return V;
10528}
10529
10532 ISD::MemIndexType IndexType) {
10533 assert(Ops.size() == 6 && "Incompatible number of operands");
10534
10536 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10537 ID.AddInteger(VT.getRawBits());
10538 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10539 dl.getIROrder(), VTs, VT, MMO, IndexType));
10540 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10541 ID.AddInteger(MMO->getFlags());
10542 void *IP = nullptr;
10543 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10544 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10545 return SDValue(E, 0);
10546 }
10547
10548 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10549 VT, MMO, IndexType);
10550 createOperands(N, Ops);
10551
10552 assert(N->getMask().getValueType().getVectorElementCount() ==
10553 N->getValueType(0).getVectorElementCount() &&
10554 "Vector width mismatch between mask and data");
10555 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10556 N->getValueType(0).getVectorElementCount().isScalable() &&
10557 "Scalable flags of index and data do not match");
10559 N->getIndex().getValueType().getVectorElementCount(),
10560 N->getValueType(0).getVectorElementCount()) &&
10561 "Vector width mismatch between index and data");
10562 assert(isa<ConstantSDNode>(N->getScale()) &&
10563 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10564 "Scale should be a constant power of 2");
10565
10566 CSEMap.InsertNode(N, IP);
10567 InsertNode(N);
10568 SDValue V(N, 0);
10569 NewSDValueDbgMsg(V, "Creating new node: ", this);
10570 return V;
10571}
10572
10575 MachineMemOperand *MMO,
10576 ISD::MemIndexType IndexType) {
10577 assert(Ops.size() == 7 && "Incompatible number of operands");
10578
10580 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10581 ID.AddInteger(VT.getRawBits());
10582 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10583 dl.getIROrder(), VTs, VT, MMO, IndexType));
10584 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10585 ID.AddInteger(MMO->getFlags());
10586 void *IP = nullptr;
10587 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10588 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10589 return SDValue(E, 0);
10590 }
10591 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10592 VT, MMO, IndexType);
10593 createOperands(N, Ops);
10594
10595 assert(N->getMask().getValueType().getVectorElementCount() ==
10596 N->getValue().getValueType().getVectorElementCount() &&
10597 "Vector width mismatch between mask and data");
10598 assert(
10599 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10600 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10601 "Scalable flags of index and data do not match");
10603 N->getIndex().getValueType().getVectorElementCount(),
10604 N->getValue().getValueType().getVectorElementCount()) &&
10605 "Vector width mismatch between index and data");
10606 assert(isa<ConstantSDNode>(N->getScale()) &&
10607 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10608 "Scale should be a constant power of 2");
10609
10610 CSEMap.InsertNode(N, IP);
10611 InsertNode(N);
10612 SDValue V(N, 0);
10613 NewSDValueDbgMsg(V, "Creating new node: ", this);
10614 return V;
10615}
10616
10619 SDValue PassThru, EVT MemVT,
10620 MachineMemOperand *MMO,
10622 ISD::LoadExtType ExtTy, bool isExpanding) {
10623 bool Indexed = AM != ISD::UNINDEXED;
10624 assert((Indexed || Offset.isUndef()) &&
10625 "Unindexed masked load with an offset!");
10626 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10627 : getVTList(VT, MVT::Other);
10628 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10631 ID.AddInteger(MemVT.getRawBits());
10632 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10633 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10634 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10635 ID.AddInteger(MMO->getFlags());
10636 void *IP = nullptr;
10637 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10638 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10639 return SDValue(E, 0);
10640 }
10641 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10642 AM, ExtTy, isExpanding, MemVT, MMO);
10643 createOperands(N, Ops);
10644
10645 CSEMap.InsertNode(N, IP);
10646 InsertNode(N);
10647 SDValue V(N, 0);
10648 NewSDValueDbgMsg(V, "Creating new node: ", this);
10649 return V;
10650}
10651
10656 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10657 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10658 Offset, LD->getMask(), LD->getPassThru(),
10659 LD->getMemoryVT(), LD->getMemOperand(), AM,
10660 LD->getExtensionType(), LD->isExpandingLoad());
10661}
10662
10665 SDValue Mask, EVT MemVT,
10666 MachineMemOperand *MMO,
10667 ISD::MemIndexedMode AM, bool IsTruncating,
10668 bool IsCompressing) {
10669 assert(Chain.getValueType() == MVT::Other &&
10670 "Invalid chain type");
10671 bool Indexed = AM != ISD::UNINDEXED;
10672 assert((Indexed || Offset.isUndef()) &&
10673 "Unindexed masked store with an offset!");
10674 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10675 : getVTList(MVT::Other);
10676 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10679 ID.AddInteger(MemVT.getRawBits());
10680 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10681 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10682 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10683 ID.AddInteger(MMO->getFlags());
10684 void *IP = nullptr;
10685 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10686 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10687 return SDValue(E, 0);
10688 }
10689 auto *N =
10690 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10691 IsTruncating, IsCompressing, MemVT, MMO);
10692 createOperands(N, Ops);
10693
10694 CSEMap.InsertNode(N, IP);
10695 InsertNode(N);
10696 SDValue V(N, 0);
10697 NewSDValueDbgMsg(V, "Creating new node: ", this);
10698 return V;
10699}
10700
10705 assert(ST->getOffset().isUndef() &&
10706 "Masked store is already a indexed store!");
10707 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10708 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10709 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10710}
10711
10714 MachineMemOperand *MMO,
10715 ISD::MemIndexType IndexType,
10716 ISD::LoadExtType ExtTy) {
10717 assert(Ops.size() == 6 && "Incompatible number of operands");
10718
10721 ID.AddInteger(MemVT.getRawBits());
10722 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10723 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10724 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10725 ID.AddInteger(MMO->getFlags());
10726 void *IP = nullptr;
10727 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10728 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10729 return SDValue(E, 0);
10730 }
10731
10732 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10733 VTs, MemVT, MMO, IndexType, ExtTy);
10734 createOperands(N, Ops);
10735
10736 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10737 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10738 assert(N->getMask().getValueType().getVectorElementCount() ==
10739 N->getValueType(0).getVectorElementCount() &&
10740 "Vector width mismatch between mask and data");
10741 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10742 N->getValueType(0).getVectorElementCount().isScalable() &&
10743 "Scalable flags of index and data do not match");
10745 N->getIndex().getValueType().getVectorElementCount(),
10746 N->getValueType(0).getVectorElementCount()) &&
10747 "Vector width mismatch between index and data");
10748 assert(isa<ConstantSDNode>(N->getScale()) &&
10749 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10750 "Scale should be a constant power of 2");
10751
10752 CSEMap.InsertNode(N, IP);
10753 InsertNode(N);
10754 SDValue V(N, 0);
10755 NewSDValueDbgMsg(V, "Creating new node: ", this);
10756 return V;
10757}
10758
10761 MachineMemOperand *MMO,
10762 ISD::MemIndexType IndexType,
10763 bool IsTrunc) {
10764 assert(Ops.size() == 6 && "Incompatible number of operands");
10765
10768 ID.AddInteger(MemVT.getRawBits());
10769 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10770 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10771 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10772 ID.AddInteger(MMO->getFlags());
10773 void *IP = nullptr;
10774 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10775 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10776 return SDValue(E, 0);
10777 }
10778
10779 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10780 VTs, MemVT, MMO, IndexType, IsTrunc);
10781 createOperands(N, Ops);
10782
10783 assert(N->getMask().getValueType().getVectorElementCount() ==
10784 N->getValue().getValueType().getVectorElementCount() &&
10785 "Vector width mismatch between mask and data");
10786 assert(
10787 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10788 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10789 "Scalable flags of index and data do not match");
10791 N->getIndex().getValueType().getVectorElementCount(),
10792 N->getValue().getValueType().getVectorElementCount()) &&
10793 "Vector width mismatch between index and data");
10794 assert(isa<ConstantSDNode>(N->getScale()) &&
10795 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10796 "Scale should be a constant power of 2");
10797
10798 CSEMap.InsertNode(N, IP);
10799 InsertNode(N);
10800 SDValue V(N, 0);
10801 NewSDValueDbgMsg(V, "Creating new node: ", this);
10802 return V;
10803}
10804
10806 const SDLoc &dl, ArrayRef<SDValue> Ops,
10807 MachineMemOperand *MMO,
10808 ISD::MemIndexType IndexType) {
10809 assert(Ops.size() == 7 && "Incompatible number of operands");
10810
10813 ID.AddInteger(MemVT.getRawBits());
10814 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10815 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10816 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10817 ID.AddInteger(MMO->getFlags());
10818 void *IP = nullptr;
10819 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10820 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10821 return SDValue(E, 0);
10822 }
10823
10824 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10825 VTs, MemVT, MMO, IndexType);
10826 createOperands(N, Ops);
10827
10828 assert(N->getMask().getValueType().getVectorElementCount() ==
10829 N->getIndex().getValueType().getVectorElementCount() &&
10830 "Vector width mismatch between mask and data");
10831 assert(isa<ConstantSDNode>(N->getScale()) &&
10832 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10833 "Scale should be a constant power of 2");
10834 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10835
10836 CSEMap.InsertNode(N, IP);
10837 InsertNode(N);
10838 SDValue V(N, 0);
10839 NewSDValueDbgMsg(V, "Creating new node: ", this);
10840 return V;
10841}
10842
10844 SDValue Ptr, SDValue Mask, SDValue EVL,
10845 MachineMemOperand *MMO) {
10846 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10847 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10849 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10850 ID.AddInteger(VT.getRawBits());
10851 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10852 VTs, VT, MMO));
10853 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10854 ID.AddInteger(MMO->getFlags());
10855 void *IP = nullptr;
10856 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10857 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10858 return SDValue(E, 0);
10859 }
10860 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10861 VT, MMO);
10862 createOperands(N, Ops);
10863
10864 CSEMap.InsertNode(N, IP);
10865 InsertNode(N);
10866 SDValue V(N, 0);
10867 NewSDValueDbgMsg(V, "Creating new node: ", this);
10868 return V;
10869}
10870
10872 EVT MemVT, MachineMemOperand *MMO) {
10873 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10874 SDVTList VTs = getVTList(MVT::Other);
10875 SDValue Ops[] = {Chain, Ptr};
10878 ID.AddInteger(MemVT.getRawBits());
10879 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10880 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10881 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10882 ID.AddInteger(MMO->getFlags());
10883 void *IP = nullptr;
10884 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10885 return SDValue(E, 0);
10886
10887 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10888 dl.getDebugLoc(), VTs, MemVT, MMO);
10889 createOperands(N, Ops);
10890
10891 CSEMap.InsertNode(N, IP);
10892 InsertNode(N);
10893 SDValue V(N, 0);
10894 NewSDValueDbgMsg(V, "Creating new node: ", this);
10895 return V;
10896}
10897
10899 EVT MemVT, MachineMemOperand *MMO) {
10900 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10901 SDVTList VTs = getVTList(MVT::Other);
10902 SDValue Ops[] = {Chain, Ptr};
10905 ID.AddInteger(MemVT.getRawBits());
10906 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10907 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10908 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10909 ID.AddInteger(MMO->getFlags());
10910 void *IP = nullptr;
10911 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10912 return SDValue(E, 0);
10913
10914 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10915 dl.getDebugLoc(), VTs, MemVT, MMO);
10916 createOperands(N, Ops);
10917
10918 CSEMap.InsertNode(N, IP);
10919 InsertNode(N);
10920 SDValue V(N, 0);
10921 NewSDValueDbgMsg(V, "Creating new node: ", this);
10922 return V;
10923}
10924
10926 // select undef, T, F --> T (if T is a constant), otherwise F
10927 // select, ?, undef, F --> F
10928 // select, ?, T, undef --> T
10929 if (Cond.isUndef())
10930 return isConstantValueOfAnyType(T) ? T : F;
10931 if (T.isUndef())
10933 if (F.isUndef())
10935
10936 // select true, T, F --> T
10937 // select false, T, F --> F
10938 if (auto C = isBoolConstant(Cond))
10939 return *C ? T : F;
10940
10941 // select ?, T, T --> T
10942 if (T == F)
10943 return T;
10944
10945 return SDValue();
10946}
10947
10949 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10950 if (X.isUndef())
10951 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10952 // shift X, undef --> undef (because it may shift by the bitwidth)
10953 if (Y.isUndef())
10954 return getUNDEF(X.getValueType());
10955
10956 // shift 0, Y --> 0
10957 // shift X, 0 --> X
10959 return X;
10960
10961 // shift X, C >= bitwidth(X) --> undef
10962 // All vector elements must be too big (or undef) to avoid partial undefs.
10963 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10964 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10965 };
10966 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10967 return getUNDEF(X.getValueType());
10968
10969 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10970 if (X.getValueType().getScalarType() == MVT::i1)
10971 return X;
10972
10973 return SDValue();
10974}
10975
10977 SDNodeFlags Flags) {
10978 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10979 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10980 // operation is poison. That result can be relaxed to undef.
10981 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10982 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10983 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10984 (YC && YC->getValueAPF().isNaN());
10985 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10986 (YC && YC->getValueAPF().isInfinity());
10987
10988 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10989 return getUNDEF(X.getValueType());
10990
10991 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10992 return getUNDEF(X.getValueType());
10993
10994 if (!YC)
10995 return SDValue();
10996
10997 // X + -0.0 --> X
10998 if (Opcode == ISD::FADD)
10999 if (YC->getValueAPF().isNegZero())
11000 return X;
11001
11002 // X - +0.0 --> X
11003 if (Opcode == ISD::FSUB)
11004 if (YC->getValueAPF().isPosZero())
11005 return X;
11006
11007 // X * 1.0 --> X
11008 // X / 1.0 --> X
11009 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11010 if (YC->getValueAPF().isExactlyValue(1.0))
11011 return X;
11012
11013 // X * 0.0 --> 0.0
11014 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11015 if (YC->getValueAPF().isZero())
11016 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11017
11018 return SDValue();
11019}
11020
11022 SDValue Ptr, SDValue SV, unsigned Align) {
11023 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11024 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11025}
11026
11027SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11029 switch (Ops.size()) {
11030 case 0: return getNode(Opcode, DL, VT);
11031 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11032 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11033 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11034 default: break;
11035 }
11036
11037 // Copy from an SDUse array into an SDValue array for use with
11038 // the regular getNode logic.
11040 return getNode(Opcode, DL, VT, NewOps);
11041}
11042
11043SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11045 SDNodeFlags Flags;
11046 if (Inserter)
11047 Flags = Inserter->getFlags();
11048 return getNode(Opcode, DL, VT, Ops, Flags);
11049}
11050
11051SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11052 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11053 unsigned NumOps = Ops.size();
11054 switch (NumOps) {
11055 case 0: return getNode(Opcode, DL, VT);
11056 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11057 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11058 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11059 default: break;
11060 }
11061
11062#ifndef NDEBUG
11063 for (const auto &Op : Ops)
11064 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11065 "Operand is DELETED_NODE!");
11066#endif
11067
11068 switch (Opcode) {
11069 default: break;
11070 case ISD::BUILD_VECTOR:
11071 // Attempt to simplify BUILD_VECTOR.
11072 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11073 return V;
11074 break;
11076 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11077 return V;
11078 break;
11079 case ISD::SELECT_CC:
11080 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11081 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11082 "LHS and RHS of condition must have same type!");
11083 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11084 "True and False arms of SelectCC must have same type!");
11085 assert(Ops[2].getValueType() == VT &&
11086 "select_cc node must be of same type as true and false value!");
11087 assert((!Ops[0].getValueType().isVector() ||
11088 Ops[0].getValueType().getVectorElementCount() ==
11089 VT.getVectorElementCount()) &&
11090 "Expected select_cc with vector result to have the same sized "
11091 "comparison type!");
11092 break;
11093 case ISD::BR_CC:
11094 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11095 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11096 "LHS/RHS of comparison should match types!");
11097 break;
11098 case ISD::VP_ADD:
11099 case ISD::VP_SUB:
11100 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11101 if (VT.getScalarType() == MVT::i1)
11102 Opcode = ISD::VP_XOR;
11103 break;
11104 case ISD::VP_MUL:
11105 // If it is VP_MUL mask operation then turn it to VP_AND
11106 if (VT.getScalarType() == MVT::i1)
11107 Opcode = ISD::VP_AND;
11108 break;
11109 case ISD::VP_REDUCE_MUL:
11110 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11111 if (VT == MVT::i1)
11112 Opcode = ISD::VP_REDUCE_AND;
11113 break;
11114 case ISD::VP_REDUCE_ADD:
11115 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11116 if (VT == MVT::i1)
11117 Opcode = ISD::VP_REDUCE_XOR;
11118 break;
11119 case ISD::VP_REDUCE_SMAX:
11120 case ISD::VP_REDUCE_UMIN:
11121 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11122 // VP_REDUCE_AND.
11123 if (VT == MVT::i1)
11124 Opcode = ISD::VP_REDUCE_AND;
11125 break;
11126 case ISD::VP_REDUCE_SMIN:
11127 case ISD::VP_REDUCE_UMAX:
11128 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11129 // VP_REDUCE_OR.
11130 if (VT == MVT::i1)
11131 Opcode = ISD::VP_REDUCE_OR;
11132 break;
11133 }
11134
11135 // Memoize nodes.
11136 SDNode *N;
11137 SDVTList VTs = getVTList(VT);
11138
11139 if (VT != MVT::Glue) {
11141 AddNodeIDNode(ID, Opcode, VTs, Ops);
11142 void *IP = nullptr;
11143
11144 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11145 E->intersectFlagsWith(Flags);
11146 return SDValue(E, 0);
11147 }
11148
11149 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11150 createOperands(N, Ops);
11151
11152 CSEMap.InsertNode(N, IP);
11153 } else {
11154 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11155 createOperands(N, Ops);
11156 }
11157
11158 N->setFlags(Flags);
11159 InsertNode(N);
11160 SDValue V(N, 0);
11161 NewSDValueDbgMsg(V, "Creating new node: ", this);
11162 return V;
11163}
11164
11165SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11166 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11167 SDNodeFlags Flags;
11168 if (Inserter)
11169 Flags = Inserter->getFlags();
11170 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11171}
11172
11173SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11175 const SDNodeFlags Flags) {
11176 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11177}
11178
11179SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11181 SDNodeFlags Flags;
11182 if (Inserter)
11183 Flags = Inserter->getFlags();
11184 return getNode(Opcode, DL, VTList, Ops, Flags);
11185}
11186
11187SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11188 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11189 if (VTList.NumVTs == 1)
11190 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11191
11192#ifndef NDEBUG
11193 for (const auto &Op : Ops)
11194 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11195 "Operand is DELETED_NODE!");
11196#endif
11197
11198 switch (Opcode) {
11199 case ISD::SADDO:
11200 case ISD::UADDO:
11201 case ISD::SSUBO:
11202 case ISD::USUBO: {
11203 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11204 "Invalid add/sub overflow op!");
11205 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11206 Ops[0].getValueType() == Ops[1].getValueType() &&
11207 Ops[0].getValueType() == VTList.VTs[0] &&
11208 "Binary operator types must match!");
11209 SDValue N1 = Ops[0], N2 = Ops[1];
11210 canonicalizeCommutativeBinop(Opcode, N1, N2);
11211
11212 // (X +- 0) -> X with zero-overflow.
11213 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11214 /*AllowTruncation*/ true);
11215 if (N2CV && N2CV->isZero()) {
11216 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11217 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11218 }
11219
11220 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11221 VTList.VTs[1].getScalarType() == MVT::i1) {
11222 SDValue F1 = getFreeze(N1);
11223 SDValue F2 = getFreeze(N2);
11224 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11225 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11226 return getNode(ISD::MERGE_VALUES, DL, VTList,
11227 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11228 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11229 Flags);
11230 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11231 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11232 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11233 return getNode(ISD::MERGE_VALUES, DL, VTList,
11234 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11235 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11236 Flags);
11237 }
11238 }
11239 break;
11240 }
11241 case ISD::SADDO_CARRY:
11242 case ISD::UADDO_CARRY:
11243 case ISD::SSUBO_CARRY:
11244 case ISD::USUBO_CARRY:
11245 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11246 "Invalid add/sub overflow op!");
11247 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11248 Ops[0].getValueType() == Ops[1].getValueType() &&
11249 Ops[0].getValueType() == VTList.VTs[0] &&
11250 Ops[2].getValueType() == VTList.VTs[1] &&
11251 "Binary operator types must match!");
11252 break;
11253 case ISD::SMUL_LOHI:
11254 case ISD::UMUL_LOHI: {
11255 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11256 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11257 VTList.VTs[0] == Ops[0].getValueType() &&
11258 VTList.VTs[0] == Ops[1].getValueType() &&
11259 "Binary operator types must match!");
11260 // Constant fold.
11263 if (LHS && RHS) {
11264 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11265 unsigned OutWidth = Width * 2;
11266 APInt Val = LHS->getAPIntValue();
11267 APInt Mul = RHS->getAPIntValue();
11268 if (Opcode == ISD::SMUL_LOHI) {
11269 Val = Val.sext(OutWidth);
11270 Mul = Mul.sext(OutWidth);
11271 } else {
11272 Val = Val.zext(OutWidth);
11273 Mul = Mul.zext(OutWidth);
11274 }
11275 Val *= Mul;
11276
11277 SDValue Hi =
11278 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11279 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11280 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11281 }
11282 break;
11283 }
11284 case ISD::FFREXP: {
11285 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11286 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11287 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11288
11290 int FrexpExp;
11291 APFloat FrexpMant =
11292 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11293 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11294 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11295 DL, VTList.VTs[1]);
11296 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11297 }
11298
11299 break;
11300 }
11302 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11303 "Invalid STRICT_FP_EXTEND!");
11304 assert(VTList.VTs[0].isFloatingPoint() &&
11305 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11306 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11307 "STRICT_FP_EXTEND result type should be vector iff the operand "
11308 "type is vector!");
11309 assert((!VTList.VTs[0].isVector() ||
11310 VTList.VTs[0].getVectorElementCount() ==
11311 Ops[1].getValueType().getVectorElementCount()) &&
11312 "Vector element count mismatch!");
11313 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11314 "Invalid fpext node, dst <= src!");
11315 break;
11317 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11318 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11319 "STRICT_FP_ROUND result type should be vector iff the operand "
11320 "type is vector!");
11321 assert((!VTList.VTs[0].isVector() ||
11322 VTList.VTs[0].getVectorElementCount() ==
11323 Ops[1].getValueType().getVectorElementCount()) &&
11324 "Vector element count mismatch!");
11325 assert(VTList.VTs[0].isFloatingPoint() &&
11326 Ops[1].getValueType().isFloatingPoint() &&
11327 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11328 Ops[2].getOpcode() == ISD::TargetConstant &&
11329 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11330 "Invalid STRICT_FP_ROUND!");
11331 break;
11332 }
11333
11334 // Memoize the node unless it returns a glue result.
11335 SDNode *N;
11336 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11338 AddNodeIDNode(ID, Opcode, VTList, Ops);
11339 void *IP = nullptr;
11340 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11341 E->intersectFlagsWith(Flags);
11342 return SDValue(E, 0);
11343 }
11344
11345 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11346 createOperands(N, Ops);
11347 CSEMap.InsertNode(N, IP);
11348 } else {
11349 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11350 createOperands(N, Ops);
11351 }
11352
11353 N->setFlags(Flags);
11354 InsertNode(N);
11355 SDValue V(N, 0);
11356 NewSDValueDbgMsg(V, "Creating new node: ", this);
11357 return V;
11358}
11359
11360SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11361 SDVTList VTList) {
11362 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11363}
11364
11365SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11366 SDValue N1) {
11367 SDValue Ops[] = { N1 };
11368 return getNode(Opcode, DL, VTList, Ops);
11369}
11370
11371SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11372 SDValue N1, SDValue N2) {
11373 SDValue Ops[] = { N1, N2 };
11374 return getNode(Opcode, DL, VTList, Ops);
11375}
11376
11377SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11378 SDValue N1, SDValue N2, SDValue N3) {
11379 SDValue Ops[] = { N1, N2, N3 };
11380 return getNode(Opcode, DL, VTList, Ops);
11381}
11382
11383SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11384 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11385 SDValue Ops[] = { N1, N2, N3, N4 };
11386 return getNode(Opcode, DL, VTList, Ops);
11387}
11388
11389SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11390 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11391 SDValue N5) {
11392 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11393 return getNode(Opcode, DL, VTList, Ops);
11394}
11395
11397 if (!VT.isExtended())
11398 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11399
11400 return makeVTList(&(*EVTs.insert(VT).first), 1);
11401}
11402
11405 ID.AddInteger(2U);
11406 ID.AddInteger(VT1.getRawBits());
11407 ID.AddInteger(VT2.getRawBits());
11408
11409 void *IP = nullptr;
11410 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11411 if (!Result) {
11412 EVT *Array = Allocator.Allocate<EVT>(2);
11413 Array[0] = VT1;
11414 Array[1] = VT2;
11415 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11416 VTListMap.InsertNode(Result, IP);
11417 }
11418 return Result->getSDVTList();
11419}
11420
11423 ID.AddInteger(3U);
11424 ID.AddInteger(VT1.getRawBits());
11425 ID.AddInteger(VT2.getRawBits());
11426 ID.AddInteger(VT3.getRawBits());
11427
11428 void *IP = nullptr;
11429 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11430 if (!Result) {
11431 EVT *Array = Allocator.Allocate<EVT>(3);
11432 Array[0] = VT1;
11433 Array[1] = VT2;
11434 Array[2] = VT3;
11435 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11436 VTListMap.InsertNode(Result, IP);
11437 }
11438 return Result->getSDVTList();
11439}
11440
11443 ID.AddInteger(4U);
11444 ID.AddInteger(VT1.getRawBits());
11445 ID.AddInteger(VT2.getRawBits());
11446 ID.AddInteger(VT3.getRawBits());
11447 ID.AddInteger(VT4.getRawBits());
11448
11449 void *IP = nullptr;
11450 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11451 if (!Result) {
11452 EVT *Array = Allocator.Allocate<EVT>(4);
11453 Array[0] = VT1;
11454 Array[1] = VT2;
11455 Array[2] = VT3;
11456 Array[3] = VT4;
11457 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11458 VTListMap.InsertNode(Result, IP);
11459 }
11460 return Result->getSDVTList();
11461}
11462
11464 unsigned NumVTs = VTs.size();
11466 ID.AddInteger(NumVTs);
11467 for (unsigned index = 0; index < NumVTs; index++) {
11468 ID.AddInteger(VTs[index].getRawBits());
11469 }
11470
11471 void *IP = nullptr;
11472 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11473 if (!Result) {
11474 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11475 llvm::copy(VTs, Array);
11476 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11477 VTListMap.InsertNode(Result, IP);
11478 }
11479 return Result->getSDVTList();
11480}
11481
11482
11483/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11484/// specified operands. If the resultant node already exists in the DAG,
11485/// this does not modify the specified node, instead it returns the node that
11486/// already exists. If the resultant node does not exist in the DAG, the
11487/// input node is returned. As a degenerate case, if you specify the same
11488/// input operands as the node already has, the input node is returned.
11490 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11491
11492 // Check to see if there is no change.
11493 if (Op == N->getOperand(0)) return N;
11494
11495 // See if the modified node already exists.
11496 void *InsertPos = nullptr;
11497 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11498 return Existing;
11499
11500 // Nope it doesn't. Remove the node from its current place in the maps.
11501 if (InsertPos)
11502 if (!RemoveNodeFromCSEMaps(N))
11503 InsertPos = nullptr;
11504
11505 // Now we update the operands.
11506 N->OperandList[0].set(Op);
11507
11509 // If this gets put into a CSE map, add it.
11510 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11511 return N;
11512}
11513
11515 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11516
11517 // Check to see if there is no change.
11518 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11519 return N; // No operands changed, just return the input node.
11520
11521 // See if the modified node already exists.
11522 void *InsertPos = nullptr;
11523 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11524 return Existing;
11525
11526 // Nope it doesn't. Remove the node from its current place in the maps.
11527 if (InsertPos)
11528 if (!RemoveNodeFromCSEMaps(N))
11529 InsertPos = nullptr;
11530
11531 // Now we update the operands.
11532 if (N->OperandList[0] != Op1)
11533 N->OperandList[0].set(Op1);
11534 if (N->OperandList[1] != Op2)
11535 N->OperandList[1].set(Op2);
11536
11538 // If this gets put into a CSE map, add it.
11539 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11540 return N;
11541}
11542
11545 SDValue Ops[] = { Op1, Op2, Op3 };
11546 return UpdateNodeOperands(N, Ops);
11547}
11548
11551 SDValue Op3, SDValue Op4) {
11552 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11553 return UpdateNodeOperands(N, Ops);
11554}
11555
11558 SDValue Op3, SDValue Op4, SDValue Op5) {
11559 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11560 return UpdateNodeOperands(N, Ops);
11561}
11562
11565 unsigned NumOps = Ops.size();
11566 assert(N->getNumOperands() == NumOps &&
11567 "Update with wrong number of operands");
11568
11569 // If no operands changed just return the input node.
11570 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11571 return N;
11572
11573 // See if the modified node already exists.
11574 void *InsertPos = nullptr;
11575 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11576 return Existing;
11577
11578 // Nope it doesn't. Remove the node from its current place in the maps.
11579 if (InsertPos)
11580 if (!RemoveNodeFromCSEMaps(N))
11581 InsertPos = nullptr;
11582
11583 // Now we update the operands.
11584 for (unsigned i = 0; i != NumOps; ++i)
11585 if (N->OperandList[i] != Ops[i])
11586 N->OperandList[i].set(Ops[i]);
11587
11589 // If this gets put into a CSE map, add it.
11590 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11591 return N;
11592}
11593
11594/// DropOperands - Release the operands and set this node to have
11595/// zero operands.
11597 // Unlike the code in MorphNodeTo that does this, we don't need to
11598 // watch for dead nodes here.
11599 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11600 SDUse &Use = *I++;
11601 Use.set(SDValue());
11602 }
11603}
11604
11606 ArrayRef<MachineMemOperand *> NewMemRefs) {
11607 if (NewMemRefs.empty()) {
11608 N->clearMemRefs();
11609 return;
11610 }
11611
11612 // Check if we can avoid allocating by storing a single reference directly.
11613 if (NewMemRefs.size() == 1) {
11614 N->MemRefs = NewMemRefs[0];
11615 N->NumMemRefs = 1;
11616 return;
11617 }
11618
11619 MachineMemOperand **MemRefsBuffer =
11620 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11621 llvm::copy(NewMemRefs, MemRefsBuffer);
11622 N->MemRefs = MemRefsBuffer;
11623 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11624}
11625
11626/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11627/// machine opcode.
11628///
11630 EVT VT) {
11631 SDVTList VTs = getVTList(VT);
11632 return SelectNodeTo(N, MachineOpc, VTs, {});
11633}
11634
11636 EVT VT, SDValue Op1) {
11637 SDVTList VTs = getVTList(VT);
11638 SDValue Ops[] = { Op1 };
11639 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11640}
11641
11643 EVT VT, SDValue Op1,
11644 SDValue Op2) {
11645 SDVTList VTs = getVTList(VT);
11646 SDValue Ops[] = { Op1, Op2 };
11647 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11648}
11649
11651 EVT VT, SDValue Op1,
11652 SDValue Op2, SDValue Op3) {
11653 SDVTList VTs = getVTList(VT);
11654 SDValue Ops[] = { Op1, Op2, Op3 };
11655 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11656}
11657
11660 SDVTList VTs = getVTList(VT);
11661 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11662}
11663
11665 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11666 SDVTList VTs = getVTList(VT1, VT2);
11667 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11668}
11669
11671 EVT VT1, EVT VT2) {
11672 SDVTList VTs = getVTList(VT1, VT2);
11673 return SelectNodeTo(N, MachineOpc, VTs, {});
11674}
11675
11677 EVT VT1, EVT VT2, EVT VT3,
11679 SDVTList VTs = getVTList(VT1, VT2, VT3);
11680 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11681}
11682
11684 EVT VT1, EVT VT2,
11685 SDValue Op1, SDValue Op2) {
11686 SDVTList VTs = getVTList(VT1, VT2);
11687 SDValue Ops[] = { Op1, Op2 };
11688 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11689}
11690
11693 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11694 // Reset the NodeID to -1.
11695 New->setNodeId(-1);
11696 if (New != N) {
11697 ReplaceAllUsesWith(N, New);
11699 }
11700 return New;
11701}
11702
11703/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11704/// the line number information on the merged node since it is not possible to
11705/// preserve the information that operation is associated with multiple lines.
11706/// This will make the debugger working better at -O0, were there is a higher
11707/// probability having other instructions associated with that line.
11708///
11709/// For IROrder, we keep the smaller of the two
11710SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11711 DebugLoc NLoc = N->getDebugLoc();
11712 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11713 N->setDebugLoc(DebugLoc());
11714 }
11715 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11716 N->setIROrder(Order);
11717 return N;
11718}
11719
11720/// MorphNodeTo - This *mutates* the specified node to have the specified
11721/// return type, opcode, and operands.
11722///
11723/// Note that MorphNodeTo returns the resultant node. If there is already a
11724/// node of the specified opcode and operands, it returns that node instead of
11725/// the current one. Note that the SDLoc need not be the same.
11726///
11727/// Using MorphNodeTo is faster than creating a new node and swapping it in
11728/// with ReplaceAllUsesWith both because it often avoids allocating a new
11729/// node, and because it doesn't require CSE recalculation for any of
11730/// the node's users.
11731///
11732/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11733/// As a consequence it isn't appropriate to use from within the DAG combiner or
11734/// the legalizer which maintain worklists that would need to be updated when
11735/// deleting things.
11738 // If an identical node already exists, use it.
11739 void *IP = nullptr;
11740 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11742 AddNodeIDNode(ID, Opc, VTs, Ops);
11743 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11744 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11745 }
11746
11747 if (!RemoveNodeFromCSEMaps(N))
11748 IP = nullptr;
11749
11750 // Start the morphing.
11751 N->NodeType = Opc;
11752 N->ValueList = VTs.VTs;
11753 N->NumValues = VTs.NumVTs;
11754
11755 // Clear the operands list, updating used nodes to remove this from their
11756 // use list. Keep track of any operands that become dead as a result.
11757 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11758 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11759 SDUse &Use = *I++;
11760 SDNode *Used = Use.getNode();
11761 Use.set(SDValue());
11762 if (Used->use_empty())
11763 DeadNodeSet.insert(Used);
11764 }
11765
11766 // For MachineNode, initialize the memory references information.
11768 MN->clearMemRefs();
11769
11770 // Swap for an appropriately sized array from the recycler.
11771 removeOperands(N);
11772 createOperands(N, Ops);
11773
11774 // Delete any nodes that are still dead after adding the uses for the
11775 // new operands.
11776 if (!DeadNodeSet.empty()) {
11777 SmallVector<SDNode *, 16> DeadNodes;
11778 for (SDNode *N : DeadNodeSet)
11779 if (N->use_empty())
11780 DeadNodes.push_back(N);
11781 RemoveDeadNodes(DeadNodes);
11782 }
11783
11784 if (IP)
11785 CSEMap.InsertNode(N, IP); // Memoize the new node.
11786 return N;
11787}
11788
11790 unsigned OrigOpc = Node->getOpcode();
11791 unsigned NewOpc;
11792 switch (OrigOpc) {
11793 default:
11794 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11795#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11796 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11797#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11798 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11799#include "llvm/IR/ConstrainedOps.def"
11800 }
11801
11802 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11803
11804 // We're taking this node out of the chain, so we need to re-link things.
11805 SDValue InputChain = Node->getOperand(0);
11806 SDValue OutputChain = SDValue(Node, 1);
11807 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11808
11810 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11811 Ops.push_back(Node->getOperand(i));
11812
11813 SDVTList VTs = getVTList(Node->getValueType(0));
11814 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11815
11816 // MorphNodeTo can operate in two ways: if an existing node with the
11817 // specified operands exists, it can just return it. Otherwise, it
11818 // updates the node in place to have the requested operands.
11819 if (Res == Node) {
11820 // If we updated the node in place, reset the node ID. To the isel,
11821 // this should be just like a newly allocated machine node.
11822 Res->setNodeId(-1);
11823 } else {
11826 }
11827
11828 return Res;
11829}
11830
11831/// getMachineNode - These are used for target selectors to create a new node
11832/// with specified return type(s), MachineInstr opcode, and operands.
11833///
11834/// Note that getMachineNode returns the resultant node. If there is already a
11835/// node of the specified opcode and operands, it returns that node instead of
11836/// the current one.
11838 EVT VT) {
11839 SDVTList VTs = getVTList(VT);
11840 return getMachineNode(Opcode, dl, VTs, {});
11841}
11842
11844 EVT VT, SDValue Op1) {
11845 SDVTList VTs = getVTList(VT);
11846 SDValue Ops[] = { Op1 };
11847 return getMachineNode(Opcode, dl, VTs, Ops);
11848}
11849
11851 EVT VT, SDValue Op1, SDValue Op2) {
11852 SDVTList VTs = getVTList(VT);
11853 SDValue Ops[] = { Op1, Op2 };
11854 return getMachineNode(Opcode, dl, VTs, Ops);
11855}
11856
11858 EVT VT, SDValue Op1, SDValue Op2,
11859 SDValue Op3) {
11860 SDVTList VTs = getVTList(VT);
11861 SDValue Ops[] = { Op1, Op2, Op3 };
11862 return getMachineNode(Opcode, dl, VTs, Ops);
11863}
11864
11867 SDVTList VTs = getVTList(VT);
11868 return getMachineNode(Opcode, dl, VTs, Ops);
11869}
11870
11872 EVT VT1, EVT VT2, SDValue Op1,
11873 SDValue Op2) {
11874 SDVTList VTs = getVTList(VT1, VT2);
11875 SDValue Ops[] = { Op1, Op2 };
11876 return getMachineNode(Opcode, dl, VTs, Ops);
11877}
11878
11880 EVT VT1, EVT VT2, SDValue Op1,
11881 SDValue Op2, SDValue Op3) {
11882 SDVTList VTs = getVTList(VT1, VT2);
11883 SDValue Ops[] = { Op1, Op2, Op3 };
11884 return getMachineNode(Opcode, dl, VTs, Ops);
11885}
11886
11888 EVT VT1, EVT VT2,
11890 SDVTList VTs = getVTList(VT1, VT2);
11891 return getMachineNode(Opcode, dl, VTs, Ops);
11892}
11893
11895 EVT VT1, EVT VT2, EVT VT3,
11896 SDValue Op1, SDValue Op2) {
11897 SDVTList VTs = getVTList(VT1, VT2, VT3);
11898 SDValue Ops[] = { Op1, Op2 };
11899 return getMachineNode(Opcode, dl, VTs, Ops);
11900}
11901
11903 EVT VT1, EVT VT2, EVT VT3,
11904 SDValue Op1, SDValue Op2,
11905 SDValue Op3) {
11906 SDVTList VTs = getVTList(VT1, VT2, VT3);
11907 SDValue Ops[] = { Op1, Op2, Op3 };
11908 return getMachineNode(Opcode, dl, VTs, Ops);
11909}
11910
11912 EVT VT1, EVT VT2, EVT VT3,
11914 SDVTList VTs = getVTList(VT1, VT2, VT3);
11915 return getMachineNode(Opcode, dl, VTs, Ops);
11916}
11917
11919 ArrayRef<EVT> ResultTys,
11921 SDVTList VTs = getVTList(ResultTys);
11922 return getMachineNode(Opcode, dl, VTs, Ops);
11923}
11924
11926 SDVTList VTs,
11928 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11930 void *IP = nullptr;
11931
11932 if (DoCSE) {
11934 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11935 IP = nullptr;
11936 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11937 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11938 }
11939 }
11940
11941 // Allocate a new MachineSDNode.
11942 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11943 createOperands(N, Ops);
11944
11945 if (DoCSE)
11946 CSEMap.InsertNode(N, IP);
11947
11948 InsertNode(N);
11949 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11950 return N;
11951}
11952
11953/// getTargetExtractSubreg - A convenience function for creating
11954/// TargetOpcode::EXTRACT_SUBREG nodes.
11956 SDValue Operand) {
11957 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11958 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11959 VT, Operand, SRIdxVal);
11960 return SDValue(Subreg, 0);
11961}
11962
11963/// getTargetInsertSubreg - A convenience function for creating
11964/// TargetOpcode::INSERT_SUBREG nodes.
11966 SDValue Operand, SDValue Subreg) {
11967 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11968 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11969 VT, Operand, Subreg, SRIdxVal);
11970 return SDValue(Result, 0);
11971}
11972
11973/// getNodeIfExists - Get the specified node if it's already available, or
11974/// else return NULL.
11977 bool AllowCommute) {
11978 SDNodeFlags Flags;
11979 if (Inserter)
11980 Flags = Inserter->getFlags();
11981 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11982}
11983
11986 const SDNodeFlags Flags,
11987 bool AllowCommute) {
11988 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11989 return nullptr;
11990
11991 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11993 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11994 void *IP = nullptr;
11995 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11996 E->intersectFlagsWith(Flags);
11997 return E;
11998 }
11999 return nullptr;
12000 };
12001
12002 if (SDNode *Existing = Lookup(Ops))
12003 return Existing;
12004
12005 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12006 return Lookup({Ops[1], Ops[0]});
12007
12008 return nullptr;
12009}
12010
12011/// doesNodeExist - Check if a node exists without modifying its flags.
12012bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12014 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12016 AddNodeIDNode(ID, Opcode, VTList, Ops);
12017 void *IP = nullptr;
12018 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12019 return true;
12020 }
12021 return false;
12022}
12023
12024/// getDbgValue - Creates a SDDbgValue node.
12025///
12026/// SDNode
12028 SDNode *N, unsigned R, bool IsIndirect,
12029 const DebugLoc &DL, unsigned O) {
12030 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12031 "Expected inlined-at fields to agree");
12032 return new (DbgInfo->getAlloc())
12033 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12034 {}, IsIndirect, DL, O,
12035 /*IsVariadic=*/false);
12036}
12037
12038/// Constant
12040 DIExpression *Expr,
12041 const Value *C,
12042 const DebugLoc &DL, unsigned O) {
12043 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12044 "Expected inlined-at fields to agree");
12045 return new (DbgInfo->getAlloc())
12046 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12047 /*IsIndirect=*/false, DL, O,
12048 /*IsVariadic=*/false);
12049}
12050
12051/// FrameIndex
12053 DIExpression *Expr, unsigned FI,
12054 bool IsIndirect,
12055 const DebugLoc &DL,
12056 unsigned O) {
12057 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12058 "Expected inlined-at fields to agree");
12059 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12060}
12061
12062/// FrameIndex with dependencies
12064 DIExpression *Expr, unsigned FI,
12065 ArrayRef<SDNode *> Dependencies,
12066 bool IsIndirect,
12067 const DebugLoc &DL,
12068 unsigned O) {
12069 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12070 "Expected inlined-at fields to agree");
12071 return new (DbgInfo->getAlloc())
12072 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12073 Dependencies, IsIndirect, DL, O,
12074 /*IsVariadic=*/false);
12075}
12076
12077/// VReg
12079 Register VReg, bool IsIndirect,
12080 const DebugLoc &DL, unsigned O) {
12081 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12082 "Expected inlined-at fields to agree");
12083 return new (DbgInfo->getAlloc())
12084 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12085 {}, IsIndirect, DL, O,
12086 /*IsVariadic=*/false);
12087}
12088
12091 ArrayRef<SDNode *> Dependencies,
12092 bool IsIndirect, const DebugLoc &DL,
12093 unsigned O, bool IsVariadic) {
12094 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12095 "Expected inlined-at fields to agree");
12096 return new (DbgInfo->getAlloc())
12097 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12098 DL, O, IsVariadic);
12099}
12100
12102 unsigned OffsetInBits, unsigned SizeInBits,
12103 bool InvalidateDbg) {
12104 SDNode *FromNode = From.getNode();
12105 SDNode *ToNode = To.getNode();
12106 assert(FromNode && ToNode && "Can't modify dbg values");
12107
12108 // PR35338
12109 // TODO: assert(From != To && "Redundant dbg value transfer");
12110 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12111 if (From == To || FromNode == ToNode)
12112 return;
12113
12114 if (!FromNode->getHasDebugValue())
12115 return;
12116
12117 SDDbgOperand FromLocOp =
12118 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12120
12122 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12123 if (Dbg->isInvalidated())
12124 continue;
12125
12126 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12127
12128 // Create a new location ops vector that is equal to the old vector, but
12129 // with each instance of FromLocOp replaced with ToLocOp.
12130 bool Changed = false;
12131 auto NewLocOps = Dbg->copyLocationOps();
12132 std::replace_if(
12133 NewLocOps.begin(), NewLocOps.end(),
12134 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12135 bool Match = Op == FromLocOp;
12136 Changed |= Match;
12137 return Match;
12138 },
12139 ToLocOp);
12140 // Ignore this SDDbgValue if we didn't find a matching location.
12141 if (!Changed)
12142 continue;
12143
12144 DIVariable *Var = Dbg->getVariable();
12145 auto *Expr = Dbg->getExpression();
12146 // If a fragment is requested, update the expression.
12147 if (SizeInBits) {
12148 // When splitting a larger (e.g., sign-extended) value whose
12149 // lower bits are described with an SDDbgValue, do not attempt
12150 // to transfer the SDDbgValue to the upper bits.
12151 if (auto FI = Expr->getFragmentInfo())
12152 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12153 continue;
12154 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12155 SizeInBits);
12156 if (!Fragment)
12157 continue;
12158 Expr = *Fragment;
12159 }
12160
12161 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12162 // Clone the SDDbgValue and move it to To.
12163 SDDbgValue *Clone = getDbgValueList(
12164 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12165 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12166 Dbg->isVariadic());
12167 ClonedDVs.push_back(Clone);
12168
12169 if (InvalidateDbg) {
12170 // Invalidate value and indicate the SDDbgValue should not be emitted.
12171 Dbg->setIsInvalidated();
12172 Dbg->setIsEmitted();
12173 }
12174 }
12175
12176 for (SDDbgValue *Dbg : ClonedDVs) {
12177 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12178 "Transferred DbgValues should depend on the new SDNode");
12179 AddDbgValue(Dbg, false);
12180 }
12181}
12182
12184 if (!N.getHasDebugValue())
12185 return;
12186
12187 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12188 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12189 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12190 return SDDbgOperand::fromNode(Node, ResNo);
12191 };
12192
12194 for (auto *DV : GetDbgValues(&N)) {
12195 if (DV->isInvalidated())
12196 continue;
12197 switch (N.getOpcode()) {
12198 default:
12199 break;
12200 case ISD::ADD: {
12201 SDValue N0 = N.getOperand(0);
12202 SDValue N1 = N.getOperand(1);
12203 if (!isa<ConstantSDNode>(N0)) {
12204 bool RHSConstant = isa<ConstantSDNode>(N1);
12206 if (RHSConstant)
12207 Offset = N.getConstantOperandVal(1);
12208 // We are not allowed to turn indirect debug values variadic, so
12209 // don't salvage those.
12210 if (!RHSConstant && DV->isIndirect())
12211 continue;
12212
12213 // Rewrite an ADD constant node into a DIExpression. Since we are
12214 // performing arithmetic to compute the variable's *value* in the
12215 // DIExpression, we need to mark the expression with a
12216 // DW_OP_stack_value.
12217 auto *DIExpr = DV->getExpression();
12218 auto NewLocOps = DV->copyLocationOps();
12219 bool Changed = false;
12220 size_t OrigLocOpsSize = NewLocOps.size();
12221 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12222 // We're not given a ResNo to compare against because the whole
12223 // node is going away. We know that any ISD::ADD only has one
12224 // result, so we can assume any node match is using the result.
12225 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12226 NewLocOps[i].getSDNode() != &N)
12227 continue;
12228 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12229 if (RHSConstant) {
12232 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12233 } else {
12234 // Convert to a variadic expression (if not already).
12235 // convertToVariadicExpression() returns a const pointer, so we use
12236 // a temporary const variable here.
12237 const auto *TmpDIExpr =
12241 ExprOps.push_back(NewLocOps.size());
12242 ExprOps.push_back(dwarf::DW_OP_plus);
12243 SDDbgOperand RHS =
12245 NewLocOps.push_back(RHS);
12246 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12247 }
12248 Changed = true;
12249 }
12250 (void)Changed;
12251 assert(Changed && "Salvage target doesn't use N");
12252
12253 bool IsVariadic =
12254 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12255
12256 auto AdditionalDependencies = DV->getAdditionalDependencies();
12257 SDDbgValue *Clone = getDbgValueList(
12258 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12259 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12260 ClonedDVs.push_back(Clone);
12261 DV->setIsInvalidated();
12262 DV->setIsEmitted();
12263 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12264 N0.getNode()->dumprFull(this);
12265 dbgs() << " into " << *DIExpr << '\n');
12266 }
12267 break;
12268 }
12269 case ISD::TRUNCATE: {
12270 SDValue N0 = N.getOperand(0);
12271 TypeSize FromSize = N0.getValueSizeInBits();
12272 TypeSize ToSize = N.getValueSizeInBits(0);
12273
12274 DIExpression *DbgExpression = DV->getExpression();
12275 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12276 auto NewLocOps = DV->copyLocationOps();
12277 bool Changed = false;
12278 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12279 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12280 NewLocOps[i].getSDNode() != &N)
12281 continue;
12282
12283 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12284 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12285 Changed = true;
12286 }
12287 assert(Changed && "Salvage target doesn't use N");
12288 (void)Changed;
12289
12290 SDDbgValue *Clone =
12291 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12292 DV->getAdditionalDependencies(), DV->isIndirect(),
12293 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12294
12295 ClonedDVs.push_back(Clone);
12296 DV->setIsInvalidated();
12297 DV->setIsEmitted();
12298 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12299 dbgs() << " into " << *DbgExpression << '\n');
12300 break;
12301 }
12302 }
12303 }
12304
12305 for (SDDbgValue *Dbg : ClonedDVs) {
12306 assert((!Dbg->getSDNodes().empty() ||
12307 llvm::any_of(Dbg->getLocationOps(),
12308 [&](const SDDbgOperand &Op) {
12309 return Op.getKind() == SDDbgOperand::FRAMEIX;
12310 })) &&
12311 "Salvaged DbgValue should depend on a new SDNode");
12312 AddDbgValue(Dbg, false);
12313 }
12314}
12315
12316/// Creates a SDDbgLabel node.
12318 const DebugLoc &DL, unsigned O) {
12319 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12320 "Expected inlined-at fields to agree");
12321 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12322}
12323
12324namespace {
12325
12326/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12327/// pointed to by a use iterator is deleted, increment the use iterator
12328/// so that it doesn't dangle.
12329///
12330class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12333
12334 void NodeDeleted(SDNode *N, SDNode *E) override {
12335 // Increment the iterator as needed.
12336 while (UI != UE && N == UI->getUser())
12337 ++UI;
12338 }
12339
12340public:
12341 RAUWUpdateListener(SelectionDAG &d,
12344 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12345};
12346
12347} // end anonymous namespace
12348
12349/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12350/// This can cause recursive merging of nodes in the DAG.
12351///
12352/// This version assumes From has a single result value.
12353///
12355 SDNode *From = FromN.getNode();
12356 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12357 "Cannot replace with this method!");
12358 assert(From != To.getNode() && "Cannot replace uses of with self");
12359
12360 // Preserve Debug Values
12361 transferDbgValues(FromN, To);
12362 // Preserve extra info.
12363 copyExtraInfo(From, To.getNode());
12364
12365 // Iterate over all the existing uses of From. New uses will be added
12366 // to the beginning of the use list, which we avoid visiting.
12367 // This specifically avoids visiting uses of From that arise while the
12368 // replacement is happening, because any such uses would be the result
12369 // of CSE: If an existing node looks like From after one of its operands
12370 // is replaced by To, we don't want to replace of all its users with To
12371 // too. See PR3018 for more info.
12372 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12373 RAUWUpdateListener Listener(*this, UI, UE);
12374 while (UI != UE) {
12375 SDNode *User = UI->getUser();
12376
12377 // This node is about to morph, remove its old self from the CSE maps.
12378 RemoveNodeFromCSEMaps(User);
12379
12380 // A user can appear in a use list multiple times, and when this
12381 // happens the uses are usually next to each other in the list.
12382 // To help reduce the number of CSE recomputations, process all
12383 // the uses of this user that we can find this way.
12384 do {
12385 SDUse &Use = *UI;
12386 ++UI;
12387 Use.set(To);
12388 if (To->isDivergent() != From->isDivergent())
12390 } while (UI != UE && UI->getUser() == User);
12391 // Now that we have modified User, add it back to the CSE maps. If it
12392 // already exists there, recursively merge the results together.
12393 AddModifiedNodeToCSEMaps(User);
12394 }
12395
12396 // If we just RAUW'd the root, take note.
12397 if (FromN == getRoot())
12398 setRoot(To);
12399}
12400
12401/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12402/// This can cause recursive merging of nodes in the DAG.
12403///
12404/// This version assumes that for each value of From, there is a
12405/// corresponding value in To in the same position with the same type.
12406///
12408#ifndef NDEBUG
12409 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12410 assert((!From->hasAnyUseOfValue(i) ||
12411 From->getValueType(i) == To->getValueType(i)) &&
12412 "Cannot use this version of ReplaceAllUsesWith!");
12413#endif
12414
12415 // Handle the trivial case.
12416 if (From == To)
12417 return;
12418
12419 // Preserve Debug Info. Only do this if there's a use.
12420 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12421 if (From->hasAnyUseOfValue(i)) {
12422 assert((i < To->getNumValues()) && "Invalid To location");
12423 transferDbgValues(SDValue(From, i), SDValue(To, i));
12424 }
12425 // Preserve extra info.
12426 copyExtraInfo(From, To);
12427
12428 // Iterate over just the existing users of From. See the comments in
12429 // the ReplaceAllUsesWith above.
12430 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12431 RAUWUpdateListener Listener(*this, UI, UE);
12432 while (UI != UE) {
12433 SDNode *User = UI->getUser();
12434
12435 // This node is about to morph, remove its old self from the CSE maps.
12436 RemoveNodeFromCSEMaps(User);
12437
12438 // A user can appear in a use list multiple times, and when this
12439 // happens the uses are usually next to each other in the list.
12440 // To help reduce the number of CSE recomputations, process all
12441 // the uses of this user that we can find this way.
12442 do {
12443 SDUse &Use = *UI;
12444 ++UI;
12445 Use.setNode(To);
12446 if (To->isDivergent() != From->isDivergent())
12448 } while (UI != UE && UI->getUser() == User);
12449
12450 // Now that we have modified User, add it back to the CSE maps. If it
12451 // already exists there, recursively merge the results together.
12452 AddModifiedNodeToCSEMaps(User);
12453 }
12454
12455 // If we just RAUW'd the root, take note.
12456 if (From == getRoot().getNode())
12457 setRoot(SDValue(To, getRoot().getResNo()));
12458}
12459
12460/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12461/// This can cause recursive merging of nodes in the DAG.
12462///
12463/// This version can replace From with any result values. To must match the
12464/// number and types of values returned by From.
12466 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12467 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12468
12469 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12470 // Preserve Debug Info.
12471 transferDbgValues(SDValue(From, i), To[i]);
12472 // Preserve extra info.
12473 copyExtraInfo(From, To[i].getNode());
12474 }
12475
12476 // Iterate over just the existing users of From. See the comments in
12477 // the ReplaceAllUsesWith above.
12478 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12479 RAUWUpdateListener Listener(*this, UI, UE);
12480 while (UI != UE) {
12481 SDNode *User = UI->getUser();
12482
12483 // This node is about to morph, remove its old self from the CSE maps.
12484 RemoveNodeFromCSEMaps(User);
12485
12486 // A user can appear in a use list multiple times, and when this happens the
12487 // uses are usually next to each other in the list. To help reduce the
12488 // number of CSE and divergence recomputations, process all the uses of this
12489 // user that we can find this way.
12490 bool To_IsDivergent = false;
12491 do {
12492 SDUse &Use = *UI;
12493 const SDValue &ToOp = To[Use.getResNo()];
12494 ++UI;
12495 Use.set(ToOp);
12496 if (ToOp.getValueType() != MVT::Other)
12497 To_IsDivergent |= ToOp->isDivergent();
12498 } while (UI != UE && UI->getUser() == User);
12499
12500 if (To_IsDivergent != From->isDivergent())
12502
12503 // Now that we have modified User, add it back to the CSE maps. If it
12504 // already exists there, recursively merge the results together.
12505 AddModifiedNodeToCSEMaps(User);
12506 }
12507
12508 // If we just RAUW'd the root, take note.
12509 if (From == getRoot().getNode())
12510 setRoot(SDValue(To[getRoot().getResNo()]));
12511}
12512
12513/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12514/// uses of other values produced by From.getNode() alone. The Deleted
12515/// vector is handled the same way as for ReplaceAllUsesWith.
12517 // Handle the really simple, really trivial case efficiently.
12518 if (From == To) return;
12519
12520 // Handle the simple, trivial, case efficiently.
12521 if (From.getNode()->getNumValues() == 1) {
12522 ReplaceAllUsesWith(From, To);
12523 return;
12524 }
12525
12526 // Preserve Debug Info.
12527 transferDbgValues(From, To);
12528 copyExtraInfo(From.getNode(), To.getNode());
12529
12530 // Iterate over just the existing users of From. See the comments in
12531 // the ReplaceAllUsesWith above.
12532 SDNode::use_iterator UI = From.getNode()->use_begin(),
12533 UE = From.getNode()->use_end();
12534 RAUWUpdateListener Listener(*this, UI, UE);
12535 while (UI != UE) {
12536 SDNode *User = UI->getUser();
12537 bool UserRemovedFromCSEMaps = false;
12538
12539 // A user can appear in a use list multiple times, and when this
12540 // happens the uses are usually next to each other in the list.
12541 // To help reduce the number of CSE recomputations, process all
12542 // the uses of this user that we can find this way.
12543 do {
12544 SDUse &Use = *UI;
12545
12546 // Skip uses of different values from the same node.
12547 if (Use.getResNo() != From.getResNo()) {
12548 ++UI;
12549 continue;
12550 }
12551
12552 // If this node hasn't been modified yet, it's still in the CSE maps,
12553 // so remove its old self from the CSE maps.
12554 if (!UserRemovedFromCSEMaps) {
12555 RemoveNodeFromCSEMaps(User);
12556 UserRemovedFromCSEMaps = true;
12557 }
12558
12559 ++UI;
12560 Use.set(To);
12561 if (To->isDivergent() != From->isDivergent())
12563 } while (UI != UE && UI->getUser() == User);
12564 // We are iterating over all uses of the From node, so if a use
12565 // doesn't use the specific value, no changes are made.
12566 if (!UserRemovedFromCSEMaps)
12567 continue;
12568
12569 // Now that we have modified User, add it back to the CSE maps. If it
12570 // already exists there, recursively merge the results together.
12571 AddModifiedNodeToCSEMaps(User);
12572 }
12573
12574 // If we just RAUW'd the root, take note.
12575 if (From == getRoot())
12576 setRoot(To);
12577}
12578
12579namespace {
12580
12581/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12582/// to record information about a use.
12583struct UseMemo {
12584 SDNode *User;
12585 unsigned Index;
12586 SDUse *Use;
12587};
12588
12589/// operator< - Sort Memos by User.
12590bool operator<(const UseMemo &L, const UseMemo &R) {
12591 return (intptr_t)L.User < (intptr_t)R.User;
12592}
12593
12594/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12595/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12596/// the node already has been taken care of recursively.
12597class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12598 SmallVectorImpl<UseMemo> &Uses;
12599
12600 void NodeDeleted(SDNode *N, SDNode *E) override {
12601 for (UseMemo &Memo : Uses)
12602 if (Memo.User == N)
12603 Memo.User = nullptr;
12604 }
12605
12606public:
12607 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12608 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12609};
12610
12611} // end anonymous namespace
12612
12613/// Return true if a glue output should propagate divergence information.
12615 switch (Node->getOpcode()) {
12616 case ISD::CopyFromReg:
12617 case ISD::CopyToReg:
12618 return false;
12619 default:
12620 return true;
12621 }
12622
12623 llvm_unreachable("covered opcode switch");
12624}
12625
12627 if (TLI->isSDNodeAlwaysUniform(N)) {
12628 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12629 "Conflicting divergence information!");
12630 return false;
12631 }
12632 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12633 return true;
12634 for (const auto &Op : N->ops()) {
12635 EVT VT = Op.getValueType();
12636
12637 // Skip Chain. It does not carry divergence.
12638 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12639 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12640 return true;
12641 }
12642 return false;
12643}
12644
12646 SmallVector<SDNode *, 16> Worklist(1, N);
12647 do {
12648 N = Worklist.pop_back_val();
12649 bool IsDivergent = calculateDivergence(N);
12650 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12651 N->SDNodeBits.IsDivergent = IsDivergent;
12652 llvm::append_range(Worklist, N->users());
12653 }
12654 } while (!Worklist.empty());
12655}
12656
12657void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12659 Order.reserve(AllNodes.size());
12660 for (auto &N : allnodes()) {
12661 unsigned NOps = N.getNumOperands();
12662 Degree[&N] = NOps;
12663 if (0 == NOps)
12664 Order.push_back(&N);
12665 }
12666 for (size_t I = 0; I != Order.size(); ++I) {
12667 SDNode *N = Order[I];
12668 for (auto *U : N->users()) {
12669 unsigned &UnsortedOps = Degree[U];
12670 if (0 == --UnsortedOps)
12671 Order.push_back(U);
12672 }
12673 }
12674}
12675
12676#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12677void SelectionDAG::VerifyDAGDivergence() {
12678 std::vector<SDNode *> TopoOrder;
12679 CreateTopologicalOrder(TopoOrder);
12680 for (auto *N : TopoOrder) {
12681 assert(calculateDivergence(N) == N->isDivergent() &&
12682 "Divergence bit inconsistency detected");
12683 }
12684}
12685#endif
12686
12687/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12688/// uses of other values produced by From.getNode() alone. The same value
12689/// may appear in both the From and To list. The Deleted vector is
12690/// handled the same way as for ReplaceAllUsesWith.
12692 const SDValue *To,
12693 unsigned Num){
12694 // Handle the simple, trivial case efficiently.
12695 if (Num == 1)
12696 return ReplaceAllUsesOfValueWith(*From, *To);
12697
12698 transferDbgValues(*From, *To);
12699 copyExtraInfo(From->getNode(), To->getNode());
12700
12701 // Read up all the uses and make records of them. This helps
12702 // processing new uses that are introduced during the
12703 // replacement process.
12705 for (unsigned i = 0; i != Num; ++i) {
12706 unsigned FromResNo = From[i].getResNo();
12707 SDNode *FromNode = From[i].getNode();
12708 for (SDUse &Use : FromNode->uses()) {
12709 if (Use.getResNo() == FromResNo) {
12710 UseMemo Memo = {Use.getUser(), i, &Use};
12711 Uses.push_back(Memo);
12712 }
12713 }
12714 }
12715
12716 // Sort the uses, so that all the uses from a given User are together.
12718 RAUOVWUpdateListener Listener(*this, Uses);
12719
12720 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12721 UseIndex != UseIndexEnd; ) {
12722 // We know that this user uses some value of From. If it is the right
12723 // value, update it.
12724 SDNode *User = Uses[UseIndex].User;
12725 // If the node has been deleted by recursive CSE updates when updating
12726 // another node, then just skip this entry.
12727 if (User == nullptr) {
12728 ++UseIndex;
12729 continue;
12730 }
12731
12732 // This node is about to morph, remove its old self from the CSE maps.
12733 RemoveNodeFromCSEMaps(User);
12734
12735 // The Uses array is sorted, so all the uses for a given User
12736 // are next to each other in the list.
12737 // To help reduce the number of CSE recomputations, process all
12738 // the uses of this user that we can find this way.
12739 do {
12740 unsigned i = Uses[UseIndex].Index;
12741 SDUse &Use = *Uses[UseIndex].Use;
12742 ++UseIndex;
12743
12744 Use.set(To[i]);
12745 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12746
12747 // Now that we have modified User, add it back to the CSE maps. If it
12748 // already exists there, recursively merge the results together.
12749 AddModifiedNodeToCSEMaps(User);
12750 }
12751}
12752
12753/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12754/// based on their topological order. It returns the maximum id and a vector
12755/// of the SDNodes* in assigned order by reference.
12757 unsigned DAGSize = 0;
12758
12759 // SortedPos tracks the progress of the algorithm. Nodes before it are
12760 // sorted, nodes after it are unsorted. When the algorithm completes
12761 // it is at the end of the list.
12762 allnodes_iterator SortedPos = allnodes_begin();
12763
12764 // Visit all the nodes. Move nodes with no operands to the front of
12765 // the list immediately. Annotate nodes that do have operands with their
12766 // operand count. Before we do this, the Node Id fields of the nodes
12767 // may contain arbitrary values. After, the Node Id fields for nodes
12768 // before SortedPos will contain the topological sort index, and the
12769 // Node Id fields for nodes At SortedPos and after will contain the
12770 // count of outstanding operands.
12772 checkForCycles(&N, this);
12773 unsigned Degree = N.getNumOperands();
12774 if (Degree == 0) {
12775 // A node with no uses, add it to the result array immediately.
12776 N.setNodeId(DAGSize++);
12777 allnodes_iterator Q(&N);
12778 if (Q != SortedPos)
12779 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12780 assert(SortedPos != AllNodes.end() && "Overran node list");
12781 ++SortedPos;
12782 } else {
12783 // Temporarily use the Node Id as scratch space for the degree count.
12784 N.setNodeId(Degree);
12785 }
12786 }
12787
12788 // Visit all the nodes. As we iterate, move nodes into sorted order,
12789 // such that by the time the end is reached all nodes will be sorted.
12790 for (SDNode &Node : allnodes()) {
12791 SDNode *N = &Node;
12792 checkForCycles(N, this);
12793 // N is in sorted position, so all its uses have one less operand
12794 // that needs to be sorted.
12795 for (SDNode *P : N->users()) {
12796 unsigned Degree = P->getNodeId();
12797 assert(Degree != 0 && "Invalid node degree");
12798 --Degree;
12799 if (Degree == 0) {
12800 // All of P's operands are sorted, so P may sorted now.
12801 P->setNodeId(DAGSize++);
12802 if (P->getIterator() != SortedPos)
12803 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12804 assert(SortedPos != AllNodes.end() && "Overran node list");
12805 ++SortedPos;
12806 } else {
12807 // Update P's outstanding operand count.
12808 P->setNodeId(Degree);
12809 }
12810 }
12811 if (Node.getIterator() == SortedPos) {
12812#ifndef NDEBUG
12814 SDNode *S = &*++I;
12815 dbgs() << "Overran sorted position:\n";
12816 S->dumprFull(this); dbgs() << "\n";
12817 dbgs() << "Checking if this is due to cycles\n";
12818 checkForCycles(this, true);
12819#endif
12820 llvm_unreachable(nullptr);
12821 }
12822 }
12823
12824 assert(SortedPos == AllNodes.end() &&
12825 "Topological sort incomplete!");
12826 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12827 "First node in topological sort is not the entry token!");
12828 assert(AllNodes.front().getNodeId() == 0 &&
12829 "First node in topological sort has non-zero id!");
12830 assert(AllNodes.front().getNumOperands() == 0 &&
12831 "First node in topological sort has operands!");
12832 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12833 "Last node in topologic sort has unexpected id!");
12834 assert(AllNodes.back().use_empty() &&
12835 "Last node in topologic sort has users!");
12836 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12837 return DAGSize;
12838}
12839
12841 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12842 SortedNodes.clear();
12843 // Node -> remaining number of outstanding operands.
12844 DenseMap<const SDNode *, unsigned> RemainingOperands;
12845
12846 // Put nodes without any operands into SortedNodes first.
12847 for (const SDNode &N : allnodes()) {
12848 checkForCycles(&N, this);
12849 unsigned NumOperands = N.getNumOperands();
12850 if (NumOperands == 0)
12851 SortedNodes.push_back(&N);
12852 else
12853 // Record their total number of outstanding operands.
12854 RemainingOperands[&N] = NumOperands;
12855 }
12856
12857 // A node is pushed into SortedNodes when all of its operands (predecessors in
12858 // the graph) are also in SortedNodes.
12859 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12860 const SDNode *N = SortedNodes[i];
12861 for (const SDNode *U : N->users()) {
12862 // HandleSDNode is never part of a DAG and therefore has no entry in
12863 // RemainingOperands.
12864 if (U->getOpcode() == ISD::HANDLENODE)
12865 continue;
12866 unsigned &NumRemOperands = RemainingOperands[U];
12867 assert(NumRemOperands && "Invalid number of remaining operands");
12868 --NumRemOperands;
12869 if (!NumRemOperands)
12870 SortedNodes.push_back(U);
12871 }
12872 }
12873
12874 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12875 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12876 "First node in topological sort is not the entry token");
12877 assert(SortedNodes.front()->getNumOperands() == 0 &&
12878 "First node in topological sort has operands");
12879}
12880
12881/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12882/// value is produced by SD.
12883void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12884 for (SDNode *SD : DB->getSDNodes()) {
12885 if (!SD)
12886 continue;
12887 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12888 SD->setHasDebugValue(true);
12889 }
12890 DbgInfo->add(DB, isParameter);
12891}
12892
12893void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12894
12896 SDValue NewMemOpChain) {
12897 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12898 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12899 // The new memory operation must have the same position as the old load in
12900 // terms of memory dependency. Create a TokenFactor for the old load and new
12901 // memory operation and update uses of the old load's output chain to use that
12902 // TokenFactor.
12903 if (OldChain == NewMemOpChain || OldChain.use_empty())
12904 return NewMemOpChain;
12905
12906 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12907 OldChain, NewMemOpChain);
12908 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12909 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12910 return TokenFactor;
12911}
12912
12914 SDValue NewMemOp) {
12915 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12916 SDValue OldChain = SDValue(OldLoad, 1);
12917 SDValue NewMemOpChain = NewMemOp.getValue(1);
12918 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12919}
12920
12922 Function **OutFunction) {
12923 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12924
12925 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12926 auto *Module = MF->getFunction().getParent();
12927 auto *Function = Module->getFunction(Symbol);
12928
12929 if (OutFunction != nullptr)
12930 *OutFunction = Function;
12931
12932 if (Function != nullptr) {
12933 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12934 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12935 }
12936
12937 std::string ErrorStr;
12938 raw_string_ostream ErrorFormatter(ErrorStr);
12939 ErrorFormatter << "Undefined external symbol ";
12940 ErrorFormatter << '"' << Symbol << '"';
12941 report_fatal_error(Twine(ErrorStr));
12942}
12943
12944//===----------------------------------------------------------------------===//
12945// SDNode Class
12946//===----------------------------------------------------------------------===//
12947
12950 return Const != nullptr && Const->isZero();
12951}
12952
12954 return V.isUndef() || isNullConstant(V);
12955}
12956
12959 return Const != nullptr && Const->isZero() && !Const->isNegative();
12960}
12961
12964 return Const != nullptr && Const->isAllOnes();
12965}
12966
12969 return Const != nullptr && Const->isOne();
12970}
12971
12974 return Const != nullptr && Const->isMinSignedValue();
12975}
12976
12977bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12978 unsigned OperandNo) {
12979 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12980 // TODO: Target-specific opcodes could be added.
12981 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12982 /*AllowTruncation*/ true)) {
12983 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12984 switch (Opcode) {
12985 case ISD::ADD:
12986 case ISD::OR:
12987 case ISD::XOR:
12988 case ISD::UMAX:
12989 return Const.isZero();
12990 case ISD::MUL:
12991 return Const.isOne();
12992 case ISD::AND:
12993 case ISD::UMIN:
12994 return Const.isAllOnes();
12995 case ISD::SMAX:
12996 return Const.isMinSignedValue();
12997 case ISD::SMIN:
12998 return Const.isMaxSignedValue();
12999 case ISD::SUB:
13000 case ISD::SHL:
13001 case ISD::SRA:
13002 case ISD::SRL:
13003 return OperandNo == 1 && Const.isZero();
13004 case ISD::UDIV:
13005 case ISD::SDIV:
13006 return OperandNo == 1 && Const.isOne();
13007 }
13008 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13009 switch (Opcode) {
13010 case ISD::FADD:
13011 return ConstFP->isZero() &&
13012 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13013 case ISD::FSUB:
13014 return OperandNo == 1 && ConstFP->isZero() &&
13015 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13016 case ISD::FMUL:
13017 return ConstFP->isExactlyValue(1.0);
13018 case ISD::FDIV:
13019 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13020 case ISD::FMINNUM:
13021 case ISD::FMAXNUM: {
13022 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13023 EVT VT = V.getValueType();
13024 const fltSemantics &Semantics = VT.getFltSemantics();
13025 APFloat NeutralAF = !Flags.hasNoNaNs()
13026 ? APFloat::getQNaN(Semantics)
13027 : !Flags.hasNoInfs()
13028 ? APFloat::getInf(Semantics)
13029 : APFloat::getLargest(Semantics);
13030 if (Opcode == ISD::FMAXNUM)
13031 NeutralAF.changeSign();
13032
13033 return ConstFP->isExactlyValue(NeutralAF);
13034 }
13035 }
13036 }
13037 return false;
13038}
13039
13041 while (V.getOpcode() == ISD::BITCAST)
13042 V = V.getOperand(0);
13043 return V;
13044}
13045
13047 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13048 V = V.getOperand(0);
13049 return V;
13050}
13051
13053 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13054 V = V.getOperand(0);
13055 return V;
13056}
13057
13059 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13060 SDValue InVec = V.getOperand(0);
13061 SDValue EltNo = V.getOperand(2);
13062 EVT VT = InVec.getValueType();
13063 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13064 if (IndexC && VT.isFixedLengthVector() &&
13065 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13066 !DemandedElts[IndexC->getZExtValue()]) {
13067 V = InVec;
13068 continue;
13069 }
13070 break;
13071 }
13072 return V;
13073}
13074
13076 while (V.getOpcode() == ISD::TRUNCATE)
13077 V = V.getOperand(0);
13078 return V;
13079}
13080
13081bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13082 if (V.getOpcode() != ISD::XOR)
13083 return false;
13084 V = peekThroughBitcasts(V.getOperand(1));
13085 unsigned NumBits = V.getScalarValueSizeInBits();
13086 ConstantSDNode *C =
13087 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13088 return C && (C->getAPIntValue().countr_one() >= NumBits);
13089}
13090
13092 bool AllowTruncation) {
13093 EVT VT = N.getValueType();
13094 APInt DemandedElts = VT.isFixedLengthVector()
13096 : APInt(1, 1);
13097 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13098}
13099
13101 bool AllowUndefs,
13102 bool AllowTruncation) {
13104 return CN;
13105
13106 // SplatVectors can truncate their operands. Ignore that case here unless
13107 // AllowTruncation is set.
13108 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13109 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13110 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13111 EVT CVT = CN->getValueType(0);
13112 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13113 if (AllowTruncation || CVT == VecEltVT)
13114 return CN;
13115 }
13116 }
13117
13119 BitVector UndefElements;
13120 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13121
13122 // BuildVectors can truncate their operands. Ignore that case here unless
13123 // AllowTruncation is set.
13124 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13125 if (CN && (UndefElements.none() || AllowUndefs)) {
13126 EVT CVT = CN->getValueType(0);
13127 EVT NSVT = N.getValueType().getScalarType();
13128 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13129 if (AllowTruncation || (CVT == NSVT))
13130 return CN;
13131 }
13132 }
13133
13134 return nullptr;
13135}
13136
13138 EVT VT = N.getValueType();
13139 APInt DemandedElts = VT.isFixedLengthVector()
13141 : APInt(1, 1);
13142 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13143}
13144
13146 const APInt &DemandedElts,
13147 bool AllowUndefs) {
13149 return CN;
13150
13152 BitVector UndefElements;
13153 ConstantFPSDNode *CN =
13154 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13155 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13156 if (CN && (UndefElements.none() || AllowUndefs))
13157 return CN;
13158 }
13159
13160 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13161 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13162 return CN;
13163
13164 return nullptr;
13165}
13166
13167bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13168 // TODO: may want to use peekThroughBitcast() here.
13169 ConstantSDNode *C =
13170 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13171 return C && C->isZero();
13172}
13173
13174bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13175 ConstantSDNode *C =
13176 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13177 return C && C->isOne();
13178}
13179
13180bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13181 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13182 return C && C->isExactlyValue(1.0);
13183}
13184
13185bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13187 unsigned BitWidth = N.getScalarValueSizeInBits();
13188 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13189 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13190}
13191
13192bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13193 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13194 return C && APInt::isSameValue(C->getAPIntValue(),
13195 APInt(C->getAPIntValue().getBitWidth(), 1));
13196}
13197
13198bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13200 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13201 return C && C->isZero();
13202}
13203
13204bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13205 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13206 return C && C->isZero();
13207}
13208
13212
13213MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13214 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13215 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13216 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13217 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13218 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13219 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13220
13221 // We check here that the size of the memory operand fits within the size of
13222 // the MMO. This is because the MMO might indicate only a possible address
13223 // range instead of specifying the affected memory addresses precisely.
13224 assert(
13225 (!MMO->getType().isValid() ||
13226 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13227 "Size mismatch!");
13228}
13229
13230/// Profile - Gather unique data for the node.
13231///
13233 AddNodeIDNode(ID, this);
13234}
13235
13236namespace {
13237
13238 struct EVTArray {
13239 std::vector<EVT> VTs;
13240
13241 EVTArray() {
13242 VTs.reserve(MVT::VALUETYPE_SIZE);
13243 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13244 VTs.push_back(MVT((MVT::SimpleValueType)i));
13245 }
13246 };
13247
13248} // end anonymous namespace
13249
13250/// getValueTypeList - Return a pointer to the specified value type.
13251///
13252const EVT *SDNode::getValueTypeList(MVT VT) {
13253 static EVTArray SimpleVTArray;
13254
13255 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13256 return &SimpleVTArray.VTs[VT.SimpleTy];
13257}
13258
13259/// hasAnyUseOfValue - Return true if there are any use of the indicated
13260/// value. This method ignores uses of other values defined by this operation.
13261bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13262 assert(Value < getNumValues() && "Bad value!");
13263
13264 for (SDUse &U : uses())
13265 if (U.getResNo() == Value)
13266 return true;
13267
13268 return false;
13269}
13270
13271/// isOnlyUserOf - Return true if this node is the only use of N.
13272bool SDNode::isOnlyUserOf(const SDNode *N) const {
13273 bool Seen = false;
13274 for (const SDNode *User : N->users()) {
13275 if (User == this)
13276 Seen = true;
13277 else
13278 return false;
13279 }
13280
13281 return Seen;
13282}
13283
13284/// Return true if the only users of N are contained in Nodes.
13286 bool Seen = false;
13287 for (const SDNode *User : N->users()) {
13288 if (llvm::is_contained(Nodes, User))
13289 Seen = true;
13290 else
13291 return false;
13292 }
13293
13294 return Seen;
13295}
13296
13297/// Return true if the referenced return value is an operand of N.
13298bool SDValue::isOperandOf(const SDNode *N) const {
13299 return is_contained(N->op_values(), *this);
13300}
13301
13302bool SDNode::isOperandOf(const SDNode *N) const {
13303 return any_of(N->op_values(),
13304 [this](SDValue Op) { return this == Op.getNode(); });
13305}
13306
13307/// reachesChainWithoutSideEffects - Return true if this operand (which must
13308/// be a chain) reaches the specified operand without crossing any
13309/// side-effecting instructions on any chain path. In practice, this looks
13310/// through token factors and non-volatile loads. In order to remain efficient,
13311/// this only looks a couple of nodes in, it does not do an exhaustive search.
13312///
13313/// Note that we only need to examine chains when we're searching for
13314/// side-effects; SelectionDAG requires that all side-effects are represented
13315/// by chains, even if another operand would force a specific ordering. This
13316/// constraint is necessary to allow transformations like splitting loads.
13318 unsigned Depth) const {
13319 if (*this == Dest) return true;
13320
13321 // Don't search too deeply, we just want to be able to see through
13322 // TokenFactor's etc.
13323 if (Depth == 0) return false;
13324
13325 // If this is a token factor, all inputs to the TF happen in parallel.
13326 if (getOpcode() == ISD::TokenFactor) {
13327 // First, try a shallow search.
13328 if (is_contained((*this)->ops(), Dest)) {
13329 // We found the chain we want as an operand of this TokenFactor.
13330 // Essentially, we reach the chain without side-effects if we could
13331 // serialize the TokenFactor into a simple chain of operations with
13332 // Dest as the last operation. This is automatically true if the
13333 // chain has one use: there are no other ordering constraints.
13334 // If the chain has more than one use, we give up: some other
13335 // use of Dest might force a side-effect between Dest and the current
13336 // node.
13337 if (Dest.hasOneUse())
13338 return true;
13339 }
13340 // Next, try a deep search: check whether every operand of the TokenFactor
13341 // reaches Dest.
13342 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13343 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13344 });
13345 }
13346
13347 // Loads don't have side effects, look through them.
13348 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13349 if (Ld->isUnordered())
13350 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13351 }
13352 return false;
13353}
13354
13355bool SDNode::hasPredecessor(const SDNode *N) const {
13358 Worklist.push_back(this);
13359 return hasPredecessorHelper(N, Visited, Worklist);
13360}
13361
13363 this->Flags &= Flags;
13364}
13365
13366SDValue
13368 ArrayRef<ISD::NodeType> CandidateBinOps,
13369 bool AllowPartials) {
13370 // The pattern must end in an extract from index 0.
13371 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13372 !isNullConstant(Extract->getOperand(1)))
13373 return SDValue();
13374
13375 // Match against one of the candidate binary ops.
13376 SDValue Op = Extract->getOperand(0);
13377 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13378 return Op.getOpcode() == unsigned(BinOp);
13379 }))
13380 return SDValue();
13381
13382 // Floating-point reductions may require relaxed constraints on the final step
13383 // of the reduction because they may reorder intermediate operations.
13384 unsigned CandidateBinOp = Op.getOpcode();
13385 if (Op.getValueType().isFloatingPoint()) {
13386 SDNodeFlags Flags = Op->getFlags();
13387 switch (CandidateBinOp) {
13388 case ISD::FADD:
13389 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13390 return SDValue();
13391 break;
13392 default:
13393 llvm_unreachable("Unhandled FP opcode for binop reduction");
13394 }
13395 }
13396
13397 // Matching failed - attempt to see if we did enough stages that a partial
13398 // reduction from a subvector is possible.
13399 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13400 if (!AllowPartials || !Op)
13401 return SDValue();
13402 EVT OpVT = Op.getValueType();
13403 EVT OpSVT = OpVT.getScalarType();
13404 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13405 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13406 return SDValue();
13407 BinOp = (ISD::NodeType)CandidateBinOp;
13408 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13409 };
13410
13411 // At each stage, we're looking for something that looks like:
13412 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13413 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13414 // i32 undef, i32 undef, i32 undef, i32 undef>
13415 // %a = binop <8 x i32> %op, %s
13416 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13417 // we expect something like:
13418 // <4,5,6,7,u,u,u,u>
13419 // <2,3,u,u,u,u,u,u>
13420 // <1,u,u,u,u,u,u,u>
13421 // While a partial reduction match would be:
13422 // <2,3,u,u,u,u,u,u>
13423 // <1,u,u,u,u,u,u,u>
13424 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13425 SDValue PrevOp;
13426 for (unsigned i = 0; i < Stages; ++i) {
13427 unsigned MaskEnd = (1 << i);
13428
13429 if (Op.getOpcode() != CandidateBinOp)
13430 return PartialReduction(PrevOp, MaskEnd);
13431
13432 SDValue Op0 = Op.getOperand(0);
13433 SDValue Op1 = Op.getOperand(1);
13434
13436 if (Shuffle) {
13437 Op = Op1;
13438 } else {
13439 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13440 Op = Op0;
13441 }
13442
13443 // The first operand of the shuffle should be the same as the other operand
13444 // of the binop.
13445 if (!Shuffle || Shuffle->getOperand(0) != Op)
13446 return PartialReduction(PrevOp, MaskEnd);
13447
13448 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13449 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13450 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13451 return PartialReduction(PrevOp, MaskEnd);
13452
13453 PrevOp = Op;
13454 }
13455
13456 // Handle subvector reductions, which tend to appear after the shuffle
13457 // reduction stages.
13458 while (Op.getOpcode() == CandidateBinOp) {
13459 unsigned NumElts = Op.getValueType().getVectorNumElements();
13460 SDValue Op0 = Op.getOperand(0);
13461 SDValue Op1 = Op.getOperand(1);
13462 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13464 Op0.getOperand(0) != Op1.getOperand(0))
13465 break;
13466 SDValue Src = Op0.getOperand(0);
13467 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13468 if (NumSrcElts != (2 * NumElts))
13469 break;
13470 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13471 Op1.getConstantOperandAPInt(1) == NumElts) &&
13472 !(Op1.getConstantOperandAPInt(1) == 0 &&
13473 Op0.getConstantOperandAPInt(1) == NumElts))
13474 break;
13475 Op = Src;
13476 }
13477
13478 BinOp = (ISD::NodeType)CandidateBinOp;
13479 return Op;
13480}
13481
13483 EVT VT = N->getValueType(0);
13484 EVT EltVT = VT.getVectorElementType();
13485 unsigned NE = VT.getVectorNumElements();
13486
13487 SDLoc dl(N);
13488
13489 // If ResNE is 0, fully unroll the vector op.
13490 if (ResNE == 0)
13491 ResNE = NE;
13492 else if (NE > ResNE)
13493 NE = ResNE;
13494
13495 if (N->getNumValues() == 2) {
13496 SmallVector<SDValue, 8> Scalars0, Scalars1;
13497 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13498 EVT VT1 = N->getValueType(1);
13499 EVT EltVT1 = VT1.getVectorElementType();
13500
13501 unsigned i;
13502 for (i = 0; i != NE; ++i) {
13503 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13504 SDValue Operand = N->getOperand(j);
13505 EVT OperandVT = Operand.getValueType();
13506
13507 // A vector operand; extract a single element.
13508 EVT OperandEltVT = OperandVT.getVectorElementType();
13509 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13510 }
13511
13512 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13513 Scalars0.push_back(EltOp);
13514 Scalars1.push_back(EltOp.getValue(1));
13515 }
13516
13517 for (; i < ResNE; ++i) {
13518 Scalars0.push_back(getUNDEF(EltVT));
13519 Scalars1.push_back(getUNDEF(EltVT1));
13520 }
13521
13522 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13523 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13524 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13525 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13526 return getMergeValues({Vec0, Vec1}, dl);
13527 }
13528
13529 assert(N->getNumValues() == 1 &&
13530 "Can't unroll a vector with multiple results!");
13531
13533 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13534
13535 unsigned i;
13536 for (i= 0; i != NE; ++i) {
13537 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13538 SDValue Operand = N->getOperand(j);
13539 EVT OperandVT = Operand.getValueType();
13540 if (OperandVT.isVector()) {
13541 // A vector operand; extract a single element.
13542 EVT OperandEltVT = OperandVT.getVectorElementType();
13543 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13544 } else {
13545 // A scalar operand; just use it as is.
13546 Operands[j] = Operand;
13547 }
13548 }
13549
13550 switch (N->getOpcode()) {
13551 default: {
13552 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13553 N->getFlags()));
13554 break;
13555 }
13556 case ISD::VSELECT:
13557 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13558 break;
13559 case ISD::SHL:
13560 case ISD::SRA:
13561 case ISD::SRL:
13562 case ISD::ROTL:
13563 case ISD::ROTR:
13564 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13565 getShiftAmountOperand(Operands[0].getValueType(),
13566 Operands[1])));
13567 break;
13569 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13570 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13571 Operands[0],
13572 getValueType(ExtVT)));
13573 break;
13574 }
13575 case ISD::ADDRSPACECAST: {
13576 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13577 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13578 ASC->getSrcAddressSpace(),
13579 ASC->getDestAddressSpace()));
13580 break;
13581 }
13582 }
13583 }
13584
13585 for (; i < ResNE; ++i)
13586 Scalars.push_back(getUNDEF(EltVT));
13587
13588 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13589 return getBuildVector(VecVT, dl, Scalars);
13590}
13591
13592std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13593 SDNode *N, unsigned ResNE) {
13594 unsigned Opcode = N->getOpcode();
13595 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13596 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13597 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13598 "Expected an overflow opcode");
13599
13600 EVT ResVT = N->getValueType(0);
13601 EVT OvVT = N->getValueType(1);
13602 EVT ResEltVT = ResVT.getVectorElementType();
13603 EVT OvEltVT = OvVT.getVectorElementType();
13604 SDLoc dl(N);
13605
13606 // If ResNE is 0, fully unroll the vector op.
13607 unsigned NE = ResVT.getVectorNumElements();
13608 if (ResNE == 0)
13609 ResNE = NE;
13610 else if (NE > ResNE)
13611 NE = ResNE;
13612
13613 SmallVector<SDValue, 8> LHSScalars;
13614 SmallVector<SDValue, 8> RHSScalars;
13615 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13616 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13617
13618 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13619 SDVTList VTs = getVTList(ResEltVT, SVT);
13620 SmallVector<SDValue, 8> ResScalars;
13621 SmallVector<SDValue, 8> OvScalars;
13622 for (unsigned i = 0; i < NE; ++i) {
13623 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13624 SDValue Ov =
13625 getSelect(dl, OvEltVT, Res.getValue(1),
13626 getBoolConstant(true, dl, OvEltVT, ResVT),
13627 getConstant(0, dl, OvEltVT));
13628
13629 ResScalars.push_back(Res);
13630 OvScalars.push_back(Ov);
13631 }
13632
13633 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13634 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13635
13636 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13637 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13638 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13639 getBuildVector(NewOvVT, dl, OvScalars));
13640}
13641
13644 unsigned Bytes,
13645 int Dist) const {
13646 if (LD->isVolatile() || Base->isVolatile())
13647 return false;
13648 // TODO: probably too restrictive for atomics, revisit
13649 if (!LD->isSimple())
13650 return false;
13651 if (LD->isIndexed() || Base->isIndexed())
13652 return false;
13653 if (LD->getChain() != Base->getChain())
13654 return false;
13655 EVT VT = LD->getMemoryVT();
13656 if (VT.getSizeInBits() / 8 != Bytes)
13657 return false;
13658
13659 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13660 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13661
13662 int64_t Offset = 0;
13663 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13664 return (Dist * (int64_t)Bytes == Offset);
13665 return false;
13666}
13667
13668/// InferPtrAlignment - Infer alignment of a load / store address. Return
13669/// std::nullopt if it cannot be inferred.
13671 // If this is a GlobalAddress + cst, return the alignment.
13672 const GlobalValue *GV = nullptr;
13673 int64_t GVOffset = 0;
13674 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13675 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13676 KnownBits Known(PtrWidth);
13678 unsigned AlignBits = Known.countMinTrailingZeros();
13679 if (AlignBits)
13680 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13681 }
13682
13683 // If this is a direct reference to a stack slot, use information about the
13684 // stack slot's alignment.
13685 int FrameIdx = INT_MIN;
13686 int64_t FrameOffset = 0;
13688 FrameIdx = FI->getIndex();
13689 } else if (isBaseWithConstantOffset(Ptr) &&
13691 // Handle FI+Cst
13692 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13693 FrameOffset = Ptr.getConstantOperandVal(1);
13694 }
13695
13696 if (FrameIdx != INT_MIN) {
13698 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13699 }
13700
13701 return std::nullopt;
13702}
13703
13704/// Split the scalar node with EXTRACT_ELEMENT using the provided
13705/// VTs and return the low/high part.
13706std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13707 const SDLoc &DL,
13708 const EVT &LoVT,
13709 const EVT &HiVT) {
13710 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13711 "Split node must be a scalar type");
13712 SDValue Lo =
13714 SDValue Hi =
13716 return std::make_pair(Lo, Hi);
13717}
13718
13719/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13720/// which is split (or expanded) into two not necessarily identical pieces.
13721std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13722 // Currently all types are split in half.
13723 EVT LoVT, HiVT;
13724 if (!VT.isVector())
13725 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13726 else
13727 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13728
13729 return std::make_pair(LoVT, HiVT);
13730}
13731
13732/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13733/// type, dependent on an enveloping VT that has been split into two identical
13734/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13735std::pair<EVT, EVT>
13737 bool *HiIsEmpty) const {
13738 EVT EltTp = VT.getVectorElementType();
13739 // Examples:
13740 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13741 // custom VL=9 with enveloping VL=8/8 yields 8/1
13742 // custom VL=10 with enveloping VL=8/8 yields 8/2
13743 // etc.
13744 ElementCount VTNumElts = VT.getVectorElementCount();
13745 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13746 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13747 "Mixing fixed width and scalable vectors when enveloping a type");
13748 EVT LoVT, HiVT;
13749 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13750 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13751 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13752 *HiIsEmpty = false;
13753 } else {
13754 // Flag that hi type has zero storage size, but return split envelop type
13755 // (this would be easier if vector types with zero elements were allowed).
13756 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13757 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13758 *HiIsEmpty = true;
13759 }
13760 return std::make_pair(LoVT, HiVT);
13761}
13762
13763/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13764/// low/high part.
13765std::pair<SDValue, SDValue>
13766SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13767 const EVT &HiVT) {
13768 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13769 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13770 "Splitting vector with an invalid mixture of fixed and scalable "
13771 "vector types");
13773 N.getValueType().getVectorMinNumElements() &&
13774 "More vector elements requested than available!");
13775 SDValue Lo, Hi;
13776 Lo = getExtractSubvector(DL, LoVT, N, 0);
13777 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13778 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13779 // IDX with the runtime scaling factor of the result vector type. For
13780 // fixed-width result vectors, that runtime scaling factor is 1.
13783 return std::make_pair(Lo, Hi);
13784}
13785
13786std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13787 const SDLoc &DL) {
13788 // Split the vector length parameter.
13789 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13790 EVT VT = N.getValueType();
13792 "Expecting the mask to be an evenly-sized vector");
13793 SDValue HalfNumElts = getElementCount(
13795 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13796 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13797 return std::make_pair(Lo, Hi);
13798}
13799
13800/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13802 EVT VT = N.getValueType();
13805 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13806}
13807
13810 unsigned Start, unsigned Count,
13811 EVT EltVT) {
13812 EVT VT = Op.getValueType();
13813 if (Count == 0)
13815 if (EltVT == EVT())
13816 EltVT = VT.getVectorElementType();
13817 SDLoc SL(Op);
13818 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13819 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13820 }
13821}
13822
13823// getAddressSpace - Return the address space this GlobalAddress belongs to.
13825 return getGlobal()->getType()->getAddressSpace();
13826}
13827
13830 return Val.MachineCPVal->getType();
13831 return Val.ConstVal->getType();
13832}
13833
13834bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13835 unsigned &SplatBitSize,
13836 bool &HasAnyUndefs,
13837 unsigned MinSplatBits,
13838 bool IsBigEndian) const {
13839 EVT VT = getValueType(0);
13840 assert(VT.isVector() && "Expected a vector type");
13841 unsigned VecWidth = VT.getSizeInBits();
13842 if (MinSplatBits > VecWidth)
13843 return false;
13844
13845 // FIXME: The widths are based on this node's type, but build vectors can
13846 // truncate their operands.
13847 SplatValue = APInt(VecWidth, 0);
13848 SplatUndef = APInt(VecWidth, 0);
13849
13850 // Get the bits. Bits with undefined values (when the corresponding element
13851 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13852 // in SplatValue. If any of the values are not constant, give up and return
13853 // false.
13854 unsigned int NumOps = getNumOperands();
13855 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13856 unsigned EltWidth = VT.getScalarSizeInBits();
13857
13858 for (unsigned j = 0; j < NumOps; ++j) {
13859 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13860 SDValue OpVal = getOperand(i);
13861 unsigned BitPos = j * EltWidth;
13862
13863 if (OpVal.isUndef())
13864 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13865 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13866 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13867 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13868 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13869 else
13870 return false;
13871 }
13872
13873 // The build_vector is all constants or undefs. Find the smallest element
13874 // size that splats the vector.
13875 HasAnyUndefs = (SplatUndef != 0);
13876
13877 // FIXME: This does not work for vectors with elements less than 8 bits.
13878 while (VecWidth > 8) {
13879 // If we can't split in half, stop here.
13880 if (VecWidth & 1)
13881 break;
13882
13883 unsigned HalfSize = VecWidth / 2;
13884 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13885 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13886 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13887 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13888
13889 // If the two halves do not match (ignoring undef bits), stop here.
13890 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13891 MinSplatBits > HalfSize)
13892 break;
13893
13894 SplatValue = HighValue | LowValue;
13895 SplatUndef = HighUndef & LowUndef;
13896
13897 VecWidth = HalfSize;
13898 }
13899
13900 // FIXME: The loop above only tries to split in halves. But if the input
13901 // vector for example is <3 x i16> it wouldn't be able to detect a
13902 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13903 // optimizations. I guess that back in the days when this helper was created
13904 // vectors normally was power-of-2 sized.
13905
13906 SplatBitSize = VecWidth;
13907 return true;
13908}
13909
13911 BitVector *UndefElements) const {
13912 unsigned NumOps = getNumOperands();
13913 if (UndefElements) {
13914 UndefElements->clear();
13915 UndefElements->resize(NumOps);
13916 }
13917 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13918 if (!DemandedElts)
13919 return SDValue();
13920 SDValue Splatted;
13921 for (unsigned i = 0; i != NumOps; ++i) {
13922 if (!DemandedElts[i])
13923 continue;
13924 SDValue Op = getOperand(i);
13925 if (Op.isUndef()) {
13926 if (UndefElements)
13927 (*UndefElements)[i] = true;
13928 } else if (!Splatted) {
13929 Splatted = Op;
13930 } else if (Splatted != Op) {
13931 return SDValue();
13932 }
13933 }
13934
13935 if (!Splatted) {
13936 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13937 assert(getOperand(FirstDemandedIdx).isUndef() &&
13938 "Can only have a splat without a constant for all undefs.");
13939 return getOperand(FirstDemandedIdx);
13940 }
13941
13942 return Splatted;
13943}
13944
13946 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13947 return getSplatValue(DemandedElts, UndefElements);
13948}
13949
13951 SmallVectorImpl<SDValue> &Sequence,
13952 BitVector *UndefElements) const {
13953 unsigned NumOps = getNumOperands();
13954 Sequence.clear();
13955 if (UndefElements) {
13956 UndefElements->clear();
13957 UndefElements->resize(NumOps);
13958 }
13959 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13960 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13961 return false;
13962
13963 // Set the undefs even if we don't find a sequence (like getSplatValue).
13964 if (UndefElements)
13965 for (unsigned I = 0; I != NumOps; ++I)
13966 if (DemandedElts[I] && getOperand(I).isUndef())
13967 (*UndefElements)[I] = true;
13968
13969 // Iteratively widen the sequence length looking for repetitions.
13970 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13971 Sequence.append(SeqLen, SDValue());
13972 for (unsigned I = 0; I != NumOps; ++I) {
13973 if (!DemandedElts[I])
13974 continue;
13975 SDValue &SeqOp = Sequence[I % SeqLen];
13977 if (Op.isUndef()) {
13978 if (!SeqOp)
13979 SeqOp = Op;
13980 continue;
13981 }
13982 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13983 Sequence.clear();
13984 break;
13985 }
13986 SeqOp = Op;
13987 }
13988 if (!Sequence.empty())
13989 return true;
13990 }
13991
13992 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13993 return false;
13994}
13995
13997 BitVector *UndefElements) const {
13998 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13999 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14000}
14001
14004 BitVector *UndefElements) const {
14006 getSplatValue(DemandedElts, UndefElements));
14007}
14008
14011 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14012}
14013
14016 BitVector *UndefElements) const {
14018 getSplatValue(DemandedElts, UndefElements));
14019}
14020
14025
14026int32_t
14028 uint32_t BitWidth) const {
14029 if (ConstantFPSDNode *CN =
14031 bool IsExact;
14032 APSInt IntVal(BitWidth);
14033 const APFloat &APF = CN->getValueAPF();
14034 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14035 APFloat::opOK ||
14036 !IsExact)
14037 return -1;
14038
14039 return IntVal.exactLogBase2();
14040 }
14041 return -1;
14042}
14043
14045 bool IsLittleEndian, unsigned DstEltSizeInBits,
14046 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14047 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14048 if (!isConstant())
14049 return false;
14050
14051 unsigned NumSrcOps = getNumOperands();
14052 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14053 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14054 "Invalid bitcast scale");
14055
14056 // Extract raw src bits.
14057 SmallVector<APInt> SrcBitElements(NumSrcOps,
14058 APInt::getZero(SrcEltSizeInBits));
14059 BitVector SrcUndeElements(NumSrcOps, false);
14060
14061 for (unsigned I = 0; I != NumSrcOps; ++I) {
14063 if (Op.isUndef()) {
14064 SrcUndeElements.set(I);
14065 continue;
14066 }
14067 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14068 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14069 assert((CInt || CFP) && "Unknown constant");
14070 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14071 : CFP->getValueAPF().bitcastToAPInt();
14072 }
14073
14074 // Recast to dst width.
14075 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14076 SrcBitElements, UndefElements, SrcUndeElements);
14077 return true;
14078}
14079
14080void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14081 unsigned DstEltSizeInBits,
14082 SmallVectorImpl<APInt> &DstBitElements,
14083 ArrayRef<APInt> SrcBitElements,
14084 BitVector &DstUndefElements,
14085 const BitVector &SrcUndefElements) {
14086 unsigned NumSrcOps = SrcBitElements.size();
14087 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14088 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14089 "Invalid bitcast scale");
14090 assert(NumSrcOps == SrcUndefElements.size() &&
14091 "Vector size mismatch");
14092
14093 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14094 DstUndefElements.clear();
14095 DstUndefElements.resize(NumDstOps, false);
14096 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14097
14098 // Concatenate src elements constant bits together into dst element.
14099 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14100 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14101 for (unsigned I = 0; I != NumDstOps; ++I) {
14102 DstUndefElements.set(I);
14103 APInt &DstBits = DstBitElements[I];
14104 for (unsigned J = 0; J != Scale; ++J) {
14105 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14106 if (SrcUndefElements[Idx])
14107 continue;
14108 DstUndefElements.reset(I);
14109 const APInt &SrcBits = SrcBitElements[Idx];
14110 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14111 "Illegal constant bitwidths");
14112 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14113 }
14114 }
14115 return;
14116 }
14117
14118 // Split src element constant bits into dst elements.
14119 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14120 for (unsigned I = 0; I != NumSrcOps; ++I) {
14121 if (SrcUndefElements[I]) {
14122 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14123 continue;
14124 }
14125 const APInt &SrcBits = SrcBitElements[I];
14126 for (unsigned J = 0; J != Scale; ++J) {
14127 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14128 APInt &DstBits = DstBitElements[Idx];
14129 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14130 }
14131 }
14132}
14133
14135 for (const SDValue &Op : op_values()) {
14136 unsigned Opc = Op.getOpcode();
14137 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14138 return false;
14139 }
14140 return true;
14141}
14142
14143std::optional<std::pair<APInt, APInt>>
14145 unsigned NumOps = getNumOperands();
14146 if (NumOps < 2)
14147 return std::nullopt;
14148
14151 return std::nullopt;
14152
14153 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14154 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14155 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14156
14157 if (Stride.isZero())
14158 return std::nullopt;
14159
14160 for (unsigned i = 2; i < NumOps; ++i) {
14162 return std::nullopt;
14163
14164 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14165 if (Val != (Start + (Stride * i)))
14166 return std::nullopt;
14167 }
14168
14169 return std::make_pair(Start, Stride);
14170}
14171
14173 // Find the first non-undef value in the shuffle mask.
14174 unsigned i, e;
14175 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14176 /* search */;
14177
14178 // If all elements are undefined, this shuffle can be considered a splat
14179 // (although it should eventually get simplified away completely).
14180 if (i == e)
14181 return true;
14182
14183 // Make sure all remaining elements are either undef or the same as the first
14184 // non-undef value.
14185 for (int Idx = Mask[i]; i != e; ++i)
14186 if (Mask[i] >= 0 && Mask[i] != Idx)
14187 return false;
14188 return true;
14189}
14190
14191// Returns true if it is a constant integer BuildVector or constant integer,
14192// possibly hidden by a bitcast.
14194 SDValue N, bool AllowOpaques) const {
14196
14197 if (auto *C = dyn_cast<ConstantSDNode>(N))
14198 return AllowOpaques || !C->isOpaque();
14199
14201 return true;
14202
14203 // Treat a GlobalAddress supporting constant offset folding as a
14204 // constant integer.
14205 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14206 if (GA->getOpcode() == ISD::GlobalAddress &&
14207 TLI->isOffsetFoldingLegal(GA))
14208 return true;
14209
14210 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14211 isa<ConstantSDNode>(N.getOperand(0)))
14212 return true;
14213 return false;
14214}
14215
14216// Returns true if it is a constant float BuildVector or constant float.
14219 return true;
14220
14222 return true;
14223
14224 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14225 isa<ConstantFPSDNode>(N.getOperand(0)))
14226 return true;
14227
14228 return false;
14229}
14230
14231std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14232 ConstantSDNode *Const =
14233 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14234 if (!Const)
14235 return std::nullopt;
14236
14237 EVT VT = N->getValueType(0);
14238 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14239 switch (TLI->getBooleanContents(N.getValueType())) {
14241 if (CVal.isOne())
14242 return true;
14243 if (CVal.isZero())
14244 return false;
14245 return std::nullopt;
14247 if (CVal.isAllOnes())
14248 return true;
14249 if (CVal.isZero())
14250 return false;
14251 return std::nullopt;
14253 return CVal[0];
14254 }
14255 llvm_unreachable("Unknown BooleanContent enum");
14256}
14257
14258void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14259 assert(!Node->OperandList && "Node already has operands");
14261 "too many operands to fit into SDNode");
14262 SDUse *Ops = OperandRecycler.allocate(
14263 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14264
14265 bool IsDivergent = false;
14266 for (unsigned I = 0; I != Vals.size(); ++I) {
14267 Ops[I].setUser(Node);
14268 Ops[I].setInitial(Vals[I]);
14269 EVT VT = Ops[I].getValueType();
14270
14271 // Skip Chain. It does not carry divergence.
14272 if (VT != MVT::Other &&
14273 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14274 Ops[I].getNode()->isDivergent()) {
14275 IsDivergent = true;
14276 }
14277 }
14278 Node->NumOperands = Vals.size();
14279 Node->OperandList = Ops;
14280 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14281 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14282 Node->SDNodeBits.IsDivergent = IsDivergent;
14283 }
14284 checkForCycles(Node);
14285}
14286
14289 size_t Limit = SDNode::getMaxNumOperands();
14290 while (Vals.size() > Limit) {
14291 unsigned SliceIdx = Vals.size() - Limit;
14292 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14293 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14294 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14295 Vals.emplace_back(NewTF);
14296 }
14297 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14298}
14299
14301 EVT VT, SDNodeFlags Flags) {
14302 switch (Opcode) {
14303 default:
14304 return SDValue();
14305 case ISD::ADD:
14306 case ISD::OR:
14307 case ISD::XOR:
14308 case ISD::UMAX:
14309 return getConstant(0, DL, VT);
14310 case ISD::MUL:
14311 return getConstant(1, DL, VT);
14312 case ISD::AND:
14313 case ISD::UMIN:
14314 return getAllOnesConstant(DL, VT);
14315 case ISD::SMAX:
14317 case ISD::SMIN:
14319 case ISD::FADD:
14320 // If flags allow, prefer positive zero since it's generally cheaper
14321 // to materialize on most targets.
14322 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14323 case ISD::FMUL:
14324 return getConstantFP(1.0, DL, VT);
14325 case ISD::FMINNUM:
14326 case ISD::FMAXNUM: {
14327 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14328 const fltSemantics &Semantics = VT.getFltSemantics();
14329 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14330 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14331 APFloat::getLargest(Semantics);
14332 if (Opcode == ISD::FMAXNUM)
14333 NeutralAF.changeSign();
14334
14335 return getConstantFP(NeutralAF, DL, VT);
14336 }
14337 case ISD::FMINIMUM:
14338 case ISD::FMAXIMUM: {
14339 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14340 const fltSemantics &Semantics = VT.getFltSemantics();
14341 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14342 : APFloat::getLargest(Semantics);
14343 if (Opcode == ISD::FMAXIMUM)
14344 NeutralAF.changeSign();
14345
14346 return getConstantFP(NeutralAF, DL, VT);
14347 }
14348
14349 }
14350}
14351
14352/// Helper used to make a call to a library function that has one argument of
14353/// pointer type.
14354///
14355/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14356/// used to get or set floating-point state. They have one argument of pointer
14357/// type, which points to the memory region containing bits of the
14358/// floating-point state. The value returned by such function is ignored in the
14359/// created call.
14360///
14361/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14362/// \param Ptr Pointer used to save/load state.
14363/// \param InChain Ingoing token chain.
14364/// \returns Outgoing chain token.
14366 SDValue InChain,
14367 const SDLoc &DLoc) {
14368 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14370 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14371 RTLIB::LibcallImpl LibcallImpl =
14372 TLI->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14373 if (LibcallImpl == RTLIB::Unsupported)
14374 reportFatalUsageError("emitting call to unsupported libcall");
14375
14376 SDValue Callee =
14377 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14379 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14380 TLI->getLibcallImplCallingConv(LibcallImpl),
14381 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14382 return TLI->LowerCallTo(CLI).second;
14383}
14384
14386 assert(From && To && "Invalid SDNode; empty source SDValue?");
14387 auto I = SDEI.find(From);
14388 if (I == SDEI.end())
14389 return;
14390
14391 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14392 // the iterator, hence the need to make a copy to prevent a use-after-free.
14393 NodeExtraInfo NEI = I->second;
14394 if (LLVM_LIKELY(!NEI.PCSections)) {
14395 // No deep copy required for the types of extra info set.
14396 //
14397 // FIXME: Investigate if other types of extra info also need deep copy. This
14398 // depends on the types of nodes they can be attached to: if some extra info
14399 // is only ever attached to nodes where a replacement To node is always the
14400 // node where later use and propagation of the extra info has the intended
14401 // semantics, no deep copy is required.
14402 SDEI[To] = std::move(NEI);
14403 return;
14404 }
14405
14406 const SDNode *EntrySDN = getEntryNode().getNode();
14407
14408 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14409 // through the replacement of From with To. Otherwise, replacements of a node
14410 // (From) with more complex nodes (To and its operands) may result in lost
14411 // extra info where the root node (To) is insignificant in further propagating
14412 // and using extra info when further lowering to MIR.
14413 //
14414 // In the first step pre-populate the visited set with the nodes reachable
14415 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14416 // DAG that is not new and should be left untouched.
14417 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14418 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14419 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14420 if (MaxDepth == 0) {
14421 // Remember this node in case we need to increase MaxDepth and continue
14422 // populating FromReach from this node.
14423 Leafs.emplace_back(N);
14424 return;
14425 }
14426 if (!FromReach.insert(N).second)
14427 return;
14428 for (const SDValue &Op : N->op_values())
14429 Self(Self, Op.getNode(), MaxDepth - 1);
14430 };
14431
14432 // Copy extra info to To and all its transitive operands (that are new).
14434 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14435 if (FromReach.contains(N))
14436 return true;
14437 if (!Visited.insert(N).second)
14438 return true;
14439 if (EntrySDN == N)
14440 return false;
14441 for (const SDValue &Op : N->op_values()) {
14442 if (N == To && Op.getNode() == EntrySDN) {
14443 // Special case: New node's operand is the entry node; just need to
14444 // copy extra info to new node.
14445 break;
14446 }
14447 if (!Self(Self, Op.getNode()))
14448 return false;
14449 }
14450 // Copy only if entry node was not reached.
14451 SDEI[N] = NEI;
14452 return true;
14453 };
14454
14455 // We first try with a lower MaxDepth, assuming that the path to common
14456 // operands between From and To is relatively short. This significantly
14457 // improves performance in the common case. The initial MaxDepth is big
14458 // enough to avoid retry in the common case; the last MaxDepth is large
14459 // enough to avoid having to use the fallback below (and protects from
14460 // potential stack exhaustion from recursion).
14461 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14462 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14463 // StartFrom is the previous (or initial) set of leafs reachable at the
14464 // previous maximum depth.
14466 std::swap(StartFrom, Leafs);
14467 for (const SDNode *N : StartFrom)
14468 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14469 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14470 return;
14471 // This should happen very rarely (reached the entry node).
14472 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14473 assert(!Leafs.empty());
14474 }
14475
14476 // This should not happen - but if it did, that means the subgraph reachable
14477 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14478 // could not visit all reachable common operands. Consequently, we were able
14479 // to reach the entry node.
14480 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14481 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14482 // Best-effort fallback if assertions disabled.
14483 SDEI[To] = std::move(NEI);
14484}
14485
14486#ifndef NDEBUG
14487static void checkForCyclesHelper(const SDNode *N,
14490 const llvm::SelectionDAG *DAG) {
14491 // If this node has already been checked, don't check it again.
14492 if (Checked.count(N))
14493 return;
14494
14495 // If a node has already been visited on this depth-first walk, reject it as
14496 // a cycle.
14497 if (!Visited.insert(N).second) {
14498 errs() << "Detected cycle in SelectionDAG\n";
14499 dbgs() << "Offending node:\n";
14500 N->dumprFull(DAG); dbgs() << "\n";
14501 abort();
14502 }
14503
14504 for (const SDValue &Op : N->op_values())
14505 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14506
14507 Checked.insert(N);
14508 Visited.erase(N);
14509}
14510#endif
14511
14513 const llvm::SelectionDAG *DAG,
14514 bool force) {
14515#ifndef NDEBUG
14516 bool check = force;
14517#ifdef EXPENSIVE_CHECKS
14518 check = true;
14519#endif // EXPENSIVE_CHECKS
14520 if (check) {
14521 assert(N && "Checking nonexistent SDNode");
14524 checkForCyclesHelper(N, visited, checked, DAG);
14525 }
14526#endif // !NDEBUG
14527}
14528
14529void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14530 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14531}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
constexpr LLT S1
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:592
#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)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#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 std::pair< SDValue, SDValue > getRuntimeCallSDValueHelper(SDValue Chain, const SDLoc &dl, TargetLowering::ArgListTy &&Args, const CallInst *CI, RTLIB::Libcall Call, SelectionDAG *DAG, const TargetLowering *TLI)
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:1183
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1271
void copySign(const APFloat &RHS)
Definition APFloat.h:1365
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5975
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1253
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:1495
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1244
bool isFinite() const
Definition APFloat.h:1517
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1410
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1262
bool isSignaling() const
Definition APFloat.h:1514
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1298
bool isZero() const
Definition APFloat.h:1508
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1201
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1395
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1161
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1289
bool isPosZero() const
Definition APFloat.h:1523
bool isNegZero() const
Definition APFloat.h:1524
void changeSign()
Definition APFloat.h:1360
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1172
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1982
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2066
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1584
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:1415
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1023
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:1549
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1400
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1679
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1394
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:1044
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1521
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:1339
APInt abs() const
Get the absolute value.
Definition APInt.h:1804
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2037
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:1677
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1497
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:1655
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1405
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1165
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:1648
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1637
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1607
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:2097
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2111
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1052
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1152
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:1444
unsigned logBase2() const
Definition APInt.h:1770
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2047
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:1747
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:996
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1376
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:1426
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:1397
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:2056
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
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
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:209
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
Tracks which library functions to use for a particular subtarget.
LLVM_ABI RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Return the lowering's selection of implementation call for Call.
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.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
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:235
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 void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, const LibcallLoweringInfo *LibcallsInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
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.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
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.
const LibcallLoweringInfo & getLibcalls() const
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 std::pair< SDValue, SDValue > getStrstr(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strstr operation into a target library call and return the resulting chain and call result as...
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 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.
CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall implementation.
unsigned getMaxStoresPerMemset(bool OptSize) const
Get maximum # of store operations permitted for llvm.memset.
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.
virtual unsigned getMaxGluedStoresPerMemcpy() const
Get maximum # of store operations to be glued together.
RTLIB::LibcallImpl getLibcallImpl(RTLIB::Libcall Call) const
Get the libcall impl routine name for the specified libcall.
std::vector< ArgListEntry > ArgListTy
unsigned getMaxStoresPerMemmove(bool OptSize) const
Get maximum # of store operations permitted for llvm.memmove.
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.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
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:632
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:207
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.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
Definition APInt.cpp:3212
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3142
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3129
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3119
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3193
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3134
LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
Definition APInt.cpp:3202
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2277
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3184
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:3020
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3217
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2282
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3114
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3124
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:818
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:787
@ TargetConstantPool
Definition ISDOpcodes.h:189
@ 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:511
@ PTRADD
PTRADD represents pointer arithmetic semantics, for targets that opt in using shouldPreservePtrArith(...
@ 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:236
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ 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:538
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:778
@ TargetBlockAddress
Definition ISDOpcodes.h:191
@ DEACTIVATION_SYMBOL
Untyped node storing deactivation symbol reference (DeactivationSymbolSDNode).
@ 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:294
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:522
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ 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:852
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ 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:879
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ 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:746
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:909
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:992
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:773
@ 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:843
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:714
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:664
@ TargetExternalSymbol
Definition ISDOpcodes.h:190
@ 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:786
@ TargetJumpTable
Definition ISDOpcodes.h:188
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:198
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ TRUNCATE_SSAT_U
Definition ISDOpcodes.h:872
@ 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:826
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:690
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:795
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:671
@ 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
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ 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:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:791
@ 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:703
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:764
@ 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:649
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ 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:576
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:849
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:810
@ 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:386
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ TargetFrameIndex
Definition ISDOpcodes.h:187
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by IMM elements and retu...
Definition ISDOpcodes.h:653
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:898
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:887
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:726
@ 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:977
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:804
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ 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:500
@ 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:925
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:505
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:738
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ 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:734
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:709
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) right by IMM elements and re...
Definition ISDOpcodes.h:656
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:680
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ 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:958
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:698
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:920
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:996
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ 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:944
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:855
@ 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:832
@ 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:534
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ TRUNCATE_SSAT_S
TRUNCATE_[SU]SAT_[SU] - Truncate for saturated operand [SU] located in middle, prefix for SAT means i...
Definition ISDOpcodes.h:870
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:721
@ TRUNCATE_USAT_U
Definition ISDOpcodes.h:874
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:338
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
LLVM_ABI NodeType getOppositeSignednessMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns the corresponding opcode with the opposi...
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.
Definition Types.h:26
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:237
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:1613
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:2544
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:1706
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
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:1595
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:1618
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:1661
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:1692
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:163
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:1642
@ 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:539
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:1883
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:719
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:1945
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:1679
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1719
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:177
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
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
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:314
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:268
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:127
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:255
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:287
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:122
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:164
KnownBits byteSwap() const
Definition KnownBits.h:532
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:302
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:536
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:246
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:175
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:334
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:238
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:324
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:183
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:199
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
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:339
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const
Truncate with signed saturation (signed input -> signed output)
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:293
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:232
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:170
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const
Truncate with unsigned saturation (unsigned input -> unsigned output)
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).
LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const
Truncate with signed saturation to unsigned (signed input -> unsigned output)
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)
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
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)