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->getMemoryVT().getRawBits());
993 for (const MachineMemOperand *MMO : MN->memoperands()) {
994 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
995 ID.AddInteger(MMO->getFlags());
996 }
997 }
998}
999
1000/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
1001/// data.
1003 AddNodeIDOpcode(ID, N->getOpcode());
1004 // Add the return value info.
1005 AddNodeIDValueTypes(ID, N->getVTList());
1006 // Add the operand info.
1007 AddNodeIDOperands(ID, N->ops());
1008
1009 // Handle SDNode leafs with special info.
1011}
1012
1013//===----------------------------------------------------------------------===//
1014// SelectionDAG Class
1015//===----------------------------------------------------------------------===//
1016
1017/// doNotCSE - Return true if CSE should not be performed for this node.
1018static bool doNotCSE(SDNode *N) {
1019 if (N->getValueType(0) == MVT::Glue)
1020 return true; // Never CSE anything that produces a glue result.
1021
1022 switch (N->getOpcode()) {
1023 default: break;
1024 case ISD::HANDLENODE:
1025 case ISD::EH_LABEL:
1026 return true; // Never CSE these nodes.
1027 }
1028
1029 // Check that remaining values produced are not flags.
1030 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1031 if (N->getValueType(i) == MVT::Glue)
1032 return true; // Never CSE anything that produces a glue result.
1033
1034 return false;
1035}
1036
1037/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1038/// SelectionDAG.
1040 // Create a dummy node (which is not added to allnodes), that adds a reference
1041 // to the root node, preventing it from being deleted.
1042 HandleSDNode Dummy(getRoot());
1043
1044 SmallVector<SDNode*, 128> DeadNodes;
1045
1046 // Add all obviously-dead nodes to the DeadNodes worklist.
1047 for (SDNode &Node : allnodes())
1048 if (Node.use_empty())
1049 DeadNodes.push_back(&Node);
1050
1051 RemoveDeadNodes(DeadNodes);
1052
1053 // If the root changed (e.g. it was a dead load, update the root).
1054 setRoot(Dummy.getValue());
1055}
1056
1057/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1058/// given list, and any nodes that become unreachable as a result.
1060
1061 // Process the worklist, deleting the nodes and adding their uses to the
1062 // worklist.
1063 while (!DeadNodes.empty()) {
1064 SDNode *N = DeadNodes.pop_back_val();
1065 // Skip to next node if we've already managed to delete the node. This could
1066 // happen if replacing a node causes a node previously added to the node to
1067 // be deleted.
1068 if (N->getOpcode() == ISD::DELETED_NODE)
1069 continue;
1070
1071 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1072 DUL->NodeDeleted(N, nullptr);
1073
1074 // Take the node out of the appropriate CSE map.
1075 RemoveNodeFromCSEMaps(N);
1076
1077 // Next, brutally remove the operand list. This is safe to do, as there are
1078 // no cycles in the graph.
1079 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1080 SDUse &Use = *I++;
1081 SDNode *Operand = Use.getNode();
1082 Use.set(SDValue());
1083
1084 // Now that we removed this operand, see if there are no uses of it left.
1085 if (Operand->use_empty())
1086 DeadNodes.push_back(Operand);
1087 }
1088
1089 DeallocateNode(N);
1090 }
1091}
1092
1094 SmallVector<SDNode*, 16> DeadNodes(1, N);
1095
1096 // Create a dummy node that adds a reference to the root node, preventing
1097 // it from being deleted. (This matters if the root is an operand of the
1098 // dead node.)
1099 HandleSDNode Dummy(getRoot());
1100
1101 RemoveDeadNodes(DeadNodes);
1102}
1103
1105 // First take this out of the appropriate CSE map.
1106 RemoveNodeFromCSEMaps(N);
1107
1108 // Finally, remove uses due to operands of this node, remove from the
1109 // AllNodes list, and delete the node.
1110 DeleteNodeNotInCSEMaps(N);
1111}
1112
1113void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1114 assert(N->getIterator() != AllNodes.begin() &&
1115 "Cannot delete the entry node!");
1116 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1117
1118 // Drop all of the operands and decrement used node's use counts.
1119 N->DropOperands();
1120
1121 DeallocateNode(N);
1122}
1123
1124void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1125 assert(!(V->isVariadic() && isParameter));
1126 if (isParameter)
1127 ByvalParmDbgValues.push_back(V);
1128 else
1129 DbgValues.push_back(V);
1130 for (const SDNode *Node : V->getSDNodes())
1131 if (Node)
1132 DbgValMap[Node].push_back(V);
1133}
1134
1136 DbgValMapType::iterator I = DbgValMap.find(Node);
1137 if (I == DbgValMap.end())
1138 return;
1139 for (auto &Val: I->second)
1140 Val->setIsInvalidated();
1141 DbgValMap.erase(I);
1142}
1143
1144void SelectionDAG::DeallocateNode(SDNode *N) {
1145 // If we have operands, deallocate them.
1147
1148 NodeAllocator.Deallocate(AllNodes.remove(N));
1149
1150 // Set the opcode to DELETED_NODE to help catch bugs when node
1151 // memory is reallocated.
1152 // FIXME: There are places in SDag that have grown a dependency on the opcode
1153 // value in the released node.
1154 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1155 N->NodeType = ISD::DELETED_NODE;
1156
1157 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1158 // them and forget about that node.
1159 DbgInfo->erase(N);
1160
1161 // Invalidate extra info.
1162 SDEI.erase(N);
1163}
1164
1165#ifndef NDEBUG
1166/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1167void SelectionDAG::verifyNode(SDNode *N) const {
1168 switch (N->getOpcode()) {
1169 default:
1170 if (N->isTargetOpcode())
1172 break;
1173 case ISD::BUILD_PAIR: {
1174 EVT VT = N->getValueType(0);
1175 assert(N->getNumValues() == 1 && "Too many results!");
1176 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1177 "Wrong return type!");
1178 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1179 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1180 "Mismatched operand types!");
1181 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1182 "Wrong operand type!");
1183 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1184 "Wrong return type size");
1185 break;
1186 }
1187 case ISD::BUILD_VECTOR: {
1188 assert(N->getNumValues() == 1 && "Too many results!");
1189 assert(N->getValueType(0).isVector() && "Wrong return type!");
1190 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1191 "Wrong number of operands!");
1192 EVT EltVT = N->getValueType(0).getVectorElementType();
1193 for (const SDUse &Op : N->ops()) {
1194 assert((Op.getValueType() == EltVT ||
1195 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1196 EltVT.bitsLE(Op.getValueType()))) &&
1197 "Wrong operand type!");
1198 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1199 "Operands must all have the same type");
1200 }
1201 break;
1202 }
1203 case ISD::SADDO:
1204 case ISD::UADDO:
1205 case ISD::SSUBO:
1206 case ISD::USUBO:
1207 assert(N->getNumValues() == 2 && "Wrong number of results!");
1208 assert(N->getVTList().NumVTs == 2 && N->getNumOperands() == 2 &&
1209 "Invalid add/sub overflow op!");
1210 assert(N->getVTList().VTs[0].isInteger() &&
1211 N->getVTList().VTs[1].isInteger() &&
1212 N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1213 N->getOperand(0).getValueType() == N->getVTList().VTs[0] &&
1214 "Binary operator types must match!");
1215 break;
1216 }
1217}
1218#endif // NDEBUG
1219
1220/// Insert a newly allocated node into the DAG.
1221///
1222/// Handles insertion into the all nodes list and CSE map, as well as
1223/// verification and other common operations when a new node is allocated.
1224void SelectionDAG::InsertNode(SDNode *N) {
1225 AllNodes.push_back(N);
1226#ifndef NDEBUG
1227 N->PersistentId = NextPersistentId++;
1228 verifyNode(N);
1229#endif
1230 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1231 DUL->NodeInserted(N);
1232}
1233
1234/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1235/// correspond to it. This is useful when we're about to delete or repurpose
1236/// the node. We don't want future request for structurally identical nodes
1237/// to return N anymore.
1238bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1239 bool Erased = false;
1240 switch (N->getOpcode()) {
1241 case ISD::HANDLENODE: return false; // noop.
1242 case ISD::CONDCODE:
1243 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1244 "Cond code doesn't exist!");
1245 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1246 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1247 break;
1249 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1250 break;
1252 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1253 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1254 ESN->getSymbol(), ESN->getTargetFlags()));
1255 break;
1256 }
1257 case ISD::MCSymbol: {
1258 auto *MCSN = cast<MCSymbolSDNode>(N);
1259 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1260 break;
1261 }
1262 case ISD::VALUETYPE: {
1263 EVT VT = cast<VTSDNode>(N)->getVT();
1264 if (VT.isExtended()) {
1265 Erased = ExtendedValueTypeNodes.erase(VT);
1266 } else {
1267 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1268 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1269 }
1270 break;
1271 }
1272 default:
1273 // Remove it from the CSE Map.
1274 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1275 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1276 Erased = CSEMap.RemoveNode(N);
1277 break;
1278 }
1279#ifndef NDEBUG
1280 // Verify that the node was actually in one of the CSE maps, unless it has a
1281 // glue result (which cannot be CSE'd) or is one of the special cases that are
1282 // not subject to CSE.
1283 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1284 !N->isMachineOpcode() && !doNotCSE(N)) {
1285 N->dump(this);
1286 dbgs() << "\n";
1287 llvm_unreachable("Node is not in map!");
1288 }
1289#endif
1290 return Erased;
1291}
1292
1293/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1294/// maps and modified in place. Add it back to the CSE maps, unless an identical
1295/// node already exists, in which case transfer all its users to the existing
1296/// node. This transfer can potentially trigger recursive merging.
1297void
1298SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1299 // For node types that aren't CSE'd, just act as if no identical node
1300 // already exists.
1301 if (!doNotCSE(N)) {
1302 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1303 if (Existing != N) {
1304 // If there was already an existing matching node, use ReplaceAllUsesWith
1305 // to replace the dead one with the existing one. This can cause
1306 // recursive merging of other unrelated nodes down the line.
1307 Existing->intersectFlagsWith(N->getFlags());
1308 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1309 MemNode->refineRanges(cast<MemSDNode>(N)->memoperands());
1310 ReplaceAllUsesWith(N, Existing);
1311
1312 // N is now dead. Inform the listeners and delete it.
1313 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1314 DUL->NodeDeleted(N, Existing);
1315 DeleteNodeNotInCSEMaps(N);
1316 return;
1317 }
1318 }
1319
1320 // If the node doesn't already exist, we updated it. Inform listeners.
1321 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1322 DUL->NodeUpdated(N);
1323}
1324
1325/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1326/// were replaced with those specified. If this node is never memoized,
1327/// return null, otherwise return a pointer to the slot it would take. If a
1328/// node already exists with these operands, the slot will be non-null.
1329SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1330 void *&InsertPos) {
1331 if (doNotCSE(N))
1332 return nullptr;
1333
1334 SDValue Ops[] = { Op };
1335 FoldingSetNodeID ID;
1336 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1338 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1339 if (Node)
1340 Node->intersectFlagsWith(N->getFlags());
1341 return Node;
1342}
1343
1344/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1345/// were replaced with those specified. If this node is never memoized,
1346/// return null, otherwise return a pointer to the slot it would take. If a
1347/// node already exists with these operands, the slot will be non-null.
1348SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1349 SDValue Op1, SDValue Op2,
1350 void *&InsertPos) {
1351 if (doNotCSE(N))
1352 return nullptr;
1353
1354 SDValue Ops[] = { Op1, Op2 };
1355 FoldingSetNodeID ID;
1356 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1358 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1359 if (Node)
1360 Node->intersectFlagsWith(N->getFlags());
1361 return Node;
1362}
1363
1364/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1365/// were replaced with those specified. If this node is never memoized,
1366/// return null, otherwise return a pointer to the slot it would take. If a
1367/// node already exists with these operands, the slot will be non-null.
1368SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1369 void *&InsertPos) {
1370 if (doNotCSE(N))
1371 return nullptr;
1372
1373 FoldingSetNodeID ID;
1374 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1376 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1377 if (Node)
1378 Node->intersectFlagsWith(N->getFlags());
1379 return Node;
1380}
1381
1383 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1384 : VT.getTypeForEVT(*getContext());
1385
1386 return getDataLayout().getABITypeAlign(Ty);
1387}
1388
1389// EntryNode could meaningfully have debug info if we can find it...
1391 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1392 getVTList(MVT::Other, MVT::Glue)),
1393 Root(getEntryNode()) {
1394 InsertNode(&EntryNode);
1395 DbgInfo = new SDDbgInfo();
1396}
1397
1399 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1400 const TargetLibraryInfo *LibraryInfo,
1401 const LibcallLoweringInfo *LibcallsInfo,
1402 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1404 FunctionVarLocs const *VarLocs) {
1405 MF = &NewMF;
1406 SDAGISelPass = PassPtr;
1407 ORE = &NewORE;
1410 LibInfo = LibraryInfo;
1411 Libcalls = LibcallsInfo;
1412 Context = &MF->getFunction().getContext();
1413 UA = NewUA;
1414 PSI = PSIin;
1415 BFI = BFIin;
1416 MMI = &MMIin;
1417 FnVarLocs = VarLocs;
1418}
1419
1421 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1422 allnodes_clear();
1423 OperandRecycler.clear(OperandAllocator);
1424 delete DbgInfo;
1425}
1426
1428 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1429}
1430
1431void SelectionDAG::allnodes_clear() {
1432 assert(&*AllNodes.begin() == &EntryNode);
1433 AllNodes.remove(AllNodes.begin());
1434 while (!AllNodes.empty())
1435 DeallocateNode(&AllNodes.front());
1436#ifndef NDEBUG
1437 NextPersistentId = 0;
1438#endif
1439}
1440
1441SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1442 void *&InsertPos) {
1443 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1444 if (N) {
1445 switch (N->getOpcode()) {
1446 default: break;
1447 case ISD::Constant:
1448 case ISD::ConstantFP:
1449 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1450 "debug location. Use another overload.");
1451 }
1452 }
1453 return N;
1454}
1455
1456SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1457 const SDLoc &DL, void *&InsertPos) {
1458 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1459 if (N) {
1460 switch (N->getOpcode()) {
1461 case ISD::Constant:
1462 case ISD::ConstantFP:
1463 // Erase debug location from the node if the node is used at several
1464 // different places. Do not propagate one location to all uses as it
1465 // will cause a worse single stepping debugging experience.
1466 if (N->getDebugLoc() != DL.getDebugLoc())
1467 N->setDebugLoc(DebugLoc());
1468 break;
1469 default:
1470 // When the node's point of use is located earlier in the instruction
1471 // sequence than its prior point of use, update its debug info to the
1472 // earlier location.
1473 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1474 N->setDebugLoc(DL.getDebugLoc());
1475 break;
1476 }
1477 }
1478 return N;
1479}
1480
1482 allnodes_clear();
1483 OperandRecycler.clear(OperandAllocator);
1484 OperandAllocator.Reset();
1485 CSEMap.clear();
1486
1487 ExtendedValueTypeNodes.clear();
1488 ExternalSymbols.clear();
1489 TargetExternalSymbols.clear();
1490 MCSymbols.clear();
1491 SDEI.clear();
1492 llvm::fill(CondCodeNodes, nullptr);
1493 llvm::fill(ValueTypeNodes, nullptr);
1494
1495 EntryNode.UseList = nullptr;
1496 InsertNode(&EntryNode);
1497 Root = getEntryNode();
1498 DbgInfo->clear();
1499}
1500
1502 return VT.bitsGT(Op.getValueType())
1503 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1504 : getNode(ISD::FP_ROUND, DL, VT, Op,
1505 getIntPtrConstant(0, DL, /*isTarget=*/true));
1506}
1507
1508std::pair<SDValue, SDValue>
1510 const SDLoc &DL, EVT VT) {
1511 assert(!VT.bitsEq(Op.getValueType()) &&
1512 "Strict no-op FP extend/round not allowed.");
1513 SDValue Res =
1514 VT.bitsGT(Op.getValueType())
1515 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1516 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1517 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1518
1519 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1520}
1521
1523 return VT.bitsGT(Op.getValueType()) ?
1524 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1525 getNode(ISD::TRUNCATE, DL, VT, Op);
1526}
1527
1529 return VT.bitsGT(Op.getValueType()) ?
1530 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1531 getNode(ISD::TRUNCATE, DL, VT, Op);
1532}
1533
1535 return VT.bitsGT(Op.getValueType()) ?
1536 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1537 getNode(ISD::TRUNCATE, DL, VT, Op);
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(EVT::getIntegerVT(*Context, Size), Op);
1549 if (DestOp.getValueType() == VT)
1550 return DestOp;
1551
1552 return getAnyExtOrTrunc(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 getSExtOrTrunc(DestOp, DL, VT);
1568}
1569
1571 EVT VT) {
1572 assert(!VT.isVector());
1573 auto Type = Op.getValueType();
1574 SDValue DestOp;
1575 if (Type == VT)
1576 return Op;
1577 auto Size = Op.getValueSizeInBits();
1578 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1579 if (DestOp.getValueType() == VT)
1580 return DestOp;
1581
1582 return getZExtOrTrunc(DestOp, DL, VT);
1583}
1584
1586 EVT OpVT) {
1587 if (VT.bitsLE(Op.getValueType()))
1588 return getNode(ISD::TRUNCATE, SL, VT, Op);
1589
1590 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1591 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1592}
1593
1595 EVT OpVT = Op.getValueType();
1596 assert(VT.isInteger() && OpVT.isInteger() &&
1597 "Cannot getZeroExtendInReg FP types");
1598 assert(VT.isVector() == OpVT.isVector() &&
1599 "getZeroExtendInReg type should be vector iff the operand "
1600 "type is vector!");
1601 assert((!VT.isVector() ||
1603 "Vector element counts must match in getZeroExtendInReg");
1604 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1605 if (OpVT == VT)
1606 return Op;
1607 // TODO: Use computeKnownBits instead of AssertZext.
1608 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1609 ->getVT()
1610 .getScalarType()
1611 .bitsLE(VT.getScalarType()))
1612 return Op;
1614 VT.getScalarSizeInBits());
1615 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1616}
1617
1619 SDValue EVL, const SDLoc &DL,
1620 EVT VT) {
1621 EVT OpVT = Op.getValueType();
1622 assert(VT.isInteger() && OpVT.isInteger() &&
1623 "Cannot getVPZeroExtendInReg FP types");
1624 assert(VT.isVector() && OpVT.isVector() &&
1625 "getVPZeroExtendInReg type and operand type should be vector!");
1627 "Vector element counts must match in getZeroExtendInReg");
1628 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1629 if (OpVT == VT)
1630 return Op;
1632 VT.getScalarSizeInBits());
1633 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1634 EVL);
1635}
1636
1638 // Only unsigned pointer semantics are supported right now. In the future this
1639 // might delegate to TLI to check pointer signedness.
1640 return getZExtOrTrunc(Op, DL, VT);
1641}
1642
1644 // Only unsigned pointer semantics are supported right now. In the future this
1645 // might delegate to TLI to check pointer signedness.
1646 return getZeroExtendInReg(Op, DL, VT);
1647}
1648
1650 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1651}
1652
1653/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1655 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1656}
1657
1659 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1660 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1661}
1662
1664 SDValue Mask, SDValue EVL, EVT VT) {
1665 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1666 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1667}
1668
1670 SDValue Mask, SDValue EVL) {
1671 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1672}
1673
1675 SDValue Mask, SDValue EVL) {
1676 if (VT.bitsGT(Op.getValueType()))
1677 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1678 if (VT.bitsLT(Op.getValueType()))
1679 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1680 return Op;
1681}
1682
1684 EVT OpVT) {
1685 if (!V)
1686 return getConstant(0, DL, VT);
1687
1688 switch (TLI->getBooleanContents(OpVT)) {
1691 return getConstant(1, DL, VT);
1693 return getAllOnesConstant(DL, VT);
1694 }
1695 llvm_unreachable("Unexpected boolean content enum!");
1696}
1697
1699 bool isT, bool isO) {
1700 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1701 DL, VT, isT, isO);
1702}
1703
1705 bool isT, bool isO) {
1706 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1707}
1708
1710 EVT VT, bool isT, bool isO) {
1711 assert(VT.isInteger() && "Cannot create FP integer constant!");
1712
1713 EVT EltVT = VT.getScalarType();
1714 const ConstantInt *Elt = &Val;
1715
1716 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1717 // to-be-splatted scalar ConstantInt.
1718 if (isa<VectorType>(Elt->getType()))
1719 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1720
1721 // In some cases the vector type is legal but the element type is illegal and
1722 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1723 // inserted value (the type does not need to match the vector element type).
1724 // Any extra bits introduced will be truncated away.
1725 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1727 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1728 APInt NewVal;
1729 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1730 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1731 else
1732 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1733 Elt = ConstantInt::get(*getContext(), NewVal);
1734 }
1735 // In other cases the element type is illegal and needs to be expanded, for
1736 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1737 // the value into n parts and use a vector type with n-times the elements.
1738 // Then bitcast to the type requested.
1739 // Legalizing constants too early makes the DAGCombiner's job harder so we
1740 // only legalize if the DAG tells us we must produce legal types.
1741 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1742 TLI->getTypeAction(*getContext(), EltVT) ==
1744 const APInt &NewVal = Elt->getValue();
1745 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1746 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1747
1748 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1749 if (VT.isScalableVector() ||
1750 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1751 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1752 "Can only handle an even split!");
1753 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1754
1755 SmallVector<SDValue, 2> ScalarParts;
1756 for (unsigned i = 0; i != Parts; ++i)
1757 ScalarParts.push_back(getConstant(
1758 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1759 ViaEltVT, isT, isO));
1760
1761 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1762 }
1763
1764 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1765 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1766
1767 // Check the temporary vector is the correct size. If this fails then
1768 // getTypeToTransformTo() probably returned a type whose size (in bits)
1769 // isn't a power-of-2 factor of the requested type size.
1770 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1771
1772 SmallVector<SDValue, 2> EltParts;
1773 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1774 EltParts.push_back(getConstant(
1775 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1776 ViaEltVT, isT, isO));
1777
1778 // EltParts is currently in little endian order. If we actually want
1779 // big-endian order then reverse it now.
1780 if (getDataLayout().isBigEndian())
1781 std::reverse(EltParts.begin(), EltParts.end());
1782
1783 // The elements must be reversed when the element order is different
1784 // to the endianness of the elements (because the BITCAST is itself a
1785 // vector shuffle in this situation). However, we do not need any code to
1786 // perform this reversal because getConstant() is producing a vector
1787 // splat.
1788 // This situation occurs in MIPS MSA.
1789
1791 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1792 llvm::append_range(Ops, EltParts);
1793
1794 SDValue V =
1795 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1796 return V;
1797 }
1798
1799 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1800 "APInt size does not match type size!");
1801 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1802 SDVTList VTs = getVTList(EltVT);
1804 AddNodeIDNode(ID, Opc, VTs, {});
1805 ID.AddPointer(Elt);
1806 ID.AddBoolean(isO);
1807 void *IP = nullptr;
1808 SDNode *N = nullptr;
1809 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1810 if (!VT.isVector())
1811 return SDValue(N, 0);
1812
1813 if (!N) {
1814 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1815 CSEMap.InsertNode(N, IP);
1816 InsertNode(N);
1817 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1818 }
1819
1820 SDValue Result(N, 0);
1821 if (VT.isVector())
1822 Result = getSplat(VT, DL, Result);
1823 return Result;
1824}
1825
1827 bool isT, bool isO) {
1828 unsigned Size = VT.getScalarSizeInBits();
1829 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1830}
1831
1833 bool IsOpaque) {
1835 IsTarget, IsOpaque);
1836}
1837
1839 bool isTarget) {
1840 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1841}
1842
1844 const SDLoc &DL) {
1845 assert(VT.isInteger() && "Shift amount is not an integer type!");
1846 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1847 return getConstant(Val, DL, ShiftVT);
1848}
1849
1851 const SDLoc &DL) {
1852 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1853 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1854}
1855
1857 bool isTarget) {
1858 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1859}
1860
1862 bool isTarget) {
1863 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1864}
1865
1867 EVT VT, bool isTarget) {
1868 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1869
1870 EVT EltVT = VT.getScalarType();
1871 const ConstantFP *Elt = &V;
1872
1873 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1874 // the to-be-splatted scalar ConstantFP.
1875 if (isa<VectorType>(Elt->getType()))
1876 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1877
1878 // Do the map lookup using the actual bit pattern for the floating point
1879 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1880 // we don't have issues with SNANs.
1881 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1882 SDVTList VTs = getVTList(EltVT);
1884 AddNodeIDNode(ID, Opc, VTs, {});
1885 ID.AddPointer(Elt);
1886 void *IP = nullptr;
1887 SDNode *N = nullptr;
1888 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1889 if (!VT.isVector())
1890 return SDValue(N, 0);
1891
1892 if (!N) {
1893 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1894 CSEMap.InsertNode(N, IP);
1895 InsertNode(N);
1896 }
1897
1898 SDValue Result(N, 0);
1899 if (VT.isVector())
1900 Result = getSplat(VT, DL, Result);
1901 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1902 return Result;
1903}
1904
1906 bool isTarget) {
1907 EVT EltVT = VT.getScalarType();
1908 if (EltVT == MVT::f32)
1909 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1910 if (EltVT == MVT::f64)
1911 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1912 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1913 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1914 bool Ignored;
1915 APFloat APF = APFloat(Val);
1917 &Ignored);
1918 return getConstantFP(APF, DL, VT, isTarget);
1919 }
1920 llvm_unreachable("Unsupported type in getConstantFP");
1921}
1922
1924 EVT VT, int64_t Offset, bool isTargetGA,
1925 unsigned TargetFlags) {
1926 assert((TargetFlags == 0 || isTargetGA) &&
1927 "Cannot set target flags on target-independent globals");
1928
1929 // Truncate (with sign-extension) the offset value to the pointer size.
1931 if (BitWidth < 64)
1933
1934 unsigned Opc;
1935 if (GV->isThreadLocal())
1937 else
1939
1940 SDVTList VTs = getVTList(VT);
1942 AddNodeIDNode(ID, Opc, VTs, {});
1943 ID.AddPointer(GV);
1944 ID.AddInteger(Offset);
1945 ID.AddInteger(TargetFlags);
1946 void *IP = nullptr;
1947 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1948 return SDValue(E, 0);
1949
1950 auto *N = newSDNode<GlobalAddressSDNode>(
1951 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1952 CSEMap.InsertNode(N, IP);
1953 InsertNode(N);
1954 return SDValue(N, 0);
1955}
1956
1958 SDVTList VTs = getVTList(MVT::Untyped);
1961 ID.AddPointer(GV);
1962 void *IP = nullptr;
1963 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1964 return SDValue(E, 0);
1965
1966 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1967 CSEMap.InsertNode(N, IP);
1968 InsertNode(N);
1969 return SDValue(N, 0);
1970}
1971
1972SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1973 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1974 SDVTList VTs = getVTList(VT);
1976 AddNodeIDNode(ID, Opc, VTs, {});
1977 ID.AddInteger(FI);
1978 void *IP = nullptr;
1979 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1980 return SDValue(E, 0);
1981
1982 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1983 CSEMap.InsertNode(N, IP);
1984 InsertNode(N);
1985 return SDValue(N, 0);
1986}
1987
1988SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1989 unsigned TargetFlags) {
1990 assert((TargetFlags == 0 || isTarget) &&
1991 "Cannot set target flags on target-independent jump tables");
1992 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1993 SDVTList VTs = getVTList(VT);
1995 AddNodeIDNode(ID, Opc, VTs, {});
1996 ID.AddInteger(JTI);
1997 ID.AddInteger(TargetFlags);
1998 void *IP = nullptr;
1999 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2000 return SDValue(E, 0);
2001
2002 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
2003 CSEMap.InsertNode(N, IP);
2004 InsertNode(N);
2005 return SDValue(N, 0);
2006}
2007
2009 const SDLoc &DL) {
2011 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
2012 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
2013}
2014
2016 MaybeAlign Alignment, int Offset,
2017 bool isTarget, unsigned TargetFlags) {
2018 assert((TargetFlags == 0 || isTarget) &&
2019 "Cannot set target flags on target-independent globals");
2020 if (!Alignment)
2021 Alignment = shouldOptForSize()
2022 ? getDataLayout().getABITypeAlign(C->getType())
2023 : getDataLayout().getPrefTypeAlign(C->getType());
2024 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2025 SDVTList VTs = getVTList(VT);
2027 AddNodeIDNode(ID, Opc, VTs, {});
2028 ID.AddInteger(Alignment->value());
2029 ID.AddInteger(Offset);
2030 ID.AddPointer(C);
2031 ID.AddInteger(TargetFlags);
2032 void *IP = nullptr;
2033 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2034 return SDValue(E, 0);
2035
2036 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2037 TargetFlags);
2038 CSEMap.InsertNode(N, IP);
2039 InsertNode(N);
2040 SDValue V = SDValue(N, 0);
2041 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2042 return V;
2043}
2044
2046 MaybeAlign Alignment, int Offset,
2047 bool isTarget, unsigned TargetFlags) {
2048 assert((TargetFlags == 0 || isTarget) &&
2049 "Cannot set target flags on target-independent globals");
2050 if (!Alignment)
2051 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2052 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2053 SDVTList VTs = getVTList(VT);
2055 AddNodeIDNode(ID, Opc, VTs, {});
2056 ID.AddInteger(Alignment->value());
2057 ID.AddInteger(Offset);
2058 C->addSelectionDAGCSEId(ID);
2059 ID.AddInteger(TargetFlags);
2060 void *IP = nullptr;
2061 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2062 return SDValue(E, 0);
2063
2064 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2065 TargetFlags);
2066 CSEMap.InsertNode(N, IP);
2067 InsertNode(N);
2068 return SDValue(N, 0);
2069}
2070
2073 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2074 ID.AddPointer(MBB);
2075 void *IP = nullptr;
2076 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2077 return SDValue(E, 0);
2078
2079 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2080 CSEMap.InsertNode(N, IP);
2081 InsertNode(N);
2082 return SDValue(N, 0);
2083}
2084
2086 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2087 ValueTypeNodes.size())
2088 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2089
2090 SDNode *&N = VT.isExtended() ?
2091 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2092
2093 if (N) return SDValue(N, 0);
2094 N = newSDNode<VTSDNode>(VT);
2095 InsertNode(N);
2096 return SDValue(N, 0);
2097}
2098
2100 SDNode *&N = ExternalSymbols[Sym];
2101 if (N) return SDValue(N, 0);
2102 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2103 InsertNode(N);
2104 return SDValue(N, 0);
2105}
2106
2107SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2109 return getExternalSymbol(SymName.data(), VT);
2110}
2111
2113 SDNode *&N = MCSymbols[Sym];
2114 if (N)
2115 return SDValue(N, 0);
2116 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2117 InsertNode(N);
2118 return SDValue(N, 0);
2119}
2120
2122 unsigned TargetFlags) {
2123 SDNode *&N =
2124 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2125 if (N) return SDValue(N, 0);
2126 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2127 InsertNode(N);
2128 return SDValue(N, 0);
2129}
2130
2132 EVT VT, unsigned TargetFlags) {
2134 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2135}
2136
2138 if ((unsigned)Cond >= CondCodeNodes.size())
2139 CondCodeNodes.resize(Cond+1);
2140
2141 if (!CondCodeNodes[Cond]) {
2142 auto *N = newSDNode<CondCodeSDNode>(Cond);
2143 CondCodeNodes[Cond] = N;
2144 InsertNode(N);
2145 }
2146
2147 return SDValue(CondCodeNodes[Cond], 0);
2148}
2149
2151 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2152 "APInt size does not match type size!");
2153
2154 if (MulImm == 0)
2155 return getConstant(0, DL, VT);
2156
2157 const MachineFunction &MF = getMachineFunction();
2158 const Function &F = MF.getFunction();
2159 ConstantRange CR = getVScaleRange(&F, 64);
2160 if (const APInt *C = CR.getSingleElement())
2161 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2162
2163 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2164}
2165
2166/// \returns a value of type \p VT that represents the runtime value of \p
2167/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2168/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2169/// or TypeSize.
2170template <typename Ty>
2172 EVT VT, Ty Quantity) {
2173 if (Quantity.isScalable())
2174 return DAG.getVScale(
2175 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2176
2177 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2178}
2179
2181 ElementCount EC) {
2182 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2183}
2184
2186 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2187}
2188
2190 ElementCount EC) {
2191 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2192 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2193 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2194 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2195}
2196
2198 APInt One(ResVT.getScalarSizeInBits(), 1);
2199 return getStepVector(DL, ResVT, One);
2200}
2201
2203 const APInt &StepVal) {
2204 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2205 if (ResVT.isScalableVector())
2206 return getNode(
2207 ISD::STEP_VECTOR, DL, ResVT,
2208 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2209
2210 SmallVector<SDValue, 16> OpsStepConstants;
2211 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2212 OpsStepConstants.push_back(
2213 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2214 return getBuildVector(ResVT, DL, OpsStepConstants);
2215}
2216
2217/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2218/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2223
2225 SDValue N2, ArrayRef<int> Mask) {
2226 assert(VT.getVectorNumElements() == Mask.size() &&
2227 "Must have the same number of vector elements as mask elements!");
2228 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2229 "Invalid VECTOR_SHUFFLE");
2230
2231 // Canonicalize shuffle undef, undef -> undef
2232 if (N1.isUndef() && N2.isUndef())
2233 return getUNDEF(VT);
2234
2235 // Validate that all indices in Mask are within the range of the elements
2236 // input to the shuffle.
2237 int NElts = Mask.size();
2238 assert(llvm::all_of(Mask,
2239 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2240 "Index out of range");
2241
2242 // Copy the mask so we can do any needed cleanup.
2243 SmallVector<int, 8> MaskVec(Mask);
2244
2245 // Canonicalize shuffle v, v -> v, undef
2246 if (N1 == N2) {
2247 N2 = getUNDEF(VT);
2248 for (int i = 0; i != NElts; ++i)
2249 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2250 }
2251
2252 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2253 if (N1.isUndef())
2254 commuteShuffle(N1, N2, MaskVec);
2255
2256 if (TLI->hasVectorBlend()) {
2257 // If shuffling a splat, try to blend the splat instead. We do this here so
2258 // that even when this arises during lowering we don't have to re-handle it.
2259 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2260 BitVector UndefElements;
2261 SDValue Splat = BV->getSplatValue(&UndefElements);
2262 if (!Splat)
2263 return;
2264
2265 for (int i = 0; i < NElts; ++i) {
2266 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2267 continue;
2268
2269 // If this input comes from undef, mark it as such.
2270 if (UndefElements[MaskVec[i] - Offset]) {
2271 MaskVec[i] = -1;
2272 continue;
2273 }
2274
2275 // If we can blend a non-undef lane, use that instead.
2276 if (!UndefElements[i])
2277 MaskVec[i] = i + Offset;
2278 }
2279 };
2280 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2281 BlendSplat(N1BV, 0);
2282 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2283 BlendSplat(N2BV, NElts);
2284 }
2285
2286 // Canonicalize all index into lhs, -> shuffle lhs, undef
2287 // Canonicalize all index into rhs, -> shuffle rhs, undef
2288 bool AllLHS = true, AllRHS = true;
2289 bool N2Undef = N2.isUndef();
2290 for (int i = 0; i != NElts; ++i) {
2291 if (MaskVec[i] >= NElts) {
2292 if (N2Undef)
2293 MaskVec[i] = -1;
2294 else
2295 AllLHS = false;
2296 } else if (MaskVec[i] >= 0) {
2297 AllRHS = false;
2298 }
2299 }
2300 if (AllLHS && AllRHS)
2301 return getUNDEF(VT);
2302 if (AllLHS && !N2Undef)
2303 N2 = getUNDEF(VT);
2304 if (AllRHS) {
2305 N1 = getUNDEF(VT);
2306 commuteShuffle(N1, N2, MaskVec);
2307 }
2308 // Reset our undef status after accounting for the mask.
2309 N2Undef = N2.isUndef();
2310 // Re-check whether both sides ended up undef.
2311 if (N1.isUndef() && N2Undef)
2312 return getUNDEF(VT);
2313
2314 // If Identity shuffle return that node.
2315 bool Identity = true, AllSame = true;
2316 for (int i = 0; i != NElts; ++i) {
2317 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2318 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2319 }
2320 if (Identity && NElts)
2321 return N1;
2322
2323 // Shuffling a constant splat doesn't change the result.
2324 if (N2Undef) {
2325 SDValue V = N1;
2326
2327 // Look through any bitcasts. We check that these don't change the number
2328 // (and size) of elements and just changes their types.
2329 while (V.getOpcode() == ISD::BITCAST)
2330 V = V->getOperand(0);
2331
2332 // A splat should always show up as a build vector node.
2333 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2334 BitVector UndefElements;
2335 SDValue Splat = BV->getSplatValue(&UndefElements);
2336 // If this is a splat of an undef, shuffling it is also undef.
2337 if (Splat && Splat.isUndef())
2338 return getUNDEF(VT);
2339
2340 bool SameNumElts =
2341 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2342
2343 // We only have a splat which can skip shuffles if there is a splatted
2344 // value and no undef lanes rearranged by the shuffle.
2345 if (Splat && UndefElements.none()) {
2346 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2347 // number of elements match or the value splatted is a zero constant.
2348 if (SameNumElts || isNullConstant(Splat))
2349 return N1;
2350 }
2351
2352 // If the shuffle itself creates a splat, build the vector directly.
2353 if (AllSame && SameNumElts) {
2354 EVT BuildVT = BV->getValueType(0);
2355 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2356 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2357
2358 // We may have jumped through bitcasts, so the type of the
2359 // BUILD_VECTOR may not match the type of the shuffle.
2360 if (BuildVT != VT)
2361 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2362 return NewBV;
2363 }
2364 }
2365 }
2366
2367 SDVTList VTs = getVTList(VT);
2369 SDValue Ops[2] = { N1, N2 };
2371 for (int i = 0; i != NElts; ++i)
2372 ID.AddInteger(MaskVec[i]);
2373
2374 void* IP = nullptr;
2375 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2376 return SDValue(E, 0);
2377
2378 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2379 // SDNode doesn't have access to it. This memory will be "leaked" when
2380 // the node is deallocated, but recovered when the NodeAllocator is released.
2381 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2382 llvm::copy(MaskVec, MaskAlloc);
2383
2384 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2385 dl.getDebugLoc(), MaskAlloc);
2386 createOperands(N, Ops);
2387
2388 CSEMap.InsertNode(N, IP);
2389 InsertNode(N);
2390 SDValue V = SDValue(N, 0);
2391 NewSDValueDbgMsg(V, "Creating new node: ", this);
2392 return V;
2393}
2394
2396 EVT VT = SV.getValueType(0);
2397 SmallVector<int, 8> MaskVec(SV.getMask());
2399
2400 SDValue Op0 = SV.getOperand(0);
2401 SDValue Op1 = SV.getOperand(1);
2402 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2403}
2404
2406 SDVTList VTs = getVTList(VT);
2408 AddNodeIDNode(ID, ISD::Register, VTs, {});
2409 ID.AddInteger(Reg.id());
2410 void *IP = nullptr;
2411 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2412 return SDValue(E, 0);
2413
2414 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2415 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2416 CSEMap.InsertNode(N, IP);
2417 InsertNode(N);
2418 return SDValue(N, 0);
2419}
2420
2423 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2424 ID.AddPointer(RegMask);
2425 void *IP = nullptr;
2426 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2427 return SDValue(E, 0);
2428
2429 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2430 CSEMap.InsertNode(N, IP);
2431 InsertNode(N);
2432 return SDValue(N, 0);
2433}
2434
2436 MCSymbol *Label) {
2437 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2438}
2439
2440SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2441 SDValue Root, MCSymbol *Label) {
2443 SDValue Ops[] = { Root };
2444 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2445 ID.AddPointer(Label);
2446 void *IP = nullptr;
2447 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2448 return SDValue(E, 0);
2449
2450 auto *N =
2451 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2452 createOperands(N, Ops);
2453
2454 CSEMap.InsertNode(N, IP);
2455 InsertNode(N);
2456 return SDValue(N, 0);
2457}
2458
2460 int64_t Offset, bool isTarget,
2461 unsigned TargetFlags) {
2462 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2463 SDVTList VTs = getVTList(VT);
2464
2466 AddNodeIDNode(ID, Opc, VTs, {});
2467 ID.AddPointer(BA);
2468 ID.AddInteger(Offset);
2469 ID.AddInteger(TargetFlags);
2470 void *IP = nullptr;
2471 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2472 return SDValue(E, 0);
2473
2474 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2475 CSEMap.InsertNode(N, IP);
2476 InsertNode(N);
2477 return SDValue(N, 0);
2478}
2479
2482 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2483 ID.AddPointer(V);
2484
2485 void *IP = nullptr;
2486 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2487 return SDValue(E, 0);
2488
2489 auto *N = newSDNode<SrcValueSDNode>(V);
2490 CSEMap.InsertNode(N, IP);
2491 InsertNode(N);
2492 return SDValue(N, 0);
2493}
2494
2497 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2498 ID.AddPointer(MD);
2499
2500 void *IP = nullptr;
2501 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2502 return SDValue(E, 0);
2503
2504 auto *N = newSDNode<MDNodeSDNode>(MD);
2505 CSEMap.InsertNode(N, IP);
2506 InsertNode(N);
2507 return SDValue(N, 0);
2508}
2509
2511 if (VT == V.getValueType())
2512 return V;
2513
2514 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2515}
2516
2518 unsigned SrcAS, unsigned DestAS) {
2519 SDVTList VTs = getVTList(VT);
2520 SDValue Ops[] = {Ptr};
2523 ID.AddInteger(SrcAS);
2524 ID.AddInteger(DestAS);
2525
2526 void *IP = nullptr;
2527 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2528 return SDValue(E, 0);
2529
2530 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2531 VTs, SrcAS, DestAS);
2532 createOperands(N, Ops);
2533
2534 CSEMap.InsertNode(N, IP);
2535 InsertNode(N);
2536 return SDValue(N, 0);
2537}
2538
2540 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2541}
2542
2544 bool PoisonOnly) {
2545 if (isGuaranteedNotToBeUndefOrPoison(V, DemandedElts, PoisonOnly))
2546 return V;
2547 return getFreeze(V);
2548}
2549
2550/// getShiftAmountOperand - Return the specified value casted to
2551/// the target's desired shift amount type.
2553 EVT OpTy = Op.getValueType();
2554 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2555 if (OpTy == ShTy || OpTy.isVector()) return Op;
2556
2557 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2558}
2559
2561 SDLoc dl(Node);
2563 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2564 EVT VT = Node->getValueType(0);
2565 SDValue Tmp1 = Node->getOperand(0);
2566 SDValue Tmp2 = Node->getOperand(1);
2567 const MaybeAlign MA(Node->getConstantOperandVal(3));
2568
2569 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2570 Tmp2, MachinePointerInfo(V));
2571 SDValue VAList = VAListLoad;
2572
2573 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2574 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2575 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2576
2577 VAList = getNode(
2578 ISD::AND, dl, VAList.getValueType(), VAList,
2579 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2580 }
2581
2582 // Increment the pointer, VAList, to the next vaarg
2583 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2584 getConstant(getDataLayout().getTypeAllocSize(
2585 VT.getTypeForEVT(*getContext())),
2586 dl, VAList.getValueType()));
2587 // Store the incremented VAList to the legalized pointer
2588 Tmp1 =
2589 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2590 // Load the actual argument out of the pointer VAList
2591 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2592}
2593
2595 SDLoc dl(Node);
2597 // This defaults to loading a pointer from the input and storing it to the
2598 // output, returning the chain.
2599 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2600 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2601 SDValue Tmp1 =
2602 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2603 Node->getOperand(2), MachinePointerInfo(VS));
2604 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2605 MachinePointerInfo(VD));
2606}
2607
2609 const DataLayout &DL = getDataLayout();
2610 Type *Ty = VT.getTypeForEVT(*getContext());
2611 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2612
2613 if (TLI->isTypeLegal(VT) || !VT.isVector())
2614 return RedAlign;
2615
2616 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2617 const Align StackAlign = TFI->getStackAlign();
2618
2619 // See if we can choose a smaller ABI alignment in cases where it's an
2620 // illegal vector type that will get broken down.
2621 if (RedAlign > StackAlign) {
2622 EVT IntermediateVT;
2623 MVT RegisterVT;
2624 unsigned NumIntermediates;
2625 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2626 NumIntermediates, RegisterVT);
2627 Ty = IntermediateVT.getTypeForEVT(*getContext());
2628 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2629 if (RedAlign2 < RedAlign)
2630 RedAlign = RedAlign2;
2631
2632 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2633 // If the stack is not realignable, the alignment should be limited to the
2634 // StackAlignment
2635 RedAlign = std::min(RedAlign, StackAlign);
2636 }
2637
2638 return RedAlign;
2639}
2640
2642 MachineFrameInfo &MFI = MF->getFrameInfo();
2643 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2644 int StackID = 0;
2645 if (Bytes.isScalable())
2646 StackID = TFI->getStackIDForScalableVectors();
2647 // The stack id gives an indication of whether the object is scalable or
2648 // not, so it's safe to pass in the minimum size here.
2649 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2650 false, nullptr, StackID);
2651 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2652}
2653
2655 Type *Ty = VT.getTypeForEVT(*getContext());
2656 Align StackAlign =
2657 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2658 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2659}
2660
2662 TypeSize VT1Size = VT1.getStoreSize();
2663 TypeSize VT2Size = VT2.getStoreSize();
2664 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2665 "Don't know how to choose the maximum size when creating a stack "
2666 "temporary");
2667 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2668 ? VT1Size
2669 : VT2Size;
2670
2671 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2672 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2673 const DataLayout &DL = getDataLayout();
2674 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2675 return CreateStackTemporary(Bytes, Align);
2676}
2677
2679 ISD::CondCode Cond, const SDLoc &dl,
2680 SDNodeFlags Flags) {
2681 EVT OpVT = N1.getValueType();
2682
2683 auto GetUndefBooleanConstant = [&]() {
2684 if (VT.getScalarType() == MVT::i1 ||
2685 TLI->getBooleanContents(OpVT) ==
2687 return getUNDEF(VT);
2688 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2689 // so we cannot use getUNDEF(). Return zero instead.
2690 return getConstant(0, dl, VT);
2691 };
2692
2693 // These setcc operations always fold.
2694 switch (Cond) {
2695 default: break;
2696 case ISD::SETFALSE:
2697 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2698 case ISD::SETTRUE:
2699 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2700
2701 case ISD::SETOEQ:
2702 case ISD::SETOGT:
2703 case ISD::SETOGE:
2704 case ISD::SETOLT:
2705 case ISD::SETOLE:
2706 case ISD::SETONE:
2707 case ISD::SETO:
2708 case ISD::SETUO:
2709 case ISD::SETUEQ:
2710 case ISD::SETUNE:
2711 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2712 break;
2713 }
2714
2715 if (OpVT.isInteger()) {
2716 // For EQ and NE, we can always pick a value for the undef to make the
2717 // predicate pass or fail, so we can return undef.
2718 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2719 // icmp eq/ne X, undef -> undef.
2720 if ((N1.isUndef() || N2.isUndef()) &&
2721 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2722 return GetUndefBooleanConstant();
2723
2724 // If both operands are undef, we can return undef for int comparison.
2725 // icmp undef, undef -> undef.
2726 if (N1.isUndef() && N2.isUndef())
2727 return GetUndefBooleanConstant();
2728
2729 // icmp X, X -> true/false
2730 // icmp X, undef -> true/false because undef could be X.
2731 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2732 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2733 }
2734
2736 const APInt &C2 = N2C->getAPIntValue();
2738 const APInt &C1 = N1C->getAPIntValue();
2739
2741 dl, VT, OpVT);
2742 }
2743 }
2744
2745 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2746 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2747
2748 if (N1CFP && N2CFP) {
2749 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2750 switch (Cond) {
2751 default: break;
2752 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2753 return GetUndefBooleanConstant();
2754 [[fallthrough]];
2755 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2756 OpVT);
2757 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2758 return GetUndefBooleanConstant();
2759 [[fallthrough]];
2761 R==APFloat::cmpLessThan, dl, VT,
2762 OpVT);
2763 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2764 return GetUndefBooleanConstant();
2765 [[fallthrough]];
2766 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2767 OpVT);
2768 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2769 return GetUndefBooleanConstant();
2770 [[fallthrough]];
2772 VT, OpVT);
2773 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2774 return GetUndefBooleanConstant();
2775 [[fallthrough]];
2777 R==APFloat::cmpEqual, dl, VT,
2778 OpVT);
2779 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2780 return GetUndefBooleanConstant();
2781 [[fallthrough]];
2783 R==APFloat::cmpEqual, dl, VT, OpVT);
2784 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2785 OpVT);
2786 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2787 OpVT);
2789 R==APFloat::cmpEqual, dl, VT,
2790 OpVT);
2791 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2792 OpVT);
2794 R==APFloat::cmpLessThan, dl, VT,
2795 OpVT);
2797 R==APFloat::cmpUnordered, dl, VT,
2798 OpVT);
2800 VT, OpVT);
2801 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2802 OpVT);
2803 }
2804 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2805 // Ensure that the constant occurs on the RHS.
2807 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2808 return SDValue();
2809 return getSetCC(dl, VT, N2, N1, SwappedCond, /*Chian=*/{},
2810 /*IsSignaling=*/false, Flags);
2811 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2812 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2813 // If an operand is known to be a nan (or undef that could be a nan), we can
2814 // fold it.
2815 // Choosing NaN for the undef will always make unordered comparison succeed
2816 // and ordered comparison fails.
2817 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2818 switch (ISD::getUnorderedFlavor(Cond)) {
2819 default:
2820 llvm_unreachable("Unknown flavor!");
2821 case 0: // Known false.
2822 return getBoolConstant(false, dl, VT, OpVT);
2823 case 1: // Known true.
2824 return getBoolConstant(true, dl, VT, OpVT);
2825 case 2: // Undefined.
2826 return GetUndefBooleanConstant();
2827 }
2828 }
2829
2830 // Could not fold it.
2831 return SDValue();
2832}
2833
2834/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2835/// use this predicate to simplify operations downstream.
2837 unsigned BitWidth = Op.getScalarValueSizeInBits();
2839}
2840
2841// TODO: Should have argument to specify if sign bit of nan is ignorable.
2843 if (Depth >= MaxRecursionDepth)
2844 return false; // Limit search depth.
2845
2846 unsigned Opc = Op.getOpcode();
2847 switch (Opc) {
2848 case ISD::FABS:
2849 return true;
2850 case ISD::AssertNoFPClass: {
2851 FPClassTest NoFPClass =
2852 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2853
2854 const FPClassTest TestMask = fcNan | fcNegative;
2855 return (NoFPClass & TestMask) == TestMask;
2856 }
2857 case ISD::ARITH_FENCE:
2858 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2859 case ISD::FEXP:
2860 case ISD::FEXP2:
2861 case ISD::FEXP10:
2862 return Op->getFlags().hasNoNaNs();
2863 case ISD::FMINNUM:
2864 case ISD::FMINNUM_IEEE:
2865 case ISD::FMINIMUM:
2866 case ISD::FMINIMUMNUM:
2867 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2868 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2869 case ISD::FMAXNUM:
2870 case ISD::FMAXNUM_IEEE:
2871 case ISD::FMAXIMUM:
2872 case ISD::FMAXIMUMNUM:
2873 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2874 // is sufficient.
2875 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2876 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2877 default:
2878 return false;
2879 }
2880
2881 llvm_unreachable("covered opcode switch");
2882}
2883
2884/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2885/// this predicate to simplify operations downstream. Mask is known to be zero
2886/// for bits that V cannot have.
2888 unsigned Depth) const {
2889 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2890}
2891
2892/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2893/// DemandedElts. We use this predicate to simplify operations downstream.
2894/// Mask is known to be zero for bits that V cannot have.
2896 const APInt &DemandedElts,
2897 unsigned Depth) const {
2898 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2899}
2900
2901/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2902/// DemandedElts. We use this predicate to simplify operations downstream.
2904 unsigned Depth /* = 0 */) const {
2905 return computeKnownBits(V, DemandedElts, Depth).isZero();
2906}
2907
2908/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2910 unsigned Depth) const {
2911 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2912}
2913
2915 const APInt &DemandedElts,
2916 unsigned Depth) const {
2917 EVT VT = Op.getValueType();
2918 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2919
2920 unsigned NumElts = VT.getVectorNumElements();
2921 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2922
2923 APInt KnownZeroElements = APInt::getZero(NumElts);
2924 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2925 if (!DemandedElts[EltIdx])
2926 continue; // Don't query elements that are not demanded.
2927 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2928 if (MaskedVectorIsZero(Op, Mask, Depth))
2929 KnownZeroElements.setBit(EltIdx);
2930 }
2931 return KnownZeroElements;
2932}
2933
2934/// isSplatValue - Return true if the vector V has the same value
2935/// across all DemandedElts. For scalable vectors, we don't know the
2936/// number of lanes at compile time. Instead, we use a 1 bit APInt
2937/// to represent a conservative value for all lanes; that is, that
2938/// one bit value is implicitly splatted across all lanes.
2939bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2940 APInt &UndefElts, unsigned Depth) const {
2941 unsigned Opcode = V.getOpcode();
2942 EVT VT = V.getValueType();
2943 assert(VT.isVector() && "Vector type expected");
2944 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2945 "scalable demanded bits are ignored");
2946
2947 if (!DemandedElts)
2948 return false; // No demanded elts, better to assume we don't know anything.
2949
2950 if (Depth >= MaxRecursionDepth)
2951 return false; // Limit search depth.
2952
2953 // Deal with some common cases here that work for both fixed and scalable
2954 // vector types.
2955 switch (Opcode) {
2956 case ISD::SPLAT_VECTOR:
2957 UndefElts = V.getOperand(0).isUndef()
2958 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2959 : APInt(DemandedElts.getBitWidth(), 0);
2960 return true;
2961 case ISD::ADD:
2962 case ISD::SUB:
2963 case ISD::AND:
2964 case ISD::XOR:
2965 case ISD::OR: {
2966 APInt UndefLHS, UndefRHS;
2967 SDValue LHS = V.getOperand(0);
2968 SDValue RHS = V.getOperand(1);
2969 // Only recognize splats with the same demanded undef elements for both
2970 // operands, otherwise we might fail to handle binop-specific undef
2971 // handling.
2972 // e.g. (and undef, 0) -> 0 etc.
2973 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2974 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2975 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2976 UndefElts = UndefLHS | UndefRHS;
2977 return true;
2978 }
2979 return false;
2980 }
2981 case ISD::ABS:
2982 case ISD::TRUNCATE:
2983 case ISD::SIGN_EXTEND:
2984 case ISD::ZERO_EXTEND:
2985 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2986 default:
2987 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2988 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2989 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2990 Depth);
2991 break;
2992 }
2993
2994 // We don't support other cases than those above for scalable vectors at
2995 // the moment.
2996 if (VT.isScalableVector())
2997 return false;
2998
2999 unsigned NumElts = VT.getVectorNumElements();
3000 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
3001 UndefElts = APInt::getZero(NumElts);
3002
3003 switch (Opcode) {
3004 case ISD::BUILD_VECTOR: {
3005 SDValue Scl;
3006 for (unsigned i = 0; i != NumElts; ++i) {
3007 SDValue Op = V.getOperand(i);
3008 if (Op.isUndef()) {
3009 UndefElts.setBit(i);
3010 continue;
3011 }
3012 if (!DemandedElts[i])
3013 continue;
3014 if (Scl && Scl != Op)
3015 return false;
3016 Scl = Op;
3017 }
3018 return true;
3019 }
3020 case ISD::VECTOR_SHUFFLE: {
3021 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
3022 APInt DemandedLHS = APInt::getZero(NumElts);
3023 APInt DemandedRHS = APInt::getZero(NumElts);
3024 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3025 for (int i = 0; i != (int)NumElts; ++i) {
3026 int M = Mask[i];
3027 if (M < 0) {
3028 UndefElts.setBit(i);
3029 continue;
3030 }
3031 if (!DemandedElts[i])
3032 continue;
3033 if (M < (int)NumElts)
3034 DemandedLHS.setBit(M);
3035 else
3036 DemandedRHS.setBit(M - NumElts);
3037 }
3038
3039 // If we aren't demanding either op, assume there's no splat.
3040 // If we are demanding both ops, assume there's no splat.
3041 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3042 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3043 return false;
3044
3045 // See if the demanded elts of the source op is a splat or we only demand
3046 // one element, which should always be a splat.
3047 // TODO: Handle source ops splats with undefs.
3048 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3049 APInt SrcUndefs;
3050 return (SrcElts.popcount() == 1) ||
3051 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3052 (SrcElts & SrcUndefs).isZero());
3053 };
3054 if (!DemandedLHS.isZero())
3055 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3056 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3057 }
3059 // Offset the demanded elts by the subvector index.
3060 SDValue Src = V.getOperand(0);
3061 // We don't support scalable vectors at the moment.
3062 if (Src.getValueType().isScalableVector())
3063 return false;
3064 uint64_t Idx = V.getConstantOperandVal(1);
3065 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3066 APInt UndefSrcElts;
3067 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3068 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3069 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3070 return true;
3071 }
3072 break;
3073 }
3077 // Widen the demanded elts by the src element count.
3078 SDValue Src = V.getOperand(0);
3079 // We don't support scalable vectors at the moment.
3080 if (Src.getValueType().isScalableVector())
3081 return false;
3082 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3083 APInt UndefSrcElts;
3084 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3085 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3086 UndefElts = UndefSrcElts.trunc(NumElts);
3087 return true;
3088 }
3089 break;
3090 }
3091 case ISD::BITCAST: {
3092 SDValue Src = V.getOperand(0);
3093 EVT SrcVT = Src.getValueType();
3094 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3095 unsigned BitWidth = VT.getScalarSizeInBits();
3096
3097 // Ignore bitcasts from unsupported types.
3098 // TODO: Add fp support?
3099 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3100 break;
3101
3102 // Bitcast 'small element' vector to 'large element' vector.
3103 if ((BitWidth % SrcBitWidth) == 0) {
3104 // See if each sub element is a splat.
3105 unsigned Scale = BitWidth / SrcBitWidth;
3106 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3107 APInt ScaledDemandedElts =
3108 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3109 for (unsigned I = 0; I != Scale; ++I) {
3110 APInt SubUndefElts;
3111 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3112 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3113 SubDemandedElts &= ScaledDemandedElts;
3114 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3115 return false;
3116 // TODO: Add support for merging sub undef elements.
3117 if (!SubUndefElts.isZero())
3118 return false;
3119 }
3120 return true;
3121 }
3122 break;
3123 }
3124 }
3125
3126 return false;
3127}
3128
3129/// Helper wrapper to main isSplatValue function.
3130bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3131 EVT VT = V.getValueType();
3132 assert(VT.isVector() && "Vector type expected");
3133
3134 APInt UndefElts;
3135 // Since the number of lanes in a scalable vector is unknown at compile time,
3136 // we track one bit which is implicitly broadcast to all lanes. This means
3137 // that all lanes in a scalable vector are considered demanded.
3138 APInt DemandedElts
3140 return isSplatValue(V, DemandedElts, UndefElts) &&
3141 (AllowUndefs || !UndefElts);
3142}
3143
3146
3147 EVT VT = V.getValueType();
3148 unsigned Opcode = V.getOpcode();
3149 switch (Opcode) {
3150 default: {
3151 APInt UndefElts;
3152 // Since the number of lanes in a scalable vector is unknown at compile time,
3153 // we track one bit which is implicitly broadcast to all lanes. This means
3154 // that all lanes in a scalable vector are considered demanded.
3155 APInt DemandedElts
3157
3158 if (isSplatValue(V, DemandedElts, UndefElts)) {
3159 if (VT.isScalableVector()) {
3160 // DemandedElts and UndefElts are ignored for scalable vectors, since
3161 // the only supported cases are SPLAT_VECTOR nodes.
3162 SplatIdx = 0;
3163 } else {
3164 // Handle case where all demanded elements are UNDEF.
3165 if (DemandedElts.isSubsetOf(UndefElts)) {
3166 SplatIdx = 0;
3167 return getUNDEF(VT);
3168 }
3169 SplatIdx = (UndefElts & DemandedElts).countr_one();
3170 }
3171 return V;
3172 }
3173 break;
3174 }
3175 case ISD::SPLAT_VECTOR:
3176 SplatIdx = 0;
3177 return V;
3178 case ISD::VECTOR_SHUFFLE: {
3179 assert(!VT.isScalableVector());
3180 // Check if this is a shuffle node doing a splat.
3181 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3182 // getTargetVShiftNode currently struggles without the splat source.
3183 auto *SVN = cast<ShuffleVectorSDNode>(V);
3184 if (!SVN->isSplat())
3185 break;
3186 int Idx = SVN->getSplatIndex();
3187 int NumElts = V.getValueType().getVectorNumElements();
3188 SplatIdx = Idx % NumElts;
3189 return V.getOperand(Idx / NumElts);
3190 }
3191 }
3192
3193 return SDValue();
3194}
3195
3197 int SplatIdx;
3198 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3199 EVT SVT = SrcVector.getValueType().getScalarType();
3200 EVT LegalSVT = SVT;
3201 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3202 if (!SVT.isInteger())
3203 return SDValue();
3204 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3205 if (LegalSVT.bitsLT(SVT))
3206 return SDValue();
3207 }
3208 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3209 }
3210 return SDValue();
3211}
3212
3213std::optional<ConstantRange>
3215 unsigned Depth) const {
3216 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3217 V.getOpcode() == ISD::SRA) &&
3218 "Unknown shift node");
3219 // Shifting more than the bitwidth is not valid.
3220 unsigned BitWidth = V.getScalarValueSizeInBits();
3221
3222 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3223 const APInt &ShAmt = Cst->getAPIntValue();
3224 if (ShAmt.uge(BitWidth))
3225 return std::nullopt;
3226 return ConstantRange(ShAmt);
3227 }
3228
3229 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3230 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3231 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3232 if (!DemandedElts[i])
3233 continue;
3234 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3235 if (!SA) {
3236 MinAmt = MaxAmt = nullptr;
3237 break;
3238 }
3239 const APInt &ShAmt = SA->getAPIntValue();
3240 if (ShAmt.uge(BitWidth))
3241 return std::nullopt;
3242 if (!MinAmt || MinAmt->ugt(ShAmt))
3243 MinAmt = &ShAmt;
3244 if (!MaxAmt || MaxAmt->ult(ShAmt))
3245 MaxAmt = &ShAmt;
3246 }
3247 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3248 "Failed to find matching min/max shift amounts");
3249 if (MinAmt && MaxAmt)
3250 return ConstantRange(*MinAmt, *MaxAmt + 1);
3251 }
3252
3253 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3254 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3255 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3256 if (KnownAmt.getMaxValue().ult(BitWidth))
3257 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3258
3259 return std::nullopt;
3260}
3261
3262std::optional<unsigned>
3264 unsigned Depth) const {
3265 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3266 V.getOpcode() == ISD::SRA) &&
3267 "Unknown shift node");
3268 if (std::optional<ConstantRange> AmtRange =
3269 getValidShiftAmountRange(V, DemandedElts, Depth))
3270 if (const APInt *ShAmt = AmtRange->getSingleElement())
3271 return ShAmt->getZExtValue();
3272 return std::nullopt;
3273}
3274
3275std::optional<unsigned>
3277 EVT VT = V.getValueType();
3278 APInt DemandedElts = VT.isFixedLengthVector()
3280 : APInt(1, 1);
3281 return getValidShiftAmount(V, DemandedElts, Depth);
3282}
3283
3284std::optional<unsigned>
3286 unsigned Depth) const {
3287 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3288 V.getOpcode() == ISD::SRA) &&
3289 "Unknown shift node");
3290 if (std::optional<ConstantRange> AmtRange =
3291 getValidShiftAmountRange(V, DemandedElts, Depth))
3292 return AmtRange->getUnsignedMin().getZExtValue();
3293 return std::nullopt;
3294}
3295
3296std::optional<unsigned>
3298 EVT VT = V.getValueType();
3299 APInt DemandedElts = VT.isFixedLengthVector()
3301 : APInt(1, 1);
3302 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3303}
3304
3305std::optional<unsigned>
3307 unsigned Depth) const {
3308 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3309 V.getOpcode() == ISD::SRA) &&
3310 "Unknown shift node");
3311 if (std::optional<ConstantRange> AmtRange =
3312 getValidShiftAmountRange(V, DemandedElts, Depth))
3313 return AmtRange->getUnsignedMax().getZExtValue();
3314 return std::nullopt;
3315}
3316
3317std::optional<unsigned>
3319 EVT VT = V.getValueType();
3320 APInt DemandedElts = VT.isFixedLengthVector()
3322 : APInt(1, 1);
3323 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3324}
3325
3326/// Determine which bits of Op are known to be either zero or one and return
3327/// them in Known. For vectors, the known bits are those that are shared by
3328/// every vector element.
3330 EVT VT = Op.getValueType();
3331
3332 // Since the number of lanes in a scalable vector is unknown at compile time,
3333 // we track one bit which is implicitly broadcast to all lanes. This means
3334 // that all lanes in a scalable vector are considered demanded.
3335 APInt DemandedElts = VT.isFixedLengthVector()
3337 : APInt(1, 1);
3338 return computeKnownBits(Op, DemandedElts, Depth);
3339}
3340
3341/// Determine which bits of Op are known to be either zero or one and return
3342/// them in Known. The DemandedElts argument allows us to only collect the known
3343/// bits that are shared by the requested vector elements.
3345 unsigned Depth) const {
3346 unsigned BitWidth = Op.getScalarValueSizeInBits();
3347
3348 KnownBits Known(BitWidth); // Don't know anything.
3349
3350 if (auto OptAPInt = Op->bitcastToAPInt()) {
3351 // We know all of the bits for a constant!
3352 return KnownBits::makeConstant(*std::move(OptAPInt));
3353 }
3354
3355 if (Depth >= MaxRecursionDepth)
3356 return Known; // Limit search depth.
3357
3358 KnownBits Known2;
3359 unsigned NumElts = DemandedElts.getBitWidth();
3360 assert((!Op.getValueType().isScalableVector() || NumElts == 1) &&
3361 "DemandedElts for scalable vectors must be 1 to represent all lanes");
3362 assert((!Op.getValueType().isFixedLengthVector() ||
3363 NumElts == Op.getValueType().getVectorNumElements()) &&
3364 "Unexpected vector size");
3365
3366 if (!DemandedElts)
3367 return Known; // No demanded elts, better to assume we don't know anything.
3368
3369 unsigned Opcode = Op.getOpcode();
3370 switch (Opcode) {
3371 case ISD::MERGE_VALUES:
3372 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3373 Depth + 1);
3374 case ISD::SPLAT_VECTOR: {
3375 SDValue SrcOp = Op.getOperand(0);
3376 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3377 "Expected SPLAT_VECTOR implicit truncation");
3378 // Implicitly truncate the bits to match the official semantics of
3379 // SPLAT_VECTOR.
3380 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3381 break;
3382 }
3384 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3385 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3386 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3387 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3388 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3389 }
3390 break;
3391 }
3392 case ISD::STEP_VECTOR: {
3393 const APInt &Step = Op.getConstantOperandAPInt(0);
3394
3395 if (Step.isPowerOf2())
3396 Known.Zero.setLowBits(Step.logBase2());
3397
3399
3400 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3401 break;
3402 const APInt MinNumElts =
3403 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3404
3405 bool Overflow;
3406 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3408 .umul_ov(MinNumElts, Overflow);
3409 if (Overflow)
3410 break;
3411
3412 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3413 if (Overflow)
3414 break;
3415
3416 Known.Zero.setHighBits(MaxValue.countl_zero());
3417 break;
3418 }
3419 case ISD::BUILD_VECTOR:
3420 assert(!Op.getValueType().isScalableVector());
3421 // Collect the known bits that are shared by every demanded vector element.
3422 Known.setAllConflict();
3423 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3424 if (!DemandedElts[i])
3425 continue;
3426
3427 SDValue SrcOp = Op.getOperand(i);
3428 Known2 = computeKnownBits(SrcOp, Depth + 1);
3429
3430 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3431 if (SrcOp.getValueSizeInBits() != BitWidth) {
3432 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3433 "Expected BUILD_VECTOR implicit truncation");
3434 Known2 = Known2.trunc(BitWidth);
3435 }
3436
3437 // Known bits are the values that are shared by every demanded element.
3438 Known = Known.intersectWith(Known2);
3439
3440 // If we don't know any bits, early out.
3441 if (Known.isUnknown())
3442 break;
3443 }
3444 break;
3445 case ISD::VECTOR_COMPRESS: {
3446 SDValue Vec = Op.getOperand(0);
3447 SDValue PassThru = Op.getOperand(2);
3448 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3449 // If we don't know any bits, early out.
3450 if (Known.isUnknown())
3451 break;
3452 Known2 = computeKnownBits(Vec, Depth + 1);
3453 Known = Known.intersectWith(Known2);
3454 break;
3455 }
3456 case ISD::VECTOR_SHUFFLE: {
3457 assert(!Op.getValueType().isScalableVector());
3458 // Collect the known bits that are shared by every vector element referenced
3459 // by the shuffle.
3460 APInt DemandedLHS, DemandedRHS;
3462 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3463 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3464 DemandedLHS, DemandedRHS))
3465 break;
3466
3467 // Known bits are the values that are shared by every demanded element.
3468 Known.setAllConflict();
3469 if (!!DemandedLHS) {
3470 SDValue LHS = Op.getOperand(0);
3471 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3472 Known = Known.intersectWith(Known2);
3473 }
3474 // If we don't know any bits, early out.
3475 if (Known.isUnknown())
3476 break;
3477 if (!!DemandedRHS) {
3478 SDValue RHS = Op.getOperand(1);
3479 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3480 Known = Known.intersectWith(Known2);
3481 }
3482 break;
3483 }
3484 case ISD::VSCALE: {
3486 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3487 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3488 break;
3489 }
3490 case ISD::CONCAT_VECTORS: {
3491 if (Op.getValueType().isScalableVector())
3492 break;
3493 // Split DemandedElts and test each of the demanded subvectors.
3494 Known.setAllConflict();
3495 EVT SubVectorVT = Op.getOperand(0).getValueType();
3496 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3497 unsigned NumSubVectors = Op.getNumOperands();
3498 for (unsigned i = 0; i != NumSubVectors; ++i) {
3499 APInt DemandedSub =
3500 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3501 if (!!DemandedSub) {
3502 SDValue Sub = Op.getOperand(i);
3503 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3504 Known = Known.intersectWith(Known2);
3505 }
3506 // If we don't know any bits, early out.
3507 if (Known.isUnknown())
3508 break;
3509 }
3510 break;
3511 }
3512 case ISD::INSERT_SUBVECTOR: {
3513 if (Op.getValueType().isScalableVector())
3514 break;
3515 // Demand any elements from the subvector and the remainder from the src its
3516 // inserted into.
3517 SDValue Src = Op.getOperand(0);
3518 SDValue Sub = Op.getOperand(1);
3519 uint64_t Idx = Op.getConstantOperandVal(2);
3520 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3521 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3522 APInt DemandedSrcElts = DemandedElts;
3523 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3524
3525 Known.setAllConflict();
3526 if (!!DemandedSubElts) {
3527 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3528 if (Known.isUnknown())
3529 break; // early-out.
3530 }
3531 if (!!DemandedSrcElts) {
3532 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3533 Known = Known.intersectWith(Known2);
3534 }
3535 break;
3536 }
3538 // Offset the demanded elts by the subvector index.
3539 SDValue Src = Op.getOperand(0);
3540
3541 APInt DemandedSrcElts;
3542 if (Src.getValueType().isScalableVector())
3543 DemandedSrcElts = APInt(1, 1); // <=> 'demand all elements'
3544 else {
3545 uint64_t Idx = Op.getConstantOperandVal(1);
3546 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3547 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3548 }
3549 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3550 break;
3551 }
3552 case ISD::SCALAR_TO_VECTOR: {
3553 if (Op.getValueType().isScalableVector())
3554 break;
3555 // We know about scalar_to_vector as much as we know about it source,
3556 // which becomes the first element of otherwise unknown vector.
3557 if (DemandedElts != 1)
3558 break;
3559
3560 SDValue N0 = Op.getOperand(0);
3561 Known = computeKnownBits(N0, Depth + 1);
3562 if (N0.getValueSizeInBits() != BitWidth)
3563 Known = Known.trunc(BitWidth);
3564
3565 break;
3566 }
3567 case ISD::BITCAST: {
3568 if (Op.getValueType().isScalableVector())
3569 break;
3570
3571 SDValue N0 = Op.getOperand(0);
3572 EVT SubVT = N0.getValueType();
3573 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3574
3575 // Ignore bitcasts from unsupported types.
3576 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3577 break;
3578
3579 // Fast handling of 'identity' bitcasts.
3580 if (BitWidth == SubBitWidth) {
3581 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3582 break;
3583 }
3584
3585 bool IsLE = getDataLayout().isLittleEndian();
3586
3587 // Bitcast 'small element' vector to 'large element' scalar/vector.
3588 if ((BitWidth % SubBitWidth) == 0) {
3589 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3590
3591 // Collect known bits for the (larger) output by collecting the known
3592 // bits from each set of sub elements and shift these into place.
3593 // We need to separately call computeKnownBits for each set of
3594 // sub elements as the knownbits for each is likely to be different.
3595 unsigned SubScale = BitWidth / SubBitWidth;
3596 APInt SubDemandedElts(NumElts * SubScale, 0);
3597 for (unsigned i = 0; i != NumElts; ++i)
3598 if (DemandedElts[i])
3599 SubDemandedElts.setBit(i * SubScale);
3600
3601 for (unsigned i = 0; i != SubScale; ++i) {
3602 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3603 Depth + 1);
3604 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3605 Known.insertBits(Known2, SubBitWidth * Shifts);
3606 }
3607 }
3608
3609 // Bitcast 'large element' scalar/vector to 'small element' vector.
3610 if ((SubBitWidth % BitWidth) == 0) {
3611 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3612
3613 // Collect known bits for the (smaller) output by collecting the known
3614 // bits from the overlapping larger input elements and extracting the
3615 // sub sections we actually care about.
3616 unsigned SubScale = SubBitWidth / BitWidth;
3617 APInt SubDemandedElts =
3618 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3619 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3620
3621 Known.setAllConflict();
3622 for (unsigned i = 0; i != NumElts; ++i)
3623 if (DemandedElts[i]) {
3624 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3625 unsigned Offset = (Shifts % SubScale) * BitWidth;
3626 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3627 // If we don't know any bits, early out.
3628 if (Known.isUnknown())
3629 break;
3630 }
3631 }
3632 break;
3633 }
3634 case ISD::AND:
3635 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3636 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3637
3638 Known &= Known2;
3639 break;
3640 case ISD::OR:
3641 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3642 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3643
3644 Known |= Known2;
3645 break;
3646 case ISD::XOR:
3647 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3648 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3649
3650 Known ^= Known2;
3651 break;
3652 case ISD::MUL: {
3653 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3654 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3655 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3656 // TODO: SelfMultiply can be poison, but not undef.
3657 if (SelfMultiply)
3658 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3659 Op.getOperand(0), DemandedElts, false, Depth + 1);
3660 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3661
3662 // If the multiplication is known not to overflow, the product of a number
3663 // with itself is non-negative. Only do this if we didn't already computed
3664 // the opposite value for the sign bit.
3665 if (Op->getFlags().hasNoSignedWrap() &&
3666 Op.getOperand(0) == Op.getOperand(1) &&
3667 !Known.isNegative())
3668 Known.makeNonNegative();
3669 break;
3670 }
3671 case ISD::MULHU: {
3672 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3673 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3674 Known = KnownBits::mulhu(Known, Known2);
3675 break;
3676 }
3677 case ISD::MULHS: {
3678 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3679 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3680 Known = KnownBits::mulhs(Known, Known2);
3681 break;
3682 }
3683 case ISD::ABDU: {
3684 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3685 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3686 Known = KnownBits::abdu(Known, Known2);
3687 break;
3688 }
3689 case ISD::ABDS: {
3690 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3691 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3692 Known = KnownBits::abds(Known, Known2);
3693 unsigned SignBits1 =
3694 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3695 if (SignBits1 == 1)
3696 break;
3697 unsigned SignBits0 =
3698 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3699 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3700 break;
3701 }
3702 case ISD::UMUL_LOHI: {
3703 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3704 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3705 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3706 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3707 if (Op.getResNo() == 0)
3708 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3709 else
3710 Known = KnownBits::mulhu(Known, Known2);
3711 break;
3712 }
3713 case ISD::SMUL_LOHI: {
3714 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3715 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3717 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3718 if (Op.getResNo() == 0)
3719 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3720 else
3721 Known = KnownBits::mulhs(Known, Known2);
3722 break;
3723 }
3724 case ISD::AVGFLOORU: {
3725 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3726 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3727 Known = KnownBits::avgFloorU(Known, Known2);
3728 break;
3729 }
3730 case ISD::AVGCEILU: {
3731 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3732 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3733 Known = KnownBits::avgCeilU(Known, Known2);
3734 break;
3735 }
3736 case ISD::AVGFLOORS: {
3737 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3738 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3739 Known = KnownBits::avgFloorS(Known, Known2);
3740 break;
3741 }
3742 case ISD::AVGCEILS: {
3743 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3744 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3745 Known = KnownBits::avgCeilS(Known, Known2);
3746 break;
3747 }
3748 case ISD::SELECT:
3749 case ISD::VSELECT:
3750 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3751 // If we don't know any bits, early out.
3752 if (Known.isUnknown())
3753 break;
3754 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3755
3756 // Only known if known in both the LHS and RHS.
3757 Known = Known.intersectWith(Known2);
3758 break;
3759 case ISD::SELECT_CC:
3760 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3761 // If we don't know any bits, early out.
3762 if (Known.isUnknown())
3763 break;
3764 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3765
3766 // Only known if known in both the LHS and RHS.
3767 Known = Known.intersectWith(Known2);
3768 break;
3769 case ISD::SMULO:
3770 case ISD::UMULO:
3771 if (Op.getResNo() != 1)
3772 break;
3773 // The boolean result conforms to getBooleanContents.
3774 // If we know the result of a setcc has the top bits zero, use this info.
3775 // We know that we have an integer-based boolean since these operations
3776 // are only available for integer.
3777 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3779 BitWidth > 1)
3780 Known.Zero.setBitsFrom(1);
3781 break;
3782 case ISD::SETCC:
3783 case ISD::SETCCCARRY:
3784 case ISD::STRICT_FSETCC:
3785 case ISD::STRICT_FSETCCS: {
3786 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3787 // If we know the result of a setcc has the top bits zero, use this info.
3788 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3790 BitWidth > 1)
3791 Known.Zero.setBitsFrom(1);
3792 break;
3793 }
3794 case ISD::SHL: {
3795 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3796 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3797
3798 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3799 bool NSW = Op->getFlags().hasNoSignedWrap();
3800
3801 bool ShAmtNonZero = Known2.isNonZero();
3802
3803 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3804
3805 // Minimum shift low bits are known zero.
3806 if (std::optional<unsigned> ShMinAmt =
3807 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3808 Known.Zero.setLowBits(*ShMinAmt);
3809 break;
3810 }
3811 case ISD::SRL:
3812 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3813 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3814 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3815 Op->getFlags().hasExact());
3816
3817 // Minimum shift high bits are known zero.
3818 if (std::optional<unsigned> ShMinAmt =
3819 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3820 Known.Zero.setHighBits(*ShMinAmt);
3821 break;
3822 case ISD::SRA:
3823 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3824 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3825 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3826 Op->getFlags().hasExact());
3827 break;
3828 case ISD::ROTL:
3829 case ISD::ROTR:
3830 if (ConstantSDNode *C =
3831 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3832 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3833
3834 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3835
3836 // Canonicalize to ROTR.
3837 if (Opcode == ISD::ROTL && Amt != 0)
3838 Amt = BitWidth - Amt;
3839
3840 Known.Zero = Known.Zero.rotr(Amt);
3841 Known.One = Known.One.rotr(Amt);
3842 }
3843 break;
3844 case ISD::FSHL:
3845 case ISD::FSHR:
3846 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3847 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3848
3849 // For fshl, 0-shift returns the 1st arg.
3850 // For fshr, 0-shift returns the 2nd arg.
3851 if (Amt == 0) {
3852 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3853 DemandedElts, Depth + 1);
3854 break;
3855 }
3856
3857 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3858 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3859 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3860 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3861 if (Opcode == ISD::FSHL) {
3862 Known <<= Amt;
3863 Known2 >>= BitWidth - Amt;
3864 } else {
3865 Known <<= BitWidth - Amt;
3866 Known2 >>= Amt;
3867 }
3868 Known = Known.unionWith(Known2);
3869 }
3870 break;
3871 case ISD::SHL_PARTS:
3872 case ISD::SRA_PARTS:
3873 case ISD::SRL_PARTS: {
3874 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3875
3876 // Collect lo/hi source values and concatenate.
3877 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3878 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3879 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3880 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3881 Known = Known2.concat(Known);
3882
3883 // Collect shift amount.
3884 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3885
3886 if (Opcode == ISD::SHL_PARTS)
3887 Known = KnownBits::shl(Known, Known2);
3888 else if (Opcode == ISD::SRA_PARTS)
3889 Known = KnownBits::ashr(Known, Known2);
3890 else // if (Opcode == ISD::SRL_PARTS)
3891 Known = KnownBits::lshr(Known, Known2);
3892
3893 // TODO: Minimum shift low/high bits are known zero.
3894
3895 if (Op.getResNo() == 0)
3896 Known = Known.extractBits(LoBits, 0);
3897 else
3898 Known = Known.extractBits(HiBits, LoBits);
3899 break;
3900 }
3902 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3903 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3904 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3905 break;
3906 }
3907 case ISD::CTTZ:
3908 case ISD::CTTZ_ZERO_UNDEF: {
3909 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3910 // If we have a known 1, its position is our upper bound.
3911 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3912 unsigned LowBits = llvm::bit_width(PossibleTZ);
3913 Known.Zero.setBitsFrom(LowBits);
3914 break;
3915 }
3916 case ISD::CTLZ:
3917 case ISD::CTLZ_ZERO_UNDEF: {
3918 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3919 // If we have a known 1, its position is our upper bound.
3920 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3921 unsigned LowBits = llvm::bit_width(PossibleLZ);
3922 Known.Zero.setBitsFrom(LowBits);
3923 break;
3924 }
3925 case ISD::CTLS: {
3926 unsigned MinRedundantSignBits =
3927 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3928 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3930 Known = Range.toKnownBits();
3931 break;
3932 }
3933 case ISD::CTPOP: {
3934 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3935 // If we know some of the bits are zero, they can't be one.
3936 unsigned PossibleOnes = Known2.countMaxPopulation();
3937 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3938 break;
3939 }
3940 case ISD::PARITY: {
3941 // Parity returns 0 everywhere but the LSB.
3942 Known.Zero.setBitsFrom(1);
3943 break;
3944 }
3945 case ISD::CLMUL: {
3946 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3947 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3948 Known = KnownBits::clmul(Known, Known2);
3949 break;
3950 }
3951 case ISD::MGATHER:
3952 case ISD::MLOAD: {
3953 ISD::LoadExtType ETy =
3954 (Opcode == ISD::MGATHER)
3955 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3956 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3957 if (ETy == ISD::ZEXTLOAD) {
3958 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3959 KnownBits Known0(MemVT.getScalarSizeInBits());
3960 return Known0.zext(BitWidth);
3961 }
3962 break;
3963 }
3964 case ISD::LOAD: {
3966 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3967 if (ISD::isNON_EXTLoad(LD) && Cst) {
3968 // Determine any common known bits from the loaded constant pool value.
3969 Type *CstTy = Cst->getType();
3970 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3971 !Op.getValueType().isScalableVector()) {
3972 // If its a vector splat, then we can (quickly) reuse the scalar path.
3973 // NOTE: We assume all elements match and none are UNDEF.
3974 if (CstTy->isVectorTy()) {
3975 if (const Constant *Splat = Cst->getSplatValue()) {
3976 Cst = Splat;
3977 CstTy = Cst->getType();
3978 }
3979 }
3980 // TODO - do we need to handle different bitwidths?
3981 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3982 // Iterate across all vector elements finding common known bits.
3983 Known.setAllConflict();
3984 for (unsigned i = 0; i != NumElts; ++i) {
3985 if (!DemandedElts[i])
3986 continue;
3987 if (Constant *Elt = Cst->getAggregateElement(i)) {
3988 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3989 const APInt &Value = CInt->getValue();
3990 Known.One &= Value;
3991 Known.Zero &= ~Value;
3992 continue;
3993 }
3994 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3995 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3996 Known.One &= Value;
3997 Known.Zero &= ~Value;
3998 continue;
3999 }
4000 }
4001 Known.One.clearAllBits();
4002 Known.Zero.clearAllBits();
4003 break;
4004 }
4005 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
4006 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
4007 Known = KnownBits::makeConstant(CInt->getValue());
4008 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
4009 Known =
4010 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
4011 }
4012 }
4013 }
4014 } else if (Op.getResNo() == 0) {
4015 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
4016 KnownBits KnownScalarMemory(ScalarMemorySize);
4017 if (const MDNode *MD = LD->getRanges())
4018 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4019
4020 // Extend the Known bits from memory to the size of the scalar result.
4021 if (ISD::isZEXTLoad(Op.getNode()))
4022 Known = KnownScalarMemory.zext(BitWidth);
4023 else if (ISD::isSEXTLoad(Op.getNode()))
4024 Known = KnownScalarMemory.sext(BitWidth);
4025 else if (ISD::isEXTLoad(Op.getNode()))
4026 Known = KnownScalarMemory.anyext(BitWidth);
4027 else
4028 Known = KnownScalarMemory;
4029 assert(Known.getBitWidth() == BitWidth);
4030 return Known;
4031 }
4032 break;
4033 }
4035 if (Op.getValueType().isScalableVector())
4036 break;
4037 EVT InVT = Op.getOperand(0).getValueType();
4038 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4039 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4040 Known = Known.zext(BitWidth);
4041 break;
4042 }
4043 case ISD::ZERO_EXTEND: {
4044 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4045 Known = Known.zext(BitWidth);
4046 break;
4047 }
4049 if (Op.getValueType().isScalableVector())
4050 break;
4051 EVT InVT = Op.getOperand(0).getValueType();
4052 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4053 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4054 // If the sign bit is known to be zero or one, then sext will extend
4055 // it to the top bits, else it will just zext.
4056 Known = Known.sext(BitWidth);
4057 break;
4058 }
4059 case ISD::SIGN_EXTEND: {
4060 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4061 // If the sign bit is known to be zero or one, then sext will extend
4062 // it to the top bits, else it will just zext.
4063 Known = Known.sext(BitWidth);
4064 break;
4065 }
4067 if (Op.getValueType().isScalableVector())
4068 break;
4069 EVT InVT = Op.getOperand(0).getValueType();
4070 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4071 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4072 Known = Known.anyext(BitWidth);
4073 break;
4074 }
4075 case ISD::ANY_EXTEND: {
4076 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4077 Known = Known.anyext(BitWidth);
4078 break;
4079 }
4080 case ISD::TRUNCATE: {
4081 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4082 Known = Known.trunc(BitWidth);
4083 break;
4084 }
4085 case ISD::TRUNCATE_SSAT_S: {
4086 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4087 Known = Known.truncSSat(BitWidth);
4088 break;
4089 }
4090 case ISD::TRUNCATE_SSAT_U: {
4091 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4092 Known = Known.truncSSatU(BitWidth);
4093 break;
4094 }
4095 case ISD::TRUNCATE_USAT_U: {
4096 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4097 Known = Known.truncUSat(BitWidth);
4098 break;
4099 }
4100 case ISD::AssertZext: {
4101 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4103 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4104 Known.Zero |= (~InMask);
4105 Known.One &= (~Known.Zero);
4106 break;
4107 }
4108 case ISD::AssertAlign: {
4109 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4110 assert(LogOfAlign != 0);
4111
4112 // TODO: Should use maximum with source
4113 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4114 // well as clearing one bits.
4115 Known.Zero.setLowBits(LogOfAlign);
4116 Known.One.clearLowBits(LogOfAlign);
4117 break;
4118 }
4119 case ISD::AssertNoFPClass: {
4120 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4121
4122 FPClassTest NoFPClass =
4123 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4124 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4125 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4126 // Cannot be negative.
4127 Known.makeNonNegative();
4128 }
4129
4130 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4131 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4132 // Cannot be positive.
4133 Known.makeNegative();
4134 }
4135
4136 break;
4137 }
4138 case ISD::FGETSIGN:
4139 // All bits are zero except the low bit.
4140 Known.Zero.setBitsFrom(1);
4141 break;
4142 case ISD::ADD:
4143 case ISD::SUB: {
4144 SDNodeFlags Flags = Op.getNode()->getFlags();
4145 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4146 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4148 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4149 Flags.hasNoUnsignedWrap(), Known, Known2);
4150 // ADD(X,X) is equivalent to SHL(X,1), the low bit is always zero.
4151 if (Op.getOpcode() == ISD::ADD && Op.getOperand(0) == Op.getOperand(1) &&
4152 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts, false,
4153 Depth + 1))
4154 Known.Zero.setBit(0);
4155 break;
4156 }
4157 case ISD::USUBO:
4158 case ISD::SSUBO:
4159 case ISD::USUBO_CARRY:
4160 case ISD::SSUBO_CARRY:
4161 if (Op.getResNo() == 1) {
4162 // If we know the result of a setcc has the top bits zero, use this info.
4163 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4165 BitWidth > 1)
4166 Known.Zero.setBitsFrom(1);
4167 break;
4168 }
4169 [[fallthrough]];
4170 case ISD::SUBC: {
4171 assert(Op.getResNo() == 0 &&
4172 "We only compute knownbits for the difference here.");
4173
4174 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4175 KnownBits Borrow(1);
4176 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4177 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4178 // Borrow has bit width 1
4179 Borrow = Borrow.trunc(1);
4180 } else {
4181 Borrow.setAllZero();
4182 }
4183
4184 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4185 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4186 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4187 break;
4188 }
4189 case ISD::UADDO:
4190 case ISD::SADDO:
4191 case ISD::UADDO_CARRY:
4192 case ISD::SADDO_CARRY:
4193 if (Op.getResNo() == 1) {
4194 // If we know the result of a setcc has the top bits zero, use this info.
4195 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4197 BitWidth > 1)
4198 Known.Zero.setBitsFrom(1);
4199 break;
4200 }
4201 [[fallthrough]];
4202 case ISD::ADDC:
4203 case ISD::ADDE: {
4204 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4205
4206 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4207 KnownBits Carry(1);
4208 if (Opcode == ISD::ADDE)
4209 // Can't track carry from glue, set carry to unknown.
4210 Carry.resetAll();
4211 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4212 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4213 // Carry has bit width 1
4214 Carry = Carry.trunc(1);
4215 } else {
4216 Carry.setAllZero();
4217 }
4218
4219 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4220 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4221 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4222 break;
4223 }
4224 case ISD::UDIV: {
4225 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4226 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4227 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4228 break;
4229 }
4230 case ISD::SDIV: {
4231 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4232 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4233 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4234 break;
4235 }
4236 case ISD::SREM: {
4237 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4238 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4239 Known = KnownBits::srem(Known, Known2);
4240 break;
4241 }
4242 case ISD::UREM: {
4243 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4244 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4245 Known = KnownBits::urem(Known, Known2);
4246 break;
4247 }
4248 case ISD::EXTRACT_ELEMENT: {
4249 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4250 const unsigned Index = Op.getConstantOperandVal(1);
4251 const unsigned EltBitWidth = Op.getValueSizeInBits();
4252
4253 // Remove low part of known bits mask
4254 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4255 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4256
4257 // Remove high part of known bit mask
4258 Known = Known.trunc(EltBitWidth);
4259 break;
4260 }
4262 SDValue InVec = Op.getOperand(0);
4263 SDValue EltNo = Op.getOperand(1);
4264 EVT VecVT = InVec.getValueType();
4265 // computeKnownBits not yet implemented for scalable vectors.
4266 if (VecVT.isScalableVector())
4267 break;
4268 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4269 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4270
4271 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4272 // anything about the extended bits.
4273 if (BitWidth > EltBitWidth)
4274 Known = Known.trunc(EltBitWidth);
4275
4276 // If we know the element index, just demand that vector element, else for
4277 // an unknown element index, ignore DemandedElts and demand them all.
4278 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4279 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4280 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4281 DemandedSrcElts =
4282 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4283
4284 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4285 if (BitWidth > EltBitWidth)
4286 Known = Known.anyext(BitWidth);
4287 break;
4288 }
4290 if (Op.getValueType().isScalableVector())
4291 break;
4292
4293 // If we know the element index, split the demand between the
4294 // source vector and the inserted element, otherwise assume we need
4295 // the original demanded vector elements and the value.
4296 SDValue InVec = Op.getOperand(0);
4297 SDValue InVal = Op.getOperand(1);
4298 SDValue EltNo = Op.getOperand(2);
4299 bool DemandedVal = true;
4300 APInt DemandedVecElts = DemandedElts;
4301 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4302 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4303 unsigned EltIdx = CEltNo->getZExtValue();
4304 DemandedVal = !!DemandedElts[EltIdx];
4305 DemandedVecElts.clearBit(EltIdx);
4306 }
4307 Known.setAllConflict();
4308 if (DemandedVal) {
4309 Known2 = computeKnownBits(InVal, Depth + 1);
4310 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4311 }
4312 if (!!DemandedVecElts) {
4313 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4314 Known = Known.intersectWith(Known2);
4315 }
4316 break;
4317 }
4318 case ISD::BITREVERSE: {
4319 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4320 Known = Known2.reverseBits();
4321 break;
4322 }
4323 case ISD::BSWAP: {
4324 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4325 Known = Known2.byteSwap();
4326 break;
4327 }
4328 case ISD::ABS: {
4329 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4330 Known = Known2.abs();
4331 Known.Zero.setHighBits(
4332 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4333 break;
4334 }
4335 case ISD::USUBSAT: {
4336 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4337 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4338 Known = KnownBits::usub_sat(Known, Known2);
4339 break;
4340 }
4341 case ISD::UMIN: {
4342 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4343 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4344 Known = KnownBits::umin(Known, Known2);
4345 break;
4346 }
4347 case ISD::UMAX: {
4348 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4349 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4350 Known = KnownBits::umax(Known, Known2);
4351 break;
4352 }
4353 case ISD::SMIN:
4354 case ISD::SMAX: {
4355 // If we have a clamp pattern, we know that the number of sign bits will be
4356 // the minimum of the clamp min/max range.
4357 bool IsMax = (Opcode == ISD::SMAX);
4358 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4359 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4360 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4361 CstHigh =
4362 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4363 if (CstLow && CstHigh) {
4364 if (!IsMax)
4365 std::swap(CstLow, CstHigh);
4366
4367 const APInt &ValueLow = CstLow->getAPIntValue();
4368 const APInt &ValueHigh = CstHigh->getAPIntValue();
4369 if (ValueLow.sle(ValueHigh)) {
4370 unsigned LowSignBits = ValueLow.getNumSignBits();
4371 unsigned HighSignBits = ValueHigh.getNumSignBits();
4372 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4373 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4374 Known.One.setHighBits(MinSignBits);
4375 break;
4376 }
4377 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4378 Known.Zero.setHighBits(MinSignBits);
4379 break;
4380 }
4381 }
4382 }
4383
4384 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4385 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4386 if (IsMax)
4387 Known = KnownBits::smax(Known, Known2);
4388 else
4389 Known = KnownBits::smin(Known, Known2);
4390
4391 // For SMAX, if CstLow is non-negative we know the result will be
4392 // non-negative and thus all sign bits are 0.
4393 // TODO: There's an equivalent of this for smin with negative constant for
4394 // known ones.
4395 if (IsMax && CstLow) {
4396 const APInt &ValueLow = CstLow->getAPIntValue();
4397 if (ValueLow.isNonNegative()) {
4398 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4399 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4400 }
4401 }
4402
4403 break;
4404 }
4405 case ISD::UINT_TO_FP: {
4406 Known.makeNonNegative();
4407 break;
4408 }
4409 case ISD::SINT_TO_FP: {
4410 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4411 if (Known2.isNonNegative())
4412 Known.makeNonNegative();
4413 else if (Known2.isNegative())
4414 Known.makeNegative();
4415 break;
4416 }
4417 case ISD::FP_TO_UINT_SAT: {
4418 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4419 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4421 break;
4422 }
4423 case ISD::ATOMIC_LOAD: {
4424 // If we are looking at the loaded value.
4425 if (Op.getResNo() == 0) {
4426 auto *AT = cast<AtomicSDNode>(Op);
4427 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4428 KnownBits KnownScalarMemory(ScalarMemorySize);
4429 if (const MDNode *MD = AT->getRanges())
4430 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4431
4432 switch (AT->getExtensionType()) {
4433 case ISD::ZEXTLOAD:
4434 Known = KnownScalarMemory.zext(BitWidth);
4435 break;
4436 case ISD::SEXTLOAD:
4437 Known = KnownScalarMemory.sext(BitWidth);
4438 break;
4439 case ISD::EXTLOAD:
4440 switch (TLI->getExtendForAtomicOps()) {
4441 case ISD::ZERO_EXTEND:
4442 Known = KnownScalarMemory.zext(BitWidth);
4443 break;
4444 case ISD::SIGN_EXTEND:
4445 Known = KnownScalarMemory.sext(BitWidth);
4446 break;
4447 default:
4448 Known = KnownScalarMemory.anyext(BitWidth);
4449 break;
4450 }
4451 break;
4452 case ISD::NON_EXTLOAD:
4453 Known = KnownScalarMemory;
4454 break;
4455 }
4456 assert(Known.getBitWidth() == BitWidth);
4457 }
4458 break;
4459 }
4461 if (Op.getResNo() == 1) {
4462 // The boolean result conforms to getBooleanContents.
4463 // If we know the result of a setcc has the top bits zero, use this info.
4464 // We know that we have an integer-based boolean since these operations
4465 // are only available for integer.
4466 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4468 BitWidth > 1)
4469 Known.Zero.setBitsFrom(1);
4470 break;
4471 }
4472 [[fallthrough]];
4474 case ISD::ATOMIC_SWAP:
4485 case ISD::ATOMIC_LOAD_UMAX: {
4486 // If we are looking at the loaded value.
4487 if (Op.getResNo() == 0) {
4488 auto *AT = cast<AtomicSDNode>(Op);
4489 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4490
4491 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4492 Known.Zero.setBitsFrom(MemBits);
4493 }
4494 break;
4495 }
4496 case ISD::FrameIndex:
4498 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4499 Known, getMachineFunction());
4500 break;
4501
4502 default:
4503 if (Opcode < ISD::BUILTIN_OP_END)
4504 break;
4505 [[fallthrough]];
4509 // TODO: Probably okay to remove after audit; here to reduce change size
4510 // in initial enablement patch for scalable vectors
4511 if (Op.getValueType().isScalableVector())
4512 break;
4513
4514 // Allow the target to implement this method for its nodes.
4515 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4516 break;
4517 }
4518
4519 return Known;
4520}
4521
4522/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4535
4538 // X + 0 never overflow
4539 if (isNullConstant(N1))
4540 return OFK_Never;
4541
4542 // If both operands each have at least two sign bits, the addition
4543 // cannot overflow.
4544 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4545 return OFK_Never;
4546
4547 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4548 return OFK_Sometime;
4549}
4550
4553 // X + 0 never overflow
4554 if (isNullConstant(N1))
4555 return OFK_Never;
4556
4557 // mulhi + 1 never overflow
4558 KnownBits N1Known = computeKnownBits(N1);
4559 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4560 N1Known.getMaxValue().ult(2))
4561 return OFK_Never;
4562
4563 KnownBits N0Known = computeKnownBits(N0);
4564 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4565 N0Known.getMaxValue().ult(2))
4566 return OFK_Never;
4567
4568 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4569 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4570 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4571 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4572}
4573
4576 // X - 0 never overflow
4577 if (isNullConstant(N1))
4578 return OFK_Never;
4579
4580 // If both operands each have at least two sign bits, the subtraction
4581 // cannot overflow.
4582 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4583 return OFK_Never;
4584
4585 KnownBits N0Known = computeKnownBits(N0);
4586 KnownBits N1Known = computeKnownBits(N1);
4587 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4588 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4589 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4590}
4591
4594 // X - 0 never overflow
4595 if (isNullConstant(N1))
4596 return OFK_Never;
4597
4598 KnownBits N0Known = computeKnownBits(N0);
4599 KnownBits N1Known = computeKnownBits(N1);
4600 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4601 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4602 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4603}
4604
4607 // X * 0 and X * 1 never overflow.
4608 if (isNullConstant(N1) || isOneConstant(N1))
4609 return OFK_Never;
4610
4611 KnownBits N0Known = computeKnownBits(N0);
4612 KnownBits N1Known = computeKnownBits(N1);
4613 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4614 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4615 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4616}
4617
4620 // X * 0 and X * 1 never overflow.
4621 if (isNullConstant(N1) || isOneConstant(N1))
4622 return OFK_Never;
4623
4624 // Get the size of the result.
4625 unsigned BitWidth = N0.getScalarValueSizeInBits();
4626
4627 // Sum of the sign bits.
4628 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4629
4630 // If we have enough sign bits, then there's no overflow.
4631 if (SignBits > BitWidth + 1)
4632 return OFK_Never;
4633
4634 if (SignBits == BitWidth + 1) {
4635 // The overflow occurs when the true multiplication of the
4636 // the operands is the minimum negative number.
4637 KnownBits N0Known = computeKnownBits(N0);
4638 KnownBits N1Known = computeKnownBits(N1);
4639 // If one of the operands is non-negative, then there's no
4640 // overflow.
4641 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4642 return OFK_Never;
4643 }
4644
4645 return OFK_Sometime;
4646}
4647
4649 unsigned Depth) const {
4650 EVT VT = Val.getValueType();
4651
4652 // Since the number of lanes in a scalable vector is unknown at compile time,
4653 // we track one bit which is implicitly broadcast to all lanes. This means
4654 // that all lanes in a scalable vector are considered demanded.
4655 APInt DemandedElts = VT.isFixedLengthVector()
4657 : APInt(1, 1);
4658
4659 return isKnownToBeAPowerOfTwo(Val, DemandedElts, OrZero, Depth);
4660}
4661
4663 const APInt &DemandedElts,
4664 bool OrZero, unsigned Depth) const {
4665 if (Depth >= MaxRecursionDepth)
4666 return false; // Limit search depth.
4667
4668 EVT OpVT = Val.getValueType();
4669 unsigned BitWidth = OpVT.getScalarSizeInBits();
4670 [[maybe_unused]] unsigned NumElts = DemandedElts.getBitWidth();
4671 assert((!OpVT.isScalableVector() || NumElts == 1) &&
4672 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4673 assert(
4674 (!OpVT.isFixedLengthVector() || NumElts == OpVT.getVectorNumElements()) &&
4675 "Unexpected vector size");
4676
4677 auto IsPowerOfTwoOrZero = [BitWidth, OrZero](const ConstantSDNode *C) {
4678 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
4679 return (OrZero && V.isZero()) || V.isPowerOf2();
4680 };
4681
4682 // Is the constant a known power of 2 or zero?
4683 if (ISD::matchUnaryPredicate(Val, IsPowerOfTwoOrZero))
4684 return true;
4685
4686 switch (Val.getOpcode()) {
4687 case ISD::BUILD_VECTOR:
4688 // Are all operands of a build vector constant powers of two or zero?
4689 if (all_of(enumerate(Val->ops()), [&](auto P) {
4690 auto *C = dyn_cast<ConstantSDNode>(P.value());
4691 return !DemandedElts[P.index()] || (C && IsPowerOfTwoOrZero(C));
4692 }))
4693 return true;
4694 break;
4695
4696 case ISD::SPLAT_VECTOR:
4697 // Is the operand of a splat vector a constant power of two?
4698 if (auto *C = dyn_cast<ConstantSDNode>(Val->getOperand(0)))
4699 if (IsPowerOfTwoOrZero(C))
4700 return true;
4701 break;
4702
4704 SDValue InVec = Val.getOperand(0);
4705 SDValue EltNo = Val.getOperand(1);
4706 EVT VecVT = InVec.getValueType();
4707
4708 // Skip scalable vectors or implicit extensions.
4709 if (VecVT.isScalableVector() ||
4710 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
4711 break;
4712
4713 // If we know the element index, just demand that vector element, else for
4714 // an unknown element index, ignore DemandedElts and demand them all.
4715 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4716 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4717 APInt DemandedSrcElts =
4718 ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts)
4719 ? APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue())
4720 : APInt::getAllOnes(NumSrcElts);
4721 return isKnownToBeAPowerOfTwo(InVec, DemandedSrcElts, OrZero, Depth + 1);
4722 }
4723
4724 case ISD::AND: {
4725 // Looking for `x & -x` pattern:
4726 // If x == 0:
4727 // x & -x -> 0
4728 // If x != 0:
4729 // x & -x -> non-zero pow2
4730 // so if we find the pattern return whether we know `x` is non-zero.
4731 SDValue X;
4732 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4733 return OrZero || isKnownNeverZero(X, DemandedElts, Depth);
4734 break;
4735 }
4736
4737 case ISD::SHL: {
4738 // A left-shift of a constant one will have exactly one bit set because
4739 // shifting the bit off the end is undefined.
4740 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4741 if (C && C->getAPIntValue() == 1)
4742 return true;
4743 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4744 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4745 Depth + 1);
4746 }
4747
4748 case ISD::SRL: {
4749 // A logical right-shift of a constant sign-bit will have exactly
4750 // one bit set.
4751 auto *C = isConstOrConstSplat(Val.getOperand(0), DemandedElts);
4752 if (C && C->getAPIntValue().isSignMask())
4753 return true;
4754 return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
4755 isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4756 Depth + 1);
4757 }
4758
4759 case ISD::ROTL:
4760 case ISD::ROTR:
4761 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4762 Depth + 1);
4763 case ISD::BSWAP:
4764 case ISD::BITREVERSE:
4765 return isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
4766 Depth + 1);
4767
4768 case ISD::SMIN:
4769 case ISD::SMAX:
4770 case ISD::UMIN:
4771 case ISD::UMAX:
4772 return isKnownToBeAPowerOfTwo(Val.getOperand(1), /*OrZero=*/false,
4773 Depth + 1) &&
4774 isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4775 Depth + 1);
4776
4777 case ISD::SELECT:
4778 case ISD::VSELECT:
4779 return isKnownToBeAPowerOfTwo(Val.getOperand(2), DemandedElts, OrZero,
4780 Depth + 1) &&
4781 isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedElts, OrZero,
4782 Depth + 1);
4783
4784 case ISD::ZERO_EXTEND:
4785 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4786 Depth + 1);
4787
4788 case ISD::VSCALE:
4789 // vscale(power-of-two) is a power-of-two
4790 return isKnownToBeAPowerOfTwo(Val.getOperand(0), /*OrZero=*/false,
4791 Depth + 1);
4792
4793 case ISD::VECTOR_SHUFFLE: {
4795 // Demanded elements with undef shuffle mask elements are unknown
4796 // - we cannot guarantee they are a power of two, so return false.
4797 APInt DemandedLHS, DemandedRHS;
4799 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4800 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4801 DemandedLHS, DemandedRHS))
4802 return false;
4803
4804 // All demanded elements from LHS must be known power of two.
4805 if (!!DemandedLHS && !isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedLHS,
4806 OrZero, Depth + 1))
4807 return false;
4808
4809 // All demanded elements from RHS must be known power of two.
4810 if (!!DemandedRHS && !isKnownToBeAPowerOfTwo(Val.getOperand(1), DemandedRHS,
4811 OrZero, Depth + 1))
4812 return false;
4813
4814 return true;
4815 }
4816 }
4817
4818 // More could be done here, though the above checks are enough
4819 // to handle some common cases.
4820 return false;
4821}
4822
4824 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4825 return C1->getValueAPF().getExactLog2Abs() >= 0;
4826
4827 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4828 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4829
4830 return false;
4831}
4832
4834 EVT VT = Op.getValueType();
4835
4836 // Since the number of lanes in a scalable vector is unknown at compile time,
4837 // we track one bit which is implicitly broadcast to all lanes. This means
4838 // that all lanes in a scalable vector are considered demanded.
4839 APInt DemandedElts = VT.isFixedLengthVector()
4841 : APInt(1, 1);
4842 return ComputeNumSignBits(Op, DemandedElts, Depth);
4843}
4844
4845unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4846 unsigned Depth) const {
4847 EVT VT = Op.getValueType();
4848 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4849 unsigned VTBits = VT.getScalarSizeInBits();
4850 unsigned NumElts = DemandedElts.getBitWidth();
4851 unsigned Tmp, Tmp2;
4852 unsigned FirstAnswer = 1;
4853
4854 assert((!VT.isScalableVector() || NumElts == 1) &&
4855 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4856
4857 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4858 const APInt &Val = C->getAPIntValue();
4859 return Val.getNumSignBits();
4860 }
4861
4862 if (Depth >= MaxRecursionDepth)
4863 return 1; // Limit search depth.
4864
4865 if (!DemandedElts)
4866 return 1; // No demanded elts, better to assume we don't know anything.
4867
4868 unsigned Opcode = Op.getOpcode();
4869 switch (Opcode) {
4870 default: break;
4871 case ISD::AssertSext:
4872 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4873 return VTBits-Tmp+1;
4874 case ISD::AssertZext:
4875 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4876 return VTBits-Tmp;
4877 case ISD::FREEZE:
4878 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4879 /*PoisonOnly=*/false))
4880 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4881 break;
4882 case ISD::MERGE_VALUES:
4883 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4884 Depth + 1);
4885 case ISD::SPLAT_VECTOR: {
4886 // Check if the sign bits of source go down as far as the truncated value.
4887 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4888 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4889 if (NumSrcSignBits > (NumSrcBits - VTBits))
4890 return NumSrcSignBits - (NumSrcBits - VTBits);
4891 break;
4892 }
4893 case ISD::BUILD_VECTOR:
4894 assert(!VT.isScalableVector());
4895 Tmp = VTBits;
4896 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4897 if (!DemandedElts[i])
4898 continue;
4899
4900 SDValue SrcOp = Op.getOperand(i);
4901 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4902 // for constant nodes to ensure we only look at the sign bits.
4904 APInt T = C->getAPIntValue().trunc(VTBits);
4905 Tmp2 = T.getNumSignBits();
4906 } else {
4907 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4908
4909 if (SrcOp.getValueSizeInBits() != VTBits) {
4910 assert(SrcOp.getValueSizeInBits() > VTBits &&
4911 "Expected BUILD_VECTOR implicit truncation");
4912 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4913 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4914 }
4915 }
4916 Tmp = std::min(Tmp, Tmp2);
4917 }
4918 return Tmp;
4919
4920 case ISD::VECTOR_COMPRESS: {
4921 SDValue Vec = Op.getOperand(0);
4922 SDValue PassThru = Op.getOperand(2);
4923 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4924 if (Tmp == 1)
4925 return 1;
4926 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4927 Tmp = std::min(Tmp, Tmp2);
4928 return Tmp;
4929 }
4930
4931 case ISD::VECTOR_SHUFFLE: {
4932 // Collect the minimum number of sign bits that are shared by every vector
4933 // element referenced by the shuffle.
4934 APInt DemandedLHS, DemandedRHS;
4936 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4937 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4938 DemandedLHS, DemandedRHS))
4939 return 1;
4940
4941 Tmp = std::numeric_limits<unsigned>::max();
4942 if (!!DemandedLHS)
4943 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4944 if (!!DemandedRHS) {
4945 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4946 Tmp = std::min(Tmp, Tmp2);
4947 }
4948 // If we don't know anything, early out and try computeKnownBits fall-back.
4949 if (Tmp == 1)
4950 break;
4951 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4952 return Tmp;
4953 }
4954
4955 case ISD::BITCAST: {
4956 if (VT.isScalableVector())
4957 break;
4958 SDValue N0 = Op.getOperand(0);
4959 EVT SrcVT = N0.getValueType();
4960 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4961
4962 // Ignore bitcasts from unsupported types..
4963 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4964 break;
4965
4966 // Fast handling of 'identity' bitcasts.
4967 if (VTBits == SrcBits)
4968 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4969
4970 bool IsLE = getDataLayout().isLittleEndian();
4971
4972 // Bitcast 'large element' scalar/vector to 'small element' vector.
4973 if ((SrcBits % VTBits) == 0) {
4974 assert(VT.isVector() && "Expected bitcast to vector");
4975
4976 unsigned Scale = SrcBits / VTBits;
4977 APInt SrcDemandedElts =
4978 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4979
4980 // Fast case - sign splat can be simply split across the small elements.
4981 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4982 if (Tmp == SrcBits)
4983 return VTBits;
4984
4985 // Slow case - determine how far the sign extends into each sub-element.
4986 Tmp2 = VTBits;
4987 for (unsigned i = 0; i != NumElts; ++i)
4988 if (DemandedElts[i]) {
4989 unsigned SubOffset = i % Scale;
4990 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4991 SubOffset = SubOffset * VTBits;
4992 if (Tmp <= SubOffset)
4993 return 1;
4994 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4995 }
4996 return Tmp2;
4997 }
4998 break;
4999 }
5000
5002 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
5003 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5004 return VTBits - Tmp + 1;
5005 case ISD::SIGN_EXTEND:
5006 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
5007 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
5009 // Max of the input and what this extends.
5010 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
5011 Tmp = VTBits-Tmp+1;
5012 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5013 return std::max(Tmp, Tmp2);
5015 if (VT.isScalableVector())
5016 break;
5017 SDValue Src = Op.getOperand(0);
5018 EVT SrcVT = Src.getValueType();
5019 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
5020 Tmp = VTBits - SrcVT.getScalarSizeInBits();
5021 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
5022 }
5023 case ISD::SRA:
5024 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5025 // SRA X, C -> adds C sign bits.
5026 if (std::optional<unsigned> ShAmt =
5027 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
5028 Tmp = std::min(Tmp + *ShAmt, VTBits);
5029 return Tmp;
5030 case ISD::SHL:
5031 if (std::optional<ConstantRange> ShAmtRange =
5032 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
5033 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
5034 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
5035 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
5036 // shifted out, then we can compute the number of sign bits for the
5037 // operand being extended. A future improvement could be to pass along the
5038 // "shifted left by" information in the recursive calls to
5039 // ComputeKnownSignBits. Allowing us to handle this more generically.
5040 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
5041 SDValue Ext = Op.getOperand(0);
5042 EVT ExtVT = Ext.getValueType();
5043 SDValue Extendee = Ext.getOperand(0);
5044 EVT ExtendeeVT = Extendee.getValueType();
5045 unsigned SizeDifference =
5046 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
5047 if (SizeDifference <= MinShAmt) {
5048 Tmp = SizeDifference +
5049 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
5050 if (MaxShAmt < Tmp)
5051 return Tmp - MaxShAmt;
5052 }
5053 }
5054 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
5055 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5056 if (MaxShAmt < Tmp)
5057 return Tmp - MaxShAmt;
5058 }
5059 break;
5060 case ISD::AND:
5061 case ISD::OR:
5062 case ISD::XOR: // NOT is handled here.
5063 // Logical binary ops preserve the number of sign bits at the worst.
5064 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
5065 if (Tmp != 1) {
5066 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5067 FirstAnswer = std::min(Tmp, Tmp2);
5068 // We computed what we know about the sign bits as our first
5069 // answer. Now proceed to the generic code that uses
5070 // computeKnownBits, and pick whichever answer is better.
5071 }
5072 break;
5073
5074 case ISD::SELECT:
5075 case ISD::VSELECT:
5076 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
5077 if (Tmp == 1) return 1; // Early out.
5078 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5079 return std::min(Tmp, Tmp2);
5080 case ISD::SELECT_CC:
5081 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
5082 if (Tmp == 1) return 1; // Early out.
5083 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
5084 return std::min(Tmp, Tmp2);
5085
5086 case ISD::SMIN:
5087 case ISD::SMAX: {
5088 // If we have a clamp pattern, we know that the number of sign bits will be
5089 // the minimum of the clamp min/max range.
5090 bool IsMax = (Opcode == ISD::SMAX);
5091 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
5092 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
5093 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
5094 CstHigh =
5095 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
5096 if (CstLow && CstHigh) {
5097 if (!IsMax)
5098 std::swap(CstLow, CstHigh);
5099 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
5100 Tmp = CstLow->getAPIntValue().getNumSignBits();
5101 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
5102 return std::min(Tmp, Tmp2);
5103 }
5104 }
5105
5106 // Fallback - just get the minimum number of sign bits of the operands.
5107 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5108 if (Tmp == 1)
5109 return 1; // Early out.
5110 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5111 return std::min(Tmp, Tmp2);
5112 }
5113 case ISD::UMIN:
5114 case ISD::UMAX:
5115 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5116 if (Tmp == 1)
5117 return 1; // Early out.
5118 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5119 return std::min(Tmp, Tmp2);
5120 case ISD::SSUBO_CARRY:
5121 case ISD::USUBO_CARRY:
5122 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5123 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5124 return VTBits;
5125 [[fallthrough]];
5126 case ISD::SADDO:
5127 case ISD::UADDO:
5128 case ISD::SADDO_CARRY:
5129 case ISD::UADDO_CARRY:
5130 case ISD::SSUBO:
5131 case ISD::USUBO:
5132 case ISD::SMULO:
5133 case ISD::UMULO:
5134 if (Op.getResNo() != 1)
5135 break;
5136 // The boolean result conforms to getBooleanContents. Fall through.
5137 // If setcc returns 0/-1, all bits are sign bits.
5138 // We know that we have an integer-based boolean since these operations
5139 // are only available for integer.
5140 if (TLI->getBooleanContents(VT.isVector(), false) ==
5142 return VTBits;
5143 break;
5144 case ISD::SETCC:
5145 case ISD::SETCCCARRY:
5146 case ISD::STRICT_FSETCC:
5147 case ISD::STRICT_FSETCCS: {
5148 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5149 // If setcc returns 0/-1, all bits are sign bits.
5150 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5152 return VTBits;
5153 break;
5154 }
5155 case ISD::ROTL:
5156 case ISD::ROTR:
5157 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5158
5159 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5160 if (Tmp == VTBits)
5161 return VTBits;
5162
5163 if (ConstantSDNode *C =
5164 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5165 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5166
5167 // Handle rotate right by N like a rotate left by 32-N.
5168 if (Opcode == ISD::ROTR)
5169 RotAmt = (VTBits - RotAmt) % VTBits;
5170
5171 // If we aren't rotating out all of the known-in sign bits, return the
5172 // number that are left. This handles rotl(sext(x), 1) for example.
5173 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5174 }
5175 break;
5176 case ISD::ADD:
5177 case ISD::ADDC:
5178 // TODO: Move Operand 1 check before Operand 0 check
5179 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5180 if (Tmp == 1) return 1; // Early out.
5181
5182 // Special case decrementing a value (ADD X, -1):
5183 if (ConstantSDNode *CRHS =
5184 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5185 if (CRHS->isAllOnes()) {
5186 KnownBits Known =
5187 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5188
5189 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5190 // sign bits set.
5191 if ((Known.Zero | 1).isAllOnes())
5192 return VTBits;
5193
5194 // If we are subtracting one from a positive number, there is no carry
5195 // out of the result.
5196 if (Known.isNonNegative())
5197 return Tmp;
5198 }
5199
5200 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5201 if (Tmp2 == 1) return 1; // Early out.
5202
5203 // Add can have at most one carry bit. Thus we know that the output
5204 // is, at worst, one more bit than the inputs.
5205 return std::min(Tmp, Tmp2) - 1;
5206 case ISD::SUB:
5207 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5208 if (Tmp2 == 1) return 1; // Early out.
5209
5210 // Handle NEG.
5211 if (ConstantSDNode *CLHS =
5212 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5213 if (CLHS->isZero()) {
5214 KnownBits Known =
5215 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5216 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5217 // sign bits set.
5218 if ((Known.Zero | 1).isAllOnes())
5219 return VTBits;
5220
5221 // If the input is known to be positive (the sign bit is known clear),
5222 // the output of the NEG has the same number of sign bits as the input.
5223 if (Known.isNonNegative())
5224 return Tmp2;
5225
5226 // Otherwise, we treat this like a SUB.
5227 }
5228
5229 // Sub can have at most one carry bit. Thus we know that the output
5230 // is, at worst, one more bit than the inputs.
5231 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5232 if (Tmp == 1) return 1; // Early out.
5233 return std::min(Tmp, Tmp2) - 1;
5234 case ISD::MUL: {
5235 // The output of the Mul can be at most twice the valid bits in the inputs.
5236 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5237 if (SignBitsOp0 == 1)
5238 break;
5239 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5240 if (SignBitsOp1 == 1)
5241 break;
5242 unsigned OutValidBits =
5243 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5244 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5245 }
5246 case ISD::AVGCEILS:
5247 case ISD::AVGFLOORS:
5248 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5249 if (Tmp == 1)
5250 return 1; // Early out.
5251 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5252 return std::min(Tmp, Tmp2);
5253 case ISD::SREM:
5254 // The sign bit is the LHS's sign bit, except when the result of the
5255 // remainder is zero. The magnitude of the result should be less than or
5256 // equal to the magnitude of the LHS. Therefore, the result should have
5257 // at least as many sign bits as the left hand side.
5258 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5259 case ISD::TRUNCATE: {
5260 // Check if the sign bits of source go down as far as the truncated value.
5261 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5262 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5263 if (NumSrcSignBits > (NumSrcBits - VTBits))
5264 return NumSrcSignBits - (NumSrcBits - VTBits);
5265 break;
5266 }
5267 case ISD::EXTRACT_ELEMENT: {
5268 if (VT.isScalableVector())
5269 break;
5270 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5271 const int BitWidth = Op.getValueSizeInBits();
5272 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5273
5274 // Get reverse index (starting from 1), Op1 value indexes elements from
5275 // little end. Sign starts at big end.
5276 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5277
5278 // If the sign portion ends in our element the subtraction gives correct
5279 // result. Otherwise it gives either negative or > bitwidth result
5280 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5281 }
5283 if (VT.isScalableVector())
5284 break;
5285 // If we know the element index, split the demand between the
5286 // source vector and the inserted element, otherwise assume we need
5287 // the original demanded vector elements and the value.
5288 SDValue InVec = Op.getOperand(0);
5289 SDValue InVal = Op.getOperand(1);
5290 SDValue EltNo = Op.getOperand(2);
5291 bool DemandedVal = true;
5292 APInt DemandedVecElts = DemandedElts;
5293 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5294 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5295 unsigned EltIdx = CEltNo->getZExtValue();
5296 DemandedVal = !!DemandedElts[EltIdx];
5297 DemandedVecElts.clearBit(EltIdx);
5298 }
5299 Tmp = std::numeric_limits<unsigned>::max();
5300 if (DemandedVal) {
5301 // TODO - handle implicit truncation of inserted elements.
5302 if (InVal.getScalarValueSizeInBits() != VTBits)
5303 break;
5304 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5305 Tmp = std::min(Tmp, Tmp2);
5306 }
5307 if (!!DemandedVecElts) {
5308 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5309 Tmp = std::min(Tmp, Tmp2);
5310 }
5311 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5312 return Tmp;
5313 }
5315 SDValue InVec = Op.getOperand(0);
5316 SDValue EltNo = Op.getOperand(1);
5317 EVT VecVT = InVec.getValueType();
5318 // ComputeNumSignBits not yet implemented for scalable vectors.
5319 if (VecVT.isScalableVector())
5320 break;
5321 const unsigned BitWidth = Op.getValueSizeInBits();
5322 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5323 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5324
5325 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5326 // anything about sign bits. But if the sizes match we can derive knowledge
5327 // about sign bits from the vector operand.
5328 if (BitWidth != EltBitWidth)
5329 break;
5330
5331 // If we know the element index, just demand that vector element, else for
5332 // an unknown element index, ignore DemandedElts and demand them all.
5333 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5334 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5335 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5336 DemandedSrcElts =
5337 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5338
5339 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5340 }
5342 // Offset the demanded elts by the subvector index.
5343 SDValue Src = Op.getOperand(0);
5344
5345 APInt DemandedSrcElts;
5346 if (Src.getValueType().isScalableVector())
5347 DemandedSrcElts = APInt(1, 1);
5348 else {
5349 uint64_t Idx = Op.getConstantOperandVal(1);
5350 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5351 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5352 }
5353 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5354 }
5355 case ISD::CONCAT_VECTORS: {
5356 if (VT.isScalableVector())
5357 break;
5358 // Determine the minimum number of sign bits across all demanded
5359 // elts of the input vectors. Early out if the result is already 1.
5360 Tmp = std::numeric_limits<unsigned>::max();
5361 EVT SubVectorVT = Op.getOperand(0).getValueType();
5362 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5363 unsigned NumSubVectors = Op.getNumOperands();
5364 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5365 APInt DemandedSub =
5366 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5367 if (!DemandedSub)
5368 continue;
5369 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5370 Tmp = std::min(Tmp, Tmp2);
5371 }
5372 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5373 return Tmp;
5374 }
5375 case ISD::INSERT_SUBVECTOR: {
5376 if (VT.isScalableVector())
5377 break;
5378 // Demand any elements from the subvector and the remainder from the src its
5379 // inserted into.
5380 SDValue Src = Op.getOperand(0);
5381 SDValue Sub = Op.getOperand(1);
5382 uint64_t Idx = Op.getConstantOperandVal(2);
5383 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5384 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5385 APInt DemandedSrcElts = DemandedElts;
5386 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5387
5388 Tmp = std::numeric_limits<unsigned>::max();
5389 if (!!DemandedSubElts) {
5390 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5391 if (Tmp == 1)
5392 return 1; // early-out
5393 }
5394 if (!!DemandedSrcElts) {
5395 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5396 Tmp = std::min(Tmp, Tmp2);
5397 }
5398 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5399 return Tmp;
5400 }
5401 case ISD::LOAD: {
5402 // If we are looking at the loaded value of the SDNode.
5403 if (Op.getResNo() != 0)
5404 break;
5405
5407 if (const MDNode *Ranges = LD->getRanges()) {
5408 if (DemandedElts != 1)
5409 break;
5410
5412 if (VTBits > CR.getBitWidth()) {
5413 switch (LD->getExtensionType()) {
5414 case ISD::SEXTLOAD:
5415 CR = CR.signExtend(VTBits);
5416 break;
5417 case ISD::ZEXTLOAD:
5418 CR = CR.zeroExtend(VTBits);
5419 break;
5420 default:
5421 break;
5422 }
5423 }
5424
5425 if (VTBits != CR.getBitWidth())
5426 break;
5427 return std::min(CR.getSignedMin().getNumSignBits(),
5429 }
5430
5431 unsigned ExtType = LD->getExtensionType();
5432 switch (ExtType) {
5433 default:
5434 break;
5435 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5436 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5437 return VTBits - Tmp + 1;
5438 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5439 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5440 return VTBits - Tmp;
5441 case ISD::NON_EXTLOAD:
5442 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5443 // We only need to handle vectors - computeKnownBits should handle
5444 // scalar cases.
5445 Type *CstTy = Cst->getType();
5446 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5447 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5448 VTBits == CstTy->getScalarSizeInBits()) {
5449 Tmp = VTBits;
5450 for (unsigned i = 0; i != NumElts; ++i) {
5451 if (!DemandedElts[i])
5452 continue;
5453 if (Constant *Elt = Cst->getAggregateElement(i)) {
5454 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5455 const APInt &Value = CInt->getValue();
5456 Tmp = std::min(Tmp, Value.getNumSignBits());
5457 continue;
5458 }
5459 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5460 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5461 Tmp = std::min(Tmp, Value.getNumSignBits());
5462 continue;
5463 }
5464 }
5465 // Unknown type. Conservatively assume no bits match sign bit.
5466 return 1;
5467 }
5468 return Tmp;
5469 }
5470 }
5471 break;
5472 }
5473
5474 break;
5475 }
5478 case ISD::ATOMIC_SWAP:
5490 case ISD::ATOMIC_LOAD: {
5491 auto *AT = cast<AtomicSDNode>(Op);
5492 // If we are looking at the loaded value.
5493 if (Op.getResNo() == 0) {
5494 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5495 if (Tmp == VTBits)
5496 return 1; // early-out
5497
5498 // For atomic_load, prefer to use the extension type.
5499 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5500 switch (AT->getExtensionType()) {
5501 default:
5502 break;
5503 case ISD::SEXTLOAD:
5504 return VTBits - Tmp + 1;
5505 case ISD::ZEXTLOAD:
5506 return VTBits - Tmp;
5507 }
5508 }
5509
5510 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5511 return VTBits - Tmp + 1;
5512 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5513 return VTBits - Tmp;
5514 }
5515 break;
5516 }
5517 }
5518
5519 // Allow the target to implement this method for its nodes.
5520 if (Opcode >= ISD::BUILTIN_OP_END ||
5521 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5522 Opcode == ISD::INTRINSIC_W_CHAIN ||
5523 Opcode == ISD::INTRINSIC_VOID) {
5524 // TODO: This can probably be removed once target code is audited. This
5525 // is here purely to reduce patch size and review complexity.
5526 if (!VT.isScalableVector()) {
5527 unsigned NumBits =
5528 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5529 if (NumBits > 1)
5530 FirstAnswer = std::max(FirstAnswer, NumBits);
5531 }
5532 }
5533
5534 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5535 // use this information.
5536 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5537 return std::max(FirstAnswer, Known.countMinSignBits());
5538}
5539
5541 unsigned Depth) const {
5542 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5543 return Op.getScalarValueSizeInBits() - SignBits + 1;
5544}
5545
5547 const APInt &DemandedElts,
5548 unsigned Depth) const {
5549 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5550 return Op.getScalarValueSizeInBits() - SignBits + 1;
5551}
5552
5554 unsigned Depth) const {
5555 // Early out for FREEZE.
5556 if (Op.getOpcode() == ISD::FREEZE)
5557 return true;
5558
5559 EVT VT = Op.getValueType();
5560 APInt DemandedElts = VT.isFixedLengthVector()
5562 : APInt(1, 1);
5563 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5564}
5565
5567 const APInt &DemandedElts,
5568 bool PoisonOnly,
5569 unsigned Depth) const {
5570 unsigned Opcode = Op.getOpcode();
5571
5572 // Early out for FREEZE.
5573 if (Opcode == ISD::FREEZE)
5574 return true;
5575
5576 if (Depth >= MaxRecursionDepth)
5577 return false; // Limit search depth.
5578
5579 if (isIntOrFPConstant(Op))
5580 return true;
5581
5582 switch (Opcode) {
5583 case ISD::CONDCODE:
5584 case ISD::VALUETYPE:
5585 case ISD::FrameIndex:
5587 case ISD::CopyFromReg:
5588 return true;
5589
5590 case ISD::POISON:
5591 return false;
5592
5593 case ISD::UNDEF:
5594 return PoisonOnly;
5595
5596 case ISD::BUILD_VECTOR:
5597 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5598 // this shouldn't affect the result.
5599 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5600 if (!DemandedElts[i])
5601 continue;
5603 Depth + 1))
5604 return false;
5605 }
5606 return true;
5607
5609 SDValue Src = Op.getOperand(0);
5610 if (Src.getValueType().isScalableVector())
5611 break;
5612 uint64_t Idx = Op.getConstantOperandVal(1);
5613 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5614 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5615 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5616 Depth + 1);
5617 }
5618
5619 case ISD::INSERT_SUBVECTOR: {
5620 if (Op.getValueType().isScalableVector())
5621 break;
5622 SDValue Src = Op.getOperand(0);
5623 SDValue Sub = Op.getOperand(1);
5624 uint64_t Idx = Op.getConstantOperandVal(2);
5625 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5626 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5627 APInt DemandedSrcElts = DemandedElts;
5628 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5629
5630 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5631 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5632 return false;
5633 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5634 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5635 return false;
5636 return true;
5637 }
5638
5640 SDValue Src = Op.getOperand(0);
5641 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5642 EVT SrcVT = Src.getValueType();
5643 if (SrcVT.isFixedLengthVector() && IndexC &&
5644 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5645 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5646 IndexC->getZExtValue());
5647 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5648 Depth + 1);
5649 }
5650 break;
5651 }
5652
5654 SDValue InVec = Op.getOperand(0);
5655 SDValue InVal = Op.getOperand(1);
5656 SDValue EltNo = Op.getOperand(2);
5657 EVT VT = InVec.getValueType();
5658 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5659 if (IndexC && VT.isFixedLengthVector() &&
5660 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5661 if (DemandedElts[IndexC->getZExtValue()] &&
5663 return false;
5664 APInt InVecDemandedElts = DemandedElts;
5665 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5666 if (!!InVecDemandedElts &&
5668 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5669 InVecDemandedElts, PoisonOnly, Depth + 1))
5670 return false;
5671 return true;
5672 }
5673 break;
5674 }
5675
5677 // Check upper (known undef) elements.
5678 if (DemandedElts.ugt(1) && !PoisonOnly)
5679 return false;
5680 // Check element zero.
5681 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5682 Op.getOperand(0), PoisonOnly, Depth + 1))
5683 return false;
5684 return true;
5685
5686 case ISD::SPLAT_VECTOR:
5687 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5688 Depth + 1);
5689
5690 case ISD::VECTOR_SHUFFLE: {
5691 APInt DemandedLHS, DemandedRHS;
5692 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5693 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5694 DemandedElts, DemandedLHS, DemandedRHS,
5695 /*AllowUndefElts=*/false))
5696 return false;
5697 if (!DemandedLHS.isZero() &&
5698 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5699 PoisonOnly, Depth + 1))
5700 return false;
5701 if (!DemandedRHS.isZero() &&
5702 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5703 PoisonOnly, Depth + 1))
5704 return false;
5705 return true;
5706 }
5707
5708 case ISD::SHL:
5709 case ISD::SRL:
5710 case ISD::SRA:
5711 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5712 // enough to check operand 0 if Op can't create undef/poison.
5713 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5714 /*ConsiderFlags*/ true, Depth) &&
5715 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5716 PoisonOnly, Depth + 1);
5717
5718 case ISD::BSWAP:
5719 case ISD::CTPOP:
5720 case ISD::BITREVERSE:
5721 case ISD::AND:
5722 case ISD::OR:
5723 case ISD::XOR:
5724 case ISD::ADD:
5725 case ISD::SUB:
5726 case ISD::MUL:
5727 case ISD::SADDSAT:
5728 case ISD::UADDSAT:
5729 case ISD::SSUBSAT:
5730 case ISD::USUBSAT:
5731 case ISD::SSHLSAT:
5732 case ISD::USHLSAT:
5733 case ISD::SMIN:
5734 case ISD::SMAX:
5735 case ISD::UMIN:
5736 case ISD::UMAX:
5737 case ISD::ZERO_EXTEND:
5738 case ISD::SIGN_EXTEND:
5739 case ISD::ANY_EXTEND:
5740 case ISD::TRUNCATE:
5741 case ISD::VSELECT: {
5742 // If Op can't create undef/poison and none of its operands are undef/poison
5743 // then Op is never undef/poison. A difference from the more common check
5744 // below, outside the switch, is that we handle elementwise operations for
5745 // which the DemandedElts mask is valid for all operands here.
5746 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5747 /*ConsiderFlags*/ true, Depth) &&
5748 all_of(Op->ops(), [&](SDValue V) {
5749 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5750 PoisonOnly, Depth + 1);
5751 });
5752 }
5753
5754 // TODO: Search for noundef attributes from library functions.
5755
5756 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5757
5758 default:
5759 // Allow the target to implement this method for its nodes.
5760 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5761 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5762 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5763 Op, DemandedElts, *this, PoisonOnly, Depth);
5764 break;
5765 }
5766
5767 // If Op can't create undef/poison and none of its operands are undef/poison
5768 // then Op is never undef/poison.
5769 // NOTE: TargetNodes can handle this in themselves in
5770 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5771 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5772 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5773 Depth) &&
5774 all_of(Op->ops(), [&](SDValue V) {
5775 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5776 });
5777}
5778
5780 bool ConsiderFlags,
5781 unsigned Depth) const {
5782 EVT VT = Op.getValueType();
5783 APInt DemandedElts = VT.isFixedLengthVector()
5785 : APInt(1, 1);
5786 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5787 Depth);
5788}
5789
5791 bool PoisonOnly, bool ConsiderFlags,
5792 unsigned Depth) const {
5793 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5794 return true;
5795
5796 unsigned Opcode = Op.getOpcode();
5797 switch (Opcode) {
5798 case ISD::AssertSext:
5799 case ISD::AssertZext:
5800 case ISD::AssertAlign:
5802 // Assertion nodes can create poison if the assertion fails.
5803 return true;
5804
5805 case ISD::FREEZE:
5809 case ISD::SADDSAT:
5810 case ISD::UADDSAT:
5811 case ISD::SSUBSAT:
5812 case ISD::USUBSAT:
5813 case ISD::MULHU:
5814 case ISD::MULHS:
5815 case ISD::AVGFLOORS:
5816 case ISD::AVGFLOORU:
5817 case ISD::AVGCEILS:
5818 case ISD::AVGCEILU:
5819 case ISD::ABDU:
5820 case ISD::ABDS:
5821 case ISD::SMIN:
5822 case ISD::SMAX:
5823 case ISD::SCMP:
5824 case ISD::UMIN:
5825 case ISD::UMAX:
5826 case ISD::UCMP:
5827 case ISD::AND:
5828 case ISD::XOR:
5829 case ISD::ROTL:
5830 case ISD::ROTR:
5831 case ISD::FSHL:
5832 case ISD::FSHR:
5833 case ISD::BSWAP:
5834 case ISD::CTTZ:
5835 case ISD::CTLZ:
5836 case ISD::CTLS:
5837 case ISD::CTPOP:
5838 case ISD::BITREVERSE:
5839 case ISD::PARITY:
5840 case ISD::SIGN_EXTEND:
5841 case ISD::TRUNCATE:
5845 case ISD::BITCAST:
5846 case ISD::BUILD_VECTOR:
5847 case ISD::BUILD_PAIR:
5848 case ISD::SPLAT_VECTOR:
5849 case ISD::FABS:
5850 return false;
5851
5852 case ISD::ABS:
5853 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5854 // Different to Intrinsic::abs.
5855 return false;
5856
5857 case ISD::ADDC:
5858 case ISD::SUBC:
5859 case ISD::ADDE:
5860 case ISD::SUBE:
5861 case ISD::SADDO:
5862 case ISD::SSUBO:
5863 case ISD::SMULO:
5864 case ISD::SADDO_CARRY:
5865 case ISD::SSUBO_CARRY:
5866 case ISD::UADDO:
5867 case ISD::USUBO:
5868 case ISD::UMULO:
5869 case ISD::UADDO_CARRY:
5870 case ISD::USUBO_CARRY:
5871 // No poison on result or overflow flags.
5872 return false;
5873
5874 case ISD::SELECT_CC:
5875 case ISD::SETCC: {
5876 // Integer setcc cannot create undef or poison.
5877 if (Op.getOperand(0).getValueType().isInteger())
5878 return false;
5879
5880 // FP compares are more complicated. They can create poison for nan/infinity
5881 // based on options and flags. The options and flags also cause special
5882 // nonan condition codes to be used. Those condition codes may be preserved
5883 // even if the nonan flag is dropped somewhere.
5884 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5885 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5886 return (unsigned)CCCode & 0x10U;
5887 }
5888
5889 case ISD::OR:
5890 case ISD::ZERO_EXTEND:
5891 case ISD::SELECT:
5892 case ISD::VSELECT:
5893 case ISD::ADD:
5894 case ISD::SUB:
5895 case ISD::MUL:
5896 case ISD::FNEG:
5897 case ISD::FADD:
5898 case ISD::FSUB:
5899 case ISD::FMUL:
5900 case ISD::FDIV:
5901 case ISD::FREM:
5902 case ISD::FCOPYSIGN:
5903 case ISD::FMA:
5904 case ISD::FMAD:
5905 case ISD::FMULADD:
5906 case ISD::FP_EXTEND:
5912 // No poison except from flags (which is handled above)
5913 return false;
5914
5915 case ISD::SHL:
5916 case ISD::SRL:
5917 case ISD::SRA:
5918 // If the max shift amount isn't in range, then the shift can
5919 // create poison.
5920 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5921
5924 // If the amount is zero then the result will be poison.
5925 // TODO: Add isKnownNeverZero DemandedElts handling.
5926 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5927
5929 // Check if we demand any upper (undef) elements.
5930 return !PoisonOnly && DemandedElts.ugt(1);
5931
5934 // Ensure that the element index is in bounds.
5935 EVT VecVT = Op.getOperand(0).getValueType();
5936 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5937 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5938 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5939 }
5940
5941 case ISD::VECTOR_SHUFFLE: {
5942 // Check for any demanded shuffle element that is undef.
5943 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5944 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5945 if (Elt < 0 && DemandedElts[Idx])
5946 return true;
5947 return false;
5948 }
5949
5951 return false;
5952
5953 default:
5954 // Allow the target to implement this method for its nodes.
5955 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5956 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5957 return TLI->canCreateUndefOrPoisonForTargetNode(
5958 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5959 break;
5960 }
5961
5962 // Be conservative and return true.
5963 return true;
5964}
5965
5966bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5967 unsigned Opcode = Op.getOpcode();
5968 if (Opcode == ISD::OR)
5969 return Op->getFlags().hasDisjoint() ||
5970 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5971 if (Opcode == ISD::XOR)
5972 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5973 return false;
5974}
5975
5977 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5978 (Op.isAnyAdd() || isADDLike(Op));
5979}
5980
5982 unsigned Depth) const {
5983 EVT VT = Op.getValueType();
5984
5985 // Since the number of lanes in a scalable vector is unknown at compile time,
5986 // we track one bit which is implicitly broadcast to all lanes. This means
5987 // that all lanes in a scalable vector are considered demanded.
5988 APInt DemandedElts = VT.isFixedLengthVector()
5990 : APInt(1, 1);
5991
5992 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5993}
5994
5996 bool SNaN, unsigned Depth) const {
5997 assert(!DemandedElts.isZero() && "No demanded elements");
5998
5999 // If we're told that NaNs won't happen, assume they won't.
6000 if (Op->getFlags().hasNoNaNs())
6001 return true;
6002
6003 if (Depth >= MaxRecursionDepth)
6004 return false; // Limit search depth.
6005
6006 // If the value is a constant, we can obviously see if it is a NaN or not.
6008 return !C->getValueAPF().isNaN() ||
6009 (SNaN && !C->getValueAPF().isSignaling());
6010 }
6011
6012 unsigned Opcode = Op.getOpcode();
6013 switch (Opcode) {
6014 case ISD::FADD:
6015 case ISD::FSUB:
6016 case ISD::FMUL:
6017 case ISD::FDIV:
6018 case ISD::FREM:
6019 case ISD::FSIN:
6020 case ISD::FCOS:
6021 case ISD::FTAN:
6022 case ISD::FASIN:
6023 case ISD::FACOS:
6024 case ISD::FATAN:
6025 case ISD::FATAN2:
6026 case ISD::FSINH:
6027 case ISD::FCOSH:
6028 case ISD::FTANH:
6029 case ISD::FMA:
6030 case ISD::FMULADD:
6031 case ISD::FMAD: {
6032 if (SNaN)
6033 return true;
6034 // TODO: Need isKnownNeverInfinity
6035 return false;
6036 }
6037 case ISD::FCANONICALIZE:
6038 case ISD::FEXP:
6039 case ISD::FEXP2:
6040 case ISD::FEXP10:
6041 case ISD::FTRUNC:
6042 case ISD::FFLOOR:
6043 case ISD::FCEIL:
6044 case ISD::FROUND:
6045 case ISD::FROUNDEVEN:
6046 case ISD::LROUND:
6047 case ISD::LLROUND:
6048 case ISD::FRINT:
6049 case ISD::LRINT:
6050 case ISD::LLRINT:
6051 case ISD::FNEARBYINT:
6052 case ISD::FLDEXP: {
6053 if (SNaN)
6054 return true;
6055 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6056 }
6057 case ISD::FABS:
6058 case ISD::FNEG:
6059 case ISD::FCOPYSIGN: {
6060 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6061 }
6062 case ISD::SELECT:
6063 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
6064 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
6065 case ISD::FP_EXTEND:
6066 case ISD::FP_ROUND: {
6067 if (SNaN)
6068 return true;
6069 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6070 }
6071 case ISD::SINT_TO_FP:
6072 case ISD::UINT_TO_FP:
6073 return true;
6074 case ISD::FSQRT: // Need is known positive
6075 case ISD::FLOG:
6076 case ISD::FLOG2:
6077 case ISD::FLOG10:
6078 case ISD::FPOWI:
6079 case ISD::FPOW: {
6080 if (SNaN)
6081 return true;
6082 // TODO: Refine on operand
6083 return false;
6084 }
6085 case ISD::FMINNUM:
6086 case ISD::FMAXNUM:
6087 case ISD::FMINIMUMNUM:
6088 case ISD::FMAXIMUMNUM: {
6089 // Only one needs to be known not-nan, since it will be returned if the
6090 // other ends up being one.
6091 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
6092 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6093 }
6094 case ISD::FMINNUM_IEEE:
6095 case ISD::FMAXNUM_IEEE: {
6096 if (SNaN)
6097 return true;
6098 // This can return a NaN if either operand is an sNaN, or if both operands
6099 // are NaN.
6100 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
6101 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
6102 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
6103 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
6104 }
6105 case ISD::FMINIMUM:
6106 case ISD::FMAXIMUM: {
6107 // TODO: Does this quiet or return the origina NaN as-is?
6108 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
6109 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
6110 }
6112 SDValue Src = Op.getOperand(0);
6113 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6114 EVT SrcVT = Src.getValueType();
6115 if (SrcVT.isFixedLengthVector() && Idx &&
6116 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
6117 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
6118 Idx->getZExtValue());
6119 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6120 }
6121 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6122 }
6124 SDValue Src = Op.getOperand(0);
6125 if (Src.getValueType().isFixedLengthVector()) {
6126 unsigned Idx = Op.getConstantOperandVal(1);
6127 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6128 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6129 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6130 }
6131 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6132 }
6133 case ISD::INSERT_SUBVECTOR: {
6134 SDValue BaseVector = Op.getOperand(0);
6135 SDValue SubVector = Op.getOperand(1);
6136 EVT BaseVectorVT = BaseVector.getValueType();
6137 if (BaseVectorVT.isFixedLengthVector()) {
6138 unsigned Idx = Op.getConstantOperandVal(2);
6139 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6140 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6141
6142 // Clear/Extract the bits at the position where the subvector will be
6143 // inserted.
6144 APInt DemandedMask =
6145 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6146 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6147 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6148
6149 bool NeverNaN = true;
6150 if (!DemandedSrcElts.isZero())
6151 NeverNaN &=
6152 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6153 if (NeverNaN && !DemandedSubElts.isZero())
6154 NeverNaN &=
6155 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6156 return NeverNaN;
6157 }
6158 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6159 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6160 }
6161 case ISD::BUILD_VECTOR: {
6162 unsigned NumElts = Op.getNumOperands();
6163 for (unsigned I = 0; I != NumElts; ++I)
6164 if (DemandedElts[I] &&
6165 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6166 return false;
6167 return true;
6168 }
6169 case ISD::SPLAT_VECTOR:
6170 return isKnownNeverNaN(Op.getOperand(0), SNaN, Depth + 1);
6171 case ISD::AssertNoFPClass: {
6172 FPClassTest NoFPClass =
6173 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6174 if ((NoFPClass & fcNan) == fcNan)
6175 return true;
6176 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6177 return true;
6178 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6179 }
6180 default:
6181 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6182 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6183 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6184 Depth);
6185 }
6186
6187 return false;
6188 }
6189}
6190
6192 assert(Op.getValueType().isFloatingPoint() &&
6193 "Floating point type expected");
6194
6195 // If the value is a constant, we can obviously see if it is a zero or not.
6197 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6198}
6199
6201 EVT VT = Op.getValueType();
6202
6203 // Since the number of lanes in a scalable vector is unknown at compile time,
6204 // we track one bit which is implicitly broadcast to all lanes. This means
6205 // that all lanes in a scalable vector are considered demanded.
6206 APInt DemandedElts = VT.isFixedLengthVector()
6208 : APInt(1, 1);
6209
6210 return isKnownNeverZero(Op, DemandedElts, Depth);
6211}
6212
6214 unsigned Depth) const {
6215 if (Depth >= MaxRecursionDepth)
6216 return false; // Limit search depth.
6217
6218 EVT OpVT = Op.getValueType();
6219 unsigned BitWidth = OpVT.getScalarSizeInBits();
6220
6221 assert(!Op.getValueType().isFloatingPoint() &&
6222 "Floating point types unsupported - use isKnownNeverZeroFloat");
6223
6224 // If the value is a constant, we can obviously see if it is a zero or not.
6225 auto IsNeverZero = [BitWidth](const ConstantSDNode *C) {
6226 APInt V = C->getAPIntValue().zextOrTrunc(BitWidth);
6227 return !V.isZero();
6228 };
6229
6230 if (ISD::matchUnaryPredicate(Op, IsNeverZero))
6231 return true;
6232
6233 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6234 // some degree.
6235 switch (Op.getOpcode()) {
6236 default:
6237 break;
6238
6239 case ISD::BUILD_VECTOR:
6240 // Are all operands of a build vector constant non-zero?
6241 if (all_of(enumerate(Op->ops()), [&](auto P) {
6242 auto *C = dyn_cast<ConstantSDNode>(P.value());
6243 return !DemandedElts[P.index()] || (C && IsNeverZero(C));
6244 }))
6245 return true;
6246 break;
6247
6248 case ISD::SPLAT_VECTOR:
6249 // Is the operand of a splat vector a constant non-zero?
6250 if (auto *C = dyn_cast<ConstantSDNode>(Op->getOperand(0)))
6251 if (IsNeverZero(C))
6252 return true;
6253 break;
6254
6256 SDValue InVec = Op.getOperand(0);
6257 SDValue EltNo = Op.getOperand(1);
6258 EVT VecVT = InVec.getValueType();
6259
6260 // Skip scalable vectors or implicit extensions.
6261 if (VecVT.isScalableVector() ||
6262 OpVT.getScalarSizeInBits() != VecVT.getScalarSizeInBits())
6263 break;
6264
6265 // If we know the element index, just demand that vector element, else for
6266 // an unknown element index, ignore DemandedElts and demand them all.
6267 const unsigned NumSrcElts = VecVT.getVectorNumElements();
6268 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
6269 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
6270 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
6271 DemandedSrcElts =
6272 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
6273
6274 return isKnownNeverZero(InVec, DemandedSrcElts, Depth + 1);
6275 }
6276
6277 case ISD::OR:
6278 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6279 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6280
6281 case ISD::VSELECT:
6282 case ISD::SELECT:
6283 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6284 isKnownNeverZero(Op.getOperand(2), DemandedElts, Depth + 1);
6285
6286 case ISD::SHL: {
6287 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6288 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6289 KnownBits ValKnown =
6290 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6291 // 1 << X is never zero.
6292 if (ValKnown.One[0])
6293 return true;
6294 // If max shift cnt of known ones is non-zero, result is non-zero.
6295 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6296 .getMaxValue();
6297 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6298 !ValKnown.One.shl(MaxCnt).isZero())
6299 return true;
6300 break;
6301 }
6302
6303 case ISD::VECTOR_SHUFFLE: {
6304 if (Op.getValueType().isScalableVector())
6305 return false;
6306
6307 unsigned NumElts = DemandedElts.getBitWidth();
6308
6309 // All demanded elements from LHS and RHS must be known non-zero.
6310 // Demanded elements with undef shuffle mask elements are unknown.
6311
6312 APInt DemandedLHS, DemandedRHS;
6313 auto *SVN = cast<ShuffleVectorSDNode>(Op);
6314 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
6315 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
6316 DemandedLHS, DemandedRHS))
6317 return false;
6318
6319 return (!DemandedLHS ||
6320 isKnownNeverZero(Op.getOperand(0), DemandedLHS, Depth + 1)) &&
6321 (!DemandedRHS ||
6322 isKnownNeverZero(Op.getOperand(1), DemandedRHS, Depth + 1));
6323 }
6324
6325 case ISD::UADDSAT:
6326 case ISD::UMAX:
6327 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6328 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6329
6330 case ISD::UMIN:
6331 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6332 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6333
6334 // For smin/smax: If either operand is known negative/positive
6335 // respectively we don't need the other to be known at all.
6336 case ISD::SMAX: {
6337 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6338 if (Op1.isStrictlyPositive())
6339 return true;
6340
6341 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6342 if (Op0.isStrictlyPositive())
6343 return true;
6344
6345 if (Op1.isNonZero() && Op0.isNonZero())
6346 return true;
6347
6348 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6349 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6350 }
6351 case ISD::SMIN: {
6352 KnownBits Op1 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
6353 if (Op1.isNegative())
6354 return true;
6355
6356 KnownBits Op0 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6357 if (Op0.isNegative())
6358 return true;
6359
6360 if (Op1.isNonZero() && Op0.isNonZero())
6361 return true;
6362
6363 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) &&
6364 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6365 }
6366
6367 case ISD::ROTL:
6368 case ISD::ROTR:
6369 case ISD::BITREVERSE:
6370 case ISD::BSWAP:
6371 case ISD::CTPOP:
6372 case ISD::ABS:
6373 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6374
6375 case ISD::SRA:
6376 case ISD::SRL: {
6377 if (Op->getFlags().hasExact())
6378 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6379 KnownBits ValKnown =
6380 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
6381 if (ValKnown.isNegative())
6382 return true;
6383 // If max shift cnt of known ones is non-zero, result is non-zero.
6384 APInt MaxCnt = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1)
6385 .getMaxValue();
6386 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6387 !ValKnown.One.lshr(MaxCnt).isZero())
6388 return true;
6389 break;
6390 }
6391 case ISD::UDIV:
6392 case ISD::SDIV:
6393 // div exact can only produce a zero if the dividend is zero.
6394 // TODO: For udiv this is also true if Op1 u<= Op0
6395 if (Op->getFlags().hasExact())
6396 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6397 break;
6398
6399 case ISD::ADD:
6400 if (Op->getFlags().hasNoUnsignedWrap())
6401 if (isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1) ||
6402 isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1))
6403 return true;
6404 // TODO: There are a lot more cases we can prove for add.
6405 break;
6406
6407 case ISD::SUB: {
6408 if (isNullConstant(Op.getOperand(0)))
6409 return isKnownNeverZero(Op.getOperand(1), DemandedElts, Depth + 1);
6410
6411 std::optional<bool> ne = KnownBits::ne(
6412 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1),
6413 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1));
6414 return ne && *ne;
6415 }
6416
6417 case ISD::MUL:
6418 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6419 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6420 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6421 return true;
6422 break;
6423
6424 case ISD::ZERO_EXTEND:
6425 case ISD::SIGN_EXTEND:
6426 return isKnownNeverZero(Op.getOperand(0), DemandedElts, Depth + 1);
6427 case ISD::VSCALE: {
6429 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6430 ConstantRange CR =
6431 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6432 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6433 return true;
6434 break;
6435 }
6436 }
6437
6439}
6440
6442 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6443 return !C1->isNegative();
6444
6445 switch (Op.getOpcode()) {
6446 case ISD::FABS:
6447 case ISD::FEXP:
6448 case ISD::FEXP2:
6449 case ISD::FEXP10:
6450 return true;
6451 default:
6452 return false;
6453 }
6454
6455 llvm_unreachable("covered opcode switch");
6456}
6457
6459 assert(Use.getValueType().isFloatingPoint());
6460 const SDNode *User = Use.getUser();
6461 if (User->getFlags().hasNoSignedZeros())
6462 return true;
6463
6464 unsigned OperandNo = Use.getOperandNo();
6465 // Check if this use is insensitive to the sign of zero
6466 switch (User->getOpcode()) {
6467 case ISD::SETCC:
6468 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6469 case ISD::FABS:
6470 // fabs always produces +0.0.
6471 return true;
6472 case ISD::FCOPYSIGN:
6473 // copysign overwrites the sign bit of the first operand.
6474 return OperandNo == 0;
6475 case ISD::FADD:
6476 case ISD::FSUB: {
6477 // Arithmetic with non-zero constants fixes the uncertainty around the
6478 // sign bit.
6479 SDValue Other = User->getOperand(1 - OperandNo);
6481 }
6482 case ISD::FP_TO_SINT:
6483 case ISD::FP_TO_UINT:
6484 // fp-to-int conversions normalize signed zeros.
6485 return true;
6486 default:
6487 return false;
6488 }
6489}
6490
6492 if (Op->getFlags().hasNoSignedZeros())
6493 return true;
6494 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6495 // regression. Ideally, this should be implemented as a demanded-bits
6496 // optimization that stems from the users.
6497 if (Op->use_size() > 2)
6498 return false;
6499 return all_of(Op->uses(),
6500 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6501}
6502
6504 // Check the obvious case.
6505 if (A == B) return true;
6506
6507 // For negative and positive zero.
6510 if (CA->isZero() && CB->isZero()) return true;
6511
6512 // Otherwise they may not be equal.
6513 return false;
6514}
6515
6516// Only bits set in Mask must be negated, other bits may be arbitrary.
6518 if (isBitwiseNot(V, AllowUndefs))
6519 return V.getOperand(0);
6520
6521 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6522 // bits in the non-extended part.
6523 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6524 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6525 return SDValue();
6526 SDValue ExtArg = V.getOperand(0);
6527 if (ExtArg.getScalarValueSizeInBits() >=
6528 MaskC->getAPIntValue().getActiveBits() &&
6529 isBitwiseNot(ExtArg, AllowUndefs) &&
6530 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6531 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6532 return ExtArg.getOperand(0).getOperand(0);
6533 return SDValue();
6534}
6535
6537 // Match masked merge pattern (X & ~M) op (Y & M)
6538 // Including degenerate case (X & ~M) op M
6539 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6540 SDValue Other) {
6541 if (SDValue NotOperand =
6542 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6543 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6544 NotOperand->getOpcode() == ISD::TRUNCATE)
6545 NotOperand = NotOperand->getOperand(0);
6546
6547 if (Other == NotOperand)
6548 return true;
6549 if (Other->getOpcode() == ISD::AND)
6550 return NotOperand == Other->getOperand(0) ||
6551 NotOperand == Other->getOperand(1);
6552 }
6553 return false;
6554 };
6555
6556 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6557 A = A->getOperand(0);
6558
6559 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6560 B = B->getOperand(0);
6561
6562 if (A->getOpcode() == ISD::AND)
6563 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6564 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6565 return false;
6566}
6567
6568// FIXME: unify with llvm::haveNoCommonBitsSet.
6570 assert(A.getValueType() == B.getValueType() &&
6571 "Values must have the same type");
6574 return true;
6577}
6578
6579static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6580 SelectionDAG &DAG) {
6581 if (cast<ConstantSDNode>(Step)->isZero())
6582 return DAG.getConstant(0, DL, VT);
6583
6584 return SDValue();
6585}
6586
6589 SelectionDAG &DAG) {
6590 int NumOps = Ops.size();
6591 assert(NumOps != 0 && "Can't build an empty vector!");
6592 assert(!VT.isScalableVector() &&
6593 "BUILD_VECTOR cannot be used with scalable types");
6594 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6595 "Incorrect element count in BUILD_VECTOR!");
6596
6597 // BUILD_VECTOR of UNDEFs is UNDEF.
6598 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6599 return DAG.getUNDEF(VT);
6600
6601 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6602 SDValue IdentitySrc;
6603 bool IsIdentity = true;
6604 for (int i = 0; i != NumOps; ++i) {
6606 Ops[i].getOperand(0).getValueType() != VT ||
6607 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6608 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6609 Ops[i].getConstantOperandAPInt(1) != i) {
6610 IsIdentity = false;
6611 break;
6612 }
6613 IdentitySrc = Ops[i].getOperand(0);
6614 }
6615 if (IsIdentity)
6616 return IdentitySrc;
6617
6618 return SDValue();
6619}
6620
6621/// Try to simplify vector concatenation to an input value, undef, or build
6622/// vector.
6625 SelectionDAG &DAG) {
6626 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6628 [Ops](SDValue Op) {
6629 return Ops[0].getValueType() == Op.getValueType();
6630 }) &&
6631 "Concatenation of vectors with inconsistent value types!");
6632 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6633 VT.getVectorElementCount() &&
6634 "Incorrect element count in vector concatenation!");
6635
6636 if (Ops.size() == 1)
6637 return Ops[0];
6638
6639 // Concat of UNDEFs is UNDEF.
6640 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6641 return DAG.getUNDEF(VT);
6642
6643 // Scan the operands and look for extract operations from a single source
6644 // that correspond to insertion at the same location via this concatenation:
6645 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6646 SDValue IdentitySrc;
6647 bool IsIdentity = true;
6648 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6649 SDValue Op = Ops[i];
6650 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6651 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6652 Op.getOperand(0).getValueType() != VT ||
6653 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6654 Op.getConstantOperandVal(1) != IdentityIndex) {
6655 IsIdentity = false;
6656 break;
6657 }
6658 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6659 "Unexpected identity source vector for concat of extracts");
6660 IdentitySrc = Op.getOperand(0);
6661 }
6662 if (IsIdentity) {
6663 assert(IdentitySrc && "Failed to set source vector of extracts");
6664 return IdentitySrc;
6665 }
6666
6667 // The code below this point is only designed to work for fixed width
6668 // vectors, so we bail out for now.
6669 if (VT.isScalableVector())
6670 return SDValue();
6671
6672 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6673 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6674 // BUILD_VECTOR.
6675 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6676 EVT SVT = VT.getScalarType();
6678 for (SDValue Op : Ops) {
6679 EVT OpVT = Op.getValueType();
6680 if (Op.isUndef())
6681 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6682 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6683 Elts.append(Op->op_begin(), Op->op_end());
6684 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6685 OpVT.getVectorNumElements() == 1 &&
6686 isNullConstant(Op.getOperand(2)))
6687 Elts.push_back(Op.getOperand(1));
6688 else
6689 return SDValue();
6690 }
6691
6692 // BUILD_VECTOR requires all inputs to be of the same type, find the
6693 // maximum type and extend them all.
6694 for (SDValue Op : Elts)
6695 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6696
6697 if (SVT.bitsGT(VT.getScalarType())) {
6698 for (SDValue &Op : Elts) {
6699 if (Op.isUndef())
6700 Op = DAG.getUNDEF(SVT);
6701 else
6702 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6703 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6704 : DAG.getSExtOrTrunc(Op, DL, SVT);
6705 }
6706 }
6707
6708 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6709 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6710 return V;
6711}
6712
6713/// Gets or creates the specified node.
6714SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6715 SDVTList VTs = getVTList(VT);
6717 AddNodeIDNode(ID, Opcode, VTs, {});
6718 void *IP = nullptr;
6719 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6720 return SDValue(E, 0);
6721
6722 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6723 CSEMap.InsertNode(N, IP);
6724
6725 InsertNode(N);
6726 SDValue V = SDValue(N, 0);
6727 NewSDValueDbgMsg(V, "Creating new node: ", this);
6728 return V;
6729}
6730
6731SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6732 SDValue N1) {
6733 SDNodeFlags Flags;
6734 if (Inserter)
6735 Flags = Inserter->getFlags();
6736 return getNode(Opcode, DL, VT, N1, Flags);
6737}
6738
6739SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6740 SDValue N1, const SDNodeFlags Flags) {
6741 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6742
6743 // Constant fold unary operations with a vector integer or float operand.
6744 switch (Opcode) {
6745 default:
6746 // FIXME: Entirely reasonable to perform folding of other unary
6747 // operations here as the need arises.
6748 break;
6749 case ISD::FNEG:
6750 case ISD::FABS:
6751 case ISD::FCEIL:
6752 case ISD::FTRUNC:
6753 case ISD::FFLOOR:
6754 case ISD::FP_EXTEND:
6755 case ISD::FP_TO_SINT:
6756 case ISD::FP_TO_UINT:
6757 case ISD::FP_TO_FP16:
6758 case ISD::FP_TO_BF16:
6759 case ISD::TRUNCATE:
6760 case ISD::ANY_EXTEND:
6761 case ISD::ZERO_EXTEND:
6762 case ISD::SIGN_EXTEND:
6763 case ISD::UINT_TO_FP:
6764 case ISD::SINT_TO_FP:
6765 case ISD::FP16_TO_FP:
6766 case ISD::BF16_TO_FP:
6767 case ISD::BITCAST:
6768 case ISD::ABS:
6769 case ISD::BITREVERSE:
6770 case ISD::BSWAP:
6771 case ISD::CTLZ:
6773 case ISD::CTTZ:
6775 case ISD::CTPOP:
6776 case ISD::CTLS:
6777 case ISD::STEP_VECTOR: {
6778 SDValue Ops = {N1};
6779 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6780 return Fold;
6781 }
6782 }
6783
6784 unsigned OpOpcode = N1.getNode()->getOpcode();
6785 switch (Opcode) {
6786 case ISD::STEP_VECTOR:
6787 assert(VT.isScalableVector() &&
6788 "STEP_VECTOR can only be used with scalable types");
6789 assert(OpOpcode == ISD::TargetConstant &&
6790 VT.getVectorElementType() == N1.getValueType() &&
6791 "Unexpected step operand");
6792 break;
6793 case ISD::FREEZE:
6794 assert(VT == N1.getValueType() && "Unexpected VT!");
6795 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6796 return N1;
6797 break;
6798 case ISD::TokenFactor:
6799 case ISD::MERGE_VALUES:
6801 return N1; // Factor, merge or concat of one node? No need.
6802 case ISD::BUILD_VECTOR: {
6803 // Attempt to simplify BUILD_VECTOR.
6804 SDValue Ops[] = {N1};
6805 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6806 return V;
6807 break;
6808 }
6809 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6810 case ISD::FP_EXTEND:
6812 "Invalid FP cast!");
6813 if (N1.getValueType() == VT) return N1; // noop conversion.
6814 assert((!VT.isVector() || VT.getVectorElementCount() ==
6816 "Vector element count mismatch!");
6817 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6818 if (N1.isUndef())
6819 return getUNDEF(VT);
6820 break;
6821 case ISD::FP_TO_SINT:
6822 case ISD::FP_TO_UINT:
6823 if (N1.isUndef())
6824 return getUNDEF(VT);
6825 break;
6826 case ISD::SINT_TO_FP:
6827 case ISD::UINT_TO_FP:
6828 // [us]itofp(undef) = 0, because the result value is bounded.
6829 if (N1.isUndef())
6830 return getConstantFP(0.0, DL, VT);
6831 break;
6832 case ISD::SIGN_EXTEND:
6833 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6834 "Invalid SIGN_EXTEND!");
6835 assert(VT.isVector() == N1.getValueType().isVector() &&
6836 "SIGN_EXTEND result type type should be vector iff the operand "
6837 "type is vector!");
6838 if (N1.getValueType() == VT) return N1; // noop extension
6839 assert((!VT.isVector() || VT.getVectorElementCount() ==
6841 "Vector element count mismatch!");
6842 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6843 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6844 SDNodeFlags Flags;
6845 if (OpOpcode == ISD::ZERO_EXTEND)
6846 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6847 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6848 transferDbgValues(N1, NewVal);
6849 return NewVal;
6850 }
6851
6852 if (OpOpcode == ISD::POISON)
6853 return getPOISON(VT);
6854
6855 if (N1.isUndef())
6856 // sext(undef) = 0, because the top bits will all be the same.
6857 return getConstant(0, DL, VT);
6858
6859 // Skip unnecessary sext_inreg pattern:
6860 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6861 if (OpOpcode == ISD::TRUNCATE) {
6862 SDValue OpOp = N1.getOperand(0);
6863 if (OpOp.getValueType() == VT) {
6864 unsigned NumSignExtBits =
6866 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6867 transferDbgValues(N1, OpOp);
6868 return OpOp;
6869 }
6870 }
6871 }
6872 break;
6873 case ISD::ZERO_EXTEND:
6874 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6875 "Invalid ZERO_EXTEND!");
6876 assert(VT.isVector() == N1.getValueType().isVector() &&
6877 "ZERO_EXTEND result type type should be vector iff the operand "
6878 "type is vector!");
6879 if (N1.getValueType() == VT) return N1; // noop extension
6880 assert((!VT.isVector() || VT.getVectorElementCount() ==
6882 "Vector element count mismatch!");
6883 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6884 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6885 SDNodeFlags Flags;
6886 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6887 SDValue NewVal =
6888 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6889 transferDbgValues(N1, NewVal);
6890 return NewVal;
6891 }
6892
6893 if (OpOpcode == ISD::POISON)
6894 return getPOISON(VT);
6895
6896 if (N1.isUndef())
6897 // zext(undef) = 0, because the top bits will be zero.
6898 return getConstant(0, DL, VT);
6899
6900 // Skip unnecessary zext_inreg pattern:
6901 // (zext (trunc x)) -> x iff the upper bits are known zero.
6902 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6903 // use to recognise zext_inreg patterns.
6904 if (OpOpcode == ISD::TRUNCATE) {
6905 SDValue OpOp = N1.getOperand(0);
6906 if (OpOp.getValueType() == VT) {
6907 if (OpOp.getOpcode() != ISD::AND) {
6910 if (MaskedValueIsZero(OpOp, HiBits)) {
6911 transferDbgValues(N1, OpOp);
6912 return OpOp;
6913 }
6914 }
6915 }
6916 }
6917 break;
6918 case ISD::ANY_EXTEND:
6919 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6920 "Invalid ANY_EXTEND!");
6921 assert(VT.isVector() == N1.getValueType().isVector() &&
6922 "ANY_EXTEND result type type should be vector iff the operand "
6923 "type is vector!");
6924 if (N1.getValueType() == VT) return N1; // noop extension
6925 assert((!VT.isVector() || VT.getVectorElementCount() ==
6927 "Vector element count mismatch!");
6928 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6929
6930 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6931 OpOpcode == ISD::ANY_EXTEND) {
6932 SDNodeFlags Flags;
6933 if (OpOpcode == ISD::ZERO_EXTEND)
6934 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6935 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6936 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6937 }
6938 if (N1.isUndef())
6939 return getUNDEF(VT);
6940
6941 // (ext (trunc x)) -> x
6942 if (OpOpcode == ISD::TRUNCATE) {
6943 SDValue OpOp = N1.getOperand(0);
6944 if (OpOp.getValueType() == VT) {
6945 transferDbgValues(N1, OpOp);
6946 return OpOp;
6947 }
6948 }
6949 break;
6950 case ISD::TRUNCATE:
6951 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6952 "Invalid TRUNCATE!");
6953 assert(VT.isVector() == N1.getValueType().isVector() &&
6954 "TRUNCATE result type type should be vector iff the operand "
6955 "type is vector!");
6956 if (N1.getValueType() == VT) return N1; // noop truncate
6957 assert((!VT.isVector() || VT.getVectorElementCount() ==
6959 "Vector element count mismatch!");
6960 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6961 if (OpOpcode == ISD::TRUNCATE)
6962 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6963 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6964 OpOpcode == ISD::ANY_EXTEND) {
6965 // If the source is smaller than the dest, we still need an extend.
6967 VT.getScalarType())) {
6968 SDNodeFlags Flags;
6969 if (OpOpcode == ISD::ZERO_EXTEND)
6970 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6971 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6972 }
6973 if (N1.getOperand(0).getValueType().bitsGT(VT))
6974 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6975 return N1.getOperand(0);
6976 }
6977 if (N1.isUndef())
6978 return getUNDEF(VT);
6979 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6980 return getVScale(DL, VT,
6982 break;
6986 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6987 assert(N1.getValueType().bitsLE(VT) &&
6988 "The input must be the same size or smaller than the result.");
6991 "The destination vector type must have fewer lanes than the input.");
6992 break;
6993 case ISD::ABS:
6994 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6995 if (N1.isUndef())
6996 return getConstant(0, DL, VT);
6997 break;
6998 case ISD::BSWAP:
6999 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
7000 assert((VT.getScalarSizeInBits() % 16 == 0) &&
7001 "BSWAP types must be a multiple of 16 bits!");
7002 if (N1.isUndef())
7003 return getUNDEF(VT);
7004 // bswap(bswap(X)) -> X.
7005 if (OpOpcode == ISD::BSWAP)
7006 return N1.getOperand(0);
7007 break;
7008 case ISD::BITREVERSE:
7009 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
7010 if (N1.isUndef())
7011 return getUNDEF(VT);
7012 break;
7013 case ISD::BITCAST:
7015 "Cannot BITCAST between types of different sizes!");
7016 if (VT == N1.getValueType()) return N1; // noop conversion.
7017 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
7018 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
7019 if (N1.isUndef())
7020 return getUNDEF(VT);
7021 break;
7023 assert(VT.isVector() && !N1.getValueType().isVector() &&
7024 (VT.getVectorElementType() == N1.getValueType() ||
7026 N1.getValueType().isInteger() &&
7028 "Illegal SCALAR_TO_VECTOR node!");
7029 if (N1.isUndef())
7030 return getUNDEF(VT);
7031 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
7032 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
7034 N1.getConstantOperandVal(1) == 0 &&
7035 N1.getOperand(0).getValueType() == VT)
7036 return N1.getOperand(0);
7037 break;
7038 case ISD::FNEG:
7039 // Negation of an unknown bag of bits is still completely undefined.
7040 if (N1.isUndef())
7041 return getUNDEF(VT);
7042
7043 if (OpOpcode == ISD::FNEG) // --X -> X
7044 return N1.getOperand(0);
7045 break;
7046 case ISD::FABS:
7047 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
7048 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
7049 break;
7050 case ISD::VSCALE:
7051 assert(VT == N1.getValueType() && "Unexpected VT!");
7052 break;
7053 case ISD::CTPOP:
7054 if (N1.getValueType().getScalarType() == MVT::i1)
7055 return N1;
7056 break;
7057 case ISD::CTLZ:
7058 case ISD::CTTZ:
7059 if (N1.getValueType().getScalarType() == MVT::i1)
7060 return getNOT(DL, N1, N1.getValueType());
7061 break;
7062 case ISD::CTLS:
7063 if (N1.getValueType().getScalarType() == MVT::i1)
7064 return getConstant(0, DL, VT);
7065 break;
7066 case ISD::VECREDUCE_ADD:
7067 if (N1.getValueType().getScalarType() == MVT::i1)
7068 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
7069 break;
7072 if (N1.getValueType().getScalarType() == MVT::i1)
7073 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
7074 break;
7077 if (N1.getValueType().getScalarType() == MVT::i1)
7078 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
7079 break;
7080 case ISD::SPLAT_VECTOR:
7081 assert(VT.isVector() && "Wrong return type!");
7082 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
7083 // that for now.
7085 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
7087 N1.getValueType().isInteger() &&
7089 "Wrong operand type!");
7090 break;
7091 }
7092
7093 SDNode *N;
7094 SDVTList VTs = getVTList(VT);
7095 SDValue Ops[] = {N1};
7096 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
7098 AddNodeIDNode(ID, Opcode, VTs, Ops);
7099 void *IP = nullptr;
7100 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
7101 E->intersectFlagsWith(Flags);
7102 return SDValue(E, 0);
7103 }
7104
7105 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7106 N->setFlags(Flags);
7107 createOperands(N, Ops);
7108 CSEMap.InsertNode(N, IP);
7109 } else {
7110 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
7111 createOperands(N, Ops);
7112 }
7113
7114 InsertNode(N);
7115 SDValue V = SDValue(N, 0);
7116 NewSDValueDbgMsg(V, "Creating new node: ", this);
7117 return V;
7118}
7119
7120static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
7121 const APInt &C2) {
7122 switch (Opcode) {
7123 case ISD::ADD: return C1 + C2;
7124 case ISD::SUB: return C1 - C2;
7125 case ISD::MUL: return C1 * C2;
7126 case ISD::AND: return C1 & C2;
7127 case ISD::OR: return C1 | C2;
7128 case ISD::XOR: return C1 ^ C2;
7129 case ISD::SHL: return C1 << C2;
7130 case ISD::SRL: return C1.lshr(C2);
7131 case ISD::SRA: return C1.ashr(C2);
7132 case ISD::ROTL: return C1.rotl(C2);
7133 case ISD::ROTR: return C1.rotr(C2);
7134 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
7135 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
7136 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
7137 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
7138 case ISD::SADDSAT: return C1.sadd_sat(C2);
7139 case ISD::UADDSAT: return C1.uadd_sat(C2);
7140 case ISD::SSUBSAT: return C1.ssub_sat(C2);
7141 case ISD::USUBSAT: return C1.usub_sat(C2);
7142 case ISD::SSHLSAT: return C1.sshl_sat(C2);
7143 case ISD::USHLSAT: return C1.ushl_sat(C2);
7144 case ISD::UDIV:
7145 if (!C2.getBoolValue())
7146 break;
7147 return C1.udiv(C2);
7148 case ISD::UREM:
7149 if (!C2.getBoolValue())
7150 break;
7151 return C1.urem(C2);
7152 case ISD::SDIV:
7153 if (!C2.getBoolValue())
7154 break;
7155 return C1.sdiv(C2);
7156 case ISD::SREM:
7157 if (!C2.getBoolValue())
7158 break;
7159 return C1.srem(C2);
7160 case ISD::AVGFLOORS:
7161 return APIntOps::avgFloorS(C1, C2);
7162 case ISD::AVGFLOORU:
7163 return APIntOps::avgFloorU(C1, C2);
7164 case ISD::AVGCEILS:
7165 return APIntOps::avgCeilS(C1, C2);
7166 case ISD::AVGCEILU:
7167 return APIntOps::avgCeilU(C1, C2);
7168 case ISD::ABDS:
7169 return APIntOps::abds(C1, C2);
7170 case ISD::ABDU:
7171 return APIntOps::abdu(C1, C2);
7172 case ISD::MULHS:
7173 return APIntOps::mulhs(C1, C2);
7174 case ISD::MULHU:
7175 return APIntOps::mulhu(C1, C2);
7176 case ISD::CLMUL:
7177 return APIntOps::clmul(C1, C2);
7178 case ISD::CLMULR:
7179 return APIntOps::clmulr(C1, C2);
7180 case ISD::CLMULH:
7181 return APIntOps::clmulh(C1, C2);
7182 }
7183 return std::nullopt;
7184}
7185// Handle constant folding with UNDEF.
7186// TODO: Handle more cases.
7187static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
7188 bool IsUndef1, const APInt &C2,
7189 bool IsUndef2) {
7190 if (!(IsUndef1 || IsUndef2))
7191 return FoldValue(Opcode, C1, C2);
7192
7193 // Fold and(x, undef) -> 0
7194 // Fold mul(x, undef) -> 0
7195 if (Opcode == ISD::AND || Opcode == ISD::MUL)
7196 return APInt::getZero(C1.getBitWidth());
7197
7198 return std::nullopt;
7199}
7200
7202 const GlobalAddressSDNode *GA,
7203 const SDNode *N2) {
7204 if (GA->getOpcode() != ISD::GlobalAddress)
7205 return SDValue();
7206 if (!TLI->isOffsetFoldingLegal(GA))
7207 return SDValue();
7208 auto *C2 = dyn_cast<ConstantSDNode>(N2);
7209 if (!C2)
7210 return SDValue();
7211 int64_t Offset = C2->getSExtValue();
7212 switch (Opcode) {
7213 case ISD::ADD:
7214 case ISD::PTRADD:
7215 break;
7216 case ISD::SUB: Offset = -uint64_t(Offset); break;
7217 default: return SDValue();
7218 }
7219 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7220 GA->getOffset() + uint64_t(Offset));
7221}
7222
7224 switch (Opcode) {
7225 case ISD::SDIV:
7226 case ISD::UDIV:
7227 case ISD::SREM:
7228 case ISD::UREM: {
7229 // If a divisor is zero/undef or any element of a divisor vector is
7230 // zero/undef, the whole op is undef.
7231 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7232 SDValue Divisor = Ops[1];
7233 if (Divisor.isUndef() || isNullConstant(Divisor))
7234 return true;
7235
7236 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7237 llvm::any_of(Divisor->op_values(),
7238 [](SDValue V) { return V.isUndef() ||
7239 isNullConstant(V); });
7240 // TODO: Handle signed overflow.
7241 }
7242 // TODO: Handle oversized shifts.
7243 default:
7244 return false;
7245 }
7246}
7247
7250 SDNodeFlags Flags) {
7251 // If the opcode is a target-specific ISD node, there's nothing we can
7252 // do here and the operand rules may not line up with the below, so
7253 // bail early.
7254 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7255 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7256 // foldCONCAT_VECTORS in getNode before this is called.
7257 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7258 return SDValue();
7259
7260 unsigned NumOps = Ops.size();
7261 if (NumOps == 0)
7262 return SDValue();
7263
7264 if (isUndef(Opcode, Ops))
7265 return getUNDEF(VT);
7266
7267 // Handle unary special cases.
7268 if (NumOps == 1) {
7269 SDValue N1 = Ops[0];
7270
7271 // Constant fold unary operations with an integer constant operand. Even
7272 // opaque constant will be folded, because the folding of unary operations
7273 // doesn't create new constants with different values. Nevertheless, the
7274 // opaque flag is preserved during folding to prevent future folding with
7275 // other constants.
7276 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7277 const APInt &Val = C->getAPIntValue();
7278 switch (Opcode) {
7279 case ISD::SIGN_EXTEND:
7280 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7281 C->isTargetOpcode(), C->isOpaque());
7282 case ISD::TRUNCATE:
7283 if (C->isOpaque())
7284 break;
7285 [[fallthrough]];
7286 case ISD::ZERO_EXTEND:
7287 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7288 C->isTargetOpcode(), C->isOpaque());
7289 case ISD::ANY_EXTEND:
7290 // Some targets like RISCV prefer to sign extend some types.
7291 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7292 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7293 C->isTargetOpcode(), C->isOpaque());
7294 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7295 C->isTargetOpcode(), C->isOpaque());
7296 case ISD::ABS:
7297 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7298 C->isOpaque());
7299 case ISD::BITREVERSE:
7300 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7301 C->isOpaque());
7302 case ISD::BSWAP:
7303 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7304 C->isOpaque());
7305 case ISD::CTPOP:
7306 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7307 C->isOpaque());
7308 case ISD::CTLZ:
7310 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7311 C->isOpaque());
7312 case ISD::CTTZ:
7314 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7315 C->isOpaque());
7316 case ISD::CTLS:
7317 // CTLS returns the number of extra sign bits so subtract one.
7318 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7319 C->isTargetOpcode(), C->isOpaque());
7320 case ISD::UINT_TO_FP:
7321 case ISD::SINT_TO_FP: {
7323 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7325 return getConstantFP(FPV, DL, VT);
7326 }
7327 case ISD::FP16_TO_FP:
7328 case ISD::BF16_TO_FP: {
7329 bool Ignored;
7330 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7331 : APFloat::BFloat(),
7332 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7333
7334 // This can return overflow, underflow, or inexact; we don't care.
7335 // FIXME need to be more flexible about rounding mode.
7337 &Ignored);
7338 return getConstantFP(FPV, DL, VT);
7339 }
7340 case ISD::STEP_VECTOR:
7341 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7342 return V;
7343 break;
7344 case ISD::BITCAST:
7345 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7346 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7347 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7348 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7349 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7350 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7351 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7352 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7353 break;
7354 }
7355 }
7356
7357 // Constant fold unary operations with a floating point constant operand.
7358 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7359 APFloat V = C->getValueAPF(); // make copy
7360 switch (Opcode) {
7361 case ISD::FNEG:
7362 V.changeSign();
7363 return getConstantFP(V, DL, VT);
7364 case ISD::FABS:
7365 V.clearSign();
7366 return getConstantFP(V, DL, VT);
7367 case ISD::FCEIL: {
7368 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7370 return getConstantFP(V, DL, VT);
7371 return SDValue();
7372 }
7373 case ISD::FTRUNC: {
7374 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7376 return getConstantFP(V, DL, VT);
7377 return SDValue();
7378 }
7379 case ISD::FFLOOR: {
7380 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7382 return getConstantFP(V, DL, VT);
7383 return SDValue();
7384 }
7385 case ISD::FP_EXTEND: {
7386 bool ignored;
7387 // This can return overflow, underflow, or inexact; we don't care.
7388 // FIXME need to be more flexible about rounding mode.
7389 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7390 &ignored);
7391 return getConstantFP(V, DL, VT);
7392 }
7393 case ISD::FP_TO_SINT:
7394 case ISD::FP_TO_UINT: {
7395 bool ignored;
7396 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7397 // FIXME need to be more flexible about rounding mode.
7399 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7400 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7401 break;
7402 return getConstant(IntVal, DL, VT);
7403 }
7404 case ISD::FP_TO_FP16:
7405 case ISD::FP_TO_BF16: {
7406 bool Ignored;
7407 // This can return overflow, underflow, or inexact; we don't care.
7408 // FIXME need to be more flexible about rounding mode.
7409 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7410 : APFloat::BFloat(),
7412 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7413 }
7414 case ISD::BITCAST:
7415 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7416 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7417 VT);
7418 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7419 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7420 VT);
7421 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7422 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7423 VT);
7424 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7425 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7426 break;
7427 }
7428 }
7429
7430 // Early-out if we failed to constant fold a bitcast.
7431 if (Opcode == ISD::BITCAST)
7432 return SDValue();
7433 }
7434
7435 // Handle binops special cases.
7436 if (NumOps == 2) {
7437 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7438 return CFP;
7439
7440 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7441 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7442 if (C1->isOpaque() || C2->isOpaque())
7443 return SDValue();
7444
7445 std::optional<APInt> FoldAttempt =
7446 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7447 if (!FoldAttempt)
7448 return SDValue();
7449
7450 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7451 assert((!Folded || !VT.isVector()) &&
7452 "Can't fold vectors ops with scalar operands");
7453 return Folded;
7454 }
7455 }
7456
7457 // fold (add Sym, c) -> Sym+c
7459 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7460 if (TLI->isCommutativeBinOp(Opcode))
7462 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7463
7464 // fold (sext_in_reg c1) -> c2
7465 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7466 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7467
7468 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7469 unsigned FromBits = EVT.getScalarSizeInBits();
7470 Val <<= Val.getBitWidth() - FromBits;
7471 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7472 return getConstant(Val, DL, ConstantVT);
7473 };
7474
7475 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7476 const APInt &Val = C1->getAPIntValue();
7477 return SignExtendInReg(Val, VT);
7478 }
7479
7481 SmallVector<SDValue, 8> ScalarOps;
7482 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7483 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7484 SDValue Op = Ops[0].getOperand(I);
7485 if (Op.isUndef()) {
7486 ScalarOps.push_back(getUNDEF(OpVT));
7487 continue;
7488 }
7489 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7490 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7491 }
7492 return getBuildVector(VT, DL, ScalarOps);
7493 }
7494
7495 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7496 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7497 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7498 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7499 Ops[0].getOperand(0).getValueType()));
7500 }
7501 }
7502
7503 // Handle fshl/fshr special cases.
7504 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7505 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7506 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7507 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7508
7509 if (C1 && C2 && C3) {
7510 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7511 return SDValue();
7512 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7513 &V3 = C3->getAPIntValue();
7514
7515 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7516 : APIntOps::fshr(V1, V2, V3);
7517 return getConstant(FoldedVal, DL, VT);
7518 }
7519 }
7520
7521 // Handle fma/fmad special cases.
7522 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7523 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7524 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7525 Ops[2].getValueType() == VT && "FMA types must match!");
7529 if (C1 && C2 && C3) {
7530 APFloat V1 = C1->getValueAPF();
7531 const APFloat &V2 = C2->getValueAPF();
7532 const APFloat &V3 = C3->getValueAPF();
7533 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7536 } else
7538 return getConstantFP(V1, DL, VT);
7539 }
7540 }
7541
7542 // This is for vector folding only from here on.
7543 if (!VT.isVector())
7544 return SDValue();
7545
7546 ElementCount NumElts = VT.getVectorElementCount();
7547
7548 // See if we can fold through any bitcasted integer ops.
7549 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7550 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7551 (Ops[0].getOpcode() == ISD::BITCAST ||
7552 Ops[1].getOpcode() == ISD::BITCAST)) {
7555 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7556 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7557 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7558 N2.getValueType().isInteger()) {
7559 bool IsLE = getDataLayout().isLittleEndian();
7560 unsigned EltBits = VT.getScalarSizeInBits();
7561 SmallVector<APInt> RawBits1, RawBits2;
7562 BitVector UndefElts1, UndefElts2;
7563 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7564 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7565 SmallVector<APInt> RawBits;
7566 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7567 std::optional<APInt> Fold = FoldValueWithUndef(
7568 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7569 if (!Fold)
7570 break;
7571 RawBits.push_back(*Fold);
7572 }
7573 if (RawBits.size() == NumElts.getFixedValue()) {
7574 // We have constant folded, but we might need to cast this again back
7575 // to the original (possibly legalized) type.
7576 EVT BVVT, BVEltVT;
7577 if (N1.getValueType() == VT) {
7578 BVVT = N1.getValueType();
7579 BVEltVT = BV1->getOperand(0).getValueType();
7580 } else {
7581 BVVT = N2.getValueType();
7582 BVEltVT = BV2->getOperand(0).getValueType();
7583 }
7584 unsigned BVEltBits = BVEltVT.getSizeInBits();
7585 SmallVector<APInt> DstBits;
7586 BitVector DstUndefs;
7588 DstBits, RawBits, DstUndefs,
7589 BitVector(RawBits.size(), false));
7590 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7591 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7592 if (DstUndefs[I])
7593 continue;
7594 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7595 }
7596 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7597 }
7598 }
7599 }
7600 }
7601
7602 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7603 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7604 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7605 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7606 APInt RHSVal;
7607 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7608 APInt NewStep = Opcode == ISD::MUL
7609 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7610 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7611 return getStepVector(DL, VT, NewStep);
7612 }
7613 }
7614
7615 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7616 return !Op.getValueType().isVector() ||
7617 Op.getValueType().getVectorElementCount() == NumElts;
7618 };
7619
7620 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7621 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7622 Op.getOpcode() == ISD::BUILD_VECTOR ||
7623 Op.getOpcode() == ISD::SPLAT_VECTOR;
7624 };
7625
7626 // All operands must be vector types with the same number of elements as
7627 // the result type and must be either UNDEF or a build/splat vector
7628 // or UNDEF scalars.
7629 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7630 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7631 return SDValue();
7632
7633 // If we are comparing vectors, then the result needs to be a i1 boolean that
7634 // is then extended back to the legal result type depending on how booleans
7635 // are represented.
7636 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7637 ISD::NodeType ExtendCode =
7638 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7639 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7641
7642 // Find legal integer scalar type for constant promotion and
7643 // ensure that its scalar size is at least as large as source.
7644 EVT LegalSVT = VT.getScalarType();
7645 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7646 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7647 if (LegalSVT.bitsLT(VT.getScalarType()))
7648 return SDValue();
7649 }
7650
7651 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7652 // only have one operand to check. For fixed-length vector types we may have
7653 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7654 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7655
7656 // Constant fold each scalar lane separately.
7657 SmallVector<SDValue, 4> ScalarResults;
7658 for (unsigned I = 0; I != NumVectorElts; I++) {
7659 SmallVector<SDValue, 4> ScalarOps;
7660 for (SDValue Op : Ops) {
7661 EVT InSVT = Op.getValueType().getScalarType();
7662 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7663 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7664 if (Op.isUndef())
7665 ScalarOps.push_back(getUNDEF(InSVT));
7666 else
7667 ScalarOps.push_back(Op);
7668 continue;
7669 }
7670
7671 SDValue ScalarOp =
7672 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7673 EVT ScalarVT = ScalarOp.getValueType();
7674
7675 // Build vector (integer) scalar operands may need implicit
7676 // truncation - do this before constant folding.
7677 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7678 // Don't create illegally-typed nodes unless they're constants or undef
7679 // - if we fail to constant fold we can't guarantee the (dead) nodes
7680 // we're creating will be cleaned up before being visited for
7681 // legalization.
7682 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7683 !isa<ConstantSDNode>(ScalarOp) &&
7684 TLI->getTypeAction(*getContext(), InSVT) !=
7686 return SDValue();
7687 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7688 }
7689
7690 ScalarOps.push_back(ScalarOp);
7691 }
7692
7693 // Constant fold the scalar operands.
7694 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7695
7696 // Scalar folding only succeeded if the result is a constant or UNDEF.
7697 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7698 ScalarResult.getOpcode() != ISD::ConstantFP)
7699 return SDValue();
7700
7701 // Legalize the (integer) scalar constant if necessary. We only do
7702 // this once we know the folding succeeded, since otherwise we would
7703 // get a node with illegal type which has a user.
7704 if (LegalSVT != SVT)
7705 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7706
7707 ScalarResults.push_back(ScalarResult);
7708 }
7709
7710 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7711 : getBuildVector(VT, DL, ScalarResults);
7712 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7713 return V;
7714}
7715
7718 // TODO: Add support for unary/ternary fp opcodes.
7719 if (Ops.size() != 2)
7720 return SDValue();
7721
7722 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7723 // should. That will require dealing with a potentially non-default
7724 // rounding mode, checking the "opStatus" return value from the APFloat
7725 // math calculations, and possibly other variations.
7726 SDValue N1 = Ops[0];
7727 SDValue N2 = Ops[1];
7728 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7729 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7730 if (N1CFP && N2CFP) {
7731 APFloat C1 = N1CFP->getValueAPF(); // make copy
7732 const APFloat &C2 = N2CFP->getValueAPF();
7733 switch (Opcode) {
7734 case ISD::FADD:
7736 return getConstantFP(C1, DL, VT);
7737 case ISD::FSUB:
7739 return getConstantFP(C1, DL, VT);
7740 case ISD::FMUL:
7742 return getConstantFP(C1, DL, VT);
7743 case ISD::FDIV:
7745 return getConstantFP(C1, DL, VT);
7746 case ISD::FREM:
7747 C1.mod(C2);
7748 return getConstantFP(C1, DL, VT);
7749 case ISD::FCOPYSIGN:
7750 C1.copySign(C2);
7751 return getConstantFP(C1, DL, VT);
7752 case ISD::FMINNUM:
7753 return getConstantFP(minnum(C1, C2), DL, VT);
7754 case ISD::FMAXNUM:
7755 return getConstantFP(maxnum(C1, C2), DL, VT);
7756 case ISD::FMINIMUM:
7757 return getConstantFP(minimum(C1, C2), DL, VT);
7758 case ISD::FMAXIMUM:
7759 return getConstantFP(maximum(C1, C2), DL, VT);
7760 case ISD::FMINIMUMNUM:
7761 return getConstantFP(minimumnum(C1, C2), DL, VT);
7762 case ISD::FMAXIMUMNUM:
7763 return getConstantFP(maximumnum(C1, C2), DL, VT);
7764 default: break;
7765 }
7766 }
7767 if (N1CFP && Opcode == ISD::FP_ROUND) {
7768 APFloat C1 = N1CFP->getValueAPF(); // make copy
7769 bool Unused;
7770 // This can return overflow, underflow, or inexact; we don't care.
7771 // FIXME need to be more flexible about rounding mode.
7773 &Unused);
7774 return getConstantFP(C1, DL, VT);
7775 }
7776
7777 switch (Opcode) {
7778 case ISD::FSUB:
7779 // -0.0 - undef --> undef (consistent with "fneg undef")
7780 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7781 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7782 return getUNDEF(VT);
7783 [[fallthrough]];
7784
7785 case ISD::FADD:
7786 case ISD::FMUL:
7787 case ISD::FDIV:
7788 case ISD::FREM:
7789 // If both operands are undef, the result is undef. If 1 operand is undef,
7790 // the result is NaN. This should match the behavior of the IR optimizer.
7791 if (N1.isUndef() && N2.isUndef())
7792 return getUNDEF(VT);
7793 if (N1.isUndef() || N2.isUndef())
7795 }
7796 return SDValue();
7797}
7798
7800 const SDLoc &DL, EVT DstEltVT) {
7801 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7802
7803 // If this is already the right type, we're done.
7804 if (SrcEltVT == DstEltVT)
7805 return SDValue(BV, 0);
7806
7807 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7808 unsigned DstBitSize = DstEltVT.getSizeInBits();
7809
7810 // If this is a conversion of N elements of one type to N elements of another
7811 // type, convert each element. This handles FP<->INT cases.
7812 if (SrcBitSize == DstBitSize) {
7814 for (SDValue Op : BV->op_values()) {
7815 // If the vector element type is not legal, the BUILD_VECTOR operands
7816 // are promoted and implicitly truncated. Make that explicit here.
7817 if (Op.getValueType() != SrcEltVT)
7818 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7819 Ops.push_back(getBitcast(DstEltVT, Op));
7820 }
7821 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7823 return getBuildVector(VT, DL, Ops);
7824 }
7825
7826 // Otherwise, we're growing or shrinking the elements. To avoid having to
7827 // handle annoying details of growing/shrinking FP values, we convert them to
7828 // int first.
7829 if (SrcEltVT.isFloatingPoint()) {
7830 // Convert the input float vector to a int vector where the elements are the
7831 // same sizes.
7832 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7833 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7835 DstEltVT);
7836 return SDValue();
7837 }
7838
7839 // Now we know the input is an integer vector. If the output is a FP type,
7840 // convert to integer first, then to FP of the right size.
7841 if (DstEltVT.isFloatingPoint()) {
7842 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7843 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7845 DstEltVT);
7846 return SDValue();
7847 }
7848
7849 // Okay, we know the src/dst types are both integers of differing types.
7850 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7851
7852 // Extract the constant raw bit data.
7853 BitVector UndefElements;
7854 SmallVector<APInt> RawBits;
7855 bool IsLE = getDataLayout().isLittleEndian();
7856 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7857 return SDValue();
7858
7860 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7861 if (UndefElements[I])
7862 Ops.push_back(getUNDEF(DstEltVT));
7863 else
7864 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7865 }
7866
7867 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7868 return getBuildVector(VT, DL, Ops);
7869}
7870
7872 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7873
7874 // There's no need to assert on a byte-aligned pointer. All pointers are at
7875 // least byte aligned.
7876 if (A == Align(1))
7877 return Val;
7878
7879 SDVTList VTs = getVTList(Val.getValueType());
7881 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7882 ID.AddInteger(A.value());
7883
7884 void *IP = nullptr;
7885 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7886 return SDValue(E, 0);
7887
7888 auto *N =
7889 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7890 createOperands(N, {Val});
7891
7892 CSEMap.InsertNode(N, IP);
7893 InsertNode(N);
7894
7895 SDValue V(N, 0);
7896 NewSDValueDbgMsg(V, "Creating new node: ", this);
7897 return V;
7898}
7899
7900SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7901 SDValue N1, SDValue N2) {
7902 SDNodeFlags Flags;
7903 if (Inserter)
7904 Flags = Inserter->getFlags();
7905 return getNode(Opcode, DL, VT, N1, N2, Flags);
7906}
7907
7909 SDValue &N2) const {
7910 if (!TLI->isCommutativeBinOp(Opcode))
7911 return;
7912
7913 // Canonicalize:
7914 // binop(const, nonconst) -> binop(nonconst, const)
7917 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7918 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7919 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7920 std::swap(N1, N2);
7921
7922 // Canonicalize:
7923 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7924 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7926 std::swap(N1, N2);
7927}
7928
7929SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7930 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7932 N2.getOpcode() != ISD::DELETED_NODE &&
7933 "Operand is DELETED_NODE!");
7934
7935 canonicalizeCommutativeBinop(Opcode, N1, N2);
7936
7937 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7938 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7939
7940 // Don't allow undefs in vector splats - we might be returning N2 when folding
7941 // to zero etc.
7942 ConstantSDNode *N2CV =
7943 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7944
7945 switch (Opcode) {
7946 default: break;
7947 case ISD::TokenFactor:
7948 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7949 N2.getValueType() == MVT::Other && "Invalid token factor!");
7950 // Fold trivial token factors.
7951 if (N1.getOpcode() == ISD::EntryToken) return N2;
7952 if (N2.getOpcode() == ISD::EntryToken) return N1;
7953 if (N1 == N2) return N1;
7954 break;
7955 case ISD::BUILD_VECTOR: {
7956 // Attempt to simplify BUILD_VECTOR.
7957 SDValue Ops[] = {N1, N2};
7958 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7959 return V;
7960 break;
7961 }
7962 case ISD::CONCAT_VECTORS: {
7963 SDValue Ops[] = {N1, N2};
7964 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7965 return V;
7966 break;
7967 }
7968 case ISD::AND:
7969 assert(VT.isInteger() && "This operator does not apply to FP types!");
7970 assert(N1.getValueType() == N2.getValueType() &&
7971 N1.getValueType() == VT && "Binary operator types must match!");
7972 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7973 // worth handling here.
7974 if (N2CV && N2CV->isZero())
7975 return N2;
7976 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7977 return N1;
7978 break;
7979 case ISD::OR:
7980 case ISD::XOR:
7981 case ISD::ADD:
7982 case ISD::PTRADD:
7983 case ISD::SUB:
7984 assert(VT.isInteger() && "This operator does not apply to FP types!");
7985 assert(N1.getValueType() == N2.getValueType() &&
7986 N1.getValueType() == VT && "Binary operator types must match!");
7987 // The equal operand types requirement is unnecessarily strong for PTRADD.
7988 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7989 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7990 // logic everywhere where PTRADDs may be folded or combined to properly
7991 // support them. If/when we introduce pointer types to the SDAG, we will
7992 // need to relax this constraint.
7993
7994 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7995 // it's worth handling here.
7996 if (N2CV && N2CV->isZero())
7997 return N1;
7998 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7999 VT.getScalarType() == MVT::i1)
8000 return getNode(ISD::XOR, DL, VT, N1, N2);
8001 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
8002 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
8003 N2.getOpcode() == ISD::VSCALE) {
8004 const APInt &C1 = N1->getConstantOperandAPInt(0);
8005 const APInt &C2 = N2->getConstantOperandAPInt(0);
8006 return getVScale(DL, VT, C1 + C2);
8007 }
8008 break;
8009 case ISD::MUL:
8010 assert(VT.isInteger() && "This operator does not apply to FP types!");
8011 assert(N1.getValueType() == N2.getValueType() &&
8012 N1.getValueType() == VT && "Binary operator types must match!");
8013 if (VT.getScalarType() == MVT::i1)
8014 return getNode(ISD::AND, DL, VT, N1, N2);
8015 if (N2CV && N2CV->isZero())
8016 return N2;
8017 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8018 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8019 const APInt &N2CImm = N2C->getAPIntValue();
8020 return getVScale(DL, VT, MulImm * N2CImm);
8021 }
8022 break;
8023 case ISD::UDIV:
8024 case ISD::UREM:
8025 case ISD::MULHU:
8026 case ISD::MULHS:
8027 case ISD::SDIV:
8028 case ISD::SREM:
8029 case ISD::SADDSAT:
8030 case ISD::SSUBSAT:
8031 case ISD::UADDSAT:
8032 case ISD::USUBSAT:
8033 assert(VT.isInteger() && "This operator does not apply to FP types!");
8034 assert(N1.getValueType() == N2.getValueType() &&
8035 N1.getValueType() == VT && "Binary operator types must match!");
8036 if (VT.getScalarType() == MVT::i1) {
8037 // fold (add_sat x, y) -> (or x, y) for bool types.
8038 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
8039 return getNode(ISD::OR, DL, VT, N1, N2);
8040 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
8041 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
8042 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
8043 }
8044 break;
8045 case ISD::SCMP:
8046 case ISD::UCMP:
8047 assert(N1.getValueType() == N2.getValueType() &&
8048 "Types of operands of UCMP/SCMP must match");
8049 assert(N1.getValueType().isVector() == VT.isVector() &&
8050 "Operands and return type of must both be scalars or vectors");
8051 if (VT.isVector())
8054 "Result and operands must have the same number of elements");
8055 break;
8056 case ISD::AVGFLOORS:
8057 case ISD::AVGFLOORU:
8058 case ISD::AVGCEILS:
8059 case ISD::AVGCEILU:
8060 assert(VT.isInteger() && "This operator does not apply to FP types!");
8061 assert(N1.getValueType() == N2.getValueType() &&
8062 N1.getValueType() == VT && "Binary operator types must match!");
8063 break;
8064 case ISD::ABDS:
8065 case ISD::ABDU:
8066 assert(VT.isInteger() && "This operator does not apply to FP types!");
8067 assert(N1.getValueType() == N2.getValueType() &&
8068 N1.getValueType() == VT && "Binary operator types must match!");
8069 if (VT.getScalarType() == MVT::i1)
8070 return getNode(ISD::XOR, DL, VT, N1, N2);
8071 break;
8072 case ISD::SMIN:
8073 case ISD::UMAX:
8074 assert(VT.isInteger() && "This operator does not apply to FP types!");
8075 assert(N1.getValueType() == N2.getValueType() &&
8076 N1.getValueType() == VT && "Binary operator types must match!");
8077 if (VT.getScalarType() == MVT::i1)
8078 return getNode(ISD::OR, DL, VT, N1, N2);
8079 break;
8080 case ISD::SMAX:
8081 case ISD::UMIN:
8082 assert(VT.isInteger() && "This operator does not apply to FP types!");
8083 assert(N1.getValueType() == N2.getValueType() &&
8084 N1.getValueType() == VT && "Binary operator types must match!");
8085 if (VT.getScalarType() == MVT::i1)
8086 return getNode(ISD::AND, DL, VT, N1, N2);
8087 break;
8088 case ISD::FADD:
8089 case ISD::FSUB:
8090 case ISD::FMUL:
8091 case ISD::FDIV:
8092 case ISD::FREM:
8093 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
8094 assert(N1.getValueType() == N2.getValueType() &&
8095 N1.getValueType() == VT && "Binary operator types must match!");
8096 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
8097 return V;
8098 break;
8099 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
8100 assert(N1.getValueType() == VT &&
8103 "Invalid FCOPYSIGN!");
8104 break;
8105 case ISD::SHL:
8106 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
8107 const APInt &MulImm = N1->getConstantOperandAPInt(0);
8108 const APInt &ShiftImm = N2C->getAPIntValue();
8109 return getVScale(DL, VT, MulImm << ShiftImm);
8110 }
8111 [[fallthrough]];
8112 case ISD::SRA:
8113 case ISD::SRL:
8114 if (SDValue V = simplifyShift(N1, N2))
8115 return V;
8116 [[fallthrough]];
8117 case ISD::ROTL:
8118 case ISD::ROTR:
8119 case ISD::SSHLSAT:
8120 case ISD::USHLSAT:
8121 assert(VT == N1.getValueType() &&
8122 "Shift operators return type must be the same as their first arg");
8123 assert(VT.isInteger() && N2.getValueType().isInteger() &&
8124 "Shifts only work on integers");
8125 assert((!VT.isVector() || VT == N2.getValueType()) &&
8126 "Vector shift amounts must be in the same as their first arg");
8127 // Verify that the shift amount VT is big enough to hold valid shift
8128 // amounts. This catches things like trying to shift an i1024 value by an
8129 // i8, which is easy to fall into in generic code that uses
8130 // TLI.getShiftAmount().
8133 "Invalid use of small shift amount with oversized value!");
8134
8135 // Always fold shifts of i1 values so the code generator doesn't need to
8136 // handle them. Since we know the size of the shift has to be less than the
8137 // size of the value, the shift/rotate count is guaranteed to be zero.
8138 if (VT == MVT::i1)
8139 return N1;
8140 if (N2CV && N2CV->isZero())
8141 return N1;
8142 break;
8143 case ISD::FP_ROUND:
8145 VT.bitsLE(N1.getValueType()) && N2C &&
8146 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
8147 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
8148 if (N1.getValueType() == VT) return N1; // noop conversion.
8149 break;
8150 case ISD::AssertNoFPClass: {
8152 "AssertNoFPClass is used for a non-floating type");
8153 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
8154 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
8155 assert(llvm::to_underlying(NoFPClass) <=
8157 "FPClassTest value too large");
8158 (void)NoFPClass;
8159 break;
8160 }
8161 case ISD::AssertSext:
8162 case ISD::AssertZext: {
8163 EVT EVT = cast<VTSDNode>(N2)->getVT();
8164 assert(VT == N1.getValueType() && "Not an inreg extend!");
8165 assert(VT.isInteger() && EVT.isInteger() &&
8166 "Cannot *_EXTEND_INREG FP types");
8167 assert(!EVT.isVector() &&
8168 "AssertSExt/AssertZExt type should be the vector element type "
8169 "rather than the vector type!");
8170 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
8171 if (VT.getScalarType() == EVT) return N1; // noop assertion.
8172 break;
8173 }
8175 EVT EVT = cast<VTSDNode>(N2)->getVT();
8176 assert(VT == N1.getValueType() && "Not an inreg extend!");
8177 assert(VT.isInteger() && EVT.isInteger() &&
8178 "Cannot *_EXTEND_INREG FP types");
8179 assert(EVT.isVector() == VT.isVector() &&
8180 "SIGN_EXTEND_INREG type should be vector iff the operand "
8181 "type is vector!");
8182 assert((!EVT.isVector() ||
8184 "Vector element counts must match in SIGN_EXTEND_INREG");
8185 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
8186 if (EVT == VT) return N1; // Not actually extending
8187 break;
8188 }
8190 case ISD::FP_TO_UINT_SAT: {
8191 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
8192 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
8193 assert(N1.getValueType().isVector() == VT.isVector() &&
8194 "FP_TO_*INT_SAT type should be vector iff the operand type is "
8195 "vector!");
8196 assert((!VT.isVector() || VT.getVectorElementCount() ==
8198 "Vector element counts must match in FP_TO_*INT_SAT");
8199 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
8200 "Type to saturate to must be a scalar.");
8201 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
8202 "Not extending!");
8203 break;
8204 }
8207 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
8208 element type of the vector.");
8209
8210 // Extract from an undefined value or using an undefined index is undefined.
8211 if (N1.isUndef() || N2.isUndef())
8212 return getUNDEF(VT);
8213
8214 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
8215 // vectors. For scalable vectors we will provide appropriate support for
8216 // dealing with arbitrary indices.
8217 if (N2C && N1.getValueType().isFixedLengthVector() &&
8218 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8219 return getUNDEF(VT);
8220
8221 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8222 // expanding copies of large vectors from registers. This only works for
8223 // fixed length vectors, since we need to know the exact number of
8224 // elements.
8225 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8227 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8228 return getExtractVectorElt(DL, VT,
8229 N1.getOperand(N2C->getZExtValue() / Factor),
8230 N2C->getZExtValue() % Factor);
8231 }
8232
8233 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8234 // lowering is expanding large vector constants.
8235 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8236 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8239 "BUILD_VECTOR used for scalable vectors");
8240 unsigned Index =
8241 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8242 SDValue Elt = N1.getOperand(Index);
8243
8244 if (VT != Elt.getValueType())
8245 // If the vector element type is not legal, the BUILD_VECTOR operands
8246 // are promoted and implicitly truncated, and the result implicitly
8247 // extended. Make that explicit here.
8248 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8249
8250 return Elt;
8251 }
8252
8253 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8254 // operations are lowered to scalars.
8255 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8256 // If the indices are the same, return the inserted element else
8257 // if the indices are known different, extract the element from
8258 // the original vector.
8259 SDValue N1Op2 = N1.getOperand(2);
8261
8262 if (N1Op2C && N2C) {
8263 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8264 if (VT == N1.getOperand(1).getValueType())
8265 return N1.getOperand(1);
8266 if (VT.isFloatingPoint()) {
8268 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8269 }
8270 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8271 }
8272 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8273 }
8274 }
8275
8276 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8277 // when vector types are scalarized and v1iX is legal.
8278 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8279 // Here we are completely ignoring the extract element index (N2),
8280 // which is fine for fixed width vectors, since any index other than 0
8281 // is undefined anyway. However, this cannot be ignored for scalable
8282 // vectors - in theory we could support this, but we don't want to do this
8283 // without a profitability check.
8284 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8286 N1.getValueType().getVectorNumElements() == 1) {
8287 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8288 N1.getOperand(1));
8289 }
8290 break;
8292 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8293 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8294 (N1.getValueType().isInteger() == VT.isInteger()) &&
8295 N1.getValueType() != VT &&
8296 "Wrong types for EXTRACT_ELEMENT!");
8297
8298 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8299 // 64-bit integers into 32-bit parts. Instead of building the extract of
8300 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8301 if (N1.getOpcode() == ISD::BUILD_PAIR)
8302 return N1.getOperand(N2C->getZExtValue());
8303
8304 // EXTRACT_ELEMENT of a constant int is also very common.
8305 if (N1C) {
8306 unsigned ElementSize = VT.getSizeInBits();
8307 unsigned Shift = ElementSize * N2C->getZExtValue();
8308 const APInt &Val = N1C->getAPIntValue();
8309 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8310 }
8311 break;
8313 EVT N1VT = N1.getValueType();
8314 assert(VT.isVector() && N1VT.isVector() &&
8315 "Extract subvector VTs must be vectors!");
8317 "Extract subvector VTs must have the same element type!");
8318 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8319 "Cannot extract a scalable vector from a fixed length vector!");
8320 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8322 "Extract subvector must be from larger vector to smaller vector!");
8323 assert(N2C && "Extract subvector index must be a constant");
8324 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8325 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8326 N1VT.getVectorMinNumElements()) &&
8327 "Extract subvector overflow!");
8328 assert(N2C->getAPIntValue().getBitWidth() ==
8329 TLI->getVectorIdxWidth(getDataLayout()) &&
8330 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8331 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8332 "Extract index is not a multiple of the output vector length");
8333
8334 // Trivial extraction.
8335 if (VT == N1VT)
8336 return N1;
8337
8338 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8339 if (N1.isUndef())
8340 return getUNDEF(VT);
8341
8342 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8343 // the concat have the same type as the extract.
8344 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8345 VT == N1.getOperand(0).getValueType()) {
8346 unsigned Factor = VT.getVectorMinNumElements();
8347 return N1.getOperand(N2C->getZExtValue() / Factor);
8348 }
8349
8350 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8351 // during shuffle legalization.
8352 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8353 VT == N1.getOperand(1).getValueType())
8354 return N1.getOperand(1);
8355 break;
8356 }
8357 }
8358
8359 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8360 switch (Opcode) {
8361 case ISD::XOR:
8362 case ISD::ADD:
8363 case ISD::PTRADD:
8364 case ISD::SUB:
8366 case ISD::UDIV:
8367 case ISD::SDIV:
8368 case ISD::UREM:
8369 case ISD::SREM:
8370 case ISD::MUL:
8371 case ISD::AND:
8372 case ISD::SSUBSAT:
8373 case ISD::USUBSAT:
8374 case ISD::UMIN:
8375 case ISD::OR:
8376 case ISD::SADDSAT:
8377 case ISD::UADDSAT:
8378 case ISD::UMAX:
8379 case ISD::SMAX:
8380 case ISD::SMIN:
8381 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8382 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8383 }
8384 }
8385
8386 // Canonicalize an UNDEF to the RHS, even over a constant.
8387 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8388 if (TLI->isCommutativeBinOp(Opcode)) {
8389 std::swap(N1, N2);
8390 } else {
8391 switch (Opcode) {
8392 case ISD::PTRADD:
8393 case ISD::SUB:
8394 // fold op(undef, non_undef_arg2) -> undef.
8395 return N1;
8397 case ISD::UDIV:
8398 case ISD::SDIV:
8399 case ISD::UREM:
8400 case ISD::SREM:
8401 case ISD::SSUBSAT:
8402 case ISD::USUBSAT:
8403 // fold op(undef, non_undef_arg2) -> 0.
8404 return getConstant(0, DL, VT);
8405 }
8406 }
8407 }
8408
8409 // Fold a bunch of operators when the RHS is undef.
8410 if (N2.getOpcode() == ISD::UNDEF) {
8411 switch (Opcode) {
8412 case ISD::XOR:
8413 if (N1.getOpcode() == ISD::UNDEF)
8414 // Handle undef ^ undef -> 0 special case. This is a common
8415 // idiom (misuse).
8416 return getConstant(0, DL, VT);
8417 [[fallthrough]];
8418 case ISD::ADD:
8419 case ISD::PTRADD:
8420 case ISD::SUB:
8421 // fold op(arg1, undef) -> undef.
8422 return N2;
8423 case ISD::UDIV:
8424 case ISD::SDIV:
8425 case ISD::UREM:
8426 case ISD::SREM:
8427 // fold op(arg1, undef) -> poison.
8428 return getPOISON(VT);
8429 case ISD::MUL:
8430 case ISD::AND:
8431 case ISD::SSUBSAT:
8432 case ISD::USUBSAT:
8433 case ISD::UMIN:
8434 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8435 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8436 case ISD::OR:
8437 case ISD::SADDSAT:
8438 case ISD::UADDSAT:
8439 case ISD::UMAX:
8440 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8441 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8442 case ISD::SMAX:
8443 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8444 return N1.getOpcode() == ISD::UNDEF
8445 ? N2
8446 : getConstant(
8448 VT);
8449 case ISD::SMIN:
8450 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8451 return N1.getOpcode() == ISD::UNDEF
8452 ? N2
8453 : getConstant(
8455 VT);
8456 }
8457 }
8458
8459 // Perform trivial constant folding.
8460 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8461 return SV;
8462
8463 // Memoize this node if possible.
8464 SDNode *N;
8465 SDVTList VTs = getVTList(VT);
8466 SDValue Ops[] = {N1, N2};
8467 if (VT != MVT::Glue) {
8469 AddNodeIDNode(ID, Opcode, VTs, Ops);
8470 void *IP = nullptr;
8471 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8472 E->intersectFlagsWith(Flags);
8473 return SDValue(E, 0);
8474 }
8475
8476 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8477 N->setFlags(Flags);
8478 createOperands(N, Ops);
8479 CSEMap.InsertNode(N, IP);
8480 } else {
8481 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8482 createOperands(N, Ops);
8483 }
8484
8485 InsertNode(N);
8486 SDValue V = SDValue(N, 0);
8487 NewSDValueDbgMsg(V, "Creating new node: ", this);
8488 return V;
8489}
8490
8491SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8492 SDValue N1, SDValue N2, SDValue N3) {
8493 SDNodeFlags Flags;
8494 if (Inserter)
8495 Flags = Inserter->getFlags();
8496 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8497}
8498
8499SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8500 SDValue N1, SDValue N2, SDValue N3,
8501 const SDNodeFlags Flags) {
8503 N2.getOpcode() != ISD::DELETED_NODE &&
8504 N3.getOpcode() != ISD::DELETED_NODE &&
8505 "Operand is DELETED_NODE!");
8506 // Perform various simplifications.
8507 switch (Opcode) {
8508 case ISD::BUILD_VECTOR: {
8509 // Attempt to simplify BUILD_VECTOR.
8510 SDValue Ops[] = {N1, N2, N3};
8511 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8512 return V;
8513 break;
8514 }
8515 case ISD::CONCAT_VECTORS: {
8516 SDValue Ops[] = {N1, N2, N3};
8517 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8518 return V;
8519 break;
8520 }
8521 case ISD::SETCC: {
8522 assert(VT.isInteger() && "SETCC result type must be an integer!");
8523 assert(N1.getValueType() == N2.getValueType() &&
8524 "SETCC operands must have the same type!");
8525 assert(VT.isVector() == N1.getValueType().isVector() &&
8526 "SETCC type should be vector iff the operand type is vector!");
8527 assert((!VT.isVector() || VT.getVectorElementCount() ==
8529 "SETCC vector element counts must match!");
8530 // Use FoldSetCC to simplify SETCC's.
8531 if (SDValue V =
8532 FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL, Flags))
8533 return V;
8534 break;
8535 }
8536 case ISD::SELECT:
8537 case ISD::VSELECT:
8538 if (SDValue V = simplifySelect(N1, N2, N3))
8539 return V;
8540 break;
8542 llvm_unreachable("should use getVectorShuffle constructor!");
8544 if (isNullConstant(N3))
8545 return N1;
8546 break;
8548 if (isNullConstant(N3))
8549 return N2;
8550 break;
8552 assert(VT.isVector() && VT == N1.getValueType() &&
8553 "INSERT_VECTOR_ELT vector type mismatch");
8555 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8556 assert((!VT.isFloatingPoint() ||
8557 VT.getVectorElementType() == N2.getValueType()) &&
8558 "INSERT_VECTOR_ELT fp scalar type mismatch");
8559 assert((!VT.isInteger() ||
8561 "INSERT_VECTOR_ELT int scalar size mismatch");
8562
8563 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8564 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8565 // for scalable vectors where we will generate appropriate code to
8566 // deal with out-of-bounds cases correctly.
8567 if (N3C && VT.isFixedLengthVector() &&
8568 N3C->getZExtValue() >= VT.getVectorNumElements())
8569 return getUNDEF(VT);
8570
8571 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8572 if (N3.isUndef())
8573 return getUNDEF(VT);
8574
8575 // If inserting poison, just use the input vector.
8576 if (N2.getOpcode() == ISD::POISON)
8577 return N1;
8578
8579 // Inserting undef into undef/poison is still undef.
8580 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8581 return getUNDEF(VT);
8582
8583 // If the inserted element is an UNDEF, just use the input vector.
8584 // But not if skipping the insert could make the result more poisonous.
8585 if (N2.isUndef()) {
8586 if (N3C && VT.isFixedLengthVector()) {
8587 APInt EltMask =
8588 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8589 if (isGuaranteedNotToBePoison(N1, EltMask))
8590 return N1;
8591 } else if (isGuaranteedNotToBePoison(N1))
8592 return N1;
8593 }
8594 break;
8595 }
8596 case ISD::INSERT_SUBVECTOR: {
8597 // If inserting poison, just use the input vector,
8598 if (N2.getOpcode() == ISD::POISON)
8599 return N1;
8600
8601 // Inserting undef into undef/poison is still undef.
8602 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8603 return getUNDEF(VT);
8604
8605 EVT N2VT = N2.getValueType();
8606 assert(VT == N1.getValueType() &&
8607 "Dest and insert subvector source types must match!");
8608 assert(VT.isVector() && N2VT.isVector() &&
8609 "Insert subvector VTs must be vectors!");
8611 "Insert subvector VTs must have the same element type!");
8612 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8613 "Cannot insert a scalable vector into a fixed length vector!");
8614 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8616 "Insert subvector must be from smaller vector to larger vector!");
8618 "Insert subvector index must be constant");
8619 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8620 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8622 "Insert subvector overflow!");
8624 TLI->getVectorIdxWidth(getDataLayout()) &&
8625 "Constant index for INSERT_SUBVECTOR has an invalid size");
8626
8627 // Trivial insertion.
8628 if (VT == N2VT)
8629 return N2;
8630
8631 // If this is an insert of an extracted vector into an undef/poison vector,
8632 // we can just use the input to the extract. But not if skipping the
8633 // extract+insert could make the result more poisonous.
8634 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8635 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8636 if (N1.getOpcode() == ISD::POISON)
8637 return N2.getOperand(0);
8638 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8639 unsigned LoBit = N3->getAsZExtVal();
8640 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8641 APInt EltMask =
8642 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8643 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8644 return N2.getOperand(0);
8645 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8646 return N2.getOperand(0);
8647 }
8648
8649 // If the inserted subvector is UNDEF, just use the input vector.
8650 // But not if skipping the insert could make the result more poisonous.
8651 if (N2.isUndef()) {
8652 if (VT.isFixedLengthVector()) {
8653 unsigned LoBit = N3->getAsZExtVal();
8654 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8655 APInt EltMask =
8656 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8657 if (isGuaranteedNotToBePoison(N1, EltMask))
8658 return N1;
8659 } else if (isGuaranteedNotToBePoison(N1))
8660 return N1;
8661 }
8662 break;
8663 }
8664 case ISD::BITCAST:
8665 // Fold bit_convert nodes from a type to themselves.
8666 if (N1.getValueType() == VT)
8667 return N1;
8668 break;
8669 case ISD::VP_TRUNCATE:
8670 case ISD::VP_SIGN_EXTEND:
8671 case ISD::VP_ZERO_EXTEND:
8672 // Don't create noop casts.
8673 if (N1.getValueType() == VT)
8674 return N1;
8675 break;
8676 case ISD::VECTOR_COMPRESS: {
8677 [[maybe_unused]] EVT VecVT = N1.getValueType();
8678 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8679 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8680 assert(VT == VecVT && "Vector and result type don't match.");
8681 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8682 "All inputs must be vectors.");
8683 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8685 "Vector and mask must have same number of elements.");
8686
8687 if (N1.isUndef() || N2.isUndef())
8688 return N3;
8689
8690 break;
8691 }
8696 [[maybe_unused]] EVT AccVT = N1.getValueType();
8697 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8698 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8699 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8700 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8701 "node to have the same type!");
8702 assert(VT.isVector() && VT == AccVT &&
8703 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8704 "the same type as its result!");
8706 AccVT.getVectorElementCount()) &&
8707 "Expected the element count of the second and third operands of the "
8708 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8709 "element count of the first operand and the result!");
8711 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8712 "node to have an element type which is the same as or smaller than "
8713 "the element type of the first operand and result!");
8714 break;
8715 }
8716 }
8717
8718 // Perform trivial constant folding for arithmetic operators.
8719 switch (Opcode) {
8720 case ISD::FMA:
8721 case ISD::FMAD:
8722 case ISD::SETCC:
8723 case ISD::FSHL:
8724 case ISD::FSHR:
8725 if (SDValue SV =
8726 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8727 return SV;
8728 break;
8729 }
8730
8731 // Memoize node if it doesn't produce a glue result.
8732 SDNode *N;
8733 SDVTList VTs = getVTList(VT);
8734 SDValue Ops[] = {N1, N2, N3};
8735 if (VT != MVT::Glue) {
8737 AddNodeIDNode(ID, Opcode, VTs, Ops);
8738 void *IP = nullptr;
8739 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8740 E->intersectFlagsWith(Flags);
8741 return SDValue(E, 0);
8742 }
8743
8744 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8745 N->setFlags(Flags);
8746 createOperands(N, Ops);
8747 CSEMap.InsertNode(N, IP);
8748 } else {
8749 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8750 createOperands(N, Ops);
8751 }
8752
8753 InsertNode(N);
8754 SDValue V = SDValue(N, 0);
8755 NewSDValueDbgMsg(V, "Creating new node: ", this);
8756 return V;
8757}
8758
8759SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8760 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8761 const SDNodeFlags Flags) {
8762 SDValue Ops[] = { N1, N2, N3, N4 };
8763 return getNode(Opcode, DL, VT, Ops, Flags);
8764}
8765
8766SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8767 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8768 SDNodeFlags Flags;
8769 if (Inserter)
8770 Flags = Inserter->getFlags();
8771 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8772}
8773
8774SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8775 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8776 SDValue N5, const SDNodeFlags Flags) {
8777 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8778 return getNode(Opcode, DL, VT, Ops, Flags);
8779}
8780
8781SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8782 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8783 SDValue N5) {
8784 SDNodeFlags Flags;
8785 if (Inserter)
8786 Flags = Inserter->getFlags();
8787 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8788}
8789
8790/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8791/// the incoming stack arguments to be loaded from the stack.
8793 SmallVector<SDValue, 8> ArgChains;
8794
8795 // Include the original chain at the beginning of the list. When this is
8796 // used by target LowerCall hooks, this helps legalize find the
8797 // CALLSEQ_BEGIN node.
8798 ArgChains.push_back(Chain);
8799
8800 // Add a chain value for each stack argument.
8801 for (SDNode *U : getEntryNode().getNode()->users())
8802 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8803 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8804 if (FI->getIndex() < 0)
8805 ArgChains.push_back(SDValue(L, 1));
8806
8807 // Build a tokenfactor for all the chains.
8808 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8809}
8810
8811/// getMemsetValue - Vectorized representation of the memset value
8812/// operand.
8814 const SDLoc &dl) {
8815 assert(!Value.isUndef());
8816
8817 unsigned NumBits = VT.getScalarSizeInBits();
8819 assert(C->getAPIntValue().getBitWidth() == 8);
8820 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8821 if (VT.isInteger()) {
8822 bool IsOpaque = VT.getSizeInBits() > 64 ||
8823 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8824 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8825 }
8826 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8827 }
8828
8829 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8830 EVT IntVT = VT.getScalarType();
8831 if (!IntVT.isInteger())
8832 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8833
8834 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8835 if (NumBits > 8) {
8836 // Use a multiplication with 0x010101... to extend the input to the
8837 // required length.
8838 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8839 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8840 DAG.getConstant(Magic, dl, IntVT));
8841 }
8842
8843 if (VT != Value.getValueType() && !VT.isInteger())
8844 Value = DAG.getBitcast(VT.getScalarType(), Value);
8845 if (VT != Value.getValueType())
8846 Value = DAG.getSplatBuildVector(VT, dl, Value);
8847
8848 return Value;
8849}
8850
8851/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8852/// used when a memcpy is turned into a memset when the source is a constant
8853/// string ptr.
8855 const TargetLowering &TLI,
8856 const ConstantDataArraySlice &Slice) {
8857 // Handle vector with all elements zero.
8858 if (Slice.Array == nullptr) {
8859 if (VT.isInteger())
8860 return DAG.getConstant(0, dl, VT);
8861 return DAG.getNode(ISD::BITCAST, dl, VT,
8862 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8863 }
8864
8865 assert(!VT.isVector() && "Can't handle vector type here!");
8866 unsigned NumVTBits = VT.getSizeInBits();
8867 unsigned NumVTBytes = NumVTBits / 8;
8868 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8869
8870 APInt Val(NumVTBits, 0);
8871 if (DAG.getDataLayout().isLittleEndian()) {
8872 for (unsigned i = 0; i != NumBytes; ++i)
8873 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8874 } else {
8875 for (unsigned i = 0; i != NumBytes; ++i)
8876 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8877 }
8878
8879 // If the "cost" of materializing the integer immediate is less than the cost
8880 // of a load, then it is cost effective to turn the load into the immediate.
8881 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8882 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8883 return DAG.getConstant(Val, dl, VT);
8884 return SDValue();
8885}
8886
8888 const SDLoc &DL,
8889 const SDNodeFlags Flags) {
8890 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8891 return getMemBasePlusOffset(Base, Index, DL, Flags);
8892}
8893
8895 const SDLoc &DL,
8896 const SDNodeFlags Flags) {
8897 assert(Offset.getValueType().isInteger());
8898 EVT BasePtrVT = Ptr.getValueType();
8899 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8900 BasePtrVT))
8901 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8902 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8903 SDNodeFlags AddFlags = Flags;
8904 AddFlags.setInBounds(false);
8905 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8906}
8907
8908/// Returns true if memcpy source is constant data.
8910 uint64_t SrcDelta = 0;
8911 GlobalAddressSDNode *G = nullptr;
8912 if (Src.getOpcode() == ISD::GlobalAddress)
8914 else if (Src->isAnyAdd() &&
8915 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8916 Src.getOperand(1).getOpcode() == ISD::Constant) {
8917 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8918 SrcDelta = Src.getConstantOperandVal(1);
8919 }
8920 if (!G)
8921 return false;
8922
8923 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8924 SrcDelta + G->getOffset());
8925}
8926
8928 SelectionDAG &DAG) {
8929 // On Darwin, -Os means optimize for size without hurting performance, so
8930 // only really optimize for size when -Oz (MinSize) is used.
8932 return MF.getFunction().hasMinSize();
8933 return DAG.shouldOptForSize();
8934}
8935
8937 SmallVector<SDValue, 32> &OutChains, unsigned From,
8938 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8939 SmallVector<SDValue, 16> &OutStoreChains) {
8940 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8941 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8942 SmallVector<SDValue, 16> GluedLoadChains;
8943 for (unsigned i = From; i < To; ++i) {
8944 OutChains.push_back(OutLoadChains[i]);
8945 GluedLoadChains.push_back(OutLoadChains[i]);
8946 }
8947
8948 // Chain for all loads.
8949 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8950 GluedLoadChains);
8951
8952 for (unsigned i = From; i < To; ++i) {
8953 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8954 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8955 ST->getBasePtr(), ST->getMemoryVT(),
8956 ST->getMemOperand());
8957 OutChains.push_back(NewStore);
8958 }
8959}
8960
8962 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8963 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8964 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8965 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8966 // Turn a memcpy of undef to nop.
8967 // FIXME: We need to honor volatile even is Src is undef.
8968 if (Src.isUndef())
8969 return Chain;
8970
8971 // Expand memcpy to a series of load and store ops if the size operand falls
8972 // below a certain threshold.
8973 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8974 // rather than maybe a humongous number of loads and stores.
8975 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8976 const DataLayout &DL = DAG.getDataLayout();
8977 LLVMContext &C = *DAG.getContext();
8978 std::vector<EVT> MemOps;
8979 bool DstAlignCanChange = false;
8981 MachineFrameInfo &MFI = MF.getFrameInfo();
8982 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8984 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8985 DstAlignCanChange = true;
8986 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8987 if (!SrcAlign || Alignment > *SrcAlign)
8988 SrcAlign = Alignment;
8989 assert(SrcAlign && "SrcAlign must be set");
8991 // If marked as volatile, perform a copy even when marked as constant.
8992 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8993 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8994 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8995 const MemOp Op = isZeroConstant
8996 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8997 /*IsZeroMemset*/ true, isVol)
8998 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8999 *SrcAlign, isVol, CopyFromConstant);
9000 if (!TLI.findOptimalMemOpLowering(
9001 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
9002 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
9003 return SDValue();
9004
9005 if (DstAlignCanChange) {
9006 Type *Ty = MemOps[0].getTypeForEVT(C);
9007 Align NewAlign = DL.getABITypeAlign(Ty);
9008
9009 // Don't promote to an alignment that would require dynamic stack
9010 // realignment which may conflict with optimizations such as tail call
9011 // optimization.
9013 if (!TRI->hasStackRealignment(MF))
9014 if (MaybeAlign StackAlign = DL.getStackAlignment())
9015 NewAlign = std::min(NewAlign, *StackAlign);
9016
9017 if (NewAlign > Alignment) {
9018 // Give the stack frame object a larger alignment if needed.
9019 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9020 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9021 Alignment = NewAlign;
9022 }
9023 }
9024
9025 // Prepare AAInfo for loads/stores after lowering this memcpy.
9026 AAMDNodes NewAAInfo = AAInfo;
9027 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9028
9029 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
9030 bool isConstant =
9031 BatchAA && SrcVal &&
9032 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
9033
9034 MachineMemOperand::Flags MMOFlags =
9036 SmallVector<SDValue, 16> OutLoadChains;
9037 SmallVector<SDValue, 16> OutStoreChains;
9038 SmallVector<SDValue, 32> OutChains;
9039 unsigned NumMemOps = MemOps.size();
9040 uint64_t SrcOff = 0, DstOff = 0;
9041 for (unsigned i = 0; i != NumMemOps; ++i) {
9042 EVT VT = MemOps[i];
9043 unsigned VTSize = VT.getSizeInBits() / 8;
9044 SDValue Value, Store;
9045
9046 if (VTSize > Size) {
9047 // Issuing an unaligned load / store pair that overlaps with the previous
9048 // pair. Adjust the offset accordingly.
9049 assert(i == NumMemOps-1 && i != 0);
9050 SrcOff -= VTSize - Size;
9051 DstOff -= VTSize - Size;
9052 }
9053
9054 if (CopyFromConstant &&
9055 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
9056 // It's unlikely a store of a vector immediate can be done in a single
9057 // instruction. It would require a load from a constantpool first.
9058 // We only handle zero vectors here.
9059 // FIXME: Handle other cases where store of vector immediate is done in
9060 // a single instruction.
9061 ConstantDataArraySlice SubSlice;
9062 if (SrcOff < Slice.Length) {
9063 SubSlice = Slice;
9064 SubSlice.move(SrcOff);
9065 } else {
9066 // This is an out-of-bounds access and hence UB. Pretend we read zero.
9067 SubSlice.Array = nullptr;
9068 SubSlice.Offset = 0;
9069 SubSlice.Length = VTSize;
9070 }
9071 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
9072 if (Value.getNode()) {
9073 Store = DAG.getStore(
9074 Chain, dl, Value,
9075 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9076 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9077 OutChains.push_back(Store);
9078 }
9079 }
9080
9081 if (!Store.getNode()) {
9082 // The type might not be legal for the target. This should only happen
9083 // if the type is smaller than a legal type, as on PPC, so the right
9084 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
9085 // to Load/Store if NVT==VT.
9086 // FIXME does the case above also need this?
9087 EVT NVT = TLI.getTypeToTransformTo(C, VT);
9088 assert(NVT.bitsGE(VT));
9089
9090 bool isDereferenceable =
9091 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9092 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9093 if (isDereferenceable)
9095 if (isConstant)
9096 SrcMMOFlags |= MachineMemOperand::MOInvariant;
9097
9098 Value = DAG.getExtLoad(
9099 ISD::EXTLOAD, dl, NVT, Chain,
9100 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9101 SrcPtrInfo.getWithOffset(SrcOff), VT,
9102 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
9103 OutLoadChains.push_back(Value.getValue(1));
9104
9105 Store = DAG.getTruncStore(
9106 Chain, dl, Value,
9107 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9108 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
9109 OutStoreChains.push_back(Store);
9110 }
9111 SrcOff += VTSize;
9112 DstOff += VTSize;
9113 Size -= VTSize;
9114 }
9115
9116 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
9118 unsigned NumLdStInMemcpy = OutStoreChains.size();
9119
9120 if (NumLdStInMemcpy) {
9121 // It may be that memcpy might be converted to memset if it's memcpy
9122 // of constants. In such a case, we won't have loads and stores, but
9123 // just stores. In the absence of loads, there is nothing to gang up.
9124 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
9125 // If target does not care, just leave as it.
9126 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
9127 OutChains.push_back(OutLoadChains[i]);
9128 OutChains.push_back(OutStoreChains[i]);
9129 }
9130 } else {
9131 // Ld/St less than/equal limit set by target.
9132 if (NumLdStInMemcpy <= GluedLdStLimit) {
9133 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
9134 NumLdStInMemcpy, OutLoadChains,
9135 OutStoreChains);
9136 } else {
9137 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
9138 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
9139 unsigned GlueIter = 0;
9140
9141 // Residual ld/st.
9142 if (RemainingLdStInMemcpy) {
9144 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
9145 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
9146 }
9147
9148 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
9149 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
9150 GlueIter - GluedLdStLimit;
9151 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
9152 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
9153 OutLoadChains, OutStoreChains);
9154 GlueIter += GluedLdStLimit;
9155 }
9156 }
9157 }
9158 }
9159 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9160}
9161
9163 SDValue Chain, SDValue Dst, SDValue Src,
9164 uint64_t Size, Align Alignment,
9165 bool isVol, bool AlwaysInline,
9166 MachinePointerInfo DstPtrInfo,
9167 MachinePointerInfo SrcPtrInfo,
9168 const AAMDNodes &AAInfo) {
9169 // Turn a memmove of undef to nop.
9170 // FIXME: We need to honor volatile even is Src is undef.
9171 if (Src.isUndef())
9172 return Chain;
9173
9174 // Expand memmove to a series of load and store ops if the size operand falls
9175 // below a certain threshold.
9176 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9177 const DataLayout &DL = DAG.getDataLayout();
9178 LLVMContext &C = *DAG.getContext();
9179 std::vector<EVT> MemOps;
9180 bool DstAlignCanChange = false;
9182 MachineFrameInfo &MFI = MF.getFrameInfo();
9183 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9185 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9186 DstAlignCanChange = true;
9187 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
9188 if (!SrcAlign || Alignment > *SrcAlign)
9189 SrcAlign = Alignment;
9190 assert(SrcAlign && "SrcAlign must be set");
9191 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
9192 if (!TLI.findOptimalMemOpLowering(
9193 C, MemOps, Limit,
9194 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
9195 /*IsVolatile*/ true),
9196 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
9197 MF.getFunction().getAttributes(), nullptr))
9198 return SDValue();
9199
9200 if (DstAlignCanChange) {
9201 Type *Ty = MemOps[0].getTypeForEVT(C);
9202 Align NewAlign = DL.getABITypeAlign(Ty);
9203
9204 // Don't promote to an alignment that would require dynamic stack
9205 // realignment which may conflict with optimizations such as tail call
9206 // optimization.
9208 if (!TRI->hasStackRealignment(MF))
9209 if (MaybeAlign StackAlign = DL.getStackAlignment())
9210 NewAlign = std::min(NewAlign, *StackAlign);
9211
9212 if (NewAlign > Alignment) {
9213 // Give the stack frame object a larger alignment if needed.
9214 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9215 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9216 Alignment = NewAlign;
9217 }
9218 }
9219
9220 // Prepare AAInfo for loads/stores after lowering this memmove.
9221 AAMDNodes NewAAInfo = AAInfo;
9222 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9223
9224 MachineMemOperand::Flags MMOFlags =
9226 uint64_t SrcOff = 0, DstOff = 0;
9227 SmallVector<SDValue, 8> LoadValues;
9228 SmallVector<SDValue, 8> LoadChains;
9229 SmallVector<SDValue, 8> OutChains;
9230 unsigned NumMemOps = MemOps.size();
9231 for (unsigned i = 0; i < NumMemOps; i++) {
9232 EVT VT = MemOps[i];
9233 unsigned VTSize = VT.getSizeInBits() / 8;
9234 SDValue Value;
9235
9236 bool isDereferenceable =
9237 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9238 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9239 if (isDereferenceable)
9241
9242 Value = DAG.getLoad(
9243 VT, dl, Chain,
9244 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9245 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
9246 LoadValues.push_back(Value);
9247 LoadChains.push_back(Value.getValue(1));
9248 SrcOff += VTSize;
9249 }
9250 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9251 OutChains.clear();
9252 for (unsigned i = 0; i < NumMemOps; i++) {
9253 EVT VT = MemOps[i];
9254 unsigned VTSize = VT.getSizeInBits() / 8;
9255 SDValue Store;
9256
9257 Store = DAG.getStore(
9258 Chain, dl, LoadValues[i],
9259 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9260 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9261 OutChains.push_back(Store);
9262 DstOff += VTSize;
9263 }
9264
9265 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9266}
9267
9268/// Lower the call to 'memset' intrinsic function into a series of store
9269/// operations.
9270///
9271/// \param DAG Selection DAG where lowered code is placed.
9272/// \param dl Link to corresponding IR location.
9273/// \param Chain Control flow dependency.
9274/// \param Dst Pointer to destination memory location.
9275/// \param Src Value of byte to write into the memory.
9276/// \param Size Number of bytes to write.
9277/// \param Alignment Alignment of the destination in bytes.
9278/// \param isVol True if destination is volatile.
9279/// \param AlwaysInline Makes sure no function call is generated.
9280/// \param DstPtrInfo IR information on the memory pointer.
9281/// \returns New head in the control flow, if lowering was successful, empty
9282/// SDValue otherwise.
9283///
9284/// The function tries to replace 'llvm.memset' intrinsic with several store
9285/// operations and value calculation code. This is usually profitable for small
9286/// memory size or when the semantic requires inlining.
9288 SDValue Chain, SDValue Dst, SDValue Src,
9289 uint64_t Size, Align Alignment, bool isVol,
9290 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9291 const AAMDNodes &AAInfo) {
9292 // Turn a memset of undef to nop.
9293 // FIXME: We need to honor volatile even is Src is undef.
9294 if (Src.isUndef())
9295 return Chain;
9296
9297 // Expand memset to a series of load/store ops if the size operand
9298 // falls below a certain threshold.
9299 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9300 std::vector<EVT> MemOps;
9301 bool DstAlignCanChange = false;
9302 LLVMContext &C = *DAG.getContext();
9304 MachineFrameInfo &MFI = MF.getFrameInfo();
9305 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9307 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9308 DstAlignCanChange = true;
9309 bool IsZeroVal = isNullConstant(Src);
9310 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9311
9312 EVT LargestVT;
9313 if (!TLI.findOptimalMemOpLowering(
9314 C, MemOps, Limit,
9315 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9316 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9317 &LargestVT))
9318 return SDValue();
9319
9320 if (DstAlignCanChange) {
9321 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9322 const DataLayout &DL = DAG.getDataLayout();
9323 Align NewAlign = DL.getABITypeAlign(Ty);
9324
9325 // Don't promote to an alignment that would require dynamic stack
9326 // realignment which may conflict with optimizations such as tail call
9327 // optimization.
9329 if (!TRI->hasStackRealignment(MF))
9330 if (MaybeAlign StackAlign = DL.getStackAlignment())
9331 NewAlign = std::min(NewAlign, *StackAlign);
9332
9333 if (NewAlign > Alignment) {
9334 // Give the stack frame object a larger alignment if needed.
9335 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9336 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9337 Alignment = NewAlign;
9338 }
9339 }
9340
9341 SmallVector<SDValue, 8> OutChains;
9342 uint64_t DstOff = 0;
9343 unsigned NumMemOps = MemOps.size();
9344
9345 // Find the largest store and generate the bit pattern for it.
9346 // If target didn't set LargestVT, compute it from MemOps.
9347 if (!LargestVT.isSimple()) {
9348 LargestVT = MemOps[0];
9349 for (unsigned i = 1; i < NumMemOps; i++)
9350 if (MemOps[i].bitsGT(LargestVT))
9351 LargestVT = MemOps[i];
9352 }
9353 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9354
9355 // Prepare AAInfo for loads/stores after lowering this memset.
9356 AAMDNodes NewAAInfo = AAInfo;
9357 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9358
9359 for (unsigned i = 0; i < NumMemOps; i++) {
9360 EVT VT = MemOps[i];
9361 unsigned VTSize = VT.getSizeInBits() / 8;
9362 // The target should specify store types that exactly cover the memset size
9363 // (with the last store potentially being oversized for overlapping stores).
9364 assert(Size > 0 && "Target specified more stores than needed in "
9365 "findOptimalMemOpLowering");
9366 if (VTSize > Size) {
9367 // Issuing an unaligned load / store pair that overlaps with the previous
9368 // pair. Adjust the offset accordingly.
9369 assert(i == NumMemOps-1 && i != 0);
9370 DstOff -= VTSize - Size;
9371 }
9372
9373 // If this store is smaller than the largest store see whether we can get
9374 // the smaller value for free with a truncate or extract vector element and
9375 // then store.
9376 SDValue Value = MemSetValue;
9377 if (VT.bitsLT(LargestVT)) {
9378 unsigned Index;
9379 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9380 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9381 if (!LargestVT.isVector() && !VT.isVector() &&
9382 TLI.isTruncateFree(LargestVT, VT))
9383 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9384 else if (LargestVT.isVector() && !VT.isVector() &&
9386 LargestVT.getTypeForEVT(*DAG.getContext()),
9387 VT.getSizeInBits(), Index) &&
9388 TLI.isTypeLegal(SVT) &&
9389 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9390 // Target which can combine store(extractelement VectorTy, Idx) can get
9391 // the smaller value for free.
9392 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9393 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9394 } else
9395 Value = getMemsetValue(Src, VT, DAG, dl);
9396 }
9397 assert(Value.getValueType() == VT && "Value with wrong type.");
9398 SDValue Store = DAG.getStore(
9399 Chain, dl, Value,
9400 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9401 DstPtrInfo.getWithOffset(DstOff), Alignment,
9403 NewAAInfo);
9404 OutChains.push_back(Store);
9405 DstOff += VT.getSizeInBits() / 8;
9406 // For oversized overlapping stores, only subtract the remaining bytes.
9407 // For normal stores, subtract the full store size.
9408 if (VTSize > Size) {
9409 Size = 0;
9410 } else {
9411 Size -= VTSize;
9412 }
9413 }
9414
9415 // After processing all stores, Size should be exactly 0. Any remaining bytes
9416 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9417 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9418 "stores that exactly cover the memset size");
9419
9420 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9421}
9422
9424 unsigned AS) {
9425 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9426 // pointer operands can be losslessly bitcasted to pointers of address space 0
9427 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9428 report_fatal_error("cannot lower memory intrinsic in address space " +
9429 Twine(AS));
9430 }
9431}
9432
9434 const SelectionDAG *SelDAG,
9435 bool AllowReturnsFirstArg) {
9436 if (!CI || !CI->isTailCall())
9437 return false;
9438 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9439 // helper symbol we lower to.
9440 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9441 AllowReturnsFirstArg &&
9443}
9444
9445static std::pair<SDValue, SDValue>
9448 const CallInst *CI, RTLIB::Libcall Call,
9449 SelectionDAG *DAG, const TargetLowering *TLI) {
9450 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9451
9452 if (LCImpl == RTLIB::Unsupported)
9453 return {};
9454
9456 bool IsTailCall =
9457 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9458 SDValue Callee =
9459 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9460
9461 CLI.setDebugLoc(dl)
9462 .setChain(Chain)
9464 CI->getType(), Callee, std::move(Args))
9465 .setTailCall(IsTailCall);
9466
9467 return TLI->LowerCallTo(CLI);
9468}
9469
9470std::pair<SDValue, SDValue> SelectionDAG::getStrcmp(SDValue Chain,
9471 const SDLoc &dl, SDValue S1,
9472 SDValue S2,
9473 const CallInst *CI) {
9475 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9476 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9477 RTLIB::STRCMP, this, TLI);
9478}
9479
9480std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9481 const SDLoc &dl, SDValue S1,
9482 SDValue S2,
9483 const CallInst *CI) {
9485 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9486 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9487 RTLIB::STRSTR, this, TLI);
9488}
9489
9490std::pair<SDValue, SDValue> SelectionDAG::getMemccpy(SDValue Chain,
9491 const SDLoc &dl,
9492 SDValue Dst, SDValue Src,
9494 const CallInst *CI) {
9496
9498 {Dst, PT},
9499 {Src, PT},
9502 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9503 RTLIB::MEMCCPY, this, TLI);
9504}
9505
9506std::pair<SDValue, SDValue>
9508 SDValue Mem1, SDValue Size, const CallInst *CI) {
9511 {Mem0, PT},
9512 {Mem1, PT},
9514 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9515 RTLIB::MEMCMP, this, TLI);
9516}
9517
9518std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9519 const SDLoc &dl,
9520 SDValue Dst, SDValue Src,
9521 const CallInst *CI) {
9523 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9524 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9525 RTLIB::STRCPY, this, TLI);
9526}
9527
9528std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9529 const SDLoc &dl,
9530 SDValue Src,
9531 const CallInst *CI) {
9532 // Emit a library call.
9535 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9536 RTLIB::STRLEN, this, TLI);
9537}
9538
9540 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9541 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9542 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9543 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9544 BatchAAResults *BatchAA) {
9545 // Check to see if we should lower the memcpy to loads and stores first.
9546 // For cases within the target-specified limits, this is the best choice.
9548 if (ConstantSize) {
9549 // Memcpy with size zero? Just return the original chain.
9550 if (ConstantSize->isZero())
9551 return Chain;
9552
9554 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9555 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9556 if (Result.getNode())
9557 return Result;
9558 }
9559
9560 // Then check to see if we should lower the memcpy with target-specific
9561 // code. If the target chooses to do this, this is the next best.
9562 if (TSI) {
9563 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9564 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9565 DstPtrInfo, SrcPtrInfo);
9566 if (Result.getNode())
9567 return Result;
9568 }
9569
9570 // If we really need inline code and the target declined to provide it,
9571 // use a (potentially long) sequence of loads and stores.
9572 if (AlwaysInline) {
9573 assert(ConstantSize && "AlwaysInline requires a constant size!");
9575 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9576 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9577 }
9578
9581
9582 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9583 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9584 // respect volatile, so they may do things like read or write memory
9585 // beyond the given memory regions. But fixing this isn't easy, and most
9586 // people don't care.
9587
9588 // Emit a library call.
9591 Args.emplace_back(Dst, PtrTy);
9592 Args.emplace_back(Src, PtrTy);
9593 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9594 // FIXME: pass in SDLoc
9596 bool IsTailCall = false;
9597 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9598
9599 if (OverrideTailCall.has_value()) {
9600 IsTailCall = *OverrideTailCall;
9601 } else {
9602 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9603 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9604 }
9605
9606 CLI.setDebugLoc(dl)
9607 .setChain(Chain)
9608 .setLibCallee(
9609 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9610 Dst.getValueType().getTypeForEVT(*getContext()),
9611 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9612 std::move(Args))
9614 .setTailCall(IsTailCall);
9615
9616 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9617 return CallResult.second;
9618}
9619
9621 SDValue Dst, SDValue Src, SDValue Size,
9622 Type *SizeTy, unsigned ElemSz,
9623 bool isTailCall,
9624 MachinePointerInfo DstPtrInfo,
9625 MachinePointerInfo SrcPtrInfo) {
9626 // Emit a library call.
9629 Args.emplace_back(Dst, ArgTy);
9630 Args.emplace_back(Src, ArgTy);
9631 Args.emplace_back(Size, SizeTy);
9632
9633 RTLIB::Libcall LibraryCall =
9635 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9636 if (LibcallImpl == RTLIB::Unsupported)
9637 report_fatal_error("Unsupported element size");
9638
9640 CLI.setDebugLoc(dl)
9641 .setChain(Chain)
9642 .setLibCallee(
9643 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9645 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9646 std::move(Args))
9648 .setTailCall(isTailCall);
9649
9650 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9651 return CallResult.second;
9652}
9653
9655 SDValue Src, SDValue Size, Align Alignment,
9656 bool isVol, const CallInst *CI,
9657 std::optional<bool> OverrideTailCall,
9658 MachinePointerInfo DstPtrInfo,
9659 MachinePointerInfo SrcPtrInfo,
9660 const AAMDNodes &AAInfo,
9661 BatchAAResults *BatchAA) {
9662 // Check to see if we should lower the memmove to loads and stores first.
9663 // For cases within the target-specified limits, this is the best choice.
9665 if (ConstantSize) {
9666 // Memmove with size zero? Just return the original chain.
9667 if (ConstantSize->isZero())
9668 return Chain;
9669
9671 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9672 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9673 if (Result.getNode())
9674 return Result;
9675 }
9676
9677 // Then check to see if we should lower the memmove with target-specific
9678 // code. If the target chooses to do this, this is the next best.
9679 if (TSI) {
9680 SDValue Result =
9681 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9682 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9683 if (Result.getNode())
9684 return Result;
9685 }
9686
9689
9690 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9691 // not be safe. See memcpy above for more details.
9692
9693 // Emit a library call.
9696 Args.emplace_back(Dst, PtrTy);
9697 Args.emplace_back(Src, PtrTy);
9698 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9699 // FIXME: pass in SDLoc
9701
9702 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9703
9704 bool IsTailCall = false;
9705 if (OverrideTailCall.has_value()) {
9706 IsTailCall = *OverrideTailCall;
9707 } else {
9708 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9709 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9710 }
9711
9712 CLI.setDebugLoc(dl)
9713 .setChain(Chain)
9714 .setLibCallee(
9715 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9716 Dst.getValueType().getTypeForEVT(*getContext()),
9717 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9718 std::move(Args))
9720 .setTailCall(IsTailCall);
9721
9722 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9723 return CallResult.second;
9724}
9725
9727 SDValue Dst, SDValue Src, SDValue Size,
9728 Type *SizeTy, unsigned ElemSz,
9729 bool isTailCall,
9730 MachinePointerInfo DstPtrInfo,
9731 MachinePointerInfo SrcPtrInfo) {
9732 // Emit a library call.
9734 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9735 Args.emplace_back(Dst, IntPtrTy);
9736 Args.emplace_back(Src, IntPtrTy);
9737 Args.emplace_back(Size, SizeTy);
9738
9739 RTLIB::Libcall LibraryCall =
9741 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9742 if (LibcallImpl == RTLIB::Unsupported)
9743 report_fatal_error("Unsupported element size");
9744
9746 CLI.setDebugLoc(dl)
9747 .setChain(Chain)
9748 .setLibCallee(
9749 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9751 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9752 std::move(Args))
9754 .setTailCall(isTailCall);
9755
9756 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9757 return CallResult.second;
9758}
9759
9761 SDValue Src, SDValue Size, Align Alignment,
9762 bool isVol, bool AlwaysInline,
9763 const CallInst *CI,
9764 MachinePointerInfo DstPtrInfo,
9765 const AAMDNodes &AAInfo) {
9766 // Check to see if we should lower the memset to stores first.
9767 // For cases within the target-specified limits, this is the best choice.
9769 if (ConstantSize) {
9770 // Memset with size zero? Just return the original chain.
9771 if (ConstantSize->isZero())
9772 return Chain;
9773
9774 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9775 ConstantSize->getZExtValue(), Alignment,
9776 isVol, false, DstPtrInfo, AAInfo);
9777
9778 if (Result.getNode())
9779 return Result;
9780 }
9781
9782 // Then check to see if we should lower the memset with target-specific
9783 // code. If the target chooses to do this, this is the next best.
9784 if (TSI) {
9785 SDValue Result = TSI->EmitTargetCodeForMemset(
9786 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9787 if (Result.getNode())
9788 return Result;
9789 }
9790
9791 // If we really need inline code and the target declined to provide it,
9792 // use a (potentially long) sequence of loads and stores.
9793 if (AlwaysInline) {
9794 assert(ConstantSize && "AlwaysInline requires a constant size!");
9795 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9796 ConstantSize->getZExtValue(), Alignment,
9797 isVol, true, DstPtrInfo, AAInfo);
9798 assert(Result &&
9799 "getMemsetStores must return a valid sequence when AlwaysInline");
9800 return Result;
9801 }
9802
9804
9805 // Emit a library call.
9806 auto &Ctx = *getContext();
9807 const auto& DL = getDataLayout();
9808
9810 // FIXME: pass in SDLoc
9811 CLI.setDebugLoc(dl).setChain(Chain);
9812
9813 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
9814 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9815
9816 // If zeroing out and bzero is present, use it.
9817 if (UseBZero) {
9819 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9820 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9821 CLI.setLibCallee(
9822 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9823 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9824 } else {
9825 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9826
9828 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9829 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9830 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9831 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
9832 Dst.getValueType().getTypeForEVT(Ctx),
9833 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9834 std::move(Args));
9835 }
9836
9837 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9838 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9839
9840 // If we're going to use bzero, make sure not to tail call unless the
9841 // subsequent return doesn't need a value, as bzero doesn't return the first
9842 // arg unlike memset.
9843 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9844 bool IsTailCall =
9845 CI && CI->isTailCall() &&
9846 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9847 CLI.setDiscardResult().setTailCall(IsTailCall);
9848
9849 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9850 return CallResult.second;
9851}
9852
9855 Type *SizeTy, unsigned ElemSz,
9856 bool isTailCall,
9857 MachinePointerInfo DstPtrInfo) {
9858 // Emit a library call.
9860 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9861 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9862 Args.emplace_back(Size, SizeTy);
9863
9864 RTLIB::Libcall LibraryCall =
9866 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9867 if (LibcallImpl == RTLIB::Unsupported)
9868 report_fatal_error("Unsupported element size");
9869
9871 CLI.setDebugLoc(dl)
9872 .setChain(Chain)
9873 .setLibCallee(
9874 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9876 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9877 std::move(Args))
9879 .setTailCall(isTailCall);
9880
9881 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9882 return CallResult.second;
9883}
9884
9885SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9887 MachineMemOperand *MMO,
9888 ISD::LoadExtType ExtType) {
9890 AddNodeIDNode(ID, Opcode, VTList, Ops);
9891 ID.AddInteger(MemVT.getRawBits());
9892 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9893 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9894 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9895 ID.AddInteger(MMO->getFlags());
9896 void* IP = nullptr;
9897 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9898 E->refineAlignment(MMO);
9899 E->refineRanges(MMO);
9900 return SDValue(E, 0);
9901 }
9902
9903 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9904 VTList, MemVT, MMO, ExtType);
9905 createOperands(N, Ops);
9906
9907 CSEMap.InsertNode(N, IP);
9908 InsertNode(N);
9909 SDValue V(N, 0);
9910 NewSDValueDbgMsg(V, "Creating new node: ", this);
9911 return V;
9912}
9913
9915 EVT MemVT, SDVTList VTs, SDValue Chain,
9916 SDValue Ptr, SDValue Cmp, SDValue Swp,
9917 MachineMemOperand *MMO) {
9918 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9920 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9921
9922 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9923 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9924}
9925
9926SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9927 SDValue Chain, SDValue Ptr, SDValue Val,
9928 MachineMemOperand *MMO) {
9929 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9930 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9931 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9932 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9933 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9934 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9935 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9936 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9937 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9938 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9939 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9940 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9941 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9942 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9943 Opcode == ISD::ATOMIC_STORE) &&
9944 "Invalid Atomic Op");
9945
9946 EVT VT = Val.getValueType();
9947
9948 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9949 getVTList(VT, MVT::Other);
9950 SDValue Ops[] = {Chain, Ptr, Val};
9951 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9952}
9953
9955 EVT MemVT, EVT VT, SDValue Chain,
9956 SDValue Ptr, MachineMemOperand *MMO) {
9957 SDVTList VTs = getVTList(VT, MVT::Other);
9958 SDValue Ops[] = {Chain, Ptr};
9959 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9960}
9961
9962/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9964 if (Ops.size() == 1)
9965 return Ops[0];
9966
9968 VTs.reserve(Ops.size());
9969 for (const SDValue &Op : Ops)
9970 VTs.push_back(Op.getValueType());
9971 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9972}
9973
9975 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9976 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9978 const AAMDNodes &AAInfo) {
9979 if (Size.hasValue() && !Size.getValue())
9981
9983 MachineMemOperand *MMO =
9984 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9985
9986 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9987}
9988
9990 SDVTList VTList,
9991 ArrayRef<SDValue> Ops, EVT MemVT,
9992 MachineMemOperand *MMO) {
9993 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, ArrayRef(MMO));
9994}
9995
9997 SDVTList VTList,
9998 ArrayRef<SDValue> Ops, EVT MemVT,
10000 assert(!MMOs.empty() && "Must have at least one MMO");
10001 assert(
10002 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
10003 Opcode == ISD::PREFETCH ||
10004 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
10005 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
10006 "Opcode is not a memory-accessing opcode!");
10007
10009 if (MMOs.size() == 1) {
10010 MemRefs = MMOs[0];
10011 } else {
10012 // Allocate: [size_t count][MMO*][MMO*]...
10013 size_t AllocSize =
10014 sizeof(size_t) + MMOs.size() * sizeof(MachineMemOperand *);
10015 void *Buffer = Allocator.Allocate(AllocSize, alignof(size_t));
10016 size_t *CountPtr = static_cast<size_t *>(Buffer);
10017 *CountPtr = MMOs.size();
10018 MachineMemOperand **Array =
10019 reinterpret_cast<MachineMemOperand **>(CountPtr + 1);
10020 llvm::copy(MMOs, Array);
10021 MemRefs = Array;
10022 }
10023
10024 // Memoize the node unless it returns a glue result.
10026 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
10028 AddNodeIDNode(ID, Opcode, VTList, Ops);
10029 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
10030 Opcode, dl.getIROrder(), VTList, MemVT, MemRefs));
10031 ID.AddInteger(MemVT.getRawBits());
10032 for (const MachineMemOperand *MMO : MMOs) {
10033 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10034 ID.AddInteger(MMO->getFlags());
10035 }
10036 void *IP = nullptr;
10037 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10038 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMOs);
10039 return SDValue(E, 0);
10040 }
10041
10042 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10043 VTList, MemVT, MemRefs);
10044 createOperands(N, Ops);
10045 CSEMap.InsertNode(N, IP);
10046 } else {
10047 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
10048 VTList, MemVT, MemRefs);
10049 createOperands(N, Ops);
10050 }
10051 InsertNode(N);
10052 SDValue V(N, 0);
10053 NewSDValueDbgMsg(V, "Creating new node: ", this);
10054 return V;
10055}
10056
10058 SDValue Chain, int FrameIndex) {
10059 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
10060 const auto VTs = getVTList(MVT::Other);
10061 SDValue Ops[2] = {
10062 Chain,
10063 getFrameIndex(FrameIndex,
10064 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
10065 true)};
10066
10068 AddNodeIDNode(ID, Opcode, VTs, Ops);
10069 ID.AddInteger(FrameIndex);
10070 void *IP = nullptr;
10071 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10072 return SDValue(E, 0);
10073
10074 LifetimeSDNode *N =
10075 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
10076 createOperands(N, Ops);
10077 CSEMap.InsertNode(N, IP);
10078 InsertNode(N);
10079 SDValue V(N, 0);
10080 NewSDValueDbgMsg(V, "Creating new node: ", this);
10081 return V;
10082}
10083
10085 uint64_t Guid, uint64_t Index,
10086 uint32_t Attr) {
10087 const unsigned Opcode = ISD::PSEUDO_PROBE;
10088 const auto VTs = getVTList(MVT::Other);
10089 SDValue Ops[] = {Chain};
10091 AddNodeIDNode(ID, Opcode, VTs, Ops);
10092 ID.AddInteger(Guid);
10093 ID.AddInteger(Index);
10094 void *IP = nullptr;
10095 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
10096 return SDValue(E, 0);
10097
10098 auto *N = newSDNode<PseudoProbeSDNode>(
10099 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
10100 createOperands(N, Ops);
10101 CSEMap.InsertNode(N, IP);
10102 InsertNode(N);
10103 SDValue V(N, 0);
10104 NewSDValueDbgMsg(V, "Creating new node: ", this);
10105 return V;
10106}
10107
10108/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10109/// MachinePointerInfo record from it. This is particularly useful because the
10110/// code generator has many cases where it doesn't bother passing in a
10111/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10113 SelectionDAG &DAG, SDValue Ptr,
10114 int64_t Offset = 0) {
10115 // If this is FI+Offset, we can model it.
10116 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
10118 FI->getIndex(), Offset);
10119
10120 // If this is (FI+Offset1)+Offset2, we can model it.
10121 if (Ptr.getOpcode() != ISD::ADD ||
10124 return Info;
10125
10126 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
10128 DAG.getMachineFunction(), FI,
10129 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
10130}
10131
10132/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
10133/// MachinePointerInfo record from it. This is particularly useful because the
10134/// code generator has many cases where it doesn't bother passing in a
10135/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
10137 SelectionDAG &DAG, SDValue Ptr,
10138 SDValue OffsetOp) {
10139 // If the 'Offset' value isn't a constant, we can't handle this.
10141 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
10142 if (OffsetOp.isUndef())
10143 return InferPointerInfo(Info, DAG, Ptr);
10144 return Info;
10145}
10146
10148 EVT VT, const SDLoc &dl, SDValue Chain,
10149 SDValue Ptr, SDValue Offset,
10150 MachinePointerInfo PtrInfo, EVT MemVT,
10151 Align Alignment,
10152 MachineMemOperand::Flags MMOFlags,
10153 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10154 assert(Chain.getValueType() == MVT::Other &&
10155 "Invalid chain type");
10156
10157 MMOFlags |= MachineMemOperand::MOLoad;
10158 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10159 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10160 // clients.
10161 if (PtrInfo.V.isNull())
10162 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10163
10164 TypeSize Size = MemVT.getStoreSize();
10166 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10167 Alignment, AAInfo, Ranges);
10168 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
10169}
10170
10172 EVT VT, const SDLoc &dl, SDValue Chain,
10173 SDValue Ptr, SDValue Offset, EVT MemVT,
10174 MachineMemOperand *MMO) {
10175 if (VT == MemVT) {
10176 ExtType = ISD::NON_EXTLOAD;
10177 } else if (ExtType == ISD::NON_EXTLOAD) {
10178 assert(VT == MemVT && "Non-extending load from different memory type!");
10179 } else {
10180 // Extending load.
10181 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
10182 "Should only be an extending load, not truncating!");
10183 assert(VT.isInteger() == MemVT.isInteger() &&
10184 "Cannot convert from FP to Int or Int -> FP!");
10185 assert(VT.isVector() == MemVT.isVector() &&
10186 "Cannot use an ext load to convert to or from a vector!");
10187 assert((!VT.isVector() ||
10189 "Cannot use an ext load to change the number of vector elements!");
10190 }
10191
10192 assert((!MMO->getRanges() ||
10194 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
10195 MemVT.isInteger())) &&
10196 "Range metadata and load type must match!");
10197
10198 bool Indexed = AM != ISD::UNINDEXED;
10199 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10200
10201 SDVTList VTs = Indexed ?
10202 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
10203 SDValue Ops[] = { Chain, Ptr, Offset };
10205 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
10206 ID.AddInteger(MemVT.getRawBits());
10207 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
10208 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
10209 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10210 ID.AddInteger(MMO->getFlags());
10211 void *IP = nullptr;
10212 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10213 E->refineAlignment(MMO);
10214 E->refineRanges(MMO);
10215 return SDValue(E, 0);
10216 }
10217 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10218 ExtType, MemVT, MMO);
10219 createOperands(N, Ops);
10220
10221 CSEMap.InsertNode(N, IP);
10222 InsertNode(N);
10223 SDValue V(N, 0);
10224 NewSDValueDbgMsg(V, "Creating new node: ", this);
10225 return V;
10226}
10227
10229 SDValue Ptr, MachinePointerInfo PtrInfo,
10230 MaybeAlign Alignment,
10231 MachineMemOperand::Flags MMOFlags,
10232 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10234 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10235 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10236}
10237
10239 SDValue Ptr, MachineMemOperand *MMO) {
10241 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10242 VT, MMO);
10243}
10244
10246 EVT VT, SDValue Chain, SDValue Ptr,
10247 MachinePointerInfo PtrInfo, EVT MemVT,
10248 MaybeAlign Alignment,
10249 MachineMemOperand::Flags MMOFlags,
10250 const AAMDNodes &AAInfo) {
10252 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10253 MemVT, Alignment, MMOFlags, AAInfo);
10254}
10255
10257 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10258 MachineMemOperand *MMO) {
10260 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10261 MemVT, MMO);
10262}
10263
10267 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10268 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10269 // Don't propagate the invariant or dereferenceable flags.
10270 auto MMOFlags =
10271 LD->getMemOperand()->getFlags() &
10273 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10274 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10275 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10276}
10277
10279 SDValue Ptr, MachinePointerInfo PtrInfo,
10280 Align Alignment,
10281 MachineMemOperand::Flags MMOFlags,
10282 const AAMDNodes &AAInfo) {
10283 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10284
10285 MMOFlags |= MachineMemOperand::MOStore;
10286 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10287
10288 if (PtrInfo.V.isNull())
10289 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10290
10293 MachineMemOperand *MMO =
10294 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10295 return getStore(Chain, dl, Val, Ptr, MMO);
10296}
10297
10299 SDValue Ptr, MachineMemOperand *MMO) {
10301 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10303}
10304
10306 SDValue Ptr, SDValue Offset, EVT SVT,
10308 bool IsTruncating) {
10309 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10310 EVT VT = Val.getValueType();
10311 if (VT == SVT) {
10312 IsTruncating = false;
10313 } else if (!IsTruncating) {
10314 assert(VT == SVT && "No-truncating store from different memory type!");
10315 } else {
10317 "Should only be a truncating store, not extending!");
10318 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10319 assert(VT.isVector() == SVT.isVector() &&
10320 "Cannot use trunc store to convert to or from a vector!");
10321 assert((!VT.isVector() ||
10323 "Cannot use trunc store to change the number of vector elements!");
10324 }
10325
10326 bool Indexed = AM != ISD::UNINDEXED;
10327 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10328 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10329 : getVTList(MVT::Other);
10330 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10333 ID.AddInteger(SVT.getRawBits());
10334 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10335 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10336 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10337 ID.AddInteger(MMO->getFlags());
10338 void *IP = nullptr;
10339 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10340 cast<StoreSDNode>(E)->refineAlignment(MMO);
10341 return SDValue(E, 0);
10342 }
10343 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10344 IsTruncating, SVT, MMO);
10345 createOperands(N, Ops);
10346
10347 CSEMap.InsertNode(N, IP);
10348 InsertNode(N);
10349 SDValue V(N, 0);
10350 NewSDValueDbgMsg(V, "Creating new node: ", this);
10351 return V;
10352}
10353
10355 SDValue Ptr, MachinePointerInfo PtrInfo,
10356 EVT SVT, Align Alignment,
10357 MachineMemOperand::Flags MMOFlags,
10358 const AAMDNodes &AAInfo) {
10359 assert(Chain.getValueType() == MVT::Other &&
10360 "Invalid chain type");
10361
10362 MMOFlags |= MachineMemOperand::MOStore;
10363 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10364
10365 if (PtrInfo.V.isNull())
10366 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10367
10369 MachineMemOperand *MMO = MF.getMachineMemOperand(
10370 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10371 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10372}
10373
10375 SDValue Ptr, EVT SVT,
10376 MachineMemOperand *MMO) {
10378 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10379}
10380
10384 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10385 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10386 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10387 ST->getMemoryVT(), ST->getMemOperand(), AM,
10388 ST->isTruncatingStore());
10389}
10390
10392 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10393 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10394 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10395 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10396 const MDNode *Ranges, bool IsExpanding) {
10397 MMOFlags |= MachineMemOperand::MOLoad;
10398 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10399 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10400 // clients.
10401 if (PtrInfo.V.isNull())
10402 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10403
10404 TypeSize Size = MemVT.getStoreSize();
10406 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10407 Alignment, AAInfo, Ranges);
10408 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10409 MMO, IsExpanding);
10410}
10411
10413 ISD::LoadExtType ExtType, EVT VT,
10414 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10415 SDValue Offset, SDValue Mask, SDValue EVL,
10416 EVT MemVT, MachineMemOperand *MMO,
10417 bool IsExpanding) {
10418 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10419 assert(Mask.getValueType().getVectorElementCount() ==
10420 VT.getVectorElementCount() &&
10421 "Vector width mismatch between mask and data");
10422
10423 bool Indexed = AM != ISD::UNINDEXED;
10424 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10425
10426 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10427 : getVTList(VT, MVT::Other);
10428 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10430 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10431 ID.AddInteger(MemVT.getRawBits());
10432 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10433 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10434 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10435 ID.AddInteger(MMO->getFlags());
10436 void *IP = nullptr;
10437 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10438 E->refineAlignment(MMO);
10439 E->refineRanges(MMO);
10440 return SDValue(E, 0);
10441 }
10442 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10443 ExtType, IsExpanding, MemVT, MMO);
10444 createOperands(N, Ops);
10445
10446 CSEMap.InsertNode(N, IP);
10447 InsertNode(N);
10448 SDValue V(N, 0);
10449 NewSDValueDbgMsg(V, "Creating new node: ", this);
10450 return V;
10451}
10452
10454 SDValue Ptr, SDValue Mask, SDValue EVL,
10455 MachinePointerInfo PtrInfo,
10456 MaybeAlign Alignment,
10457 MachineMemOperand::Flags MMOFlags,
10458 const AAMDNodes &AAInfo, const MDNode *Ranges,
10459 bool IsExpanding) {
10461 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10462 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10463 IsExpanding);
10464}
10465
10467 SDValue Ptr, SDValue Mask, SDValue EVL,
10468 MachineMemOperand *MMO, bool IsExpanding) {
10470 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10471 Mask, EVL, VT, MMO, IsExpanding);
10472}
10473
10475 EVT VT, SDValue Chain, SDValue Ptr,
10476 SDValue Mask, SDValue EVL,
10477 MachinePointerInfo PtrInfo, EVT MemVT,
10478 MaybeAlign Alignment,
10479 MachineMemOperand::Flags MMOFlags,
10480 const AAMDNodes &AAInfo, bool IsExpanding) {
10482 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10483 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10484 IsExpanding);
10485}
10486
10488 EVT VT, SDValue Chain, SDValue Ptr,
10489 SDValue Mask, SDValue EVL, EVT MemVT,
10490 MachineMemOperand *MMO, bool IsExpanding) {
10492 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10493 EVL, MemVT, MMO, IsExpanding);
10494}
10495
10499 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10500 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10501 // Don't propagate the invariant or dereferenceable flags.
10502 auto MMOFlags =
10503 LD->getMemOperand()->getFlags() &
10505 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10506 LD->getChain(), Base, Offset, LD->getMask(),
10507 LD->getVectorLength(), LD->getPointerInfo(),
10508 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10509 nullptr, LD->isExpandingLoad());
10510}
10511
10513 SDValue Ptr, SDValue Offset, SDValue Mask,
10514 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10515 ISD::MemIndexedMode AM, bool IsTruncating,
10516 bool IsCompressing) {
10517 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10518 assert(Mask.getValueType().getVectorElementCount() ==
10520 "Vector width mismatch between mask and data");
10521
10522 bool Indexed = AM != ISD::UNINDEXED;
10523 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10524 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10525 : getVTList(MVT::Other);
10526 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10528 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10529 ID.AddInteger(MemVT.getRawBits());
10530 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10531 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10532 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10533 ID.AddInteger(MMO->getFlags());
10534 void *IP = nullptr;
10535 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10536 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10537 return SDValue(E, 0);
10538 }
10539 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10540 IsTruncating, IsCompressing, MemVT, MMO);
10541 createOperands(N, Ops);
10542
10543 CSEMap.InsertNode(N, IP);
10544 InsertNode(N);
10545 SDValue V(N, 0);
10546 NewSDValueDbgMsg(V, "Creating new node: ", this);
10547 return V;
10548}
10549
10551 SDValue Val, SDValue Ptr, SDValue Mask,
10552 SDValue EVL, MachinePointerInfo PtrInfo,
10553 EVT SVT, Align Alignment,
10554 MachineMemOperand::Flags MMOFlags,
10555 const AAMDNodes &AAInfo,
10556 bool IsCompressing) {
10557 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10558
10559 MMOFlags |= MachineMemOperand::MOStore;
10560 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10561
10562 if (PtrInfo.V.isNull())
10563 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10564
10566 MachineMemOperand *MMO = MF.getMachineMemOperand(
10567 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10568 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10569 IsCompressing);
10570}
10571
10573 SDValue Val, SDValue Ptr, SDValue Mask,
10574 SDValue EVL, EVT SVT,
10575 MachineMemOperand *MMO,
10576 bool IsCompressing) {
10577 EVT VT = Val.getValueType();
10578
10579 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10580 if (VT == SVT)
10581 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10582 EVL, VT, MMO, ISD::UNINDEXED,
10583 /*IsTruncating*/ false, IsCompressing);
10584
10586 "Should only be a truncating store, not extending!");
10587 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10588 assert(VT.isVector() == SVT.isVector() &&
10589 "Cannot use trunc store to convert to or from a vector!");
10590 assert((!VT.isVector() ||
10592 "Cannot use trunc store to change the number of vector elements!");
10593
10594 SDVTList VTs = getVTList(MVT::Other);
10596 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10598 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10599 ID.AddInteger(SVT.getRawBits());
10600 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10601 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10602 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10603 ID.AddInteger(MMO->getFlags());
10604 void *IP = nullptr;
10605 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10606 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10607 return SDValue(E, 0);
10608 }
10609 auto *N =
10610 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10611 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10612 createOperands(N, Ops);
10613
10614 CSEMap.InsertNode(N, IP);
10615 InsertNode(N);
10616 SDValue V(N, 0);
10617 NewSDValueDbgMsg(V, "Creating new node: ", this);
10618 return V;
10619}
10620
10624 auto *ST = cast<VPStoreSDNode>(OrigStore);
10625 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10626 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10627 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10628 Offset, ST->getMask(), ST->getVectorLength()};
10630 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10631 ID.AddInteger(ST->getMemoryVT().getRawBits());
10632 ID.AddInteger(ST->getRawSubclassData());
10633 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10634 ID.AddInteger(ST->getMemOperand()->getFlags());
10635 void *IP = nullptr;
10636 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10637 return SDValue(E, 0);
10638
10639 auto *N = newSDNode<VPStoreSDNode>(
10640 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10641 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10642 createOperands(N, Ops);
10643
10644 CSEMap.InsertNode(N, IP);
10645 InsertNode(N);
10646 SDValue V(N, 0);
10647 NewSDValueDbgMsg(V, "Creating new node: ", this);
10648 return V;
10649}
10650
10652 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10653 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10654 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10655 bool Indexed = AM != ISD::UNINDEXED;
10656 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10657
10658 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10659 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10660 : getVTList(VT, MVT::Other);
10662 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10663 ID.AddInteger(VT.getRawBits());
10664 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10665 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10666 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10667
10668 void *IP = nullptr;
10669 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10670 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10671 return SDValue(E, 0);
10672 }
10673
10674 auto *N =
10675 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10676 ExtType, IsExpanding, MemVT, MMO);
10677 createOperands(N, Ops);
10678 CSEMap.InsertNode(N, IP);
10679 InsertNode(N);
10680 SDValue V(N, 0);
10681 NewSDValueDbgMsg(V, "Creating new node: ", this);
10682 return V;
10683}
10684
10686 SDValue Ptr, SDValue Stride,
10687 SDValue Mask, SDValue EVL,
10688 MachineMemOperand *MMO,
10689 bool IsExpanding) {
10691 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10692 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10693}
10694
10696 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10697 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10698 MachineMemOperand *MMO, bool IsExpanding) {
10700 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10701 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10702}
10703
10705 SDValue Val, SDValue Ptr,
10706 SDValue Offset, SDValue Stride,
10707 SDValue Mask, SDValue EVL, EVT MemVT,
10708 MachineMemOperand *MMO,
10710 bool IsTruncating, bool IsCompressing) {
10711 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10712 bool Indexed = AM != ISD::UNINDEXED;
10713 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10714 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10715 : getVTList(MVT::Other);
10716 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10718 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10719 ID.AddInteger(MemVT.getRawBits());
10720 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10721 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10722 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10723 void *IP = nullptr;
10724 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10725 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10726 return SDValue(E, 0);
10727 }
10728 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10729 VTs, AM, IsTruncating,
10730 IsCompressing, MemVT, MMO);
10731 createOperands(N, Ops);
10732
10733 CSEMap.InsertNode(N, IP);
10734 InsertNode(N);
10735 SDValue V(N, 0);
10736 NewSDValueDbgMsg(V, "Creating new node: ", this);
10737 return V;
10738}
10739
10741 SDValue Val, SDValue Ptr,
10742 SDValue Stride, SDValue Mask,
10743 SDValue EVL, EVT SVT,
10744 MachineMemOperand *MMO,
10745 bool IsCompressing) {
10746 EVT VT = Val.getValueType();
10747
10748 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10749 if (VT == SVT)
10750 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10751 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10752 /*IsTruncating*/ false, IsCompressing);
10753
10755 "Should only be a truncating store, not extending!");
10756 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10757 assert(VT.isVector() == SVT.isVector() &&
10758 "Cannot use trunc store to convert to or from a vector!");
10759 assert((!VT.isVector() ||
10761 "Cannot use trunc store to change the number of vector elements!");
10762
10763 SDVTList VTs = getVTList(MVT::Other);
10765 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10767 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10768 ID.AddInteger(SVT.getRawBits());
10769 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10770 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10771 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10772 void *IP = nullptr;
10773 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10774 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10775 return SDValue(E, 0);
10776 }
10777 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10778 VTs, ISD::UNINDEXED, true,
10779 IsCompressing, SVT, MMO);
10780 createOperands(N, Ops);
10781
10782 CSEMap.InsertNode(N, IP);
10783 InsertNode(N);
10784 SDValue V(N, 0);
10785 NewSDValueDbgMsg(V, "Creating new node: ", this);
10786 return V;
10787}
10788
10791 ISD::MemIndexType IndexType) {
10792 assert(Ops.size() == 6 && "Incompatible number of operands");
10793
10795 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10796 ID.AddInteger(VT.getRawBits());
10797 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10798 dl.getIROrder(), VTs, VT, MMO, IndexType));
10799 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10800 ID.AddInteger(MMO->getFlags());
10801 void *IP = nullptr;
10802 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10803 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10804 return SDValue(E, 0);
10805 }
10806
10807 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10808 VT, MMO, IndexType);
10809 createOperands(N, Ops);
10810
10811 assert(N->getMask().getValueType().getVectorElementCount() ==
10812 N->getValueType(0).getVectorElementCount() &&
10813 "Vector width mismatch between mask and data");
10814 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10815 N->getValueType(0).getVectorElementCount().isScalable() &&
10816 "Scalable flags of index and data do not match");
10818 N->getIndex().getValueType().getVectorElementCount(),
10819 N->getValueType(0).getVectorElementCount()) &&
10820 "Vector width mismatch between index and data");
10821 assert(isa<ConstantSDNode>(N->getScale()) &&
10822 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10823 "Scale should be a constant power of 2");
10824
10825 CSEMap.InsertNode(N, IP);
10826 InsertNode(N);
10827 SDValue V(N, 0);
10828 NewSDValueDbgMsg(V, "Creating new node: ", this);
10829 return V;
10830}
10831
10834 MachineMemOperand *MMO,
10835 ISD::MemIndexType IndexType) {
10836 assert(Ops.size() == 7 && "Incompatible number of operands");
10837
10839 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10840 ID.AddInteger(VT.getRawBits());
10841 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10842 dl.getIROrder(), VTs, VT, MMO, IndexType));
10843 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10844 ID.AddInteger(MMO->getFlags());
10845 void *IP = nullptr;
10846 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10847 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10848 return SDValue(E, 0);
10849 }
10850 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10851 VT, MMO, IndexType);
10852 createOperands(N, Ops);
10853
10854 assert(N->getMask().getValueType().getVectorElementCount() ==
10855 N->getValue().getValueType().getVectorElementCount() &&
10856 "Vector width mismatch between mask and data");
10857 assert(
10858 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10859 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10860 "Scalable flags of index and data do not match");
10862 N->getIndex().getValueType().getVectorElementCount(),
10863 N->getValue().getValueType().getVectorElementCount()) &&
10864 "Vector width mismatch between index and data");
10865 assert(isa<ConstantSDNode>(N->getScale()) &&
10866 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10867 "Scale should be a constant power of 2");
10868
10869 CSEMap.InsertNode(N, IP);
10870 InsertNode(N);
10871 SDValue V(N, 0);
10872 NewSDValueDbgMsg(V, "Creating new node: ", this);
10873 return V;
10874}
10875
10878 SDValue PassThru, EVT MemVT,
10879 MachineMemOperand *MMO,
10881 ISD::LoadExtType ExtTy, bool isExpanding) {
10882 bool Indexed = AM != ISD::UNINDEXED;
10883 assert((Indexed || Offset.isUndef()) &&
10884 "Unindexed masked load with an offset!");
10885 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10886 : getVTList(VT, MVT::Other);
10887 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10890 ID.AddInteger(MemVT.getRawBits());
10891 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10892 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10893 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10894 ID.AddInteger(MMO->getFlags());
10895 void *IP = nullptr;
10896 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10897 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10898 return SDValue(E, 0);
10899 }
10900 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10901 AM, ExtTy, isExpanding, MemVT, MMO);
10902 createOperands(N, Ops);
10903
10904 CSEMap.InsertNode(N, IP);
10905 InsertNode(N);
10906 SDValue V(N, 0);
10907 NewSDValueDbgMsg(V, "Creating new node: ", this);
10908 return V;
10909}
10910
10915 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10916 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10917 Offset, LD->getMask(), LD->getPassThru(),
10918 LD->getMemoryVT(), LD->getMemOperand(), AM,
10919 LD->getExtensionType(), LD->isExpandingLoad());
10920}
10921
10924 SDValue Mask, EVT MemVT,
10925 MachineMemOperand *MMO,
10926 ISD::MemIndexedMode AM, bool IsTruncating,
10927 bool IsCompressing) {
10928 assert(Chain.getValueType() == MVT::Other &&
10929 "Invalid chain type");
10930 bool Indexed = AM != ISD::UNINDEXED;
10931 assert((Indexed || Offset.isUndef()) &&
10932 "Unindexed masked store with an offset!");
10933 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10934 : getVTList(MVT::Other);
10935 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10938 ID.AddInteger(MemVT.getRawBits());
10939 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10940 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10941 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10942 ID.AddInteger(MMO->getFlags());
10943 void *IP = nullptr;
10944 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10945 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10946 return SDValue(E, 0);
10947 }
10948 auto *N =
10949 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10950 IsTruncating, IsCompressing, MemVT, MMO);
10951 createOperands(N, Ops);
10952
10953 CSEMap.InsertNode(N, IP);
10954 InsertNode(N);
10955 SDValue V(N, 0);
10956 NewSDValueDbgMsg(V, "Creating new node: ", this);
10957 return V;
10958}
10959
10964 assert(ST->getOffset().isUndef() &&
10965 "Masked store is already a indexed store!");
10966 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10967 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10968 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10969}
10970
10973 MachineMemOperand *MMO,
10974 ISD::MemIndexType IndexType,
10975 ISD::LoadExtType ExtTy) {
10976 assert(Ops.size() == 6 && "Incompatible number of operands");
10977
10980 ID.AddInteger(MemVT.getRawBits());
10981 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10982 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10983 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10984 ID.AddInteger(MMO->getFlags());
10985 void *IP = nullptr;
10986 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10987 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10988 return SDValue(E, 0);
10989 }
10990
10991 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10992 VTs, MemVT, MMO, IndexType, ExtTy);
10993 createOperands(N, Ops);
10994
10995 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10996 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10997 assert(N->getMask().getValueType().getVectorElementCount() ==
10998 N->getValueType(0).getVectorElementCount() &&
10999 "Vector width mismatch between mask and data");
11000 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11001 N->getValueType(0).getVectorElementCount().isScalable() &&
11002 "Scalable flags of index and data do not match");
11004 N->getIndex().getValueType().getVectorElementCount(),
11005 N->getValueType(0).getVectorElementCount()) &&
11006 "Vector width mismatch between index and data");
11007 assert(isa<ConstantSDNode>(N->getScale()) &&
11008 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11009 "Scale should be a constant power of 2");
11010
11011 CSEMap.InsertNode(N, IP);
11012 InsertNode(N);
11013 SDValue V(N, 0);
11014 NewSDValueDbgMsg(V, "Creating new node: ", this);
11015 return V;
11016}
11017
11020 MachineMemOperand *MMO,
11021 ISD::MemIndexType IndexType,
11022 bool IsTrunc) {
11023 assert(Ops.size() == 6 && "Incompatible number of operands");
11024
11027 ID.AddInteger(MemVT.getRawBits());
11028 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
11029 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
11030 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11031 ID.AddInteger(MMO->getFlags());
11032 void *IP = nullptr;
11033 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11034 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
11035 return SDValue(E, 0);
11036 }
11037
11038 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11039 VTs, MemVT, MMO, IndexType, IsTrunc);
11040 createOperands(N, Ops);
11041
11042 assert(N->getMask().getValueType().getVectorElementCount() ==
11043 N->getValue().getValueType().getVectorElementCount() &&
11044 "Vector width mismatch between mask and data");
11045 assert(
11046 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
11047 N->getValue().getValueType().getVectorElementCount().isScalable() &&
11048 "Scalable flags of index and data do not match");
11050 N->getIndex().getValueType().getVectorElementCount(),
11051 N->getValue().getValueType().getVectorElementCount()) &&
11052 "Vector width mismatch between index and data");
11053 assert(isa<ConstantSDNode>(N->getScale()) &&
11054 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11055 "Scale should be a constant power of 2");
11056
11057 CSEMap.InsertNode(N, IP);
11058 InsertNode(N);
11059 SDValue V(N, 0);
11060 NewSDValueDbgMsg(V, "Creating new node: ", this);
11061 return V;
11062}
11063
11065 const SDLoc &dl, ArrayRef<SDValue> Ops,
11066 MachineMemOperand *MMO,
11067 ISD::MemIndexType IndexType) {
11068 assert(Ops.size() == 7 && "Incompatible number of operands");
11069
11072 ID.AddInteger(MemVT.getRawBits());
11073 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
11074 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
11075 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11076 ID.AddInteger(MMO->getFlags());
11077 void *IP = nullptr;
11078 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
11079 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
11080 return SDValue(E, 0);
11081 }
11082
11083 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
11084 VTs, MemVT, MMO, IndexType);
11085 createOperands(N, Ops);
11086
11087 assert(N->getMask().getValueType().getVectorElementCount() ==
11088 N->getIndex().getValueType().getVectorElementCount() &&
11089 "Vector width mismatch between mask and data");
11090 assert(isa<ConstantSDNode>(N->getScale()) &&
11091 N->getScale()->getAsAPIntVal().isPowerOf2() &&
11092 "Scale should be a constant power of 2");
11093 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
11094
11095 CSEMap.InsertNode(N, IP);
11096 InsertNode(N);
11097 SDValue V(N, 0);
11098 NewSDValueDbgMsg(V, "Creating new node: ", this);
11099 return V;
11100}
11101
11103 SDValue Ptr, SDValue Mask, SDValue EVL,
11104 MachineMemOperand *MMO) {
11105 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
11106 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
11108 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
11109 ID.AddInteger(VT.getRawBits());
11110 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
11111 VTs, VT, MMO));
11112 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11113 ID.AddInteger(MMO->getFlags());
11114 void *IP = nullptr;
11115 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11116 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
11117 return SDValue(E, 0);
11118 }
11119 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
11120 VT, MMO);
11121 createOperands(N, Ops);
11122
11123 CSEMap.InsertNode(N, IP);
11124 InsertNode(N);
11125 SDValue V(N, 0);
11126 NewSDValueDbgMsg(V, "Creating new node: ", this);
11127 return V;
11128}
11129
11131 EVT MemVT, MachineMemOperand *MMO) {
11132 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11133 SDVTList VTs = getVTList(MVT::Other);
11134 SDValue Ops[] = {Chain, Ptr};
11137 ID.AddInteger(MemVT.getRawBits());
11138 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11139 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11140 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11141 ID.AddInteger(MMO->getFlags());
11142 void *IP = nullptr;
11143 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11144 return SDValue(E, 0);
11145
11146 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
11147 dl.getDebugLoc(), VTs, MemVT, MMO);
11148 createOperands(N, Ops);
11149
11150 CSEMap.InsertNode(N, IP);
11151 InsertNode(N);
11152 SDValue V(N, 0);
11153 NewSDValueDbgMsg(V, "Creating new node: ", this);
11154 return V;
11155}
11156
11158 EVT MemVT, MachineMemOperand *MMO) {
11159 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
11160 SDVTList VTs = getVTList(MVT::Other);
11161 SDValue Ops[] = {Chain, Ptr};
11164 ID.AddInteger(MemVT.getRawBits());
11165 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
11166 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
11167 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
11168 ID.AddInteger(MMO->getFlags());
11169 void *IP = nullptr;
11170 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
11171 return SDValue(E, 0);
11172
11173 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
11174 dl.getDebugLoc(), VTs, MemVT, MMO);
11175 createOperands(N, Ops);
11176
11177 CSEMap.InsertNode(N, IP);
11178 InsertNode(N);
11179 SDValue V(N, 0);
11180 NewSDValueDbgMsg(V, "Creating new node: ", this);
11181 return V;
11182}
11183
11185 // select undef, T, F --> T (if T is a constant), otherwise F
11186 // select, ?, undef, F --> F
11187 // select, ?, T, undef --> T
11188 if (Cond.isUndef())
11189 return isConstantValueOfAnyType(T) ? T : F;
11190 if (T.isUndef())
11192 if (F.isUndef())
11194
11195 // select true, T, F --> T
11196 // select false, T, F --> F
11197 if (auto C = isBoolConstant(Cond))
11198 return *C ? T : F;
11199
11200 // select ?, T, T --> T
11201 if (T == F)
11202 return T;
11203
11204 return SDValue();
11205}
11206
11208 // shift undef, Y --> 0 (can always assume that the undef value is 0)
11209 if (X.isUndef())
11210 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
11211 // shift X, undef --> undef (because it may shift by the bitwidth)
11212 if (Y.isUndef())
11213 return getUNDEF(X.getValueType());
11214
11215 // shift 0, Y --> 0
11216 // shift X, 0 --> X
11218 return X;
11219
11220 // shift X, C >= bitwidth(X) --> undef
11221 // All vector elements must be too big (or undef) to avoid partial undefs.
11222 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11223 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11224 };
11225 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11226 return getUNDEF(X.getValueType());
11227
11228 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11229 if (X.getValueType().getScalarType() == MVT::i1)
11230 return X;
11231
11232 return SDValue();
11233}
11234
11236 SDNodeFlags Flags) {
11237 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11238 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11239 // operation is poison. That result can be relaxed to undef.
11240 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11241 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11242 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11243 (YC && YC->getValueAPF().isNaN());
11244 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11245 (YC && YC->getValueAPF().isInfinity());
11246
11247 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11248 return getUNDEF(X.getValueType());
11249
11250 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11251 return getUNDEF(X.getValueType());
11252
11253 if (!YC)
11254 return SDValue();
11255
11256 // X + -0.0 --> X
11257 if (Opcode == ISD::FADD)
11258 if (YC->getValueAPF().isNegZero())
11259 return X;
11260
11261 // X - +0.0 --> X
11262 if (Opcode == ISD::FSUB)
11263 if (YC->getValueAPF().isPosZero())
11264 return X;
11265
11266 // X * 1.0 --> X
11267 // X / 1.0 --> X
11268 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11269 if (YC->getValueAPF().isExactlyValue(1.0))
11270 return X;
11271
11272 // X * 0.0 --> 0.0
11273 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11274 if (YC->getValueAPF().isZero())
11275 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11276
11277 return SDValue();
11278}
11279
11281 SDValue Ptr, SDValue SV, unsigned Align) {
11282 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11283 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11284}
11285
11286SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11288 switch (Ops.size()) {
11289 case 0: return getNode(Opcode, DL, VT);
11290 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11291 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11292 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11293 default: break;
11294 }
11295
11296 // Copy from an SDUse array into an SDValue array for use with
11297 // the regular getNode logic.
11299 return getNode(Opcode, DL, VT, NewOps);
11300}
11301
11302SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11304 SDNodeFlags Flags;
11305 if (Inserter)
11306 Flags = Inserter->getFlags();
11307 return getNode(Opcode, DL, VT, Ops, Flags);
11308}
11309
11310SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11311 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11312 unsigned NumOps = Ops.size();
11313 switch (NumOps) {
11314 case 0: return getNode(Opcode, DL, VT);
11315 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11316 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11317 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11318 default: break;
11319 }
11320
11321#ifndef NDEBUG
11322 for (const auto &Op : Ops)
11323 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11324 "Operand is DELETED_NODE!");
11325#endif
11326
11327 switch (Opcode) {
11328 default: break;
11329 case ISD::BUILD_VECTOR:
11330 // Attempt to simplify BUILD_VECTOR.
11331 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11332 return V;
11333 break;
11335 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11336 return V;
11337 break;
11338 case ISD::SELECT_CC:
11339 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11340 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11341 "LHS and RHS of condition must have same type!");
11342 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11343 "True and False arms of SelectCC must have same type!");
11344 assert(Ops[2].getValueType() == VT &&
11345 "select_cc node must be of same type as true and false value!");
11346 assert((!Ops[0].getValueType().isVector() ||
11347 Ops[0].getValueType().getVectorElementCount() ==
11348 VT.getVectorElementCount()) &&
11349 "Expected select_cc with vector result to have the same sized "
11350 "comparison type!");
11351 break;
11352 case ISD::BR_CC:
11353 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11354 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11355 "LHS/RHS of comparison should match types!");
11356 break;
11357 case ISD::VP_ADD:
11358 case ISD::VP_SUB:
11359 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11360 if (VT.getScalarType() == MVT::i1)
11361 Opcode = ISD::VP_XOR;
11362 break;
11363 case ISD::VP_MUL:
11364 // If it is VP_MUL mask operation then turn it to VP_AND
11365 if (VT.getScalarType() == MVT::i1)
11366 Opcode = ISD::VP_AND;
11367 break;
11368 case ISD::VP_REDUCE_MUL:
11369 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11370 if (VT == MVT::i1)
11371 Opcode = ISD::VP_REDUCE_AND;
11372 break;
11373 case ISD::VP_REDUCE_ADD:
11374 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11375 if (VT == MVT::i1)
11376 Opcode = ISD::VP_REDUCE_XOR;
11377 break;
11378 case ISD::VP_REDUCE_SMAX:
11379 case ISD::VP_REDUCE_UMIN:
11380 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11381 // VP_REDUCE_AND.
11382 if (VT == MVT::i1)
11383 Opcode = ISD::VP_REDUCE_AND;
11384 break;
11385 case ISD::VP_REDUCE_SMIN:
11386 case ISD::VP_REDUCE_UMAX:
11387 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11388 // VP_REDUCE_OR.
11389 if (VT == MVT::i1)
11390 Opcode = ISD::VP_REDUCE_OR;
11391 break;
11392 }
11393
11394 // Memoize nodes.
11395 SDNode *N;
11396 SDVTList VTs = getVTList(VT);
11397
11398 if (VT != MVT::Glue) {
11400 AddNodeIDNode(ID, Opcode, VTs, Ops);
11401 void *IP = nullptr;
11402
11403 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11404 E->intersectFlagsWith(Flags);
11405 return SDValue(E, 0);
11406 }
11407
11408 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11409 createOperands(N, Ops);
11410
11411 CSEMap.InsertNode(N, IP);
11412 } else {
11413 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11414 createOperands(N, Ops);
11415 }
11416
11417 N->setFlags(Flags);
11418 InsertNode(N);
11419 SDValue V(N, 0);
11420 NewSDValueDbgMsg(V, "Creating new node: ", this);
11421 return V;
11422}
11423
11424SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11425 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11426 SDNodeFlags Flags;
11427 if (Inserter)
11428 Flags = Inserter->getFlags();
11429 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11430}
11431
11432SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11434 const SDNodeFlags Flags) {
11435 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11436}
11437
11438SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11440 SDNodeFlags Flags;
11441 if (Inserter)
11442 Flags = Inserter->getFlags();
11443 return getNode(Opcode, DL, VTList, Ops, Flags);
11444}
11445
11446SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11447 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11448 if (VTList.NumVTs == 1)
11449 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11450
11451#ifndef NDEBUG
11452 for (const auto &Op : Ops)
11453 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11454 "Operand is DELETED_NODE!");
11455#endif
11456
11457 switch (Opcode) {
11458 case ISD::SADDO:
11459 case ISD::UADDO:
11460 case ISD::SSUBO:
11461 case ISD::USUBO: {
11462 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11463 "Invalid add/sub overflow op!");
11464 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11465 Ops[0].getValueType() == Ops[1].getValueType() &&
11466 Ops[0].getValueType() == VTList.VTs[0] &&
11467 "Binary operator types must match!");
11468 SDValue N1 = Ops[0], N2 = Ops[1];
11469 canonicalizeCommutativeBinop(Opcode, N1, N2);
11470
11471 // (X +- 0) -> X with zero-overflow.
11472 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11473 /*AllowTruncation*/ true);
11474 if (N2CV && N2CV->isZero()) {
11475 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11476 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11477 }
11478
11479 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11480 VTList.VTs[1].getScalarType() == MVT::i1) {
11481 SDValue F1 = getFreeze(N1);
11482 SDValue F2 = getFreeze(N2);
11483 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11484 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11485 return getNode(ISD::MERGE_VALUES, DL, VTList,
11486 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11487 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11488 Flags);
11489 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11490 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11491 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11492 return getNode(ISD::MERGE_VALUES, DL, VTList,
11493 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11494 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11495 Flags);
11496 }
11497 }
11498 break;
11499 }
11500 case ISD::SADDO_CARRY:
11501 case ISD::UADDO_CARRY:
11502 case ISD::SSUBO_CARRY:
11503 case ISD::USUBO_CARRY:
11504 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11505 "Invalid add/sub overflow op!");
11506 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11507 Ops[0].getValueType() == Ops[1].getValueType() &&
11508 Ops[0].getValueType() == VTList.VTs[0] &&
11509 Ops[2].getValueType() == VTList.VTs[1] &&
11510 "Binary operator types must match!");
11511 break;
11512 case ISD::SMUL_LOHI:
11513 case ISD::UMUL_LOHI: {
11514 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11515 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11516 VTList.VTs[0] == Ops[0].getValueType() &&
11517 VTList.VTs[0] == Ops[1].getValueType() &&
11518 "Binary operator types must match!");
11519 // Constant fold.
11522 if (LHS && RHS) {
11523 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11524 unsigned OutWidth = Width * 2;
11525 APInt Val = LHS->getAPIntValue();
11526 APInt Mul = RHS->getAPIntValue();
11527 if (Opcode == ISD::SMUL_LOHI) {
11528 Val = Val.sext(OutWidth);
11529 Mul = Mul.sext(OutWidth);
11530 } else {
11531 Val = Val.zext(OutWidth);
11532 Mul = Mul.zext(OutWidth);
11533 }
11534 Val *= Mul;
11535
11536 SDValue Hi =
11537 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11538 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11539 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11540 }
11541 break;
11542 }
11543 case ISD::FFREXP: {
11544 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11545 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11546 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11547
11549 int FrexpExp;
11550 APFloat FrexpMant =
11551 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11552 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11553 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11554 DL, VTList.VTs[1]);
11555 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11556 }
11557
11558 break;
11559 }
11561 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11562 "Invalid STRICT_FP_EXTEND!");
11563 assert(VTList.VTs[0].isFloatingPoint() &&
11564 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11565 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11566 "STRICT_FP_EXTEND result type should be vector iff the operand "
11567 "type is vector!");
11568 assert((!VTList.VTs[0].isVector() ||
11569 VTList.VTs[0].getVectorElementCount() ==
11570 Ops[1].getValueType().getVectorElementCount()) &&
11571 "Vector element count mismatch!");
11572 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11573 "Invalid fpext node, dst <= src!");
11574 break;
11576 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11577 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11578 "STRICT_FP_ROUND result type should be vector iff the operand "
11579 "type is vector!");
11580 assert((!VTList.VTs[0].isVector() ||
11581 VTList.VTs[0].getVectorElementCount() ==
11582 Ops[1].getValueType().getVectorElementCount()) &&
11583 "Vector element count mismatch!");
11584 assert(VTList.VTs[0].isFloatingPoint() &&
11585 Ops[1].getValueType().isFloatingPoint() &&
11586 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11587 Ops[2].getOpcode() == ISD::TargetConstant &&
11588 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11589 "Invalid STRICT_FP_ROUND!");
11590 break;
11591 }
11592
11593 // Memoize the node unless it returns a glue result.
11594 SDNode *N;
11595 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11597 AddNodeIDNode(ID, Opcode, VTList, Ops);
11598 void *IP = nullptr;
11599 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11600 E->intersectFlagsWith(Flags);
11601 return SDValue(E, 0);
11602 }
11603
11604 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11605 createOperands(N, Ops);
11606 CSEMap.InsertNode(N, IP);
11607 } else {
11608 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11609 createOperands(N, Ops);
11610 }
11611
11612 N->setFlags(Flags);
11613 InsertNode(N);
11614 SDValue V(N, 0);
11615 NewSDValueDbgMsg(V, "Creating new node: ", this);
11616 return V;
11617}
11618
11619SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11620 SDVTList VTList) {
11621 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11622}
11623
11624SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11625 SDValue N1) {
11626 SDValue Ops[] = { N1 };
11627 return getNode(Opcode, DL, VTList, Ops);
11628}
11629
11630SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11631 SDValue N1, SDValue N2) {
11632 SDValue Ops[] = { N1, N2 };
11633 return getNode(Opcode, DL, VTList, Ops);
11634}
11635
11636SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11637 SDValue N1, SDValue N2, SDValue N3) {
11638 SDValue Ops[] = { N1, N2, N3 };
11639 return getNode(Opcode, DL, VTList, Ops);
11640}
11641
11642SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11643 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11644 SDValue Ops[] = { N1, N2, N3, N4 };
11645 return getNode(Opcode, DL, VTList, Ops);
11646}
11647
11648SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11649 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11650 SDValue N5) {
11651 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11652 return getNode(Opcode, DL, VTList, Ops);
11653}
11654
11656 if (!VT.isExtended())
11657 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11658
11659 return makeVTList(&(*EVTs.insert(VT).first), 1);
11660}
11661
11664 ID.AddInteger(2U);
11665 ID.AddInteger(VT1.getRawBits());
11666 ID.AddInteger(VT2.getRawBits());
11667
11668 void *IP = nullptr;
11669 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11670 if (!Result) {
11671 EVT *Array = Allocator.Allocate<EVT>(2);
11672 Array[0] = VT1;
11673 Array[1] = VT2;
11674 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11675 VTListMap.InsertNode(Result, IP);
11676 }
11677 return Result->getSDVTList();
11678}
11679
11682 ID.AddInteger(3U);
11683 ID.AddInteger(VT1.getRawBits());
11684 ID.AddInteger(VT2.getRawBits());
11685 ID.AddInteger(VT3.getRawBits());
11686
11687 void *IP = nullptr;
11688 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11689 if (!Result) {
11690 EVT *Array = Allocator.Allocate<EVT>(3);
11691 Array[0] = VT1;
11692 Array[1] = VT2;
11693 Array[2] = VT3;
11694 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11695 VTListMap.InsertNode(Result, IP);
11696 }
11697 return Result->getSDVTList();
11698}
11699
11702 ID.AddInteger(4U);
11703 ID.AddInteger(VT1.getRawBits());
11704 ID.AddInteger(VT2.getRawBits());
11705 ID.AddInteger(VT3.getRawBits());
11706 ID.AddInteger(VT4.getRawBits());
11707
11708 void *IP = nullptr;
11709 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11710 if (!Result) {
11711 EVT *Array = Allocator.Allocate<EVT>(4);
11712 Array[0] = VT1;
11713 Array[1] = VT2;
11714 Array[2] = VT3;
11715 Array[3] = VT4;
11716 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11717 VTListMap.InsertNode(Result, IP);
11718 }
11719 return Result->getSDVTList();
11720}
11721
11723 unsigned NumVTs = VTs.size();
11725 ID.AddInteger(NumVTs);
11726 for (unsigned index = 0; index < NumVTs; index++) {
11727 ID.AddInteger(VTs[index].getRawBits());
11728 }
11729
11730 void *IP = nullptr;
11731 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11732 if (!Result) {
11733 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11734 llvm::copy(VTs, Array);
11735 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11736 VTListMap.InsertNode(Result, IP);
11737 }
11738 return Result->getSDVTList();
11739}
11740
11741
11742/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11743/// specified operands. If the resultant node already exists in the DAG,
11744/// this does not modify the specified node, instead it returns the node that
11745/// already exists. If the resultant node does not exist in the DAG, the
11746/// input node is returned. As a degenerate case, if you specify the same
11747/// input operands as the node already has, the input node is returned.
11749 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11750
11751 // Check to see if there is no change.
11752 if (Op == N->getOperand(0)) return N;
11753
11754 // See if the modified node already exists.
11755 void *InsertPos = nullptr;
11756 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11757 return Existing;
11758
11759 // Nope it doesn't. Remove the node from its current place in the maps.
11760 if (InsertPos)
11761 if (!RemoveNodeFromCSEMaps(N))
11762 InsertPos = nullptr;
11763
11764 // Now we update the operands.
11765 N->OperandList[0].set(Op);
11766
11768 // If this gets put into a CSE map, add it.
11769 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11770 return N;
11771}
11772
11774 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11775
11776 // Check to see if there is no change.
11777 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11778 return N; // No operands changed, just return the input node.
11779
11780 // See if the modified node already exists.
11781 void *InsertPos = nullptr;
11782 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11783 return Existing;
11784
11785 // Nope it doesn't. Remove the node from its current place in the maps.
11786 if (InsertPos)
11787 if (!RemoveNodeFromCSEMaps(N))
11788 InsertPos = nullptr;
11789
11790 // Now we update the operands.
11791 if (N->OperandList[0] != Op1)
11792 N->OperandList[0].set(Op1);
11793 if (N->OperandList[1] != Op2)
11794 N->OperandList[1].set(Op2);
11795
11797 // If this gets put into a CSE map, add it.
11798 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11799 return N;
11800}
11801
11804 SDValue Ops[] = { Op1, Op2, Op3 };
11805 return UpdateNodeOperands(N, Ops);
11806}
11807
11810 SDValue Op3, SDValue Op4) {
11811 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11812 return UpdateNodeOperands(N, Ops);
11813}
11814
11817 SDValue Op3, SDValue Op4, SDValue Op5) {
11818 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11819 return UpdateNodeOperands(N, Ops);
11820}
11821
11824 unsigned NumOps = Ops.size();
11825 assert(N->getNumOperands() == NumOps &&
11826 "Update with wrong number of operands");
11827
11828 // If no operands changed just return the input node.
11829 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11830 return N;
11831
11832 // See if the modified node already exists.
11833 void *InsertPos = nullptr;
11834 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11835 return Existing;
11836
11837 // Nope it doesn't. Remove the node from its current place in the maps.
11838 if (InsertPos)
11839 if (!RemoveNodeFromCSEMaps(N))
11840 InsertPos = nullptr;
11841
11842 // Now we update the operands.
11843 for (unsigned i = 0; i != NumOps; ++i)
11844 if (N->OperandList[i] != Ops[i])
11845 N->OperandList[i].set(Ops[i]);
11846
11848 // If this gets put into a CSE map, add it.
11849 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11850 return N;
11851}
11852
11853/// DropOperands - Release the operands and set this node to have
11854/// zero operands.
11856 // Unlike the code in MorphNodeTo that does this, we don't need to
11857 // watch for dead nodes here.
11858 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11859 SDUse &Use = *I++;
11860 Use.set(SDValue());
11861 }
11862}
11863
11865 ArrayRef<MachineMemOperand *> NewMemRefs) {
11866 if (NewMemRefs.empty()) {
11867 N->clearMemRefs();
11868 return;
11869 }
11870
11871 // Check if we can avoid allocating by storing a single reference directly.
11872 if (NewMemRefs.size() == 1) {
11873 N->MemRefs = NewMemRefs[0];
11874 N->NumMemRefs = 1;
11875 return;
11876 }
11877
11878 MachineMemOperand **MemRefsBuffer =
11879 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11880 llvm::copy(NewMemRefs, MemRefsBuffer);
11881 N->MemRefs = MemRefsBuffer;
11882 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11883}
11884
11885/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11886/// machine opcode.
11887///
11889 EVT VT) {
11890 SDVTList VTs = getVTList(VT);
11891 return SelectNodeTo(N, MachineOpc, VTs, {});
11892}
11893
11895 EVT VT, SDValue Op1) {
11896 SDVTList VTs = getVTList(VT);
11897 SDValue Ops[] = { Op1 };
11898 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11899}
11900
11902 EVT VT, SDValue Op1,
11903 SDValue Op2) {
11904 SDVTList VTs = getVTList(VT);
11905 SDValue Ops[] = { Op1, Op2 };
11906 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11907}
11908
11910 EVT VT, SDValue Op1,
11911 SDValue Op2, SDValue Op3) {
11912 SDVTList VTs = getVTList(VT);
11913 SDValue Ops[] = { Op1, Op2, Op3 };
11914 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11915}
11916
11919 SDVTList VTs = getVTList(VT);
11920 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11921}
11922
11924 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11925 SDVTList VTs = getVTList(VT1, VT2);
11926 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11927}
11928
11930 EVT VT1, EVT VT2) {
11931 SDVTList VTs = getVTList(VT1, VT2);
11932 return SelectNodeTo(N, MachineOpc, VTs, {});
11933}
11934
11936 EVT VT1, EVT VT2, EVT VT3,
11938 SDVTList VTs = getVTList(VT1, VT2, VT3);
11939 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11940}
11941
11943 EVT VT1, EVT VT2,
11944 SDValue Op1, SDValue Op2) {
11945 SDVTList VTs = getVTList(VT1, VT2);
11946 SDValue Ops[] = { Op1, Op2 };
11947 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11948}
11949
11952 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11953 // Reset the NodeID to -1.
11954 New->setNodeId(-1);
11955 if (New != N) {
11956 ReplaceAllUsesWith(N, New);
11958 }
11959 return New;
11960}
11961
11962/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11963/// the line number information on the merged node since it is not possible to
11964/// preserve the information that operation is associated with multiple lines.
11965/// This will make the debugger working better at -O0, were there is a higher
11966/// probability having other instructions associated with that line.
11967///
11968/// For IROrder, we keep the smaller of the two
11969SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11970 DebugLoc NLoc = N->getDebugLoc();
11971 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11972 N->setDebugLoc(DebugLoc());
11973 }
11974 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11975 N->setIROrder(Order);
11976 return N;
11977}
11978
11979/// MorphNodeTo - This *mutates* the specified node to have the specified
11980/// return type, opcode, and operands.
11981///
11982/// Note that MorphNodeTo returns the resultant node. If there is already a
11983/// node of the specified opcode and operands, it returns that node instead of
11984/// the current one. Note that the SDLoc need not be the same.
11985///
11986/// Using MorphNodeTo is faster than creating a new node and swapping it in
11987/// with ReplaceAllUsesWith both because it often avoids allocating a new
11988/// node, and because it doesn't require CSE recalculation for any of
11989/// the node's users.
11990///
11991/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11992/// As a consequence it isn't appropriate to use from within the DAG combiner or
11993/// the legalizer which maintain worklists that would need to be updated when
11994/// deleting things.
11997 // If an identical node already exists, use it.
11998 void *IP = nullptr;
11999 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
12001 AddNodeIDNode(ID, Opc, VTs, Ops);
12002 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
12003 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
12004 }
12005
12006 if (!RemoveNodeFromCSEMaps(N))
12007 IP = nullptr;
12008
12009 // Start the morphing.
12010 N->NodeType = Opc;
12011 N->ValueList = VTs.VTs;
12012 N->NumValues = VTs.NumVTs;
12013
12014 // Clear the operands list, updating used nodes to remove this from their
12015 // use list. Keep track of any operands that become dead as a result.
12016 SmallPtrSet<SDNode*, 16> DeadNodeSet;
12017 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
12018 SDUse &Use = *I++;
12019 SDNode *Used = Use.getNode();
12020 Use.set(SDValue());
12021 if (Used->use_empty())
12022 DeadNodeSet.insert(Used);
12023 }
12024
12025 // For MachineNode, initialize the memory references information.
12027 MN->clearMemRefs();
12028
12029 // Swap for an appropriately sized array from the recycler.
12030 removeOperands(N);
12031 createOperands(N, Ops);
12032
12033 // Delete any nodes that are still dead after adding the uses for the
12034 // new operands.
12035 if (!DeadNodeSet.empty()) {
12036 SmallVector<SDNode *, 16> DeadNodes;
12037 for (SDNode *N : DeadNodeSet)
12038 if (N->use_empty())
12039 DeadNodes.push_back(N);
12040 RemoveDeadNodes(DeadNodes);
12041 }
12042
12043 if (IP)
12044 CSEMap.InsertNode(N, IP); // Memoize the new node.
12045 return N;
12046}
12047
12049 unsigned OrigOpc = Node->getOpcode();
12050 unsigned NewOpc;
12051 switch (OrigOpc) {
12052 default:
12053 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
12054#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12055 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
12056#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
12057 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
12058#include "llvm/IR/ConstrainedOps.def"
12059 }
12060
12061 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
12062
12063 // We're taking this node out of the chain, so we need to re-link things.
12064 SDValue InputChain = Node->getOperand(0);
12065 SDValue OutputChain = SDValue(Node, 1);
12066 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
12067
12069 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
12070 Ops.push_back(Node->getOperand(i));
12071
12072 SDVTList VTs = getVTList(Node->getValueType(0));
12073 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
12074
12075 // MorphNodeTo can operate in two ways: if an existing node with the
12076 // specified operands exists, it can just return it. Otherwise, it
12077 // updates the node in place to have the requested operands.
12078 if (Res == Node) {
12079 // If we updated the node in place, reset the node ID. To the isel,
12080 // this should be just like a newly allocated machine node.
12081 Res->setNodeId(-1);
12082 } else {
12085 }
12086
12087 return Res;
12088}
12089
12090/// getMachineNode - These are used for target selectors to create a new node
12091/// with specified return type(s), MachineInstr opcode, and operands.
12092///
12093/// Note that getMachineNode returns the resultant node. If there is already a
12094/// node of the specified opcode and operands, it returns that node instead of
12095/// the current one.
12097 EVT VT) {
12098 SDVTList VTs = getVTList(VT);
12099 return getMachineNode(Opcode, dl, VTs, {});
12100}
12101
12103 EVT VT, SDValue Op1) {
12104 SDVTList VTs = getVTList(VT);
12105 SDValue Ops[] = { Op1 };
12106 return getMachineNode(Opcode, dl, VTs, Ops);
12107}
12108
12110 EVT VT, SDValue Op1, SDValue Op2) {
12111 SDVTList VTs = getVTList(VT);
12112 SDValue Ops[] = { Op1, Op2 };
12113 return getMachineNode(Opcode, dl, VTs, Ops);
12114}
12115
12117 EVT VT, SDValue Op1, SDValue Op2,
12118 SDValue Op3) {
12119 SDVTList VTs = getVTList(VT);
12120 SDValue Ops[] = { Op1, Op2, Op3 };
12121 return getMachineNode(Opcode, dl, VTs, Ops);
12122}
12123
12126 SDVTList VTs = getVTList(VT);
12127 return getMachineNode(Opcode, dl, VTs, Ops);
12128}
12129
12131 EVT VT1, EVT VT2, SDValue Op1,
12132 SDValue Op2) {
12133 SDVTList VTs = getVTList(VT1, VT2);
12134 SDValue Ops[] = { Op1, Op2 };
12135 return getMachineNode(Opcode, dl, VTs, Ops);
12136}
12137
12139 EVT VT1, EVT VT2, SDValue Op1,
12140 SDValue Op2, SDValue Op3) {
12141 SDVTList VTs = getVTList(VT1, VT2);
12142 SDValue Ops[] = { Op1, Op2, Op3 };
12143 return getMachineNode(Opcode, dl, VTs, Ops);
12144}
12145
12147 EVT VT1, EVT VT2,
12149 SDVTList VTs = getVTList(VT1, VT2);
12150 return getMachineNode(Opcode, dl, VTs, Ops);
12151}
12152
12154 EVT VT1, EVT VT2, EVT VT3,
12155 SDValue Op1, SDValue Op2) {
12156 SDVTList VTs = getVTList(VT1, VT2, VT3);
12157 SDValue Ops[] = { Op1, Op2 };
12158 return getMachineNode(Opcode, dl, VTs, Ops);
12159}
12160
12162 EVT VT1, EVT VT2, EVT VT3,
12163 SDValue Op1, SDValue Op2,
12164 SDValue Op3) {
12165 SDVTList VTs = getVTList(VT1, VT2, VT3);
12166 SDValue Ops[] = { Op1, Op2, Op3 };
12167 return getMachineNode(Opcode, dl, VTs, Ops);
12168}
12169
12171 EVT VT1, EVT VT2, EVT VT3,
12173 SDVTList VTs = getVTList(VT1, VT2, VT3);
12174 return getMachineNode(Opcode, dl, VTs, Ops);
12175}
12176
12178 ArrayRef<EVT> ResultTys,
12180 SDVTList VTs = getVTList(ResultTys);
12181 return getMachineNode(Opcode, dl, VTs, Ops);
12182}
12183
12185 SDVTList VTs,
12187 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
12189 void *IP = nullptr;
12190
12191 if (DoCSE) {
12193 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
12194 IP = nullptr;
12195 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
12196 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
12197 }
12198 }
12199
12200 // Allocate a new MachineSDNode.
12201 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
12202 createOperands(N, Ops);
12203
12204 if (DoCSE)
12205 CSEMap.InsertNode(N, IP);
12206
12207 InsertNode(N);
12208 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
12209 return N;
12210}
12211
12212/// getTargetExtractSubreg - A convenience function for creating
12213/// TargetOpcode::EXTRACT_SUBREG nodes.
12215 SDValue Operand) {
12216 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12217 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12218 VT, Operand, SRIdxVal);
12219 return SDValue(Subreg, 0);
12220}
12221
12222/// getTargetInsertSubreg - A convenience function for creating
12223/// TargetOpcode::INSERT_SUBREG nodes.
12225 SDValue Operand, SDValue Subreg) {
12226 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12227 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12228 VT, Operand, Subreg, SRIdxVal);
12229 return SDValue(Result, 0);
12230}
12231
12232/// getNodeIfExists - Get the specified node if it's already available, or
12233/// else return NULL.
12236 bool AllowCommute) {
12237 SDNodeFlags Flags;
12238 if (Inserter)
12239 Flags = Inserter->getFlags();
12240 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12241}
12242
12245 const SDNodeFlags Flags,
12246 bool AllowCommute) {
12247 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12248 return nullptr;
12249
12250 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12252 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12253 void *IP = nullptr;
12254 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12255 E->intersectFlagsWith(Flags);
12256 return E;
12257 }
12258 return nullptr;
12259 };
12260
12261 if (SDNode *Existing = Lookup(Ops))
12262 return Existing;
12263
12264 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12265 return Lookup({Ops[1], Ops[0]});
12266
12267 return nullptr;
12268}
12269
12270/// doesNodeExist - Check if a node exists without modifying its flags.
12271bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12273 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12275 AddNodeIDNode(ID, Opcode, VTList, Ops);
12276 void *IP = nullptr;
12277 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12278 return true;
12279 }
12280 return false;
12281}
12282
12283/// getDbgValue - Creates a SDDbgValue node.
12284///
12285/// SDNode
12287 SDNode *N, unsigned R, bool IsIndirect,
12288 const DebugLoc &DL, unsigned O) {
12289 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12290 "Expected inlined-at fields to agree");
12291 return new (DbgInfo->getAlloc())
12292 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12293 {}, IsIndirect, DL, O,
12294 /*IsVariadic=*/false);
12295}
12296
12297/// Constant
12299 DIExpression *Expr,
12300 const Value *C,
12301 const DebugLoc &DL, unsigned O) {
12302 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12303 "Expected inlined-at fields to agree");
12304 return new (DbgInfo->getAlloc())
12305 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12306 /*IsIndirect=*/false, DL, O,
12307 /*IsVariadic=*/false);
12308}
12309
12310/// FrameIndex
12312 DIExpression *Expr, unsigned FI,
12313 bool IsIndirect,
12314 const DebugLoc &DL,
12315 unsigned O) {
12316 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12317 "Expected inlined-at fields to agree");
12318 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12319}
12320
12321/// FrameIndex with dependencies
12323 DIExpression *Expr, unsigned FI,
12324 ArrayRef<SDNode *> Dependencies,
12325 bool IsIndirect,
12326 const DebugLoc &DL,
12327 unsigned O) {
12328 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12329 "Expected inlined-at fields to agree");
12330 return new (DbgInfo->getAlloc())
12331 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12332 Dependencies, IsIndirect, DL, O,
12333 /*IsVariadic=*/false);
12334}
12335
12336/// VReg
12338 Register VReg, bool IsIndirect,
12339 const DebugLoc &DL, unsigned O) {
12340 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12341 "Expected inlined-at fields to agree");
12342 return new (DbgInfo->getAlloc())
12343 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12344 {}, IsIndirect, DL, O,
12345 /*IsVariadic=*/false);
12346}
12347
12350 ArrayRef<SDNode *> Dependencies,
12351 bool IsIndirect, const DebugLoc &DL,
12352 unsigned O, bool IsVariadic) {
12353 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12354 "Expected inlined-at fields to agree");
12355 return new (DbgInfo->getAlloc())
12356 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12357 DL, O, IsVariadic);
12358}
12359
12361 unsigned OffsetInBits, unsigned SizeInBits,
12362 bool InvalidateDbg) {
12363 SDNode *FromNode = From.getNode();
12364 SDNode *ToNode = To.getNode();
12365 assert(FromNode && ToNode && "Can't modify dbg values");
12366
12367 // PR35338
12368 // TODO: assert(From != To && "Redundant dbg value transfer");
12369 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12370 if (From == To || FromNode == ToNode)
12371 return;
12372
12373 if (!FromNode->getHasDebugValue())
12374 return;
12375
12376 SDDbgOperand FromLocOp =
12377 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12379
12381 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12382 if (Dbg->isInvalidated())
12383 continue;
12384
12385 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12386
12387 // Create a new location ops vector that is equal to the old vector, but
12388 // with each instance of FromLocOp replaced with ToLocOp.
12389 bool Changed = false;
12390 auto NewLocOps = Dbg->copyLocationOps();
12391 std::replace_if(
12392 NewLocOps.begin(), NewLocOps.end(),
12393 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12394 bool Match = Op == FromLocOp;
12395 Changed |= Match;
12396 return Match;
12397 },
12398 ToLocOp);
12399 // Ignore this SDDbgValue if we didn't find a matching location.
12400 if (!Changed)
12401 continue;
12402
12403 DIVariable *Var = Dbg->getVariable();
12404 auto *Expr = Dbg->getExpression();
12405 // If a fragment is requested, update the expression.
12406 if (SizeInBits) {
12407 // When splitting a larger (e.g., sign-extended) value whose
12408 // lower bits are described with an SDDbgValue, do not attempt
12409 // to transfer the SDDbgValue to the upper bits.
12410 if (auto FI = Expr->getFragmentInfo())
12411 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12412 continue;
12413 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12414 SizeInBits);
12415 if (!Fragment)
12416 continue;
12417 Expr = *Fragment;
12418 }
12419
12420 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12421 // Clone the SDDbgValue and move it to To.
12422 SDDbgValue *Clone = getDbgValueList(
12423 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12424 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12425 Dbg->isVariadic());
12426 ClonedDVs.push_back(Clone);
12427
12428 if (InvalidateDbg) {
12429 // Invalidate value and indicate the SDDbgValue should not be emitted.
12430 Dbg->setIsInvalidated();
12431 Dbg->setIsEmitted();
12432 }
12433 }
12434
12435 for (SDDbgValue *Dbg : ClonedDVs) {
12436 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12437 "Transferred DbgValues should depend on the new SDNode");
12438 AddDbgValue(Dbg, false);
12439 }
12440}
12441
12443 if (!N.getHasDebugValue())
12444 return;
12445
12446 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12447 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12448 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12449 return SDDbgOperand::fromNode(Node, ResNo);
12450 };
12451
12453 for (auto *DV : GetDbgValues(&N)) {
12454 if (DV->isInvalidated())
12455 continue;
12456 switch (N.getOpcode()) {
12457 default:
12458 break;
12459 case ISD::ADD: {
12460 SDValue N0 = N.getOperand(0);
12461 SDValue N1 = N.getOperand(1);
12462 if (!isa<ConstantSDNode>(N0)) {
12463 bool RHSConstant = isa<ConstantSDNode>(N1);
12465 if (RHSConstant)
12466 Offset = N.getConstantOperandVal(1);
12467 // We are not allowed to turn indirect debug values variadic, so
12468 // don't salvage those.
12469 if (!RHSConstant && DV->isIndirect())
12470 continue;
12471
12472 // Rewrite an ADD constant node into a DIExpression. Since we are
12473 // performing arithmetic to compute the variable's *value* in the
12474 // DIExpression, we need to mark the expression with a
12475 // DW_OP_stack_value.
12476 auto *DIExpr = DV->getExpression();
12477 auto NewLocOps = DV->copyLocationOps();
12478 bool Changed = false;
12479 size_t OrigLocOpsSize = NewLocOps.size();
12480 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12481 // We're not given a ResNo to compare against because the whole
12482 // node is going away. We know that any ISD::ADD only has one
12483 // result, so we can assume any node match is using the result.
12484 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12485 NewLocOps[i].getSDNode() != &N)
12486 continue;
12487 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12488 if (RHSConstant) {
12491 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12492 } else {
12493 // Convert to a variadic expression (if not already).
12494 // convertToVariadicExpression() returns a const pointer, so we use
12495 // a temporary const variable here.
12496 const auto *TmpDIExpr =
12500 ExprOps.push_back(NewLocOps.size());
12501 ExprOps.push_back(dwarf::DW_OP_plus);
12502 SDDbgOperand RHS =
12504 NewLocOps.push_back(RHS);
12505 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12506 }
12507 Changed = true;
12508 }
12509 (void)Changed;
12510 assert(Changed && "Salvage target doesn't use N");
12511
12512 bool IsVariadic =
12513 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12514
12515 auto AdditionalDependencies = DV->getAdditionalDependencies();
12516 SDDbgValue *Clone = getDbgValueList(
12517 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12518 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12519 ClonedDVs.push_back(Clone);
12520 DV->setIsInvalidated();
12521 DV->setIsEmitted();
12522 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12523 N0.getNode()->dumprFull(this);
12524 dbgs() << " into " << *DIExpr << '\n');
12525 }
12526 break;
12527 }
12528 case ISD::TRUNCATE: {
12529 SDValue N0 = N.getOperand(0);
12530 TypeSize FromSize = N0.getValueSizeInBits();
12531 TypeSize ToSize = N.getValueSizeInBits(0);
12532
12533 DIExpression *DbgExpression = DV->getExpression();
12534 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12535 auto NewLocOps = DV->copyLocationOps();
12536 bool Changed = false;
12537 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12538 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12539 NewLocOps[i].getSDNode() != &N)
12540 continue;
12541
12542 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12543 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12544 Changed = true;
12545 }
12546 assert(Changed && "Salvage target doesn't use N");
12547 (void)Changed;
12548
12549 SDDbgValue *Clone =
12550 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12551 DV->getAdditionalDependencies(), DV->isIndirect(),
12552 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12553
12554 ClonedDVs.push_back(Clone);
12555 DV->setIsInvalidated();
12556 DV->setIsEmitted();
12557 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12558 dbgs() << " into " << *DbgExpression << '\n');
12559 break;
12560 }
12561 }
12562 }
12563
12564 for (SDDbgValue *Dbg : ClonedDVs) {
12565 assert((!Dbg->getSDNodes().empty() ||
12566 llvm::any_of(Dbg->getLocationOps(),
12567 [&](const SDDbgOperand &Op) {
12568 return Op.getKind() == SDDbgOperand::FRAMEIX;
12569 })) &&
12570 "Salvaged DbgValue should depend on a new SDNode");
12571 AddDbgValue(Dbg, false);
12572 }
12573}
12574
12575/// Creates a SDDbgLabel node.
12577 const DebugLoc &DL, unsigned O) {
12578 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12579 "Expected inlined-at fields to agree");
12580 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12581}
12582
12583namespace {
12584
12585/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12586/// pointed to by a use iterator is deleted, increment the use iterator
12587/// so that it doesn't dangle.
12588///
12589class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12592
12593 void NodeDeleted(SDNode *N, SDNode *E) override {
12594 // Increment the iterator as needed.
12595 while (UI != UE && N == UI->getUser())
12596 ++UI;
12597 }
12598
12599public:
12600 RAUWUpdateListener(SelectionDAG &d,
12603 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12604};
12605
12606} // end anonymous namespace
12607
12608/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12609/// This can cause recursive merging of nodes in the DAG.
12610///
12611/// This version assumes From has a single result value.
12612///
12614 SDNode *From = FromN.getNode();
12615 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12616 "Cannot replace with this method!");
12617 assert(From != To.getNode() && "Cannot replace uses of with self");
12618
12619 // Preserve Debug Values
12620 transferDbgValues(FromN, To);
12621 // Preserve extra info.
12622 copyExtraInfo(From, To.getNode());
12623
12624 // Iterate over all the existing uses of From. New uses will be added
12625 // to the beginning of the use list, which we avoid visiting.
12626 // This specifically avoids visiting uses of From that arise while the
12627 // replacement is happening, because any such uses would be the result
12628 // of CSE: If an existing node looks like From after one of its operands
12629 // is replaced by To, we don't want to replace of all its users with To
12630 // too. See PR3018 for more info.
12631 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12632 RAUWUpdateListener Listener(*this, UI, UE);
12633 while (UI != UE) {
12634 SDNode *User = UI->getUser();
12635
12636 // This node is about to morph, remove its old self from the CSE maps.
12637 RemoveNodeFromCSEMaps(User);
12638
12639 // A user can appear in a use list multiple times, and when this
12640 // happens the uses are usually next to each other in the list.
12641 // To help reduce the number of CSE recomputations, process all
12642 // the uses of this user that we can find this way.
12643 do {
12644 SDUse &Use = *UI;
12645 ++UI;
12646 Use.set(To);
12647 if (To->isDivergent() != From->isDivergent())
12649 } while (UI != UE && UI->getUser() == User);
12650 // Now that we have modified User, add it back to the CSE maps. If it
12651 // already exists there, recursively merge the results together.
12652 AddModifiedNodeToCSEMaps(User);
12653 }
12654
12655 // If we just RAUW'd the root, take note.
12656 if (FromN == getRoot())
12657 setRoot(To);
12658}
12659
12660/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12661/// This can cause recursive merging of nodes in the DAG.
12662///
12663/// This version assumes that for each value of From, there is a
12664/// corresponding value in To in the same position with the same type.
12665///
12667#ifndef NDEBUG
12668 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12669 assert((!From->hasAnyUseOfValue(i) ||
12670 From->getValueType(i) == To->getValueType(i)) &&
12671 "Cannot use this version of ReplaceAllUsesWith!");
12672#endif
12673
12674 // Handle the trivial case.
12675 if (From == To)
12676 return;
12677
12678 // Preserve Debug Info. Only do this if there's a use.
12679 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12680 if (From->hasAnyUseOfValue(i)) {
12681 assert((i < To->getNumValues()) && "Invalid To location");
12682 transferDbgValues(SDValue(From, i), SDValue(To, i));
12683 }
12684 // Preserve extra info.
12685 copyExtraInfo(From, To);
12686
12687 // Iterate over just the existing users of From. See the comments in
12688 // the ReplaceAllUsesWith above.
12689 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12690 RAUWUpdateListener Listener(*this, UI, UE);
12691 while (UI != UE) {
12692 SDNode *User = UI->getUser();
12693
12694 // This node is about to morph, remove its old self from the CSE maps.
12695 RemoveNodeFromCSEMaps(User);
12696
12697 // A user can appear in a use list multiple times, and when this
12698 // happens the uses are usually next to each other in the list.
12699 // To help reduce the number of CSE recomputations, process all
12700 // the uses of this user that we can find this way.
12701 do {
12702 SDUse &Use = *UI;
12703 ++UI;
12704 Use.setNode(To);
12705 if (To->isDivergent() != From->isDivergent())
12707 } while (UI != UE && UI->getUser() == User);
12708
12709 // Now that we have modified User, add it back to the CSE maps. If it
12710 // already exists there, recursively merge the results together.
12711 AddModifiedNodeToCSEMaps(User);
12712 }
12713
12714 // If we just RAUW'd the root, take note.
12715 if (From == getRoot().getNode())
12716 setRoot(SDValue(To, getRoot().getResNo()));
12717}
12718
12719/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12720/// This can cause recursive merging of nodes in the DAG.
12721///
12722/// This version can replace From with any result values. To must match the
12723/// number and types of values returned by From.
12725 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12726 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12727
12728 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12729 // Preserve Debug Info.
12730 transferDbgValues(SDValue(From, i), To[i]);
12731 // Preserve extra info.
12732 copyExtraInfo(From, To[i].getNode());
12733 }
12734
12735 // Iterate over just the existing users of From. See the comments in
12736 // the ReplaceAllUsesWith above.
12737 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12738 RAUWUpdateListener Listener(*this, UI, UE);
12739 while (UI != UE) {
12740 SDNode *User = UI->getUser();
12741
12742 // This node is about to morph, remove its old self from the CSE maps.
12743 RemoveNodeFromCSEMaps(User);
12744
12745 // A user can appear in a use list multiple times, and when this happens the
12746 // uses are usually next to each other in the list. To help reduce the
12747 // number of CSE and divergence recomputations, process all the uses of this
12748 // user that we can find this way.
12749 bool To_IsDivergent = false;
12750 do {
12751 SDUse &Use = *UI;
12752 const SDValue &ToOp = To[Use.getResNo()];
12753 ++UI;
12754 Use.set(ToOp);
12755 if (ToOp.getValueType() != MVT::Other)
12756 To_IsDivergent |= ToOp->isDivergent();
12757 } while (UI != UE && UI->getUser() == User);
12758
12759 if (To_IsDivergent != From->isDivergent())
12761
12762 // Now that we have modified User, add it back to the CSE maps. If it
12763 // already exists there, recursively merge the results together.
12764 AddModifiedNodeToCSEMaps(User);
12765 }
12766
12767 // If we just RAUW'd the root, take note.
12768 if (From == getRoot().getNode())
12769 setRoot(SDValue(To[getRoot().getResNo()]));
12770}
12771
12772/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12773/// uses of other values produced by From.getNode() alone. The Deleted
12774/// vector is handled the same way as for ReplaceAllUsesWith.
12776 // Handle the really simple, really trivial case efficiently.
12777 if (From == To) return;
12778
12779 // Handle the simple, trivial, case efficiently.
12780 if (From.getNode()->getNumValues() == 1) {
12781 ReplaceAllUsesWith(From, To);
12782 return;
12783 }
12784
12785 // Preserve Debug Info.
12786 transferDbgValues(From, To);
12787 copyExtraInfo(From.getNode(), To.getNode());
12788
12789 // Iterate over just the existing users of From. See the comments in
12790 // the ReplaceAllUsesWith above.
12791 SDNode::use_iterator UI = From.getNode()->use_begin(),
12792 UE = From.getNode()->use_end();
12793 RAUWUpdateListener Listener(*this, UI, UE);
12794 while (UI != UE) {
12795 SDNode *User = UI->getUser();
12796 bool UserRemovedFromCSEMaps = false;
12797
12798 // A user can appear in a use list multiple times, and when this
12799 // happens the uses are usually next to each other in the list.
12800 // To help reduce the number of CSE recomputations, process all
12801 // the uses of this user that we can find this way.
12802 do {
12803 SDUse &Use = *UI;
12804
12805 // Skip uses of different values from the same node.
12806 if (Use.getResNo() != From.getResNo()) {
12807 ++UI;
12808 continue;
12809 }
12810
12811 // If this node hasn't been modified yet, it's still in the CSE maps,
12812 // so remove its old self from the CSE maps.
12813 if (!UserRemovedFromCSEMaps) {
12814 RemoveNodeFromCSEMaps(User);
12815 UserRemovedFromCSEMaps = true;
12816 }
12817
12818 ++UI;
12819 Use.set(To);
12820 if (To->isDivergent() != From->isDivergent())
12822 } while (UI != UE && UI->getUser() == User);
12823 // We are iterating over all uses of the From node, so if a use
12824 // doesn't use the specific value, no changes are made.
12825 if (!UserRemovedFromCSEMaps)
12826 continue;
12827
12828 // Now that we have modified User, add it back to the CSE maps. If it
12829 // already exists there, recursively merge the results together.
12830 AddModifiedNodeToCSEMaps(User);
12831 }
12832
12833 // If we just RAUW'd the root, take note.
12834 if (From == getRoot())
12835 setRoot(To);
12836}
12837
12838namespace {
12839
12840/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12841/// to record information about a use.
12842struct UseMemo {
12843 SDNode *User;
12844 unsigned Index;
12845 SDUse *Use;
12846};
12847
12848/// operator< - Sort Memos by User.
12849bool operator<(const UseMemo &L, const UseMemo &R) {
12850 return (intptr_t)L.User < (intptr_t)R.User;
12851}
12852
12853/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12854/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12855/// the node already has been taken care of recursively.
12856class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12857 SmallVectorImpl<UseMemo> &Uses;
12858
12859 void NodeDeleted(SDNode *N, SDNode *E) override {
12860 for (UseMemo &Memo : Uses)
12861 if (Memo.User == N)
12862 Memo.User = nullptr;
12863 }
12864
12865public:
12866 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12867 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12868};
12869
12870} // end anonymous namespace
12871
12872/// Return true if a glue output should propagate divergence information.
12874 switch (Node->getOpcode()) {
12875 case ISD::CopyFromReg:
12876 case ISD::CopyToReg:
12877 return false;
12878 default:
12879 return true;
12880 }
12881
12882 llvm_unreachable("covered opcode switch");
12883}
12884
12886 if (TLI->isSDNodeAlwaysUniform(N)) {
12887 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12888 "Conflicting divergence information!");
12889 return false;
12890 }
12891 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12892 return true;
12893 for (const auto &Op : N->ops()) {
12894 EVT VT = Op.getValueType();
12895
12896 // Skip Chain. It does not carry divergence.
12897 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12898 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12899 return true;
12900 }
12901 return false;
12902}
12903
12905 SmallVector<SDNode *, 16> Worklist(1, N);
12906 do {
12907 N = Worklist.pop_back_val();
12908 bool IsDivergent = calculateDivergence(N);
12909 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12910 N->SDNodeBits.IsDivergent = IsDivergent;
12911 llvm::append_range(Worklist, N->users());
12912 }
12913 } while (!Worklist.empty());
12914}
12915
12916void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12918 Order.reserve(AllNodes.size());
12919 for (auto &N : allnodes()) {
12920 unsigned NOps = N.getNumOperands();
12921 Degree[&N] = NOps;
12922 if (0 == NOps)
12923 Order.push_back(&N);
12924 }
12925 for (size_t I = 0; I != Order.size(); ++I) {
12926 SDNode *N = Order[I];
12927 for (auto *U : N->users()) {
12928 unsigned &UnsortedOps = Degree[U];
12929 if (0 == --UnsortedOps)
12930 Order.push_back(U);
12931 }
12932 }
12933}
12934
12935#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12936void SelectionDAG::VerifyDAGDivergence() {
12937 std::vector<SDNode *> TopoOrder;
12938 CreateTopologicalOrder(TopoOrder);
12939 for (auto *N : TopoOrder) {
12940 assert(calculateDivergence(N) == N->isDivergent() &&
12941 "Divergence bit inconsistency detected");
12942 }
12943}
12944#endif
12945
12946/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12947/// uses of other values produced by From.getNode() alone. The same value
12948/// may appear in both the From and To list. The Deleted vector is
12949/// handled the same way as for ReplaceAllUsesWith.
12951 const SDValue *To,
12952 unsigned Num){
12953 // Handle the simple, trivial case efficiently.
12954 if (Num == 1)
12955 return ReplaceAllUsesOfValueWith(*From, *To);
12956
12957 transferDbgValues(*From, *To);
12958 copyExtraInfo(From->getNode(), To->getNode());
12959
12960 // Read up all the uses and make records of them. This helps
12961 // processing new uses that are introduced during the
12962 // replacement process.
12964 for (unsigned i = 0; i != Num; ++i) {
12965 unsigned FromResNo = From[i].getResNo();
12966 SDNode *FromNode = From[i].getNode();
12967 for (SDUse &Use : FromNode->uses()) {
12968 if (Use.getResNo() == FromResNo) {
12969 UseMemo Memo = {Use.getUser(), i, &Use};
12970 Uses.push_back(Memo);
12971 }
12972 }
12973 }
12974
12975 // Sort the uses, so that all the uses from a given User are together.
12977 RAUOVWUpdateListener Listener(*this, Uses);
12978
12979 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12980 UseIndex != UseIndexEnd; ) {
12981 // We know that this user uses some value of From. If it is the right
12982 // value, update it.
12983 SDNode *User = Uses[UseIndex].User;
12984 // If the node has been deleted by recursive CSE updates when updating
12985 // another node, then just skip this entry.
12986 if (User == nullptr) {
12987 ++UseIndex;
12988 continue;
12989 }
12990
12991 // This node is about to morph, remove its old self from the CSE maps.
12992 RemoveNodeFromCSEMaps(User);
12993
12994 // The Uses array is sorted, so all the uses for a given User
12995 // are next to each other in the list.
12996 // To help reduce the number of CSE recomputations, process all
12997 // the uses of this user that we can find this way.
12998 do {
12999 unsigned i = Uses[UseIndex].Index;
13000 SDUse &Use = *Uses[UseIndex].Use;
13001 ++UseIndex;
13002
13003 Use.set(To[i]);
13004 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
13005
13006 // Now that we have modified User, add it back to the CSE maps. If it
13007 // already exists there, recursively merge the results together.
13008 AddModifiedNodeToCSEMaps(User);
13009 }
13010}
13011
13012/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
13013/// based on their topological order. It returns the maximum id and a vector
13014/// of the SDNodes* in assigned order by reference.
13016 unsigned DAGSize = 0;
13017
13018 // SortedPos tracks the progress of the algorithm. Nodes before it are
13019 // sorted, nodes after it are unsorted. When the algorithm completes
13020 // it is at the end of the list.
13021 allnodes_iterator SortedPos = allnodes_begin();
13022
13023 // Visit all the nodes. Move nodes with no operands to the front of
13024 // the list immediately. Annotate nodes that do have operands with their
13025 // operand count. Before we do this, the Node Id fields of the nodes
13026 // may contain arbitrary values. After, the Node Id fields for nodes
13027 // before SortedPos will contain the topological sort index, and the
13028 // Node Id fields for nodes At SortedPos and after will contain the
13029 // count of outstanding operands.
13031 checkForCycles(&N, this);
13032 unsigned Degree = N.getNumOperands();
13033 if (Degree == 0) {
13034 // A node with no uses, add it to the result array immediately.
13035 N.setNodeId(DAGSize++);
13036 allnodes_iterator Q(&N);
13037 if (Q != SortedPos)
13038 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
13039 assert(SortedPos != AllNodes.end() && "Overran node list");
13040 ++SortedPos;
13041 } else {
13042 // Temporarily use the Node Id as scratch space for the degree count.
13043 N.setNodeId(Degree);
13044 }
13045 }
13046
13047 // Visit all the nodes. As we iterate, move nodes into sorted order,
13048 // such that by the time the end is reached all nodes will be sorted.
13049 for (SDNode &Node : allnodes()) {
13050 SDNode *N = &Node;
13051 checkForCycles(N, this);
13052 // N is in sorted position, so all its uses have one less operand
13053 // that needs to be sorted.
13054 for (SDNode *P : N->users()) {
13055 unsigned Degree = P->getNodeId();
13056 assert(Degree != 0 && "Invalid node degree");
13057 --Degree;
13058 if (Degree == 0) {
13059 // All of P's operands are sorted, so P may sorted now.
13060 P->setNodeId(DAGSize++);
13061 if (P->getIterator() != SortedPos)
13062 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
13063 assert(SortedPos != AllNodes.end() && "Overran node list");
13064 ++SortedPos;
13065 } else {
13066 // Update P's outstanding operand count.
13067 P->setNodeId(Degree);
13068 }
13069 }
13070 if (Node.getIterator() == SortedPos) {
13071#ifndef NDEBUG
13073 SDNode *S = &*++I;
13074 dbgs() << "Overran sorted position:\n";
13075 S->dumprFull(this); dbgs() << "\n";
13076 dbgs() << "Checking if this is due to cycles\n";
13077 checkForCycles(this, true);
13078#endif
13079 llvm_unreachable(nullptr);
13080 }
13081 }
13082
13083 assert(SortedPos == AllNodes.end() &&
13084 "Topological sort incomplete!");
13085 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
13086 "First node in topological sort is not the entry token!");
13087 assert(AllNodes.front().getNodeId() == 0 &&
13088 "First node in topological sort has non-zero id!");
13089 assert(AllNodes.front().getNumOperands() == 0 &&
13090 "First node in topological sort has operands!");
13091 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
13092 "Last node in topologic sort has unexpected id!");
13093 assert(AllNodes.back().use_empty() &&
13094 "Last node in topologic sort has users!");
13095 assert(DAGSize == allnodes_size() && "Node count mismatch!");
13096 return DAGSize;
13097}
13098
13100 SmallVectorImpl<const SDNode *> &SortedNodes) const {
13101 SortedNodes.clear();
13102 // Node -> remaining number of outstanding operands.
13103 DenseMap<const SDNode *, unsigned> RemainingOperands;
13104
13105 // Put nodes without any operands into SortedNodes first.
13106 for (const SDNode &N : allnodes()) {
13107 checkForCycles(&N, this);
13108 unsigned NumOperands = N.getNumOperands();
13109 if (NumOperands == 0)
13110 SortedNodes.push_back(&N);
13111 else
13112 // Record their total number of outstanding operands.
13113 RemainingOperands[&N] = NumOperands;
13114 }
13115
13116 // A node is pushed into SortedNodes when all of its operands (predecessors in
13117 // the graph) are also in SortedNodes.
13118 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
13119 const SDNode *N = SortedNodes[i];
13120 for (const SDNode *U : N->users()) {
13121 // HandleSDNode is never part of a DAG and therefore has no entry in
13122 // RemainingOperands.
13123 if (U->getOpcode() == ISD::HANDLENODE)
13124 continue;
13125 unsigned &NumRemOperands = RemainingOperands[U];
13126 assert(NumRemOperands && "Invalid number of remaining operands");
13127 --NumRemOperands;
13128 if (!NumRemOperands)
13129 SortedNodes.push_back(U);
13130 }
13131 }
13132
13133 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
13134 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
13135 "First node in topological sort is not the entry token");
13136 assert(SortedNodes.front()->getNumOperands() == 0 &&
13137 "First node in topological sort has operands");
13138}
13139
13140/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
13141/// value is produced by SD.
13142void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
13143 for (SDNode *SD : DB->getSDNodes()) {
13144 if (!SD)
13145 continue;
13146 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
13147 SD->setHasDebugValue(true);
13148 }
13149 DbgInfo->add(DB, isParameter);
13150}
13151
13152void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
13153
13155 SDValue NewMemOpChain) {
13156 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
13157 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
13158 // The new memory operation must have the same position as the old load in
13159 // terms of memory dependency. Create a TokenFactor for the old load and new
13160 // memory operation and update uses of the old load's output chain to use that
13161 // TokenFactor.
13162 if (OldChain == NewMemOpChain || OldChain.use_empty())
13163 return NewMemOpChain;
13164
13165 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
13166 OldChain, NewMemOpChain);
13167 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
13168 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
13169 return TokenFactor;
13170}
13171
13173 SDValue NewMemOp) {
13174 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
13175 SDValue OldChain = SDValue(OldLoad, 1);
13176 SDValue NewMemOpChain = NewMemOp.getValue(1);
13177 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
13178}
13179
13181 Function **OutFunction) {
13182 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
13183
13184 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
13185 auto *Module = MF->getFunction().getParent();
13186 auto *Function = Module->getFunction(Symbol);
13187
13188 if (OutFunction != nullptr)
13189 *OutFunction = Function;
13190
13191 if (Function != nullptr) {
13192 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
13193 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
13194 }
13195
13196 std::string ErrorStr;
13197 raw_string_ostream ErrorFormatter(ErrorStr);
13198 ErrorFormatter << "Undefined external symbol ";
13199 ErrorFormatter << '"' << Symbol << '"';
13200 report_fatal_error(Twine(ErrorStr));
13201}
13202
13203//===----------------------------------------------------------------------===//
13204// SDNode Class
13205//===----------------------------------------------------------------------===//
13206
13209 return Const != nullptr && Const->isZero();
13210}
13211
13213 return V.isUndef() || isNullConstant(V);
13214}
13215
13218 return Const != nullptr && Const->isZero() && !Const->isNegative();
13219}
13220
13223 return Const != nullptr && Const->isAllOnes();
13224}
13225
13228 return Const != nullptr && Const->isOne();
13229}
13230
13233 return Const != nullptr && Const->isMinSignedValue();
13234}
13235
13236bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
13237 unsigned OperandNo) {
13238 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13239 // TODO: Target-specific opcodes could be added.
13240 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
13241 /*AllowTruncation*/ true)) {
13242 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
13243 switch (Opcode) {
13244 case ISD::ADD:
13245 case ISD::OR:
13246 case ISD::XOR:
13247 case ISD::UMAX:
13248 return Const.isZero();
13249 case ISD::MUL:
13250 return Const.isOne();
13251 case ISD::AND:
13252 case ISD::UMIN:
13253 return Const.isAllOnes();
13254 case ISD::SMAX:
13255 return Const.isMinSignedValue();
13256 case ISD::SMIN:
13257 return Const.isMaxSignedValue();
13258 case ISD::SUB:
13259 case ISD::SHL:
13260 case ISD::SRA:
13261 case ISD::SRL:
13262 return OperandNo == 1 && Const.isZero();
13263 case ISD::UDIV:
13264 case ISD::SDIV:
13265 return OperandNo == 1 && Const.isOne();
13266 }
13267 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13268 switch (Opcode) {
13269 case ISD::FADD:
13270 return ConstFP->isZero() &&
13271 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13272 case ISD::FSUB:
13273 return OperandNo == 1 && ConstFP->isZero() &&
13274 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13275 case ISD::FMUL:
13276 return ConstFP->isExactlyValue(1.0);
13277 case ISD::FDIV:
13278 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13279 case ISD::FMINNUM:
13280 case ISD::FMAXNUM: {
13281 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13282 EVT VT = V.getValueType();
13283 const fltSemantics &Semantics = VT.getFltSemantics();
13284 APFloat NeutralAF = !Flags.hasNoNaNs()
13285 ? APFloat::getQNaN(Semantics)
13286 : !Flags.hasNoInfs()
13287 ? APFloat::getInf(Semantics)
13288 : APFloat::getLargest(Semantics);
13289 if (Opcode == ISD::FMAXNUM)
13290 NeutralAF.changeSign();
13291
13292 return ConstFP->isExactlyValue(NeutralAF);
13293 }
13294 }
13295 }
13296 return false;
13297}
13298
13300 while (V.getOpcode() == ISD::BITCAST)
13301 V = V.getOperand(0);
13302 return V;
13303}
13304
13306 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13307 V = V.getOperand(0);
13308 return V;
13309}
13310
13312 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13313 V = V.getOperand(0);
13314 return V;
13315}
13316
13318 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13319 SDValue InVec = V.getOperand(0);
13320 SDValue EltNo = V.getOperand(2);
13321 EVT VT = InVec.getValueType();
13322 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13323 if (IndexC && VT.isFixedLengthVector() &&
13324 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13325 !DemandedElts[IndexC->getZExtValue()]) {
13326 V = InVec;
13327 continue;
13328 }
13329 break;
13330 }
13331 return V;
13332}
13333
13335 while (V.getOpcode() == ISD::TRUNCATE)
13336 V = V.getOperand(0);
13337 return V;
13338}
13339
13340bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13341 if (V.getOpcode() != ISD::XOR)
13342 return false;
13343 V = peekThroughBitcasts(V.getOperand(1));
13344 unsigned NumBits = V.getScalarValueSizeInBits();
13345 ConstantSDNode *C =
13346 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13347 return C && (C->getAPIntValue().countr_one() >= NumBits);
13348}
13349
13351 bool AllowTruncation) {
13352 EVT VT = N.getValueType();
13353 APInt DemandedElts = VT.isFixedLengthVector()
13355 : APInt(1, 1);
13356 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13357}
13358
13360 bool AllowUndefs,
13361 bool AllowTruncation) {
13363 return CN;
13364
13365 // SplatVectors can truncate their operands. Ignore that case here unless
13366 // AllowTruncation is set.
13367 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13368 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13369 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13370 EVT CVT = CN->getValueType(0);
13371 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13372 if (AllowTruncation || CVT == VecEltVT)
13373 return CN;
13374 }
13375 }
13376
13378 BitVector UndefElements;
13379 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13380
13381 // BuildVectors can truncate their operands. Ignore that case here unless
13382 // AllowTruncation is set.
13383 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13384 if (CN && (UndefElements.none() || AllowUndefs)) {
13385 EVT CVT = CN->getValueType(0);
13386 EVT NSVT = N.getValueType().getScalarType();
13387 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13388 if (AllowTruncation || (CVT == NSVT))
13389 return CN;
13390 }
13391 }
13392
13393 return nullptr;
13394}
13395
13397 EVT VT = N.getValueType();
13398 APInt DemandedElts = VT.isFixedLengthVector()
13400 : APInt(1, 1);
13401 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13402}
13403
13405 const APInt &DemandedElts,
13406 bool AllowUndefs) {
13408 return CN;
13409
13411 BitVector UndefElements;
13412 ConstantFPSDNode *CN =
13413 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13414 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13415 if (CN && (UndefElements.none() || AllowUndefs))
13416 return CN;
13417 }
13418
13419 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13420 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13421 return CN;
13422
13423 return nullptr;
13424}
13425
13426bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13427 // TODO: may want to use peekThroughBitcast() here.
13428 ConstantSDNode *C =
13429 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13430 return C && C->isZero();
13431}
13432
13433bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13434 ConstantSDNode *C =
13435 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13436 return C && C->isOne();
13437}
13438
13439bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13440 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13441 return C && C->isExactlyValue(1.0);
13442}
13443
13444bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13446 unsigned BitWidth = N.getScalarValueSizeInBits();
13447 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13448 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13449}
13450
13451bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13452 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13453 return C && APInt::isSameValue(C->getAPIntValue(),
13454 APInt(C->getAPIntValue().getBitWidth(), 1));
13455}
13456
13457bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13459 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13460 return C && C->isZero();
13461}
13462
13463bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13464 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13465 return C && C->isZero();
13466}
13467
13471
13473 unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt,
13475 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MemRefs(memrefs) {
13476 bool IsVolatile = false;
13477 bool IsNonTemporal = false;
13478 bool IsDereferenceable = true;
13479 bool IsInvariant = true;
13480 for (const MachineMemOperand *MMO : memoperands()) {
13481 IsVolatile |= MMO->isVolatile();
13482 IsNonTemporal |= MMO->isNonTemporal();
13483 IsDereferenceable &= MMO->isDereferenceable();
13484 IsInvariant &= MMO->isInvariant();
13485 }
13486 MemSDNodeBits.IsVolatile = IsVolatile;
13487 MemSDNodeBits.IsNonTemporal = IsNonTemporal;
13488 MemSDNodeBits.IsDereferenceable = IsDereferenceable;
13489 MemSDNodeBits.IsInvariant = IsInvariant;
13490
13491 // For the single-MMO case, we check here that the size of the memory operand
13492 // fits within the size of the MMO. This is because the MMO might indicate
13493 // only a possible address range instead of specifying the affected memory
13494 // addresses precisely.
13497 getMemOperand()->getSize().getValue())) &&
13498 "Size mismatch!");
13499}
13500
13501/// Profile - Gather unique data for the node.
13502///
13504 AddNodeIDNode(ID, this);
13505}
13506
13507namespace {
13508
13509 struct EVTArray {
13510 std::vector<EVT> VTs;
13511
13512 EVTArray() {
13513 VTs.reserve(MVT::VALUETYPE_SIZE);
13514 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13515 VTs.push_back(MVT((MVT::SimpleValueType)i));
13516 }
13517 };
13518
13519} // end anonymous namespace
13520
13521/// getValueTypeList - Return a pointer to the specified value type.
13522///
13523const EVT *SDNode::getValueTypeList(MVT VT) {
13524 static EVTArray SimpleVTArray;
13525
13526 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13527 return &SimpleVTArray.VTs[VT.SimpleTy];
13528}
13529
13530/// hasAnyUseOfValue - Return true if there are any use of the indicated
13531/// value. This method ignores uses of other values defined by this operation.
13532bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13533 assert(Value < getNumValues() && "Bad value!");
13534
13535 for (SDUse &U : uses())
13536 if (U.getResNo() == Value)
13537 return true;
13538
13539 return false;
13540}
13541
13542/// isOnlyUserOf - Return true if this node is the only use of N.
13543bool SDNode::isOnlyUserOf(const SDNode *N) const {
13544 bool Seen = false;
13545 for (const SDNode *User : N->users()) {
13546 if (User == this)
13547 Seen = true;
13548 else
13549 return false;
13550 }
13551
13552 return Seen;
13553}
13554
13555/// Return true if the only users of N are contained in Nodes.
13557 bool Seen = false;
13558 for (const SDNode *User : N->users()) {
13559 if (llvm::is_contained(Nodes, User))
13560 Seen = true;
13561 else
13562 return false;
13563 }
13564
13565 return Seen;
13566}
13567
13568/// Return true if the referenced return value is an operand of N.
13569bool SDValue::isOperandOf(const SDNode *N) const {
13570 return is_contained(N->op_values(), *this);
13571}
13572
13573bool SDNode::isOperandOf(const SDNode *N) const {
13574 return any_of(N->op_values(),
13575 [this](SDValue Op) { return this == Op.getNode(); });
13576}
13577
13578/// reachesChainWithoutSideEffects - Return true if this operand (which must
13579/// be a chain) reaches the specified operand without crossing any
13580/// side-effecting instructions on any chain path. In practice, this looks
13581/// through token factors and non-volatile loads. In order to remain efficient,
13582/// this only looks a couple of nodes in, it does not do an exhaustive search.
13583///
13584/// Note that we only need to examine chains when we're searching for
13585/// side-effects; SelectionDAG requires that all side-effects are represented
13586/// by chains, even if another operand would force a specific ordering. This
13587/// constraint is necessary to allow transformations like splitting loads.
13589 unsigned Depth) const {
13590 if (*this == Dest) return true;
13591
13592 // Don't search too deeply, we just want to be able to see through
13593 // TokenFactor's etc.
13594 if (Depth == 0) return false;
13595
13596 // If this is a token factor, all inputs to the TF happen in parallel.
13597 if (getOpcode() == ISD::TokenFactor) {
13598 // First, try a shallow search.
13599 if (is_contained((*this)->ops(), Dest)) {
13600 // We found the chain we want as an operand of this TokenFactor.
13601 // Essentially, we reach the chain without side-effects if we could
13602 // serialize the TokenFactor into a simple chain of operations with
13603 // Dest as the last operation. This is automatically true if the
13604 // chain has one use: there are no other ordering constraints.
13605 // If the chain has more than one use, we give up: some other
13606 // use of Dest might force a side-effect between Dest and the current
13607 // node.
13608 if (Dest.hasOneUse())
13609 return true;
13610 }
13611 // Next, try a deep search: check whether every operand of the TokenFactor
13612 // reaches Dest.
13613 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13614 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13615 });
13616 }
13617
13618 // Loads don't have side effects, look through them.
13619 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13620 if (Ld->isUnordered())
13621 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13622 }
13623 return false;
13624}
13625
13626bool SDNode::hasPredecessor(const SDNode *N) const {
13629 Worklist.push_back(this);
13630 return hasPredecessorHelper(N, Visited, Worklist);
13631}
13632
13634 this->Flags &= Flags;
13635}
13636
13637SDValue
13639 ArrayRef<ISD::NodeType> CandidateBinOps,
13640 bool AllowPartials) {
13641 // The pattern must end in an extract from index 0.
13642 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13643 !isNullConstant(Extract->getOperand(1)))
13644 return SDValue();
13645
13646 // Match against one of the candidate binary ops.
13647 SDValue Op = Extract->getOperand(0);
13648 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13649 return Op.getOpcode() == unsigned(BinOp);
13650 }))
13651 return SDValue();
13652
13653 // Floating-point reductions may require relaxed constraints on the final step
13654 // of the reduction because they may reorder intermediate operations.
13655 unsigned CandidateBinOp = Op.getOpcode();
13656 if (Op.getValueType().isFloatingPoint()) {
13657 SDNodeFlags Flags = Op->getFlags();
13658 switch (CandidateBinOp) {
13659 case ISD::FADD:
13660 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13661 return SDValue();
13662 break;
13663 default:
13664 llvm_unreachable("Unhandled FP opcode for binop reduction");
13665 }
13666 }
13667
13668 // Matching failed - attempt to see if we did enough stages that a partial
13669 // reduction from a subvector is possible.
13670 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13671 if (!AllowPartials || !Op)
13672 return SDValue();
13673 EVT OpVT = Op.getValueType();
13674 EVT OpSVT = OpVT.getScalarType();
13675 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13676 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13677 return SDValue();
13678 BinOp = (ISD::NodeType)CandidateBinOp;
13679 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13680 };
13681
13682 // At each stage, we're looking for something that looks like:
13683 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13684 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13685 // i32 undef, i32 undef, i32 undef, i32 undef>
13686 // %a = binop <8 x i32> %op, %s
13687 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13688 // we expect something like:
13689 // <4,5,6,7,u,u,u,u>
13690 // <2,3,u,u,u,u,u,u>
13691 // <1,u,u,u,u,u,u,u>
13692 // While a partial reduction match would be:
13693 // <2,3,u,u,u,u,u,u>
13694 // <1,u,u,u,u,u,u,u>
13695 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13696 SDValue PrevOp;
13697 for (unsigned i = 0; i < Stages; ++i) {
13698 unsigned MaskEnd = (1 << i);
13699
13700 if (Op.getOpcode() != CandidateBinOp)
13701 return PartialReduction(PrevOp, MaskEnd);
13702
13703 SDValue Op0 = Op.getOperand(0);
13704 SDValue Op1 = Op.getOperand(1);
13705
13707 if (Shuffle) {
13708 Op = Op1;
13709 } else {
13710 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13711 Op = Op0;
13712 }
13713
13714 // The first operand of the shuffle should be the same as the other operand
13715 // of the binop.
13716 if (!Shuffle || Shuffle->getOperand(0) != Op)
13717 return PartialReduction(PrevOp, MaskEnd);
13718
13719 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13720 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13721 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13722 return PartialReduction(PrevOp, MaskEnd);
13723
13724 PrevOp = Op;
13725 }
13726
13727 // Handle subvector reductions, which tend to appear after the shuffle
13728 // reduction stages.
13729 while (Op.getOpcode() == CandidateBinOp) {
13730 unsigned NumElts = Op.getValueType().getVectorNumElements();
13731 SDValue Op0 = Op.getOperand(0);
13732 SDValue Op1 = Op.getOperand(1);
13733 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13735 Op0.getOperand(0) != Op1.getOperand(0))
13736 break;
13737 SDValue Src = Op0.getOperand(0);
13738 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13739 if (NumSrcElts != (2 * NumElts))
13740 break;
13741 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13742 Op1.getConstantOperandAPInt(1) == NumElts) &&
13743 !(Op1.getConstantOperandAPInt(1) == 0 &&
13744 Op0.getConstantOperandAPInt(1) == NumElts))
13745 break;
13746 Op = Src;
13747 }
13748
13749 BinOp = (ISD::NodeType)CandidateBinOp;
13750 return Op;
13751}
13752
13754 EVT VT = N->getValueType(0);
13755 EVT EltVT = VT.getVectorElementType();
13756 unsigned NE = VT.getVectorNumElements();
13757
13758 SDLoc dl(N);
13759
13760 // If ResNE is 0, fully unroll the vector op.
13761 if (ResNE == 0)
13762 ResNE = NE;
13763 else if (NE > ResNE)
13764 NE = ResNE;
13765
13766 if (N->getNumValues() == 2) {
13767 SmallVector<SDValue, 8> Scalars0, Scalars1;
13768 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13769 EVT VT1 = N->getValueType(1);
13770 EVT EltVT1 = VT1.getVectorElementType();
13771
13772 unsigned i;
13773 for (i = 0; i != NE; ++i) {
13774 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13775 SDValue Operand = N->getOperand(j);
13776 EVT OperandVT = Operand.getValueType();
13777
13778 // A vector operand; extract a single element.
13779 EVT OperandEltVT = OperandVT.getVectorElementType();
13780 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13781 }
13782
13783 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13784 Scalars0.push_back(EltOp);
13785 Scalars1.push_back(EltOp.getValue(1));
13786 }
13787
13788 for (; i < ResNE; ++i) {
13789 Scalars0.push_back(getUNDEF(EltVT));
13790 Scalars1.push_back(getUNDEF(EltVT1));
13791 }
13792
13793 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13794 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13795 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13796 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13797 return getMergeValues({Vec0, Vec1}, dl);
13798 }
13799
13800 assert(N->getNumValues() == 1 &&
13801 "Can't unroll a vector with multiple results!");
13802
13804 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13805
13806 unsigned i;
13807 for (i= 0; i != NE; ++i) {
13808 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13809 SDValue Operand = N->getOperand(j);
13810 EVT OperandVT = Operand.getValueType();
13811 if (OperandVT.isVector()) {
13812 // A vector operand; extract a single element.
13813 EVT OperandEltVT = OperandVT.getVectorElementType();
13814 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13815 } else {
13816 // A scalar operand; just use it as is.
13817 Operands[j] = Operand;
13818 }
13819 }
13820
13821 switch (N->getOpcode()) {
13822 default: {
13823 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13824 N->getFlags()));
13825 break;
13826 }
13827 case ISD::VSELECT:
13828 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13829 break;
13830 case ISD::SHL:
13831 case ISD::SRA:
13832 case ISD::SRL:
13833 case ISD::ROTL:
13834 case ISD::ROTR:
13835 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13836 getShiftAmountOperand(Operands[0].getValueType(),
13837 Operands[1])));
13838 break;
13840 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13841 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13842 Operands[0],
13843 getValueType(ExtVT)));
13844 break;
13845 }
13846 case ISD::ADDRSPACECAST: {
13847 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13848 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13849 ASC->getSrcAddressSpace(),
13850 ASC->getDestAddressSpace()));
13851 break;
13852 }
13853 }
13854 }
13855
13856 for (; i < ResNE; ++i)
13857 Scalars.push_back(getUNDEF(EltVT));
13858
13859 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13860 return getBuildVector(VecVT, dl, Scalars);
13861}
13862
13863std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13864 SDNode *N, unsigned ResNE) {
13865 unsigned Opcode = N->getOpcode();
13866 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13867 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13868 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13869 "Expected an overflow opcode");
13870
13871 EVT ResVT = N->getValueType(0);
13872 EVT OvVT = N->getValueType(1);
13873 EVT ResEltVT = ResVT.getVectorElementType();
13874 EVT OvEltVT = OvVT.getVectorElementType();
13875 SDLoc dl(N);
13876
13877 // If ResNE is 0, fully unroll the vector op.
13878 unsigned NE = ResVT.getVectorNumElements();
13879 if (ResNE == 0)
13880 ResNE = NE;
13881 else if (NE > ResNE)
13882 NE = ResNE;
13883
13884 SmallVector<SDValue, 8> LHSScalars;
13885 SmallVector<SDValue, 8> RHSScalars;
13886 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13887 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13888
13889 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13890 SDVTList VTs = getVTList(ResEltVT, SVT);
13891 SmallVector<SDValue, 8> ResScalars;
13892 SmallVector<SDValue, 8> OvScalars;
13893 for (unsigned i = 0; i < NE; ++i) {
13894 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13895 SDValue Ov =
13896 getSelect(dl, OvEltVT, Res.getValue(1),
13897 getBoolConstant(true, dl, OvEltVT, ResVT),
13898 getConstant(0, dl, OvEltVT));
13899
13900 ResScalars.push_back(Res);
13901 OvScalars.push_back(Ov);
13902 }
13903
13904 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13905 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13906
13907 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13908 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13909 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13910 getBuildVector(NewOvVT, dl, OvScalars));
13911}
13912
13915 unsigned Bytes,
13916 int Dist) const {
13917 if (LD->isVolatile() || Base->isVolatile())
13918 return false;
13919 // TODO: probably too restrictive for atomics, revisit
13920 if (!LD->isSimple())
13921 return false;
13922 if (LD->isIndexed() || Base->isIndexed())
13923 return false;
13924 if (LD->getChain() != Base->getChain())
13925 return false;
13926 EVT VT = LD->getMemoryVT();
13927 if (VT.getSizeInBits() / 8 != Bytes)
13928 return false;
13929
13930 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13931 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13932
13933 int64_t Offset = 0;
13934 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13935 return (Dist * (int64_t)Bytes == Offset);
13936 return false;
13937}
13938
13939/// InferPtrAlignment - Infer alignment of a load / store address. Return
13940/// std::nullopt if it cannot be inferred.
13942 // If this is a GlobalAddress + cst, return the alignment.
13943 const GlobalValue *GV = nullptr;
13944 int64_t GVOffset = 0;
13945 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13946 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13947 KnownBits Known(PtrWidth);
13949 unsigned AlignBits = Known.countMinTrailingZeros();
13950 if (AlignBits)
13951 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13952 }
13953
13954 // If this is a direct reference to a stack slot, use information about the
13955 // stack slot's alignment.
13956 int FrameIdx = INT_MIN;
13957 int64_t FrameOffset = 0;
13959 FrameIdx = FI->getIndex();
13960 } else if (isBaseWithConstantOffset(Ptr) &&
13962 // Handle FI+Cst
13963 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13964 FrameOffset = Ptr.getConstantOperandVal(1);
13965 }
13966
13967 if (FrameIdx != INT_MIN) {
13969 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13970 }
13971
13972 return std::nullopt;
13973}
13974
13975/// Split the scalar node with EXTRACT_ELEMENT using the provided
13976/// VTs and return the low/high part.
13977std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13978 const SDLoc &DL,
13979 const EVT &LoVT,
13980 const EVT &HiVT) {
13981 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13982 "Split node must be a scalar type");
13983 SDValue Lo =
13985 SDValue Hi =
13987 return std::make_pair(Lo, Hi);
13988}
13989
13990/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13991/// which is split (or expanded) into two not necessarily identical pieces.
13992std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13993 // Currently all types are split in half.
13994 EVT LoVT, HiVT;
13995 if (!VT.isVector())
13996 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13997 else
13998 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13999
14000 return std::make_pair(LoVT, HiVT);
14001}
14002
14003/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
14004/// type, dependent on an enveloping VT that has been split into two identical
14005/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
14006std::pair<EVT, EVT>
14008 bool *HiIsEmpty) const {
14009 EVT EltTp = VT.getVectorElementType();
14010 // Examples:
14011 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
14012 // custom VL=9 with enveloping VL=8/8 yields 8/1
14013 // custom VL=10 with enveloping VL=8/8 yields 8/2
14014 // etc.
14015 ElementCount VTNumElts = VT.getVectorElementCount();
14016 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
14017 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
14018 "Mixing fixed width and scalable vectors when enveloping a type");
14019 EVT LoVT, HiVT;
14020 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
14021 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14022 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
14023 *HiIsEmpty = false;
14024 } else {
14025 // Flag that hi type has zero storage size, but return split envelop type
14026 // (this would be easier if vector types with zero elements were allowed).
14027 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
14028 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
14029 *HiIsEmpty = true;
14030 }
14031 return std::make_pair(LoVT, HiVT);
14032}
14033
14034/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
14035/// low/high part.
14036std::pair<SDValue, SDValue>
14037SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
14038 const EVT &HiVT) {
14039 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
14040 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
14041 "Splitting vector with an invalid mixture of fixed and scalable "
14042 "vector types");
14044 N.getValueType().getVectorMinNumElements() &&
14045 "More vector elements requested than available!");
14046 SDValue Lo, Hi;
14047 Lo = getExtractSubvector(DL, LoVT, N, 0);
14048 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
14049 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
14050 // IDX with the runtime scaling factor of the result vector type. For
14051 // fixed-width result vectors, that runtime scaling factor is 1.
14054 return std::make_pair(Lo, Hi);
14055}
14056
14057std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
14058 const SDLoc &DL) {
14059 // Split the vector length parameter.
14060 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
14061 EVT VT = N.getValueType();
14063 "Expecting the mask to be an evenly-sized vector");
14064 SDValue HalfNumElts = getElementCount(
14066 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
14067 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
14068 return std::make_pair(Lo, Hi);
14069}
14070
14071/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
14073 EVT VT = N.getValueType();
14076 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
14077}
14078
14081 unsigned Start, unsigned Count,
14082 EVT EltVT) {
14083 EVT VT = Op.getValueType();
14084 if (Count == 0)
14086 if (EltVT == EVT())
14087 EltVT = VT.getVectorElementType();
14088 SDLoc SL(Op);
14089 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
14090 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
14091 }
14092}
14093
14094// getAddressSpace - Return the address space this GlobalAddress belongs to.
14096 return getGlobal()->getType()->getAddressSpace();
14097}
14098
14101 return Val.MachineCPVal->getType();
14102 return Val.ConstVal->getType();
14103}
14104
14105bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
14106 unsigned &SplatBitSize,
14107 bool &HasAnyUndefs,
14108 unsigned MinSplatBits,
14109 bool IsBigEndian) const {
14110 EVT VT = getValueType(0);
14111 assert(VT.isVector() && "Expected a vector type");
14112 unsigned VecWidth = VT.getSizeInBits();
14113 if (MinSplatBits > VecWidth)
14114 return false;
14115
14116 // FIXME: The widths are based on this node's type, but build vectors can
14117 // truncate their operands.
14118 SplatValue = APInt(VecWidth, 0);
14119 SplatUndef = APInt(VecWidth, 0);
14120
14121 // Get the bits. Bits with undefined values (when the corresponding element
14122 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
14123 // in SplatValue. If any of the values are not constant, give up and return
14124 // false.
14125 unsigned int NumOps = getNumOperands();
14126 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
14127 unsigned EltWidth = VT.getScalarSizeInBits();
14128
14129 for (unsigned j = 0; j < NumOps; ++j) {
14130 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
14131 SDValue OpVal = getOperand(i);
14132 unsigned BitPos = j * EltWidth;
14133
14134 if (OpVal.isUndef())
14135 SplatUndef.setBits(BitPos, BitPos + EltWidth);
14136 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
14137 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
14138 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
14139 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
14140 else
14141 return false;
14142 }
14143
14144 // The build_vector is all constants or undefs. Find the smallest element
14145 // size that splats the vector.
14146 HasAnyUndefs = (SplatUndef != 0);
14147
14148 // FIXME: This does not work for vectors with elements less than 8 bits.
14149 while (VecWidth > 8) {
14150 // If we can't split in half, stop here.
14151 if (VecWidth & 1)
14152 break;
14153
14154 unsigned HalfSize = VecWidth / 2;
14155 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
14156 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
14157 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
14158 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
14159
14160 // If the two halves do not match (ignoring undef bits), stop here.
14161 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
14162 MinSplatBits > HalfSize)
14163 break;
14164
14165 SplatValue = HighValue | LowValue;
14166 SplatUndef = HighUndef & LowUndef;
14167
14168 VecWidth = HalfSize;
14169 }
14170
14171 // FIXME: The loop above only tries to split in halves. But if the input
14172 // vector for example is <3 x i16> it wouldn't be able to detect a
14173 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
14174 // optimizations. I guess that back in the days when this helper was created
14175 // vectors normally was power-of-2 sized.
14176
14177 SplatBitSize = VecWidth;
14178 return true;
14179}
14180
14182 BitVector *UndefElements) const {
14183 unsigned NumOps = getNumOperands();
14184 if (UndefElements) {
14185 UndefElements->clear();
14186 UndefElements->resize(NumOps);
14187 }
14188 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14189 if (!DemandedElts)
14190 return SDValue();
14191 SDValue Splatted;
14192 for (unsigned i = 0; i != NumOps; ++i) {
14193 if (!DemandedElts[i])
14194 continue;
14195 SDValue Op = getOperand(i);
14196 if (Op.isUndef()) {
14197 if (UndefElements)
14198 (*UndefElements)[i] = true;
14199 } else if (!Splatted) {
14200 Splatted = Op;
14201 } else if (Splatted != Op) {
14202 return SDValue();
14203 }
14204 }
14205
14206 if (!Splatted) {
14207 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
14208 assert(getOperand(FirstDemandedIdx).isUndef() &&
14209 "Can only have a splat without a constant for all undefs.");
14210 return getOperand(FirstDemandedIdx);
14211 }
14212
14213 return Splatted;
14214}
14215
14217 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14218 return getSplatValue(DemandedElts, UndefElements);
14219}
14220
14222 SmallVectorImpl<SDValue> &Sequence,
14223 BitVector *UndefElements) const {
14224 unsigned NumOps = getNumOperands();
14225 Sequence.clear();
14226 if (UndefElements) {
14227 UndefElements->clear();
14228 UndefElements->resize(NumOps);
14229 }
14230 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14231 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14232 return false;
14233
14234 // Set the undefs even if we don't find a sequence (like getSplatValue).
14235 if (UndefElements)
14236 for (unsigned I = 0; I != NumOps; ++I)
14237 if (DemandedElts[I] && getOperand(I).isUndef())
14238 (*UndefElements)[I] = true;
14239
14240 // Iteratively widen the sequence length looking for repetitions.
14241 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14242 Sequence.append(SeqLen, SDValue());
14243 for (unsigned I = 0; I != NumOps; ++I) {
14244 if (!DemandedElts[I])
14245 continue;
14246 SDValue &SeqOp = Sequence[I % SeqLen];
14248 if (Op.isUndef()) {
14249 if (!SeqOp)
14250 SeqOp = Op;
14251 continue;
14252 }
14253 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14254 Sequence.clear();
14255 break;
14256 }
14257 SeqOp = Op;
14258 }
14259 if (!Sequence.empty())
14260 return true;
14261 }
14262
14263 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14264 return false;
14265}
14266
14268 BitVector *UndefElements) const {
14269 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14270 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14271}
14272
14275 BitVector *UndefElements) const {
14277 getSplatValue(DemandedElts, UndefElements));
14278}
14279
14282 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14283}
14284
14287 BitVector *UndefElements) const {
14289 getSplatValue(DemandedElts, UndefElements));
14290}
14291
14296
14297int32_t
14299 uint32_t BitWidth) const {
14300 if (ConstantFPSDNode *CN =
14302 bool IsExact;
14303 APSInt IntVal(BitWidth);
14304 const APFloat &APF = CN->getValueAPF();
14305 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14306 APFloat::opOK ||
14307 !IsExact)
14308 return -1;
14309
14310 return IntVal.exactLogBase2();
14311 }
14312 return -1;
14313}
14314
14316 bool IsLittleEndian, unsigned DstEltSizeInBits,
14317 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14318 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14319 if (!isConstant())
14320 return false;
14321
14322 unsigned NumSrcOps = getNumOperands();
14323 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14324 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14325 "Invalid bitcast scale");
14326
14327 // Extract raw src bits.
14328 SmallVector<APInt> SrcBitElements(NumSrcOps,
14329 APInt::getZero(SrcEltSizeInBits));
14330 BitVector SrcUndeElements(NumSrcOps, false);
14331
14332 for (unsigned I = 0; I != NumSrcOps; ++I) {
14334 if (Op.isUndef()) {
14335 SrcUndeElements.set(I);
14336 continue;
14337 }
14338 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14339 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14340 assert((CInt || CFP) && "Unknown constant");
14341 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14342 : CFP->getValueAPF().bitcastToAPInt();
14343 }
14344
14345 // Recast to dst width.
14346 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14347 SrcBitElements, UndefElements, SrcUndeElements);
14348 return true;
14349}
14350
14351void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14352 unsigned DstEltSizeInBits,
14353 SmallVectorImpl<APInt> &DstBitElements,
14354 ArrayRef<APInt> SrcBitElements,
14355 BitVector &DstUndefElements,
14356 const BitVector &SrcUndefElements) {
14357 unsigned NumSrcOps = SrcBitElements.size();
14358 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14359 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14360 "Invalid bitcast scale");
14361 assert(NumSrcOps == SrcUndefElements.size() &&
14362 "Vector size mismatch");
14363
14364 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14365 DstUndefElements.clear();
14366 DstUndefElements.resize(NumDstOps, false);
14367 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14368
14369 // Concatenate src elements constant bits together into dst element.
14370 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14371 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14372 for (unsigned I = 0; I != NumDstOps; ++I) {
14373 DstUndefElements.set(I);
14374 APInt &DstBits = DstBitElements[I];
14375 for (unsigned J = 0; J != Scale; ++J) {
14376 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14377 if (SrcUndefElements[Idx])
14378 continue;
14379 DstUndefElements.reset(I);
14380 const APInt &SrcBits = SrcBitElements[Idx];
14381 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14382 "Illegal constant bitwidths");
14383 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14384 }
14385 }
14386 return;
14387 }
14388
14389 // Split src element constant bits into dst elements.
14390 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14391 for (unsigned I = 0; I != NumSrcOps; ++I) {
14392 if (SrcUndefElements[I]) {
14393 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14394 continue;
14395 }
14396 const APInt &SrcBits = SrcBitElements[I];
14397 for (unsigned J = 0; J != Scale; ++J) {
14398 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14399 APInt &DstBits = DstBitElements[Idx];
14400 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14401 }
14402 }
14403}
14404
14406 for (const SDValue &Op : op_values()) {
14407 unsigned Opc = Op.getOpcode();
14408 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14409 return false;
14410 }
14411 return true;
14412}
14413
14414std::optional<std::pair<APInt, APInt>>
14416 unsigned NumOps = getNumOperands();
14417 if (NumOps < 2)
14418 return std::nullopt;
14419
14420 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14421 APInt Start, Stride;
14422 int FirstIdx = -1, SecondIdx = -1;
14423
14424 // Find the first two non-undef constant elements to determine Start and
14425 // Stride, then verify all remaining elements match the sequence.
14426 for (unsigned I = 0; I < NumOps; ++I) {
14428 if (Op->isUndef())
14429 continue;
14430 if (!isa<ConstantSDNode>(Op))
14431 return std::nullopt;
14432
14433 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14434 if (FirstIdx < 0) {
14435 FirstIdx = I;
14436 Start = Val;
14437 } else if (SecondIdx < 0) {
14438 SecondIdx = I;
14439 // Compute stride using modular arithmetic. Simple division would handle
14440 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14441 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14442 // Note that modular arithmetic is agnostic to signed/unsigned.
14443 unsigned IdxDiff = I - FirstIdx;
14444 APInt ValDiff = Val - Start;
14445
14446 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14447 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14448 if (ValDiff.countr_zero() < CommonPow2Bits)
14449 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14450 IdxDiff >>= CommonPow2Bits;
14451 ValDiff.lshrInPlace(CommonPow2Bits);
14452
14453 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14454 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14455 // one, but we could try all candidates to handle more cases.
14456 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14457 if (Stride.isZero())
14458 return std::nullopt;
14459
14460 // Step 3: Adjust Start based on the first defined element's index.
14461 Start -= Stride * FirstIdx;
14462 } else {
14463 // Verify this element matches the sequence.
14464 if (Val != Start + Stride * I)
14465 return std::nullopt;
14466 }
14467 }
14468
14469 // Need at least two defined elements.
14470 if (SecondIdx < 0)
14471 return std::nullopt;
14472
14473 return std::make_pair(Start, Stride);
14474}
14475
14477 // Find the first non-undef value in the shuffle mask.
14478 unsigned i, e;
14479 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14480 /* search */;
14481
14482 // If all elements are undefined, this shuffle can be considered a splat
14483 // (although it should eventually get simplified away completely).
14484 if (i == e)
14485 return true;
14486
14487 // Make sure all remaining elements are either undef or the same as the first
14488 // non-undef value.
14489 for (int Idx = Mask[i]; i != e; ++i)
14490 if (Mask[i] >= 0 && Mask[i] != Idx)
14491 return false;
14492 return true;
14493}
14494
14495// Returns true if it is a constant integer BuildVector or constant integer,
14496// possibly hidden by a bitcast.
14498 SDValue N, bool AllowOpaques) const {
14500
14501 if (auto *C = dyn_cast<ConstantSDNode>(N))
14502 return AllowOpaques || !C->isOpaque();
14503
14505 return true;
14506
14507 // Treat a GlobalAddress supporting constant offset folding as a
14508 // constant integer.
14509 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14510 if (GA->getOpcode() == ISD::GlobalAddress &&
14511 TLI->isOffsetFoldingLegal(GA))
14512 return true;
14513
14514 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14515 isa<ConstantSDNode>(N.getOperand(0)))
14516 return true;
14517 return false;
14518}
14519
14520// Returns true if it is a constant float BuildVector or constant float.
14523 return true;
14524
14526 return true;
14527
14528 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14529 isa<ConstantFPSDNode>(N.getOperand(0)))
14530 return true;
14531
14532 return false;
14533}
14534
14535std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14536 ConstantSDNode *Const =
14537 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14538 if (!Const)
14539 return std::nullopt;
14540
14541 EVT VT = N->getValueType(0);
14542 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14543 switch (TLI->getBooleanContents(N.getValueType())) {
14545 if (CVal.isOne())
14546 return true;
14547 if (CVal.isZero())
14548 return false;
14549 return std::nullopt;
14551 if (CVal.isAllOnes())
14552 return true;
14553 if (CVal.isZero())
14554 return false;
14555 return std::nullopt;
14557 return CVal[0];
14558 }
14559 llvm_unreachable("Unknown BooleanContent enum");
14560}
14561
14562void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14563 assert(!Node->OperandList && "Node already has operands");
14565 "too many operands to fit into SDNode");
14566 SDUse *Ops = OperandRecycler.allocate(
14567 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14568
14569 bool IsDivergent = false;
14570 for (unsigned I = 0; I != Vals.size(); ++I) {
14571 Ops[I].setUser(Node);
14572 Ops[I].setInitial(Vals[I]);
14573 EVT VT = Ops[I].getValueType();
14574
14575 // Skip Chain. It does not carry divergence.
14576 if (VT != MVT::Other &&
14577 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14578 Ops[I].getNode()->isDivergent()) {
14579 IsDivergent = true;
14580 }
14581 }
14582 Node->NumOperands = Vals.size();
14583 Node->OperandList = Ops;
14584 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14585 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14586 Node->SDNodeBits.IsDivergent = IsDivergent;
14587 }
14588 checkForCycles(Node);
14589}
14590
14593 size_t Limit = SDNode::getMaxNumOperands();
14594 while (Vals.size() > Limit) {
14595 unsigned SliceIdx = Vals.size() - Limit;
14596 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14597 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14598 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14599 Vals.emplace_back(NewTF);
14600 }
14601 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14602}
14603
14605 EVT VT, SDNodeFlags Flags) {
14606 switch (Opcode) {
14607 default:
14608 return SDValue();
14609 case ISD::ADD:
14610 case ISD::OR:
14611 case ISD::XOR:
14612 case ISD::UMAX:
14613 return getConstant(0, DL, VT);
14614 case ISD::MUL:
14615 return getConstant(1, DL, VT);
14616 case ISD::AND:
14617 case ISD::UMIN:
14618 return getAllOnesConstant(DL, VT);
14619 case ISD::SMAX:
14621 case ISD::SMIN:
14623 case ISD::FADD:
14624 // If flags allow, prefer positive zero since it's generally cheaper
14625 // to materialize on most targets.
14626 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14627 case ISD::FMUL:
14628 return getConstantFP(1.0, DL, VT);
14629 case ISD::FMINNUM:
14630 case ISD::FMAXNUM: {
14631 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14632 const fltSemantics &Semantics = VT.getFltSemantics();
14633 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14634 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14635 APFloat::getLargest(Semantics);
14636 if (Opcode == ISD::FMAXNUM)
14637 NeutralAF.changeSign();
14638
14639 return getConstantFP(NeutralAF, DL, VT);
14640 }
14641 case ISD::FMINIMUM:
14642 case ISD::FMAXIMUM: {
14643 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14644 const fltSemantics &Semantics = VT.getFltSemantics();
14645 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14646 : APFloat::getLargest(Semantics);
14647 if (Opcode == ISD::FMAXIMUM)
14648 NeutralAF.changeSign();
14649
14650 return getConstantFP(NeutralAF, DL, VT);
14651 }
14652
14653 }
14654}
14655
14656/// Helper used to make a call to a library function that has one argument of
14657/// pointer type.
14658///
14659/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14660/// used to get or set floating-point state. They have one argument of pointer
14661/// type, which points to the memory region containing bits of the
14662/// floating-point state. The value returned by such function is ignored in the
14663/// created call.
14664///
14665/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14666/// \param Ptr Pointer used to save/load state.
14667/// \param InChain Ingoing token chain.
14668/// \returns Outgoing chain token.
14670 SDValue InChain,
14671 const SDLoc &DLoc) {
14672 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14674 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14675 RTLIB::LibcallImpl LibcallImpl =
14676 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14677 if (LibcallImpl == RTLIB::Unsupported)
14678 reportFatalUsageError("emitting call to unsupported libcall");
14679
14680 SDValue Callee =
14681 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14683 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14684 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14685 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14686 return TLI->LowerCallTo(CLI).second;
14687}
14688
14690 assert(From && To && "Invalid SDNode; empty source SDValue?");
14691 auto I = SDEI.find(From);
14692 if (I == SDEI.end())
14693 return;
14694
14695 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14696 // the iterator, hence the need to make a copy to prevent a use-after-free.
14697 NodeExtraInfo NEI = I->second;
14698 if (LLVM_LIKELY(!NEI.PCSections)) {
14699 // No deep copy required for the types of extra info set.
14700 //
14701 // FIXME: Investigate if other types of extra info also need deep copy. This
14702 // depends on the types of nodes they can be attached to: if some extra info
14703 // is only ever attached to nodes where a replacement To node is always the
14704 // node where later use and propagation of the extra info has the intended
14705 // semantics, no deep copy is required.
14706 SDEI[To] = std::move(NEI);
14707 return;
14708 }
14709
14710 const SDNode *EntrySDN = getEntryNode().getNode();
14711
14712 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14713 // through the replacement of From with To. Otherwise, replacements of a node
14714 // (From) with more complex nodes (To and its operands) may result in lost
14715 // extra info where the root node (To) is insignificant in further propagating
14716 // and using extra info when further lowering to MIR.
14717 //
14718 // In the first step pre-populate the visited set with the nodes reachable
14719 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14720 // DAG that is not new and should be left untouched.
14721 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14722 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14723 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14724 if (MaxDepth == 0) {
14725 // Remember this node in case we need to increase MaxDepth and continue
14726 // populating FromReach from this node.
14727 Leafs.emplace_back(N);
14728 return;
14729 }
14730 if (!FromReach.insert(N).second)
14731 return;
14732 for (const SDValue &Op : N->op_values())
14733 Self(Self, Op.getNode(), MaxDepth - 1);
14734 };
14735
14736 // Copy extra info to To and all its transitive operands (that are new).
14738 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14739 if (FromReach.contains(N))
14740 return true;
14741 if (!Visited.insert(N).second)
14742 return true;
14743 if (EntrySDN == N)
14744 return false;
14745 for (const SDValue &Op : N->op_values()) {
14746 if (N == To && Op.getNode() == EntrySDN) {
14747 // Special case: New node's operand is the entry node; just need to
14748 // copy extra info to new node.
14749 break;
14750 }
14751 if (!Self(Self, Op.getNode()))
14752 return false;
14753 }
14754 // Copy only if entry node was not reached.
14755 SDEI[N] = std::move(NEI);
14756 return true;
14757 };
14758
14759 // We first try with a lower MaxDepth, assuming that the path to common
14760 // operands between From and To is relatively short. This significantly
14761 // improves performance in the common case. The initial MaxDepth is big
14762 // enough to avoid retry in the common case; the last MaxDepth is large
14763 // enough to avoid having to use the fallback below (and protects from
14764 // potential stack exhaustion from recursion).
14765 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14766 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14767 // StartFrom is the previous (or initial) set of leafs reachable at the
14768 // previous maximum depth.
14770 std::swap(StartFrom, Leafs);
14771 for (const SDNode *N : StartFrom)
14772 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14773 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14774 return;
14775 // This should happen very rarely (reached the entry node).
14776 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14777 assert(!Leafs.empty());
14778 }
14779
14780 // This should not happen - but if it did, that means the subgraph reachable
14781 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14782 // could not visit all reachable common operands. Consequently, we were able
14783 // to reach the entry node.
14784 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14785 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14786 // Best-effort fallback if assertions disabled.
14787 SDEI[To] = std::move(NEI);
14788}
14789
14790#ifndef NDEBUG
14791static void checkForCyclesHelper(const SDNode *N,
14794 const llvm::SelectionDAG *DAG) {
14795 // If this node has already been checked, don't check it again.
14796 if (Checked.count(N))
14797 return;
14798
14799 // If a node has already been visited on this depth-first walk, reject it as
14800 // a cycle.
14801 if (!Visited.insert(N).second) {
14802 errs() << "Detected cycle in SelectionDAG\n";
14803 dbgs() << "Offending node:\n";
14804 N->dumprFull(DAG); dbgs() << "\n";
14805 abort();
14806 }
14807
14808 for (const SDValue &Op : N->op_values())
14809 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14810
14811 Checked.insert(N);
14812 Visited.erase(N);
14813}
14814#endif
14815
14817 const llvm::SelectionDAG *DAG,
14818 bool force) {
14819#ifndef NDEBUG
14820 bool check = force;
14821#ifdef EXPENSIVE_CHECKS
14822 check = true;
14823#endif // EXPENSIVE_CHECKS
14824 if (check) {
14825 assert(N && "Checking nonexistent SDNode");
14828 checkForCyclesHelper(N, visited, checked, DAG);
14829 }
14830#endif // !NDEBUG
14831}
14832
14833void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14834 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14835}
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
#define X(NUM, ENUM, NAME)
Definition ELF.h:849
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")
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 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
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
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 SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
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 unsigned getSize(unsigned Kind)
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:1175
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1263
void copySign(const APFloat &RHS)
Definition APFloat.h:1357
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5890
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1245
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:1499
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1236
bool isFinite() const
Definition APFloat.h:1521
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1402
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1254
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1290
bool isZero() const
Definition APFloat.h:1512
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1193
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1387
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1153
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1281
bool isPosZero() const
Definition APFloat.h:1527
bool isNegZero() const
Definition APFloat.h:1528
void changeSign()
Definition APFloat.h:1352
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1164
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:1421
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:1555
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1406
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1685
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1400
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:1527
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:1345
APInt abs() const
Get the absolute value.
Definition APInt.h:1810
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:1189
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:1503
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
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:1411
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:841
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1654
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1643
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1613
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
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
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:1450
unsigned logBase2() const
Definition APInt.h:1776
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:834
LLVM_ABI APInt multiplicativeInverse() const
Definition APInt.cpp:1285
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:1157
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:1382
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
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:1264
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
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:1432
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:1403
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:1244
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
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition APInt.h:865
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
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:1065
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 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 std::optional< std::pair< APInt, APInt > > isArithmeticSequence() const
If this BuildVector is constant and represents an arithmetic sequence "<a, a+n, a+2n,...
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:420
const APFloat & getValue() const
Definition Constants.h:464
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:215
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:711
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:354
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 CallingConv::ID getLibcallImplCallingConv(RTLIB::LibcallImpl Call) const
Get the CallingConv that should be used for the specified libcall.
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:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
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.
size_t getNumMemOperands() const
Return the number of memory operands.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, PointerUnion< MachineMemOperand *, MachineMemOperand ** > memrefs)
Constructor that supports single or multiple MMOs.
PointerUnion< MachineMemOperand *, MachineMemOperand ** > MemRefs
Memory reference information.
MachineMemOperand * getMemOperand() const
Return the unique MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
ArrayRef< MachineMemOperand * > memoperands() const
Return the memory operands for this node.
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.
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
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 std::pair< SDValue, SDValue > getMemccpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue C, SDValue Size, const CallInst *CI)
Lower a memccpy operation into a target library call and return the resulting chain and call result a...
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.
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl, SDNodeFlags Flags={})
Constant fold a setcc to true or false.
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 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)
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 std::pair< SDValue, SDValue > getStrcmp(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strcmp operation into a target library call and return the resulting chain and call result as...
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,...
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false, SDNodeFlags Flags={})
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
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 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 void dump() const
Dump the textual format of this DAG.
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 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 bool isKnownToBeAPowerOfTwo(SDValue Val, bool OrZero=false, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
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:137
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.
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.
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, EVT *LargestVT=nullptr) 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:639
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:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:290
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:313
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:311
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:201
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:236
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:907
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:2283
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:2288
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:819
@ 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:788
@ 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:779
@ 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:853
@ 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:880
@ 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:747
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:910
@ 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:993
@ 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:774
@ 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:844
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:715
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:665
@ 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:787
@ 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:873
@ 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:827
@ 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:691
@ 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:796
@ 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:672
@ 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:792
@ 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:704
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:765
@ 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:850
@ 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:811
@ 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, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
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:899
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:888
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ 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:978
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:805
@ 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:926
@ 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:739
@ 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:735
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:710
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ 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:681
@ 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:959
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:699
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:921
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:997
@ 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:945
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:856
@ 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:833
@ 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:871
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:722
@ TRUNCATE_USAT_U
Definition ISDOpcodes.h:875
@ 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:668
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:360
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:1759
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:1739
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:1607
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
@ Undef
Value of the register doesn't matter.
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:2554
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:1710
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
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:634
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:1589
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:1622
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
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:1746
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:1665
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:1636
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1696
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:1753
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:1646
@ 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:1885
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:1947
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:1683
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1723
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:763
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:783
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:780
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:403
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:145
intptr_t getRawBits() const
Definition ValueTypes.h:528
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:70
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:129
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:292
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:308
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:155
ElementCount getVectorElementCount() const
Definition ValueTypes.h:358
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:381
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:367
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:393
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:324
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:61
bool isFixedLengthVector() const
Definition ValueTypes.h:189
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:176
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:331
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:300
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:264
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:182
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:336
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:150
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:344
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:316
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:469
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:160
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:317
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:271
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:258
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:290
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:167
KnownBits byteSwap() const
Definition KnownBits.h:538
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:305
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:542
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:249
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:178
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:337
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:241
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:327
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:186
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:202
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:61
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:342
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:54
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:296
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:235
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:173
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
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)