LLVM 23.0.0git
SelectionDAG.cpp
Go to the documentation of this file.
1//===- SelectionDAG.cpp - Implement the SelectionDAG data structures ------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This implements the SelectionDAG class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "SDNodeDbgValue.h"
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/BitVector.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/FoldingSet.h"
22#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/Twine.h"
51#include "llvm/IR/Constant.h"
52#include "llvm/IR/Constants.h"
53#include "llvm/IR/DataLayout.h"
55#include "llvm/IR/DebugLoc.h"
57#include "llvm/IR/Function.h"
58#include "llvm/IR/GlobalValue.h"
59#include "llvm/IR/Metadata.h"
60#include "llvm/IR/Type.h"
64#include "llvm/Support/Debug.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <cstdlib>
77#include <limits>
78#include <optional>
79#include <string>
80#include <utility>
81#include <vector>
82
83using namespace llvm;
84using namespace llvm::SDPatternMatch;
85
86/// makeVTList - Return an instance of the SDVTList struct initialized with the
87/// specified members.
88static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs) {
89 SDVTList Res = {VTs, NumVTs};
90 return Res;
91}
92
93// Default null implementations of the callbacks.
97
98void SelectionDAG::DAGNodeDeletedListener::anchor() {}
99void SelectionDAG::DAGNodeInsertedListener::anchor() {}
100
101#define DEBUG_TYPE "selectiondag"
102
103static cl::opt<bool> EnableMemCpyDAGOpt("enable-memcpy-dag-opt",
104 cl::Hidden, cl::init(true),
105 cl::desc("Gang up loads and stores generated by inlining of memcpy"));
106
107static cl::opt<int> MaxLdStGlue("ldstmemcpy-glue-max",
108 cl::desc("Number limit for gluing ld/st of memcpy."),
109 cl::Hidden, cl::init(0));
110
112 MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192),
113 cl::desc("DAG combiner limit number of steps when searching DAG "
114 "for predecessor nodes"));
115
117 LLVM_DEBUG(dbgs() << Msg; V.getNode()->dump(G););
118}
119
121
122//===----------------------------------------------------------------------===//
123// ConstantFPSDNode Class
124//===----------------------------------------------------------------------===//
125
126/// isExactlyValue - We don't rely on operator== working on double values, as
127/// it returns true for things that are clearly not equal, like -0.0 and 0.0.
128/// As such, this method can be used to do an exact bit-for-bit comparison of
129/// two floating point values.
131 return getValueAPF().bitwiseIsEqual(V);
132}
133
135 const APFloat& Val) {
136 assert(VT.isFloatingPoint() && "Can only convert between FP types");
137
138 // convert modifies in place, so make a copy.
139 APFloat Val2 = APFloat(Val);
140 bool losesInfo;
142 &losesInfo);
143 return !losesInfo;
144}
145
146//===----------------------------------------------------------------------===//
147// ISD Namespace
148//===----------------------------------------------------------------------===//
149
150bool ISD::isConstantSplatVector(const SDNode *N, APInt &SplatVal) {
151 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
152 if (auto OptAPInt = N->getOperand(0)->bitcastToAPInt()) {
153 unsigned EltSize =
154 N->getValueType(0).getVectorElementType().getSizeInBits();
155 SplatVal = OptAPInt->trunc(EltSize);
156 return true;
157 }
158 }
159
160 auto *BV = dyn_cast<BuildVectorSDNode>(N);
161 if (!BV)
162 return false;
163
164 APInt SplatUndef;
165 unsigned SplatBitSize;
166 bool HasUndefs;
167 unsigned EltSize = N->getValueType(0).getVectorElementType().getSizeInBits();
168 // Endianness does not matter here. We are checking for a splat given the
169 // element size of the vector, and if we find such a splat for little endian
170 // layout, then that should be valid also for big endian (as the full vector
171 // size is known to be a multiple of the element size).
172 const bool IsBigEndian = false;
173 return BV->isConstantSplat(SplatVal, SplatUndef, SplatBitSize, HasUndefs,
174 EltSize, IsBigEndian) &&
175 EltSize == SplatBitSize;
176}
177
178// FIXME: AllOnes and AllZeros duplicate a lot of code. Could these be
179// specializations of the more general isConstantSplatVector()?
180
181bool ISD::isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly) {
182 // Look through a bit convert.
183 while (N->getOpcode() == ISD::BITCAST)
184 N = N->getOperand(0).getNode();
185
186 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
187 APInt SplatVal;
188 return isConstantSplatVector(N, SplatVal) && SplatVal.isAllOnes();
189 }
190
191 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
192
193 unsigned i = 0, e = N->getNumOperands();
194
195 // Skip over all of the undef values.
196 while (i != e && N->getOperand(i).isUndef())
197 ++i;
198
199 // Do not accept an all-undef vector.
200 if (i == e) return false;
201
202 // Do not accept build_vectors that aren't all constants or which have non-~0
203 // elements. We have to be a bit careful here, as the type of the constant
204 // may not be the same as the type of the vector elements due to type
205 // legalization (the elements are promoted to a legal type for the target and
206 // a vector of a type may be legal when the base element type is not).
207 // We only want to check enough bits to cover the vector elements, because
208 // we care if the resultant vector is all ones, not whether the individual
209 // constants are.
210 SDValue NotZero = N->getOperand(i);
211 if (auto OptAPInt = NotZero->bitcastToAPInt()) {
212 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
213 if (OptAPInt->countr_one() < EltSize)
214 return false;
215 } else
216 return false;
217
218 // Okay, we have at least one ~0 value, check to see if the rest match or are
219 // undefs. Even with the above element type twiddling, this should be OK, as
220 // the same type legalization should have applied to all the elements.
221 for (++i; i != e; ++i)
222 if (N->getOperand(i) != NotZero && !N->getOperand(i).isUndef())
223 return false;
224 return true;
225}
226
227bool ISD::isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly) {
228 // Look through a bit convert.
229 while (N->getOpcode() == ISD::BITCAST)
230 N = N->getOperand(0).getNode();
231
232 if (!BuildVectorOnly && N->getOpcode() == ISD::SPLAT_VECTOR) {
233 APInt SplatVal;
234 return isConstantSplatVector(N, SplatVal) && SplatVal.isZero();
235 }
236
237 if (N->getOpcode() != ISD::BUILD_VECTOR) return false;
238
239 bool IsAllUndef = true;
240 for (const SDValue &Op : N->op_values()) {
241 if (Op.isUndef())
242 continue;
243 IsAllUndef = false;
244 // Do not accept build_vectors that aren't all constants or which have non-0
245 // elements. We have to be a bit careful here, as the type of the constant
246 // may not be the same as the type of the vector elements due to type
247 // legalization (the elements are promoted to a legal type for the target
248 // and a vector of a type may be legal when the base element type is not).
249 // We only want to check enough bits to cover the vector elements, because
250 // we care if the resultant vector is all zeros, not whether the individual
251 // constants are.
252 if (auto OptAPInt = Op->bitcastToAPInt()) {
253 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
254 if (OptAPInt->countr_zero() < EltSize)
255 return false;
256 } else
257 return false;
258 }
259
260 // Do not accept an all-undef vector.
261 if (IsAllUndef)
262 return false;
263 return true;
264}
265
267 return isConstantSplatVectorAllOnes(N, /*BuildVectorOnly*/ true);
268}
269
271 return isConstantSplatVectorAllZeros(N, /*BuildVectorOnly*/ true);
272}
273
275 if (N->getOpcode() != ISD::BUILD_VECTOR)
276 return false;
277
278 for (const SDValue &Op : N->op_values()) {
279 if (Op.isUndef())
280 continue;
282 return false;
283 }
284 return true;
285}
286
288 if (N->getOpcode() != ISD::BUILD_VECTOR)
289 return false;
290
291 for (const SDValue &Op : N->op_values()) {
292 if (Op.isUndef())
293 continue;
295 return false;
296 }
297 return true;
298}
299
300bool ISD::isVectorShrinkable(const SDNode *N, unsigned NewEltSize,
301 bool Signed) {
302 assert(N->getValueType(0).isVector() && "Expected a vector!");
303
304 unsigned EltSize = N->getValueType(0).getScalarSizeInBits();
305 if (EltSize <= NewEltSize)
306 return false;
307
308 if (N->getOpcode() == ISD::ZERO_EXTEND) {
309 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
310 NewEltSize) &&
311 !Signed;
312 }
313 if (N->getOpcode() == ISD::SIGN_EXTEND) {
314 return (N->getOperand(0).getValueType().getScalarSizeInBits() <=
315 NewEltSize) &&
316 Signed;
317 }
318 if (N->getOpcode() != ISD::BUILD_VECTOR)
319 return false;
320
321 for (const SDValue &Op : N->op_values()) {
322 if (Op.isUndef())
323 continue;
325 return false;
326
327 APInt C = Op->getAsAPIntVal().trunc(EltSize);
328 if (Signed && C.trunc(NewEltSize).sext(EltSize) != C)
329 return false;
330 if (!Signed && C.trunc(NewEltSize).zext(EltSize) != C)
331 return false;
332 }
333
334 return true;
335}
336
338 // Return false if the node has no operands.
339 // This is "logically inconsistent" with the definition of "all" but
340 // is probably the desired behavior.
341 if (N->getNumOperands() == 0)
342 return false;
343 return all_of(N->op_values(), [](SDValue Op) { return Op.isUndef(); });
344}
345
347 return N->getOpcode() == ISD::FREEZE && N->getOperand(0).isUndef();
348}
349
350template <typename ConstNodeType>
352 std::function<bool(ConstNodeType *)> Match,
353 bool AllowUndefs, bool AllowTruncation) {
354 // FIXME: Add support for scalar UNDEF cases?
355 if (auto *C = dyn_cast<ConstNodeType>(Op))
356 return Match(C);
357
358 // FIXME: Add support for vector UNDEF cases?
359 if (ISD::BUILD_VECTOR != Op.getOpcode() &&
360 ISD::SPLAT_VECTOR != Op.getOpcode())
361 return false;
362
363 EVT SVT = Op.getValueType().getScalarType();
364 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
365 if (AllowUndefs && Op.getOperand(i).isUndef()) {
366 if (!Match(nullptr))
367 return false;
368 continue;
369 }
370
371 auto *Cst = dyn_cast<ConstNodeType>(Op.getOperand(i));
372 if (!Cst || (!AllowTruncation && Cst->getValueType(0) != SVT) ||
373 !Match(Cst))
374 return false;
375 }
376 return true;
377}
378// Build used template types.
380 SDValue, std::function<bool(ConstantSDNode *)>, bool, bool);
382 SDValue, std::function<bool(ConstantFPSDNode *)>, bool, bool);
383
385 SDValue LHS, SDValue RHS,
386 std::function<bool(ConstantSDNode *, ConstantSDNode *)> Match,
387 bool AllowUndefs, bool AllowTypeMismatch) {
388 if (!AllowTypeMismatch && LHS.getValueType() != RHS.getValueType())
389 return false;
390
391 // TODO: Add support for scalar UNDEF cases?
392 if (auto *LHSCst = dyn_cast<ConstantSDNode>(LHS))
393 if (auto *RHSCst = dyn_cast<ConstantSDNode>(RHS))
394 return Match(LHSCst, RHSCst);
395
396 // TODO: Add support for vector UNDEF cases?
397 if (LHS.getOpcode() != RHS.getOpcode() ||
398 (LHS.getOpcode() != ISD::BUILD_VECTOR &&
399 LHS.getOpcode() != ISD::SPLAT_VECTOR))
400 return false;
401
402 EVT SVT = LHS.getValueType().getScalarType();
403 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
404 SDValue LHSOp = LHS.getOperand(i);
405 SDValue RHSOp = RHS.getOperand(i);
406 bool LHSUndef = AllowUndefs && LHSOp.isUndef();
407 bool RHSUndef = AllowUndefs && RHSOp.isUndef();
408 auto *LHSCst = dyn_cast<ConstantSDNode>(LHSOp);
409 auto *RHSCst = dyn_cast<ConstantSDNode>(RHSOp);
410 if ((!LHSCst && !LHSUndef) || (!RHSCst && !RHSUndef))
411 return false;
412 if (!AllowTypeMismatch && (LHSOp.getValueType() != SVT ||
413 LHSOp.getValueType() != RHSOp.getValueType()))
414 return false;
415 if (!Match(LHSCst, RHSCst))
416 return false;
417 }
418 return true;
419}
420
422 switch (MinMaxOpc) {
423 default:
424 llvm_unreachable("unrecognized opcode");
425 case ISD::UMIN:
426 return ISD::UMAX;
427 case ISD::UMAX:
428 return ISD::UMIN;
429 case ISD::SMIN:
430 return ISD::SMAX;
431 case ISD::SMAX:
432 return ISD::SMIN;
433 }
434}
435
437 switch (MinMaxOpc) {
438 default:
439 llvm_unreachable("unrecognized min/max opcode");
440 case ISD::SMIN:
441 return ISD::UMIN;
442 case ISD::SMAX:
443 return ISD::UMAX;
444 case ISD::UMIN:
445 return ISD::SMIN;
446 case ISD::UMAX:
447 return ISD::SMAX;
448 }
449}
450
452 switch (VecReduceOpcode) {
453 default:
454 llvm_unreachable("Expected VECREDUCE opcode");
457 case ISD::VP_REDUCE_FADD:
458 case ISD::VP_REDUCE_SEQ_FADD:
459 return ISD::FADD;
462 case ISD::VP_REDUCE_FMUL:
463 case ISD::VP_REDUCE_SEQ_FMUL:
464 return ISD::FMUL;
466 case ISD::VP_REDUCE_ADD:
467 return ISD::ADD;
469 case ISD::VP_REDUCE_MUL:
470 return ISD::MUL;
472 case ISD::VP_REDUCE_AND:
473 return ISD::AND;
475 case ISD::VP_REDUCE_OR:
476 return ISD::OR;
478 case ISD::VP_REDUCE_XOR:
479 return ISD::XOR;
481 case ISD::VP_REDUCE_SMAX:
482 return ISD::SMAX;
484 case ISD::VP_REDUCE_SMIN:
485 return ISD::SMIN;
487 case ISD::VP_REDUCE_UMAX:
488 return ISD::UMAX;
490 case ISD::VP_REDUCE_UMIN:
491 return ISD::UMIN;
493 case ISD::VP_REDUCE_FMAX:
494 return ISD::FMAXNUM;
496 case ISD::VP_REDUCE_FMIN:
497 return ISD::FMINNUM;
499 case ISD::VP_REDUCE_FMAXIMUM:
500 return ISD::FMAXIMUM;
502 case ISD::VP_REDUCE_FMINIMUM:
503 return ISD::FMINIMUM;
504 }
505}
506
507bool ISD::isVPOpcode(unsigned Opcode) {
508 switch (Opcode) {
509 default:
510 return false;
511#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) \
512 case ISD::VPSD: \
513 return true;
514#include "llvm/IR/VPIntrinsics.def"
515 }
516}
517
518bool ISD::isVPBinaryOp(unsigned Opcode) {
519 switch (Opcode) {
520 default:
521 break;
522#define BEGIN_REGISTER_VP_SDNODE(VPSD, ...) case ISD::VPSD:
523#define VP_PROPERTY_BINARYOP return true;
524#define END_REGISTER_VP_SDNODE(VPSD) break;
525#include "llvm/IR/VPIntrinsics.def"
526 }
527 return false;
528}
529
530bool ISD::isVPReduction(unsigned Opcode) {
531 switch (Opcode) {
532 default:
533 return false;
534 case ISD::VP_REDUCE_ADD:
535 case ISD::VP_REDUCE_MUL:
536 case ISD::VP_REDUCE_AND:
537 case ISD::VP_REDUCE_OR:
538 case ISD::VP_REDUCE_XOR:
539 case ISD::VP_REDUCE_SMAX:
540 case ISD::VP_REDUCE_SMIN:
541 case ISD::VP_REDUCE_UMAX:
542 case ISD::VP_REDUCE_UMIN:
543 case ISD::VP_REDUCE_FMAX:
544 case ISD::VP_REDUCE_FMIN:
545 case ISD::VP_REDUCE_FMAXIMUM:
546 case ISD::VP_REDUCE_FMINIMUM:
547 case ISD::VP_REDUCE_FADD:
548 case ISD::VP_REDUCE_FMUL:
549 case ISD::VP_REDUCE_SEQ_FADD:
550 case ISD::VP_REDUCE_SEQ_FMUL:
551 return true;
552 }
553}
554
555/// The operand position of the vector mask.
556std::optional<unsigned> ISD::getVPMaskIdx(unsigned Opcode) {
557 switch (Opcode) {
558 default:
559 return std::nullopt;
560#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, ...) \
561 case ISD::VPSD: \
562 return MASKPOS;
563#include "llvm/IR/VPIntrinsics.def"
564 }
565}
566
567/// The operand position of the explicit vector length parameter.
568std::optional<unsigned> ISD::getVPExplicitVectorLengthIdx(unsigned Opcode) {
569 switch (Opcode) {
570 default:
571 return std::nullopt;
572#define BEGIN_REGISTER_VP_SDNODE(VPSD, LEGALPOS, TDNAME, MASKPOS, EVLPOS) \
573 case ISD::VPSD: \
574 return EVLPOS;
575#include "llvm/IR/VPIntrinsics.def"
576 }
577}
578
579std::optional<unsigned> ISD::getBaseOpcodeForVP(unsigned VPOpcode,
580 bool hasFPExcept) {
581 // FIXME: Return strict opcodes in case of fp exceptions.
582 switch (VPOpcode) {
583 default:
584 return std::nullopt;
585#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) case ISD::VPOPC:
586#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) return ISD::SDOPC;
587#define END_REGISTER_VP_SDNODE(VPOPC) break;
588#include "llvm/IR/VPIntrinsics.def"
589 }
590 return std::nullopt;
591}
592
593std::optional<unsigned> ISD::getVPForBaseOpcode(unsigned Opcode) {
594 switch (Opcode) {
595 default:
596 return std::nullopt;
597#define BEGIN_REGISTER_VP_SDNODE(VPOPC, ...) break;
598#define VP_PROPERTY_FUNCTIONAL_SDOPC(SDOPC) case ISD::SDOPC:
599#define END_REGISTER_VP_SDNODE(VPOPC) return ISD::VPOPC;
600#include "llvm/IR/VPIntrinsics.def"
601 }
602}
603
605 switch (ExtType) {
606 case ISD::EXTLOAD:
607 return IsFP ? ISD::FP_EXTEND : ISD::ANY_EXTEND;
608 case ISD::SEXTLOAD:
609 return ISD::SIGN_EXTEND;
610 case ISD::ZEXTLOAD:
611 return ISD::ZERO_EXTEND;
612 default:
613 break;
614 }
615
616 llvm_unreachable("Invalid LoadExtType");
617}
618
620 // To perform this operation, we just need to swap the L and G bits of the
621 // operation.
622 unsigned OldL = (Operation >> 2) & 1;
623 unsigned OldG = (Operation >> 1) & 1;
624 return ISD::CondCode((Operation & ~6) | // Keep the N, U, E bits
625 (OldL << 1) | // New G bit
626 (OldG << 2)); // New L bit.
627}
628
630 unsigned Operation = Op;
631 if (isIntegerLike)
632 Operation ^= 7; // Flip L, G, E bits, but not U.
633 else
634 Operation ^= 15; // Flip all of the condition bits.
635
637 Operation &= ~8; // Don't let N and U bits get set.
638
639 return ISD::CondCode(Operation);
640}
641
645
647 bool isIntegerLike) {
648 return getSetCCInverseImpl(Op, isIntegerLike);
649}
650
651/// For an integer comparison, return 1 if the comparison is a signed operation
652/// and 2 if the result is an unsigned comparison. Return zero if the operation
653/// does not depend on the sign of the input (setne and seteq).
654static int isSignedOp(ISD::CondCode Opcode) {
655 switch (Opcode) {
656 default: llvm_unreachable("Illegal integer setcc operation!");
657 case ISD::SETEQ:
658 case ISD::SETNE: return 0;
659 case ISD::SETLT:
660 case ISD::SETLE:
661 case ISD::SETGT:
662 case ISD::SETGE: return 1;
663 case ISD::SETULT:
664 case ISD::SETULE:
665 case ISD::SETUGT:
666 case ISD::SETUGE: return 2;
667 }
668}
669
671 EVT Type) {
672 bool IsInteger = Type.isInteger();
673 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
674 // Cannot fold a signed integer setcc with an unsigned integer setcc.
675 return ISD::SETCC_INVALID;
676
677 unsigned Op = Op1 | Op2; // Combine all of the condition bits.
678
679 // If the N and U bits get set, then the resultant comparison DOES suddenly
680 // care about orderedness, and it is true when ordered.
681 if (Op > ISD::SETTRUE2)
682 Op &= ~16; // Clear the U bit if the N bit is set.
683
684 // Canonicalize illegal integer setcc's.
685 if (IsInteger && Op == ISD::SETUNE) // e.g. SETUGT | SETULT
686 Op = ISD::SETNE;
687
688 return ISD::CondCode(Op);
689}
690
692 EVT Type) {
693 bool IsInteger = Type.isInteger();
694 if (IsInteger && (isSignedOp(Op1) | isSignedOp(Op2)) == 3)
695 // Cannot fold a signed setcc with an unsigned setcc.
696 return ISD::SETCC_INVALID;
697
698 // Combine all of the condition bits.
699 ISD::CondCode Result = ISD::CondCode(Op1 & Op2);
700
701 // Canonicalize illegal integer setcc's.
702 if (IsInteger) {
703 switch (Result) {
704 default: break;
705 case ISD::SETUO : Result = ISD::SETFALSE; break; // SETUGT & SETULT
706 case ISD::SETOEQ: // SETEQ & SETU[LG]E
707 case ISD::SETUEQ: Result = ISD::SETEQ ; break; // SETUGE & SETULE
708 case ISD::SETOLT: Result = ISD::SETULT ; break; // SETULT & SETNE
709 case ISD::SETOGT: Result = ISD::SETUGT ; break; // SETUGT & SETNE
710 }
711 }
712
713 return Result;
714}
715
716//===----------------------------------------------------------------------===//
717// SDNode Profile Support
718//===----------------------------------------------------------------------===//
719
720/// AddNodeIDOpcode - Add the node opcode to the NodeID data.
721static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC) {
722 ID.AddInteger(OpC);
723}
724
725/// AddNodeIDValueTypes - Value type lists are intern'd so we can represent them
726/// solely with their pointer.
728 ID.AddPointer(VTList.VTs);
729}
730
731/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
734 for (const auto &Op : Ops) {
735 ID.AddPointer(Op.getNode());
736 ID.AddInteger(Op.getResNo());
737 }
738}
739
740/// AddNodeIDOperands - Various routines for adding operands to the NodeID data.
743 for (const auto &Op : Ops) {
744 ID.AddPointer(Op.getNode());
745 ID.AddInteger(Op.getResNo());
746 }
747}
748
749static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC,
750 SDVTList VTList, ArrayRef<SDValue> OpList) {
751 AddNodeIDOpcode(ID, OpC);
752 AddNodeIDValueTypes(ID, VTList);
753 AddNodeIDOperands(ID, OpList);
754}
755
756/// If this is an SDNode with special info, add this info to the NodeID data.
758 switch (N->getOpcode()) {
761 case ISD::MCSymbol:
762 llvm_unreachable("Should only be used on nodes with operands");
763 default: break; // Normal nodes don't need extra info.
765 case ISD::Constant: {
767 ID.AddPointer(C->getConstantIntValue());
768 ID.AddBoolean(C->isOpaque());
769 break;
770 }
772 case ISD::ConstantFP:
773 ID.AddPointer(cast<ConstantFPSDNode>(N)->getConstantFPValue());
774 break;
780 ID.AddPointer(GA->getGlobal());
781 ID.AddInteger(GA->getOffset());
782 ID.AddInteger(GA->getTargetFlags());
783 break;
784 }
785 case ISD::BasicBlock:
787 break;
788 case ISD::Register:
789 ID.AddInteger(cast<RegisterSDNode>(N)->getReg().id());
790 break;
792 ID.AddPointer(cast<RegisterMaskSDNode>(N)->getRegMask());
793 break;
794 case ISD::SRCVALUE:
795 ID.AddPointer(cast<SrcValueSDNode>(N)->getValue());
796 break;
797 case ISD::FrameIndex:
799 ID.AddInteger(cast<FrameIndexSDNode>(N)->getIndex());
800 break;
802 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getGuid());
803 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getIndex());
804 ID.AddInteger(cast<PseudoProbeSDNode>(N)->getAttributes());
805 break;
806 case ISD::JumpTable:
808 ID.AddInteger(cast<JumpTableSDNode>(N)->getIndex());
809 ID.AddInteger(cast<JumpTableSDNode>(N)->getTargetFlags());
810 break;
814 ID.AddInteger(CP->getAlign().value());
815 ID.AddInteger(CP->getOffset());
818 else
819 ID.AddPointer(CP->getConstVal());
820 ID.AddInteger(CP->getTargetFlags());
821 break;
822 }
823 case ISD::TargetIndex: {
825 ID.AddInteger(TI->getIndex());
826 ID.AddInteger(TI->getOffset());
827 ID.AddInteger(TI->getTargetFlags());
828 break;
829 }
830 case ISD::LOAD: {
831 const LoadSDNode *LD = cast<LoadSDNode>(N);
832 ID.AddInteger(LD->getMemoryVT().getRawBits());
833 ID.AddInteger(LD->getRawSubclassData());
834 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
835 ID.AddInteger(LD->getMemOperand()->getFlags());
836 break;
837 }
838 case ISD::STORE: {
839 const StoreSDNode *ST = cast<StoreSDNode>(N);
840 ID.AddInteger(ST->getMemoryVT().getRawBits());
841 ID.AddInteger(ST->getRawSubclassData());
842 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
843 ID.AddInteger(ST->getMemOperand()->getFlags());
844 break;
845 }
846 case ISD::VP_LOAD: {
847 const VPLoadSDNode *ELD = cast<VPLoadSDNode>(N);
848 ID.AddInteger(ELD->getMemoryVT().getRawBits());
849 ID.AddInteger(ELD->getRawSubclassData());
850 ID.AddInteger(ELD->getPointerInfo().getAddrSpace());
851 ID.AddInteger(ELD->getMemOperand()->getFlags());
852 break;
853 }
854 case ISD::VP_LOAD_FF: {
855 const auto *LD = cast<VPLoadFFSDNode>(N);
856 ID.AddInteger(LD->getMemoryVT().getRawBits());
857 ID.AddInteger(LD->getRawSubclassData());
858 ID.AddInteger(LD->getPointerInfo().getAddrSpace());
859 ID.AddInteger(LD->getMemOperand()->getFlags());
860 break;
861 }
862 case ISD::VP_STORE: {
863 const VPStoreSDNode *EST = cast<VPStoreSDNode>(N);
864 ID.AddInteger(EST->getMemoryVT().getRawBits());
865 ID.AddInteger(EST->getRawSubclassData());
866 ID.AddInteger(EST->getPointerInfo().getAddrSpace());
867 ID.AddInteger(EST->getMemOperand()->getFlags());
868 break;
869 }
870 case ISD::EXPERIMENTAL_VP_STRIDED_LOAD: {
872 ID.AddInteger(SLD->getMemoryVT().getRawBits());
873 ID.AddInteger(SLD->getRawSubclassData());
874 ID.AddInteger(SLD->getPointerInfo().getAddrSpace());
875 break;
876 }
877 case ISD::EXPERIMENTAL_VP_STRIDED_STORE: {
879 ID.AddInteger(SST->getMemoryVT().getRawBits());
880 ID.AddInteger(SST->getRawSubclassData());
881 ID.AddInteger(SST->getPointerInfo().getAddrSpace());
882 break;
883 }
884 case ISD::VP_GATHER: {
886 ID.AddInteger(EG->getMemoryVT().getRawBits());
887 ID.AddInteger(EG->getRawSubclassData());
888 ID.AddInteger(EG->getPointerInfo().getAddrSpace());
889 ID.AddInteger(EG->getMemOperand()->getFlags());
890 break;
891 }
892 case ISD::VP_SCATTER: {
894 ID.AddInteger(ES->getMemoryVT().getRawBits());
895 ID.AddInteger(ES->getRawSubclassData());
896 ID.AddInteger(ES->getPointerInfo().getAddrSpace());
897 ID.AddInteger(ES->getMemOperand()->getFlags());
898 break;
899 }
900 case ISD::MLOAD: {
902 ID.AddInteger(MLD->getMemoryVT().getRawBits());
903 ID.AddInteger(MLD->getRawSubclassData());
904 ID.AddInteger(MLD->getPointerInfo().getAddrSpace());
905 ID.AddInteger(MLD->getMemOperand()->getFlags());
906 break;
907 }
908 case ISD::MSTORE: {
910 ID.AddInteger(MST->getMemoryVT().getRawBits());
911 ID.AddInteger(MST->getRawSubclassData());
912 ID.AddInteger(MST->getPointerInfo().getAddrSpace());
913 ID.AddInteger(MST->getMemOperand()->getFlags());
914 break;
915 }
916 case ISD::MGATHER: {
918 ID.AddInteger(MG->getMemoryVT().getRawBits());
919 ID.AddInteger(MG->getRawSubclassData());
920 ID.AddInteger(MG->getPointerInfo().getAddrSpace());
921 ID.AddInteger(MG->getMemOperand()->getFlags());
922 break;
923 }
924 case ISD::MSCATTER: {
926 ID.AddInteger(MS->getMemoryVT().getRawBits());
927 ID.AddInteger(MS->getRawSubclassData());
928 ID.AddInteger(MS->getPointerInfo().getAddrSpace());
929 ID.AddInteger(MS->getMemOperand()->getFlags());
930 break;
931 }
934 case ISD::ATOMIC_SWAP:
946 case ISD::ATOMIC_LOAD:
947 case ISD::ATOMIC_STORE: {
948 const AtomicSDNode *AT = cast<AtomicSDNode>(N);
949 ID.AddInteger(AT->getMemoryVT().getRawBits());
950 ID.AddInteger(AT->getRawSubclassData());
951 ID.AddInteger(AT->getPointerInfo().getAddrSpace());
952 ID.AddInteger(AT->getMemOperand()->getFlags());
953 break;
954 }
955 case ISD::VECTOR_SHUFFLE: {
956 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask();
957 for (int M : Mask)
958 ID.AddInteger(M);
959 break;
960 }
961 case ISD::ADDRSPACECAST: {
963 ID.AddInteger(ASC->getSrcAddressSpace());
964 ID.AddInteger(ASC->getDestAddressSpace());
965 break;
966 }
968 case ISD::BlockAddress: {
970 ID.AddPointer(BA->getBlockAddress());
971 ID.AddInteger(BA->getOffset());
972 ID.AddInteger(BA->getTargetFlags());
973 break;
974 }
975 case ISD::AssertAlign:
976 ID.AddInteger(cast<AssertAlignSDNode>(N)->getAlign().value());
977 break;
978 case ISD::PREFETCH:
981 // Handled by MemIntrinsicSDNode check after the switch.
982 break;
984 ID.AddPointer(cast<MDNodeSDNode>(N)->getMD());
985 break;
986 } // end switch (N->getOpcode())
987
988 // MemIntrinsic nodes could also have subclass data, address spaces, and flags
989 // to check.
990 if (auto *MN = dyn_cast<MemIntrinsicSDNode>(N)) {
991 ID.AddInteger(MN->getRawSubclassData());
992 ID.AddInteger(MN->getPointerInfo().getAddrSpace());
993 ID.AddInteger(MN->getMemOperand()->getFlags());
994 ID.AddInteger(MN->getMemoryVT().getRawBits());
995 }
996}
997
998/// AddNodeIDNode - Generic routine for adding a nodes info to the NodeID
999/// data.
1001 AddNodeIDOpcode(ID, N->getOpcode());
1002 // Add the return value info.
1003 AddNodeIDValueTypes(ID, N->getVTList());
1004 // Add the operand info.
1005 AddNodeIDOperands(ID, N->ops());
1006
1007 // Handle SDNode leafs with special info.
1009}
1010
1011//===----------------------------------------------------------------------===//
1012// SelectionDAG Class
1013//===----------------------------------------------------------------------===//
1014
1015/// doNotCSE - Return true if CSE should not be performed for this node.
1016static bool doNotCSE(SDNode *N) {
1017 if (N->getValueType(0) == MVT::Glue)
1018 return true; // Never CSE anything that produces a glue result.
1019
1020 switch (N->getOpcode()) {
1021 default: break;
1022 case ISD::HANDLENODE:
1023 case ISD::EH_LABEL:
1024 return true; // Never CSE these nodes.
1025 }
1026
1027 // Check that remaining values produced are not flags.
1028 for (unsigned i = 1, e = N->getNumValues(); i != e; ++i)
1029 if (N->getValueType(i) == MVT::Glue)
1030 return true; // Never CSE anything that produces a glue result.
1031
1032 return false;
1033}
1034
1035/// RemoveDeadNodes - This method deletes all unreachable nodes in the
1036/// SelectionDAG.
1038 // Create a dummy node (which is not added to allnodes), that adds a reference
1039 // to the root node, preventing it from being deleted.
1040 HandleSDNode Dummy(getRoot());
1041
1042 SmallVector<SDNode*, 128> DeadNodes;
1043
1044 // Add all obviously-dead nodes to the DeadNodes worklist.
1045 for (SDNode &Node : allnodes())
1046 if (Node.use_empty())
1047 DeadNodes.push_back(&Node);
1048
1049 RemoveDeadNodes(DeadNodes);
1050
1051 // If the root changed (e.g. it was a dead load, update the root).
1052 setRoot(Dummy.getValue());
1053}
1054
1055/// RemoveDeadNodes - This method deletes the unreachable nodes in the
1056/// given list, and any nodes that become unreachable as a result.
1058
1059 // Process the worklist, deleting the nodes and adding their uses to the
1060 // worklist.
1061 while (!DeadNodes.empty()) {
1062 SDNode *N = DeadNodes.pop_back_val();
1063 // Skip to next node if we've already managed to delete the node. This could
1064 // happen if replacing a node causes a node previously added to the node to
1065 // be deleted.
1066 if (N->getOpcode() == ISD::DELETED_NODE)
1067 continue;
1068
1069 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1070 DUL->NodeDeleted(N, nullptr);
1071
1072 // Take the node out of the appropriate CSE map.
1073 RemoveNodeFromCSEMaps(N);
1074
1075 // Next, brutally remove the operand list. This is safe to do, as there are
1076 // no cycles in the graph.
1077 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
1078 SDUse &Use = *I++;
1079 SDNode *Operand = Use.getNode();
1080 Use.set(SDValue());
1081
1082 // Now that we removed this operand, see if there are no uses of it left.
1083 if (Operand->use_empty())
1084 DeadNodes.push_back(Operand);
1085 }
1086
1087 DeallocateNode(N);
1088 }
1089}
1090
1092 SmallVector<SDNode*, 16> DeadNodes(1, N);
1093
1094 // Create a dummy node that adds a reference to the root node, preventing
1095 // it from being deleted. (This matters if the root is an operand of the
1096 // dead node.)
1097 HandleSDNode Dummy(getRoot());
1098
1099 RemoveDeadNodes(DeadNodes);
1100}
1101
1103 // First take this out of the appropriate CSE map.
1104 RemoveNodeFromCSEMaps(N);
1105
1106 // Finally, remove uses due to operands of this node, remove from the
1107 // AllNodes list, and delete the node.
1108 DeleteNodeNotInCSEMaps(N);
1109}
1110
1111void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *N) {
1112 assert(N->getIterator() != AllNodes.begin() &&
1113 "Cannot delete the entry node!");
1114 assert(N->use_empty() && "Cannot delete a node that is not dead!");
1115
1116 // Drop all of the operands and decrement used node's use counts.
1117 N->DropOperands();
1118
1119 DeallocateNode(N);
1120}
1121
1122void SDDbgInfo::add(SDDbgValue *V, bool isParameter) {
1123 assert(!(V->isVariadic() && isParameter));
1124 if (isParameter)
1125 ByvalParmDbgValues.push_back(V);
1126 else
1127 DbgValues.push_back(V);
1128 for (const SDNode *Node : V->getSDNodes())
1129 if (Node)
1130 DbgValMap[Node].push_back(V);
1131}
1132
1134 DbgValMapType::iterator I = DbgValMap.find(Node);
1135 if (I == DbgValMap.end())
1136 return;
1137 for (auto &Val: I->second)
1138 Val->setIsInvalidated();
1139 DbgValMap.erase(I);
1140}
1141
1142void SelectionDAG::DeallocateNode(SDNode *N) {
1143 // If we have operands, deallocate them.
1145
1146 NodeAllocator.Deallocate(AllNodes.remove(N));
1147
1148 // Set the opcode to DELETED_NODE to help catch bugs when node
1149 // memory is reallocated.
1150 // FIXME: There are places in SDag that have grown a dependency on the opcode
1151 // value in the released node.
1152 __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
1153 N->NodeType = ISD::DELETED_NODE;
1154
1155 // If any of the SDDbgValue nodes refer to this SDNode, invalidate
1156 // them and forget about that node.
1157 DbgInfo->erase(N);
1158
1159 // Invalidate extra info.
1160 SDEI.erase(N);
1161}
1162
1163#ifndef NDEBUG
1164/// VerifySDNode - Check the given SDNode. Aborts if it is invalid.
1165void SelectionDAG::verifyNode(SDNode *N) const {
1166 switch (N->getOpcode()) {
1167 default:
1168 if (N->isTargetOpcode())
1170 break;
1171 case ISD::BUILD_PAIR: {
1172 EVT VT = N->getValueType(0);
1173 assert(N->getNumValues() == 1 && "Too many results!");
1174 assert(!VT.isVector() && (VT.isInteger() || VT.isFloatingPoint()) &&
1175 "Wrong return type!");
1176 assert(N->getNumOperands() == 2 && "Wrong number of operands!");
1177 assert(N->getOperand(0).getValueType() == N->getOperand(1).getValueType() &&
1178 "Mismatched operand types!");
1179 assert(N->getOperand(0).getValueType().isInteger() == VT.isInteger() &&
1180 "Wrong operand type!");
1181 assert(VT.getSizeInBits() == 2 * N->getOperand(0).getValueSizeInBits() &&
1182 "Wrong return type size");
1183 break;
1184 }
1185 case ISD::BUILD_VECTOR: {
1186 assert(N->getNumValues() == 1 && "Too many results!");
1187 assert(N->getValueType(0).isVector() && "Wrong return type!");
1188 assert(N->getNumOperands() == N->getValueType(0).getVectorNumElements() &&
1189 "Wrong number of operands!");
1190 EVT EltVT = N->getValueType(0).getVectorElementType();
1191 for (const SDUse &Op : N->ops()) {
1192 assert((Op.getValueType() == EltVT ||
1193 (EltVT.isInteger() && Op.getValueType().isInteger() &&
1194 EltVT.bitsLE(Op.getValueType()))) &&
1195 "Wrong operand type!");
1196 assert(Op.getValueType() == N->getOperand(0).getValueType() &&
1197 "Operands must all have the same type");
1198 }
1199 break;
1200 }
1201 }
1202}
1203#endif // NDEBUG
1204
1205/// Insert a newly allocated node into the DAG.
1206///
1207/// Handles insertion into the all nodes list and CSE map, as well as
1208/// verification and other common operations when a new node is allocated.
1209void SelectionDAG::InsertNode(SDNode *N) {
1210 AllNodes.push_back(N);
1211#ifndef NDEBUG
1212 N->PersistentId = NextPersistentId++;
1213 verifyNode(N);
1214#endif
1215 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1216 DUL->NodeInserted(N);
1217}
1218
1219/// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
1220/// correspond to it. This is useful when we're about to delete or repurpose
1221/// the node. We don't want future request for structurally identical nodes
1222/// to return N anymore.
1223bool SelectionDAG::RemoveNodeFromCSEMaps(SDNode *N) {
1224 bool Erased = false;
1225 switch (N->getOpcode()) {
1226 case ISD::HANDLENODE: return false; // noop.
1227 case ISD::CONDCODE:
1228 assert(CondCodeNodes[cast<CondCodeSDNode>(N)->get()] &&
1229 "Cond code doesn't exist!");
1230 Erased = CondCodeNodes[cast<CondCodeSDNode>(N)->get()] != nullptr;
1231 CondCodeNodes[cast<CondCodeSDNode>(N)->get()] = nullptr;
1232 break;
1234 Erased = ExternalSymbols.erase(cast<ExternalSymbolSDNode>(N)->getSymbol());
1235 break;
1237 ExternalSymbolSDNode *ESN = cast<ExternalSymbolSDNode>(N);
1238 Erased = TargetExternalSymbols.erase(std::pair<std::string, unsigned>(
1239 ESN->getSymbol(), ESN->getTargetFlags()));
1240 break;
1241 }
1242 case ISD::MCSymbol: {
1243 auto *MCSN = cast<MCSymbolSDNode>(N);
1244 Erased = MCSymbols.erase(MCSN->getMCSymbol());
1245 break;
1246 }
1247 case ISD::VALUETYPE: {
1248 EVT VT = cast<VTSDNode>(N)->getVT();
1249 if (VT.isExtended()) {
1250 Erased = ExtendedValueTypeNodes.erase(VT);
1251 } else {
1252 Erased = ValueTypeNodes[VT.getSimpleVT().SimpleTy] != nullptr;
1253 ValueTypeNodes[VT.getSimpleVT().SimpleTy] = nullptr;
1254 }
1255 break;
1256 }
1257 default:
1258 // Remove it from the CSE Map.
1259 assert(N->getOpcode() != ISD::DELETED_NODE && "DELETED_NODE in CSEMap!");
1260 assert(N->getOpcode() != ISD::EntryToken && "EntryToken in CSEMap!");
1261 Erased = CSEMap.RemoveNode(N);
1262 break;
1263 }
1264#ifndef NDEBUG
1265 // Verify that the node was actually in one of the CSE maps, unless it has a
1266 // glue result (which cannot be CSE'd) or is one of the special cases that are
1267 // not subject to CSE.
1268 if (!Erased && N->getValueType(N->getNumValues()-1) != MVT::Glue &&
1269 !N->isMachineOpcode() && !doNotCSE(N)) {
1270 N->dump(this);
1271 dbgs() << "\n";
1272 llvm_unreachable("Node is not in map!");
1273 }
1274#endif
1275 return Erased;
1276}
1277
1278/// AddModifiedNodeToCSEMaps - The specified node has been removed from the CSE
1279/// maps and modified in place. Add it back to the CSE maps, unless an identical
1280/// node already exists, in which case transfer all its users to the existing
1281/// node. This transfer can potentially trigger recursive merging.
1282void
1283SelectionDAG::AddModifiedNodeToCSEMaps(SDNode *N) {
1284 // For node types that aren't CSE'd, just act as if no identical node
1285 // already exists.
1286 if (!doNotCSE(N)) {
1287 SDNode *Existing = CSEMap.GetOrInsertNode(N);
1288 if (Existing != N) {
1289 // If there was already an existing matching node, use ReplaceAllUsesWith
1290 // to replace the dead one with the existing one. This can cause
1291 // recursive merging of other unrelated nodes down the line.
1292 Existing->intersectFlagsWith(N->getFlags());
1293 if (auto *MemNode = dyn_cast<MemSDNode>(Existing))
1294 MemNode->refineRanges(cast<MemSDNode>(N)->getMemOperand());
1295 ReplaceAllUsesWith(N, Existing);
1296
1297 // N is now dead. Inform the listeners and delete it.
1298 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1299 DUL->NodeDeleted(N, Existing);
1300 DeleteNodeNotInCSEMaps(N);
1301 return;
1302 }
1303 }
1304
1305 // If the node doesn't already exist, we updated it. Inform listeners.
1306 for (DAGUpdateListener *DUL = UpdateListeners; DUL; DUL = DUL->Next)
1307 DUL->NodeUpdated(N);
1308}
1309
1310/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1311/// were replaced with those specified. If this node is never memoized,
1312/// return null, otherwise return a pointer to the slot it would take. If a
1313/// node already exists with these operands, the slot will be non-null.
1314SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, SDValue Op,
1315 void *&InsertPos) {
1316 if (doNotCSE(N))
1317 return nullptr;
1318
1319 SDValue Ops[] = { Op };
1320 FoldingSetNodeID ID;
1321 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1323 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1324 if (Node)
1325 Node->intersectFlagsWith(N->getFlags());
1326 return Node;
1327}
1328
1329/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1330/// were replaced with those specified. If this node is never memoized,
1331/// return null, otherwise return a pointer to the slot it would take. If a
1332/// node already exists with these operands, the slot will be non-null.
1333SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N,
1334 SDValue Op1, SDValue Op2,
1335 void *&InsertPos) {
1336 if (doNotCSE(N))
1337 return nullptr;
1338
1339 SDValue Ops[] = { Op1, Op2 };
1340 FoldingSetNodeID ID;
1341 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1343 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1344 if (Node)
1345 Node->intersectFlagsWith(N->getFlags());
1346 return Node;
1347}
1348
1349/// FindModifiedNodeSlot - Find a slot for the specified node if its operands
1350/// were replaced with those specified. If this node is never memoized,
1351/// return null, otherwise return a pointer to the slot it would take. If a
1352/// node already exists with these operands, the slot will be non-null.
1353SDNode *SelectionDAG::FindModifiedNodeSlot(SDNode *N, ArrayRef<SDValue> Ops,
1354 void *&InsertPos) {
1355 if (doNotCSE(N))
1356 return nullptr;
1357
1358 FoldingSetNodeID ID;
1359 AddNodeIDNode(ID, N->getOpcode(), N->getVTList(), Ops);
1361 SDNode *Node = FindNodeOrInsertPos(ID, SDLoc(N), InsertPos);
1362 if (Node)
1363 Node->intersectFlagsWith(N->getFlags());
1364 return Node;
1365}
1366
1368 Type *Ty = VT == MVT::iPTR ? PointerType::get(*getContext(), 0)
1369 : VT.getTypeForEVT(*getContext());
1370
1371 return getDataLayout().getABITypeAlign(Ty);
1372}
1373
1374// EntryNode could meaningfully have debug info if we can find it...
1376 : TM(tm), OptLevel(OL), EntryNode(ISD::EntryToken, 0, DebugLoc(),
1377 getVTList(MVT::Other, MVT::Glue)),
1378 Root(getEntryNode()) {
1379 InsertNode(&EntryNode);
1380 DbgInfo = new SDDbgInfo();
1381}
1382
1384 OptimizationRemarkEmitter &NewORE, Pass *PassPtr,
1385 const TargetLibraryInfo *LibraryInfo,
1386 const LibcallLoweringInfo *LibcallsInfo,
1387 UniformityInfo *NewUA, ProfileSummaryInfo *PSIin,
1389 FunctionVarLocs const *VarLocs) {
1390 MF = &NewMF;
1391 SDAGISelPass = PassPtr;
1392 ORE = &NewORE;
1395 LibInfo = LibraryInfo;
1396 Libcalls = LibcallsInfo;
1397 Context = &MF->getFunction().getContext();
1398 UA = NewUA;
1399 PSI = PSIin;
1400 BFI = BFIin;
1401 MMI = &MMIin;
1402 FnVarLocs = VarLocs;
1403}
1404
1406 assert(!UpdateListeners && "Dangling registered DAGUpdateListeners");
1407 allnodes_clear();
1408 OperandRecycler.clear(OperandAllocator);
1409 delete DbgInfo;
1410}
1411
1413 return llvm::shouldOptimizeForSize(FLI->MBB->getBasicBlock(), PSI, BFI);
1414}
1415
1416void SelectionDAG::allnodes_clear() {
1417 assert(&*AllNodes.begin() == &EntryNode);
1418 AllNodes.remove(AllNodes.begin());
1419 while (!AllNodes.empty())
1420 DeallocateNode(&AllNodes.front());
1421#ifndef NDEBUG
1422 NextPersistentId = 0;
1423#endif
1424}
1425
1426SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1427 void *&InsertPos) {
1428 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1429 if (N) {
1430 switch (N->getOpcode()) {
1431 default: break;
1432 case ISD::Constant:
1433 case ISD::ConstantFP:
1434 llvm_unreachable("Querying for Constant and ConstantFP nodes requires "
1435 "debug location. Use another overload.");
1436 }
1437 }
1438 return N;
1439}
1440
1441SDNode *SelectionDAG::FindNodeOrInsertPos(const FoldingSetNodeID &ID,
1442 const SDLoc &DL, void *&InsertPos) {
1443 SDNode *N = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
1444 if (N) {
1445 switch (N->getOpcode()) {
1446 case ISD::Constant:
1447 case ISD::ConstantFP:
1448 // Erase debug location from the node if the node is used at several
1449 // different places. Do not propagate one location to all uses as it
1450 // will cause a worse single stepping debugging experience.
1451 if (N->getDebugLoc() != DL.getDebugLoc())
1452 N->setDebugLoc(DebugLoc());
1453 break;
1454 default:
1455 // When the node's point of use is located earlier in the instruction
1456 // sequence than its prior point of use, update its debug info to the
1457 // earlier location.
1458 if (DL.getIROrder() && DL.getIROrder() < N->getIROrder())
1459 N->setDebugLoc(DL.getDebugLoc());
1460 break;
1461 }
1462 }
1463 return N;
1464}
1465
1467 allnodes_clear();
1468 OperandRecycler.clear(OperandAllocator);
1469 OperandAllocator.Reset();
1470 CSEMap.clear();
1471
1472 ExtendedValueTypeNodes.clear();
1473 ExternalSymbols.clear();
1474 TargetExternalSymbols.clear();
1475 MCSymbols.clear();
1476 SDEI.clear();
1477 llvm::fill(CondCodeNodes, nullptr);
1478 llvm::fill(ValueTypeNodes, nullptr);
1479
1480 EntryNode.UseList = nullptr;
1481 InsertNode(&EntryNode);
1482 Root = getEntryNode();
1483 DbgInfo->clear();
1484}
1485
1487 return VT.bitsGT(Op.getValueType())
1488 ? getNode(ISD::FP_EXTEND, DL, VT, Op)
1489 : getNode(ISD::FP_ROUND, DL, VT, Op,
1490 getIntPtrConstant(0, DL, /*isTarget=*/true));
1491}
1492
1493std::pair<SDValue, SDValue>
1495 const SDLoc &DL, EVT VT) {
1496 assert(!VT.bitsEq(Op.getValueType()) &&
1497 "Strict no-op FP extend/round not allowed.");
1498 SDValue Res =
1499 VT.bitsGT(Op.getValueType())
1500 ? getNode(ISD::STRICT_FP_EXTEND, DL, {VT, MVT::Other}, {Chain, Op})
1501 : getNode(ISD::STRICT_FP_ROUND, DL, {VT, MVT::Other},
1502 {Chain, Op, getIntPtrConstant(0, DL, /*isTarget=*/true)});
1503
1504 return std::pair<SDValue, SDValue>(Res, SDValue(Res.getNode(), 1));
1505}
1506
1508 return VT.bitsGT(Op.getValueType()) ?
1509 getNode(ISD::ANY_EXTEND, DL, VT, Op) :
1510 getNode(ISD::TRUNCATE, DL, VT, Op);
1511}
1512
1514 return VT.bitsGT(Op.getValueType()) ?
1515 getNode(ISD::SIGN_EXTEND, DL, VT, Op) :
1516 getNode(ISD::TRUNCATE, DL, VT, Op);
1517}
1518
1520 return VT.bitsGT(Op.getValueType()) ?
1521 getNode(ISD::ZERO_EXTEND, DL, VT, Op) :
1522 getNode(ISD::TRUNCATE, DL, VT, Op);
1523}
1524
1526 EVT VT) {
1527 assert(!VT.isVector());
1528 auto Type = Op.getValueType();
1529 SDValue DestOp;
1530 if (Type == VT)
1531 return Op;
1532 auto Size = Op.getValueSizeInBits();
1533 DestOp = getBitcast(EVT::getIntegerVT(*Context, Size), Op);
1534 if (DestOp.getValueType() == VT)
1535 return DestOp;
1536
1537 return getAnyExtOrTrunc(DestOp, DL, VT);
1538}
1539
1541 EVT VT) {
1542 assert(!VT.isVector());
1543 auto Type = Op.getValueType();
1544 SDValue DestOp;
1545 if (Type == VT)
1546 return Op;
1547 auto Size = Op.getValueSizeInBits();
1548 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1549 if (DestOp.getValueType() == VT)
1550 return DestOp;
1551
1552 return getSExtOrTrunc(DestOp, DL, VT);
1553}
1554
1556 EVT VT) {
1557 assert(!VT.isVector());
1558 auto Type = Op.getValueType();
1559 SDValue DestOp;
1560 if (Type == VT)
1561 return Op;
1562 auto Size = Op.getValueSizeInBits();
1563 DestOp = getBitcast(MVT::getIntegerVT(Size), Op);
1564 if (DestOp.getValueType() == VT)
1565 return DestOp;
1566
1567 return getZExtOrTrunc(DestOp, DL, VT);
1568}
1569
1571 EVT OpVT) {
1572 if (VT.bitsLE(Op.getValueType()))
1573 return getNode(ISD::TRUNCATE, SL, VT, Op);
1574
1575 TargetLowering::BooleanContent BType = TLI->getBooleanContents(OpVT);
1576 return getNode(TLI->getExtendForContent(BType), SL, VT, Op);
1577}
1578
1580 EVT OpVT = Op.getValueType();
1581 assert(VT.isInteger() && OpVT.isInteger() &&
1582 "Cannot getZeroExtendInReg FP types");
1583 assert(VT.isVector() == OpVT.isVector() &&
1584 "getZeroExtendInReg type should be vector iff the operand "
1585 "type is vector!");
1586 assert((!VT.isVector() ||
1588 "Vector element counts must match in getZeroExtendInReg");
1589 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1590 if (OpVT == VT)
1591 return Op;
1592 // TODO: Use computeKnownBits instead of AssertZext.
1593 if (Op.getOpcode() == ISD::AssertZext && cast<VTSDNode>(Op.getOperand(1))
1594 ->getVT()
1595 .getScalarType()
1596 .bitsLE(VT.getScalarType()))
1597 return Op;
1599 VT.getScalarSizeInBits());
1600 return getNode(ISD::AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT));
1601}
1602
1604 SDValue EVL, const SDLoc &DL,
1605 EVT VT) {
1606 EVT OpVT = Op.getValueType();
1607 assert(VT.isInteger() && OpVT.isInteger() &&
1608 "Cannot getVPZeroExtendInReg FP types");
1609 assert(VT.isVector() && OpVT.isVector() &&
1610 "getVPZeroExtendInReg type and operand type should be vector!");
1612 "Vector element counts must match in getZeroExtendInReg");
1613 assert(VT.getScalarType().bitsLE(OpVT.getScalarType()) && "Not extending!");
1614 if (OpVT == VT)
1615 return Op;
1617 VT.getScalarSizeInBits());
1618 return getNode(ISD::VP_AND, DL, OpVT, Op, getConstant(Imm, DL, OpVT), Mask,
1619 EVL);
1620}
1621
1623 // Only unsigned pointer semantics are supported right now. In the future this
1624 // might delegate to TLI to check pointer signedness.
1625 return getZExtOrTrunc(Op, DL, VT);
1626}
1627
1629 // Only unsigned pointer semantics are supported right now. In the future this
1630 // might delegate to TLI to check pointer signedness.
1631 return getZeroExtendInReg(Op, DL, VT);
1632}
1633
1635 return getNode(ISD::SUB, DL, VT, getConstant(0, DL, VT), Val);
1636}
1637
1638/// getNOT - Create a bitwise NOT operation as (XOR Val, -1).
1640 return getNode(ISD::XOR, DL, VT, Val, getAllOnesConstant(DL, VT));
1641}
1642
1644 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1645 return getNode(ISD::XOR, DL, VT, Val, TrueValue);
1646}
1647
1649 SDValue Mask, SDValue EVL, EVT VT) {
1650 SDValue TrueValue = getBoolConstant(true, DL, VT, VT);
1651 return getNode(ISD::VP_XOR, DL, VT, Val, TrueValue, Mask, EVL);
1652}
1653
1655 SDValue Mask, SDValue EVL) {
1656 return getVPZExtOrTrunc(DL, VT, Op, Mask, EVL);
1657}
1658
1660 SDValue Mask, SDValue EVL) {
1661 if (VT.bitsGT(Op.getValueType()))
1662 return getNode(ISD::VP_ZERO_EXTEND, DL, VT, Op, Mask, EVL);
1663 if (VT.bitsLT(Op.getValueType()))
1664 return getNode(ISD::VP_TRUNCATE, DL, VT, Op, Mask, EVL);
1665 return Op;
1666}
1667
1669 EVT OpVT) {
1670 if (!V)
1671 return getConstant(0, DL, VT);
1672
1673 switch (TLI->getBooleanContents(OpVT)) {
1676 return getConstant(1, DL, VT);
1678 return getAllOnesConstant(DL, VT);
1679 }
1680 llvm_unreachable("Unexpected boolean content enum!");
1681}
1682
1684 bool isT, bool isO) {
1685 return getConstant(APInt(VT.getScalarSizeInBits(), Val, /*isSigned=*/false),
1686 DL, VT, isT, isO);
1687}
1688
1690 bool isT, bool isO) {
1691 return getConstant(*ConstantInt::get(*Context, Val), DL, VT, isT, isO);
1692}
1693
1695 EVT VT, bool isT, bool isO) {
1696 assert(VT.isInteger() && "Cannot create FP integer constant!");
1697
1698 EVT EltVT = VT.getScalarType();
1699 const ConstantInt *Elt = &Val;
1700
1701 // Vector splats are explicit within the DAG, with ConstantSDNode holding the
1702 // to-be-splatted scalar ConstantInt.
1703 if (isa<VectorType>(Elt->getType()))
1704 Elt = ConstantInt::get(*getContext(), Elt->getValue());
1705
1706 // In some cases the vector type is legal but the element type is illegal and
1707 // needs to be promoted, for example v8i8 on ARM. In this case, promote the
1708 // inserted value (the type does not need to match the vector element type).
1709 // Any extra bits introduced will be truncated away.
1710 if (VT.isVector() && TLI->getTypeAction(*getContext(), EltVT) ==
1712 EltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1713 APInt NewVal;
1714 if (TLI->isSExtCheaperThanZExt(VT.getScalarType(), EltVT))
1715 NewVal = Elt->getValue().sextOrTrunc(EltVT.getSizeInBits());
1716 else
1717 NewVal = Elt->getValue().zextOrTrunc(EltVT.getSizeInBits());
1718 Elt = ConstantInt::get(*getContext(), NewVal);
1719 }
1720 // In other cases the element type is illegal and needs to be expanded, for
1721 // example v2i64 on MIPS32. In this case, find the nearest legal type, split
1722 // the value into n parts and use a vector type with n-times the elements.
1723 // Then bitcast to the type requested.
1724 // Legalizing constants too early makes the DAGCombiner's job harder so we
1725 // only legalize if the DAG tells us we must produce legal types.
1726 else if (NewNodesMustHaveLegalTypes && VT.isVector() &&
1727 TLI->getTypeAction(*getContext(), EltVT) ==
1729 const APInt &NewVal = Elt->getValue();
1730 EVT ViaEltVT = TLI->getTypeToTransformTo(*getContext(), EltVT);
1731 unsigned ViaEltSizeInBits = ViaEltVT.getSizeInBits();
1732
1733 // For scalable vectors, try to use a SPLAT_VECTOR_PARTS node.
1734 if (VT.isScalableVector() ||
1735 TLI->isOperationLegal(ISD::SPLAT_VECTOR, VT)) {
1736 assert(EltVT.getSizeInBits() % ViaEltSizeInBits == 0 &&
1737 "Can only handle an even split!");
1738 unsigned Parts = EltVT.getSizeInBits() / ViaEltSizeInBits;
1739
1740 SmallVector<SDValue, 2> ScalarParts;
1741 for (unsigned i = 0; i != Parts; ++i)
1742 ScalarParts.push_back(getConstant(
1743 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1744 ViaEltVT, isT, isO));
1745
1746 return getNode(ISD::SPLAT_VECTOR_PARTS, DL, VT, ScalarParts);
1747 }
1748
1749 unsigned ViaVecNumElts = VT.getSizeInBits() / ViaEltSizeInBits;
1750 EVT ViaVecVT = EVT::getVectorVT(*getContext(), ViaEltVT, ViaVecNumElts);
1751
1752 // Check the temporary vector is the correct size. If this fails then
1753 // getTypeToTransformTo() probably returned a type whose size (in bits)
1754 // isn't a power-of-2 factor of the requested type size.
1755 assert(ViaVecVT.getSizeInBits() == VT.getSizeInBits());
1756
1757 SmallVector<SDValue, 2> EltParts;
1758 for (unsigned i = 0; i < ViaVecNumElts / VT.getVectorNumElements(); ++i)
1759 EltParts.push_back(getConstant(
1760 NewVal.extractBits(ViaEltSizeInBits, i * ViaEltSizeInBits), DL,
1761 ViaEltVT, isT, isO));
1762
1763 // EltParts is currently in little endian order. If we actually want
1764 // big-endian order then reverse it now.
1765 if (getDataLayout().isBigEndian())
1766 std::reverse(EltParts.begin(), EltParts.end());
1767
1768 // The elements must be reversed when the element order is different
1769 // to the endianness of the elements (because the BITCAST is itself a
1770 // vector shuffle in this situation). However, we do not need any code to
1771 // perform this reversal because getConstant() is producing a vector
1772 // splat.
1773 // This situation occurs in MIPS MSA.
1774
1776 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
1777 llvm::append_range(Ops, EltParts);
1778
1779 SDValue V =
1780 getNode(ISD::BITCAST, DL, VT, getBuildVector(ViaVecVT, DL, Ops));
1781 return V;
1782 }
1783
1784 assert(Elt->getBitWidth() == EltVT.getSizeInBits() &&
1785 "APInt size does not match type size!");
1786 unsigned Opc = isT ? ISD::TargetConstant : ISD::Constant;
1787 SDVTList VTs = getVTList(EltVT);
1789 AddNodeIDNode(ID, Opc, VTs, {});
1790 ID.AddPointer(Elt);
1791 ID.AddBoolean(isO);
1792 void *IP = nullptr;
1793 SDNode *N = nullptr;
1794 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1795 if (!VT.isVector())
1796 return SDValue(N, 0);
1797
1798 if (!N) {
1799 N = newSDNode<ConstantSDNode>(isT, isO, Elt, VTs);
1800 CSEMap.InsertNode(N, IP);
1801 InsertNode(N);
1802 NewSDValueDbgMsg(SDValue(N, 0), "Creating constant: ", this);
1803 }
1804
1805 SDValue Result(N, 0);
1806 if (VT.isVector())
1807 Result = getSplat(VT, DL, Result);
1808 return Result;
1809}
1810
1812 bool isT, bool isO) {
1813 unsigned Size = VT.getScalarSizeInBits();
1814 return getConstant(APInt(Size, Val, /*isSigned=*/true), DL, VT, isT, isO);
1815}
1816
1818 bool IsOpaque) {
1820 IsTarget, IsOpaque);
1821}
1822
1824 bool isTarget) {
1825 return getConstant(Val, DL, TLI->getPointerTy(getDataLayout()), isTarget);
1826}
1827
1829 const SDLoc &DL) {
1830 assert(VT.isInteger() && "Shift amount is not an integer type!");
1831 EVT ShiftVT = TLI->getShiftAmountTy(VT, getDataLayout());
1832 return getConstant(Val, DL, ShiftVT);
1833}
1834
1836 const SDLoc &DL) {
1837 assert(Val.ult(VT.getScalarSizeInBits()) && "Out of range shift");
1838 return getShiftAmountConstant(Val.getZExtValue(), VT, DL);
1839}
1840
1842 bool isTarget) {
1843 return getConstant(Val, DL, TLI->getVectorIdxTy(getDataLayout()), isTarget);
1844}
1845
1847 bool isTarget) {
1848 return getConstantFP(*ConstantFP::get(*getContext(), V), DL, VT, isTarget);
1849}
1850
1852 EVT VT, bool isTarget) {
1853 assert(VT.isFloatingPoint() && "Cannot create integer FP constant!");
1854
1855 EVT EltVT = VT.getScalarType();
1856 const ConstantFP *Elt = &V;
1857
1858 // Vector splats are explicit within the DAG, with ConstantFPSDNode holding
1859 // the to-be-splatted scalar ConstantFP.
1860 if (isa<VectorType>(Elt->getType()))
1861 Elt = ConstantFP::get(*getContext(), Elt->getValue());
1862
1863 // Do the map lookup using the actual bit pattern for the floating point
1864 // value, so that we don't have problems with 0.0 comparing equal to -0.0, and
1865 // we don't have issues with SNANs.
1866 unsigned Opc = isTarget ? ISD::TargetConstantFP : ISD::ConstantFP;
1867 SDVTList VTs = getVTList(EltVT);
1869 AddNodeIDNode(ID, Opc, VTs, {});
1870 ID.AddPointer(Elt);
1871 void *IP = nullptr;
1872 SDNode *N = nullptr;
1873 if ((N = FindNodeOrInsertPos(ID, DL, IP)))
1874 if (!VT.isVector())
1875 return SDValue(N, 0);
1876
1877 if (!N) {
1878 N = newSDNode<ConstantFPSDNode>(isTarget, Elt, VTs);
1879 CSEMap.InsertNode(N, IP);
1880 InsertNode(N);
1881 }
1882
1883 SDValue Result(N, 0);
1884 if (VT.isVector())
1885 Result = getSplat(VT, DL, Result);
1886 NewSDValueDbgMsg(Result, "Creating fp constant: ", this);
1887 return Result;
1888}
1889
1891 bool isTarget) {
1892 EVT EltVT = VT.getScalarType();
1893 if (EltVT == MVT::f32)
1894 return getConstantFP(APFloat((float)Val), DL, VT, isTarget);
1895 if (EltVT == MVT::f64)
1896 return getConstantFP(APFloat(Val), DL, VT, isTarget);
1897 if (EltVT == MVT::f80 || EltVT == MVT::f128 || EltVT == MVT::ppcf128 ||
1898 EltVT == MVT::f16 || EltVT == MVT::bf16) {
1899 bool Ignored;
1900 APFloat APF = APFloat(Val);
1902 &Ignored);
1903 return getConstantFP(APF, DL, VT, isTarget);
1904 }
1905 llvm_unreachable("Unsupported type in getConstantFP");
1906}
1907
1909 EVT VT, int64_t Offset, bool isTargetGA,
1910 unsigned TargetFlags) {
1911 assert((TargetFlags == 0 || isTargetGA) &&
1912 "Cannot set target flags on target-independent globals");
1913
1914 // Truncate (with sign-extension) the offset value to the pointer size.
1916 if (BitWidth < 64)
1918
1919 unsigned Opc;
1920 if (GV->isThreadLocal())
1922 else
1924
1925 SDVTList VTs = getVTList(VT);
1927 AddNodeIDNode(ID, Opc, VTs, {});
1928 ID.AddPointer(GV);
1929 ID.AddInteger(Offset);
1930 ID.AddInteger(TargetFlags);
1931 void *IP = nullptr;
1932 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
1933 return SDValue(E, 0);
1934
1935 auto *N = newSDNode<GlobalAddressSDNode>(
1936 Opc, DL.getIROrder(), DL.getDebugLoc(), GV, VTs, Offset, TargetFlags);
1937 CSEMap.InsertNode(N, IP);
1938 InsertNode(N);
1939 return SDValue(N, 0);
1940}
1941
1943 SDVTList VTs = getVTList(MVT::Untyped);
1946 ID.AddPointer(GV);
1947 void *IP = nullptr;
1948 if (SDNode *E = FindNodeOrInsertPos(ID, SDLoc(), IP))
1949 return SDValue(E, 0);
1950
1951 auto *N = newSDNode<DeactivationSymbolSDNode>(GV, VTs);
1952 CSEMap.InsertNode(N, IP);
1953 InsertNode(N);
1954 return SDValue(N, 0);
1955}
1956
1957SDValue SelectionDAG::getFrameIndex(int FI, EVT VT, bool isTarget) {
1958 unsigned Opc = isTarget ? ISD::TargetFrameIndex : ISD::FrameIndex;
1959 SDVTList VTs = getVTList(VT);
1961 AddNodeIDNode(ID, Opc, VTs, {});
1962 ID.AddInteger(FI);
1963 void *IP = nullptr;
1964 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1965 return SDValue(E, 0);
1966
1967 auto *N = newSDNode<FrameIndexSDNode>(FI, VTs, isTarget);
1968 CSEMap.InsertNode(N, IP);
1969 InsertNode(N);
1970 return SDValue(N, 0);
1971}
1972
1973SDValue SelectionDAG::getJumpTable(int JTI, EVT VT, bool isTarget,
1974 unsigned TargetFlags) {
1975 assert((TargetFlags == 0 || isTarget) &&
1976 "Cannot set target flags on target-independent jump tables");
1977 unsigned Opc = isTarget ? ISD::TargetJumpTable : ISD::JumpTable;
1978 SDVTList VTs = getVTList(VT);
1980 AddNodeIDNode(ID, Opc, VTs, {});
1981 ID.AddInteger(JTI);
1982 ID.AddInteger(TargetFlags);
1983 void *IP = nullptr;
1984 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
1985 return SDValue(E, 0);
1986
1987 auto *N = newSDNode<JumpTableSDNode>(JTI, VTs, isTarget, TargetFlags);
1988 CSEMap.InsertNode(N, IP);
1989 InsertNode(N);
1990 return SDValue(N, 0);
1991}
1992
1994 const SDLoc &DL) {
1996 return getNode(ISD::JUMP_TABLE_DEBUG_INFO, DL, MVT::Other, Chain,
1997 getTargetConstant(static_cast<uint64_t>(JTI), DL, PTy, true));
1998}
1999
2001 MaybeAlign Alignment, int Offset,
2002 bool isTarget, unsigned TargetFlags) {
2003 assert((TargetFlags == 0 || isTarget) &&
2004 "Cannot set target flags on target-independent globals");
2005 if (!Alignment)
2006 Alignment = shouldOptForSize()
2007 ? getDataLayout().getABITypeAlign(C->getType())
2008 : getDataLayout().getPrefTypeAlign(C->getType());
2009 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2010 SDVTList VTs = getVTList(VT);
2012 AddNodeIDNode(ID, Opc, VTs, {});
2013 ID.AddInteger(Alignment->value());
2014 ID.AddInteger(Offset);
2015 ID.AddPointer(C);
2016 ID.AddInteger(TargetFlags);
2017 void *IP = nullptr;
2018 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2019 return SDValue(E, 0);
2020
2021 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2022 TargetFlags);
2023 CSEMap.InsertNode(N, IP);
2024 InsertNode(N);
2025 SDValue V = SDValue(N, 0);
2026 NewSDValueDbgMsg(V, "Creating new constant pool: ", this);
2027 return V;
2028}
2029
2031 MaybeAlign Alignment, int Offset,
2032 bool isTarget, unsigned TargetFlags) {
2033 assert((TargetFlags == 0 || isTarget) &&
2034 "Cannot set target flags on target-independent globals");
2035 if (!Alignment)
2036 Alignment = getDataLayout().getPrefTypeAlign(C->getType());
2037 unsigned Opc = isTarget ? ISD::TargetConstantPool : ISD::ConstantPool;
2038 SDVTList VTs = getVTList(VT);
2040 AddNodeIDNode(ID, Opc, VTs, {});
2041 ID.AddInteger(Alignment->value());
2042 ID.AddInteger(Offset);
2043 C->addSelectionDAGCSEId(ID);
2044 ID.AddInteger(TargetFlags);
2045 void *IP = nullptr;
2046 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2047 return SDValue(E, 0);
2048
2049 auto *N = newSDNode<ConstantPoolSDNode>(isTarget, C, VTs, Offset, *Alignment,
2050 TargetFlags);
2051 CSEMap.InsertNode(N, IP);
2052 InsertNode(N);
2053 return SDValue(N, 0);
2054}
2055
2058 AddNodeIDNode(ID, ISD::BasicBlock, getVTList(MVT::Other), {});
2059 ID.AddPointer(MBB);
2060 void *IP = nullptr;
2061 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2062 return SDValue(E, 0);
2063
2064 auto *N = newSDNode<BasicBlockSDNode>(MBB);
2065 CSEMap.InsertNode(N, IP);
2066 InsertNode(N);
2067 return SDValue(N, 0);
2068}
2069
2071 if (VT.isSimple() && (unsigned)VT.getSimpleVT().SimpleTy >=
2072 ValueTypeNodes.size())
2073 ValueTypeNodes.resize(VT.getSimpleVT().SimpleTy+1);
2074
2075 SDNode *&N = VT.isExtended() ?
2076 ExtendedValueTypeNodes[VT] : ValueTypeNodes[VT.getSimpleVT().SimpleTy];
2077
2078 if (N) return SDValue(N, 0);
2079 N = newSDNode<VTSDNode>(VT);
2080 InsertNode(N);
2081 return SDValue(N, 0);
2082}
2083
2085 SDNode *&N = ExternalSymbols[Sym];
2086 if (N) return SDValue(N, 0);
2087 N = newSDNode<ExternalSymbolSDNode>(false, Sym, 0, getVTList(VT));
2088 InsertNode(N);
2089 return SDValue(N, 0);
2090}
2091
2092SDValue SelectionDAG::getExternalSymbol(RTLIB::LibcallImpl Libcall, EVT VT) {
2094 return getExternalSymbol(SymName.data(), VT);
2095}
2096
2098 SDNode *&N = MCSymbols[Sym];
2099 if (N)
2100 return SDValue(N, 0);
2101 N = newSDNode<MCSymbolSDNode>(Sym, getVTList(VT));
2102 InsertNode(N);
2103 return SDValue(N, 0);
2104}
2105
2107 unsigned TargetFlags) {
2108 SDNode *&N =
2109 TargetExternalSymbols[std::pair<std::string, unsigned>(Sym, TargetFlags)];
2110 if (N) return SDValue(N, 0);
2111 N = newSDNode<ExternalSymbolSDNode>(true, Sym, TargetFlags, getVTList(VT));
2112 InsertNode(N);
2113 return SDValue(N, 0);
2114}
2115
2117 EVT VT, unsigned TargetFlags) {
2119 return getTargetExternalSymbol(SymName.data(), VT, TargetFlags);
2120}
2121
2123 if ((unsigned)Cond >= CondCodeNodes.size())
2124 CondCodeNodes.resize(Cond+1);
2125
2126 if (!CondCodeNodes[Cond]) {
2127 auto *N = newSDNode<CondCodeSDNode>(Cond);
2128 CondCodeNodes[Cond] = N;
2129 InsertNode(N);
2130 }
2131
2132 return SDValue(CondCodeNodes[Cond], 0);
2133}
2134
2136 assert(MulImm.getBitWidth() == VT.getSizeInBits() &&
2137 "APInt size does not match type size!");
2138
2139 if (MulImm == 0)
2140 return getConstant(0, DL, VT);
2141
2142 const MachineFunction &MF = getMachineFunction();
2143 const Function &F = MF.getFunction();
2144 ConstantRange CR = getVScaleRange(&F, 64);
2145 if (const APInt *C = CR.getSingleElement())
2146 return getConstant(MulImm * C->getZExtValue(), DL, VT);
2147
2148 return getNode(ISD::VSCALE, DL, VT, getConstant(MulImm, DL, VT));
2149}
2150
2151/// \returns a value of type \p VT that represents the runtime value of \p
2152/// Quantity, i.e. scaled by vscale if it's scalable, or a fixed constant
2153/// otherwise. Quantity should be a FixedOrScalableQuantity, i.e. ElementCount
2154/// or TypeSize.
2155template <typename Ty>
2157 EVT VT, Ty Quantity) {
2158 if (Quantity.isScalable())
2159 return DAG.getVScale(
2160 DL, VT, APInt(VT.getSizeInBits(), Quantity.getKnownMinValue()));
2161
2162 return DAG.getConstant(Quantity.getKnownMinValue(), DL, VT);
2163}
2164
2166 ElementCount EC) {
2167 return getFixedOrScalableQuantity(*this, DL, VT, EC);
2168}
2169
2171 return getFixedOrScalableQuantity(*this, DL, VT, TS);
2172}
2173
2175 ElementCount EC) {
2176 EVT IdxVT = TLI->getVectorIdxTy(getDataLayout());
2177 EVT MaskVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), DataVT);
2178 return getNode(ISD::GET_ACTIVE_LANE_MASK, DL, MaskVT,
2179 getConstant(0, DL, IdxVT), getElementCount(DL, IdxVT, EC));
2180}
2181
2183 APInt One(ResVT.getScalarSizeInBits(), 1);
2184 return getStepVector(DL, ResVT, One);
2185}
2186
2188 const APInt &StepVal) {
2189 assert(ResVT.getScalarSizeInBits() == StepVal.getBitWidth());
2190 if (ResVT.isScalableVector())
2191 return getNode(
2192 ISD::STEP_VECTOR, DL, ResVT,
2193 getTargetConstant(StepVal, DL, ResVT.getVectorElementType()));
2194
2195 SmallVector<SDValue, 16> OpsStepConstants;
2196 for (uint64_t i = 0; i < ResVT.getVectorNumElements(); i++)
2197 OpsStepConstants.push_back(
2198 getConstant(StepVal * i, DL, ResVT.getVectorElementType()));
2199 return getBuildVector(ResVT, DL, OpsStepConstants);
2200}
2201
2202/// Swaps the values of N1 and N2. Swaps all indices in the shuffle mask M that
2203/// point at N1 to point at N2 and indices that point at N2 to point at N1.
2208
2210 SDValue N2, ArrayRef<int> Mask) {
2211 assert(VT.getVectorNumElements() == Mask.size() &&
2212 "Must have the same number of vector elements as mask elements!");
2213 assert(VT == N1.getValueType() && VT == N2.getValueType() &&
2214 "Invalid VECTOR_SHUFFLE");
2215
2216 // Canonicalize shuffle undef, undef -> undef
2217 if (N1.isUndef() && N2.isUndef())
2218 return getUNDEF(VT);
2219
2220 // Validate that all indices in Mask are within the range of the elements
2221 // input to the shuffle.
2222 int NElts = Mask.size();
2223 assert(llvm::all_of(Mask,
2224 [&](int M) { return M < (NElts * 2) && M >= -1; }) &&
2225 "Index out of range");
2226
2227 // Copy the mask so we can do any needed cleanup.
2228 SmallVector<int, 8> MaskVec(Mask);
2229
2230 // Canonicalize shuffle v, v -> v, undef
2231 if (N1 == N2) {
2232 N2 = getUNDEF(VT);
2233 for (int i = 0; i != NElts; ++i)
2234 if (MaskVec[i] >= NElts) MaskVec[i] -= NElts;
2235 }
2236
2237 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
2238 if (N1.isUndef())
2239 commuteShuffle(N1, N2, MaskVec);
2240
2241 if (TLI->hasVectorBlend()) {
2242 // If shuffling a splat, try to blend the splat instead. We do this here so
2243 // that even when this arises during lowering we don't have to re-handle it.
2244 auto BlendSplat = [&](BuildVectorSDNode *BV, int Offset) {
2245 BitVector UndefElements;
2246 SDValue Splat = BV->getSplatValue(&UndefElements);
2247 if (!Splat)
2248 return;
2249
2250 for (int i = 0; i < NElts; ++i) {
2251 if (MaskVec[i] < Offset || MaskVec[i] >= (Offset + NElts))
2252 continue;
2253
2254 // If this input comes from undef, mark it as such.
2255 if (UndefElements[MaskVec[i] - Offset]) {
2256 MaskVec[i] = -1;
2257 continue;
2258 }
2259
2260 // If we can blend a non-undef lane, use that instead.
2261 if (!UndefElements[i])
2262 MaskVec[i] = i + Offset;
2263 }
2264 };
2265 if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
2266 BlendSplat(N1BV, 0);
2267 if (auto *N2BV = dyn_cast<BuildVectorSDNode>(N2))
2268 BlendSplat(N2BV, NElts);
2269 }
2270
2271 // Canonicalize all index into lhs, -> shuffle lhs, undef
2272 // Canonicalize all index into rhs, -> shuffle rhs, undef
2273 bool AllLHS = true, AllRHS = true;
2274 bool N2Undef = N2.isUndef();
2275 for (int i = 0; i != NElts; ++i) {
2276 if (MaskVec[i] >= NElts) {
2277 if (N2Undef)
2278 MaskVec[i] = -1;
2279 else
2280 AllLHS = false;
2281 } else if (MaskVec[i] >= 0) {
2282 AllRHS = false;
2283 }
2284 }
2285 if (AllLHS && AllRHS)
2286 return getUNDEF(VT);
2287 if (AllLHS && !N2Undef)
2288 N2 = getUNDEF(VT);
2289 if (AllRHS) {
2290 N1 = getUNDEF(VT);
2291 commuteShuffle(N1, N2, MaskVec);
2292 }
2293 // Reset our undef status after accounting for the mask.
2294 N2Undef = N2.isUndef();
2295 // Re-check whether both sides ended up undef.
2296 if (N1.isUndef() && N2Undef)
2297 return getUNDEF(VT);
2298
2299 // If Identity shuffle return that node.
2300 bool Identity = true, AllSame = true;
2301 for (int i = 0; i != NElts; ++i) {
2302 if (MaskVec[i] >= 0 && MaskVec[i] != i) Identity = false;
2303 if (MaskVec[i] != MaskVec[0]) AllSame = false;
2304 }
2305 if (Identity && NElts)
2306 return N1;
2307
2308 // Shuffling a constant splat doesn't change the result.
2309 if (N2Undef) {
2310 SDValue V = N1;
2311
2312 // Look through any bitcasts. We check that these don't change the number
2313 // (and size) of elements and just changes their types.
2314 while (V.getOpcode() == ISD::BITCAST)
2315 V = V->getOperand(0);
2316
2317 // A splat should always show up as a build vector node.
2318 if (auto *BV = dyn_cast<BuildVectorSDNode>(V)) {
2319 BitVector UndefElements;
2320 SDValue Splat = BV->getSplatValue(&UndefElements);
2321 // If this is a splat of an undef, shuffling it is also undef.
2322 if (Splat && Splat.isUndef())
2323 return getUNDEF(VT);
2324
2325 bool SameNumElts =
2326 V.getValueType().getVectorNumElements() == VT.getVectorNumElements();
2327
2328 // We only have a splat which can skip shuffles if there is a splatted
2329 // value and no undef lanes rearranged by the shuffle.
2330 if (Splat && UndefElements.none()) {
2331 // Splat of <x, x, ..., x>, return <x, x, ..., x>, provided that the
2332 // number of elements match or the value splatted is a zero constant.
2333 if (SameNumElts || isNullConstant(Splat))
2334 return N1;
2335 }
2336
2337 // If the shuffle itself creates a splat, build the vector directly.
2338 if (AllSame && SameNumElts) {
2339 EVT BuildVT = BV->getValueType(0);
2340 const SDValue &Splatted = BV->getOperand(MaskVec[0]);
2341 SDValue NewBV = getSplatBuildVector(BuildVT, dl, Splatted);
2342
2343 // We may have jumped through bitcasts, so the type of the
2344 // BUILD_VECTOR may not match the type of the shuffle.
2345 if (BuildVT != VT)
2346 NewBV = getNode(ISD::BITCAST, dl, VT, NewBV);
2347 return NewBV;
2348 }
2349 }
2350 }
2351
2352 SDVTList VTs = getVTList(VT);
2354 SDValue Ops[2] = { N1, N2 };
2356 for (int i = 0; i != NElts; ++i)
2357 ID.AddInteger(MaskVec[i]);
2358
2359 void* IP = nullptr;
2360 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2361 return SDValue(E, 0);
2362
2363 // Allocate the mask array for the node out of the BumpPtrAllocator, since
2364 // SDNode doesn't have access to it. This memory will be "leaked" when
2365 // the node is deallocated, but recovered when the NodeAllocator is released.
2366 int *MaskAlloc = OperandAllocator.Allocate<int>(NElts);
2367 llvm::copy(MaskVec, MaskAlloc);
2368
2369 auto *N = newSDNode<ShuffleVectorSDNode>(VTs, dl.getIROrder(),
2370 dl.getDebugLoc(), MaskAlloc);
2371 createOperands(N, Ops);
2372
2373 CSEMap.InsertNode(N, IP);
2374 InsertNode(N);
2375 SDValue V = SDValue(N, 0);
2376 NewSDValueDbgMsg(V, "Creating new node: ", this);
2377 return V;
2378}
2379
2381 EVT VT = SV.getValueType(0);
2382 SmallVector<int, 8> MaskVec(SV.getMask());
2384
2385 SDValue Op0 = SV.getOperand(0);
2386 SDValue Op1 = SV.getOperand(1);
2387 return getVectorShuffle(VT, SDLoc(&SV), Op1, Op0, MaskVec);
2388}
2389
2391 SDVTList VTs = getVTList(VT);
2393 AddNodeIDNode(ID, ISD::Register, VTs, {});
2394 ID.AddInteger(Reg.id());
2395 void *IP = nullptr;
2396 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2397 return SDValue(E, 0);
2398
2399 auto *N = newSDNode<RegisterSDNode>(Reg, VTs);
2400 N->SDNodeBits.IsDivergent = TLI->isSDNodeSourceOfDivergence(N, FLI, UA);
2401 CSEMap.InsertNode(N, IP);
2402 InsertNode(N);
2403 return SDValue(N, 0);
2404}
2405
2408 AddNodeIDNode(ID, ISD::RegisterMask, getVTList(MVT::Untyped), {});
2409 ID.AddPointer(RegMask);
2410 void *IP = nullptr;
2411 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2412 return SDValue(E, 0);
2413
2414 auto *N = newSDNode<RegisterMaskSDNode>(RegMask);
2415 CSEMap.InsertNode(N, IP);
2416 InsertNode(N);
2417 return SDValue(N, 0);
2418}
2419
2421 MCSymbol *Label) {
2422 return getLabelNode(ISD::EH_LABEL, dl, Root, Label);
2423}
2424
2425SDValue SelectionDAG::getLabelNode(unsigned Opcode, const SDLoc &dl,
2426 SDValue Root, MCSymbol *Label) {
2428 SDValue Ops[] = { Root };
2429 AddNodeIDNode(ID, Opcode, getVTList(MVT::Other), Ops);
2430 ID.AddPointer(Label);
2431 void *IP = nullptr;
2432 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2433 return SDValue(E, 0);
2434
2435 auto *N =
2436 newSDNode<LabelSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), Label);
2437 createOperands(N, Ops);
2438
2439 CSEMap.InsertNode(N, IP);
2440 InsertNode(N);
2441 return SDValue(N, 0);
2442}
2443
2445 int64_t Offset, bool isTarget,
2446 unsigned TargetFlags) {
2447 unsigned Opc = isTarget ? ISD::TargetBlockAddress : ISD::BlockAddress;
2448 SDVTList VTs = getVTList(VT);
2449
2451 AddNodeIDNode(ID, Opc, VTs, {});
2452 ID.AddPointer(BA);
2453 ID.AddInteger(Offset);
2454 ID.AddInteger(TargetFlags);
2455 void *IP = nullptr;
2456 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2457 return SDValue(E, 0);
2458
2459 auto *N = newSDNode<BlockAddressSDNode>(Opc, VTs, BA, Offset, TargetFlags);
2460 CSEMap.InsertNode(N, IP);
2461 InsertNode(N);
2462 return SDValue(N, 0);
2463}
2464
2467 AddNodeIDNode(ID, ISD::SRCVALUE, getVTList(MVT::Other), {});
2468 ID.AddPointer(V);
2469
2470 void *IP = nullptr;
2471 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2472 return SDValue(E, 0);
2473
2474 auto *N = newSDNode<SrcValueSDNode>(V);
2475 CSEMap.InsertNode(N, IP);
2476 InsertNode(N);
2477 return SDValue(N, 0);
2478}
2479
2482 AddNodeIDNode(ID, ISD::MDNODE_SDNODE, getVTList(MVT::Other), {});
2483 ID.AddPointer(MD);
2484
2485 void *IP = nullptr;
2486 if (SDNode *E = FindNodeOrInsertPos(ID, IP))
2487 return SDValue(E, 0);
2488
2489 auto *N = newSDNode<MDNodeSDNode>(MD);
2490 CSEMap.InsertNode(N, IP);
2491 InsertNode(N);
2492 return SDValue(N, 0);
2493}
2494
2496 if (VT == V.getValueType())
2497 return V;
2498
2499 return getNode(ISD::BITCAST, SDLoc(V), VT, V);
2500}
2501
2503 unsigned SrcAS, unsigned DestAS) {
2504 SDVTList VTs = getVTList(VT);
2505 SDValue Ops[] = {Ptr};
2508 ID.AddInteger(SrcAS);
2509 ID.AddInteger(DestAS);
2510
2511 void *IP = nullptr;
2512 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
2513 return SDValue(E, 0);
2514
2515 auto *N = newSDNode<AddrSpaceCastSDNode>(dl.getIROrder(), dl.getDebugLoc(),
2516 VTs, SrcAS, DestAS);
2517 createOperands(N, Ops);
2518
2519 CSEMap.InsertNode(N, IP);
2520 InsertNode(N);
2521 return SDValue(N, 0);
2522}
2523
2525 return getNode(ISD::FREEZE, SDLoc(V), V.getValueType(), V);
2526}
2527
2528/// getShiftAmountOperand - Return the specified value casted to
2529/// the target's desired shift amount type.
2531 EVT OpTy = Op.getValueType();
2532 EVT ShTy = TLI->getShiftAmountTy(LHSTy, getDataLayout());
2533 if (OpTy == ShTy || OpTy.isVector()) return Op;
2534
2535 return getZExtOrTrunc(Op, SDLoc(Op), ShTy);
2536}
2537
2539 SDLoc dl(Node);
2541 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2542 EVT VT = Node->getValueType(0);
2543 SDValue Tmp1 = Node->getOperand(0);
2544 SDValue Tmp2 = Node->getOperand(1);
2545 const MaybeAlign MA(Node->getConstantOperandVal(3));
2546
2547 SDValue VAListLoad = getLoad(TLI.getPointerTy(getDataLayout()), dl, Tmp1,
2548 Tmp2, MachinePointerInfo(V));
2549 SDValue VAList = VAListLoad;
2550
2551 if (MA && *MA > TLI.getMinStackArgumentAlignment()) {
2552 VAList = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2553 getConstant(MA->value() - 1, dl, VAList.getValueType()));
2554
2555 VAList = getNode(
2556 ISD::AND, dl, VAList.getValueType(), VAList,
2557 getSignedConstant(-(int64_t)MA->value(), dl, VAList.getValueType()));
2558 }
2559
2560 // Increment the pointer, VAList, to the next vaarg
2561 Tmp1 = getNode(ISD::ADD, dl, VAList.getValueType(), VAList,
2562 getConstant(getDataLayout().getTypeAllocSize(
2563 VT.getTypeForEVT(*getContext())),
2564 dl, VAList.getValueType()));
2565 // Store the incremented VAList to the legalized pointer
2566 Tmp1 =
2567 getStore(VAListLoad.getValue(1), dl, Tmp1, Tmp2, MachinePointerInfo(V));
2568 // Load the actual argument out of the pointer VAList
2569 return getLoad(VT, dl, Tmp1, VAList, MachinePointerInfo());
2570}
2571
2573 SDLoc dl(Node);
2575 // This defaults to loading a pointer from the input and storing it to the
2576 // output, returning the chain.
2577 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
2578 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
2579 SDValue Tmp1 =
2580 getLoad(TLI.getPointerTy(getDataLayout()), dl, Node->getOperand(0),
2581 Node->getOperand(2), MachinePointerInfo(VS));
2582 return getStore(Tmp1.getValue(1), dl, Tmp1, Node->getOperand(1),
2583 MachinePointerInfo(VD));
2584}
2585
2587 const DataLayout &DL = getDataLayout();
2588 Type *Ty = VT.getTypeForEVT(*getContext());
2589 Align RedAlign = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2590
2591 if (TLI->isTypeLegal(VT) || !VT.isVector())
2592 return RedAlign;
2593
2594 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2595 const Align StackAlign = TFI->getStackAlign();
2596
2597 // See if we can choose a smaller ABI alignment in cases where it's an
2598 // illegal vector type that will get broken down.
2599 if (RedAlign > StackAlign) {
2600 EVT IntermediateVT;
2601 MVT RegisterVT;
2602 unsigned NumIntermediates;
2603 TLI->getVectorTypeBreakdown(*getContext(), VT, IntermediateVT,
2604 NumIntermediates, RegisterVT);
2605 Ty = IntermediateVT.getTypeForEVT(*getContext());
2606 Align RedAlign2 = UseABI ? DL.getABITypeAlign(Ty) : DL.getPrefTypeAlign(Ty);
2607 if (RedAlign2 < RedAlign)
2608 RedAlign = RedAlign2;
2609
2610 if (!getMachineFunction().getFrameInfo().isStackRealignable())
2611 // If the stack is not realignable, the alignment should be limited to the
2612 // StackAlignment
2613 RedAlign = std::min(RedAlign, StackAlign);
2614 }
2615
2616 return RedAlign;
2617}
2618
2620 MachineFrameInfo &MFI = MF->getFrameInfo();
2621 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
2622 int StackID = 0;
2623 if (Bytes.isScalable())
2624 StackID = TFI->getStackIDForScalableVectors();
2625 // The stack id gives an indication of whether the object is scalable or
2626 // not, so it's safe to pass in the minimum size here.
2627 int FrameIdx = MFI.CreateStackObject(Bytes.getKnownMinValue(), Alignment,
2628 false, nullptr, StackID);
2629 return getFrameIndex(FrameIdx, TLI->getFrameIndexTy(getDataLayout()));
2630}
2631
2633 Type *Ty = VT.getTypeForEVT(*getContext());
2634 Align StackAlign =
2635 std::max(getDataLayout().getPrefTypeAlign(Ty), Align(minAlign));
2636 return CreateStackTemporary(VT.getStoreSize(), StackAlign);
2637}
2638
2640 TypeSize VT1Size = VT1.getStoreSize();
2641 TypeSize VT2Size = VT2.getStoreSize();
2642 assert(VT1Size.isScalable() == VT2Size.isScalable() &&
2643 "Don't know how to choose the maximum size when creating a stack "
2644 "temporary");
2645 TypeSize Bytes = VT1Size.getKnownMinValue() > VT2Size.getKnownMinValue()
2646 ? VT1Size
2647 : VT2Size;
2648
2649 Type *Ty1 = VT1.getTypeForEVT(*getContext());
2650 Type *Ty2 = VT2.getTypeForEVT(*getContext());
2651 const DataLayout &DL = getDataLayout();
2652 Align Align = std::max(DL.getPrefTypeAlign(Ty1), DL.getPrefTypeAlign(Ty2));
2653 return CreateStackTemporary(Bytes, Align);
2654}
2655
2657 ISD::CondCode Cond, const SDLoc &dl) {
2658 EVT OpVT = N1.getValueType();
2659
2660 auto GetUndefBooleanConstant = [&]() {
2661 if (VT.getScalarType() == MVT::i1 ||
2662 TLI->getBooleanContents(OpVT) ==
2664 return getUNDEF(VT);
2665 // ZeroOrOne / ZeroOrNegative require specific values for the high bits,
2666 // so we cannot use getUNDEF(). Return zero instead.
2667 return getConstant(0, dl, VT);
2668 };
2669
2670 // These setcc operations always fold.
2671 switch (Cond) {
2672 default: break;
2673 case ISD::SETFALSE:
2674 case ISD::SETFALSE2: return getBoolConstant(false, dl, VT, OpVT);
2675 case ISD::SETTRUE:
2676 case ISD::SETTRUE2: return getBoolConstant(true, dl, VT, OpVT);
2677
2678 case ISD::SETOEQ:
2679 case ISD::SETOGT:
2680 case ISD::SETOGE:
2681 case ISD::SETOLT:
2682 case ISD::SETOLE:
2683 case ISD::SETONE:
2684 case ISD::SETO:
2685 case ISD::SETUO:
2686 case ISD::SETUEQ:
2687 case ISD::SETUNE:
2688 assert(!OpVT.isInteger() && "Illegal setcc for integer!");
2689 break;
2690 }
2691
2692 if (OpVT.isInteger()) {
2693 // For EQ and NE, we can always pick a value for the undef to make the
2694 // predicate pass or fail, so we can return undef.
2695 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2696 // icmp eq/ne X, undef -> undef.
2697 if ((N1.isUndef() || N2.isUndef()) &&
2698 (Cond == ISD::SETEQ || Cond == ISD::SETNE))
2699 return GetUndefBooleanConstant();
2700
2701 // If both operands are undef, we can return undef for int comparison.
2702 // icmp undef, undef -> undef.
2703 if (N1.isUndef() && N2.isUndef())
2704 return GetUndefBooleanConstant();
2705
2706 // icmp X, X -> true/false
2707 // icmp X, undef -> true/false because undef could be X.
2708 if (N1.isUndef() || N2.isUndef() || N1 == N2)
2709 return getBoolConstant(ISD::isTrueWhenEqual(Cond), dl, VT, OpVT);
2710 }
2711
2713 const APInt &C2 = N2C->getAPIntValue();
2715 const APInt &C1 = N1C->getAPIntValue();
2716
2718 dl, VT, OpVT);
2719 }
2720 }
2721
2722 auto *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
2723 auto *N2CFP = dyn_cast<ConstantFPSDNode>(N2);
2724
2725 if (N1CFP && N2CFP) {
2726 APFloat::cmpResult R = N1CFP->getValueAPF().compare(N2CFP->getValueAPF());
2727 switch (Cond) {
2728 default: break;
2729 case ISD::SETEQ: if (R==APFloat::cmpUnordered)
2730 return GetUndefBooleanConstant();
2731 [[fallthrough]];
2732 case ISD::SETOEQ: return getBoolConstant(R==APFloat::cmpEqual, dl, VT,
2733 OpVT);
2734 case ISD::SETNE: if (R==APFloat::cmpUnordered)
2735 return GetUndefBooleanConstant();
2736 [[fallthrough]];
2738 R==APFloat::cmpLessThan, dl, VT,
2739 OpVT);
2740 case ISD::SETLT: if (R==APFloat::cmpUnordered)
2741 return GetUndefBooleanConstant();
2742 [[fallthrough]];
2743 case ISD::SETOLT: return getBoolConstant(R==APFloat::cmpLessThan, dl, VT,
2744 OpVT);
2745 case ISD::SETGT: if (R==APFloat::cmpUnordered)
2746 return GetUndefBooleanConstant();
2747 [[fallthrough]];
2749 VT, OpVT);
2750 case ISD::SETLE: if (R==APFloat::cmpUnordered)
2751 return GetUndefBooleanConstant();
2752 [[fallthrough]];
2754 R==APFloat::cmpEqual, dl, VT,
2755 OpVT);
2756 case ISD::SETGE: if (R==APFloat::cmpUnordered)
2757 return GetUndefBooleanConstant();
2758 [[fallthrough]];
2760 R==APFloat::cmpEqual, dl, VT, OpVT);
2761 case ISD::SETO: return getBoolConstant(R!=APFloat::cmpUnordered, dl, VT,
2762 OpVT);
2763 case ISD::SETUO: return getBoolConstant(R==APFloat::cmpUnordered, dl, VT,
2764 OpVT);
2766 R==APFloat::cmpEqual, dl, VT,
2767 OpVT);
2768 case ISD::SETUNE: return getBoolConstant(R!=APFloat::cmpEqual, dl, VT,
2769 OpVT);
2771 R==APFloat::cmpLessThan, dl, VT,
2772 OpVT);
2774 R==APFloat::cmpUnordered, dl, VT,
2775 OpVT);
2777 VT, OpVT);
2778 case ISD::SETUGE: return getBoolConstant(R!=APFloat::cmpLessThan, dl, VT,
2779 OpVT);
2780 }
2781 } else if (N1CFP && OpVT.isSimple() && !N2.isUndef()) {
2782 // Ensure that the constant occurs on the RHS.
2784 if (!TLI->isCondCodeLegal(SwappedCond, OpVT.getSimpleVT()))
2785 return SDValue();
2786 return getSetCC(dl, VT, N2, N1, SwappedCond);
2787 } else if ((N2CFP && N2CFP->getValueAPF().isNaN()) ||
2788 (OpVT.isFloatingPoint() && (N1.isUndef() || N2.isUndef()))) {
2789 // If an operand is known to be a nan (or undef that could be a nan), we can
2790 // fold it.
2791 // Choosing NaN for the undef will always make unordered comparison succeed
2792 // and ordered comparison fails.
2793 // Matches behavior in llvm::ConstantFoldCompareInstruction.
2794 switch (ISD::getUnorderedFlavor(Cond)) {
2795 default:
2796 llvm_unreachable("Unknown flavor!");
2797 case 0: // Known false.
2798 return getBoolConstant(false, dl, VT, OpVT);
2799 case 1: // Known true.
2800 return getBoolConstant(true, dl, VT, OpVT);
2801 case 2: // Undefined.
2802 return GetUndefBooleanConstant();
2803 }
2804 }
2805
2806 // Could not fold it.
2807 return SDValue();
2808}
2809
2810/// SignBitIsZero - Return true if the sign bit of Op is known to be zero. We
2811/// use this predicate to simplify operations downstream.
2813 unsigned BitWidth = Op.getScalarValueSizeInBits();
2815}
2816
2817// TODO: Should have argument to specify if sign bit of nan is ignorable.
2819 if (Depth >= MaxRecursionDepth)
2820 return false; // Limit search depth.
2821
2822 unsigned Opc = Op.getOpcode();
2823 switch (Opc) {
2824 case ISD::FABS:
2825 return true;
2826 case ISD::AssertNoFPClass: {
2827 FPClassTest NoFPClass =
2828 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
2829
2830 const FPClassTest TestMask = fcNan | fcNegative;
2831 return (NoFPClass & TestMask) == TestMask;
2832 }
2833 case ISD::ARITH_FENCE:
2834 return SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2835 case ISD::FEXP:
2836 case ISD::FEXP2:
2837 case ISD::FEXP10:
2838 return Op->getFlags().hasNoNaNs();
2839 case ISD::FMINNUM:
2840 case ISD::FMINNUM_IEEE:
2841 case ISD::FMINIMUM:
2842 case ISD::FMINIMUMNUM:
2843 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2844 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2845 case ISD::FMAXNUM:
2846 case ISD::FMAXNUM_IEEE:
2847 case ISD::FMAXIMUM:
2848 case ISD::FMAXIMUMNUM:
2849 // TODO: If we can ignore the sign bit of nans, only one side being known 0
2850 // is sufficient.
2851 return SignBitIsZeroFP(Op.getOperand(1), Depth + 1) &&
2852 SignBitIsZeroFP(Op.getOperand(0), Depth + 1);
2853 default:
2854 return false;
2855 }
2856
2857 llvm_unreachable("covered opcode switch");
2858}
2859
2860/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero. We use
2861/// this predicate to simplify operations downstream. Mask is known to be zero
2862/// for bits that V cannot have.
2864 unsigned Depth) const {
2865 return Mask.isSubsetOf(computeKnownBits(V, Depth).Zero);
2866}
2867
2868/// MaskedValueIsZero - Return true if 'V & Mask' is known to be zero in
2869/// DemandedElts. We use this predicate to simplify operations downstream.
2870/// Mask is known to be zero for bits that V cannot have.
2872 const APInt &DemandedElts,
2873 unsigned Depth) const {
2874 return Mask.isSubsetOf(computeKnownBits(V, DemandedElts, Depth).Zero);
2875}
2876
2877/// MaskedVectorIsZero - Return true if 'Op' is known to be zero in
2878/// DemandedElts. We use this predicate to simplify operations downstream.
2880 unsigned Depth /* = 0 */) const {
2881 return computeKnownBits(V, DemandedElts, Depth).isZero();
2882}
2883
2884/// MaskedValueIsAllOnes - Return true if '(Op & Mask) == Mask'.
2886 unsigned Depth) const {
2887 return Mask.isSubsetOf(computeKnownBits(V, Depth).One);
2888}
2889
2891 const APInt &DemandedElts,
2892 unsigned Depth) const {
2893 EVT VT = Op.getValueType();
2894 assert(VT.isVector() && !VT.isScalableVector() && "Only for fixed vectors!");
2895
2896 unsigned NumElts = VT.getVectorNumElements();
2897 assert(DemandedElts.getBitWidth() == NumElts && "Unexpected demanded mask.");
2898
2899 APInt KnownZeroElements = APInt::getZero(NumElts);
2900 for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) {
2901 if (!DemandedElts[EltIdx])
2902 continue; // Don't query elements that are not demanded.
2903 APInt Mask = APInt::getOneBitSet(NumElts, EltIdx);
2904 if (MaskedVectorIsZero(Op, Mask, Depth))
2905 KnownZeroElements.setBit(EltIdx);
2906 }
2907 return KnownZeroElements;
2908}
2909
2910/// isSplatValue - Return true if the vector V has the same value
2911/// across all DemandedElts. For scalable vectors, we don't know the
2912/// number of lanes at compile time. Instead, we use a 1 bit APInt
2913/// to represent a conservative value for all lanes; that is, that
2914/// one bit value is implicitly splatted across all lanes.
2915bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
2916 APInt &UndefElts, unsigned Depth) const {
2917 unsigned Opcode = V.getOpcode();
2918 EVT VT = V.getValueType();
2919 assert(VT.isVector() && "Vector type expected");
2920 assert((!VT.isScalableVector() || DemandedElts.getBitWidth() == 1) &&
2921 "scalable demanded bits are ignored");
2922
2923 if (!DemandedElts)
2924 return false; // No demanded elts, better to assume we don't know anything.
2925
2926 if (Depth >= MaxRecursionDepth)
2927 return false; // Limit search depth.
2928
2929 // Deal with some common cases here that work for both fixed and scalable
2930 // vector types.
2931 switch (Opcode) {
2932 case ISD::SPLAT_VECTOR:
2933 UndefElts = V.getOperand(0).isUndef()
2934 ? APInt::getAllOnes(DemandedElts.getBitWidth())
2935 : APInt(DemandedElts.getBitWidth(), 0);
2936 return true;
2937 case ISD::ADD:
2938 case ISD::SUB:
2939 case ISD::AND:
2940 case ISD::XOR:
2941 case ISD::OR: {
2942 APInt UndefLHS, UndefRHS;
2943 SDValue LHS = V.getOperand(0);
2944 SDValue RHS = V.getOperand(1);
2945 // Only recognize splats with the same demanded undef elements for both
2946 // operands, otherwise we might fail to handle binop-specific undef
2947 // handling.
2948 // e.g. (and undef, 0) -> 0 etc.
2949 if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
2950 isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1) &&
2951 (DemandedElts & UndefLHS) == (DemandedElts & UndefRHS)) {
2952 UndefElts = UndefLHS | UndefRHS;
2953 return true;
2954 }
2955 return false;
2956 }
2957 case ISD::ABS:
2958 case ISD::TRUNCATE:
2959 case ISD::SIGN_EXTEND:
2960 case ISD::ZERO_EXTEND:
2961 return isSplatValue(V.getOperand(0), DemandedElts, UndefElts, Depth + 1);
2962 default:
2963 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
2964 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
2965 return TLI->isSplatValueForTargetNode(V, DemandedElts, UndefElts, *this,
2966 Depth);
2967 break;
2968 }
2969
2970 // We don't support other cases than those above for scalable vectors at
2971 // the moment.
2972 if (VT.isScalableVector())
2973 return false;
2974
2975 unsigned NumElts = VT.getVectorNumElements();
2976 assert(NumElts == DemandedElts.getBitWidth() && "Vector size mismatch");
2977 UndefElts = APInt::getZero(NumElts);
2978
2979 switch (Opcode) {
2980 case ISD::BUILD_VECTOR: {
2981 SDValue Scl;
2982 for (unsigned i = 0; i != NumElts; ++i) {
2983 SDValue Op = V.getOperand(i);
2984 if (Op.isUndef()) {
2985 UndefElts.setBit(i);
2986 continue;
2987 }
2988 if (!DemandedElts[i])
2989 continue;
2990 if (Scl && Scl != Op)
2991 return false;
2992 Scl = Op;
2993 }
2994 return true;
2995 }
2996 case ISD::VECTOR_SHUFFLE: {
2997 // Check if this is a shuffle node doing a splat or a shuffle of a splat.
2998 APInt DemandedLHS = APInt::getZero(NumElts);
2999 APInt DemandedRHS = APInt::getZero(NumElts);
3000 ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(V)->getMask();
3001 for (int i = 0; i != (int)NumElts; ++i) {
3002 int M = Mask[i];
3003 if (M < 0) {
3004 UndefElts.setBit(i);
3005 continue;
3006 }
3007 if (!DemandedElts[i])
3008 continue;
3009 if (M < (int)NumElts)
3010 DemandedLHS.setBit(M);
3011 else
3012 DemandedRHS.setBit(M - NumElts);
3013 }
3014
3015 // If we aren't demanding either op, assume there's no splat.
3016 // If we are demanding both ops, assume there's no splat.
3017 if ((DemandedLHS.isZero() && DemandedRHS.isZero()) ||
3018 (!DemandedLHS.isZero() && !DemandedRHS.isZero()))
3019 return false;
3020
3021 // See if the demanded elts of the source op is a splat or we only demand
3022 // one element, which should always be a splat.
3023 // TODO: Handle source ops splats with undefs.
3024 auto CheckSplatSrc = [&](SDValue Src, const APInt &SrcElts) {
3025 APInt SrcUndefs;
3026 return (SrcElts.popcount() == 1) ||
3027 (isSplatValue(Src, SrcElts, SrcUndefs, Depth + 1) &&
3028 (SrcElts & SrcUndefs).isZero());
3029 };
3030 if (!DemandedLHS.isZero())
3031 return CheckSplatSrc(V.getOperand(0), DemandedLHS);
3032 return CheckSplatSrc(V.getOperand(1), DemandedRHS);
3033 }
3035 // Offset the demanded elts by the subvector index.
3036 SDValue Src = V.getOperand(0);
3037 // We don't support scalable vectors at the moment.
3038 if (Src.getValueType().isScalableVector())
3039 return false;
3040 uint64_t Idx = V.getConstantOperandVal(1);
3041 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3042 APInt UndefSrcElts;
3043 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3044 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3045 UndefElts = UndefSrcElts.extractBits(NumElts, Idx);
3046 return true;
3047 }
3048 break;
3049 }
3053 // Widen the demanded elts by the src element count.
3054 SDValue Src = V.getOperand(0);
3055 // We don't support scalable vectors at the moment.
3056 if (Src.getValueType().isScalableVector())
3057 return false;
3058 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3059 APInt UndefSrcElts;
3060 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts);
3061 if (isSplatValue(Src, DemandedSrcElts, UndefSrcElts, Depth + 1)) {
3062 UndefElts = UndefSrcElts.trunc(NumElts);
3063 return true;
3064 }
3065 break;
3066 }
3067 case ISD::BITCAST: {
3068 SDValue Src = V.getOperand(0);
3069 EVT SrcVT = Src.getValueType();
3070 unsigned SrcBitWidth = SrcVT.getScalarSizeInBits();
3071 unsigned BitWidth = VT.getScalarSizeInBits();
3072
3073 // Ignore bitcasts from unsupported types.
3074 // TODO: Add fp support?
3075 if (!SrcVT.isVector() || !SrcVT.isInteger() || !VT.isInteger())
3076 break;
3077
3078 // Bitcast 'small element' vector to 'large element' vector.
3079 if ((BitWidth % SrcBitWidth) == 0) {
3080 // See if each sub element is a splat.
3081 unsigned Scale = BitWidth / SrcBitWidth;
3082 unsigned NumSrcElts = SrcVT.getVectorNumElements();
3083 APInt ScaledDemandedElts =
3084 APIntOps::ScaleBitMask(DemandedElts, NumSrcElts);
3085 for (unsigned I = 0; I != Scale; ++I) {
3086 APInt SubUndefElts;
3087 APInt SubDemandedElt = APInt::getOneBitSet(Scale, I);
3088 APInt SubDemandedElts = APInt::getSplat(NumSrcElts, SubDemandedElt);
3089 SubDemandedElts &= ScaledDemandedElts;
3090 if (!isSplatValue(Src, SubDemandedElts, SubUndefElts, Depth + 1))
3091 return false;
3092 // TODO: Add support for merging sub undef elements.
3093 if (!SubUndefElts.isZero())
3094 return false;
3095 }
3096 return true;
3097 }
3098 break;
3099 }
3100 }
3101
3102 return false;
3103}
3104
3105/// Helper wrapper to main isSplatValue function.
3106bool SelectionDAG::isSplatValue(SDValue V, bool AllowUndefs) const {
3107 EVT VT = V.getValueType();
3108 assert(VT.isVector() && "Vector type expected");
3109
3110 APInt UndefElts;
3111 // Since the number of lanes in a scalable vector is unknown at compile time,
3112 // we track one bit which is implicitly broadcast to all lanes. This means
3113 // that all lanes in a scalable vector are considered demanded.
3114 APInt DemandedElts
3116 return isSplatValue(V, DemandedElts, UndefElts) &&
3117 (AllowUndefs || !UndefElts);
3118}
3119
3122
3123 EVT VT = V.getValueType();
3124 unsigned Opcode = V.getOpcode();
3125 switch (Opcode) {
3126 default: {
3127 APInt UndefElts;
3128 // Since the number of lanes in a scalable vector is unknown at compile time,
3129 // we track one bit which is implicitly broadcast to all lanes. This means
3130 // that all lanes in a scalable vector are considered demanded.
3131 APInt DemandedElts
3133
3134 if (isSplatValue(V, DemandedElts, UndefElts)) {
3135 if (VT.isScalableVector()) {
3136 // DemandedElts and UndefElts are ignored for scalable vectors, since
3137 // the only supported cases are SPLAT_VECTOR nodes.
3138 SplatIdx = 0;
3139 } else {
3140 // Handle case where all demanded elements are UNDEF.
3141 if (DemandedElts.isSubsetOf(UndefElts)) {
3142 SplatIdx = 0;
3143 return getUNDEF(VT);
3144 }
3145 SplatIdx = (UndefElts & DemandedElts).countr_one();
3146 }
3147 return V;
3148 }
3149 break;
3150 }
3151 case ISD::SPLAT_VECTOR:
3152 SplatIdx = 0;
3153 return V;
3154 case ISD::VECTOR_SHUFFLE: {
3155 assert(!VT.isScalableVector());
3156 // Check if this is a shuffle node doing a splat.
3157 // TODO - remove this and rely purely on SelectionDAG::isSplatValue,
3158 // getTargetVShiftNode currently struggles without the splat source.
3159 auto *SVN = cast<ShuffleVectorSDNode>(V);
3160 if (!SVN->isSplat())
3161 break;
3162 int Idx = SVN->getSplatIndex();
3163 int NumElts = V.getValueType().getVectorNumElements();
3164 SplatIdx = Idx % NumElts;
3165 return V.getOperand(Idx / NumElts);
3166 }
3167 }
3168
3169 return SDValue();
3170}
3171
3173 int SplatIdx;
3174 if (SDValue SrcVector = getSplatSourceVector(V, SplatIdx)) {
3175 EVT SVT = SrcVector.getValueType().getScalarType();
3176 EVT LegalSVT = SVT;
3177 if (LegalTypes && !TLI->isTypeLegal(SVT)) {
3178 if (!SVT.isInteger())
3179 return SDValue();
3180 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
3181 if (LegalSVT.bitsLT(SVT))
3182 return SDValue();
3183 }
3184 return getExtractVectorElt(SDLoc(V), LegalSVT, SrcVector, SplatIdx);
3185 }
3186 return SDValue();
3187}
3188
3189std::optional<ConstantRange>
3191 unsigned Depth) const {
3192 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3193 V.getOpcode() == ISD::SRA) &&
3194 "Unknown shift node");
3195 // Shifting more than the bitwidth is not valid.
3196 unsigned BitWidth = V.getScalarValueSizeInBits();
3197
3198 if (auto *Cst = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3199 const APInt &ShAmt = Cst->getAPIntValue();
3200 if (ShAmt.uge(BitWidth))
3201 return std::nullopt;
3202 return ConstantRange(ShAmt);
3203 }
3204
3205 if (auto *BV = dyn_cast<BuildVectorSDNode>(V.getOperand(1))) {
3206 const APInt *MinAmt = nullptr, *MaxAmt = nullptr;
3207 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3208 if (!DemandedElts[i])
3209 continue;
3210 auto *SA = dyn_cast<ConstantSDNode>(BV->getOperand(i));
3211 if (!SA) {
3212 MinAmt = MaxAmt = nullptr;
3213 break;
3214 }
3215 const APInt &ShAmt = SA->getAPIntValue();
3216 if (ShAmt.uge(BitWidth))
3217 return std::nullopt;
3218 if (!MinAmt || MinAmt->ugt(ShAmt))
3219 MinAmt = &ShAmt;
3220 if (!MaxAmt || MaxAmt->ult(ShAmt))
3221 MaxAmt = &ShAmt;
3222 }
3223 assert(((!MinAmt && !MaxAmt) || (MinAmt && MaxAmt)) &&
3224 "Failed to find matching min/max shift amounts");
3225 if (MinAmt && MaxAmt)
3226 return ConstantRange(*MinAmt, *MaxAmt + 1);
3227 }
3228
3229 // Use computeKnownBits to find a hidden constant/knownbits (usually type
3230 // legalized). e.g. Hidden behind multiple bitcasts/build_vector/casts etc.
3231 KnownBits KnownAmt = computeKnownBits(V.getOperand(1), DemandedElts, Depth);
3232 if (KnownAmt.getMaxValue().ult(BitWidth))
3233 return ConstantRange::fromKnownBits(KnownAmt, /*IsSigned=*/false);
3234
3235 return std::nullopt;
3236}
3237
3238std::optional<unsigned>
3240 unsigned Depth) const {
3241 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3242 V.getOpcode() == ISD::SRA) &&
3243 "Unknown shift node");
3244 if (std::optional<ConstantRange> AmtRange =
3245 getValidShiftAmountRange(V, DemandedElts, Depth))
3246 if (const APInt *ShAmt = AmtRange->getSingleElement())
3247 return ShAmt->getZExtValue();
3248 return std::nullopt;
3249}
3250
3251std::optional<unsigned>
3253 EVT VT = V.getValueType();
3254 APInt DemandedElts = VT.isFixedLengthVector()
3256 : APInt(1, 1);
3257 return getValidShiftAmount(V, DemandedElts, Depth);
3258}
3259
3260std::optional<unsigned>
3262 unsigned Depth) const {
3263 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3264 V.getOpcode() == ISD::SRA) &&
3265 "Unknown shift node");
3266 if (std::optional<ConstantRange> AmtRange =
3267 getValidShiftAmountRange(V, DemandedElts, Depth))
3268 return AmtRange->getUnsignedMin().getZExtValue();
3269 return std::nullopt;
3270}
3271
3272std::optional<unsigned>
3274 EVT VT = V.getValueType();
3275 APInt DemandedElts = VT.isFixedLengthVector()
3277 : APInt(1, 1);
3278 return getValidMinimumShiftAmount(V, DemandedElts, Depth);
3279}
3280
3281std::optional<unsigned>
3283 unsigned Depth) const {
3284 assert((V.getOpcode() == ISD::SHL || V.getOpcode() == ISD::SRL ||
3285 V.getOpcode() == ISD::SRA) &&
3286 "Unknown shift node");
3287 if (std::optional<ConstantRange> AmtRange =
3288 getValidShiftAmountRange(V, DemandedElts, Depth))
3289 return AmtRange->getUnsignedMax().getZExtValue();
3290 return std::nullopt;
3291}
3292
3293std::optional<unsigned>
3295 EVT VT = V.getValueType();
3296 APInt DemandedElts = VT.isFixedLengthVector()
3298 : APInt(1, 1);
3299 return getValidMaximumShiftAmount(V, DemandedElts, Depth);
3300}
3301
3302/// Determine which bits of Op are known to be either zero or one and return
3303/// them in Known. For vectors, the known bits are those that are shared by
3304/// every vector element.
3306 EVT VT = Op.getValueType();
3307
3308 // Since the number of lanes in a scalable vector is unknown at compile time,
3309 // we track one bit which is implicitly broadcast to all lanes. This means
3310 // that all lanes in a scalable vector are considered demanded.
3311 APInt DemandedElts = VT.isFixedLengthVector()
3313 : APInt(1, 1);
3314 return computeKnownBits(Op, DemandedElts, Depth);
3315}
3316
3317/// Determine which bits of Op are known to be either zero or one and return
3318/// them in Known. The DemandedElts argument allows us to only collect the known
3319/// bits that are shared by the requested vector elements.
3321 unsigned Depth) const {
3322 unsigned BitWidth = Op.getScalarValueSizeInBits();
3323
3324 KnownBits Known(BitWidth); // Don't know anything.
3325
3326 if (auto OptAPInt = Op->bitcastToAPInt()) {
3327 // We know all of the bits for a constant!
3328 return KnownBits::makeConstant(*std::move(OptAPInt));
3329 }
3330
3331 if (Depth >= MaxRecursionDepth)
3332 return Known; // Limit search depth.
3333
3334 KnownBits Known2;
3335 unsigned NumElts = DemandedElts.getBitWidth();
3336 assert((!Op.getValueType().isFixedLengthVector() ||
3337 NumElts == Op.getValueType().getVectorNumElements()) &&
3338 "Unexpected vector size");
3339
3340 if (!DemandedElts)
3341 return Known; // No demanded elts, better to assume we don't know anything.
3342
3343 unsigned Opcode = Op.getOpcode();
3344 switch (Opcode) {
3345 case ISD::MERGE_VALUES:
3346 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3347 Depth + 1);
3348 case ISD::SPLAT_VECTOR: {
3349 SDValue SrcOp = Op.getOperand(0);
3350 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3351 "Expected SPLAT_VECTOR implicit truncation");
3352 // Implicitly truncate the bits to match the official semantics of
3353 // SPLAT_VECTOR.
3354 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3355 break;
3356 }
3358 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3359 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3360 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3361 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3362 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3363 }
3364 break;
3365 }
3366 case ISD::STEP_VECTOR: {
3367 const APInt &Step = Op.getConstantOperandAPInt(0);
3368
3369 if (Step.isPowerOf2())
3370 Known.Zero.setLowBits(Step.logBase2());
3371
3373
3374 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3375 break;
3376 const APInt MinNumElts =
3377 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3378
3379 bool Overflow;
3380 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3382 .umul_ov(MinNumElts, Overflow);
3383 if (Overflow)
3384 break;
3385
3386 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3387 if (Overflow)
3388 break;
3389
3390 Known.Zero.setHighBits(MaxValue.countl_zero());
3391 break;
3392 }
3393 case ISD::BUILD_VECTOR:
3394 assert(!Op.getValueType().isScalableVector());
3395 // Collect the known bits that are shared by every demanded vector element.
3396 Known.setAllConflict();
3397 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3398 if (!DemandedElts[i])
3399 continue;
3400
3401 SDValue SrcOp = Op.getOperand(i);
3402 Known2 = computeKnownBits(SrcOp, Depth + 1);
3403
3404 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3405 if (SrcOp.getValueSizeInBits() != BitWidth) {
3406 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3407 "Expected BUILD_VECTOR implicit truncation");
3408 Known2 = Known2.trunc(BitWidth);
3409 }
3410
3411 // Known bits are the values that are shared by every demanded element.
3412 Known = Known.intersectWith(Known2);
3413
3414 // If we don't know any bits, early out.
3415 if (Known.isUnknown())
3416 break;
3417 }
3418 break;
3419 case ISD::VECTOR_COMPRESS: {
3420 SDValue Vec = Op.getOperand(0);
3421 SDValue PassThru = Op.getOperand(2);
3422 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3423 // If we don't know any bits, early out.
3424 if (Known.isUnknown())
3425 break;
3426 Known2 = computeKnownBits(Vec, Depth + 1);
3427 Known = Known.intersectWith(Known2);
3428 break;
3429 }
3430 case ISD::VECTOR_SHUFFLE: {
3431 assert(!Op.getValueType().isScalableVector());
3432 // Collect the known bits that are shared by every vector element referenced
3433 // by the shuffle.
3434 APInt DemandedLHS, DemandedRHS;
3436 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3437 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3438 DemandedLHS, DemandedRHS))
3439 break;
3440
3441 // Known bits are the values that are shared by every demanded element.
3442 Known.setAllConflict();
3443 if (!!DemandedLHS) {
3444 SDValue LHS = Op.getOperand(0);
3445 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3446 Known = Known.intersectWith(Known2);
3447 }
3448 // If we don't know any bits, early out.
3449 if (Known.isUnknown())
3450 break;
3451 if (!!DemandedRHS) {
3452 SDValue RHS = Op.getOperand(1);
3453 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3454 Known = Known.intersectWith(Known2);
3455 }
3456 break;
3457 }
3458 case ISD::VSCALE: {
3460 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3461 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3462 break;
3463 }
3464 case ISD::CONCAT_VECTORS: {
3465 if (Op.getValueType().isScalableVector())
3466 break;
3467 // Split DemandedElts and test each of the demanded subvectors.
3468 Known.setAllConflict();
3469 EVT SubVectorVT = Op.getOperand(0).getValueType();
3470 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3471 unsigned NumSubVectors = Op.getNumOperands();
3472 for (unsigned i = 0; i != NumSubVectors; ++i) {
3473 APInt DemandedSub =
3474 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3475 if (!!DemandedSub) {
3476 SDValue Sub = Op.getOperand(i);
3477 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3478 Known = Known.intersectWith(Known2);
3479 }
3480 // If we don't know any bits, early out.
3481 if (Known.isUnknown())
3482 break;
3483 }
3484 break;
3485 }
3486 case ISD::INSERT_SUBVECTOR: {
3487 if (Op.getValueType().isScalableVector())
3488 break;
3489 // Demand any elements from the subvector and the remainder from the src its
3490 // inserted into.
3491 SDValue Src = Op.getOperand(0);
3492 SDValue Sub = Op.getOperand(1);
3493 uint64_t Idx = Op.getConstantOperandVal(2);
3494 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3495 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3496 APInt DemandedSrcElts = DemandedElts;
3497 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3498
3499 Known.setAllConflict();
3500 if (!!DemandedSubElts) {
3501 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3502 if (Known.isUnknown())
3503 break; // early-out.
3504 }
3505 if (!!DemandedSrcElts) {
3506 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3507 Known = Known.intersectWith(Known2);
3508 }
3509 break;
3510 }
3512 // Offset the demanded elts by the subvector index.
3513 SDValue Src = Op.getOperand(0);
3514 // Bail until we can represent demanded elements for scalable vectors.
3515 if (Op.getValueType().isScalableVector() || Src.getValueType().isScalableVector())
3516 break;
3517 uint64_t Idx = Op.getConstantOperandVal(1);
3518 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3519 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3520 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3521 break;
3522 }
3523 case ISD::SCALAR_TO_VECTOR: {
3524 if (Op.getValueType().isScalableVector())
3525 break;
3526 // We know about scalar_to_vector as much as we know about it source,
3527 // which becomes the first element of otherwise unknown vector.
3528 if (DemandedElts != 1)
3529 break;
3530
3531 SDValue N0 = Op.getOperand(0);
3532 Known = computeKnownBits(N0, Depth + 1);
3533 if (N0.getValueSizeInBits() != BitWidth)
3534 Known = Known.trunc(BitWidth);
3535
3536 break;
3537 }
3538 case ISD::BITCAST: {
3539 if (Op.getValueType().isScalableVector())
3540 break;
3541
3542 SDValue N0 = Op.getOperand(0);
3543 EVT SubVT = N0.getValueType();
3544 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3545
3546 // Ignore bitcasts from unsupported types.
3547 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3548 break;
3549
3550 // Fast handling of 'identity' bitcasts.
3551 if (BitWidth == SubBitWidth) {
3552 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3553 break;
3554 }
3555
3556 bool IsLE = getDataLayout().isLittleEndian();
3557
3558 // Bitcast 'small element' vector to 'large element' scalar/vector.
3559 if ((BitWidth % SubBitWidth) == 0) {
3560 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3561
3562 // Collect known bits for the (larger) output by collecting the known
3563 // bits from each set of sub elements and shift these into place.
3564 // We need to separately call computeKnownBits for each set of
3565 // sub elements as the knownbits for each is likely to be different.
3566 unsigned SubScale = BitWidth / SubBitWidth;
3567 APInt SubDemandedElts(NumElts * SubScale, 0);
3568 for (unsigned i = 0; i != NumElts; ++i)
3569 if (DemandedElts[i])
3570 SubDemandedElts.setBit(i * SubScale);
3571
3572 for (unsigned i = 0; i != SubScale; ++i) {
3573 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3574 Depth + 1);
3575 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3576 Known.insertBits(Known2, SubBitWidth * Shifts);
3577 }
3578 }
3579
3580 // Bitcast 'large element' scalar/vector to 'small element' vector.
3581 if ((SubBitWidth % BitWidth) == 0) {
3582 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3583
3584 // Collect known bits for the (smaller) output by collecting the known
3585 // bits from the overlapping larger input elements and extracting the
3586 // sub sections we actually care about.
3587 unsigned SubScale = SubBitWidth / BitWidth;
3588 APInt SubDemandedElts =
3589 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3590 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3591
3592 Known.setAllConflict();
3593 for (unsigned i = 0; i != NumElts; ++i)
3594 if (DemandedElts[i]) {
3595 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3596 unsigned Offset = (Shifts % SubScale) * BitWidth;
3597 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3598 // If we don't know any bits, early out.
3599 if (Known.isUnknown())
3600 break;
3601 }
3602 }
3603 break;
3604 }
3605 case ISD::AND:
3606 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3607 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3608
3609 Known &= Known2;
3610 break;
3611 case ISD::OR:
3612 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3613 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3614
3615 Known |= Known2;
3616 break;
3617 case ISD::XOR:
3618 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3619 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3620
3621 Known ^= Known2;
3622 break;
3623 case ISD::MUL: {
3624 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3625 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3626 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3627 // TODO: SelfMultiply can be poison, but not undef.
3628 if (SelfMultiply)
3629 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3630 Op.getOperand(0), DemandedElts, false, Depth + 1);
3631 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3632
3633 // If the multiplication is known not to overflow, the product of a number
3634 // with itself is non-negative. Only do this if we didn't already computed
3635 // the opposite value for the sign bit.
3636 if (Op->getFlags().hasNoSignedWrap() &&
3637 Op.getOperand(0) == Op.getOperand(1) &&
3638 !Known.isNegative())
3639 Known.makeNonNegative();
3640 break;
3641 }
3642 case ISD::MULHU: {
3643 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3644 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3645 Known = KnownBits::mulhu(Known, Known2);
3646 break;
3647 }
3648 case ISD::MULHS: {
3649 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3650 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3651 Known = KnownBits::mulhs(Known, Known2);
3652 break;
3653 }
3654 case ISD::ABDU: {
3655 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3656 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3657 Known = KnownBits::abdu(Known, Known2);
3658 break;
3659 }
3660 case ISD::ABDS: {
3661 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3662 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3663 Known = KnownBits::abds(Known, Known2);
3664 unsigned SignBits1 =
3665 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3666 if (SignBits1 == 1)
3667 break;
3668 unsigned SignBits0 =
3669 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3670 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3671 break;
3672 }
3673 case ISD::UMUL_LOHI: {
3674 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3675 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3676 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3677 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3678 if (Op.getResNo() == 0)
3679 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3680 else
3681 Known = KnownBits::mulhu(Known, Known2);
3682 break;
3683 }
3684 case ISD::SMUL_LOHI: {
3685 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3686 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3687 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3688 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3689 if (Op.getResNo() == 0)
3690 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3691 else
3692 Known = KnownBits::mulhs(Known, Known2);
3693 break;
3694 }
3695 case ISD::AVGFLOORU: {
3696 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3697 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3698 Known = KnownBits::avgFloorU(Known, Known2);
3699 break;
3700 }
3701 case ISD::AVGCEILU: {
3702 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3703 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3704 Known = KnownBits::avgCeilU(Known, Known2);
3705 break;
3706 }
3707 case ISD::AVGFLOORS: {
3708 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3709 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3710 Known = KnownBits::avgFloorS(Known, Known2);
3711 break;
3712 }
3713 case ISD::AVGCEILS: {
3714 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3715 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3716 Known = KnownBits::avgCeilS(Known, Known2);
3717 break;
3718 }
3719 case ISD::SELECT:
3720 case ISD::VSELECT:
3721 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3722 // If we don't know any bits, early out.
3723 if (Known.isUnknown())
3724 break;
3725 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3726
3727 // Only known if known in both the LHS and RHS.
3728 Known = Known.intersectWith(Known2);
3729 break;
3730 case ISD::SELECT_CC:
3731 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3732 // If we don't know any bits, early out.
3733 if (Known.isUnknown())
3734 break;
3735 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3736
3737 // Only known if known in both the LHS and RHS.
3738 Known = Known.intersectWith(Known2);
3739 break;
3740 case ISD::SMULO:
3741 case ISD::UMULO:
3742 if (Op.getResNo() != 1)
3743 break;
3744 // The boolean result conforms to getBooleanContents.
3745 // If we know the result of a setcc has the top bits zero, use this info.
3746 // We know that we have an integer-based boolean since these operations
3747 // are only available for integer.
3748 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3750 BitWidth > 1)
3751 Known.Zero.setBitsFrom(1);
3752 break;
3753 case ISD::SETCC:
3754 case ISD::SETCCCARRY:
3755 case ISD::STRICT_FSETCC:
3756 case ISD::STRICT_FSETCCS: {
3757 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3758 // If we know the result of a setcc has the top bits zero, use this info.
3759 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3761 BitWidth > 1)
3762 Known.Zero.setBitsFrom(1);
3763 break;
3764 }
3765 case ISD::SHL: {
3766 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3767 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3768
3769 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3770 bool NSW = Op->getFlags().hasNoSignedWrap();
3771
3772 bool ShAmtNonZero = Known2.isNonZero();
3773
3774 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3775
3776 // Minimum shift low bits are known zero.
3777 if (std::optional<unsigned> ShMinAmt =
3778 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3779 Known.Zero.setLowBits(*ShMinAmt);
3780 break;
3781 }
3782 case ISD::SRL:
3783 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3784 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3785 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3786 Op->getFlags().hasExact());
3787
3788 // Minimum shift high bits are known zero.
3789 if (std::optional<unsigned> ShMinAmt =
3790 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3791 Known.Zero.setHighBits(*ShMinAmt);
3792 break;
3793 case ISD::SRA:
3794 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3795 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3796 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3797 Op->getFlags().hasExact());
3798 break;
3799 case ISD::ROTL:
3800 case ISD::ROTR:
3801 if (ConstantSDNode *C =
3802 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3803 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3804
3805 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3806
3807 // Canonicalize to ROTR.
3808 if (Opcode == ISD::ROTL && Amt != 0)
3809 Amt = BitWidth - Amt;
3810
3811 Known.Zero = Known.Zero.rotr(Amt);
3812 Known.One = Known.One.rotr(Amt);
3813 }
3814 break;
3815 case ISD::FSHL:
3816 case ISD::FSHR:
3817 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3818 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3819
3820 // For fshl, 0-shift returns the 1st arg.
3821 // For fshr, 0-shift returns the 2nd arg.
3822 if (Amt == 0) {
3823 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3824 DemandedElts, Depth + 1);
3825 break;
3826 }
3827
3828 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3829 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3830 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3831 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3832 if (Opcode == ISD::FSHL) {
3833 Known <<= Amt;
3834 Known2 >>= BitWidth - Amt;
3835 } else {
3836 Known <<= BitWidth - Amt;
3837 Known2 >>= Amt;
3838 }
3839 Known = Known.unionWith(Known2);
3840 }
3841 break;
3842 case ISD::SHL_PARTS:
3843 case ISD::SRA_PARTS:
3844 case ISD::SRL_PARTS: {
3845 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3846
3847 // Collect lo/hi source values and concatenate.
3848 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3849 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3850 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3851 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3852 Known = Known2.concat(Known);
3853
3854 // Collect shift amount.
3855 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3856
3857 if (Opcode == ISD::SHL_PARTS)
3858 Known = KnownBits::shl(Known, Known2);
3859 else if (Opcode == ISD::SRA_PARTS)
3860 Known = KnownBits::ashr(Known, Known2);
3861 else // if (Opcode == ISD::SRL_PARTS)
3862 Known = KnownBits::lshr(Known, Known2);
3863
3864 // TODO: Minimum shift low/high bits are known zero.
3865
3866 if (Op.getResNo() == 0)
3867 Known = Known.extractBits(LoBits, 0);
3868 else
3869 Known = Known.extractBits(HiBits, LoBits);
3870 break;
3871 }
3873 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3874 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3875 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3876 break;
3877 }
3878 case ISD::CTTZ:
3879 case ISD::CTTZ_ZERO_UNDEF: {
3880 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3881 // If we have a known 1, its position is our upper bound.
3882 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3883 unsigned LowBits = llvm::bit_width(PossibleTZ);
3884 Known.Zero.setBitsFrom(LowBits);
3885 break;
3886 }
3887 case ISD::CTLZ:
3888 case ISD::CTLZ_ZERO_UNDEF: {
3889 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3890 // If we have a known 1, its position is our upper bound.
3891 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3892 unsigned LowBits = llvm::bit_width(PossibleLZ);
3893 Known.Zero.setBitsFrom(LowBits);
3894 break;
3895 }
3896 case ISD::CTLS: {
3897 unsigned MinRedundantSignBits =
3898 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3899 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3901 Known = Range.toKnownBits();
3902 break;
3903 }
3904 case ISD::CTPOP: {
3905 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3906 // If we know some of the bits are zero, they can't be one.
3907 unsigned PossibleOnes = Known2.countMaxPopulation();
3908 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3909 break;
3910 }
3911 case ISD::PARITY: {
3912 // Parity returns 0 everywhere but the LSB.
3913 Known.Zero.setBitsFrom(1);
3914 break;
3915 }
3916 case ISD::MGATHER:
3917 case ISD::MLOAD: {
3918 ISD::LoadExtType ETy =
3919 (Opcode == ISD::MGATHER)
3920 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3921 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3922 if (ETy == ISD::ZEXTLOAD) {
3923 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3924 KnownBits Known0(MemVT.getScalarSizeInBits());
3925 return Known0.zext(BitWidth);
3926 }
3927 break;
3928 }
3929 case ISD::LOAD: {
3931 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3932 if (ISD::isNON_EXTLoad(LD) && Cst) {
3933 // Determine any common known bits from the loaded constant pool value.
3934 Type *CstTy = Cst->getType();
3935 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3936 !Op.getValueType().isScalableVector()) {
3937 // If its a vector splat, then we can (quickly) reuse the scalar path.
3938 // NOTE: We assume all elements match and none are UNDEF.
3939 if (CstTy->isVectorTy()) {
3940 if (const Constant *Splat = Cst->getSplatValue()) {
3941 Cst = Splat;
3942 CstTy = Cst->getType();
3943 }
3944 }
3945 // TODO - do we need to handle different bitwidths?
3946 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3947 // Iterate across all vector elements finding common known bits.
3948 Known.setAllConflict();
3949 for (unsigned i = 0; i != NumElts; ++i) {
3950 if (!DemandedElts[i])
3951 continue;
3952 if (Constant *Elt = Cst->getAggregateElement(i)) {
3953 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3954 const APInt &Value = CInt->getValue();
3955 Known.One &= Value;
3956 Known.Zero &= ~Value;
3957 continue;
3958 }
3959 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3960 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3961 Known.One &= Value;
3962 Known.Zero &= ~Value;
3963 continue;
3964 }
3965 }
3966 Known.One.clearAllBits();
3967 Known.Zero.clearAllBits();
3968 break;
3969 }
3970 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3971 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3972 Known = KnownBits::makeConstant(CInt->getValue());
3973 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3974 Known =
3975 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3976 }
3977 }
3978 }
3979 } else if (Op.getResNo() == 0) {
3980 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3981 KnownBits KnownScalarMemory(ScalarMemorySize);
3982 if (const MDNode *MD = LD->getRanges())
3983 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3984
3985 // Extend the Known bits from memory to the size of the scalar result.
3986 if (ISD::isZEXTLoad(Op.getNode()))
3987 Known = KnownScalarMemory.zext(BitWidth);
3988 else if (ISD::isSEXTLoad(Op.getNode()))
3989 Known = KnownScalarMemory.sext(BitWidth);
3990 else if (ISD::isEXTLoad(Op.getNode()))
3991 Known = KnownScalarMemory.anyext(BitWidth);
3992 else
3993 Known = KnownScalarMemory;
3994 assert(Known.getBitWidth() == BitWidth);
3995 return Known;
3996 }
3997 break;
3998 }
4000 if (Op.getValueType().isScalableVector())
4001 break;
4002 EVT InVT = Op.getOperand(0).getValueType();
4003 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4004 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4005 Known = Known.zext(BitWidth);
4006 break;
4007 }
4008 case ISD::ZERO_EXTEND: {
4009 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4010 Known = Known.zext(BitWidth);
4011 break;
4012 }
4014 if (Op.getValueType().isScalableVector())
4015 break;
4016 EVT InVT = Op.getOperand(0).getValueType();
4017 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4018 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4019 // If the sign bit is known to be zero or one, then sext will extend
4020 // it to the top bits, else it will just zext.
4021 Known = Known.sext(BitWidth);
4022 break;
4023 }
4024 case ISD::SIGN_EXTEND: {
4025 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4026 // If the sign bit is known to be zero or one, then sext will extend
4027 // it to the top bits, else it will just zext.
4028 Known = Known.sext(BitWidth);
4029 break;
4030 }
4032 if (Op.getValueType().isScalableVector())
4033 break;
4034 EVT InVT = Op.getOperand(0).getValueType();
4035 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4036 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4037 Known = Known.anyext(BitWidth);
4038 break;
4039 }
4040 case ISD::ANY_EXTEND: {
4041 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4042 Known = Known.anyext(BitWidth);
4043 break;
4044 }
4045 case ISD::TRUNCATE: {
4046 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4047 Known = Known.trunc(BitWidth);
4048 break;
4049 }
4050 case ISD::TRUNCATE_SSAT_S: {
4051 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4052 Known = Known.truncSSat(BitWidth);
4053 break;
4054 }
4055 case ISD::TRUNCATE_SSAT_U: {
4056 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4057 Known = Known.truncSSatU(BitWidth);
4058 break;
4059 }
4060 case ISD::TRUNCATE_USAT_U: {
4061 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4062 Known = Known.truncUSat(BitWidth);
4063 break;
4064 }
4065 case ISD::AssertZext: {
4066 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4068 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4069 Known.Zero |= (~InMask);
4070 Known.One &= (~Known.Zero);
4071 break;
4072 }
4073 case ISD::AssertAlign: {
4074 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4075 assert(LogOfAlign != 0);
4076
4077 // TODO: Should use maximum with source
4078 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4079 // well as clearing one bits.
4080 Known.Zero.setLowBits(LogOfAlign);
4081 Known.One.clearLowBits(LogOfAlign);
4082 break;
4083 }
4084 case ISD::AssertNoFPClass: {
4085 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4086
4087 FPClassTest NoFPClass =
4088 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4089 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4090 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4091 // Cannot be negative.
4092 Known.makeNonNegative();
4093 }
4094
4095 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4096 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4097 // Cannot be positive.
4098 Known.makeNegative();
4099 }
4100
4101 break;
4102 }
4103 case ISD::FGETSIGN:
4104 // All bits are zero except the low bit.
4105 Known.Zero.setBitsFrom(1);
4106 break;
4107 case ISD::ADD:
4108 case ISD::SUB: {
4109 SDNodeFlags Flags = Op.getNode()->getFlags();
4110 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4111 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4113 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4114 Flags.hasNoUnsignedWrap(), Known, Known2);
4115 break;
4116 }
4117 case ISD::USUBO:
4118 case ISD::SSUBO:
4119 case ISD::USUBO_CARRY:
4120 case ISD::SSUBO_CARRY:
4121 if (Op.getResNo() == 1) {
4122 // If we know the result of a setcc has the top bits zero, use this info.
4123 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4125 BitWidth > 1)
4126 Known.Zero.setBitsFrom(1);
4127 break;
4128 }
4129 [[fallthrough]];
4130 case ISD::SUBC: {
4131 assert(Op.getResNo() == 0 &&
4132 "We only compute knownbits for the difference here.");
4133
4134 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4135 KnownBits Borrow(1);
4136 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4137 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4138 // Borrow has bit width 1
4139 Borrow = Borrow.trunc(1);
4140 } else {
4141 Borrow.setAllZero();
4142 }
4143
4144 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4145 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4146 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4147 break;
4148 }
4149 case ISD::UADDO:
4150 case ISD::SADDO:
4151 case ISD::UADDO_CARRY:
4152 case ISD::SADDO_CARRY:
4153 if (Op.getResNo() == 1) {
4154 // If we know the result of a setcc has the top bits zero, use this info.
4155 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4157 BitWidth > 1)
4158 Known.Zero.setBitsFrom(1);
4159 break;
4160 }
4161 [[fallthrough]];
4162 case ISD::ADDC:
4163 case ISD::ADDE: {
4164 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4165
4166 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4167 KnownBits Carry(1);
4168 if (Opcode == ISD::ADDE)
4169 // Can't track carry from glue, set carry to unknown.
4170 Carry.resetAll();
4171 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4172 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4173 // Carry has bit width 1
4174 Carry = Carry.trunc(1);
4175 } else {
4176 Carry.setAllZero();
4177 }
4178
4179 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4180 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4181 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4182 break;
4183 }
4184 case ISD::UDIV: {
4185 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4186 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4187 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4188 break;
4189 }
4190 case ISD::SDIV: {
4191 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4192 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4193 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4194 break;
4195 }
4196 case ISD::SREM: {
4197 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4198 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4199 Known = KnownBits::srem(Known, Known2);
4200 break;
4201 }
4202 case ISD::UREM: {
4203 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4204 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4205 Known = KnownBits::urem(Known, Known2);
4206 break;
4207 }
4208 case ISD::EXTRACT_ELEMENT: {
4209 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4210 const unsigned Index = Op.getConstantOperandVal(1);
4211 const unsigned EltBitWidth = Op.getValueSizeInBits();
4212
4213 // Remove low part of known bits mask
4214 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4215 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4216
4217 // Remove high part of known bit mask
4218 Known = Known.trunc(EltBitWidth);
4219 break;
4220 }
4222 SDValue InVec = Op.getOperand(0);
4223 SDValue EltNo = Op.getOperand(1);
4224 EVT VecVT = InVec.getValueType();
4225 // computeKnownBits not yet implemented for scalable vectors.
4226 if (VecVT.isScalableVector())
4227 break;
4228 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4229 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4230
4231 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4232 // anything about the extended bits.
4233 if (BitWidth > EltBitWidth)
4234 Known = Known.trunc(EltBitWidth);
4235
4236 // If we know the element index, just demand that vector element, else for
4237 // an unknown element index, ignore DemandedElts and demand them all.
4238 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4239 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4240 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4241 DemandedSrcElts =
4242 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4243
4244 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4245 if (BitWidth > EltBitWidth)
4246 Known = Known.anyext(BitWidth);
4247 break;
4248 }
4250 if (Op.getValueType().isScalableVector())
4251 break;
4252
4253 // If we know the element index, split the demand between the
4254 // source vector and the inserted element, otherwise assume we need
4255 // the original demanded vector elements and the value.
4256 SDValue InVec = Op.getOperand(0);
4257 SDValue InVal = Op.getOperand(1);
4258 SDValue EltNo = Op.getOperand(2);
4259 bool DemandedVal = true;
4260 APInt DemandedVecElts = DemandedElts;
4261 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4262 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4263 unsigned EltIdx = CEltNo->getZExtValue();
4264 DemandedVal = !!DemandedElts[EltIdx];
4265 DemandedVecElts.clearBit(EltIdx);
4266 }
4267 Known.setAllConflict();
4268 if (DemandedVal) {
4269 Known2 = computeKnownBits(InVal, Depth + 1);
4270 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4271 }
4272 if (!!DemandedVecElts) {
4273 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4274 Known = Known.intersectWith(Known2);
4275 }
4276 break;
4277 }
4278 case ISD::BITREVERSE: {
4279 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4280 Known = Known2.reverseBits();
4281 break;
4282 }
4283 case ISD::BSWAP: {
4284 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4285 Known = Known2.byteSwap();
4286 break;
4287 }
4288 case ISD::ABS: {
4289 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4290 Known = Known2.abs();
4291 Known.Zero.setHighBits(
4292 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4293 break;
4294 }
4295 case ISD::USUBSAT: {
4296 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4297 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4298 Known = KnownBits::usub_sat(Known, Known2);
4299 break;
4300 }
4301 case ISD::UMIN: {
4302 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4303 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4304 Known = KnownBits::umin(Known, Known2);
4305 break;
4306 }
4307 case ISD::UMAX: {
4308 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4309 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4310 Known = KnownBits::umax(Known, Known2);
4311 break;
4312 }
4313 case ISD::SMIN:
4314 case ISD::SMAX: {
4315 // If we have a clamp pattern, we know that the number of sign bits will be
4316 // the minimum of the clamp min/max range.
4317 bool IsMax = (Opcode == ISD::SMAX);
4318 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4319 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4320 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4321 CstHigh =
4322 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4323 if (CstLow && CstHigh) {
4324 if (!IsMax)
4325 std::swap(CstLow, CstHigh);
4326
4327 const APInt &ValueLow = CstLow->getAPIntValue();
4328 const APInt &ValueHigh = CstHigh->getAPIntValue();
4329 if (ValueLow.sle(ValueHigh)) {
4330 unsigned LowSignBits = ValueLow.getNumSignBits();
4331 unsigned HighSignBits = ValueHigh.getNumSignBits();
4332 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4333 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4334 Known.One.setHighBits(MinSignBits);
4335 break;
4336 }
4337 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4338 Known.Zero.setHighBits(MinSignBits);
4339 break;
4340 }
4341 }
4342 }
4343
4344 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4345 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4346 if (IsMax)
4347 Known = KnownBits::smax(Known, Known2);
4348 else
4349 Known = KnownBits::smin(Known, Known2);
4350
4351 // For SMAX, if CstLow is non-negative we know the result will be
4352 // non-negative and thus all sign bits are 0.
4353 // TODO: There's an equivalent of this for smin with negative constant for
4354 // known ones.
4355 if (IsMax && CstLow) {
4356 const APInt &ValueLow = CstLow->getAPIntValue();
4357 if (ValueLow.isNonNegative()) {
4358 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4359 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4360 }
4361 }
4362
4363 break;
4364 }
4365 case ISD::UINT_TO_FP: {
4366 Known.makeNonNegative();
4367 break;
4368 }
4369 case ISD::SINT_TO_FP: {
4370 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4371 if (Known2.isNonNegative())
4372 Known.makeNonNegative();
4373 else if (Known2.isNegative())
4374 Known.makeNegative();
4375 break;
4376 }
4377 case ISD::FP_TO_UINT_SAT: {
4378 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4379 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4381 break;
4382 }
4383 case ISD::ATOMIC_LOAD: {
4384 // If we are looking at the loaded value.
4385 if (Op.getResNo() == 0) {
4386 auto *AT = cast<AtomicSDNode>(Op);
4387 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4388 KnownBits KnownScalarMemory(ScalarMemorySize);
4389 if (const MDNode *MD = AT->getRanges())
4390 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4391
4392 switch (AT->getExtensionType()) {
4393 case ISD::ZEXTLOAD:
4394 Known = KnownScalarMemory.zext(BitWidth);
4395 break;
4396 case ISD::SEXTLOAD:
4397 Known = KnownScalarMemory.sext(BitWidth);
4398 break;
4399 case ISD::EXTLOAD:
4400 switch (TLI->getExtendForAtomicOps()) {
4401 case ISD::ZERO_EXTEND:
4402 Known = KnownScalarMemory.zext(BitWidth);
4403 break;
4404 case ISD::SIGN_EXTEND:
4405 Known = KnownScalarMemory.sext(BitWidth);
4406 break;
4407 default:
4408 Known = KnownScalarMemory.anyext(BitWidth);
4409 break;
4410 }
4411 break;
4412 case ISD::NON_EXTLOAD:
4413 Known = KnownScalarMemory;
4414 break;
4415 }
4416 assert(Known.getBitWidth() == BitWidth);
4417 }
4418 break;
4419 }
4421 if (Op.getResNo() == 1) {
4422 // The boolean result conforms to getBooleanContents.
4423 // If we know the result of a setcc has the top bits zero, use this info.
4424 // We know that we have an integer-based boolean since these operations
4425 // are only available for integer.
4426 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4428 BitWidth > 1)
4429 Known.Zero.setBitsFrom(1);
4430 break;
4431 }
4432 [[fallthrough]];
4434 case ISD::ATOMIC_SWAP:
4445 case ISD::ATOMIC_LOAD_UMAX: {
4446 // If we are looking at the loaded value.
4447 if (Op.getResNo() == 0) {
4448 auto *AT = cast<AtomicSDNode>(Op);
4449 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4450
4451 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4452 Known.Zero.setBitsFrom(MemBits);
4453 }
4454 break;
4455 }
4456 case ISD::FrameIndex:
4458 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4459 Known, getMachineFunction());
4460 break;
4461
4462 default:
4463 if (Opcode < ISD::BUILTIN_OP_END)
4464 break;
4465 [[fallthrough]];
4469 // TODO: Probably okay to remove after audit; here to reduce change size
4470 // in initial enablement patch for scalable vectors
4471 if (Op.getValueType().isScalableVector())
4472 break;
4473
4474 // Allow the target to implement this method for its nodes.
4475 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4476 break;
4477 }
4478
4479 return Known;
4480}
4481
4482/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4495
4498 // X + 0 never overflow
4499 if (isNullConstant(N1))
4500 return OFK_Never;
4501
4502 // If both operands each have at least two sign bits, the addition
4503 // cannot overflow.
4504 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4505 return OFK_Never;
4506
4507 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4508 return OFK_Sometime;
4509}
4510
4513 // X + 0 never overflow
4514 if (isNullConstant(N1))
4515 return OFK_Never;
4516
4517 // mulhi + 1 never overflow
4518 KnownBits N1Known = computeKnownBits(N1);
4519 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4520 N1Known.getMaxValue().ult(2))
4521 return OFK_Never;
4522
4523 KnownBits N0Known = computeKnownBits(N0);
4524 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4525 N0Known.getMaxValue().ult(2))
4526 return OFK_Never;
4527
4528 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4529 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4530 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4531 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4532}
4533
4536 // X - 0 never overflow
4537 if (isNullConstant(N1))
4538 return OFK_Never;
4539
4540 // If both operands each have at least two sign bits, the subtraction
4541 // cannot overflow.
4542 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4543 return OFK_Never;
4544
4545 KnownBits N0Known = computeKnownBits(N0);
4546 KnownBits N1Known = computeKnownBits(N1);
4547 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4548 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4549 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4550}
4551
4554 // X - 0 never overflow
4555 if (isNullConstant(N1))
4556 return OFK_Never;
4557
4558 KnownBits N0Known = computeKnownBits(N0);
4559 KnownBits N1Known = computeKnownBits(N1);
4560 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4561 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4562 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4563}
4564
4567 // X * 0 and X * 1 never overflow.
4568 if (isNullConstant(N1) || isOneConstant(N1))
4569 return OFK_Never;
4570
4571 KnownBits N0Known = computeKnownBits(N0);
4572 KnownBits N1Known = computeKnownBits(N1);
4573 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4574 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4575 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4576}
4577
4580 // X * 0 and X * 1 never overflow.
4581 if (isNullConstant(N1) || isOneConstant(N1))
4582 return OFK_Never;
4583
4584 // Get the size of the result.
4585 unsigned BitWidth = N0.getScalarValueSizeInBits();
4586
4587 // Sum of the sign bits.
4588 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4589
4590 // If we have enough sign bits, then there's no overflow.
4591 if (SignBits > BitWidth + 1)
4592 return OFK_Never;
4593
4594 if (SignBits == BitWidth + 1) {
4595 // The overflow occurs when the true multiplication of the
4596 // the operands is the minimum negative number.
4597 KnownBits N0Known = computeKnownBits(N0);
4598 KnownBits N1Known = computeKnownBits(N1);
4599 // If one of the operands is non-negative, then there's no
4600 // overflow.
4601 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4602 return OFK_Never;
4603 }
4604
4605 return OFK_Sometime;
4606}
4607
4609 if (Depth >= MaxRecursionDepth)
4610 return false; // Limit search depth.
4611
4612 EVT OpVT = Val.getValueType();
4613 unsigned BitWidth = OpVT.getScalarSizeInBits();
4614
4615 // Is the constant a known power of 2?
4617 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4618 }))
4619 return true;
4620
4621 // A left-shift of a constant one will have exactly one bit set because
4622 // shifting the bit off the end is undefined.
4623 if (Val.getOpcode() == ISD::SHL) {
4624 auto *C = isConstOrConstSplat(Val.getOperand(0));
4625 if (C && C->getAPIntValue() == 1)
4626 return true;
4627 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4628 isKnownNeverZero(Val, Depth);
4629 }
4630
4631 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4632 // one bit set.
4633 if (Val.getOpcode() == ISD::SRL) {
4634 auto *C = isConstOrConstSplat(Val.getOperand(0));
4635 if (C && C->getAPIntValue().isSignMask())
4636 return true;
4637 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4638 isKnownNeverZero(Val, Depth);
4639 }
4640
4641 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4642 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4643
4644 // Are all operands of a build vector constant powers of two?
4645 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4646 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4647 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4648 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4649 return false;
4650 }))
4651 return true;
4652
4653 // Is the operand of a splat vector a constant power of two?
4654 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4656 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4657 return true;
4658
4659 // vscale(power-of-two) is a power-of-two for some targets
4660 if (Val.getOpcode() == ISD::VSCALE &&
4661 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4663 return true;
4664
4665 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4666 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4667 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4669
4670 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4671 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4673
4674 // Looking for `x & -x` pattern:
4675 // If x == 0:
4676 // x & -x -> 0
4677 // If x != 0:
4678 // x & -x -> non-zero pow2
4679 // so if we find the pattern return whether we know `x` is non-zero.
4680 SDValue X;
4681 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4682 return isKnownNeverZero(X, Depth);
4683
4684 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4685 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4686
4687 // More could be done here, though the above checks are enough
4688 // to handle some common cases.
4689 return false;
4690}
4691
4693 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4694 return C1->getValueAPF().getExactLog2Abs() >= 0;
4695
4696 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4697 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4698
4699 return false;
4700}
4701
4703 EVT VT = Op.getValueType();
4704
4705 // Since the number of lanes in a scalable vector is unknown at compile time,
4706 // we track one bit which is implicitly broadcast to all lanes. This means
4707 // that all lanes in a scalable vector are considered demanded.
4708 APInt DemandedElts = VT.isFixedLengthVector()
4710 : APInt(1, 1);
4711 return ComputeNumSignBits(Op, DemandedElts, Depth);
4712}
4713
4714unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4715 unsigned Depth) const {
4716 EVT VT = Op.getValueType();
4717 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4718 unsigned VTBits = VT.getScalarSizeInBits();
4719 unsigned NumElts = DemandedElts.getBitWidth();
4720 unsigned Tmp, Tmp2;
4721 unsigned FirstAnswer = 1;
4722
4723 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4724 const APInt &Val = C->getAPIntValue();
4725 return Val.getNumSignBits();
4726 }
4727
4728 if (Depth >= MaxRecursionDepth)
4729 return 1; // Limit search depth.
4730
4731 if (!DemandedElts)
4732 return 1; // No demanded elts, better to assume we don't know anything.
4733
4734 unsigned Opcode = Op.getOpcode();
4735 switch (Opcode) {
4736 default: break;
4737 case ISD::AssertSext:
4738 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4739 return VTBits-Tmp+1;
4740 case ISD::AssertZext:
4741 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4742 return VTBits-Tmp;
4743 case ISD::FREEZE:
4744 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4745 /*PoisonOnly=*/false))
4746 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4747 break;
4748 case ISD::MERGE_VALUES:
4749 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4750 Depth + 1);
4751 case ISD::SPLAT_VECTOR: {
4752 // Check if the sign bits of source go down as far as the truncated value.
4753 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4754 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4755 if (NumSrcSignBits > (NumSrcBits - VTBits))
4756 return NumSrcSignBits - (NumSrcBits - VTBits);
4757 break;
4758 }
4759 case ISD::BUILD_VECTOR:
4760 assert(!VT.isScalableVector());
4761 Tmp = VTBits;
4762 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4763 if (!DemandedElts[i])
4764 continue;
4765
4766 SDValue SrcOp = Op.getOperand(i);
4767 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4768 // for constant nodes to ensure we only look at the sign bits.
4770 APInt T = C->getAPIntValue().trunc(VTBits);
4771 Tmp2 = T.getNumSignBits();
4772 } else {
4773 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4774
4775 if (SrcOp.getValueSizeInBits() != VTBits) {
4776 assert(SrcOp.getValueSizeInBits() > VTBits &&
4777 "Expected BUILD_VECTOR implicit truncation");
4778 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4779 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4780 }
4781 }
4782 Tmp = std::min(Tmp, Tmp2);
4783 }
4784 return Tmp;
4785
4786 case ISD::VECTOR_COMPRESS: {
4787 SDValue Vec = Op.getOperand(0);
4788 SDValue PassThru = Op.getOperand(2);
4789 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4790 if (Tmp == 1)
4791 return 1;
4792 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4793 Tmp = std::min(Tmp, Tmp2);
4794 return Tmp;
4795 }
4796
4797 case ISD::VECTOR_SHUFFLE: {
4798 // Collect the minimum number of sign bits that are shared by every vector
4799 // element referenced by the shuffle.
4800 APInt DemandedLHS, DemandedRHS;
4802 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4803 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4804 DemandedLHS, DemandedRHS))
4805 return 1;
4806
4807 Tmp = std::numeric_limits<unsigned>::max();
4808 if (!!DemandedLHS)
4809 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4810 if (!!DemandedRHS) {
4811 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4812 Tmp = std::min(Tmp, Tmp2);
4813 }
4814 // If we don't know anything, early out and try computeKnownBits fall-back.
4815 if (Tmp == 1)
4816 break;
4817 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4818 return Tmp;
4819 }
4820
4821 case ISD::BITCAST: {
4822 if (VT.isScalableVector())
4823 break;
4824 SDValue N0 = Op.getOperand(0);
4825 EVT SrcVT = N0.getValueType();
4826 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4827
4828 // Ignore bitcasts from unsupported types..
4829 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4830 break;
4831
4832 // Fast handling of 'identity' bitcasts.
4833 if (VTBits == SrcBits)
4834 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4835
4836 bool IsLE = getDataLayout().isLittleEndian();
4837
4838 // Bitcast 'large element' scalar/vector to 'small element' vector.
4839 if ((SrcBits % VTBits) == 0) {
4840 assert(VT.isVector() && "Expected bitcast to vector");
4841
4842 unsigned Scale = SrcBits / VTBits;
4843 APInt SrcDemandedElts =
4844 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4845
4846 // Fast case - sign splat can be simply split across the small elements.
4847 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4848 if (Tmp == SrcBits)
4849 return VTBits;
4850
4851 // Slow case - determine how far the sign extends into each sub-element.
4852 Tmp2 = VTBits;
4853 for (unsigned i = 0; i != NumElts; ++i)
4854 if (DemandedElts[i]) {
4855 unsigned SubOffset = i % Scale;
4856 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4857 SubOffset = SubOffset * VTBits;
4858 if (Tmp <= SubOffset)
4859 return 1;
4860 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4861 }
4862 return Tmp2;
4863 }
4864 break;
4865 }
4866
4868 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4869 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4870 return VTBits - Tmp + 1;
4871 case ISD::SIGN_EXTEND:
4872 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4873 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4875 // Max of the input and what this extends.
4876 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4877 Tmp = VTBits-Tmp+1;
4878 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4879 return std::max(Tmp, Tmp2);
4881 if (VT.isScalableVector())
4882 break;
4883 SDValue Src = Op.getOperand(0);
4884 EVT SrcVT = Src.getValueType();
4885 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4886 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4887 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4888 }
4889 case ISD::SRA:
4890 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4891 // SRA X, C -> adds C sign bits.
4892 if (std::optional<unsigned> ShAmt =
4893 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4894 Tmp = std::min(Tmp + *ShAmt, VTBits);
4895 return Tmp;
4896 case ISD::SHL:
4897 if (std::optional<ConstantRange> ShAmtRange =
4898 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4899 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4900 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4901 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4902 // shifted out, then we can compute the number of sign bits for the
4903 // operand being extended. A future improvement could be to pass along the
4904 // "shifted left by" information in the recursive calls to
4905 // ComputeKnownSignBits. Allowing us to handle this more generically.
4906 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4907 SDValue Ext = Op.getOperand(0);
4908 EVT ExtVT = Ext.getValueType();
4909 SDValue Extendee = Ext.getOperand(0);
4910 EVT ExtendeeVT = Extendee.getValueType();
4911 unsigned SizeDifference =
4912 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4913 if (SizeDifference <= MinShAmt) {
4914 Tmp = SizeDifference +
4915 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4916 if (MaxShAmt < Tmp)
4917 return Tmp - MaxShAmt;
4918 }
4919 }
4920 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4921 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4922 if (MaxShAmt < Tmp)
4923 return Tmp - MaxShAmt;
4924 }
4925 break;
4926 case ISD::AND:
4927 case ISD::OR:
4928 case ISD::XOR: // NOT is handled here.
4929 // Logical binary ops preserve the number of sign bits at the worst.
4930 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4931 if (Tmp != 1) {
4932 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4933 FirstAnswer = std::min(Tmp, Tmp2);
4934 // We computed what we know about the sign bits as our first
4935 // answer. Now proceed to the generic code that uses
4936 // computeKnownBits, and pick whichever answer is better.
4937 }
4938 break;
4939
4940 case ISD::SELECT:
4941 case ISD::VSELECT:
4942 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4943 if (Tmp == 1) return 1; // Early out.
4944 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4945 return std::min(Tmp, Tmp2);
4946 case ISD::SELECT_CC:
4947 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4948 if (Tmp == 1) return 1; // Early out.
4949 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4950 return std::min(Tmp, Tmp2);
4951
4952 case ISD::SMIN:
4953 case ISD::SMAX: {
4954 // If we have a clamp pattern, we know that the number of sign bits will be
4955 // the minimum of the clamp min/max range.
4956 bool IsMax = (Opcode == ISD::SMAX);
4957 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4958 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4959 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4960 CstHigh =
4961 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4962 if (CstLow && CstHigh) {
4963 if (!IsMax)
4964 std::swap(CstLow, CstHigh);
4965 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4966 Tmp = CstLow->getAPIntValue().getNumSignBits();
4967 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4968 return std::min(Tmp, Tmp2);
4969 }
4970 }
4971
4972 // Fallback - just get the minimum number of sign bits of the operands.
4973 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4974 if (Tmp == 1)
4975 return 1; // Early out.
4976 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4977 return std::min(Tmp, Tmp2);
4978 }
4979 case ISD::UMIN:
4980 case ISD::UMAX:
4981 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4982 if (Tmp == 1)
4983 return 1; // Early out.
4984 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4985 return std::min(Tmp, Tmp2);
4986 case ISD::SSUBO_CARRY:
4987 case ISD::USUBO_CARRY:
4988 // sub_carry(x,x,c) -> 0/-1 (sext carry)
4989 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
4990 return VTBits;
4991 [[fallthrough]];
4992 case ISD::SADDO:
4993 case ISD::UADDO:
4994 case ISD::SADDO_CARRY:
4995 case ISD::UADDO_CARRY:
4996 case ISD::SSUBO:
4997 case ISD::USUBO:
4998 case ISD::SMULO:
4999 case ISD::UMULO:
5000 if (Op.getResNo() != 1)
5001 break;
5002 // The boolean result conforms to getBooleanContents. Fall through.
5003 // If setcc returns 0/-1, all bits are sign bits.
5004 // We know that we have an integer-based boolean since these operations
5005 // are only available for integer.
5006 if (TLI->getBooleanContents(VT.isVector(), false) ==
5008 return VTBits;
5009 break;
5010 case ISD::SETCC:
5011 case ISD::SETCCCARRY:
5012 case ISD::STRICT_FSETCC:
5013 case ISD::STRICT_FSETCCS: {
5014 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5015 // If setcc returns 0/-1, all bits are sign bits.
5016 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5018 return VTBits;
5019 break;
5020 }
5021 case ISD::ROTL:
5022 case ISD::ROTR:
5023 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5024
5025 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5026 if (Tmp == VTBits)
5027 return VTBits;
5028
5029 if (ConstantSDNode *C =
5030 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5031 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5032
5033 // Handle rotate right by N like a rotate left by 32-N.
5034 if (Opcode == ISD::ROTR)
5035 RotAmt = (VTBits - RotAmt) % VTBits;
5036
5037 // If we aren't rotating out all of the known-in sign bits, return the
5038 // number that are left. This handles rotl(sext(x), 1) for example.
5039 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5040 }
5041 break;
5042 case ISD::ADD:
5043 case ISD::ADDC:
5044 // TODO: Move Operand 1 check before Operand 0 check
5045 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5046 if (Tmp == 1) return 1; // Early out.
5047
5048 // Special case decrementing a value (ADD X, -1):
5049 if (ConstantSDNode *CRHS =
5050 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5051 if (CRHS->isAllOnes()) {
5052 KnownBits Known =
5053 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5054
5055 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5056 // sign bits set.
5057 if ((Known.Zero | 1).isAllOnes())
5058 return VTBits;
5059
5060 // If we are subtracting one from a positive number, there is no carry
5061 // out of the result.
5062 if (Known.isNonNegative())
5063 return Tmp;
5064 }
5065
5066 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5067 if (Tmp2 == 1) return 1; // Early out.
5068
5069 // Add can have at most one carry bit. Thus we know that the output
5070 // is, at worst, one more bit than the inputs.
5071 return std::min(Tmp, Tmp2) - 1;
5072 case ISD::SUB:
5073 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5074 if (Tmp2 == 1) return 1; // Early out.
5075
5076 // Handle NEG.
5077 if (ConstantSDNode *CLHS =
5078 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5079 if (CLHS->isZero()) {
5080 KnownBits Known =
5081 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5082 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5083 // sign bits set.
5084 if ((Known.Zero | 1).isAllOnes())
5085 return VTBits;
5086
5087 // If the input is known to be positive (the sign bit is known clear),
5088 // the output of the NEG has the same number of sign bits as the input.
5089 if (Known.isNonNegative())
5090 return Tmp2;
5091
5092 // Otherwise, we treat this like a SUB.
5093 }
5094
5095 // Sub can have at most one carry bit. Thus we know that the output
5096 // is, at worst, one more bit than the inputs.
5097 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5098 if (Tmp == 1) return 1; // Early out.
5099 return std::min(Tmp, Tmp2) - 1;
5100 case ISD::MUL: {
5101 // The output of the Mul can be at most twice the valid bits in the inputs.
5102 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5103 if (SignBitsOp0 == 1)
5104 break;
5105 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5106 if (SignBitsOp1 == 1)
5107 break;
5108 unsigned OutValidBits =
5109 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5110 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5111 }
5112 case ISD::AVGCEILS:
5113 case ISD::AVGFLOORS:
5114 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5115 if (Tmp == 1)
5116 return 1; // Early out.
5117 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5118 return std::min(Tmp, Tmp2);
5119 case ISD::SREM:
5120 // The sign bit is the LHS's sign bit, except when the result of the
5121 // remainder is zero. The magnitude of the result should be less than or
5122 // equal to the magnitude of the LHS. Therefore, the result should have
5123 // at least as many sign bits as the left hand side.
5124 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5125 case ISD::TRUNCATE: {
5126 // Check if the sign bits of source go down as far as the truncated value.
5127 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5128 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5129 if (NumSrcSignBits > (NumSrcBits - VTBits))
5130 return NumSrcSignBits - (NumSrcBits - VTBits);
5131 break;
5132 }
5133 case ISD::EXTRACT_ELEMENT: {
5134 if (VT.isScalableVector())
5135 break;
5136 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5137 const int BitWidth = Op.getValueSizeInBits();
5138 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5139
5140 // Get reverse index (starting from 1), Op1 value indexes elements from
5141 // little end. Sign starts at big end.
5142 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5143
5144 // If the sign portion ends in our element the subtraction gives correct
5145 // result. Otherwise it gives either negative or > bitwidth result
5146 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5147 }
5149 if (VT.isScalableVector())
5150 break;
5151 // If we know the element index, split the demand between the
5152 // source vector and the inserted element, otherwise assume we need
5153 // the original demanded vector elements and the value.
5154 SDValue InVec = Op.getOperand(0);
5155 SDValue InVal = Op.getOperand(1);
5156 SDValue EltNo = Op.getOperand(2);
5157 bool DemandedVal = true;
5158 APInt DemandedVecElts = DemandedElts;
5159 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5160 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5161 unsigned EltIdx = CEltNo->getZExtValue();
5162 DemandedVal = !!DemandedElts[EltIdx];
5163 DemandedVecElts.clearBit(EltIdx);
5164 }
5165 Tmp = std::numeric_limits<unsigned>::max();
5166 if (DemandedVal) {
5167 // TODO - handle implicit truncation of inserted elements.
5168 if (InVal.getScalarValueSizeInBits() != VTBits)
5169 break;
5170 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5171 Tmp = std::min(Tmp, Tmp2);
5172 }
5173 if (!!DemandedVecElts) {
5174 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5175 Tmp = std::min(Tmp, Tmp2);
5176 }
5177 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5178 return Tmp;
5179 }
5181 SDValue InVec = Op.getOperand(0);
5182 SDValue EltNo = Op.getOperand(1);
5183 EVT VecVT = InVec.getValueType();
5184 // ComputeNumSignBits not yet implemented for scalable vectors.
5185 if (VecVT.isScalableVector())
5186 break;
5187 const unsigned BitWidth = Op.getValueSizeInBits();
5188 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5189 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5190
5191 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5192 // anything about sign bits. But if the sizes match we can derive knowledge
5193 // about sign bits from the vector operand.
5194 if (BitWidth != EltBitWidth)
5195 break;
5196
5197 // If we know the element index, just demand that vector element, else for
5198 // an unknown element index, ignore DemandedElts and demand them all.
5199 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5200 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5201 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5202 DemandedSrcElts =
5203 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5204
5205 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5206 }
5208 // Offset the demanded elts by the subvector index.
5209 SDValue Src = Op.getOperand(0);
5210 // Bail until we can represent demanded elements for scalable vectors.
5211 if (Src.getValueType().isScalableVector())
5212 break;
5213 uint64_t Idx = Op.getConstantOperandVal(1);
5214 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5215 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5216 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5217 }
5218 case ISD::CONCAT_VECTORS: {
5219 if (VT.isScalableVector())
5220 break;
5221 // Determine the minimum number of sign bits across all demanded
5222 // elts of the input vectors. Early out if the result is already 1.
5223 Tmp = std::numeric_limits<unsigned>::max();
5224 EVT SubVectorVT = Op.getOperand(0).getValueType();
5225 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5226 unsigned NumSubVectors = Op.getNumOperands();
5227 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5228 APInt DemandedSub =
5229 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5230 if (!DemandedSub)
5231 continue;
5232 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5233 Tmp = std::min(Tmp, Tmp2);
5234 }
5235 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5236 return Tmp;
5237 }
5238 case ISD::INSERT_SUBVECTOR: {
5239 if (VT.isScalableVector())
5240 break;
5241 // Demand any elements from the subvector and the remainder from the src its
5242 // inserted into.
5243 SDValue Src = Op.getOperand(0);
5244 SDValue Sub = Op.getOperand(1);
5245 uint64_t Idx = Op.getConstantOperandVal(2);
5246 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5247 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5248 APInt DemandedSrcElts = DemandedElts;
5249 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5250
5251 Tmp = std::numeric_limits<unsigned>::max();
5252 if (!!DemandedSubElts) {
5253 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5254 if (Tmp == 1)
5255 return 1; // early-out
5256 }
5257 if (!!DemandedSrcElts) {
5258 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5259 Tmp = std::min(Tmp, Tmp2);
5260 }
5261 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5262 return Tmp;
5263 }
5264 case ISD::LOAD: {
5265 // If we are looking at the loaded value of the SDNode.
5266 if (Op.getResNo() != 0)
5267 break;
5268
5270 if (const MDNode *Ranges = LD->getRanges()) {
5271 if (DemandedElts != 1)
5272 break;
5273
5275 if (VTBits > CR.getBitWidth()) {
5276 switch (LD->getExtensionType()) {
5277 case ISD::SEXTLOAD:
5278 CR = CR.signExtend(VTBits);
5279 break;
5280 case ISD::ZEXTLOAD:
5281 CR = CR.zeroExtend(VTBits);
5282 break;
5283 default:
5284 break;
5285 }
5286 }
5287
5288 if (VTBits != CR.getBitWidth())
5289 break;
5290 return std::min(CR.getSignedMin().getNumSignBits(),
5292 }
5293
5294 unsigned ExtType = LD->getExtensionType();
5295 switch (ExtType) {
5296 default:
5297 break;
5298 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5299 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5300 return VTBits - Tmp + 1;
5301 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5302 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5303 return VTBits - Tmp;
5304 case ISD::NON_EXTLOAD:
5305 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5306 // We only need to handle vectors - computeKnownBits should handle
5307 // scalar cases.
5308 Type *CstTy = Cst->getType();
5309 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5310 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5311 VTBits == CstTy->getScalarSizeInBits()) {
5312 Tmp = VTBits;
5313 for (unsigned i = 0; i != NumElts; ++i) {
5314 if (!DemandedElts[i])
5315 continue;
5316 if (Constant *Elt = Cst->getAggregateElement(i)) {
5317 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5318 const APInt &Value = CInt->getValue();
5319 Tmp = std::min(Tmp, Value.getNumSignBits());
5320 continue;
5321 }
5322 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5323 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5324 Tmp = std::min(Tmp, Value.getNumSignBits());
5325 continue;
5326 }
5327 }
5328 // Unknown type. Conservatively assume no bits match sign bit.
5329 return 1;
5330 }
5331 return Tmp;
5332 }
5333 }
5334 break;
5335 }
5336
5337 break;
5338 }
5341 case ISD::ATOMIC_SWAP:
5353 case ISD::ATOMIC_LOAD: {
5354 auto *AT = cast<AtomicSDNode>(Op);
5355 // If we are looking at the loaded value.
5356 if (Op.getResNo() == 0) {
5357 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5358 if (Tmp == VTBits)
5359 return 1; // early-out
5360
5361 // For atomic_load, prefer to use the extension type.
5362 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5363 switch (AT->getExtensionType()) {
5364 default:
5365 break;
5366 case ISD::SEXTLOAD:
5367 return VTBits - Tmp + 1;
5368 case ISD::ZEXTLOAD:
5369 return VTBits - Tmp;
5370 }
5371 }
5372
5373 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5374 return VTBits - Tmp + 1;
5375 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5376 return VTBits - Tmp;
5377 }
5378 break;
5379 }
5380 }
5381
5382 // Allow the target to implement this method for its nodes.
5383 if (Opcode >= ISD::BUILTIN_OP_END ||
5384 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5385 Opcode == ISD::INTRINSIC_W_CHAIN ||
5386 Opcode == ISD::INTRINSIC_VOID) {
5387 // TODO: This can probably be removed once target code is audited. This
5388 // is here purely to reduce patch size and review complexity.
5389 if (!VT.isScalableVector()) {
5390 unsigned NumBits =
5391 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5392 if (NumBits > 1)
5393 FirstAnswer = std::max(FirstAnswer, NumBits);
5394 }
5395 }
5396
5397 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5398 // use this information.
5399 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5400 return std::max(FirstAnswer, Known.countMinSignBits());
5401}
5402
5404 unsigned Depth) const {
5405 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5406 return Op.getScalarValueSizeInBits() - SignBits + 1;
5407}
5408
5410 const APInt &DemandedElts,
5411 unsigned Depth) const {
5412 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5413 return Op.getScalarValueSizeInBits() - SignBits + 1;
5414}
5415
5417 unsigned Depth) const {
5418 // Early out for FREEZE.
5419 if (Op.getOpcode() == ISD::FREEZE)
5420 return true;
5421
5422 EVT VT = Op.getValueType();
5423 APInt DemandedElts = VT.isFixedLengthVector()
5425 : APInt(1, 1);
5426 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5427}
5428
5430 const APInt &DemandedElts,
5431 bool PoisonOnly,
5432 unsigned Depth) const {
5433 unsigned Opcode = Op.getOpcode();
5434
5435 // Early out for FREEZE.
5436 if (Opcode == ISD::FREEZE)
5437 return true;
5438
5439 if (Depth >= MaxRecursionDepth)
5440 return false; // Limit search depth.
5441
5442 if (isIntOrFPConstant(Op))
5443 return true;
5444
5445 switch (Opcode) {
5446 case ISD::CONDCODE:
5447 case ISD::VALUETYPE:
5448 case ISD::FrameIndex:
5450 case ISD::CopyFromReg:
5451 return true;
5452
5453 case ISD::POISON:
5454 return false;
5455
5456 case ISD::UNDEF:
5457 return PoisonOnly;
5458
5459 case ISD::BUILD_VECTOR:
5460 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5461 // this shouldn't affect the result.
5462 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5463 if (!DemandedElts[i])
5464 continue;
5466 Depth + 1))
5467 return false;
5468 }
5469 return true;
5470
5472 SDValue Src = Op.getOperand(0);
5473 if (Src.getValueType().isScalableVector())
5474 break;
5475 uint64_t Idx = Op.getConstantOperandVal(1);
5476 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5477 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5478 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5479 Depth + 1);
5480 }
5481
5482 case ISD::INSERT_SUBVECTOR: {
5483 if (Op.getValueType().isScalableVector())
5484 break;
5485 SDValue Src = Op.getOperand(0);
5486 SDValue Sub = Op.getOperand(1);
5487 uint64_t Idx = Op.getConstantOperandVal(2);
5488 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5489 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5490 APInt DemandedSrcElts = DemandedElts;
5491 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5492
5493 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5494 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5495 return false;
5496 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5497 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5498 return false;
5499 return true;
5500 }
5501
5503 SDValue Src = Op.getOperand(0);
5504 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5505 EVT SrcVT = Src.getValueType();
5506 if (SrcVT.isFixedLengthVector() && IndexC &&
5507 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5508 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5509 IndexC->getZExtValue());
5510 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5511 Depth + 1);
5512 }
5513 break;
5514 }
5515
5517 SDValue InVec = Op.getOperand(0);
5518 SDValue InVal = Op.getOperand(1);
5519 SDValue EltNo = Op.getOperand(2);
5520 EVT VT = InVec.getValueType();
5521 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5522 if (IndexC && VT.isFixedLengthVector() &&
5523 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5524 if (DemandedElts[IndexC->getZExtValue()] &&
5526 return false;
5527 APInt InVecDemandedElts = DemandedElts;
5528 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5529 if (!!InVecDemandedElts &&
5531 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5532 InVecDemandedElts, PoisonOnly, Depth + 1))
5533 return false;
5534 return true;
5535 }
5536 break;
5537 }
5538
5540 // Check upper (known undef) elements.
5541 if (DemandedElts.ugt(1) && !PoisonOnly)
5542 return false;
5543 // Check element zero.
5544 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5545 Op.getOperand(0), PoisonOnly, Depth + 1))
5546 return false;
5547 return true;
5548
5549 case ISD::SPLAT_VECTOR:
5550 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5551 Depth + 1);
5552
5553 case ISD::VECTOR_SHUFFLE: {
5554 APInt DemandedLHS, DemandedRHS;
5555 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5556 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5557 DemandedElts, DemandedLHS, DemandedRHS,
5558 /*AllowUndefElts=*/false))
5559 return false;
5560 if (!DemandedLHS.isZero() &&
5561 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5562 PoisonOnly, Depth + 1))
5563 return false;
5564 if (!DemandedRHS.isZero() &&
5565 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5566 PoisonOnly, Depth + 1))
5567 return false;
5568 return true;
5569 }
5570
5571 case ISD::SHL:
5572 case ISD::SRL:
5573 case ISD::SRA:
5574 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5575 // enough to check operand 0 if Op can't create undef/poison.
5576 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5577 /*ConsiderFlags*/ true, Depth) &&
5578 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5579 PoisonOnly, Depth + 1);
5580
5581 case ISD::BSWAP:
5582 case ISD::CTPOP:
5583 case ISD::BITREVERSE:
5584 case ISD::AND:
5585 case ISD::OR:
5586 case ISD::XOR:
5587 case ISD::ADD:
5588 case ISD::SUB:
5589 case ISD::MUL:
5590 case ISD::SADDSAT:
5591 case ISD::UADDSAT:
5592 case ISD::SSUBSAT:
5593 case ISD::USUBSAT:
5594 case ISD::SSHLSAT:
5595 case ISD::USHLSAT:
5596 case ISD::SMIN:
5597 case ISD::SMAX:
5598 case ISD::UMIN:
5599 case ISD::UMAX:
5600 case ISD::ZERO_EXTEND:
5601 case ISD::SIGN_EXTEND:
5602 case ISD::ANY_EXTEND:
5603 case ISD::TRUNCATE:
5604 case ISD::VSELECT: {
5605 // If Op can't create undef/poison and none of its operands are undef/poison
5606 // then Op is never undef/poison. A difference from the more common check
5607 // below, outside the switch, is that we handle elementwise operations for
5608 // which the DemandedElts mask is valid for all operands here.
5609 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5610 /*ConsiderFlags*/ true, Depth) &&
5611 all_of(Op->ops(), [&](SDValue V) {
5612 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5613 PoisonOnly, Depth + 1);
5614 });
5615 }
5616
5617 // TODO: Search for noundef attributes from library functions.
5618
5619 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5620
5621 default:
5622 // Allow the target to implement this method for its nodes.
5623 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5624 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5625 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5626 Op, DemandedElts, *this, PoisonOnly, Depth);
5627 break;
5628 }
5629
5630 // If Op can't create undef/poison and none of its operands are undef/poison
5631 // then Op is never undef/poison.
5632 // NOTE: TargetNodes can handle this in themselves in
5633 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5634 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5635 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5636 Depth) &&
5637 all_of(Op->ops(), [&](SDValue V) {
5638 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5639 });
5640}
5641
5643 bool ConsiderFlags,
5644 unsigned Depth) const {
5645 EVT VT = Op.getValueType();
5646 APInt DemandedElts = VT.isFixedLengthVector()
5648 : APInt(1, 1);
5649 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5650 Depth);
5651}
5652
5654 bool PoisonOnly, bool ConsiderFlags,
5655 unsigned Depth) const {
5656 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5657 return true;
5658
5659 unsigned Opcode = Op.getOpcode();
5660 switch (Opcode) {
5661 case ISD::AssertSext:
5662 case ISD::AssertZext:
5663 case ISD::AssertAlign:
5665 // Assertion nodes can create poison if the assertion fails.
5666 return true;
5667
5668 case ISD::FREEZE:
5672 case ISD::SADDSAT:
5673 case ISD::UADDSAT:
5674 case ISD::SSUBSAT:
5675 case ISD::USUBSAT:
5676 case ISD::MULHU:
5677 case ISD::MULHS:
5678 case ISD::AVGFLOORS:
5679 case ISD::AVGFLOORU:
5680 case ISD::AVGCEILS:
5681 case ISD::AVGCEILU:
5682 case ISD::ABDU:
5683 case ISD::ABDS:
5684 case ISD::SMIN:
5685 case ISD::SMAX:
5686 case ISD::SCMP:
5687 case ISD::UMIN:
5688 case ISD::UMAX:
5689 case ISD::UCMP:
5690 case ISD::AND:
5691 case ISD::XOR:
5692 case ISD::ROTL:
5693 case ISD::ROTR:
5694 case ISD::FSHL:
5695 case ISD::FSHR:
5696 case ISD::BSWAP:
5697 case ISD::CTTZ:
5698 case ISD::CTLZ:
5699 case ISD::CTLS:
5700 case ISD::CTPOP:
5701 case ISD::BITREVERSE:
5702 case ISD::PARITY:
5703 case ISD::SIGN_EXTEND:
5704 case ISD::TRUNCATE:
5708 case ISD::BITCAST:
5709 case ISD::BUILD_VECTOR:
5710 case ISD::BUILD_PAIR:
5711 case ISD::SPLAT_VECTOR:
5712 case ISD::FABS:
5713 return false;
5714
5715 case ISD::ABS:
5716 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5717 // Different to Intrinsic::abs.
5718 return false;
5719
5720 case ISD::ADDC:
5721 case ISD::SUBC:
5722 case ISD::ADDE:
5723 case ISD::SUBE:
5724 case ISD::SADDO:
5725 case ISD::SSUBO:
5726 case ISD::SMULO:
5727 case ISD::SADDO_CARRY:
5728 case ISD::SSUBO_CARRY:
5729 case ISD::UADDO:
5730 case ISD::USUBO:
5731 case ISD::UMULO:
5732 case ISD::UADDO_CARRY:
5733 case ISD::USUBO_CARRY:
5734 // No poison on result or overflow flags.
5735 return false;
5736
5737 case ISD::SELECT_CC:
5738 case ISD::SETCC: {
5739 // Integer setcc cannot create undef or poison.
5740 if (Op.getOperand(0).getValueType().isInteger())
5741 return false;
5742
5743 // FP compares are more complicated. They can create poison for nan/infinity
5744 // based on options and flags. The options and flags also cause special
5745 // nonan condition codes to be used. Those condition codes may be preserved
5746 // even if the nonan flag is dropped somewhere.
5747 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5748 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5749 return (unsigned)CCCode & 0x10U;
5750 }
5751
5752 case ISD::OR:
5753 case ISD::ZERO_EXTEND:
5754 case ISD::SELECT:
5755 case ISD::VSELECT:
5756 case ISD::ADD:
5757 case ISD::SUB:
5758 case ISD::MUL:
5759 case ISD::FNEG:
5760 case ISD::FADD:
5761 case ISD::FSUB:
5762 case ISD::FMUL:
5763 case ISD::FDIV:
5764 case ISD::FREM:
5765 case ISD::FCOPYSIGN:
5766 case ISD::FMA:
5767 case ISD::FMAD:
5768 case ISD::FMULADD:
5769 case ISD::FP_EXTEND:
5775 // No poison except from flags (which is handled above)
5776 return false;
5777
5778 case ISD::SHL:
5779 case ISD::SRL:
5780 case ISD::SRA:
5781 // If the max shift amount isn't in range, then the shift can
5782 // create poison.
5783 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5784
5787 // If the amount is zero then the result will be poison.
5788 // TODO: Add isKnownNeverZero DemandedElts handling.
5789 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5790
5792 // Check if we demand any upper (undef) elements.
5793 return !PoisonOnly && DemandedElts.ugt(1);
5794
5797 // Ensure that the element index is in bounds.
5798 EVT VecVT = Op.getOperand(0).getValueType();
5799 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5800 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5801 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5802 }
5803
5804 case ISD::VECTOR_SHUFFLE: {
5805 // Check for any demanded shuffle element that is undef.
5806 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5807 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5808 if (Elt < 0 && DemandedElts[Idx])
5809 return true;
5810 return false;
5811 }
5812
5814 return false;
5815
5816 default:
5817 // Allow the target to implement this method for its nodes.
5818 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5819 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5820 return TLI->canCreateUndefOrPoisonForTargetNode(
5821 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5822 break;
5823 }
5824
5825 // Be conservative and return true.
5826 return true;
5827}
5828
5829bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5830 unsigned Opcode = Op.getOpcode();
5831 if (Opcode == ISD::OR)
5832 return Op->getFlags().hasDisjoint() ||
5833 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5834 if (Opcode == ISD::XOR)
5835 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5836 return false;
5837}
5838
5840 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5841 (Op.isAnyAdd() || isADDLike(Op));
5842}
5843
5845 unsigned Depth) const {
5846 EVT VT = Op.getValueType();
5847
5848 // Since the number of lanes in a scalable vector is unknown at compile time,
5849 // we track one bit which is implicitly broadcast to all lanes. This means
5850 // that all lanes in a scalable vector are considered demanded.
5851 APInt DemandedElts = VT.isFixedLengthVector()
5853 : APInt(1, 1);
5854
5855 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5856}
5857
5859 bool SNaN, unsigned Depth) const {
5860 assert(!DemandedElts.isZero() && "No demanded elements");
5861
5862 // If we're told that NaNs won't happen, assume they won't.
5863 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5864 return true;
5865
5866 if (Depth >= MaxRecursionDepth)
5867 return false; // Limit search depth.
5868
5869 // If the value is a constant, we can obviously see if it is a NaN or not.
5871 return !C->getValueAPF().isNaN() ||
5872 (SNaN && !C->getValueAPF().isSignaling());
5873 }
5874
5875 unsigned Opcode = Op.getOpcode();
5876 switch (Opcode) {
5877 case ISD::FADD:
5878 case ISD::FSUB:
5879 case ISD::FMUL:
5880 case ISD::FDIV:
5881 case ISD::FREM:
5882 case ISD::FSIN:
5883 case ISD::FCOS:
5884 case ISD::FTAN:
5885 case ISD::FASIN:
5886 case ISD::FACOS:
5887 case ISD::FATAN:
5888 case ISD::FATAN2:
5889 case ISD::FSINH:
5890 case ISD::FCOSH:
5891 case ISD::FTANH:
5892 case ISD::FMA:
5893 case ISD::FMULADD:
5894 case ISD::FMAD: {
5895 if (SNaN)
5896 return true;
5897 // TODO: Need isKnownNeverInfinity
5898 return false;
5899 }
5900 case ISD::FCANONICALIZE:
5901 case ISD::FEXP:
5902 case ISD::FEXP2:
5903 case ISD::FEXP10:
5904 case ISD::FTRUNC:
5905 case ISD::FFLOOR:
5906 case ISD::FCEIL:
5907 case ISD::FROUND:
5908 case ISD::FROUNDEVEN:
5909 case ISD::LROUND:
5910 case ISD::LLROUND:
5911 case ISD::FRINT:
5912 case ISD::LRINT:
5913 case ISD::LLRINT:
5914 case ISD::FNEARBYINT:
5915 case ISD::FLDEXP: {
5916 if (SNaN)
5917 return true;
5918 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5919 }
5920 case ISD::FABS:
5921 case ISD::FNEG:
5922 case ISD::FCOPYSIGN: {
5923 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5924 }
5925 case ISD::SELECT:
5926 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5927 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5928 case ISD::FP_EXTEND:
5929 case ISD::FP_ROUND: {
5930 if (SNaN)
5931 return true;
5932 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5933 }
5934 case ISD::SINT_TO_FP:
5935 case ISD::UINT_TO_FP:
5936 return true;
5937 case ISD::FSQRT: // Need is known positive
5938 case ISD::FLOG:
5939 case ISD::FLOG2:
5940 case ISD::FLOG10:
5941 case ISD::FPOWI:
5942 case ISD::FPOW: {
5943 if (SNaN)
5944 return true;
5945 // TODO: Refine on operand
5946 return false;
5947 }
5948 case ISD::FMINNUM:
5949 case ISD::FMAXNUM:
5950 case ISD::FMINIMUMNUM:
5951 case ISD::FMAXIMUMNUM: {
5952 // Only one needs to be known not-nan, since it will be returned if the
5953 // other ends up being one.
5954 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5955 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5956 }
5957 case ISD::FMINNUM_IEEE:
5958 case ISD::FMAXNUM_IEEE: {
5959 if (SNaN)
5960 return true;
5961 // This can return a NaN if either operand is an sNaN, or if both operands
5962 // are NaN.
5963 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5964 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5965 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5966 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5967 }
5968 case ISD::FMINIMUM:
5969 case ISD::FMAXIMUM: {
5970 // TODO: Does this quiet or return the origina NaN as-is?
5971 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5972 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5973 }
5975 SDValue Src = Op.getOperand(0);
5976 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5977 EVT SrcVT = Src.getValueType();
5978 if (SrcVT.isFixedLengthVector() && Idx &&
5979 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5980 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5981 Idx->getZExtValue());
5982 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5983 }
5984 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5985 }
5987 SDValue Src = Op.getOperand(0);
5988 if (Src.getValueType().isFixedLengthVector()) {
5989 unsigned Idx = Op.getConstantOperandVal(1);
5990 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5991 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5992 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
5993 }
5994 return isKnownNeverNaN(Src, SNaN, Depth + 1);
5995 }
5996 case ISD::INSERT_SUBVECTOR: {
5997 SDValue BaseVector = Op.getOperand(0);
5998 SDValue SubVector = Op.getOperand(1);
5999 EVT BaseVectorVT = BaseVector.getValueType();
6000 if (BaseVectorVT.isFixedLengthVector()) {
6001 unsigned Idx = Op.getConstantOperandVal(2);
6002 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6003 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6004
6005 // Clear/Extract the bits at the position where the subvector will be
6006 // inserted.
6007 APInt DemandedMask =
6008 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6009 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6010 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6011
6012 bool NeverNaN = true;
6013 if (!DemandedSrcElts.isZero())
6014 NeverNaN &=
6015 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6016 if (NeverNaN && !DemandedSubElts.isZero())
6017 NeverNaN &=
6018 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6019 return NeverNaN;
6020 }
6021 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6022 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6023 }
6024 case ISD::BUILD_VECTOR: {
6025 unsigned NumElts = Op.getNumOperands();
6026 for (unsigned I = 0; I != NumElts; ++I)
6027 if (DemandedElts[I] &&
6028 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6029 return false;
6030 return true;
6031 }
6032 case ISD::AssertNoFPClass: {
6033 FPClassTest NoFPClass =
6034 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6035 if ((NoFPClass & fcNan) == fcNan)
6036 return true;
6037 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6038 return true;
6039 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6040 }
6041 default:
6042 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6043 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6044 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6045 Depth);
6046 }
6047
6048 return false;
6049 }
6050}
6051
6053 assert(Op.getValueType().isFloatingPoint() &&
6054 "Floating point type expected");
6055
6056 // If the value is a constant, we can obviously see if it is a zero or not.
6058 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6059}
6060
6062 if (Depth >= MaxRecursionDepth)
6063 return false; // Limit search depth.
6064
6065 assert(!Op.getValueType().isFloatingPoint() &&
6066 "Floating point types unsupported - use isKnownNeverZeroFloat");
6067
6068 // If the value is a constant, we can obviously see if it is a zero or not.
6070 [](ConstantSDNode *C) { return !C->isZero(); }))
6071 return true;
6072
6073 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6074 // some degree.
6075 switch (Op.getOpcode()) {
6076 default:
6077 break;
6078
6079 case ISD::OR:
6080 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6081 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6082
6083 case ISD::VSELECT:
6084 case ISD::SELECT:
6085 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6086 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6087
6088 case ISD::SHL: {
6089 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6090 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6091 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6092 // 1 << X is never zero.
6093 if (ValKnown.One[0])
6094 return true;
6095 // If max shift cnt of known ones is non-zero, result is non-zero.
6096 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6097 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6098 !ValKnown.One.shl(MaxCnt).isZero())
6099 return true;
6100 break;
6101 }
6102 case ISD::UADDSAT:
6103 case ISD::UMAX:
6104 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6105 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6106
6107 // For smin/smax: If either operand is known negative/positive
6108 // respectively we don't need the other to be known at all.
6109 case ISD::SMAX: {
6110 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6111 if (Op1.isStrictlyPositive())
6112 return true;
6113
6114 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6115 if (Op0.isStrictlyPositive())
6116 return true;
6117
6118 if (Op1.isNonZero() && Op0.isNonZero())
6119 return true;
6120
6121 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6122 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6123 }
6124 case ISD::SMIN: {
6125 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6126 if (Op1.isNegative())
6127 return true;
6128
6129 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6130 if (Op0.isNegative())
6131 return true;
6132
6133 if (Op1.isNonZero() && Op0.isNonZero())
6134 return true;
6135
6136 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6137 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6138 }
6139 case ISD::UMIN:
6140 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6141 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6142
6143 case ISD::ROTL:
6144 case ISD::ROTR:
6145 case ISD::BITREVERSE:
6146 case ISD::BSWAP:
6147 case ISD::CTPOP:
6148 case ISD::ABS:
6149 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6150
6151 case ISD::SRA:
6152 case ISD::SRL: {
6153 if (Op->getFlags().hasExact())
6154 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6155 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6156 if (ValKnown.isNegative())
6157 return true;
6158 // If max shift cnt of known ones is non-zero, result is non-zero.
6159 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6160 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6161 !ValKnown.One.lshr(MaxCnt).isZero())
6162 return true;
6163 break;
6164 }
6165 case ISD::UDIV:
6166 case ISD::SDIV:
6167 // div exact can only produce a zero if the dividend is zero.
6168 // TODO: For udiv this is also true if Op1 u<= Op0
6169 if (Op->getFlags().hasExact())
6170 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6171 break;
6172
6173 case ISD::ADD:
6174 if (Op->getFlags().hasNoUnsignedWrap())
6175 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6176 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6177 return true;
6178 // TODO: There are a lot more cases we can prove for add.
6179 break;
6180
6181 case ISD::SUB: {
6182 if (isNullConstant(Op.getOperand(0)))
6183 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6184
6185 std::optional<bool> ne =
6186 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6187 computeKnownBits(Op.getOperand(1), Depth + 1));
6188 return ne && *ne;
6189 }
6190
6191 case ISD::MUL:
6192 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6193 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6194 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6195 return true;
6196 break;
6197
6198 case ISD::ZERO_EXTEND:
6199 case ISD::SIGN_EXTEND:
6200 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6201 case ISD::VSCALE: {
6203 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6204 ConstantRange CR =
6205 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6206 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6207 return true;
6208 break;
6209 }
6210 }
6211
6213}
6214
6216 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6217 return !C1->isNegative();
6218
6219 switch (Op.getOpcode()) {
6220 case ISD::FABS:
6221 case ISD::FEXP:
6222 case ISD::FEXP2:
6223 case ISD::FEXP10:
6224 return true;
6225 default:
6226 return false;
6227 }
6228
6229 llvm_unreachable("covered opcode switch");
6230}
6231
6233 assert(Use.getValueType().isFloatingPoint());
6234 const SDNode *User = Use.getUser();
6235 unsigned OperandNo = Use.getOperandNo();
6236 // Check if this use is insensitive to the sign of zero
6237 switch (User->getOpcode()) {
6238 case ISD::SETCC:
6239 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6240 case ISD::FABS:
6241 // fabs always produces +0.0.
6242 return true;
6243 case ISD::FCOPYSIGN:
6244 // copysign overwrites the sign bit of the first operand.
6245 return OperandNo == 0;
6246 case ISD::FADD:
6247 case ISD::FSUB: {
6248 // Arithmetic with non-zero constants fixes the uncertainty around the
6249 // sign bit.
6250 SDValue Other = User->getOperand(1 - OperandNo);
6252 }
6253 case ISD::FP_TO_SINT:
6254 case ISD::FP_TO_UINT:
6255 // fp-to-int conversions normalize signed zeros.
6256 return true;
6257 default:
6258 return false;
6259 }
6260}
6261
6263 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6264 // regression. Ideally, this should be implemented as a demanded-bits
6265 // optimization that stems from the users.
6266 if (Op->use_size() > 2)
6267 return false;
6268 return all_of(Op->uses(),
6269 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6270}
6271
6273 // Check the obvious case.
6274 if (A == B) return true;
6275
6276 // For negative and positive zero.
6279 if (CA->isZero() && CB->isZero()) return true;
6280
6281 // Otherwise they may not be equal.
6282 return false;
6283}
6284
6285// Only bits set in Mask must be negated, other bits may be arbitrary.
6287 if (isBitwiseNot(V, AllowUndefs))
6288 return V.getOperand(0);
6289
6290 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6291 // bits in the non-extended part.
6292 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6293 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6294 return SDValue();
6295 SDValue ExtArg = V.getOperand(0);
6296 if (ExtArg.getScalarValueSizeInBits() >=
6297 MaskC->getAPIntValue().getActiveBits() &&
6298 isBitwiseNot(ExtArg, AllowUndefs) &&
6299 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6300 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6301 return ExtArg.getOperand(0).getOperand(0);
6302 return SDValue();
6303}
6304
6306 // Match masked merge pattern (X & ~M) op (Y & M)
6307 // Including degenerate case (X & ~M) op M
6308 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6309 SDValue Other) {
6310 if (SDValue NotOperand =
6311 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6312 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6313 NotOperand->getOpcode() == ISD::TRUNCATE)
6314 NotOperand = NotOperand->getOperand(0);
6315
6316 if (Other == NotOperand)
6317 return true;
6318 if (Other->getOpcode() == ISD::AND)
6319 return NotOperand == Other->getOperand(0) ||
6320 NotOperand == Other->getOperand(1);
6321 }
6322 return false;
6323 };
6324
6325 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6326 A = A->getOperand(0);
6327
6328 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6329 B = B->getOperand(0);
6330
6331 if (A->getOpcode() == ISD::AND)
6332 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6333 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6334 return false;
6335}
6336
6337// FIXME: unify with llvm::haveNoCommonBitsSet.
6339 assert(A.getValueType() == B.getValueType() &&
6340 "Values must have the same type");
6343 return true;
6346}
6347
6348static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6349 SelectionDAG &DAG) {
6350 if (cast<ConstantSDNode>(Step)->isZero())
6351 return DAG.getConstant(0, DL, VT);
6352
6353 return SDValue();
6354}
6355
6358 SelectionDAG &DAG) {
6359 int NumOps = Ops.size();
6360 assert(NumOps != 0 && "Can't build an empty vector!");
6361 assert(!VT.isScalableVector() &&
6362 "BUILD_VECTOR cannot be used with scalable types");
6363 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6364 "Incorrect element count in BUILD_VECTOR!");
6365
6366 // BUILD_VECTOR of UNDEFs is UNDEF.
6367 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6368 return DAG.getUNDEF(VT);
6369
6370 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6371 SDValue IdentitySrc;
6372 bool IsIdentity = true;
6373 for (int i = 0; i != NumOps; ++i) {
6375 Ops[i].getOperand(0).getValueType() != VT ||
6376 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6377 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6378 Ops[i].getConstantOperandAPInt(1) != i) {
6379 IsIdentity = false;
6380 break;
6381 }
6382 IdentitySrc = Ops[i].getOperand(0);
6383 }
6384 if (IsIdentity)
6385 return IdentitySrc;
6386
6387 return SDValue();
6388}
6389
6390/// Try to simplify vector concatenation to an input value, undef, or build
6391/// vector.
6394 SelectionDAG &DAG) {
6395 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6397 [Ops](SDValue Op) {
6398 return Ops[0].getValueType() == Op.getValueType();
6399 }) &&
6400 "Concatenation of vectors with inconsistent value types!");
6401 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6402 VT.getVectorElementCount() &&
6403 "Incorrect element count in vector concatenation!");
6404
6405 if (Ops.size() == 1)
6406 return Ops[0];
6407
6408 // Concat of UNDEFs is UNDEF.
6409 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6410 return DAG.getUNDEF(VT);
6411
6412 // Scan the operands and look for extract operations from a single source
6413 // that correspond to insertion at the same location via this concatenation:
6414 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6415 SDValue IdentitySrc;
6416 bool IsIdentity = true;
6417 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6418 SDValue Op = Ops[i];
6419 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6420 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6421 Op.getOperand(0).getValueType() != VT ||
6422 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6423 Op.getConstantOperandVal(1) != IdentityIndex) {
6424 IsIdentity = false;
6425 break;
6426 }
6427 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6428 "Unexpected identity source vector for concat of extracts");
6429 IdentitySrc = Op.getOperand(0);
6430 }
6431 if (IsIdentity) {
6432 assert(IdentitySrc && "Failed to set source vector of extracts");
6433 return IdentitySrc;
6434 }
6435
6436 // The code below this point is only designed to work for fixed width
6437 // vectors, so we bail out for now.
6438 if (VT.isScalableVector())
6439 return SDValue();
6440
6441 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6442 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6443 // BUILD_VECTOR.
6444 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6445 EVT SVT = VT.getScalarType();
6447 for (SDValue Op : Ops) {
6448 EVT OpVT = Op.getValueType();
6449 if (Op.isUndef())
6450 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6451 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6452 Elts.append(Op->op_begin(), Op->op_end());
6453 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6454 OpVT.getVectorNumElements() == 1 &&
6455 isNullConstant(Op.getOperand(2)))
6456 Elts.push_back(Op.getOperand(1));
6457 else
6458 return SDValue();
6459 }
6460
6461 // BUILD_VECTOR requires all inputs to be of the same type, find the
6462 // maximum type and extend them all.
6463 for (SDValue Op : Elts)
6464 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6465
6466 if (SVT.bitsGT(VT.getScalarType())) {
6467 for (SDValue &Op : Elts) {
6468 if (Op.isUndef())
6469 Op = DAG.getUNDEF(SVT);
6470 else
6471 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6472 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6473 : DAG.getSExtOrTrunc(Op, DL, SVT);
6474 }
6475 }
6476
6477 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6478 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6479 return V;
6480}
6481
6482/// Gets or creates the specified node.
6483SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6484 SDVTList VTs = getVTList(VT);
6486 AddNodeIDNode(ID, Opcode, VTs, {});
6487 void *IP = nullptr;
6488 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6489 return SDValue(E, 0);
6490
6491 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6492 CSEMap.InsertNode(N, IP);
6493
6494 InsertNode(N);
6495 SDValue V = SDValue(N, 0);
6496 NewSDValueDbgMsg(V, "Creating new node: ", this);
6497 return V;
6498}
6499
6500SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6501 SDValue N1) {
6502 SDNodeFlags Flags;
6503 if (Inserter)
6504 Flags = Inserter->getFlags();
6505 return getNode(Opcode, DL, VT, N1, Flags);
6506}
6507
6508SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6509 SDValue N1, const SDNodeFlags Flags) {
6510 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6511
6512 // Constant fold unary operations with a vector integer or float operand.
6513 switch (Opcode) {
6514 default:
6515 // FIXME: Entirely reasonable to perform folding of other unary
6516 // operations here as the need arises.
6517 break;
6518 case ISD::FNEG:
6519 case ISD::FABS:
6520 case ISD::FCEIL:
6521 case ISD::FTRUNC:
6522 case ISD::FFLOOR:
6523 case ISD::FP_EXTEND:
6524 case ISD::FP_TO_SINT:
6525 case ISD::FP_TO_UINT:
6526 case ISD::FP_TO_FP16:
6527 case ISD::FP_TO_BF16:
6528 case ISD::TRUNCATE:
6529 case ISD::ANY_EXTEND:
6530 case ISD::ZERO_EXTEND:
6531 case ISD::SIGN_EXTEND:
6532 case ISD::UINT_TO_FP:
6533 case ISD::SINT_TO_FP:
6534 case ISD::FP16_TO_FP:
6535 case ISD::BF16_TO_FP:
6536 case ISD::BITCAST:
6537 case ISD::ABS:
6538 case ISD::BITREVERSE:
6539 case ISD::BSWAP:
6540 case ISD::CTLZ:
6542 case ISD::CTTZ:
6544 case ISD::CTPOP:
6545 case ISD::STEP_VECTOR: {
6546 SDValue Ops = {N1};
6547 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6548 return Fold;
6549 }
6550 }
6551
6552 unsigned OpOpcode = N1.getNode()->getOpcode();
6553 switch (Opcode) {
6554 case ISD::STEP_VECTOR:
6555 assert(VT.isScalableVector() &&
6556 "STEP_VECTOR can only be used with scalable types");
6557 assert(OpOpcode == ISD::TargetConstant &&
6558 VT.getVectorElementType() == N1.getValueType() &&
6559 "Unexpected step operand");
6560 break;
6561 case ISD::FREEZE:
6562 assert(VT == N1.getValueType() && "Unexpected VT!");
6563 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6564 return N1;
6565 break;
6566 case ISD::TokenFactor:
6567 case ISD::MERGE_VALUES:
6569 return N1; // Factor, merge or concat of one node? No need.
6570 case ISD::BUILD_VECTOR: {
6571 // Attempt to simplify BUILD_VECTOR.
6572 SDValue Ops[] = {N1};
6573 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6574 return V;
6575 break;
6576 }
6577 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6578 case ISD::FP_EXTEND:
6580 "Invalid FP cast!");
6581 if (N1.getValueType() == VT) return N1; // noop conversion.
6582 assert((!VT.isVector() || VT.getVectorElementCount() ==
6584 "Vector element count mismatch!");
6585 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6586 if (N1.isUndef())
6587 return getUNDEF(VT);
6588 break;
6589 case ISD::FP_TO_SINT:
6590 case ISD::FP_TO_UINT:
6591 if (N1.isUndef())
6592 return getUNDEF(VT);
6593 break;
6594 case ISD::SINT_TO_FP:
6595 case ISD::UINT_TO_FP:
6596 // [us]itofp(undef) = 0, because the result value is bounded.
6597 if (N1.isUndef())
6598 return getConstantFP(0.0, DL, VT);
6599 break;
6600 case ISD::SIGN_EXTEND:
6601 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6602 "Invalid SIGN_EXTEND!");
6603 assert(VT.isVector() == N1.getValueType().isVector() &&
6604 "SIGN_EXTEND result type type should be vector iff the operand "
6605 "type is vector!");
6606 if (N1.getValueType() == VT) return N1; // noop extension
6607 assert((!VT.isVector() || VT.getVectorElementCount() ==
6609 "Vector element count mismatch!");
6610 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6611 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6612 SDNodeFlags Flags;
6613 if (OpOpcode == ISD::ZERO_EXTEND)
6614 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6615 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6616 transferDbgValues(N1, NewVal);
6617 return NewVal;
6618 }
6619
6620 if (OpOpcode == ISD::POISON)
6621 return getPOISON(VT);
6622
6623 if (N1.isUndef())
6624 // sext(undef) = 0, because the top bits will all be the same.
6625 return getConstant(0, DL, VT);
6626
6627 // Skip unnecessary sext_inreg pattern:
6628 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6629 if (OpOpcode == ISD::TRUNCATE) {
6630 SDValue OpOp = N1.getOperand(0);
6631 if (OpOp.getValueType() == VT) {
6632 unsigned NumSignExtBits =
6634 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6635 transferDbgValues(N1, OpOp);
6636 return OpOp;
6637 }
6638 }
6639 }
6640 break;
6641 case ISD::ZERO_EXTEND:
6642 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6643 "Invalid ZERO_EXTEND!");
6644 assert(VT.isVector() == N1.getValueType().isVector() &&
6645 "ZERO_EXTEND result type type should be vector iff the operand "
6646 "type is vector!");
6647 if (N1.getValueType() == VT) return N1; // noop extension
6648 assert((!VT.isVector() || VT.getVectorElementCount() ==
6650 "Vector element count mismatch!");
6651 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6652 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6653 SDNodeFlags Flags;
6654 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6655 SDValue NewVal =
6656 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6657 transferDbgValues(N1, NewVal);
6658 return NewVal;
6659 }
6660
6661 if (OpOpcode == ISD::POISON)
6662 return getPOISON(VT);
6663
6664 if (N1.isUndef())
6665 // zext(undef) = 0, because the top bits will be zero.
6666 return getConstant(0, DL, VT);
6667
6668 // Skip unnecessary zext_inreg pattern:
6669 // (zext (trunc x)) -> x iff the upper bits are known zero.
6670 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6671 // use to recognise zext_inreg patterns.
6672 if (OpOpcode == ISD::TRUNCATE) {
6673 SDValue OpOp = N1.getOperand(0);
6674 if (OpOp.getValueType() == VT) {
6675 if (OpOp.getOpcode() != ISD::AND) {
6678 if (MaskedValueIsZero(OpOp, HiBits)) {
6679 transferDbgValues(N1, OpOp);
6680 return OpOp;
6681 }
6682 }
6683 }
6684 }
6685 break;
6686 case ISD::ANY_EXTEND:
6687 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6688 "Invalid ANY_EXTEND!");
6689 assert(VT.isVector() == N1.getValueType().isVector() &&
6690 "ANY_EXTEND result type type should be vector iff the operand "
6691 "type is vector!");
6692 if (N1.getValueType() == VT) return N1; // noop extension
6693 assert((!VT.isVector() || VT.getVectorElementCount() ==
6695 "Vector element count mismatch!");
6696 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6697
6698 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6699 OpOpcode == ISD::ANY_EXTEND) {
6700 SDNodeFlags Flags;
6701 if (OpOpcode == ISD::ZERO_EXTEND)
6702 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6703 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6704 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6705 }
6706 if (N1.isUndef())
6707 return getUNDEF(VT);
6708
6709 // (ext (trunc x)) -> x
6710 if (OpOpcode == ISD::TRUNCATE) {
6711 SDValue OpOp = N1.getOperand(0);
6712 if (OpOp.getValueType() == VT) {
6713 transferDbgValues(N1, OpOp);
6714 return OpOp;
6715 }
6716 }
6717 break;
6718 case ISD::TRUNCATE:
6719 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6720 "Invalid TRUNCATE!");
6721 assert(VT.isVector() == N1.getValueType().isVector() &&
6722 "TRUNCATE result type type should be vector iff the operand "
6723 "type is vector!");
6724 if (N1.getValueType() == VT) return N1; // noop truncate
6725 assert((!VT.isVector() || VT.getVectorElementCount() ==
6727 "Vector element count mismatch!");
6728 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6729 if (OpOpcode == ISD::TRUNCATE)
6730 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6731 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6732 OpOpcode == ISD::ANY_EXTEND) {
6733 // If the source is smaller than the dest, we still need an extend.
6735 VT.getScalarType())) {
6736 SDNodeFlags Flags;
6737 if (OpOpcode == ISD::ZERO_EXTEND)
6738 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6739 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6740 }
6741 if (N1.getOperand(0).getValueType().bitsGT(VT))
6742 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6743 return N1.getOperand(0);
6744 }
6745 if (N1.isUndef())
6746 return getUNDEF(VT);
6747 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6748 return getVScale(DL, VT,
6750 break;
6754 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6755 assert(N1.getValueType().bitsLE(VT) &&
6756 "The input must be the same size or smaller than the result.");
6759 "The destination vector type must have fewer lanes than the input.");
6760 break;
6761 case ISD::ABS:
6762 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6763 if (N1.isUndef())
6764 return getConstant(0, DL, VT);
6765 break;
6766 case ISD::BSWAP:
6767 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6768 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6769 "BSWAP types must be a multiple of 16 bits!");
6770 if (N1.isUndef())
6771 return getUNDEF(VT);
6772 // bswap(bswap(X)) -> X.
6773 if (OpOpcode == ISD::BSWAP)
6774 return N1.getOperand(0);
6775 break;
6776 case ISD::BITREVERSE:
6777 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6778 if (N1.isUndef())
6779 return getUNDEF(VT);
6780 break;
6781 case ISD::BITCAST:
6783 "Cannot BITCAST between types of different sizes!");
6784 if (VT == N1.getValueType()) return N1; // noop conversion.
6785 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6786 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6787 if (N1.isUndef())
6788 return getUNDEF(VT);
6789 break;
6791 assert(VT.isVector() && !N1.getValueType().isVector() &&
6792 (VT.getVectorElementType() == N1.getValueType() ||
6794 N1.getValueType().isInteger() &&
6796 "Illegal SCALAR_TO_VECTOR node!");
6797 if (N1.isUndef())
6798 return getUNDEF(VT);
6799 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6800 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6802 N1.getConstantOperandVal(1) == 0 &&
6803 N1.getOperand(0).getValueType() == VT)
6804 return N1.getOperand(0);
6805 break;
6806 case ISD::FNEG:
6807 // Negation of an unknown bag of bits is still completely undefined.
6808 if (N1.isUndef())
6809 return getUNDEF(VT);
6810
6811 if (OpOpcode == ISD::FNEG) // --X -> X
6812 return N1.getOperand(0);
6813 break;
6814 case ISD::FABS:
6815 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6816 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6817 break;
6818 case ISD::VSCALE:
6819 assert(VT == N1.getValueType() && "Unexpected VT!");
6820 break;
6821 case ISD::CTPOP:
6822 if (N1.getValueType().getScalarType() == MVT::i1)
6823 return N1;
6824 break;
6825 case ISD::CTLZ:
6826 case ISD::CTTZ:
6827 if (N1.getValueType().getScalarType() == MVT::i1)
6828 return getNOT(DL, N1, N1.getValueType());
6829 break;
6830 case ISD::VECREDUCE_ADD:
6831 if (N1.getValueType().getScalarType() == MVT::i1)
6832 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6833 break;
6836 if (N1.getValueType().getScalarType() == MVT::i1)
6837 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6838 break;
6841 if (N1.getValueType().getScalarType() == MVT::i1)
6842 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6843 break;
6844 case ISD::SPLAT_VECTOR:
6845 assert(VT.isVector() && "Wrong return type!");
6846 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6847 // that for now.
6849 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6851 N1.getValueType().isInteger() &&
6853 "Wrong operand type!");
6854 break;
6855 }
6856
6857 SDNode *N;
6858 SDVTList VTs = getVTList(VT);
6859 SDValue Ops[] = {N1};
6860 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6862 AddNodeIDNode(ID, Opcode, VTs, Ops);
6863 void *IP = nullptr;
6864 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6865 E->intersectFlagsWith(Flags);
6866 return SDValue(E, 0);
6867 }
6868
6869 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6870 N->setFlags(Flags);
6871 createOperands(N, Ops);
6872 CSEMap.InsertNode(N, IP);
6873 } else {
6874 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6875 createOperands(N, Ops);
6876 }
6877
6878 InsertNode(N);
6879 SDValue V = SDValue(N, 0);
6880 NewSDValueDbgMsg(V, "Creating new node: ", this);
6881 return V;
6882}
6883
6884static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6885 const APInt &C2) {
6886 switch (Opcode) {
6887 case ISD::ADD: return C1 + C2;
6888 case ISD::SUB: return C1 - C2;
6889 case ISD::MUL: return C1 * C2;
6890 case ISD::AND: return C1 & C2;
6891 case ISD::OR: return C1 | C2;
6892 case ISD::XOR: return C1 ^ C2;
6893 case ISD::SHL: return C1 << C2;
6894 case ISD::SRL: return C1.lshr(C2);
6895 case ISD::SRA: return C1.ashr(C2);
6896 case ISD::ROTL: return C1.rotl(C2);
6897 case ISD::ROTR: return C1.rotr(C2);
6898 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6899 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6900 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6901 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6902 case ISD::SADDSAT: return C1.sadd_sat(C2);
6903 case ISD::UADDSAT: return C1.uadd_sat(C2);
6904 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6905 case ISD::USUBSAT: return C1.usub_sat(C2);
6906 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6907 case ISD::USHLSAT: return C1.ushl_sat(C2);
6908 case ISD::UDIV:
6909 if (!C2.getBoolValue())
6910 break;
6911 return C1.udiv(C2);
6912 case ISD::UREM:
6913 if (!C2.getBoolValue())
6914 break;
6915 return C1.urem(C2);
6916 case ISD::SDIV:
6917 if (!C2.getBoolValue())
6918 break;
6919 return C1.sdiv(C2);
6920 case ISD::SREM:
6921 if (!C2.getBoolValue())
6922 break;
6923 return C1.srem(C2);
6924 case ISD::AVGFLOORS:
6925 return APIntOps::avgFloorS(C1, C2);
6926 case ISD::AVGFLOORU:
6927 return APIntOps::avgFloorU(C1, C2);
6928 case ISD::AVGCEILS:
6929 return APIntOps::avgCeilS(C1, C2);
6930 case ISD::AVGCEILU:
6931 return APIntOps::avgCeilU(C1, C2);
6932 case ISD::ABDS:
6933 return APIntOps::abds(C1, C2);
6934 case ISD::ABDU:
6935 return APIntOps::abdu(C1, C2);
6936 case ISD::MULHS:
6937 return APIntOps::mulhs(C1, C2);
6938 case ISD::MULHU:
6939 return APIntOps::mulhu(C1, C2);
6940 case ISD::CLMUL:
6941 return APIntOps::clmul(C1, C2);
6942 case ISD::CLMULR:
6943 return APIntOps::clmulr(C1, C2);
6944 case ISD::CLMULH:
6945 return APIntOps::clmulh(C1, C2);
6946 }
6947 return std::nullopt;
6948}
6949// Handle constant folding with UNDEF.
6950// TODO: Handle more cases.
6951static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6952 bool IsUndef1, const APInt &C2,
6953 bool IsUndef2) {
6954 if (!(IsUndef1 || IsUndef2))
6955 return FoldValue(Opcode, C1, C2);
6956
6957 // Fold and(x, undef) -> 0
6958 // Fold mul(x, undef) -> 0
6959 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6960 return APInt::getZero(C1.getBitWidth());
6961
6962 return std::nullopt;
6963}
6964
6966 const GlobalAddressSDNode *GA,
6967 const SDNode *N2) {
6968 if (GA->getOpcode() != ISD::GlobalAddress)
6969 return SDValue();
6970 if (!TLI->isOffsetFoldingLegal(GA))
6971 return SDValue();
6972 auto *C2 = dyn_cast<ConstantSDNode>(N2);
6973 if (!C2)
6974 return SDValue();
6975 int64_t Offset = C2->getSExtValue();
6976 switch (Opcode) {
6977 case ISD::ADD:
6978 case ISD::PTRADD:
6979 break;
6980 case ISD::SUB: Offset = -uint64_t(Offset); break;
6981 default: return SDValue();
6982 }
6983 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
6984 GA->getOffset() + uint64_t(Offset));
6985}
6986
6988 switch (Opcode) {
6989 case ISD::SDIV:
6990 case ISD::UDIV:
6991 case ISD::SREM:
6992 case ISD::UREM: {
6993 // If a divisor is zero/undef or any element of a divisor vector is
6994 // zero/undef, the whole op is undef.
6995 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
6996 SDValue Divisor = Ops[1];
6997 if (Divisor.isUndef() || isNullConstant(Divisor))
6998 return true;
6999
7000 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7001 llvm::any_of(Divisor->op_values(),
7002 [](SDValue V) { return V.isUndef() ||
7003 isNullConstant(V); });
7004 // TODO: Handle signed overflow.
7005 }
7006 // TODO: Handle oversized shifts.
7007 default:
7008 return false;
7009 }
7010}
7011
7014 SDNodeFlags Flags) {
7015 // If the opcode is a target-specific ISD node, there's nothing we can
7016 // do here and the operand rules may not line up with the below, so
7017 // bail early.
7018 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7019 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7020 // foldCONCAT_VECTORS in getNode before this is called.
7021 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7022 return SDValue();
7023
7024 unsigned NumOps = Ops.size();
7025 if (NumOps == 0)
7026 return SDValue();
7027
7028 if (isUndef(Opcode, Ops))
7029 return getUNDEF(VT);
7030
7031 // Handle unary special cases.
7032 if (NumOps == 1) {
7033 SDValue N1 = Ops[0];
7034
7035 // Constant fold unary operations with an integer constant operand. Even
7036 // opaque constant will be folded, because the folding of unary operations
7037 // doesn't create new constants with different values. Nevertheless, the
7038 // opaque flag is preserved during folding to prevent future folding with
7039 // other constants.
7040 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7041 const APInt &Val = C->getAPIntValue();
7042 switch (Opcode) {
7043 case ISD::SIGN_EXTEND:
7044 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7045 C->isTargetOpcode(), C->isOpaque());
7046 case ISD::TRUNCATE:
7047 if (C->isOpaque())
7048 break;
7049 [[fallthrough]];
7050 case ISD::ZERO_EXTEND:
7051 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7052 C->isTargetOpcode(), C->isOpaque());
7053 case ISD::ANY_EXTEND:
7054 // Some targets like RISCV prefer to sign extend some types.
7055 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7056 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7057 C->isTargetOpcode(), C->isOpaque());
7058 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7059 C->isTargetOpcode(), C->isOpaque());
7060 case ISD::ABS:
7061 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7062 C->isOpaque());
7063 case ISD::BITREVERSE:
7064 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7065 C->isOpaque());
7066 case ISD::BSWAP:
7067 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7068 C->isOpaque());
7069 case ISD::CTPOP:
7070 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7071 C->isOpaque());
7072 case ISD::CTLZ:
7074 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7075 C->isOpaque());
7076 case ISD::CTTZ:
7078 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7079 C->isOpaque());
7080 case ISD::UINT_TO_FP:
7081 case ISD::SINT_TO_FP: {
7083 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7085 return getConstantFP(FPV, DL, VT);
7086 }
7087 case ISD::FP16_TO_FP:
7088 case ISD::BF16_TO_FP: {
7089 bool Ignored;
7090 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7091 : APFloat::BFloat(),
7092 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7093
7094 // This can return overflow, underflow, or inexact; we don't care.
7095 // FIXME need to be more flexible about rounding mode.
7097 &Ignored);
7098 return getConstantFP(FPV, DL, VT);
7099 }
7100 case ISD::STEP_VECTOR:
7101 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7102 return V;
7103 break;
7104 case ISD::BITCAST:
7105 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7106 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7107 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7108 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7109 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7110 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7111 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7112 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7113 break;
7114 }
7115 }
7116
7117 // Constant fold unary operations with a floating point constant operand.
7118 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7119 APFloat V = C->getValueAPF(); // make copy
7120 switch (Opcode) {
7121 case ISD::FNEG:
7122 V.changeSign();
7123 return getConstantFP(V, DL, VT);
7124 case ISD::FABS:
7125 V.clearSign();
7126 return getConstantFP(V, DL, VT);
7127 case ISD::FCEIL: {
7128 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7130 return getConstantFP(V, DL, VT);
7131 return SDValue();
7132 }
7133 case ISD::FTRUNC: {
7134 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7136 return getConstantFP(V, DL, VT);
7137 return SDValue();
7138 }
7139 case ISD::FFLOOR: {
7140 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7142 return getConstantFP(V, DL, VT);
7143 return SDValue();
7144 }
7145 case ISD::FP_EXTEND: {
7146 bool ignored;
7147 // This can return overflow, underflow, or inexact; we don't care.
7148 // FIXME need to be more flexible about rounding mode.
7149 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7150 &ignored);
7151 return getConstantFP(V, DL, VT);
7152 }
7153 case ISD::FP_TO_SINT:
7154 case ISD::FP_TO_UINT: {
7155 bool ignored;
7156 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7157 // FIXME need to be more flexible about rounding mode.
7159 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7160 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7161 break;
7162 return getConstant(IntVal, DL, VT);
7163 }
7164 case ISD::FP_TO_FP16:
7165 case ISD::FP_TO_BF16: {
7166 bool Ignored;
7167 // This can return overflow, underflow, or inexact; we don't care.
7168 // FIXME need to be more flexible about rounding mode.
7169 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7170 : APFloat::BFloat(),
7172 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7173 }
7174 case ISD::BITCAST:
7175 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7176 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7177 VT);
7178 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7179 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7180 VT);
7181 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7182 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7183 VT);
7184 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7185 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7186 break;
7187 }
7188 }
7189
7190 // Early-out if we failed to constant fold a bitcast.
7191 if (Opcode == ISD::BITCAST)
7192 return SDValue();
7193 }
7194
7195 // Handle binops special cases.
7196 if (NumOps == 2) {
7197 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7198 return CFP;
7199
7200 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7201 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7202 if (C1->isOpaque() || C2->isOpaque())
7203 return SDValue();
7204
7205 std::optional<APInt> FoldAttempt =
7206 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7207 if (!FoldAttempt)
7208 return SDValue();
7209
7210 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7211 assert((!Folded || !VT.isVector()) &&
7212 "Can't fold vectors ops with scalar operands");
7213 return Folded;
7214 }
7215 }
7216
7217 // fold (add Sym, c) -> Sym+c
7219 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7220 if (TLI->isCommutativeBinOp(Opcode))
7222 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7223
7224 // fold (sext_in_reg c1) -> c2
7225 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7226 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7227
7228 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7229 unsigned FromBits = EVT.getScalarSizeInBits();
7230 Val <<= Val.getBitWidth() - FromBits;
7231 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7232 return getConstant(Val, DL, ConstantVT);
7233 };
7234
7235 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7236 const APInt &Val = C1->getAPIntValue();
7237 return SignExtendInReg(Val, VT);
7238 }
7239
7241 SmallVector<SDValue, 8> ScalarOps;
7242 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7243 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7244 SDValue Op = Ops[0].getOperand(I);
7245 if (Op.isUndef()) {
7246 ScalarOps.push_back(getUNDEF(OpVT));
7247 continue;
7248 }
7249 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7250 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7251 }
7252 return getBuildVector(VT, DL, ScalarOps);
7253 }
7254
7255 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7256 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7257 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7258 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7259 Ops[0].getOperand(0).getValueType()));
7260 }
7261 }
7262
7263 // Handle fshl/fshr special cases.
7264 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7265 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7266 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7267 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7268
7269 if (C1 && C2 && C3) {
7270 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7271 return SDValue();
7272 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7273 &V3 = C3->getAPIntValue();
7274
7275 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7276 : APIntOps::fshr(V1, V2, V3);
7277 return getConstant(FoldedVal, DL, VT);
7278 }
7279 }
7280
7281 // Handle fma/fmad special cases.
7282 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7283 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7284 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7285 Ops[2].getValueType() == VT && "FMA types must match!");
7289 if (C1 && C2 && C3) {
7290 APFloat V1 = C1->getValueAPF();
7291 const APFloat &V2 = C2->getValueAPF();
7292 const APFloat &V3 = C3->getValueAPF();
7293 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7296 } else
7298 return getConstantFP(V1, DL, VT);
7299 }
7300 }
7301
7302 // This is for vector folding only from here on.
7303 if (!VT.isVector())
7304 return SDValue();
7305
7306 ElementCount NumElts = VT.getVectorElementCount();
7307
7308 // See if we can fold through any bitcasted integer ops.
7309 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7310 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7311 (Ops[0].getOpcode() == ISD::BITCAST ||
7312 Ops[1].getOpcode() == ISD::BITCAST)) {
7315 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7316 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7317 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7318 N2.getValueType().isInteger()) {
7319 bool IsLE = getDataLayout().isLittleEndian();
7320 unsigned EltBits = VT.getScalarSizeInBits();
7321 SmallVector<APInt> RawBits1, RawBits2;
7322 BitVector UndefElts1, UndefElts2;
7323 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7324 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7325 SmallVector<APInt> RawBits;
7326 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7327 std::optional<APInt> Fold = FoldValueWithUndef(
7328 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7329 if (!Fold)
7330 break;
7331 RawBits.push_back(*Fold);
7332 }
7333 if (RawBits.size() == NumElts.getFixedValue()) {
7334 // We have constant folded, but we might need to cast this again back
7335 // to the original (possibly legalized) type.
7336 EVT BVVT, BVEltVT;
7337 if (N1.getValueType() == VT) {
7338 BVVT = N1.getValueType();
7339 BVEltVT = BV1->getOperand(0).getValueType();
7340 } else {
7341 BVVT = N2.getValueType();
7342 BVEltVT = BV2->getOperand(0).getValueType();
7343 }
7344 unsigned BVEltBits = BVEltVT.getSizeInBits();
7345 SmallVector<APInt> DstBits;
7346 BitVector DstUndefs;
7348 DstBits, RawBits, DstUndefs,
7349 BitVector(RawBits.size(), false));
7350 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7351 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7352 if (DstUndefs[I])
7353 continue;
7354 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7355 }
7356 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7357 }
7358 }
7359 }
7360 }
7361
7362 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7363 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7364 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7365 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7366 APInt RHSVal;
7367 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7368 APInt NewStep = Opcode == ISD::MUL
7369 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7370 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7371 return getStepVector(DL, VT, NewStep);
7372 }
7373 }
7374
7375 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7376 return !Op.getValueType().isVector() ||
7377 Op.getValueType().getVectorElementCount() == NumElts;
7378 };
7379
7380 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7381 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7382 Op.getOpcode() == ISD::BUILD_VECTOR ||
7383 Op.getOpcode() == ISD::SPLAT_VECTOR;
7384 };
7385
7386 // All operands must be vector types with the same number of elements as
7387 // the result type and must be either UNDEF or a build/splat vector
7388 // or UNDEF scalars.
7389 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7390 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7391 return SDValue();
7392
7393 // If we are comparing vectors, then the result needs to be a i1 boolean that
7394 // is then extended back to the legal result type depending on how booleans
7395 // are represented.
7396 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7397 ISD::NodeType ExtendCode =
7398 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7399 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7401
7402 // Find legal integer scalar type for constant promotion and
7403 // ensure that its scalar size is at least as large as source.
7404 EVT LegalSVT = VT.getScalarType();
7405 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7406 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7407 if (LegalSVT.bitsLT(VT.getScalarType()))
7408 return SDValue();
7409 }
7410
7411 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7412 // only have one operand to check. For fixed-length vector types we may have
7413 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7414 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7415
7416 // Constant fold each scalar lane separately.
7417 SmallVector<SDValue, 4> ScalarResults;
7418 for (unsigned I = 0; I != NumVectorElts; I++) {
7419 SmallVector<SDValue, 4> ScalarOps;
7420 for (SDValue Op : Ops) {
7421 EVT InSVT = Op.getValueType().getScalarType();
7422 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7423 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7424 if (Op.isUndef())
7425 ScalarOps.push_back(getUNDEF(InSVT));
7426 else
7427 ScalarOps.push_back(Op);
7428 continue;
7429 }
7430
7431 SDValue ScalarOp =
7432 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7433 EVT ScalarVT = ScalarOp.getValueType();
7434
7435 // Build vector (integer) scalar operands may need implicit
7436 // truncation - do this before constant folding.
7437 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7438 // Don't create illegally-typed nodes unless they're constants or undef
7439 // - if we fail to constant fold we can't guarantee the (dead) nodes
7440 // we're creating will be cleaned up before being visited for
7441 // legalization.
7442 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7443 !isa<ConstantSDNode>(ScalarOp) &&
7444 TLI->getTypeAction(*getContext(), InSVT) !=
7446 return SDValue();
7447 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7448 }
7449
7450 ScalarOps.push_back(ScalarOp);
7451 }
7452
7453 // Constant fold the scalar operands.
7454 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7455
7456 // Scalar folding only succeeded if the result is a constant or UNDEF.
7457 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7458 ScalarResult.getOpcode() != ISD::ConstantFP)
7459 return SDValue();
7460
7461 // Legalize the (integer) scalar constant if necessary. We only do
7462 // this once we know the folding succeeded, since otherwise we would
7463 // get a node with illegal type which has a user.
7464 if (LegalSVT != SVT)
7465 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7466
7467 ScalarResults.push_back(ScalarResult);
7468 }
7469
7470 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7471 : getBuildVector(VT, DL, ScalarResults);
7472 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7473 return V;
7474}
7475
7478 // TODO: Add support for unary/ternary fp opcodes.
7479 if (Ops.size() != 2)
7480 return SDValue();
7481
7482 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7483 // should. That will require dealing with a potentially non-default
7484 // rounding mode, checking the "opStatus" return value from the APFloat
7485 // math calculations, and possibly other variations.
7486 SDValue N1 = Ops[0];
7487 SDValue N2 = Ops[1];
7488 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7489 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7490 if (N1CFP && N2CFP) {
7491 APFloat C1 = N1CFP->getValueAPF(); // make copy
7492 const APFloat &C2 = N2CFP->getValueAPF();
7493 switch (Opcode) {
7494 case ISD::FADD:
7496 return getConstantFP(C1, DL, VT);
7497 case ISD::FSUB:
7499 return getConstantFP(C1, DL, VT);
7500 case ISD::FMUL:
7502 return getConstantFP(C1, DL, VT);
7503 case ISD::FDIV:
7505 return getConstantFP(C1, DL, VT);
7506 case ISD::FREM:
7507 C1.mod(C2);
7508 return getConstantFP(C1, DL, VT);
7509 case ISD::FCOPYSIGN:
7510 C1.copySign(C2);
7511 return getConstantFP(C1, DL, VT);
7512 case ISD::FMINNUM:
7513 if (C1.isSignaling() || C2.isSignaling())
7514 return SDValue();
7515 return getConstantFP(minnum(C1, C2), DL, VT);
7516 case ISD::FMAXNUM:
7517 if (C1.isSignaling() || C2.isSignaling())
7518 return SDValue();
7519 return getConstantFP(maxnum(C1, C2), DL, VT);
7520 case ISD::FMINIMUM:
7521 return getConstantFP(minimum(C1, C2), DL, VT);
7522 case ISD::FMAXIMUM:
7523 return getConstantFP(maximum(C1, C2), DL, VT);
7524 case ISD::FMINIMUMNUM:
7525 return getConstantFP(minimumnum(C1, C2), DL, VT);
7526 case ISD::FMAXIMUMNUM:
7527 return getConstantFP(maximumnum(C1, C2), DL, VT);
7528 default: break;
7529 }
7530 }
7531 if (N1CFP && Opcode == ISD::FP_ROUND) {
7532 APFloat C1 = N1CFP->getValueAPF(); // make copy
7533 bool Unused;
7534 // This can return overflow, underflow, or inexact; we don't care.
7535 // FIXME need to be more flexible about rounding mode.
7537 &Unused);
7538 return getConstantFP(C1, DL, VT);
7539 }
7540
7541 switch (Opcode) {
7542 case ISD::FSUB:
7543 // -0.0 - undef --> undef (consistent with "fneg undef")
7544 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7545 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7546 return getUNDEF(VT);
7547 [[fallthrough]];
7548
7549 case ISD::FADD:
7550 case ISD::FMUL:
7551 case ISD::FDIV:
7552 case ISD::FREM:
7553 // If both operands are undef, the result is undef. If 1 operand is undef,
7554 // the result is NaN. This should match the behavior of the IR optimizer.
7555 if (N1.isUndef() && N2.isUndef())
7556 return getUNDEF(VT);
7557 if (N1.isUndef() || N2.isUndef())
7559 }
7560 return SDValue();
7561}
7562
7564 const SDLoc &DL, EVT DstEltVT) {
7565 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7566
7567 // If this is already the right type, we're done.
7568 if (SrcEltVT == DstEltVT)
7569 return SDValue(BV, 0);
7570
7571 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7572 unsigned DstBitSize = DstEltVT.getSizeInBits();
7573
7574 // If this is a conversion of N elements of one type to N elements of another
7575 // type, convert each element. This handles FP<->INT cases.
7576 if (SrcBitSize == DstBitSize) {
7578 for (SDValue Op : BV->op_values()) {
7579 // If the vector element type is not legal, the BUILD_VECTOR operands
7580 // are promoted and implicitly truncated. Make that explicit here.
7581 if (Op.getValueType() != SrcEltVT)
7582 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7583 Ops.push_back(getBitcast(DstEltVT, Op));
7584 }
7585 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7587 return getBuildVector(VT, DL, Ops);
7588 }
7589
7590 // Otherwise, we're growing or shrinking the elements. To avoid having to
7591 // handle annoying details of growing/shrinking FP values, we convert them to
7592 // int first.
7593 if (SrcEltVT.isFloatingPoint()) {
7594 // Convert the input float vector to a int vector where the elements are the
7595 // same sizes.
7596 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7597 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7599 DstEltVT);
7600 return SDValue();
7601 }
7602
7603 // Now we know the input is an integer vector. If the output is a FP type,
7604 // convert to integer first, then to FP of the right size.
7605 if (DstEltVT.isFloatingPoint()) {
7606 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7607 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7609 DstEltVT);
7610 return SDValue();
7611 }
7612
7613 // Okay, we know the src/dst types are both integers of differing types.
7614 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7615
7616 // Extract the constant raw bit data.
7617 BitVector UndefElements;
7618 SmallVector<APInt> RawBits;
7619 bool IsLE = getDataLayout().isLittleEndian();
7620 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7621 return SDValue();
7622
7624 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7625 if (UndefElements[I])
7626 Ops.push_back(getUNDEF(DstEltVT));
7627 else
7628 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7629 }
7630
7631 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7632 return getBuildVector(VT, DL, Ops);
7633}
7634
7636 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7637
7638 // There's no need to assert on a byte-aligned pointer. All pointers are at
7639 // least byte aligned.
7640 if (A == Align(1))
7641 return Val;
7642
7643 SDVTList VTs = getVTList(Val.getValueType());
7645 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7646 ID.AddInteger(A.value());
7647
7648 void *IP = nullptr;
7649 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7650 return SDValue(E, 0);
7651
7652 auto *N =
7653 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7654 createOperands(N, {Val});
7655
7656 CSEMap.InsertNode(N, IP);
7657 InsertNode(N);
7658
7659 SDValue V(N, 0);
7660 NewSDValueDbgMsg(V, "Creating new node: ", this);
7661 return V;
7662}
7663
7664SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7665 SDValue N1, SDValue N2) {
7666 SDNodeFlags Flags;
7667 if (Inserter)
7668 Flags = Inserter->getFlags();
7669 return getNode(Opcode, DL, VT, N1, N2, Flags);
7670}
7671
7673 SDValue &N2) const {
7674 if (!TLI->isCommutativeBinOp(Opcode))
7675 return;
7676
7677 // Canonicalize:
7678 // binop(const, nonconst) -> binop(nonconst, const)
7681 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7682 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7683 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7684 std::swap(N1, N2);
7685
7686 // Canonicalize:
7687 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7688 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7690 std::swap(N1, N2);
7691}
7692
7693SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7694 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7696 N2.getOpcode() != ISD::DELETED_NODE &&
7697 "Operand is DELETED_NODE!");
7698
7699 canonicalizeCommutativeBinop(Opcode, N1, N2);
7700
7701 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7702 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7703
7704 // Don't allow undefs in vector splats - we might be returning N2 when folding
7705 // to zero etc.
7706 ConstantSDNode *N2CV =
7707 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7708
7709 switch (Opcode) {
7710 default: break;
7711 case ISD::TokenFactor:
7712 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7713 N2.getValueType() == MVT::Other && "Invalid token factor!");
7714 // Fold trivial token factors.
7715 if (N1.getOpcode() == ISD::EntryToken) return N2;
7716 if (N2.getOpcode() == ISD::EntryToken) return N1;
7717 if (N1 == N2) return N1;
7718 break;
7719 case ISD::BUILD_VECTOR: {
7720 // Attempt to simplify BUILD_VECTOR.
7721 SDValue Ops[] = {N1, N2};
7722 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7723 return V;
7724 break;
7725 }
7726 case ISD::CONCAT_VECTORS: {
7727 SDValue Ops[] = {N1, N2};
7728 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7729 return V;
7730 break;
7731 }
7732 case ISD::AND:
7733 assert(VT.isInteger() && "This operator does not apply to FP types!");
7734 assert(N1.getValueType() == N2.getValueType() &&
7735 N1.getValueType() == VT && "Binary operator types must match!");
7736 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7737 // worth handling here.
7738 if (N2CV && N2CV->isZero())
7739 return N2;
7740 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7741 return N1;
7742 break;
7743 case ISD::OR:
7744 case ISD::XOR:
7745 case ISD::ADD:
7746 case ISD::PTRADD:
7747 case ISD::SUB:
7748 assert(VT.isInteger() && "This operator does not apply to FP types!");
7749 assert(N1.getValueType() == N2.getValueType() &&
7750 N1.getValueType() == VT && "Binary operator types must match!");
7751 // The equal operand types requirement is unnecessarily strong for PTRADD.
7752 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7753 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7754 // logic everywhere where PTRADDs may be folded or combined to properly
7755 // support them. If/when we introduce pointer types to the SDAG, we will
7756 // need to relax this constraint.
7757
7758 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7759 // it's worth handling here.
7760 if (N2CV && N2CV->isZero())
7761 return N1;
7762 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7763 VT.getScalarType() == MVT::i1)
7764 return getNode(ISD::XOR, DL, VT, N1, N2);
7765 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7766 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7767 N2.getOpcode() == ISD::VSCALE) {
7768 const APInt &C1 = N1->getConstantOperandAPInt(0);
7769 const APInt &C2 = N2->getConstantOperandAPInt(0);
7770 return getVScale(DL, VT, C1 + C2);
7771 }
7772 break;
7773 case ISD::MUL:
7774 assert(VT.isInteger() && "This operator does not apply to FP types!");
7775 assert(N1.getValueType() == N2.getValueType() &&
7776 N1.getValueType() == VT && "Binary operator types must match!");
7777 if (VT.getScalarType() == MVT::i1)
7778 return getNode(ISD::AND, DL, VT, N1, N2);
7779 if (N2CV && N2CV->isZero())
7780 return N2;
7781 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7782 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7783 const APInt &N2CImm = N2C->getAPIntValue();
7784 return getVScale(DL, VT, MulImm * N2CImm);
7785 }
7786 break;
7787 case ISD::UDIV:
7788 case ISD::UREM:
7789 case ISD::MULHU:
7790 case ISD::MULHS:
7791 case ISD::SDIV:
7792 case ISD::SREM:
7793 case ISD::SADDSAT:
7794 case ISD::SSUBSAT:
7795 case ISD::UADDSAT:
7796 case ISD::USUBSAT:
7797 assert(VT.isInteger() && "This operator does not apply to FP types!");
7798 assert(N1.getValueType() == N2.getValueType() &&
7799 N1.getValueType() == VT && "Binary operator types must match!");
7800 if (VT.getScalarType() == MVT::i1) {
7801 // fold (add_sat x, y) -> (or x, y) for bool types.
7802 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7803 return getNode(ISD::OR, DL, VT, N1, N2);
7804 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7805 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7806 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7807 }
7808 break;
7809 case ISD::SCMP:
7810 case ISD::UCMP:
7811 assert(N1.getValueType() == N2.getValueType() &&
7812 "Types of operands of UCMP/SCMP must match");
7813 assert(N1.getValueType().isVector() == VT.isVector() &&
7814 "Operands and return type of must both be scalars or vectors");
7815 if (VT.isVector())
7818 "Result and operands must have the same number of elements");
7819 break;
7820 case ISD::AVGFLOORS:
7821 case ISD::AVGFLOORU:
7822 case ISD::AVGCEILS:
7823 case ISD::AVGCEILU:
7824 assert(VT.isInteger() && "This operator does not apply to FP types!");
7825 assert(N1.getValueType() == N2.getValueType() &&
7826 N1.getValueType() == VT && "Binary operator types must match!");
7827 break;
7828 case ISD::ABDS:
7829 case ISD::ABDU:
7830 assert(VT.isInteger() && "This operator does not apply to FP types!");
7831 assert(N1.getValueType() == N2.getValueType() &&
7832 N1.getValueType() == VT && "Binary operator types must match!");
7833 if (VT.getScalarType() == MVT::i1)
7834 return getNode(ISD::XOR, DL, VT, N1, N2);
7835 break;
7836 case ISD::SMIN:
7837 case ISD::UMAX:
7838 assert(VT.isInteger() && "This operator does not apply to FP types!");
7839 assert(N1.getValueType() == N2.getValueType() &&
7840 N1.getValueType() == VT && "Binary operator types must match!");
7841 if (VT.getScalarType() == MVT::i1)
7842 return getNode(ISD::OR, DL, VT, N1, N2);
7843 break;
7844 case ISD::SMAX:
7845 case ISD::UMIN:
7846 assert(VT.isInteger() && "This operator does not apply to FP types!");
7847 assert(N1.getValueType() == N2.getValueType() &&
7848 N1.getValueType() == VT && "Binary operator types must match!");
7849 if (VT.getScalarType() == MVT::i1)
7850 return getNode(ISD::AND, DL, VT, N1, N2);
7851 break;
7852 case ISD::FADD:
7853 case ISD::FSUB:
7854 case ISD::FMUL:
7855 case ISD::FDIV:
7856 case ISD::FREM:
7857 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7858 assert(N1.getValueType() == N2.getValueType() &&
7859 N1.getValueType() == VT && "Binary operator types must match!");
7860 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7861 return V;
7862 break;
7863 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7864 assert(N1.getValueType() == VT &&
7867 "Invalid FCOPYSIGN!");
7868 break;
7869 case ISD::SHL:
7870 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7871 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7872 const APInt &ShiftImm = N2C->getAPIntValue();
7873 return getVScale(DL, VT, MulImm << ShiftImm);
7874 }
7875 [[fallthrough]];
7876 case ISD::SRA:
7877 case ISD::SRL:
7878 if (SDValue V = simplifyShift(N1, N2))
7879 return V;
7880 [[fallthrough]];
7881 case ISD::ROTL:
7882 case ISD::ROTR:
7883 case ISD::SSHLSAT:
7884 case ISD::USHLSAT:
7885 assert(VT == N1.getValueType() &&
7886 "Shift operators return type must be the same as their first arg");
7887 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7888 "Shifts only work on integers");
7889 assert((!VT.isVector() || VT == N2.getValueType()) &&
7890 "Vector shift amounts must be in the same as their first arg");
7891 // Verify that the shift amount VT is big enough to hold valid shift
7892 // amounts. This catches things like trying to shift an i1024 value by an
7893 // i8, which is easy to fall into in generic code that uses
7894 // TLI.getShiftAmount().
7897 "Invalid use of small shift amount with oversized value!");
7898
7899 // Always fold shifts of i1 values so the code generator doesn't need to
7900 // handle them. Since we know the size of the shift has to be less than the
7901 // size of the value, the shift/rotate count is guaranteed to be zero.
7902 if (VT == MVT::i1)
7903 return N1;
7904 if (N2CV && N2CV->isZero())
7905 return N1;
7906 break;
7907 case ISD::FP_ROUND:
7909 VT.bitsLE(N1.getValueType()) && N2C &&
7910 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7911 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7912 if (N1.getValueType() == VT) return N1; // noop conversion.
7913 break;
7914 case ISD::AssertNoFPClass: {
7916 "AssertNoFPClass is used for a non-floating type");
7917 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7918 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7919 assert(llvm::to_underlying(NoFPClass) <=
7921 "FPClassTest value too large");
7922 (void)NoFPClass;
7923 break;
7924 }
7925 case ISD::AssertSext:
7926 case ISD::AssertZext: {
7927 EVT EVT = cast<VTSDNode>(N2)->getVT();
7928 assert(VT == N1.getValueType() && "Not an inreg extend!");
7929 assert(VT.isInteger() && EVT.isInteger() &&
7930 "Cannot *_EXTEND_INREG FP types");
7931 assert(!EVT.isVector() &&
7932 "AssertSExt/AssertZExt type should be the vector element type "
7933 "rather than the vector type!");
7934 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7935 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7936 break;
7937 }
7939 EVT EVT = cast<VTSDNode>(N2)->getVT();
7940 assert(VT == N1.getValueType() && "Not an inreg extend!");
7941 assert(VT.isInteger() && EVT.isInteger() &&
7942 "Cannot *_EXTEND_INREG FP types");
7943 assert(EVT.isVector() == VT.isVector() &&
7944 "SIGN_EXTEND_INREG type should be vector iff the operand "
7945 "type is vector!");
7946 assert((!EVT.isVector() ||
7948 "Vector element counts must match in SIGN_EXTEND_INREG");
7949 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
7950 if (EVT == VT) return N1; // Not actually extending
7951 break;
7952 }
7954 case ISD::FP_TO_UINT_SAT: {
7955 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7956 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7957 assert(N1.getValueType().isVector() == VT.isVector() &&
7958 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7959 "vector!");
7960 assert((!VT.isVector() || VT.getVectorElementCount() ==
7962 "Vector element counts must match in FP_TO_*INT_SAT");
7963 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7964 "Type to saturate to must be a scalar.");
7965 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7966 "Not extending!");
7967 break;
7968 }
7971 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
7972 element type of the vector.");
7973
7974 // Extract from an undefined value or using an undefined index is undefined.
7975 if (N1.isUndef() || N2.isUndef())
7976 return getUNDEF(VT);
7977
7978 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
7979 // vectors. For scalable vectors we will provide appropriate support for
7980 // dealing with arbitrary indices.
7981 if (N2C && N1.getValueType().isFixedLengthVector() &&
7982 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
7983 return getUNDEF(VT);
7984
7985 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
7986 // expanding copies of large vectors from registers. This only works for
7987 // fixed length vectors, since we need to know the exact number of
7988 // elements.
7989 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
7991 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
7992 return getExtractVectorElt(DL, VT,
7993 N1.getOperand(N2C->getZExtValue() / Factor),
7994 N2C->getZExtValue() % Factor);
7995 }
7996
7997 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
7998 // lowering is expanding large vector constants.
7999 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8000 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8003 "BUILD_VECTOR used for scalable vectors");
8004 unsigned Index =
8005 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8006 SDValue Elt = N1.getOperand(Index);
8007
8008 if (VT != Elt.getValueType())
8009 // If the vector element type is not legal, the BUILD_VECTOR operands
8010 // are promoted and implicitly truncated, and the result implicitly
8011 // extended. Make that explicit here.
8012 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8013
8014 return Elt;
8015 }
8016
8017 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8018 // operations are lowered to scalars.
8019 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8020 // If the indices are the same, return the inserted element else
8021 // if the indices are known different, extract the element from
8022 // the original vector.
8023 SDValue N1Op2 = N1.getOperand(2);
8025
8026 if (N1Op2C && N2C) {
8027 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8028 if (VT == N1.getOperand(1).getValueType())
8029 return N1.getOperand(1);
8030 if (VT.isFloatingPoint()) {
8032 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8033 }
8034 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8035 }
8036 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8037 }
8038 }
8039
8040 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8041 // when vector types are scalarized and v1iX is legal.
8042 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8043 // Here we are completely ignoring the extract element index (N2),
8044 // which is fine for fixed width vectors, since any index other than 0
8045 // is undefined anyway. However, this cannot be ignored for scalable
8046 // vectors - in theory we could support this, but we don't want to do this
8047 // without a profitability check.
8048 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8050 N1.getValueType().getVectorNumElements() == 1) {
8051 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8052 N1.getOperand(1));
8053 }
8054 break;
8056 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8057 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8058 (N1.getValueType().isInteger() == VT.isInteger()) &&
8059 N1.getValueType() != VT &&
8060 "Wrong types for EXTRACT_ELEMENT!");
8061
8062 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8063 // 64-bit integers into 32-bit parts. Instead of building the extract of
8064 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8065 if (N1.getOpcode() == ISD::BUILD_PAIR)
8066 return N1.getOperand(N2C->getZExtValue());
8067
8068 // EXTRACT_ELEMENT of a constant int is also very common.
8069 if (N1C) {
8070 unsigned ElementSize = VT.getSizeInBits();
8071 unsigned Shift = ElementSize * N2C->getZExtValue();
8072 const APInt &Val = N1C->getAPIntValue();
8073 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8074 }
8075 break;
8077 EVT N1VT = N1.getValueType();
8078 assert(VT.isVector() && N1VT.isVector() &&
8079 "Extract subvector VTs must be vectors!");
8081 "Extract subvector VTs must have the same element type!");
8082 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8083 "Cannot extract a scalable vector from a fixed length vector!");
8084 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8086 "Extract subvector must be from larger vector to smaller vector!");
8087 assert(N2C && "Extract subvector index must be a constant");
8088 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8089 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8090 N1VT.getVectorMinNumElements()) &&
8091 "Extract subvector overflow!");
8092 assert(N2C->getAPIntValue().getBitWidth() ==
8093 TLI->getVectorIdxWidth(getDataLayout()) &&
8094 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8095 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8096 "Extract index is not a multiple of the output vector length");
8097
8098 // Trivial extraction.
8099 if (VT == N1VT)
8100 return N1;
8101
8102 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8103 if (N1.isUndef())
8104 return getUNDEF(VT);
8105
8106 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8107 // the concat have the same type as the extract.
8108 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8109 VT == N1.getOperand(0).getValueType()) {
8110 unsigned Factor = VT.getVectorMinNumElements();
8111 return N1.getOperand(N2C->getZExtValue() / Factor);
8112 }
8113
8114 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8115 // during shuffle legalization.
8116 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8117 VT == N1.getOperand(1).getValueType())
8118 return N1.getOperand(1);
8119 break;
8120 }
8121 }
8122
8123 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8124 switch (Opcode) {
8125 case ISD::XOR:
8126 case ISD::ADD:
8127 case ISD::PTRADD:
8128 case ISD::SUB:
8130 case ISD::UDIV:
8131 case ISD::SDIV:
8132 case ISD::UREM:
8133 case ISD::SREM:
8134 case ISD::MUL:
8135 case ISD::AND:
8136 case ISD::SSUBSAT:
8137 case ISD::USUBSAT:
8138 case ISD::UMIN:
8139 case ISD::OR:
8140 case ISD::SADDSAT:
8141 case ISD::UADDSAT:
8142 case ISD::UMAX:
8143 case ISD::SMAX:
8144 case ISD::SMIN:
8145 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8146 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8147 }
8148 }
8149
8150 // Canonicalize an UNDEF to the RHS, even over a constant.
8151 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8152 if (TLI->isCommutativeBinOp(Opcode)) {
8153 std::swap(N1, N2);
8154 } else {
8155 switch (Opcode) {
8156 case ISD::PTRADD:
8157 case ISD::SUB:
8158 // fold op(undef, non_undef_arg2) -> undef.
8159 return N1;
8161 case ISD::UDIV:
8162 case ISD::SDIV:
8163 case ISD::UREM:
8164 case ISD::SREM:
8165 case ISD::SSUBSAT:
8166 case ISD::USUBSAT:
8167 // fold op(undef, non_undef_arg2) -> 0.
8168 return getConstant(0, DL, VT);
8169 }
8170 }
8171 }
8172
8173 // Fold a bunch of operators when the RHS is undef.
8174 if (N2.getOpcode() == ISD::UNDEF) {
8175 switch (Opcode) {
8176 case ISD::XOR:
8177 if (N1.getOpcode() == ISD::UNDEF)
8178 // Handle undef ^ undef -> 0 special case. This is a common
8179 // idiom (misuse).
8180 return getConstant(0, DL, VT);
8181 [[fallthrough]];
8182 case ISD::ADD:
8183 case ISD::PTRADD:
8184 case ISD::SUB:
8185 // fold op(arg1, undef) -> undef.
8186 return N2;
8187 case ISD::UDIV:
8188 case ISD::SDIV:
8189 case ISD::UREM:
8190 case ISD::SREM:
8191 // fold op(arg1, undef) -> poison.
8192 return getPOISON(VT);
8193 case ISD::MUL:
8194 case ISD::AND:
8195 case ISD::SSUBSAT:
8196 case ISD::USUBSAT:
8197 case ISD::UMIN:
8198 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8199 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8200 case ISD::OR:
8201 case ISD::SADDSAT:
8202 case ISD::UADDSAT:
8203 case ISD::UMAX:
8204 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8205 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8206 case ISD::SMAX:
8207 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8208 return N1.getOpcode() == ISD::UNDEF
8209 ? N2
8210 : getConstant(
8212 VT);
8213 case ISD::SMIN:
8214 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8215 return N1.getOpcode() == ISD::UNDEF
8216 ? N2
8217 : getConstant(
8219 VT);
8220 }
8221 }
8222
8223 // Perform trivial constant folding.
8224 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8225 return SV;
8226
8227 // Memoize this node if possible.
8228 SDNode *N;
8229 SDVTList VTs = getVTList(VT);
8230 SDValue Ops[] = {N1, N2};
8231 if (VT != MVT::Glue) {
8233 AddNodeIDNode(ID, Opcode, VTs, Ops);
8234 void *IP = nullptr;
8235 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8236 E->intersectFlagsWith(Flags);
8237 return SDValue(E, 0);
8238 }
8239
8240 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8241 N->setFlags(Flags);
8242 createOperands(N, Ops);
8243 CSEMap.InsertNode(N, IP);
8244 } else {
8245 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8246 createOperands(N, Ops);
8247 }
8248
8249 InsertNode(N);
8250 SDValue V = SDValue(N, 0);
8251 NewSDValueDbgMsg(V, "Creating new node: ", this);
8252 return V;
8253}
8254
8255SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8256 SDValue N1, SDValue N2, SDValue N3) {
8257 SDNodeFlags Flags;
8258 if (Inserter)
8259 Flags = Inserter->getFlags();
8260 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8261}
8262
8263SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8264 SDValue N1, SDValue N2, SDValue N3,
8265 const SDNodeFlags Flags) {
8267 N2.getOpcode() != ISD::DELETED_NODE &&
8268 N3.getOpcode() != ISD::DELETED_NODE &&
8269 "Operand is DELETED_NODE!");
8270 // Perform various simplifications.
8271 switch (Opcode) {
8272 case ISD::BUILD_VECTOR: {
8273 // Attempt to simplify BUILD_VECTOR.
8274 SDValue Ops[] = {N1, N2, N3};
8275 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8276 return V;
8277 break;
8278 }
8279 case ISD::CONCAT_VECTORS: {
8280 SDValue Ops[] = {N1, N2, N3};
8281 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8282 return V;
8283 break;
8284 }
8285 case ISD::SETCC: {
8286 assert(VT.isInteger() && "SETCC result type must be an integer!");
8287 assert(N1.getValueType() == N2.getValueType() &&
8288 "SETCC operands must have the same type!");
8289 assert(VT.isVector() == N1.getValueType().isVector() &&
8290 "SETCC type should be vector iff the operand type is vector!");
8291 assert((!VT.isVector() || VT.getVectorElementCount() ==
8293 "SETCC vector element counts must match!");
8294 // Use FoldSetCC to simplify SETCC's.
8295 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8296 return V;
8297 break;
8298 }
8299 case ISD::SELECT:
8300 case ISD::VSELECT:
8301 if (SDValue V = simplifySelect(N1, N2, N3))
8302 return V;
8303 break;
8305 llvm_unreachable("should use getVectorShuffle constructor!");
8307 if (isNullConstant(N3))
8308 return N1;
8309 break;
8311 if (isNullConstant(N3))
8312 return N2;
8313 break;
8315 assert(VT.isVector() && VT == N1.getValueType() &&
8316 "INSERT_VECTOR_ELT vector type mismatch");
8318 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8319 assert((!VT.isFloatingPoint() ||
8320 VT.getVectorElementType() == N2.getValueType()) &&
8321 "INSERT_VECTOR_ELT fp scalar type mismatch");
8322 assert((!VT.isInteger() ||
8324 "INSERT_VECTOR_ELT int scalar size mismatch");
8325
8326 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8327 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8328 // for scalable vectors where we will generate appropriate code to
8329 // deal with out-of-bounds cases correctly.
8330 if (N3C && VT.isFixedLengthVector() &&
8331 N3C->getZExtValue() >= VT.getVectorNumElements())
8332 return getUNDEF(VT);
8333
8334 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8335 if (N3.isUndef())
8336 return getUNDEF(VT);
8337
8338 // If inserting poison, just use the input vector.
8339 if (N2.getOpcode() == ISD::POISON)
8340 return N1;
8341
8342 // Inserting undef into undef/poison is still undef.
8343 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8344 return getUNDEF(VT);
8345
8346 // If the inserted element is an UNDEF, just use the input vector.
8347 // But not if skipping the insert could make the result more poisonous.
8348 if (N2.isUndef()) {
8349 if (N3C && VT.isFixedLengthVector()) {
8350 APInt EltMask =
8351 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8352 if (isGuaranteedNotToBePoison(N1, EltMask))
8353 return N1;
8354 } else if (isGuaranteedNotToBePoison(N1))
8355 return N1;
8356 }
8357 break;
8358 }
8359 case ISD::INSERT_SUBVECTOR: {
8360 // If inserting poison, just use the input vector,
8361 if (N2.getOpcode() == ISD::POISON)
8362 return N1;
8363
8364 // Inserting undef into undef/poison is still undef.
8365 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8366 return getUNDEF(VT);
8367
8368 EVT N2VT = N2.getValueType();
8369 assert(VT == N1.getValueType() &&
8370 "Dest and insert subvector source types must match!");
8371 assert(VT.isVector() && N2VT.isVector() &&
8372 "Insert subvector VTs must be vectors!");
8374 "Insert subvector VTs must have the same element type!");
8375 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8376 "Cannot insert a scalable vector into a fixed length vector!");
8377 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8379 "Insert subvector must be from smaller vector to larger vector!");
8381 "Insert subvector index must be constant");
8382 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8383 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8385 "Insert subvector overflow!");
8387 TLI->getVectorIdxWidth(getDataLayout()) &&
8388 "Constant index for INSERT_SUBVECTOR has an invalid size");
8389
8390 // Trivial insertion.
8391 if (VT == N2VT)
8392 return N2;
8393
8394 // If this is an insert of an extracted vector into an undef/poison vector,
8395 // we can just use the input to the extract. But not if skipping the
8396 // extract+insert could make the result more poisonous.
8397 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8398 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8399 if (N1.getOpcode() == ISD::POISON)
8400 return N2.getOperand(0);
8401 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8402 unsigned LoBit = N3->getAsZExtVal();
8403 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8404 APInt EltMask =
8405 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8406 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8407 return N2.getOperand(0);
8408 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8409 return N2.getOperand(0);
8410 }
8411
8412 // If the inserted subvector is UNDEF, just use the input vector.
8413 // But not if skipping the insert could make the result more poisonous.
8414 if (N2.isUndef()) {
8415 if (VT.isFixedLengthVector()) {
8416 unsigned LoBit = N3->getAsZExtVal();
8417 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8418 APInt EltMask =
8419 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8420 if (isGuaranteedNotToBePoison(N1, EltMask))
8421 return N1;
8422 } else if (isGuaranteedNotToBePoison(N1))
8423 return N1;
8424 }
8425 break;
8426 }
8427 case ISD::BITCAST:
8428 // Fold bit_convert nodes from a type to themselves.
8429 if (N1.getValueType() == VT)
8430 return N1;
8431 break;
8432 case ISD::VP_TRUNCATE:
8433 case ISD::VP_SIGN_EXTEND:
8434 case ISD::VP_ZERO_EXTEND:
8435 // Don't create noop casts.
8436 if (N1.getValueType() == VT)
8437 return N1;
8438 break;
8439 case ISD::VECTOR_COMPRESS: {
8440 [[maybe_unused]] EVT VecVT = N1.getValueType();
8441 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8442 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8443 assert(VT == VecVT && "Vector and result type don't match.");
8444 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8445 "All inputs must be vectors.");
8446 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8448 "Vector and mask must have same number of elements.");
8449
8450 if (N1.isUndef() || N2.isUndef())
8451 return N3;
8452
8453 break;
8454 }
8459 [[maybe_unused]] EVT AccVT = N1.getValueType();
8460 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8461 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8462 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8463 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8464 "node to have the same type!");
8465 assert(VT.isVector() && VT == AccVT &&
8466 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8467 "the same type as its result!");
8469 AccVT.getVectorElementCount()) &&
8470 "Expected the element count of the second and third operands of the "
8471 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8472 "element count of the first operand and the result!");
8474 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8475 "node to have an element type which is the same as or smaller than "
8476 "the element type of the first operand and result!");
8477 break;
8478 }
8479 }
8480
8481 // Perform trivial constant folding for arithmetic operators.
8482 switch (Opcode) {
8483 case ISD::FMA:
8484 case ISD::FMAD:
8485 case ISD::SETCC:
8486 case ISD::FSHL:
8487 case ISD::FSHR:
8488 if (SDValue SV =
8489 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8490 return SV;
8491 break;
8492 }
8493
8494 // Memoize node if it doesn't produce a glue result.
8495 SDNode *N;
8496 SDVTList VTs = getVTList(VT);
8497 SDValue Ops[] = {N1, N2, N3};
8498 if (VT != MVT::Glue) {
8500 AddNodeIDNode(ID, Opcode, VTs, Ops);
8501 void *IP = nullptr;
8502 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8503 E->intersectFlagsWith(Flags);
8504 return SDValue(E, 0);
8505 }
8506
8507 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8508 N->setFlags(Flags);
8509 createOperands(N, Ops);
8510 CSEMap.InsertNode(N, IP);
8511 } else {
8512 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8513 createOperands(N, Ops);
8514 }
8515
8516 InsertNode(N);
8517 SDValue V = SDValue(N, 0);
8518 NewSDValueDbgMsg(V, "Creating new node: ", this);
8519 return V;
8520}
8521
8522SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8523 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8524 const SDNodeFlags Flags) {
8525 SDValue Ops[] = { N1, N2, N3, N4 };
8526 return getNode(Opcode, DL, VT, Ops, Flags);
8527}
8528
8529SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8530 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8531 SDNodeFlags Flags;
8532 if (Inserter)
8533 Flags = Inserter->getFlags();
8534 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8535}
8536
8537SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8538 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8539 SDValue N5, const SDNodeFlags Flags) {
8540 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8541 return getNode(Opcode, DL, VT, Ops, Flags);
8542}
8543
8544SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8545 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8546 SDValue N5) {
8547 SDNodeFlags Flags;
8548 if (Inserter)
8549 Flags = Inserter->getFlags();
8550 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8551}
8552
8553/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8554/// the incoming stack arguments to be loaded from the stack.
8556 SmallVector<SDValue, 8> ArgChains;
8557
8558 // Include the original chain at the beginning of the list. When this is
8559 // used by target LowerCall hooks, this helps legalize find the
8560 // CALLSEQ_BEGIN node.
8561 ArgChains.push_back(Chain);
8562
8563 // Add a chain value for each stack argument.
8564 for (SDNode *U : getEntryNode().getNode()->users())
8565 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8566 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8567 if (FI->getIndex() < 0)
8568 ArgChains.push_back(SDValue(L, 1));
8569
8570 // Build a tokenfactor for all the chains.
8571 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8572}
8573
8574/// getMemsetValue - Vectorized representation of the memset value
8575/// operand.
8577 const SDLoc &dl) {
8578 assert(!Value.isUndef());
8579
8580 unsigned NumBits = VT.getScalarSizeInBits();
8582 assert(C->getAPIntValue().getBitWidth() == 8);
8583 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8584 if (VT.isInteger()) {
8585 bool IsOpaque = VT.getSizeInBits() > 64 ||
8586 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8587 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8588 }
8589 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8590 }
8591
8592 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8593 EVT IntVT = VT.getScalarType();
8594 if (!IntVT.isInteger())
8595 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8596
8597 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8598 if (NumBits > 8) {
8599 // Use a multiplication with 0x010101... to extend the input to the
8600 // required length.
8601 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8602 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8603 DAG.getConstant(Magic, dl, IntVT));
8604 }
8605
8606 if (VT != Value.getValueType() && !VT.isInteger())
8607 Value = DAG.getBitcast(VT.getScalarType(), Value);
8608 if (VT != Value.getValueType())
8609 Value = DAG.getSplatBuildVector(VT, dl, Value);
8610
8611 return Value;
8612}
8613
8614/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8615/// used when a memcpy is turned into a memset when the source is a constant
8616/// string ptr.
8618 const TargetLowering &TLI,
8619 const ConstantDataArraySlice &Slice) {
8620 // Handle vector with all elements zero.
8621 if (Slice.Array == nullptr) {
8622 if (VT.isInteger())
8623 return DAG.getConstant(0, dl, VT);
8624 return DAG.getNode(ISD::BITCAST, dl, VT,
8625 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8626 }
8627
8628 assert(!VT.isVector() && "Can't handle vector type here!");
8629 unsigned NumVTBits = VT.getSizeInBits();
8630 unsigned NumVTBytes = NumVTBits / 8;
8631 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8632
8633 APInt Val(NumVTBits, 0);
8634 if (DAG.getDataLayout().isLittleEndian()) {
8635 for (unsigned i = 0; i != NumBytes; ++i)
8636 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8637 } else {
8638 for (unsigned i = 0; i != NumBytes; ++i)
8639 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8640 }
8641
8642 // If the "cost" of materializing the integer immediate is less than the cost
8643 // of a load, then it is cost effective to turn the load into the immediate.
8644 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8645 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8646 return DAG.getConstant(Val, dl, VT);
8647 return SDValue();
8648}
8649
8651 const SDLoc &DL,
8652 const SDNodeFlags Flags) {
8653 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8654 return getMemBasePlusOffset(Base, Index, DL, Flags);
8655}
8656
8658 const SDLoc &DL,
8659 const SDNodeFlags Flags) {
8660 assert(Offset.getValueType().isInteger());
8661 EVT BasePtrVT = Ptr.getValueType();
8662 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8663 BasePtrVT))
8664 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8665 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8666 SDNodeFlags AddFlags = Flags;
8667 AddFlags.setInBounds(false);
8668 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8669}
8670
8671/// Returns true if memcpy source is constant data.
8673 uint64_t SrcDelta = 0;
8674 GlobalAddressSDNode *G = nullptr;
8675 if (Src.getOpcode() == ISD::GlobalAddress)
8677 else if (Src->isAnyAdd() &&
8678 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8679 Src.getOperand(1).getOpcode() == ISD::Constant) {
8680 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8681 SrcDelta = Src.getConstantOperandVal(1);
8682 }
8683 if (!G)
8684 return false;
8685
8686 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8687 SrcDelta + G->getOffset());
8688}
8689
8691 SelectionDAG &DAG) {
8692 // On Darwin, -Os means optimize for size without hurting performance, so
8693 // only really optimize for size when -Oz (MinSize) is used.
8695 return MF.getFunction().hasMinSize();
8696 return DAG.shouldOptForSize();
8697}
8698
8700 SmallVector<SDValue, 32> &OutChains, unsigned From,
8701 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8702 SmallVector<SDValue, 16> &OutStoreChains) {
8703 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8704 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8705 SmallVector<SDValue, 16> GluedLoadChains;
8706 for (unsigned i = From; i < To; ++i) {
8707 OutChains.push_back(OutLoadChains[i]);
8708 GluedLoadChains.push_back(OutLoadChains[i]);
8709 }
8710
8711 // Chain for all loads.
8712 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8713 GluedLoadChains);
8714
8715 for (unsigned i = From; i < To; ++i) {
8716 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8717 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8718 ST->getBasePtr(), ST->getMemoryVT(),
8719 ST->getMemOperand());
8720 OutChains.push_back(NewStore);
8721 }
8722}
8723
8725 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8726 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8727 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8728 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8729 // Turn a memcpy of undef to nop.
8730 // FIXME: We need to honor volatile even is Src is undef.
8731 if (Src.isUndef())
8732 return Chain;
8733
8734 // Expand memcpy to a series of load and store ops if the size operand falls
8735 // below a certain threshold.
8736 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8737 // rather than maybe a humongous number of loads and stores.
8738 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8739 const DataLayout &DL = DAG.getDataLayout();
8740 LLVMContext &C = *DAG.getContext();
8741 std::vector<EVT> MemOps;
8742 bool DstAlignCanChange = false;
8744 MachineFrameInfo &MFI = MF.getFrameInfo();
8745 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8747 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8748 DstAlignCanChange = true;
8749 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8750 if (!SrcAlign || Alignment > *SrcAlign)
8751 SrcAlign = Alignment;
8752 assert(SrcAlign && "SrcAlign must be set");
8754 // If marked as volatile, perform a copy even when marked as constant.
8755 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8756 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8757 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8758 const MemOp Op = isZeroConstant
8759 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8760 /*IsZeroMemset*/ true, isVol)
8761 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8762 *SrcAlign, isVol, CopyFromConstant);
8763 if (!TLI.findOptimalMemOpLowering(
8764 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8765 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes()))
8766 return SDValue();
8767
8768 if (DstAlignCanChange) {
8769 Type *Ty = MemOps[0].getTypeForEVT(C);
8770 Align NewAlign = DL.getABITypeAlign(Ty);
8771
8772 // Don't promote to an alignment that would require dynamic stack
8773 // realignment which may conflict with optimizations such as tail call
8774 // optimization.
8776 if (!TRI->hasStackRealignment(MF))
8777 if (MaybeAlign StackAlign = DL.getStackAlignment())
8778 NewAlign = std::min(NewAlign, *StackAlign);
8779
8780 if (NewAlign > Alignment) {
8781 // Give the stack frame object a larger alignment if needed.
8782 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8783 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8784 Alignment = NewAlign;
8785 }
8786 }
8787
8788 // Prepare AAInfo for loads/stores after lowering this memcpy.
8789 AAMDNodes NewAAInfo = AAInfo;
8790 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8791
8792 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8793 bool isConstant =
8794 BatchAA && SrcVal &&
8795 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8796
8797 MachineMemOperand::Flags MMOFlags =
8799 SmallVector<SDValue, 16> OutLoadChains;
8800 SmallVector<SDValue, 16> OutStoreChains;
8801 SmallVector<SDValue, 32> OutChains;
8802 unsigned NumMemOps = MemOps.size();
8803 uint64_t SrcOff = 0, DstOff = 0;
8804 for (unsigned i = 0; i != NumMemOps; ++i) {
8805 EVT VT = MemOps[i];
8806 unsigned VTSize = VT.getSizeInBits() / 8;
8807 SDValue Value, Store;
8808
8809 if (VTSize > Size) {
8810 // Issuing an unaligned load / store pair that overlaps with the previous
8811 // pair. Adjust the offset accordingly.
8812 assert(i == NumMemOps-1 && i != 0);
8813 SrcOff -= VTSize - Size;
8814 DstOff -= VTSize - Size;
8815 }
8816
8817 if (CopyFromConstant &&
8818 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8819 // It's unlikely a store of a vector immediate can be done in a single
8820 // instruction. It would require a load from a constantpool first.
8821 // We only handle zero vectors here.
8822 // FIXME: Handle other cases where store of vector immediate is done in
8823 // a single instruction.
8824 ConstantDataArraySlice SubSlice;
8825 if (SrcOff < Slice.Length) {
8826 SubSlice = Slice;
8827 SubSlice.move(SrcOff);
8828 } else {
8829 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8830 SubSlice.Array = nullptr;
8831 SubSlice.Offset = 0;
8832 SubSlice.Length = VTSize;
8833 }
8834 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8835 if (Value.getNode()) {
8836 Store = DAG.getStore(
8837 Chain, dl, Value,
8838 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8839 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8840 OutChains.push_back(Store);
8841 }
8842 }
8843
8844 if (!Store.getNode()) {
8845 // The type might not be legal for the target. This should only happen
8846 // if the type is smaller than a legal type, as on PPC, so the right
8847 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8848 // to Load/Store if NVT==VT.
8849 // FIXME does the case above also need this?
8850 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8851 assert(NVT.bitsGE(VT));
8852
8853 bool isDereferenceable =
8854 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8855 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8856 if (isDereferenceable)
8858 if (isConstant)
8859 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8860
8861 Value = DAG.getExtLoad(
8862 ISD::EXTLOAD, dl, NVT, Chain,
8863 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8864 SrcPtrInfo.getWithOffset(SrcOff), VT,
8865 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8866 OutLoadChains.push_back(Value.getValue(1));
8867
8868 Store = DAG.getTruncStore(
8869 Chain, dl, Value,
8870 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8871 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8872 OutStoreChains.push_back(Store);
8873 }
8874 SrcOff += VTSize;
8875 DstOff += VTSize;
8876 Size -= VTSize;
8877 }
8878
8879 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8881 unsigned NumLdStInMemcpy = OutStoreChains.size();
8882
8883 if (NumLdStInMemcpy) {
8884 // It may be that memcpy might be converted to memset if it's memcpy
8885 // of constants. In such a case, we won't have loads and stores, but
8886 // just stores. In the absence of loads, there is nothing to gang up.
8887 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8888 // If target does not care, just leave as it.
8889 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8890 OutChains.push_back(OutLoadChains[i]);
8891 OutChains.push_back(OutStoreChains[i]);
8892 }
8893 } else {
8894 // Ld/St less than/equal limit set by target.
8895 if (NumLdStInMemcpy <= GluedLdStLimit) {
8896 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8897 NumLdStInMemcpy, OutLoadChains,
8898 OutStoreChains);
8899 } else {
8900 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8901 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8902 unsigned GlueIter = 0;
8903
8904 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8905 unsigned IndexFrom = NumLdStInMemcpy - GlueIter - GluedLdStLimit;
8906 unsigned IndexTo = NumLdStInMemcpy - GlueIter;
8907
8908 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8909 OutLoadChains, OutStoreChains);
8910 GlueIter += GluedLdStLimit;
8911 }
8912
8913 // Residual ld/st.
8914 if (RemainingLdStInMemcpy) {
8915 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8916 RemainingLdStInMemcpy, OutLoadChains,
8917 OutStoreChains);
8918 }
8919 }
8920 }
8921 }
8922 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8923}
8924
8926 SDValue Chain, SDValue Dst, SDValue Src,
8927 uint64_t Size, Align Alignment,
8928 bool isVol, bool AlwaysInline,
8929 MachinePointerInfo DstPtrInfo,
8930 MachinePointerInfo SrcPtrInfo,
8931 const AAMDNodes &AAInfo) {
8932 // Turn a memmove of undef to nop.
8933 // FIXME: We need to honor volatile even is Src is undef.
8934 if (Src.isUndef())
8935 return Chain;
8936
8937 // Expand memmove to a series of load and store ops if the size operand falls
8938 // below a certain threshold.
8939 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8940 const DataLayout &DL = DAG.getDataLayout();
8941 LLVMContext &C = *DAG.getContext();
8942 std::vector<EVT> MemOps;
8943 bool DstAlignCanChange = false;
8945 MachineFrameInfo &MFI = MF.getFrameInfo();
8946 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8948 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8949 DstAlignCanChange = true;
8950 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8951 if (!SrcAlign || Alignment > *SrcAlign)
8952 SrcAlign = Alignment;
8953 assert(SrcAlign && "SrcAlign must be set");
8954 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8955 if (!TLI.findOptimalMemOpLowering(
8956 C, MemOps, Limit,
8957 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8958 /*IsVolatile*/ true),
8959 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8960 MF.getFunction().getAttributes()))
8961 return SDValue();
8962
8963 if (DstAlignCanChange) {
8964 Type *Ty = MemOps[0].getTypeForEVT(C);
8965 Align NewAlign = DL.getABITypeAlign(Ty);
8966
8967 // Don't promote to an alignment that would require dynamic stack
8968 // realignment which may conflict with optimizations such as tail call
8969 // optimization.
8971 if (!TRI->hasStackRealignment(MF))
8972 if (MaybeAlign StackAlign = DL.getStackAlignment())
8973 NewAlign = std::min(NewAlign, *StackAlign);
8974
8975 if (NewAlign > Alignment) {
8976 // Give the stack frame object a larger alignment if needed.
8977 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8978 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8979 Alignment = NewAlign;
8980 }
8981 }
8982
8983 // Prepare AAInfo for loads/stores after lowering this memmove.
8984 AAMDNodes NewAAInfo = AAInfo;
8985 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8986
8987 MachineMemOperand::Flags MMOFlags =
8989 uint64_t SrcOff = 0, DstOff = 0;
8990 SmallVector<SDValue, 8> LoadValues;
8991 SmallVector<SDValue, 8> LoadChains;
8992 SmallVector<SDValue, 8> OutChains;
8993 unsigned NumMemOps = MemOps.size();
8994 for (unsigned i = 0; i < NumMemOps; i++) {
8995 EVT VT = MemOps[i];
8996 unsigned VTSize = VT.getSizeInBits() / 8;
8997 SDValue Value;
8998
8999 bool isDereferenceable =
9000 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9001 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9002 if (isDereferenceable)
9004
9005 Value = DAG.getLoad(
9006 VT, dl, Chain,
9007 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9008 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
9009 LoadValues.push_back(Value);
9010 LoadChains.push_back(Value.getValue(1));
9011 SrcOff += VTSize;
9012 }
9013 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9014 OutChains.clear();
9015 for (unsigned i = 0; i < NumMemOps; i++) {
9016 EVT VT = MemOps[i];
9017 unsigned VTSize = VT.getSizeInBits() / 8;
9018 SDValue Store;
9019
9020 Store = DAG.getStore(
9021 Chain, dl, LoadValues[i],
9022 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9023 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9024 OutChains.push_back(Store);
9025 DstOff += VTSize;
9026 }
9027
9028 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9029}
9030
9031/// Lower the call to 'memset' intrinsic function into a series of store
9032/// operations.
9033///
9034/// \param DAG Selection DAG where lowered code is placed.
9035/// \param dl Link to corresponding IR location.
9036/// \param Chain Control flow dependency.
9037/// \param Dst Pointer to destination memory location.
9038/// \param Src Value of byte to write into the memory.
9039/// \param Size Number of bytes to write.
9040/// \param Alignment Alignment of the destination in bytes.
9041/// \param isVol True if destination is volatile.
9042/// \param AlwaysInline Makes sure no function call is generated.
9043/// \param DstPtrInfo IR information on the memory pointer.
9044/// \returns New head in the control flow, if lowering was successful, empty
9045/// SDValue otherwise.
9046///
9047/// The function tries to replace 'llvm.memset' intrinsic with several store
9048/// operations and value calculation code. This is usually profitable for small
9049/// memory size or when the semantic requires inlining.
9051 SDValue Chain, SDValue Dst, SDValue Src,
9052 uint64_t Size, Align Alignment, bool isVol,
9053 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9054 const AAMDNodes &AAInfo) {
9055 // Turn a memset of undef to nop.
9056 // FIXME: We need to honor volatile even is Src is undef.
9057 if (Src.isUndef())
9058 return Chain;
9059
9060 // Expand memset to a series of load/store ops if the size operand
9061 // falls below a certain threshold.
9062 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9063 std::vector<EVT> MemOps;
9064 bool DstAlignCanChange = false;
9065 LLVMContext &C = *DAG.getContext();
9067 MachineFrameInfo &MFI = MF.getFrameInfo();
9068 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9070 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9071 DstAlignCanChange = true;
9072 bool IsZeroVal = isNullConstant(Src);
9073 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9074
9075 if (!TLI.findOptimalMemOpLowering(
9076 C, MemOps, Limit,
9077 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9078 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes()))
9079 return SDValue();
9080
9081 if (DstAlignCanChange) {
9082 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9083 const DataLayout &DL = DAG.getDataLayout();
9084 Align NewAlign = DL.getABITypeAlign(Ty);
9085
9086 // Don't promote to an alignment that would require dynamic stack
9087 // realignment which may conflict with optimizations such as tail call
9088 // optimization.
9090 if (!TRI->hasStackRealignment(MF))
9091 if (MaybeAlign StackAlign = DL.getStackAlignment())
9092 NewAlign = std::min(NewAlign, *StackAlign);
9093
9094 if (NewAlign > Alignment) {
9095 // Give the stack frame object a larger alignment if needed.
9096 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9097 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9098 Alignment = NewAlign;
9099 }
9100 }
9101
9102 SmallVector<SDValue, 8> OutChains;
9103 uint64_t DstOff = 0;
9104 unsigned NumMemOps = MemOps.size();
9105
9106 // Find the largest store and generate the bit pattern for it.
9107 EVT LargestVT = MemOps[0];
9108 for (unsigned i = 1; i < NumMemOps; i++)
9109 if (MemOps[i].bitsGT(LargestVT))
9110 LargestVT = MemOps[i];
9111 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9112
9113 // Prepare AAInfo for loads/stores after lowering this memset.
9114 AAMDNodes NewAAInfo = AAInfo;
9115 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9116
9117 for (unsigned i = 0; i < NumMemOps; i++) {
9118 EVT VT = MemOps[i];
9119 unsigned VTSize = VT.getSizeInBits() / 8;
9120 if (VTSize > Size) {
9121 // Issuing an unaligned load / store pair that overlaps with the previous
9122 // pair. Adjust the offset accordingly.
9123 assert(i == NumMemOps-1 && i != 0);
9124 DstOff -= VTSize - Size;
9125 }
9126
9127 // If this store is smaller than the largest store see whether we can get
9128 // the smaller value for free with a truncate or extract vector element and
9129 // then store.
9130 SDValue Value = MemSetValue;
9131 if (VT.bitsLT(LargestVT)) {
9132 unsigned Index;
9133 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9134 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9135 if (!LargestVT.isVector() && !VT.isVector() &&
9136 TLI.isTruncateFree(LargestVT, VT))
9137 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9138 else if (LargestVT.isVector() && !VT.isVector() &&
9140 LargestVT.getTypeForEVT(*DAG.getContext()),
9141 VT.getSizeInBits(), Index) &&
9142 TLI.isTypeLegal(SVT) &&
9143 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9144 // Target which can combine store(extractelement VectorTy, Idx) can get
9145 // the smaller value for free.
9146 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9147 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9148 } else
9149 Value = getMemsetValue(Src, VT, DAG, dl);
9150 }
9151 assert(Value.getValueType() == VT && "Value with wrong type.");
9152 SDValue Store = DAG.getStore(
9153 Chain, dl, Value,
9154 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9155 DstPtrInfo.getWithOffset(DstOff), Alignment,
9157 NewAAInfo);
9158 OutChains.push_back(Store);
9159 DstOff += VT.getSizeInBits() / 8;
9160 Size -= VTSize;
9161 }
9162
9163 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9164}
9165
9167 unsigned AS) {
9168 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9169 // pointer operands can be losslessly bitcasted to pointers of address space 0
9170 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9171 report_fatal_error("cannot lower memory intrinsic in address space " +
9172 Twine(AS));
9173 }
9174}
9175
9177 const SelectionDAG *SelDAG,
9178 bool AllowReturnsFirstArg) {
9179 if (!CI || !CI->isTailCall())
9180 return false;
9181 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9182 // helper symbol we lower to.
9183 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9184 AllowReturnsFirstArg &&
9186}
9187
9188static std::pair<SDValue, SDValue>
9191 const CallInst *CI, RTLIB::Libcall Call,
9192 SelectionDAG *DAG, const TargetLowering *TLI) {
9193 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9194
9195 if (LCImpl == RTLIB::Unsupported)
9196 return {};
9197
9199 bool IsTailCall =
9200 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9201 SDValue Callee =
9202 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9203
9204 CLI.setDebugLoc(dl)
9205 .setChain(Chain)
9207 CI->getType(), Callee, std::move(Args))
9208 .setTailCall(IsTailCall);
9209
9210 return TLI->LowerCallTo(CLI);
9211}
9212
9213std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9214 const SDLoc &dl, SDValue S1,
9215 SDValue S2,
9216 const CallInst *CI) {
9218 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9219 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9220 RTLIB::STRSTR, this, TLI);
9221}
9222
9223std::pair<SDValue, SDValue>
9225 SDValue Mem1, SDValue Size, const CallInst *CI) {
9226 RTLIB::LibcallImpl MemcmpImpl = Libcalls->getLibcallImpl(RTLIB::MEMCMP);
9227 if (MemcmpImpl == RTLIB::Unsupported)
9228 return {};
9229
9232 {Mem0, PT},
9233 {Mem1, PT},
9235
9237 bool IsTailCall =
9238 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9239
9240 CLI.setDebugLoc(dl)
9241 .setChain(Chain)
9242 .setLibCallee(
9243 Libcalls->getLibcallImplCallingConv(MemcmpImpl),
9245 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9246 std::move(Args))
9247 .setTailCall(IsTailCall);
9248
9249 return TLI->LowerCallTo(CLI);
9250}
9251
9252std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9253 const SDLoc &dl,
9254 SDValue Dst, SDValue Src,
9255 const CallInst *CI) {
9256 RTLIB::LibcallImpl LCImpl = Libcalls->getLibcallImpl(RTLIB::STRCPY);
9257 if (LCImpl == RTLIB::Unsupported)
9258 return {};
9259
9261 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9262
9264 bool IsTailCall =
9265 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9266
9267 CLI.setDebugLoc(dl)
9268 .setChain(Chain)
9269 .setLibCallee(
9270 Libcalls->getLibcallImplCallingConv(LCImpl), CI->getType(),
9271 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9272 std::move(Args))
9273 .setTailCall(IsTailCall);
9274
9275 return TLI->LowerCallTo(CLI);
9276}
9277
9278std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9279 const SDLoc &dl,
9280 SDValue Src,
9281 const CallInst *CI) {
9282 RTLIB::LibcallImpl StrlenImpl = Libcalls->getLibcallImpl(RTLIB::STRLEN);
9283 if (StrlenImpl == RTLIB::Unsupported)
9284 return {};
9285
9286 // Emit a library call.
9289
9291 bool IsTailCall =
9292 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9293
9294 CLI.setDebugLoc(dl)
9295 .setChain(Chain)
9296 .setLibCallee(Libcalls->getLibcallImplCallingConv(StrlenImpl),
9297 CI->getType(),
9299 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9300 std::move(Args))
9301 .setTailCall(IsTailCall);
9302
9303 return TLI->LowerCallTo(CLI);
9304}
9305
9307 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9308 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9309 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9310 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9311 BatchAAResults *BatchAA) {
9312 // Check to see if we should lower the memcpy to loads and stores first.
9313 // For cases within the target-specified limits, this is the best choice.
9315 if (ConstantSize) {
9316 // Memcpy with size zero? Just return the original chain.
9317 if (ConstantSize->isZero())
9318 return Chain;
9319
9321 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9322 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9323 if (Result.getNode())
9324 return Result;
9325 }
9326
9327 // Then check to see if we should lower the memcpy with target-specific
9328 // code. If the target chooses to do this, this is the next best.
9329 if (TSI) {
9330 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9331 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9332 DstPtrInfo, SrcPtrInfo);
9333 if (Result.getNode())
9334 return Result;
9335 }
9336
9337 // If we really need inline code and the target declined to provide it,
9338 // use a (potentially long) sequence of loads and stores.
9339 if (AlwaysInline) {
9340 assert(ConstantSize && "AlwaysInline requires a constant size!");
9342 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9343 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9344 }
9345
9348
9349 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9350 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9351 // respect volatile, so they may do things like read or write memory
9352 // beyond the given memory regions. But fixing this isn't easy, and most
9353 // people don't care.
9354
9355 // Emit a library call.
9358 Args.emplace_back(Dst, PtrTy);
9359 Args.emplace_back(Src, PtrTy);
9360 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9361 // FIXME: pass in SDLoc
9363 bool IsTailCall = false;
9364 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9365
9366 if (OverrideTailCall.has_value()) {
9367 IsTailCall = *OverrideTailCall;
9368 } else {
9369 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9370 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9371 }
9372
9373 CLI.setDebugLoc(dl)
9374 .setChain(Chain)
9375 .setLibCallee(
9376 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9377 Dst.getValueType().getTypeForEVT(*getContext()),
9378 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9379 std::move(Args))
9381 .setTailCall(IsTailCall);
9382
9383 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9384 return CallResult.second;
9385}
9386
9388 SDValue Dst, SDValue Src, SDValue Size,
9389 Type *SizeTy, unsigned ElemSz,
9390 bool isTailCall,
9391 MachinePointerInfo DstPtrInfo,
9392 MachinePointerInfo SrcPtrInfo) {
9393 // Emit a library call.
9396 Args.emplace_back(Dst, ArgTy);
9397 Args.emplace_back(Src, ArgTy);
9398 Args.emplace_back(Size, SizeTy);
9399
9400 RTLIB::Libcall LibraryCall =
9402 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9403 if (LibcallImpl == RTLIB::Unsupported)
9404 report_fatal_error("Unsupported element size");
9405
9407 CLI.setDebugLoc(dl)
9408 .setChain(Chain)
9409 .setLibCallee(
9410 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9412 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9413 std::move(Args))
9415 .setTailCall(isTailCall);
9416
9417 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9418 return CallResult.second;
9419}
9420
9422 SDValue Src, SDValue Size, Align Alignment,
9423 bool isVol, const CallInst *CI,
9424 std::optional<bool> OverrideTailCall,
9425 MachinePointerInfo DstPtrInfo,
9426 MachinePointerInfo SrcPtrInfo,
9427 const AAMDNodes &AAInfo,
9428 BatchAAResults *BatchAA) {
9429 // Check to see if we should lower the memmove to loads and stores first.
9430 // For cases within the target-specified limits, this is the best choice.
9432 if (ConstantSize) {
9433 // Memmove with size zero? Just return the original chain.
9434 if (ConstantSize->isZero())
9435 return Chain;
9436
9438 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9439 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9440 if (Result.getNode())
9441 return Result;
9442 }
9443
9444 // Then check to see if we should lower the memmove with target-specific
9445 // code. If the target chooses to do this, this is the next best.
9446 if (TSI) {
9447 SDValue Result =
9448 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9449 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9450 if (Result.getNode())
9451 return Result;
9452 }
9453
9456
9457 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9458 // not be safe. See memcpy above for more details.
9459
9460 // Emit a library call.
9463 Args.emplace_back(Dst, PtrTy);
9464 Args.emplace_back(Src, PtrTy);
9465 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9466 // FIXME: pass in SDLoc
9468
9469 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9470
9471 bool IsTailCall = false;
9472 if (OverrideTailCall.has_value()) {
9473 IsTailCall = *OverrideTailCall;
9474 } else {
9475 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9476 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9477 }
9478
9479 CLI.setDebugLoc(dl)
9480 .setChain(Chain)
9481 .setLibCallee(
9482 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9483 Dst.getValueType().getTypeForEVT(*getContext()),
9484 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9485 std::move(Args))
9487 .setTailCall(IsTailCall);
9488
9489 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9490 return CallResult.second;
9491}
9492
9494 SDValue Dst, SDValue Src, SDValue Size,
9495 Type *SizeTy, unsigned ElemSz,
9496 bool isTailCall,
9497 MachinePointerInfo DstPtrInfo,
9498 MachinePointerInfo SrcPtrInfo) {
9499 // Emit a library call.
9501 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9502 Args.emplace_back(Dst, IntPtrTy);
9503 Args.emplace_back(Src, IntPtrTy);
9504 Args.emplace_back(Size, SizeTy);
9505
9506 RTLIB::Libcall LibraryCall =
9508 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9509 if (LibcallImpl == RTLIB::Unsupported)
9510 report_fatal_error("Unsupported element size");
9511
9513 CLI.setDebugLoc(dl)
9514 .setChain(Chain)
9515 .setLibCallee(
9516 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9518 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9519 std::move(Args))
9521 .setTailCall(isTailCall);
9522
9523 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9524 return CallResult.second;
9525}
9526
9528 SDValue Src, SDValue Size, Align Alignment,
9529 bool isVol, bool AlwaysInline,
9530 const CallInst *CI,
9531 MachinePointerInfo DstPtrInfo,
9532 const AAMDNodes &AAInfo) {
9533 // Check to see if we should lower the memset to stores first.
9534 // For cases within the target-specified limits, this is the best choice.
9536 if (ConstantSize) {
9537 // Memset with size zero? Just return the original chain.
9538 if (ConstantSize->isZero())
9539 return Chain;
9540
9541 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9542 ConstantSize->getZExtValue(), Alignment,
9543 isVol, false, DstPtrInfo, AAInfo);
9544
9545 if (Result.getNode())
9546 return Result;
9547 }
9548
9549 // Then check to see if we should lower the memset with target-specific
9550 // code. If the target chooses to do this, this is the next best.
9551 if (TSI) {
9552 SDValue Result = TSI->EmitTargetCodeForMemset(
9553 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9554 if (Result.getNode())
9555 return Result;
9556 }
9557
9558 // If we really need inline code and the target declined to provide it,
9559 // use a (potentially long) sequence of loads and stores.
9560 if (AlwaysInline) {
9561 assert(ConstantSize && "AlwaysInline requires a constant size!");
9562 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9563 ConstantSize->getZExtValue(), Alignment,
9564 isVol, true, DstPtrInfo, AAInfo);
9565 assert(Result &&
9566 "getMemsetStores must return a valid sequence when AlwaysInline");
9567 return Result;
9568 }
9569
9571
9572 // Emit a library call.
9573 auto &Ctx = *getContext();
9574 const auto& DL = getDataLayout();
9575
9577 // FIXME: pass in SDLoc
9578 CLI.setDebugLoc(dl).setChain(Chain);
9579
9580 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
9581 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9582
9583 // If zeroing out and bzero is present, use it.
9584 if (UseBZero) {
9586 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9587 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9588 CLI.setLibCallee(
9589 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9590 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9591 } else {
9592 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9593
9595 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9596 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9597 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9598 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
9599 Dst.getValueType().getTypeForEVT(Ctx),
9600 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9601 std::move(Args));
9602 }
9603
9604 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9605 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9606
9607 // If we're going to use bzero, make sure not to tail call unless the
9608 // subsequent return doesn't need a value, as bzero doesn't return the first
9609 // arg unlike memset.
9610 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9611 bool IsTailCall =
9612 CI && CI->isTailCall() &&
9613 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9614 CLI.setDiscardResult().setTailCall(IsTailCall);
9615
9616 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9617 return CallResult.second;
9618}
9619
9622 Type *SizeTy, unsigned ElemSz,
9623 bool isTailCall,
9624 MachinePointerInfo DstPtrInfo) {
9625 // Emit a library call.
9627 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9628 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9629 Args.emplace_back(Size, SizeTy);
9630
9631 RTLIB::Libcall LibraryCall =
9633 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9634 if (LibcallImpl == RTLIB::Unsupported)
9635 report_fatal_error("Unsupported element size");
9636
9638 CLI.setDebugLoc(dl)
9639 .setChain(Chain)
9640 .setLibCallee(
9641 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9643 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9644 std::move(Args))
9646 .setTailCall(isTailCall);
9647
9648 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9649 return CallResult.second;
9650}
9651
9652SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9654 MachineMemOperand *MMO,
9655 ISD::LoadExtType ExtType) {
9657 AddNodeIDNode(ID, Opcode, VTList, Ops);
9658 ID.AddInteger(MemVT.getRawBits());
9659 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9660 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9661 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9662 ID.AddInteger(MMO->getFlags());
9663 void* IP = nullptr;
9664 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9665 E->refineAlignment(MMO);
9666 E->refineRanges(MMO);
9667 return SDValue(E, 0);
9668 }
9669
9670 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9671 VTList, MemVT, MMO, ExtType);
9672 createOperands(N, Ops);
9673
9674 CSEMap.InsertNode(N, IP);
9675 InsertNode(N);
9676 SDValue V(N, 0);
9677 NewSDValueDbgMsg(V, "Creating new node: ", this);
9678 return V;
9679}
9680
9682 EVT MemVT, SDVTList VTs, SDValue Chain,
9683 SDValue Ptr, SDValue Cmp, SDValue Swp,
9684 MachineMemOperand *MMO) {
9685 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9687 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9688
9689 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9690 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9691}
9692
9693SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9694 SDValue Chain, SDValue Ptr, SDValue Val,
9695 MachineMemOperand *MMO) {
9696 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9697 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9698 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9699 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9700 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9701 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9702 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9703 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9704 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9705 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9706 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9707 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9708 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9709 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9710 Opcode == ISD::ATOMIC_STORE) &&
9711 "Invalid Atomic Op");
9712
9713 EVT VT = Val.getValueType();
9714
9715 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9716 getVTList(VT, MVT::Other);
9717 SDValue Ops[] = {Chain, Ptr, Val};
9718 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9719}
9720
9722 EVT MemVT, EVT VT, SDValue Chain,
9723 SDValue Ptr, MachineMemOperand *MMO) {
9724 SDVTList VTs = getVTList(VT, MVT::Other);
9725 SDValue Ops[] = {Chain, Ptr};
9726 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9727}
9728
9729/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9731 if (Ops.size() == 1)
9732 return Ops[0];
9733
9735 VTs.reserve(Ops.size());
9736 for (const SDValue &Op : Ops)
9737 VTs.push_back(Op.getValueType());
9738 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9739}
9740
9742 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9743 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9745 const AAMDNodes &AAInfo) {
9746 if (Size.hasValue() && !Size.getValue())
9748
9750 MachineMemOperand *MMO =
9751 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9752
9753 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9754}
9755
9757 SDVTList VTList,
9758 ArrayRef<SDValue> Ops, EVT MemVT,
9759 MachineMemOperand *MMO) {
9760 assert(
9761 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9762 Opcode == ISD::PREFETCH ||
9763 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9764 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9765 "Opcode is not a memory-accessing opcode!");
9766
9767 // Memoize the node unless it returns a glue result.
9769 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9771 AddNodeIDNode(ID, Opcode, VTList, Ops);
9772 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9773 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9774 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9775 ID.AddInteger(MMO->getFlags());
9776 ID.AddInteger(MemVT.getRawBits());
9777 void *IP = nullptr;
9778 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9779 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9780 return SDValue(E, 0);
9781 }
9782
9783 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9784 VTList, MemVT, MMO);
9785 createOperands(N, Ops);
9786
9787 CSEMap.InsertNode(N, IP);
9788 } else {
9789 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9790 VTList, MemVT, MMO);
9791 createOperands(N, Ops);
9792 }
9793 InsertNode(N);
9794 SDValue V(N, 0);
9795 NewSDValueDbgMsg(V, "Creating new node: ", this);
9796 return V;
9797}
9798
9800 SDValue Chain, int FrameIndex) {
9801 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9802 const auto VTs = getVTList(MVT::Other);
9803 SDValue Ops[2] = {
9804 Chain,
9805 getFrameIndex(FrameIndex,
9806 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9807 true)};
9808
9810 AddNodeIDNode(ID, Opcode, VTs, Ops);
9811 ID.AddInteger(FrameIndex);
9812 void *IP = nullptr;
9813 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9814 return SDValue(E, 0);
9815
9816 LifetimeSDNode *N =
9817 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9818 createOperands(N, Ops);
9819 CSEMap.InsertNode(N, IP);
9820 InsertNode(N);
9821 SDValue V(N, 0);
9822 NewSDValueDbgMsg(V, "Creating new node: ", this);
9823 return V;
9824}
9825
9827 uint64_t Guid, uint64_t Index,
9828 uint32_t Attr) {
9829 const unsigned Opcode = ISD::PSEUDO_PROBE;
9830 const auto VTs = getVTList(MVT::Other);
9831 SDValue Ops[] = {Chain};
9833 AddNodeIDNode(ID, Opcode, VTs, Ops);
9834 ID.AddInteger(Guid);
9835 ID.AddInteger(Index);
9836 void *IP = nullptr;
9837 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9838 return SDValue(E, 0);
9839
9840 auto *N = newSDNode<PseudoProbeSDNode>(
9841 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9842 createOperands(N, Ops);
9843 CSEMap.InsertNode(N, IP);
9844 InsertNode(N);
9845 SDValue V(N, 0);
9846 NewSDValueDbgMsg(V, "Creating new node: ", this);
9847 return V;
9848}
9849
9850/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9851/// MachinePointerInfo record from it. This is particularly useful because the
9852/// code generator has many cases where it doesn't bother passing in a
9853/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9855 SelectionDAG &DAG, SDValue Ptr,
9856 int64_t Offset = 0) {
9857 // If this is FI+Offset, we can model it.
9858 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9860 FI->getIndex(), Offset);
9861
9862 // If this is (FI+Offset1)+Offset2, we can model it.
9863 if (Ptr.getOpcode() != ISD::ADD ||
9866 return Info;
9867
9868 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9870 DAG.getMachineFunction(), FI,
9871 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9872}
9873
9874/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9875/// MachinePointerInfo record from it. This is particularly useful because the
9876/// code generator has many cases where it doesn't bother passing in a
9877/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9879 SelectionDAG &DAG, SDValue Ptr,
9880 SDValue OffsetOp) {
9881 // If the 'Offset' value isn't a constant, we can't handle this.
9883 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9884 if (OffsetOp.isUndef())
9885 return InferPointerInfo(Info, DAG, Ptr);
9886 return Info;
9887}
9888
9890 EVT VT, const SDLoc &dl, SDValue Chain,
9891 SDValue Ptr, SDValue Offset,
9892 MachinePointerInfo PtrInfo, EVT MemVT,
9893 Align Alignment,
9894 MachineMemOperand::Flags MMOFlags,
9895 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9896 assert(Chain.getValueType() == MVT::Other &&
9897 "Invalid chain type");
9898
9899 MMOFlags |= MachineMemOperand::MOLoad;
9900 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9901 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9902 // clients.
9903 if (PtrInfo.V.isNull())
9904 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9905
9906 TypeSize Size = MemVT.getStoreSize();
9908 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9909 Alignment, AAInfo, Ranges);
9910 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9911}
9912
9914 EVT VT, const SDLoc &dl, SDValue Chain,
9915 SDValue Ptr, SDValue Offset, EVT MemVT,
9916 MachineMemOperand *MMO) {
9917 if (VT == MemVT) {
9918 ExtType = ISD::NON_EXTLOAD;
9919 } else if (ExtType == ISD::NON_EXTLOAD) {
9920 assert(VT == MemVT && "Non-extending load from different memory type!");
9921 } else {
9922 // Extending load.
9923 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9924 "Should only be an extending load, not truncating!");
9925 assert(VT.isInteger() == MemVT.isInteger() &&
9926 "Cannot convert from FP to Int or Int -> FP!");
9927 assert(VT.isVector() == MemVT.isVector() &&
9928 "Cannot use an ext load to convert to or from a vector!");
9929 assert((!VT.isVector() ||
9931 "Cannot use an ext load to change the number of vector elements!");
9932 }
9933
9934 assert((!MMO->getRanges() ||
9936 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9937 MemVT.isInteger())) &&
9938 "Range metadata and load type must match!");
9939
9940 bool Indexed = AM != ISD::UNINDEXED;
9941 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9942
9943 SDVTList VTs = Indexed ?
9944 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9945 SDValue Ops[] = { Chain, Ptr, Offset };
9947 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9948 ID.AddInteger(MemVT.getRawBits());
9949 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
9950 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
9951 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9952 ID.AddInteger(MMO->getFlags());
9953 void *IP = nullptr;
9954 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9955 E->refineAlignment(MMO);
9956 E->refineRanges(MMO);
9957 return SDValue(E, 0);
9958 }
9959 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
9960 ExtType, MemVT, MMO);
9961 createOperands(N, Ops);
9962
9963 CSEMap.InsertNode(N, IP);
9964 InsertNode(N);
9965 SDValue V(N, 0);
9966 NewSDValueDbgMsg(V, "Creating new node: ", this);
9967 return V;
9968}
9969
9971 SDValue Ptr, MachinePointerInfo PtrInfo,
9972 MaybeAlign Alignment,
9973 MachineMemOperand::Flags MMOFlags,
9974 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9975 SDValue Undef = getUNDEF(Ptr.getValueType());
9976 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9977 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
9978}
9979
9981 SDValue Ptr, MachineMemOperand *MMO) {
9982 SDValue Undef = getUNDEF(Ptr.getValueType());
9983 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
9984 VT, MMO);
9985}
9986
9988 EVT VT, SDValue Chain, SDValue Ptr,
9989 MachinePointerInfo PtrInfo, EVT MemVT,
9990 MaybeAlign Alignment,
9991 MachineMemOperand::Flags MMOFlags,
9992 const AAMDNodes &AAInfo) {
9993 SDValue Undef = getUNDEF(Ptr.getValueType());
9994 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
9995 MemVT, Alignment, MMOFlags, AAInfo);
9996}
9997
9999 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10000 MachineMemOperand *MMO) {
10001 SDValue Undef = getUNDEF(Ptr.getValueType());
10002 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10003 MemVT, MMO);
10004}
10005
10009 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10010 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10011 // Don't propagate the invariant or dereferenceable flags.
10012 auto MMOFlags =
10013 LD->getMemOperand()->getFlags() &
10015 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10016 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10017 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10018}
10019
10021 SDValue Ptr, MachinePointerInfo PtrInfo,
10022 Align Alignment,
10023 MachineMemOperand::Flags MMOFlags,
10024 const AAMDNodes &AAInfo) {
10025 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10026
10027 MMOFlags |= MachineMemOperand::MOStore;
10028 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10029
10030 if (PtrInfo.V.isNull())
10031 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10032
10035 MachineMemOperand *MMO =
10036 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10037 return getStore(Chain, dl, Val, Ptr, MMO);
10038}
10039
10041 SDValue Ptr, MachineMemOperand *MMO) {
10042 SDValue Undef = getUNDEF(Ptr.getValueType());
10043 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10045}
10046
10048 SDValue Ptr, SDValue Offset, EVT SVT,
10050 bool IsTruncating) {
10051 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10052 EVT VT = Val.getValueType();
10053 if (VT == SVT) {
10054 IsTruncating = false;
10055 } else if (!IsTruncating) {
10056 assert(VT == SVT && "No-truncating store from different memory type!");
10057 } else {
10059 "Should only be a truncating store, not extending!");
10060 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10061 assert(VT.isVector() == SVT.isVector() &&
10062 "Cannot use trunc store to convert to or from a vector!");
10063 assert((!VT.isVector() ||
10065 "Cannot use trunc store to change the number of vector elements!");
10066 }
10067
10068 bool Indexed = AM != ISD::UNINDEXED;
10069 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10070 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10071 : getVTList(MVT::Other);
10072 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10075 ID.AddInteger(SVT.getRawBits());
10076 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10077 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10078 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10079 ID.AddInteger(MMO->getFlags());
10080 void *IP = nullptr;
10081 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10082 cast<StoreSDNode>(E)->refineAlignment(MMO);
10083 return SDValue(E, 0);
10084 }
10085 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10086 IsTruncating, SVT, MMO);
10087 createOperands(N, Ops);
10088
10089 CSEMap.InsertNode(N, IP);
10090 InsertNode(N);
10091 SDValue V(N, 0);
10092 NewSDValueDbgMsg(V, "Creating new node: ", this);
10093 return V;
10094}
10095
10097 SDValue Ptr, MachinePointerInfo PtrInfo,
10098 EVT SVT, Align Alignment,
10099 MachineMemOperand::Flags MMOFlags,
10100 const AAMDNodes &AAInfo) {
10101 assert(Chain.getValueType() == MVT::Other &&
10102 "Invalid chain type");
10103
10104 MMOFlags |= MachineMemOperand::MOStore;
10105 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10106
10107 if (PtrInfo.V.isNull())
10108 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10109
10111 MachineMemOperand *MMO = MF.getMachineMemOperand(
10112 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10113 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10114}
10115
10117 SDValue Ptr, EVT SVT,
10118 MachineMemOperand *MMO) {
10119 SDValue Undef = getUNDEF(Ptr.getValueType());
10120 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10121}
10122
10126 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10127 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10128 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10129 ST->getMemoryVT(), ST->getMemOperand(), AM,
10130 ST->isTruncatingStore());
10131}
10132
10134 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10135 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10136 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10137 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10138 const MDNode *Ranges, bool IsExpanding) {
10139 MMOFlags |= MachineMemOperand::MOLoad;
10140 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10141 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10142 // clients.
10143 if (PtrInfo.V.isNull())
10144 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10145
10146 TypeSize Size = MemVT.getStoreSize();
10148 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10149 Alignment, AAInfo, Ranges);
10150 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10151 MMO, IsExpanding);
10152}
10153
10155 ISD::LoadExtType ExtType, EVT VT,
10156 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10157 SDValue Offset, SDValue Mask, SDValue EVL,
10158 EVT MemVT, MachineMemOperand *MMO,
10159 bool IsExpanding) {
10160 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10161 assert(Mask.getValueType().getVectorElementCount() ==
10162 VT.getVectorElementCount() &&
10163 "Vector width mismatch between mask and data");
10164
10165 bool Indexed = AM != ISD::UNINDEXED;
10166 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10167
10168 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10169 : getVTList(VT, MVT::Other);
10170 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10172 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10173 ID.AddInteger(MemVT.getRawBits());
10174 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10175 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10176 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10177 ID.AddInteger(MMO->getFlags());
10178 void *IP = nullptr;
10179 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10180 E->refineAlignment(MMO);
10181 E->refineRanges(MMO);
10182 return SDValue(E, 0);
10183 }
10184 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10185 ExtType, IsExpanding, MemVT, MMO);
10186 createOperands(N, Ops);
10187
10188 CSEMap.InsertNode(N, IP);
10189 InsertNode(N);
10190 SDValue V(N, 0);
10191 NewSDValueDbgMsg(V, "Creating new node: ", this);
10192 return V;
10193}
10194
10196 SDValue Ptr, SDValue Mask, SDValue EVL,
10197 MachinePointerInfo PtrInfo,
10198 MaybeAlign Alignment,
10199 MachineMemOperand::Flags MMOFlags,
10200 const AAMDNodes &AAInfo, const MDNode *Ranges,
10201 bool IsExpanding) {
10202 SDValue Undef = getUNDEF(Ptr.getValueType());
10203 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10204 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10205 IsExpanding);
10206}
10207
10209 SDValue Ptr, SDValue Mask, SDValue EVL,
10210 MachineMemOperand *MMO, bool IsExpanding) {
10211 SDValue Undef = getUNDEF(Ptr.getValueType());
10212 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10213 Mask, EVL, VT, MMO, IsExpanding);
10214}
10215
10217 EVT VT, SDValue Chain, SDValue Ptr,
10218 SDValue Mask, SDValue EVL,
10219 MachinePointerInfo PtrInfo, EVT MemVT,
10220 MaybeAlign Alignment,
10221 MachineMemOperand::Flags MMOFlags,
10222 const AAMDNodes &AAInfo, bool IsExpanding) {
10223 SDValue Undef = getUNDEF(Ptr.getValueType());
10224 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10225 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10226 IsExpanding);
10227}
10228
10230 EVT VT, SDValue Chain, SDValue Ptr,
10231 SDValue Mask, SDValue EVL, EVT MemVT,
10232 MachineMemOperand *MMO, bool IsExpanding) {
10233 SDValue Undef = getUNDEF(Ptr.getValueType());
10234 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10235 EVL, MemVT, MMO, IsExpanding);
10236}
10237
10241 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10242 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10243 // Don't propagate the invariant or dereferenceable flags.
10244 auto MMOFlags =
10245 LD->getMemOperand()->getFlags() &
10247 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10248 LD->getChain(), Base, Offset, LD->getMask(),
10249 LD->getVectorLength(), LD->getPointerInfo(),
10250 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10251 nullptr, LD->isExpandingLoad());
10252}
10253
10255 SDValue Ptr, SDValue Offset, SDValue Mask,
10256 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10257 ISD::MemIndexedMode AM, bool IsTruncating,
10258 bool IsCompressing) {
10259 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10260 assert(Mask.getValueType().getVectorElementCount() ==
10262 "Vector width mismatch between mask and data");
10263
10264 bool Indexed = AM != ISD::UNINDEXED;
10265 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10266 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10267 : getVTList(MVT::Other);
10268 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10270 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10271 ID.AddInteger(MemVT.getRawBits());
10272 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10273 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10274 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10275 ID.AddInteger(MMO->getFlags());
10276 void *IP = nullptr;
10277 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10278 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10279 return SDValue(E, 0);
10280 }
10281 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10282 IsTruncating, IsCompressing, MemVT, MMO);
10283 createOperands(N, Ops);
10284
10285 CSEMap.InsertNode(N, IP);
10286 InsertNode(N);
10287 SDValue V(N, 0);
10288 NewSDValueDbgMsg(V, "Creating new node: ", this);
10289 return V;
10290}
10291
10293 SDValue Val, SDValue Ptr, SDValue Mask,
10294 SDValue EVL, MachinePointerInfo PtrInfo,
10295 EVT SVT, Align Alignment,
10296 MachineMemOperand::Flags MMOFlags,
10297 const AAMDNodes &AAInfo,
10298 bool IsCompressing) {
10299 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10300
10301 MMOFlags |= MachineMemOperand::MOStore;
10302 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10303
10304 if (PtrInfo.V.isNull())
10305 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10306
10308 MachineMemOperand *MMO = MF.getMachineMemOperand(
10309 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10310 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10311 IsCompressing);
10312}
10313
10315 SDValue Val, SDValue Ptr, SDValue Mask,
10316 SDValue EVL, EVT SVT,
10317 MachineMemOperand *MMO,
10318 bool IsCompressing) {
10319 EVT VT = Val.getValueType();
10320
10321 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10322 if (VT == SVT)
10323 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10324 EVL, VT, MMO, ISD::UNINDEXED,
10325 /*IsTruncating*/ false, IsCompressing);
10326
10328 "Should only be a truncating store, not extending!");
10329 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10330 assert(VT.isVector() == SVT.isVector() &&
10331 "Cannot use trunc store to convert to or from a vector!");
10332 assert((!VT.isVector() ||
10334 "Cannot use trunc store to change the number of vector elements!");
10335
10336 SDVTList VTs = getVTList(MVT::Other);
10337 SDValue Undef = getUNDEF(Ptr.getValueType());
10338 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10340 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10341 ID.AddInteger(SVT.getRawBits());
10342 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10343 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10344 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10345 ID.AddInteger(MMO->getFlags());
10346 void *IP = nullptr;
10347 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10348 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10349 return SDValue(E, 0);
10350 }
10351 auto *N =
10352 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10353 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10354 createOperands(N, Ops);
10355
10356 CSEMap.InsertNode(N, IP);
10357 InsertNode(N);
10358 SDValue V(N, 0);
10359 NewSDValueDbgMsg(V, "Creating new node: ", this);
10360 return V;
10361}
10362
10366 auto *ST = cast<VPStoreSDNode>(OrigStore);
10367 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10368 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10369 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10370 Offset, ST->getMask(), ST->getVectorLength()};
10372 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10373 ID.AddInteger(ST->getMemoryVT().getRawBits());
10374 ID.AddInteger(ST->getRawSubclassData());
10375 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10376 ID.AddInteger(ST->getMemOperand()->getFlags());
10377 void *IP = nullptr;
10378 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10379 return SDValue(E, 0);
10380
10381 auto *N = newSDNode<VPStoreSDNode>(
10382 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10383 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10384 createOperands(N, Ops);
10385
10386 CSEMap.InsertNode(N, IP);
10387 InsertNode(N);
10388 SDValue V(N, 0);
10389 NewSDValueDbgMsg(V, "Creating new node: ", this);
10390 return V;
10391}
10392
10394 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10395 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10396 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10397 bool Indexed = AM != ISD::UNINDEXED;
10398 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10399
10400 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10401 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10402 : getVTList(VT, MVT::Other);
10404 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10405 ID.AddInteger(VT.getRawBits());
10406 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10407 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10408 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10409
10410 void *IP = nullptr;
10411 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10412 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10413 return SDValue(E, 0);
10414 }
10415
10416 auto *N =
10417 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10418 ExtType, IsExpanding, MemVT, MMO);
10419 createOperands(N, Ops);
10420 CSEMap.InsertNode(N, IP);
10421 InsertNode(N);
10422 SDValue V(N, 0);
10423 NewSDValueDbgMsg(V, "Creating new node: ", this);
10424 return V;
10425}
10426
10428 SDValue Ptr, SDValue Stride,
10429 SDValue Mask, SDValue EVL,
10430 MachineMemOperand *MMO,
10431 bool IsExpanding) {
10432 SDValue Undef = getUNDEF(Ptr.getValueType());
10433 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10434 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10435}
10436
10438 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10439 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10440 MachineMemOperand *MMO, bool IsExpanding) {
10441 SDValue Undef = getUNDEF(Ptr.getValueType());
10442 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10443 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10444}
10445
10447 SDValue Val, SDValue Ptr,
10448 SDValue Offset, SDValue Stride,
10449 SDValue Mask, SDValue EVL, EVT MemVT,
10450 MachineMemOperand *MMO,
10452 bool IsTruncating, bool IsCompressing) {
10453 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10454 bool Indexed = AM != ISD::UNINDEXED;
10455 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10456 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10457 : getVTList(MVT::Other);
10458 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10460 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10461 ID.AddInteger(MemVT.getRawBits());
10462 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10463 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10464 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10465 void *IP = nullptr;
10466 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10467 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10468 return SDValue(E, 0);
10469 }
10470 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10471 VTs, AM, IsTruncating,
10472 IsCompressing, MemVT, MMO);
10473 createOperands(N, Ops);
10474
10475 CSEMap.InsertNode(N, IP);
10476 InsertNode(N);
10477 SDValue V(N, 0);
10478 NewSDValueDbgMsg(V, "Creating new node: ", this);
10479 return V;
10480}
10481
10483 SDValue Val, SDValue Ptr,
10484 SDValue Stride, SDValue Mask,
10485 SDValue EVL, EVT SVT,
10486 MachineMemOperand *MMO,
10487 bool IsCompressing) {
10488 EVT VT = Val.getValueType();
10489
10490 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10491 if (VT == SVT)
10492 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10493 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10494 /*IsTruncating*/ false, IsCompressing);
10495
10497 "Should only be a truncating store, not extending!");
10498 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10499 assert(VT.isVector() == SVT.isVector() &&
10500 "Cannot use trunc store to convert to or from a vector!");
10501 assert((!VT.isVector() ||
10503 "Cannot use trunc store to change the number of vector elements!");
10504
10505 SDVTList VTs = getVTList(MVT::Other);
10506 SDValue Undef = getUNDEF(Ptr.getValueType());
10507 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10509 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10510 ID.AddInteger(SVT.getRawBits());
10511 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10512 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10513 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10514 void *IP = nullptr;
10515 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10516 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10517 return SDValue(E, 0);
10518 }
10519 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10520 VTs, ISD::UNINDEXED, true,
10521 IsCompressing, SVT, MMO);
10522 createOperands(N, Ops);
10523
10524 CSEMap.InsertNode(N, IP);
10525 InsertNode(N);
10526 SDValue V(N, 0);
10527 NewSDValueDbgMsg(V, "Creating new node: ", this);
10528 return V;
10529}
10530
10533 ISD::MemIndexType IndexType) {
10534 assert(Ops.size() == 6 && "Incompatible number of operands");
10535
10537 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10538 ID.AddInteger(VT.getRawBits());
10539 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10540 dl.getIROrder(), VTs, VT, MMO, IndexType));
10541 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10542 ID.AddInteger(MMO->getFlags());
10543 void *IP = nullptr;
10544 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10545 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10546 return SDValue(E, 0);
10547 }
10548
10549 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10550 VT, MMO, IndexType);
10551 createOperands(N, Ops);
10552
10553 assert(N->getMask().getValueType().getVectorElementCount() ==
10554 N->getValueType(0).getVectorElementCount() &&
10555 "Vector width mismatch between mask and data");
10556 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10557 N->getValueType(0).getVectorElementCount().isScalable() &&
10558 "Scalable flags of index and data do not match");
10560 N->getIndex().getValueType().getVectorElementCount(),
10561 N->getValueType(0).getVectorElementCount()) &&
10562 "Vector width mismatch between index and data");
10563 assert(isa<ConstantSDNode>(N->getScale()) &&
10564 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10565 "Scale should be a constant power of 2");
10566
10567 CSEMap.InsertNode(N, IP);
10568 InsertNode(N);
10569 SDValue V(N, 0);
10570 NewSDValueDbgMsg(V, "Creating new node: ", this);
10571 return V;
10572}
10573
10576 MachineMemOperand *MMO,
10577 ISD::MemIndexType IndexType) {
10578 assert(Ops.size() == 7 && "Incompatible number of operands");
10579
10581 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10582 ID.AddInteger(VT.getRawBits());
10583 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10584 dl.getIROrder(), VTs, VT, MMO, IndexType));
10585 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10586 ID.AddInteger(MMO->getFlags());
10587 void *IP = nullptr;
10588 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10589 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10590 return SDValue(E, 0);
10591 }
10592 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10593 VT, MMO, IndexType);
10594 createOperands(N, Ops);
10595
10596 assert(N->getMask().getValueType().getVectorElementCount() ==
10597 N->getValue().getValueType().getVectorElementCount() &&
10598 "Vector width mismatch between mask and data");
10599 assert(
10600 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10601 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10602 "Scalable flags of index and data do not match");
10604 N->getIndex().getValueType().getVectorElementCount(),
10605 N->getValue().getValueType().getVectorElementCount()) &&
10606 "Vector width mismatch between index and data");
10607 assert(isa<ConstantSDNode>(N->getScale()) &&
10608 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10609 "Scale should be a constant power of 2");
10610
10611 CSEMap.InsertNode(N, IP);
10612 InsertNode(N);
10613 SDValue V(N, 0);
10614 NewSDValueDbgMsg(V, "Creating new node: ", this);
10615 return V;
10616}
10617
10620 SDValue PassThru, EVT MemVT,
10621 MachineMemOperand *MMO,
10623 ISD::LoadExtType ExtTy, bool isExpanding) {
10624 bool Indexed = AM != ISD::UNINDEXED;
10625 assert((Indexed || Offset.isUndef()) &&
10626 "Unindexed masked load with an offset!");
10627 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10628 : getVTList(VT, MVT::Other);
10629 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10632 ID.AddInteger(MemVT.getRawBits());
10633 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10634 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10635 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10636 ID.AddInteger(MMO->getFlags());
10637 void *IP = nullptr;
10638 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10639 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10640 return SDValue(E, 0);
10641 }
10642 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10643 AM, ExtTy, isExpanding, MemVT, MMO);
10644 createOperands(N, Ops);
10645
10646 CSEMap.InsertNode(N, IP);
10647 InsertNode(N);
10648 SDValue V(N, 0);
10649 NewSDValueDbgMsg(V, "Creating new node: ", this);
10650 return V;
10651}
10652
10657 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10658 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10659 Offset, LD->getMask(), LD->getPassThru(),
10660 LD->getMemoryVT(), LD->getMemOperand(), AM,
10661 LD->getExtensionType(), LD->isExpandingLoad());
10662}
10663
10666 SDValue Mask, EVT MemVT,
10667 MachineMemOperand *MMO,
10668 ISD::MemIndexedMode AM, bool IsTruncating,
10669 bool IsCompressing) {
10670 assert(Chain.getValueType() == MVT::Other &&
10671 "Invalid chain type");
10672 bool Indexed = AM != ISD::UNINDEXED;
10673 assert((Indexed || Offset.isUndef()) &&
10674 "Unindexed masked store with an offset!");
10675 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10676 : getVTList(MVT::Other);
10677 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10680 ID.AddInteger(MemVT.getRawBits());
10681 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10682 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10683 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10684 ID.AddInteger(MMO->getFlags());
10685 void *IP = nullptr;
10686 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10687 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10688 return SDValue(E, 0);
10689 }
10690 auto *N =
10691 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10692 IsTruncating, IsCompressing, MemVT, MMO);
10693 createOperands(N, Ops);
10694
10695 CSEMap.InsertNode(N, IP);
10696 InsertNode(N);
10697 SDValue V(N, 0);
10698 NewSDValueDbgMsg(V, "Creating new node: ", this);
10699 return V;
10700}
10701
10706 assert(ST->getOffset().isUndef() &&
10707 "Masked store is already a indexed store!");
10708 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10709 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10710 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10711}
10712
10715 MachineMemOperand *MMO,
10716 ISD::MemIndexType IndexType,
10717 ISD::LoadExtType ExtTy) {
10718 assert(Ops.size() == 6 && "Incompatible number of operands");
10719
10722 ID.AddInteger(MemVT.getRawBits());
10723 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10724 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10725 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10726 ID.AddInteger(MMO->getFlags());
10727 void *IP = nullptr;
10728 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10729 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10730 return SDValue(E, 0);
10731 }
10732
10733 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10734 VTs, MemVT, MMO, IndexType, ExtTy);
10735 createOperands(N, Ops);
10736
10737 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10738 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10739 assert(N->getMask().getValueType().getVectorElementCount() ==
10740 N->getValueType(0).getVectorElementCount() &&
10741 "Vector width mismatch between mask and data");
10742 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10743 N->getValueType(0).getVectorElementCount().isScalable() &&
10744 "Scalable flags of index and data do not match");
10746 N->getIndex().getValueType().getVectorElementCount(),
10747 N->getValueType(0).getVectorElementCount()) &&
10748 "Vector width mismatch between index and data");
10749 assert(isa<ConstantSDNode>(N->getScale()) &&
10750 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10751 "Scale should be a constant power of 2");
10752
10753 CSEMap.InsertNode(N, IP);
10754 InsertNode(N);
10755 SDValue V(N, 0);
10756 NewSDValueDbgMsg(V, "Creating new node: ", this);
10757 return V;
10758}
10759
10762 MachineMemOperand *MMO,
10763 ISD::MemIndexType IndexType,
10764 bool IsTrunc) {
10765 assert(Ops.size() == 6 && "Incompatible number of operands");
10766
10769 ID.AddInteger(MemVT.getRawBits());
10770 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10771 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10772 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10773 ID.AddInteger(MMO->getFlags());
10774 void *IP = nullptr;
10775 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10776 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10777 return SDValue(E, 0);
10778 }
10779
10780 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10781 VTs, MemVT, MMO, IndexType, IsTrunc);
10782 createOperands(N, Ops);
10783
10784 assert(N->getMask().getValueType().getVectorElementCount() ==
10785 N->getValue().getValueType().getVectorElementCount() &&
10786 "Vector width mismatch between mask and data");
10787 assert(
10788 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10789 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10790 "Scalable flags of index and data do not match");
10792 N->getIndex().getValueType().getVectorElementCount(),
10793 N->getValue().getValueType().getVectorElementCount()) &&
10794 "Vector width mismatch between index and data");
10795 assert(isa<ConstantSDNode>(N->getScale()) &&
10796 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10797 "Scale should be a constant power of 2");
10798
10799 CSEMap.InsertNode(N, IP);
10800 InsertNode(N);
10801 SDValue V(N, 0);
10802 NewSDValueDbgMsg(V, "Creating new node: ", this);
10803 return V;
10804}
10805
10807 const SDLoc &dl, ArrayRef<SDValue> Ops,
10808 MachineMemOperand *MMO,
10809 ISD::MemIndexType IndexType) {
10810 assert(Ops.size() == 7 && "Incompatible number of operands");
10811
10814 ID.AddInteger(MemVT.getRawBits());
10815 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10816 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10817 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10818 ID.AddInteger(MMO->getFlags());
10819 void *IP = nullptr;
10820 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10821 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10822 return SDValue(E, 0);
10823 }
10824
10825 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10826 VTs, MemVT, MMO, IndexType);
10827 createOperands(N, Ops);
10828
10829 assert(N->getMask().getValueType().getVectorElementCount() ==
10830 N->getIndex().getValueType().getVectorElementCount() &&
10831 "Vector width mismatch between mask and data");
10832 assert(isa<ConstantSDNode>(N->getScale()) &&
10833 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10834 "Scale should be a constant power of 2");
10835 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10836
10837 CSEMap.InsertNode(N, IP);
10838 InsertNode(N);
10839 SDValue V(N, 0);
10840 NewSDValueDbgMsg(V, "Creating new node: ", this);
10841 return V;
10842}
10843
10845 SDValue Ptr, SDValue Mask, SDValue EVL,
10846 MachineMemOperand *MMO) {
10847 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10848 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10850 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10851 ID.AddInteger(VT.getRawBits());
10852 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10853 VTs, VT, MMO));
10854 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10855 ID.AddInteger(MMO->getFlags());
10856 void *IP = nullptr;
10857 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10858 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10859 return SDValue(E, 0);
10860 }
10861 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10862 VT, MMO);
10863 createOperands(N, Ops);
10864
10865 CSEMap.InsertNode(N, IP);
10866 InsertNode(N);
10867 SDValue V(N, 0);
10868 NewSDValueDbgMsg(V, "Creating new node: ", this);
10869 return V;
10870}
10871
10873 EVT MemVT, MachineMemOperand *MMO) {
10874 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10875 SDVTList VTs = getVTList(MVT::Other);
10876 SDValue Ops[] = {Chain, Ptr};
10879 ID.AddInteger(MemVT.getRawBits());
10880 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10881 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10882 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10883 ID.AddInteger(MMO->getFlags());
10884 void *IP = nullptr;
10885 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10886 return SDValue(E, 0);
10887
10888 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10889 dl.getDebugLoc(), VTs, MemVT, MMO);
10890 createOperands(N, Ops);
10891
10892 CSEMap.InsertNode(N, IP);
10893 InsertNode(N);
10894 SDValue V(N, 0);
10895 NewSDValueDbgMsg(V, "Creating new node: ", this);
10896 return V;
10897}
10898
10900 EVT MemVT, MachineMemOperand *MMO) {
10901 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10902 SDVTList VTs = getVTList(MVT::Other);
10903 SDValue Ops[] = {Chain, Ptr};
10906 ID.AddInteger(MemVT.getRawBits());
10907 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10908 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10909 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10910 ID.AddInteger(MMO->getFlags());
10911 void *IP = nullptr;
10912 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10913 return SDValue(E, 0);
10914
10915 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10916 dl.getDebugLoc(), VTs, MemVT, MMO);
10917 createOperands(N, Ops);
10918
10919 CSEMap.InsertNode(N, IP);
10920 InsertNode(N);
10921 SDValue V(N, 0);
10922 NewSDValueDbgMsg(V, "Creating new node: ", this);
10923 return V;
10924}
10925
10927 // select undef, T, F --> T (if T is a constant), otherwise F
10928 // select, ?, undef, F --> F
10929 // select, ?, T, undef --> T
10930 if (Cond.isUndef())
10931 return isConstantValueOfAnyType(T) ? T : F;
10932 if (T.isUndef())
10934 if (F.isUndef())
10936
10937 // select true, T, F --> T
10938 // select false, T, F --> F
10939 if (auto C = isBoolConstant(Cond))
10940 return *C ? T : F;
10941
10942 // select ?, T, T --> T
10943 if (T == F)
10944 return T;
10945
10946 return SDValue();
10947}
10948
10950 // shift undef, Y --> 0 (can always assume that the undef value is 0)
10951 if (X.isUndef())
10952 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
10953 // shift X, undef --> undef (because it may shift by the bitwidth)
10954 if (Y.isUndef())
10955 return getUNDEF(X.getValueType());
10956
10957 // shift 0, Y --> 0
10958 // shift X, 0 --> X
10960 return X;
10961
10962 // shift X, C >= bitwidth(X) --> undef
10963 // All vector elements must be too big (or undef) to avoid partial undefs.
10964 auto isShiftTooBig = [X](ConstantSDNode *Val) {
10965 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
10966 };
10967 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
10968 return getUNDEF(X.getValueType());
10969
10970 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
10971 if (X.getValueType().getScalarType() == MVT::i1)
10972 return X;
10973
10974 return SDValue();
10975}
10976
10978 SDNodeFlags Flags) {
10979 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
10980 // (an undef operand can be chosen to be Nan/Inf), then the result of this
10981 // operation is poison. That result can be relaxed to undef.
10982 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
10983 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
10984 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
10985 (YC && YC->getValueAPF().isNaN());
10986 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
10987 (YC && YC->getValueAPF().isInfinity());
10988
10989 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
10990 return getUNDEF(X.getValueType());
10991
10992 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
10993 return getUNDEF(X.getValueType());
10994
10995 if (!YC)
10996 return SDValue();
10997
10998 // X + -0.0 --> X
10999 if (Opcode == ISD::FADD)
11000 if (YC->getValueAPF().isNegZero())
11001 return X;
11002
11003 // X - +0.0 --> X
11004 if (Opcode == ISD::FSUB)
11005 if (YC->getValueAPF().isPosZero())
11006 return X;
11007
11008 // X * 1.0 --> X
11009 // X / 1.0 --> X
11010 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11011 if (YC->getValueAPF().isExactlyValue(1.0))
11012 return X;
11013
11014 // X * 0.0 --> 0.0
11015 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11016 if (YC->getValueAPF().isZero())
11017 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11018
11019 return SDValue();
11020}
11021
11023 SDValue Ptr, SDValue SV, unsigned Align) {
11024 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11025 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11026}
11027
11028SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11030 switch (Ops.size()) {
11031 case 0: return getNode(Opcode, DL, VT);
11032 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11033 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11034 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11035 default: break;
11036 }
11037
11038 // Copy from an SDUse array into an SDValue array for use with
11039 // the regular getNode logic.
11041 return getNode(Opcode, DL, VT, NewOps);
11042}
11043
11044SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11046 SDNodeFlags Flags;
11047 if (Inserter)
11048 Flags = Inserter->getFlags();
11049 return getNode(Opcode, DL, VT, Ops, Flags);
11050}
11051
11052SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11053 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11054 unsigned NumOps = Ops.size();
11055 switch (NumOps) {
11056 case 0: return getNode(Opcode, DL, VT);
11057 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11058 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11059 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11060 default: break;
11061 }
11062
11063#ifndef NDEBUG
11064 for (const auto &Op : Ops)
11065 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11066 "Operand is DELETED_NODE!");
11067#endif
11068
11069 switch (Opcode) {
11070 default: break;
11071 case ISD::BUILD_VECTOR:
11072 // Attempt to simplify BUILD_VECTOR.
11073 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11074 return V;
11075 break;
11077 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11078 return V;
11079 break;
11080 case ISD::SELECT_CC:
11081 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11082 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11083 "LHS and RHS of condition must have same type!");
11084 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11085 "True and False arms of SelectCC must have same type!");
11086 assert(Ops[2].getValueType() == VT &&
11087 "select_cc node must be of same type as true and false value!");
11088 assert((!Ops[0].getValueType().isVector() ||
11089 Ops[0].getValueType().getVectorElementCount() ==
11090 VT.getVectorElementCount()) &&
11091 "Expected select_cc with vector result to have the same sized "
11092 "comparison type!");
11093 break;
11094 case ISD::BR_CC:
11095 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11096 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11097 "LHS/RHS of comparison should match types!");
11098 break;
11099 case ISD::VP_ADD:
11100 case ISD::VP_SUB:
11101 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11102 if (VT.getScalarType() == MVT::i1)
11103 Opcode = ISD::VP_XOR;
11104 break;
11105 case ISD::VP_MUL:
11106 // If it is VP_MUL mask operation then turn it to VP_AND
11107 if (VT.getScalarType() == MVT::i1)
11108 Opcode = ISD::VP_AND;
11109 break;
11110 case ISD::VP_REDUCE_MUL:
11111 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11112 if (VT == MVT::i1)
11113 Opcode = ISD::VP_REDUCE_AND;
11114 break;
11115 case ISD::VP_REDUCE_ADD:
11116 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11117 if (VT == MVT::i1)
11118 Opcode = ISD::VP_REDUCE_XOR;
11119 break;
11120 case ISD::VP_REDUCE_SMAX:
11121 case ISD::VP_REDUCE_UMIN:
11122 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11123 // VP_REDUCE_AND.
11124 if (VT == MVT::i1)
11125 Opcode = ISD::VP_REDUCE_AND;
11126 break;
11127 case ISD::VP_REDUCE_SMIN:
11128 case ISD::VP_REDUCE_UMAX:
11129 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11130 // VP_REDUCE_OR.
11131 if (VT == MVT::i1)
11132 Opcode = ISD::VP_REDUCE_OR;
11133 break;
11134 }
11135
11136 // Memoize nodes.
11137 SDNode *N;
11138 SDVTList VTs = getVTList(VT);
11139
11140 if (VT != MVT::Glue) {
11142 AddNodeIDNode(ID, Opcode, VTs, Ops);
11143 void *IP = nullptr;
11144
11145 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11146 E->intersectFlagsWith(Flags);
11147 return SDValue(E, 0);
11148 }
11149
11150 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11151 createOperands(N, Ops);
11152
11153 CSEMap.InsertNode(N, IP);
11154 } else {
11155 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11156 createOperands(N, Ops);
11157 }
11158
11159 N->setFlags(Flags);
11160 InsertNode(N);
11161 SDValue V(N, 0);
11162 NewSDValueDbgMsg(V, "Creating new node: ", this);
11163 return V;
11164}
11165
11166SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11167 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11168 SDNodeFlags Flags;
11169 if (Inserter)
11170 Flags = Inserter->getFlags();
11171 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11172}
11173
11174SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11176 const SDNodeFlags Flags) {
11177 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11178}
11179
11180SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11182 SDNodeFlags Flags;
11183 if (Inserter)
11184 Flags = Inserter->getFlags();
11185 return getNode(Opcode, DL, VTList, Ops, Flags);
11186}
11187
11188SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11189 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11190 if (VTList.NumVTs == 1)
11191 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11192
11193#ifndef NDEBUG
11194 for (const auto &Op : Ops)
11195 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11196 "Operand is DELETED_NODE!");
11197#endif
11198
11199 switch (Opcode) {
11200 case ISD::SADDO:
11201 case ISD::UADDO:
11202 case ISD::SSUBO:
11203 case ISD::USUBO: {
11204 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11205 "Invalid add/sub overflow op!");
11206 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11207 Ops[0].getValueType() == Ops[1].getValueType() &&
11208 Ops[0].getValueType() == VTList.VTs[0] &&
11209 "Binary operator types must match!");
11210 SDValue N1 = Ops[0], N2 = Ops[1];
11211 canonicalizeCommutativeBinop(Opcode, N1, N2);
11212
11213 // (X +- 0) -> X with zero-overflow.
11214 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11215 /*AllowTruncation*/ true);
11216 if (N2CV && N2CV->isZero()) {
11217 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11218 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11219 }
11220
11221 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11222 VTList.VTs[1].getScalarType() == MVT::i1) {
11223 SDValue F1 = getFreeze(N1);
11224 SDValue F2 = getFreeze(N2);
11225 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11226 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11227 return getNode(ISD::MERGE_VALUES, DL, VTList,
11228 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11229 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11230 Flags);
11231 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11232 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11233 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11234 return getNode(ISD::MERGE_VALUES, DL, VTList,
11235 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11236 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11237 Flags);
11238 }
11239 }
11240 break;
11241 }
11242 case ISD::SADDO_CARRY:
11243 case ISD::UADDO_CARRY:
11244 case ISD::SSUBO_CARRY:
11245 case ISD::USUBO_CARRY:
11246 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11247 "Invalid add/sub overflow op!");
11248 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11249 Ops[0].getValueType() == Ops[1].getValueType() &&
11250 Ops[0].getValueType() == VTList.VTs[0] &&
11251 Ops[2].getValueType() == VTList.VTs[1] &&
11252 "Binary operator types must match!");
11253 break;
11254 case ISD::SMUL_LOHI:
11255 case ISD::UMUL_LOHI: {
11256 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11257 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11258 VTList.VTs[0] == Ops[0].getValueType() &&
11259 VTList.VTs[0] == Ops[1].getValueType() &&
11260 "Binary operator types must match!");
11261 // Constant fold.
11264 if (LHS && RHS) {
11265 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11266 unsigned OutWidth = Width * 2;
11267 APInt Val = LHS->getAPIntValue();
11268 APInt Mul = RHS->getAPIntValue();
11269 if (Opcode == ISD::SMUL_LOHI) {
11270 Val = Val.sext(OutWidth);
11271 Mul = Mul.sext(OutWidth);
11272 } else {
11273 Val = Val.zext(OutWidth);
11274 Mul = Mul.zext(OutWidth);
11275 }
11276 Val *= Mul;
11277
11278 SDValue Hi =
11279 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11280 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11281 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11282 }
11283 break;
11284 }
11285 case ISD::FFREXP: {
11286 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11287 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11288 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11289
11291 int FrexpExp;
11292 APFloat FrexpMant =
11293 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11294 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11295 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11296 DL, VTList.VTs[1]);
11297 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11298 }
11299
11300 break;
11301 }
11303 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11304 "Invalid STRICT_FP_EXTEND!");
11305 assert(VTList.VTs[0].isFloatingPoint() &&
11306 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11307 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11308 "STRICT_FP_EXTEND result type should be vector iff the operand "
11309 "type is vector!");
11310 assert((!VTList.VTs[0].isVector() ||
11311 VTList.VTs[0].getVectorElementCount() ==
11312 Ops[1].getValueType().getVectorElementCount()) &&
11313 "Vector element count mismatch!");
11314 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11315 "Invalid fpext node, dst <= src!");
11316 break;
11318 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11319 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11320 "STRICT_FP_ROUND result type should be vector iff the operand "
11321 "type is vector!");
11322 assert((!VTList.VTs[0].isVector() ||
11323 VTList.VTs[0].getVectorElementCount() ==
11324 Ops[1].getValueType().getVectorElementCount()) &&
11325 "Vector element count mismatch!");
11326 assert(VTList.VTs[0].isFloatingPoint() &&
11327 Ops[1].getValueType().isFloatingPoint() &&
11328 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11329 Ops[2].getOpcode() == ISD::TargetConstant &&
11330 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11331 "Invalid STRICT_FP_ROUND!");
11332 break;
11333 }
11334
11335 // Memoize the node unless it returns a glue result.
11336 SDNode *N;
11337 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11339 AddNodeIDNode(ID, Opcode, VTList, Ops);
11340 void *IP = nullptr;
11341 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11342 E->intersectFlagsWith(Flags);
11343 return SDValue(E, 0);
11344 }
11345
11346 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11347 createOperands(N, Ops);
11348 CSEMap.InsertNode(N, IP);
11349 } else {
11350 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11351 createOperands(N, Ops);
11352 }
11353
11354 N->setFlags(Flags);
11355 InsertNode(N);
11356 SDValue V(N, 0);
11357 NewSDValueDbgMsg(V, "Creating new node: ", this);
11358 return V;
11359}
11360
11361SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11362 SDVTList VTList) {
11363 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11364}
11365
11366SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11367 SDValue N1) {
11368 SDValue Ops[] = { N1 };
11369 return getNode(Opcode, DL, VTList, Ops);
11370}
11371
11372SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11373 SDValue N1, SDValue N2) {
11374 SDValue Ops[] = { N1, N2 };
11375 return getNode(Opcode, DL, VTList, Ops);
11376}
11377
11378SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11379 SDValue N1, SDValue N2, SDValue N3) {
11380 SDValue Ops[] = { N1, N2, N3 };
11381 return getNode(Opcode, DL, VTList, Ops);
11382}
11383
11384SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11385 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11386 SDValue Ops[] = { N1, N2, N3, N4 };
11387 return getNode(Opcode, DL, VTList, Ops);
11388}
11389
11390SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11391 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11392 SDValue N5) {
11393 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11394 return getNode(Opcode, DL, VTList, Ops);
11395}
11396
11398 if (!VT.isExtended())
11399 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11400
11401 return makeVTList(&(*EVTs.insert(VT).first), 1);
11402}
11403
11406 ID.AddInteger(2U);
11407 ID.AddInteger(VT1.getRawBits());
11408 ID.AddInteger(VT2.getRawBits());
11409
11410 void *IP = nullptr;
11411 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11412 if (!Result) {
11413 EVT *Array = Allocator.Allocate<EVT>(2);
11414 Array[0] = VT1;
11415 Array[1] = VT2;
11416 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11417 VTListMap.InsertNode(Result, IP);
11418 }
11419 return Result->getSDVTList();
11420}
11421
11424 ID.AddInteger(3U);
11425 ID.AddInteger(VT1.getRawBits());
11426 ID.AddInteger(VT2.getRawBits());
11427 ID.AddInteger(VT3.getRawBits());
11428
11429 void *IP = nullptr;
11430 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11431 if (!Result) {
11432 EVT *Array = Allocator.Allocate<EVT>(3);
11433 Array[0] = VT1;
11434 Array[1] = VT2;
11435 Array[2] = VT3;
11436 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11437 VTListMap.InsertNode(Result, IP);
11438 }
11439 return Result->getSDVTList();
11440}
11441
11444 ID.AddInteger(4U);
11445 ID.AddInteger(VT1.getRawBits());
11446 ID.AddInteger(VT2.getRawBits());
11447 ID.AddInteger(VT3.getRawBits());
11448 ID.AddInteger(VT4.getRawBits());
11449
11450 void *IP = nullptr;
11451 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11452 if (!Result) {
11453 EVT *Array = Allocator.Allocate<EVT>(4);
11454 Array[0] = VT1;
11455 Array[1] = VT2;
11456 Array[2] = VT3;
11457 Array[3] = VT4;
11458 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11459 VTListMap.InsertNode(Result, IP);
11460 }
11461 return Result->getSDVTList();
11462}
11463
11465 unsigned NumVTs = VTs.size();
11467 ID.AddInteger(NumVTs);
11468 for (unsigned index = 0; index < NumVTs; index++) {
11469 ID.AddInteger(VTs[index].getRawBits());
11470 }
11471
11472 void *IP = nullptr;
11473 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11474 if (!Result) {
11475 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11476 llvm::copy(VTs, Array);
11477 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11478 VTListMap.InsertNode(Result, IP);
11479 }
11480 return Result->getSDVTList();
11481}
11482
11483
11484/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11485/// specified operands. If the resultant node already exists in the DAG,
11486/// this does not modify the specified node, instead it returns the node that
11487/// already exists. If the resultant node does not exist in the DAG, the
11488/// input node is returned. As a degenerate case, if you specify the same
11489/// input operands as the node already has, the input node is returned.
11491 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11492
11493 // Check to see if there is no change.
11494 if (Op == N->getOperand(0)) return N;
11495
11496 // See if the modified node already exists.
11497 void *InsertPos = nullptr;
11498 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11499 return Existing;
11500
11501 // Nope it doesn't. Remove the node from its current place in the maps.
11502 if (InsertPos)
11503 if (!RemoveNodeFromCSEMaps(N))
11504 InsertPos = nullptr;
11505
11506 // Now we update the operands.
11507 N->OperandList[0].set(Op);
11508
11510 // If this gets put into a CSE map, add it.
11511 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11512 return N;
11513}
11514
11516 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11517
11518 // Check to see if there is no change.
11519 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11520 return N; // No operands changed, just return the input node.
11521
11522 // See if the modified node already exists.
11523 void *InsertPos = nullptr;
11524 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11525 return Existing;
11526
11527 // Nope it doesn't. Remove the node from its current place in the maps.
11528 if (InsertPos)
11529 if (!RemoveNodeFromCSEMaps(N))
11530 InsertPos = nullptr;
11531
11532 // Now we update the operands.
11533 if (N->OperandList[0] != Op1)
11534 N->OperandList[0].set(Op1);
11535 if (N->OperandList[1] != Op2)
11536 N->OperandList[1].set(Op2);
11537
11539 // If this gets put into a CSE map, add it.
11540 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11541 return N;
11542}
11543
11546 SDValue Ops[] = { Op1, Op2, Op3 };
11547 return UpdateNodeOperands(N, Ops);
11548}
11549
11552 SDValue Op3, SDValue Op4) {
11553 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11554 return UpdateNodeOperands(N, Ops);
11555}
11556
11559 SDValue Op3, SDValue Op4, SDValue Op5) {
11560 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11561 return UpdateNodeOperands(N, Ops);
11562}
11563
11566 unsigned NumOps = Ops.size();
11567 assert(N->getNumOperands() == NumOps &&
11568 "Update with wrong number of operands");
11569
11570 // If no operands changed just return the input node.
11571 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11572 return N;
11573
11574 // See if the modified node already exists.
11575 void *InsertPos = nullptr;
11576 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11577 return Existing;
11578
11579 // Nope it doesn't. Remove the node from its current place in the maps.
11580 if (InsertPos)
11581 if (!RemoveNodeFromCSEMaps(N))
11582 InsertPos = nullptr;
11583
11584 // Now we update the operands.
11585 for (unsigned i = 0; i != NumOps; ++i)
11586 if (N->OperandList[i] != Ops[i])
11587 N->OperandList[i].set(Ops[i]);
11588
11590 // If this gets put into a CSE map, add it.
11591 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11592 return N;
11593}
11594
11595/// DropOperands - Release the operands and set this node to have
11596/// zero operands.
11598 // Unlike the code in MorphNodeTo that does this, we don't need to
11599 // watch for dead nodes here.
11600 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11601 SDUse &Use = *I++;
11602 Use.set(SDValue());
11603 }
11604}
11605
11607 ArrayRef<MachineMemOperand *> NewMemRefs) {
11608 if (NewMemRefs.empty()) {
11609 N->clearMemRefs();
11610 return;
11611 }
11612
11613 // Check if we can avoid allocating by storing a single reference directly.
11614 if (NewMemRefs.size() == 1) {
11615 N->MemRefs = NewMemRefs[0];
11616 N->NumMemRefs = 1;
11617 return;
11618 }
11619
11620 MachineMemOperand **MemRefsBuffer =
11621 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11622 llvm::copy(NewMemRefs, MemRefsBuffer);
11623 N->MemRefs = MemRefsBuffer;
11624 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11625}
11626
11627/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11628/// machine opcode.
11629///
11631 EVT VT) {
11632 SDVTList VTs = getVTList(VT);
11633 return SelectNodeTo(N, MachineOpc, VTs, {});
11634}
11635
11637 EVT VT, SDValue Op1) {
11638 SDVTList VTs = getVTList(VT);
11639 SDValue Ops[] = { Op1 };
11640 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11641}
11642
11644 EVT VT, SDValue Op1,
11645 SDValue Op2) {
11646 SDVTList VTs = getVTList(VT);
11647 SDValue Ops[] = { Op1, Op2 };
11648 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11649}
11650
11652 EVT VT, SDValue Op1,
11653 SDValue Op2, SDValue Op3) {
11654 SDVTList VTs = getVTList(VT);
11655 SDValue Ops[] = { Op1, Op2, Op3 };
11656 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11657}
11658
11661 SDVTList VTs = getVTList(VT);
11662 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11663}
11664
11666 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11667 SDVTList VTs = getVTList(VT1, VT2);
11668 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11669}
11670
11672 EVT VT1, EVT VT2) {
11673 SDVTList VTs = getVTList(VT1, VT2);
11674 return SelectNodeTo(N, MachineOpc, VTs, {});
11675}
11676
11678 EVT VT1, EVT VT2, EVT VT3,
11680 SDVTList VTs = getVTList(VT1, VT2, VT3);
11681 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11682}
11683
11685 EVT VT1, EVT VT2,
11686 SDValue Op1, SDValue Op2) {
11687 SDVTList VTs = getVTList(VT1, VT2);
11688 SDValue Ops[] = { Op1, Op2 };
11689 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11690}
11691
11694 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11695 // Reset the NodeID to -1.
11696 New->setNodeId(-1);
11697 if (New != N) {
11698 ReplaceAllUsesWith(N, New);
11700 }
11701 return New;
11702}
11703
11704/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11705/// the line number information on the merged node since it is not possible to
11706/// preserve the information that operation is associated with multiple lines.
11707/// This will make the debugger working better at -O0, were there is a higher
11708/// probability having other instructions associated with that line.
11709///
11710/// For IROrder, we keep the smaller of the two
11711SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11712 DebugLoc NLoc = N->getDebugLoc();
11713 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11714 N->setDebugLoc(DebugLoc());
11715 }
11716 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11717 N->setIROrder(Order);
11718 return N;
11719}
11720
11721/// MorphNodeTo - This *mutates* the specified node to have the specified
11722/// return type, opcode, and operands.
11723///
11724/// Note that MorphNodeTo returns the resultant node. If there is already a
11725/// node of the specified opcode and operands, it returns that node instead of
11726/// the current one. Note that the SDLoc need not be the same.
11727///
11728/// Using MorphNodeTo is faster than creating a new node and swapping it in
11729/// with ReplaceAllUsesWith both because it often avoids allocating a new
11730/// node, and because it doesn't require CSE recalculation for any of
11731/// the node's users.
11732///
11733/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11734/// As a consequence it isn't appropriate to use from within the DAG combiner or
11735/// the legalizer which maintain worklists that would need to be updated when
11736/// deleting things.
11739 // If an identical node already exists, use it.
11740 void *IP = nullptr;
11741 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11743 AddNodeIDNode(ID, Opc, VTs, Ops);
11744 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11745 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11746 }
11747
11748 if (!RemoveNodeFromCSEMaps(N))
11749 IP = nullptr;
11750
11751 // Start the morphing.
11752 N->NodeType = Opc;
11753 N->ValueList = VTs.VTs;
11754 N->NumValues = VTs.NumVTs;
11755
11756 // Clear the operands list, updating used nodes to remove this from their
11757 // use list. Keep track of any operands that become dead as a result.
11758 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11759 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11760 SDUse &Use = *I++;
11761 SDNode *Used = Use.getNode();
11762 Use.set(SDValue());
11763 if (Used->use_empty())
11764 DeadNodeSet.insert(Used);
11765 }
11766
11767 // For MachineNode, initialize the memory references information.
11769 MN->clearMemRefs();
11770
11771 // Swap for an appropriately sized array from the recycler.
11772 removeOperands(N);
11773 createOperands(N, Ops);
11774
11775 // Delete any nodes that are still dead after adding the uses for the
11776 // new operands.
11777 if (!DeadNodeSet.empty()) {
11778 SmallVector<SDNode *, 16> DeadNodes;
11779 for (SDNode *N : DeadNodeSet)
11780 if (N->use_empty())
11781 DeadNodes.push_back(N);
11782 RemoveDeadNodes(DeadNodes);
11783 }
11784
11785 if (IP)
11786 CSEMap.InsertNode(N, IP); // Memoize the new node.
11787 return N;
11788}
11789
11791 unsigned OrigOpc = Node->getOpcode();
11792 unsigned NewOpc;
11793 switch (OrigOpc) {
11794 default:
11795 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11796#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11797 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11798#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11799 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11800#include "llvm/IR/ConstrainedOps.def"
11801 }
11802
11803 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11804
11805 // We're taking this node out of the chain, so we need to re-link things.
11806 SDValue InputChain = Node->getOperand(0);
11807 SDValue OutputChain = SDValue(Node, 1);
11808 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11809
11811 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11812 Ops.push_back(Node->getOperand(i));
11813
11814 SDVTList VTs = getVTList(Node->getValueType(0));
11815 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11816
11817 // MorphNodeTo can operate in two ways: if an existing node with the
11818 // specified operands exists, it can just return it. Otherwise, it
11819 // updates the node in place to have the requested operands.
11820 if (Res == Node) {
11821 // If we updated the node in place, reset the node ID. To the isel,
11822 // this should be just like a newly allocated machine node.
11823 Res->setNodeId(-1);
11824 } else {
11827 }
11828
11829 return Res;
11830}
11831
11832/// getMachineNode - These are used for target selectors to create a new node
11833/// with specified return type(s), MachineInstr opcode, and operands.
11834///
11835/// Note that getMachineNode returns the resultant node. If there is already a
11836/// node of the specified opcode and operands, it returns that node instead of
11837/// the current one.
11839 EVT VT) {
11840 SDVTList VTs = getVTList(VT);
11841 return getMachineNode(Opcode, dl, VTs, {});
11842}
11843
11845 EVT VT, SDValue Op1) {
11846 SDVTList VTs = getVTList(VT);
11847 SDValue Ops[] = { Op1 };
11848 return getMachineNode(Opcode, dl, VTs, Ops);
11849}
11850
11852 EVT VT, SDValue Op1, SDValue Op2) {
11853 SDVTList VTs = getVTList(VT);
11854 SDValue Ops[] = { Op1, Op2 };
11855 return getMachineNode(Opcode, dl, VTs, Ops);
11856}
11857
11859 EVT VT, SDValue Op1, SDValue Op2,
11860 SDValue Op3) {
11861 SDVTList VTs = getVTList(VT);
11862 SDValue Ops[] = { Op1, Op2, Op3 };
11863 return getMachineNode(Opcode, dl, VTs, Ops);
11864}
11865
11868 SDVTList VTs = getVTList(VT);
11869 return getMachineNode(Opcode, dl, VTs, Ops);
11870}
11871
11873 EVT VT1, EVT VT2, SDValue Op1,
11874 SDValue Op2) {
11875 SDVTList VTs = getVTList(VT1, VT2);
11876 SDValue Ops[] = { Op1, Op2 };
11877 return getMachineNode(Opcode, dl, VTs, Ops);
11878}
11879
11881 EVT VT1, EVT VT2, SDValue Op1,
11882 SDValue Op2, SDValue Op3) {
11883 SDVTList VTs = getVTList(VT1, VT2);
11884 SDValue Ops[] = { Op1, Op2, Op3 };
11885 return getMachineNode(Opcode, dl, VTs, Ops);
11886}
11887
11889 EVT VT1, EVT VT2,
11891 SDVTList VTs = getVTList(VT1, VT2);
11892 return getMachineNode(Opcode, dl, VTs, Ops);
11893}
11894
11896 EVT VT1, EVT VT2, EVT VT3,
11897 SDValue Op1, SDValue Op2) {
11898 SDVTList VTs = getVTList(VT1, VT2, VT3);
11899 SDValue Ops[] = { Op1, Op2 };
11900 return getMachineNode(Opcode, dl, VTs, Ops);
11901}
11902
11904 EVT VT1, EVT VT2, EVT VT3,
11905 SDValue Op1, SDValue Op2,
11906 SDValue Op3) {
11907 SDVTList VTs = getVTList(VT1, VT2, VT3);
11908 SDValue Ops[] = { Op1, Op2, Op3 };
11909 return getMachineNode(Opcode, dl, VTs, Ops);
11910}
11911
11913 EVT VT1, EVT VT2, EVT VT3,
11915 SDVTList VTs = getVTList(VT1, VT2, VT3);
11916 return getMachineNode(Opcode, dl, VTs, Ops);
11917}
11918
11920 ArrayRef<EVT> ResultTys,
11922 SDVTList VTs = getVTList(ResultTys);
11923 return getMachineNode(Opcode, dl, VTs, Ops);
11924}
11925
11927 SDVTList VTs,
11929 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11931 void *IP = nullptr;
11932
11933 if (DoCSE) {
11935 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11936 IP = nullptr;
11937 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11938 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11939 }
11940 }
11941
11942 // Allocate a new MachineSDNode.
11943 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11944 createOperands(N, Ops);
11945
11946 if (DoCSE)
11947 CSEMap.InsertNode(N, IP);
11948
11949 InsertNode(N);
11950 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
11951 return N;
11952}
11953
11954/// getTargetExtractSubreg - A convenience function for creating
11955/// TargetOpcode::EXTRACT_SUBREG nodes.
11957 SDValue Operand) {
11958 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11959 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
11960 VT, Operand, SRIdxVal);
11961 return SDValue(Subreg, 0);
11962}
11963
11964/// getTargetInsertSubreg - A convenience function for creating
11965/// TargetOpcode::INSERT_SUBREG nodes.
11967 SDValue Operand, SDValue Subreg) {
11968 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
11969 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
11970 VT, Operand, Subreg, SRIdxVal);
11971 return SDValue(Result, 0);
11972}
11973
11974/// getNodeIfExists - Get the specified node if it's already available, or
11975/// else return NULL.
11978 bool AllowCommute) {
11979 SDNodeFlags Flags;
11980 if (Inserter)
11981 Flags = Inserter->getFlags();
11982 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
11983}
11984
11987 const SDNodeFlags Flags,
11988 bool AllowCommute) {
11989 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
11990 return nullptr;
11991
11992 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
11994 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
11995 void *IP = nullptr;
11996 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
11997 E->intersectFlagsWith(Flags);
11998 return E;
11999 }
12000 return nullptr;
12001 };
12002
12003 if (SDNode *Existing = Lookup(Ops))
12004 return Existing;
12005
12006 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12007 return Lookup({Ops[1], Ops[0]});
12008
12009 return nullptr;
12010}
12011
12012/// doesNodeExist - Check if a node exists without modifying its flags.
12013bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12015 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12017 AddNodeIDNode(ID, Opcode, VTList, Ops);
12018 void *IP = nullptr;
12019 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12020 return true;
12021 }
12022 return false;
12023}
12024
12025/// getDbgValue - Creates a SDDbgValue node.
12026///
12027/// SDNode
12029 SDNode *N, unsigned R, bool IsIndirect,
12030 const DebugLoc &DL, unsigned O) {
12031 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12032 "Expected inlined-at fields to agree");
12033 return new (DbgInfo->getAlloc())
12034 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromNode(N, R),
12035 {}, IsIndirect, DL, O,
12036 /*IsVariadic=*/false);
12037}
12038
12039/// Constant
12041 DIExpression *Expr,
12042 const Value *C,
12043 const DebugLoc &DL, unsigned O) {
12044 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12045 "Expected inlined-at fields to agree");
12046 return new (DbgInfo->getAlloc())
12047 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromConst(C), {},
12048 /*IsIndirect=*/false, DL, O,
12049 /*IsVariadic=*/false);
12050}
12051
12052/// FrameIndex
12054 DIExpression *Expr, unsigned FI,
12055 bool IsIndirect,
12056 const DebugLoc &DL,
12057 unsigned O) {
12058 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12059 "Expected inlined-at fields to agree");
12060 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12061}
12062
12063/// FrameIndex with dependencies
12065 DIExpression *Expr, unsigned FI,
12066 ArrayRef<SDNode *> Dependencies,
12067 bool IsIndirect,
12068 const DebugLoc &DL,
12069 unsigned O) {
12070 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12071 "Expected inlined-at fields to agree");
12072 return new (DbgInfo->getAlloc())
12073 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12074 Dependencies, IsIndirect, DL, O,
12075 /*IsVariadic=*/false);
12076}
12077
12078/// VReg
12080 Register VReg, bool IsIndirect,
12081 const DebugLoc &DL, unsigned O) {
12082 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12083 "Expected inlined-at fields to agree");
12084 return new (DbgInfo->getAlloc())
12085 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12086 {}, IsIndirect, DL, O,
12087 /*IsVariadic=*/false);
12088}
12089
12092 ArrayRef<SDNode *> Dependencies,
12093 bool IsIndirect, const DebugLoc &DL,
12094 unsigned O, bool IsVariadic) {
12095 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12096 "Expected inlined-at fields to agree");
12097 return new (DbgInfo->getAlloc())
12098 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12099 DL, O, IsVariadic);
12100}
12101
12103 unsigned OffsetInBits, unsigned SizeInBits,
12104 bool InvalidateDbg) {
12105 SDNode *FromNode = From.getNode();
12106 SDNode *ToNode = To.getNode();
12107 assert(FromNode && ToNode && "Can't modify dbg values");
12108
12109 // PR35338
12110 // TODO: assert(From != To && "Redundant dbg value transfer");
12111 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12112 if (From == To || FromNode == ToNode)
12113 return;
12114
12115 if (!FromNode->getHasDebugValue())
12116 return;
12117
12118 SDDbgOperand FromLocOp =
12119 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12121
12123 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12124 if (Dbg->isInvalidated())
12125 continue;
12126
12127 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12128
12129 // Create a new location ops vector that is equal to the old vector, but
12130 // with each instance of FromLocOp replaced with ToLocOp.
12131 bool Changed = false;
12132 auto NewLocOps = Dbg->copyLocationOps();
12133 std::replace_if(
12134 NewLocOps.begin(), NewLocOps.end(),
12135 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12136 bool Match = Op == FromLocOp;
12137 Changed |= Match;
12138 return Match;
12139 },
12140 ToLocOp);
12141 // Ignore this SDDbgValue if we didn't find a matching location.
12142 if (!Changed)
12143 continue;
12144
12145 DIVariable *Var = Dbg->getVariable();
12146 auto *Expr = Dbg->getExpression();
12147 // If a fragment is requested, update the expression.
12148 if (SizeInBits) {
12149 // When splitting a larger (e.g., sign-extended) value whose
12150 // lower bits are described with an SDDbgValue, do not attempt
12151 // to transfer the SDDbgValue to the upper bits.
12152 if (auto FI = Expr->getFragmentInfo())
12153 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12154 continue;
12155 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12156 SizeInBits);
12157 if (!Fragment)
12158 continue;
12159 Expr = *Fragment;
12160 }
12161
12162 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12163 // Clone the SDDbgValue and move it to To.
12164 SDDbgValue *Clone = getDbgValueList(
12165 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12166 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12167 Dbg->isVariadic());
12168 ClonedDVs.push_back(Clone);
12169
12170 if (InvalidateDbg) {
12171 // Invalidate value and indicate the SDDbgValue should not be emitted.
12172 Dbg->setIsInvalidated();
12173 Dbg->setIsEmitted();
12174 }
12175 }
12176
12177 for (SDDbgValue *Dbg : ClonedDVs) {
12178 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12179 "Transferred DbgValues should depend on the new SDNode");
12180 AddDbgValue(Dbg, false);
12181 }
12182}
12183
12185 if (!N.getHasDebugValue())
12186 return;
12187
12188 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12189 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12190 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12191 return SDDbgOperand::fromNode(Node, ResNo);
12192 };
12193
12195 for (auto *DV : GetDbgValues(&N)) {
12196 if (DV->isInvalidated())
12197 continue;
12198 switch (N.getOpcode()) {
12199 default:
12200 break;
12201 case ISD::ADD: {
12202 SDValue N0 = N.getOperand(0);
12203 SDValue N1 = N.getOperand(1);
12204 if (!isa<ConstantSDNode>(N0)) {
12205 bool RHSConstant = isa<ConstantSDNode>(N1);
12207 if (RHSConstant)
12208 Offset = N.getConstantOperandVal(1);
12209 // We are not allowed to turn indirect debug values variadic, so
12210 // don't salvage those.
12211 if (!RHSConstant && DV->isIndirect())
12212 continue;
12213
12214 // Rewrite an ADD constant node into a DIExpression. Since we are
12215 // performing arithmetic to compute the variable's *value* in the
12216 // DIExpression, we need to mark the expression with a
12217 // DW_OP_stack_value.
12218 auto *DIExpr = DV->getExpression();
12219 auto NewLocOps = DV->copyLocationOps();
12220 bool Changed = false;
12221 size_t OrigLocOpsSize = NewLocOps.size();
12222 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12223 // We're not given a ResNo to compare against because the whole
12224 // node is going away. We know that any ISD::ADD only has one
12225 // result, so we can assume any node match is using the result.
12226 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12227 NewLocOps[i].getSDNode() != &N)
12228 continue;
12229 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12230 if (RHSConstant) {
12233 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12234 } else {
12235 // Convert to a variadic expression (if not already).
12236 // convertToVariadicExpression() returns a const pointer, so we use
12237 // a temporary const variable here.
12238 const auto *TmpDIExpr =
12242 ExprOps.push_back(NewLocOps.size());
12243 ExprOps.push_back(dwarf::DW_OP_plus);
12244 SDDbgOperand RHS =
12246 NewLocOps.push_back(RHS);
12247 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12248 }
12249 Changed = true;
12250 }
12251 (void)Changed;
12252 assert(Changed && "Salvage target doesn't use N");
12253
12254 bool IsVariadic =
12255 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12256
12257 auto AdditionalDependencies = DV->getAdditionalDependencies();
12258 SDDbgValue *Clone = getDbgValueList(
12259 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12260 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12261 ClonedDVs.push_back(Clone);
12262 DV->setIsInvalidated();
12263 DV->setIsEmitted();
12264 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12265 N0.getNode()->dumprFull(this);
12266 dbgs() << " into " << *DIExpr << '\n');
12267 }
12268 break;
12269 }
12270 case ISD::TRUNCATE: {
12271 SDValue N0 = N.getOperand(0);
12272 TypeSize FromSize = N0.getValueSizeInBits();
12273 TypeSize ToSize = N.getValueSizeInBits(0);
12274
12275 DIExpression *DbgExpression = DV->getExpression();
12276 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12277 auto NewLocOps = DV->copyLocationOps();
12278 bool Changed = false;
12279 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12280 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12281 NewLocOps[i].getSDNode() != &N)
12282 continue;
12283
12284 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12285 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12286 Changed = true;
12287 }
12288 assert(Changed && "Salvage target doesn't use N");
12289 (void)Changed;
12290
12291 SDDbgValue *Clone =
12292 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12293 DV->getAdditionalDependencies(), DV->isIndirect(),
12294 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12295
12296 ClonedDVs.push_back(Clone);
12297 DV->setIsInvalidated();
12298 DV->setIsEmitted();
12299 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12300 dbgs() << " into " << *DbgExpression << '\n');
12301 break;
12302 }
12303 }
12304 }
12305
12306 for (SDDbgValue *Dbg : ClonedDVs) {
12307 assert((!Dbg->getSDNodes().empty() ||
12308 llvm::any_of(Dbg->getLocationOps(),
12309 [&](const SDDbgOperand &Op) {
12310 return Op.getKind() == SDDbgOperand::FRAMEIX;
12311 })) &&
12312 "Salvaged DbgValue should depend on a new SDNode");
12313 AddDbgValue(Dbg, false);
12314 }
12315}
12316
12317/// Creates a SDDbgLabel node.
12319 const DebugLoc &DL, unsigned O) {
12320 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12321 "Expected inlined-at fields to agree");
12322 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12323}
12324
12325namespace {
12326
12327/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12328/// pointed to by a use iterator is deleted, increment the use iterator
12329/// so that it doesn't dangle.
12330///
12331class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12334
12335 void NodeDeleted(SDNode *N, SDNode *E) override {
12336 // Increment the iterator as needed.
12337 while (UI != UE && N == UI->getUser())
12338 ++UI;
12339 }
12340
12341public:
12342 RAUWUpdateListener(SelectionDAG &d,
12345 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12346};
12347
12348} // end anonymous namespace
12349
12350/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12351/// This can cause recursive merging of nodes in the DAG.
12352///
12353/// This version assumes From has a single result value.
12354///
12356 SDNode *From = FromN.getNode();
12357 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12358 "Cannot replace with this method!");
12359 assert(From != To.getNode() && "Cannot replace uses of with self");
12360
12361 // Preserve Debug Values
12362 transferDbgValues(FromN, To);
12363 // Preserve extra info.
12364 copyExtraInfo(From, To.getNode());
12365
12366 // Iterate over all the existing uses of From. New uses will be added
12367 // to the beginning of the use list, which we avoid visiting.
12368 // This specifically avoids visiting uses of From that arise while the
12369 // replacement is happening, because any such uses would be the result
12370 // of CSE: If an existing node looks like From after one of its operands
12371 // is replaced by To, we don't want to replace of all its users with To
12372 // too. See PR3018 for more info.
12373 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12374 RAUWUpdateListener Listener(*this, UI, UE);
12375 while (UI != UE) {
12376 SDNode *User = UI->getUser();
12377
12378 // This node is about to morph, remove its old self from the CSE maps.
12379 RemoveNodeFromCSEMaps(User);
12380
12381 // A user can appear in a use list multiple times, and when this
12382 // happens the uses are usually next to each other in the list.
12383 // To help reduce the number of CSE recomputations, process all
12384 // the uses of this user that we can find this way.
12385 do {
12386 SDUse &Use = *UI;
12387 ++UI;
12388 Use.set(To);
12389 if (To->isDivergent() != From->isDivergent())
12391 } while (UI != UE && UI->getUser() == User);
12392 // Now that we have modified User, add it back to the CSE maps. If it
12393 // already exists there, recursively merge the results together.
12394 AddModifiedNodeToCSEMaps(User);
12395 }
12396
12397 // If we just RAUW'd the root, take note.
12398 if (FromN == getRoot())
12399 setRoot(To);
12400}
12401
12402/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12403/// This can cause recursive merging of nodes in the DAG.
12404///
12405/// This version assumes that for each value of From, there is a
12406/// corresponding value in To in the same position with the same type.
12407///
12409#ifndef NDEBUG
12410 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12411 assert((!From->hasAnyUseOfValue(i) ||
12412 From->getValueType(i) == To->getValueType(i)) &&
12413 "Cannot use this version of ReplaceAllUsesWith!");
12414#endif
12415
12416 // Handle the trivial case.
12417 if (From == To)
12418 return;
12419
12420 // Preserve Debug Info. Only do this if there's a use.
12421 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12422 if (From->hasAnyUseOfValue(i)) {
12423 assert((i < To->getNumValues()) && "Invalid To location");
12424 transferDbgValues(SDValue(From, i), SDValue(To, i));
12425 }
12426 // Preserve extra info.
12427 copyExtraInfo(From, To);
12428
12429 // Iterate over just the existing users of From. See the comments in
12430 // the ReplaceAllUsesWith above.
12431 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12432 RAUWUpdateListener Listener(*this, UI, UE);
12433 while (UI != UE) {
12434 SDNode *User = UI->getUser();
12435
12436 // This node is about to morph, remove its old self from the CSE maps.
12437 RemoveNodeFromCSEMaps(User);
12438
12439 // A user can appear in a use list multiple times, and when this
12440 // happens the uses are usually next to each other in the list.
12441 // To help reduce the number of CSE recomputations, process all
12442 // the uses of this user that we can find this way.
12443 do {
12444 SDUse &Use = *UI;
12445 ++UI;
12446 Use.setNode(To);
12447 if (To->isDivergent() != From->isDivergent())
12449 } while (UI != UE && UI->getUser() == User);
12450
12451 // Now that we have modified User, add it back to the CSE maps. If it
12452 // already exists there, recursively merge the results together.
12453 AddModifiedNodeToCSEMaps(User);
12454 }
12455
12456 // If we just RAUW'd the root, take note.
12457 if (From == getRoot().getNode())
12458 setRoot(SDValue(To, getRoot().getResNo()));
12459}
12460
12461/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12462/// This can cause recursive merging of nodes in the DAG.
12463///
12464/// This version can replace From with any result values. To must match the
12465/// number and types of values returned by From.
12467 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12468 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12469
12470 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12471 // Preserve Debug Info.
12472 transferDbgValues(SDValue(From, i), To[i]);
12473 // Preserve extra info.
12474 copyExtraInfo(From, To[i].getNode());
12475 }
12476
12477 // Iterate over just the existing users of From. See the comments in
12478 // the ReplaceAllUsesWith above.
12479 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12480 RAUWUpdateListener Listener(*this, UI, UE);
12481 while (UI != UE) {
12482 SDNode *User = UI->getUser();
12483
12484 // This node is about to morph, remove its old self from the CSE maps.
12485 RemoveNodeFromCSEMaps(User);
12486
12487 // A user can appear in a use list multiple times, and when this happens the
12488 // uses are usually next to each other in the list. To help reduce the
12489 // number of CSE and divergence recomputations, process all the uses of this
12490 // user that we can find this way.
12491 bool To_IsDivergent = false;
12492 do {
12493 SDUse &Use = *UI;
12494 const SDValue &ToOp = To[Use.getResNo()];
12495 ++UI;
12496 Use.set(ToOp);
12497 if (ToOp.getValueType() != MVT::Other)
12498 To_IsDivergent |= ToOp->isDivergent();
12499 } while (UI != UE && UI->getUser() == User);
12500
12501 if (To_IsDivergent != From->isDivergent())
12503
12504 // Now that we have modified User, add it back to the CSE maps. If it
12505 // already exists there, recursively merge the results together.
12506 AddModifiedNodeToCSEMaps(User);
12507 }
12508
12509 // If we just RAUW'd the root, take note.
12510 if (From == getRoot().getNode())
12511 setRoot(SDValue(To[getRoot().getResNo()]));
12512}
12513
12514/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12515/// uses of other values produced by From.getNode() alone. The Deleted
12516/// vector is handled the same way as for ReplaceAllUsesWith.
12518 // Handle the really simple, really trivial case efficiently.
12519 if (From == To) return;
12520
12521 // Handle the simple, trivial, case efficiently.
12522 if (From.getNode()->getNumValues() == 1) {
12523 ReplaceAllUsesWith(From, To);
12524 return;
12525 }
12526
12527 // Preserve Debug Info.
12528 transferDbgValues(From, To);
12529 copyExtraInfo(From.getNode(), To.getNode());
12530
12531 // Iterate over just the existing users of From. See the comments in
12532 // the ReplaceAllUsesWith above.
12533 SDNode::use_iterator UI = From.getNode()->use_begin(),
12534 UE = From.getNode()->use_end();
12535 RAUWUpdateListener Listener(*this, UI, UE);
12536 while (UI != UE) {
12537 SDNode *User = UI->getUser();
12538 bool UserRemovedFromCSEMaps = false;
12539
12540 // A user can appear in a use list multiple times, and when this
12541 // happens the uses are usually next to each other in the list.
12542 // To help reduce the number of CSE recomputations, process all
12543 // the uses of this user that we can find this way.
12544 do {
12545 SDUse &Use = *UI;
12546
12547 // Skip uses of different values from the same node.
12548 if (Use.getResNo() != From.getResNo()) {
12549 ++UI;
12550 continue;
12551 }
12552
12553 // If this node hasn't been modified yet, it's still in the CSE maps,
12554 // so remove its old self from the CSE maps.
12555 if (!UserRemovedFromCSEMaps) {
12556 RemoveNodeFromCSEMaps(User);
12557 UserRemovedFromCSEMaps = true;
12558 }
12559
12560 ++UI;
12561 Use.set(To);
12562 if (To->isDivergent() != From->isDivergent())
12564 } while (UI != UE && UI->getUser() == User);
12565 // We are iterating over all uses of the From node, so if a use
12566 // doesn't use the specific value, no changes are made.
12567 if (!UserRemovedFromCSEMaps)
12568 continue;
12569
12570 // Now that we have modified User, add it back to the CSE maps. If it
12571 // already exists there, recursively merge the results together.
12572 AddModifiedNodeToCSEMaps(User);
12573 }
12574
12575 // If we just RAUW'd the root, take note.
12576 if (From == getRoot())
12577 setRoot(To);
12578}
12579
12580namespace {
12581
12582/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12583/// to record information about a use.
12584struct UseMemo {
12585 SDNode *User;
12586 unsigned Index;
12587 SDUse *Use;
12588};
12589
12590/// operator< - Sort Memos by User.
12591bool operator<(const UseMemo &L, const UseMemo &R) {
12592 return (intptr_t)L.User < (intptr_t)R.User;
12593}
12594
12595/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12596/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12597/// the node already has been taken care of recursively.
12598class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12599 SmallVectorImpl<UseMemo> &Uses;
12600
12601 void NodeDeleted(SDNode *N, SDNode *E) override {
12602 for (UseMemo &Memo : Uses)
12603 if (Memo.User == N)
12604 Memo.User = nullptr;
12605 }
12606
12607public:
12608 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12609 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12610};
12611
12612} // end anonymous namespace
12613
12614/// Return true if a glue output should propagate divergence information.
12616 switch (Node->getOpcode()) {
12617 case ISD::CopyFromReg:
12618 case ISD::CopyToReg:
12619 return false;
12620 default:
12621 return true;
12622 }
12623
12624 llvm_unreachable("covered opcode switch");
12625}
12626
12628 if (TLI->isSDNodeAlwaysUniform(N)) {
12629 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12630 "Conflicting divergence information!");
12631 return false;
12632 }
12633 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12634 return true;
12635 for (const auto &Op : N->ops()) {
12636 EVT VT = Op.getValueType();
12637
12638 // Skip Chain. It does not carry divergence.
12639 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12640 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12641 return true;
12642 }
12643 return false;
12644}
12645
12647 SmallVector<SDNode *, 16> Worklist(1, N);
12648 do {
12649 N = Worklist.pop_back_val();
12650 bool IsDivergent = calculateDivergence(N);
12651 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12652 N->SDNodeBits.IsDivergent = IsDivergent;
12653 llvm::append_range(Worklist, N->users());
12654 }
12655 } while (!Worklist.empty());
12656}
12657
12658void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12660 Order.reserve(AllNodes.size());
12661 for (auto &N : allnodes()) {
12662 unsigned NOps = N.getNumOperands();
12663 Degree[&N] = NOps;
12664 if (0 == NOps)
12665 Order.push_back(&N);
12666 }
12667 for (size_t I = 0; I != Order.size(); ++I) {
12668 SDNode *N = Order[I];
12669 for (auto *U : N->users()) {
12670 unsigned &UnsortedOps = Degree[U];
12671 if (0 == --UnsortedOps)
12672 Order.push_back(U);
12673 }
12674 }
12675}
12676
12677#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12678void SelectionDAG::VerifyDAGDivergence() {
12679 std::vector<SDNode *> TopoOrder;
12680 CreateTopologicalOrder(TopoOrder);
12681 for (auto *N : TopoOrder) {
12682 assert(calculateDivergence(N) == N->isDivergent() &&
12683 "Divergence bit inconsistency detected");
12684 }
12685}
12686#endif
12687
12688/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12689/// uses of other values produced by From.getNode() alone. The same value
12690/// may appear in both the From and To list. The Deleted vector is
12691/// handled the same way as for ReplaceAllUsesWith.
12693 const SDValue *To,
12694 unsigned Num){
12695 // Handle the simple, trivial case efficiently.
12696 if (Num == 1)
12697 return ReplaceAllUsesOfValueWith(*From, *To);
12698
12699 transferDbgValues(*From, *To);
12700 copyExtraInfo(From->getNode(), To->getNode());
12701
12702 // Read up all the uses and make records of them. This helps
12703 // processing new uses that are introduced during the
12704 // replacement process.
12706 for (unsigned i = 0; i != Num; ++i) {
12707 unsigned FromResNo = From[i].getResNo();
12708 SDNode *FromNode = From[i].getNode();
12709 for (SDUse &Use : FromNode->uses()) {
12710 if (Use.getResNo() == FromResNo) {
12711 UseMemo Memo = {Use.getUser(), i, &Use};
12712 Uses.push_back(Memo);
12713 }
12714 }
12715 }
12716
12717 // Sort the uses, so that all the uses from a given User are together.
12719 RAUOVWUpdateListener Listener(*this, Uses);
12720
12721 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12722 UseIndex != UseIndexEnd; ) {
12723 // We know that this user uses some value of From. If it is the right
12724 // value, update it.
12725 SDNode *User = Uses[UseIndex].User;
12726 // If the node has been deleted by recursive CSE updates when updating
12727 // another node, then just skip this entry.
12728 if (User == nullptr) {
12729 ++UseIndex;
12730 continue;
12731 }
12732
12733 // This node is about to morph, remove its old self from the CSE maps.
12734 RemoveNodeFromCSEMaps(User);
12735
12736 // The Uses array is sorted, so all the uses for a given User
12737 // are next to each other in the list.
12738 // To help reduce the number of CSE recomputations, process all
12739 // the uses of this user that we can find this way.
12740 do {
12741 unsigned i = Uses[UseIndex].Index;
12742 SDUse &Use = *Uses[UseIndex].Use;
12743 ++UseIndex;
12744
12745 Use.set(To[i]);
12746 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12747
12748 // Now that we have modified User, add it back to the CSE maps. If it
12749 // already exists there, recursively merge the results together.
12750 AddModifiedNodeToCSEMaps(User);
12751 }
12752}
12753
12754/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12755/// based on their topological order. It returns the maximum id and a vector
12756/// of the SDNodes* in assigned order by reference.
12758 unsigned DAGSize = 0;
12759
12760 // SortedPos tracks the progress of the algorithm. Nodes before it are
12761 // sorted, nodes after it are unsorted. When the algorithm completes
12762 // it is at the end of the list.
12763 allnodes_iterator SortedPos = allnodes_begin();
12764
12765 // Visit all the nodes. Move nodes with no operands to the front of
12766 // the list immediately. Annotate nodes that do have operands with their
12767 // operand count. Before we do this, the Node Id fields of the nodes
12768 // may contain arbitrary values. After, the Node Id fields for nodes
12769 // before SortedPos will contain the topological sort index, and the
12770 // Node Id fields for nodes At SortedPos and after will contain the
12771 // count of outstanding operands.
12773 checkForCycles(&N, this);
12774 unsigned Degree = N.getNumOperands();
12775 if (Degree == 0) {
12776 // A node with no uses, add it to the result array immediately.
12777 N.setNodeId(DAGSize++);
12778 allnodes_iterator Q(&N);
12779 if (Q != SortedPos)
12780 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12781 assert(SortedPos != AllNodes.end() && "Overran node list");
12782 ++SortedPos;
12783 } else {
12784 // Temporarily use the Node Id as scratch space for the degree count.
12785 N.setNodeId(Degree);
12786 }
12787 }
12788
12789 // Visit all the nodes. As we iterate, move nodes into sorted order,
12790 // such that by the time the end is reached all nodes will be sorted.
12791 for (SDNode &Node : allnodes()) {
12792 SDNode *N = &Node;
12793 checkForCycles(N, this);
12794 // N is in sorted position, so all its uses have one less operand
12795 // that needs to be sorted.
12796 for (SDNode *P : N->users()) {
12797 unsigned Degree = P->getNodeId();
12798 assert(Degree != 0 && "Invalid node degree");
12799 --Degree;
12800 if (Degree == 0) {
12801 // All of P's operands are sorted, so P may sorted now.
12802 P->setNodeId(DAGSize++);
12803 if (P->getIterator() != SortedPos)
12804 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12805 assert(SortedPos != AllNodes.end() && "Overran node list");
12806 ++SortedPos;
12807 } else {
12808 // Update P's outstanding operand count.
12809 P->setNodeId(Degree);
12810 }
12811 }
12812 if (Node.getIterator() == SortedPos) {
12813#ifndef NDEBUG
12815 SDNode *S = &*++I;
12816 dbgs() << "Overran sorted position:\n";
12817 S->dumprFull(this); dbgs() << "\n";
12818 dbgs() << "Checking if this is due to cycles\n";
12819 checkForCycles(this, true);
12820#endif
12821 llvm_unreachable(nullptr);
12822 }
12823 }
12824
12825 assert(SortedPos == AllNodes.end() &&
12826 "Topological sort incomplete!");
12827 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12828 "First node in topological sort is not the entry token!");
12829 assert(AllNodes.front().getNodeId() == 0 &&
12830 "First node in topological sort has non-zero id!");
12831 assert(AllNodes.front().getNumOperands() == 0 &&
12832 "First node in topological sort has operands!");
12833 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12834 "Last node in topologic sort has unexpected id!");
12835 assert(AllNodes.back().use_empty() &&
12836 "Last node in topologic sort has users!");
12837 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12838 return DAGSize;
12839}
12840
12842 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12843 SortedNodes.clear();
12844 // Node -> remaining number of outstanding operands.
12845 DenseMap<const SDNode *, unsigned> RemainingOperands;
12846
12847 // Put nodes without any operands into SortedNodes first.
12848 for (const SDNode &N : allnodes()) {
12849 checkForCycles(&N, this);
12850 unsigned NumOperands = N.getNumOperands();
12851 if (NumOperands == 0)
12852 SortedNodes.push_back(&N);
12853 else
12854 // Record their total number of outstanding operands.
12855 RemainingOperands[&N] = NumOperands;
12856 }
12857
12858 // A node is pushed into SortedNodes when all of its operands (predecessors in
12859 // the graph) are also in SortedNodes.
12860 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12861 const SDNode *N = SortedNodes[i];
12862 for (const SDNode *U : N->users()) {
12863 // HandleSDNode is never part of a DAG and therefore has no entry in
12864 // RemainingOperands.
12865 if (U->getOpcode() == ISD::HANDLENODE)
12866 continue;
12867 unsigned &NumRemOperands = RemainingOperands[U];
12868 assert(NumRemOperands && "Invalid number of remaining operands");
12869 --NumRemOperands;
12870 if (!NumRemOperands)
12871 SortedNodes.push_back(U);
12872 }
12873 }
12874
12875 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12876 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12877 "First node in topological sort is not the entry token");
12878 assert(SortedNodes.front()->getNumOperands() == 0 &&
12879 "First node in topological sort has operands");
12880}
12881
12882/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12883/// value is produced by SD.
12884void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12885 for (SDNode *SD : DB->getSDNodes()) {
12886 if (!SD)
12887 continue;
12888 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12889 SD->setHasDebugValue(true);
12890 }
12891 DbgInfo->add(DB, isParameter);
12892}
12893
12894void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12895
12897 SDValue NewMemOpChain) {
12898 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12899 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12900 // The new memory operation must have the same position as the old load in
12901 // terms of memory dependency. Create a TokenFactor for the old load and new
12902 // memory operation and update uses of the old load's output chain to use that
12903 // TokenFactor.
12904 if (OldChain == NewMemOpChain || OldChain.use_empty())
12905 return NewMemOpChain;
12906
12907 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12908 OldChain, NewMemOpChain);
12909 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12910 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12911 return TokenFactor;
12912}
12913
12915 SDValue NewMemOp) {
12916 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12917 SDValue OldChain = SDValue(OldLoad, 1);
12918 SDValue NewMemOpChain = NewMemOp.getValue(1);
12919 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12920}
12921
12923 Function **OutFunction) {
12924 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12925
12926 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12927 auto *Module = MF->getFunction().getParent();
12928 auto *Function = Module->getFunction(Symbol);
12929
12930 if (OutFunction != nullptr)
12931 *OutFunction = Function;
12932
12933 if (Function != nullptr) {
12934 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12935 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12936 }
12937
12938 std::string ErrorStr;
12939 raw_string_ostream ErrorFormatter(ErrorStr);
12940 ErrorFormatter << "Undefined external symbol ";
12941 ErrorFormatter << '"' << Symbol << '"';
12942 report_fatal_error(Twine(ErrorStr));
12943}
12944
12945//===----------------------------------------------------------------------===//
12946// SDNode Class
12947//===----------------------------------------------------------------------===//
12948
12951 return Const != nullptr && Const->isZero();
12952}
12953
12955 return V.isUndef() || isNullConstant(V);
12956}
12957
12960 return Const != nullptr && Const->isZero() && !Const->isNegative();
12961}
12962
12965 return Const != nullptr && Const->isAllOnes();
12966}
12967
12970 return Const != nullptr && Const->isOne();
12971}
12972
12975 return Const != nullptr && Const->isMinSignedValue();
12976}
12977
12978bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
12979 unsigned OperandNo) {
12980 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
12981 // TODO: Target-specific opcodes could be added.
12982 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
12983 /*AllowTruncation*/ true)) {
12984 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
12985 switch (Opcode) {
12986 case ISD::ADD:
12987 case ISD::OR:
12988 case ISD::XOR:
12989 case ISD::UMAX:
12990 return Const.isZero();
12991 case ISD::MUL:
12992 return Const.isOne();
12993 case ISD::AND:
12994 case ISD::UMIN:
12995 return Const.isAllOnes();
12996 case ISD::SMAX:
12997 return Const.isMinSignedValue();
12998 case ISD::SMIN:
12999 return Const.isMaxSignedValue();
13000 case ISD::SUB:
13001 case ISD::SHL:
13002 case ISD::SRA:
13003 case ISD::SRL:
13004 return OperandNo == 1 && Const.isZero();
13005 case ISD::UDIV:
13006 case ISD::SDIV:
13007 return OperandNo == 1 && Const.isOne();
13008 }
13009 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13010 switch (Opcode) {
13011 case ISD::FADD:
13012 return ConstFP->isZero() &&
13013 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13014 case ISD::FSUB:
13015 return OperandNo == 1 && ConstFP->isZero() &&
13016 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13017 case ISD::FMUL:
13018 return ConstFP->isExactlyValue(1.0);
13019 case ISD::FDIV:
13020 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13021 case ISD::FMINNUM:
13022 case ISD::FMAXNUM: {
13023 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13024 EVT VT = V.getValueType();
13025 const fltSemantics &Semantics = VT.getFltSemantics();
13026 APFloat NeutralAF = !Flags.hasNoNaNs()
13027 ? APFloat::getQNaN(Semantics)
13028 : !Flags.hasNoInfs()
13029 ? APFloat::getInf(Semantics)
13030 : APFloat::getLargest(Semantics);
13031 if (Opcode == ISD::FMAXNUM)
13032 NeutralAF.changeSign();
13033
13034 return ConstFP->isExactlyValue(NeutralAF);
13035 }
13036 }
13037 }
13038 return false;
13039}
13040
13042 while (V.getOpcode() == ISD::BITCAST)
13043 V = V.getOperand(0);
13044 return V;
13045}
13046
13048 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13049 V = V.getOperand(0);
13050 return V;
13051}
13052
13054 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13055 V = V.getOperand(0);
13056 return V;
13057}
13058
13060 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13061 SDValue InVec = V.getOperand(0);
13062 SDValue EltNo = V.getOperand(2);
13063 EVT VT = InVec.getValueType();
13064 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13065 if (IndexC && VT.isFixedLengthVector() &&
13066 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13067 !DemandedElts[IndexC->getZExtValue()]) {
13068 V = InVec;
13069 continue;
13070 }
13071 break;
13072 }
13073 return V;
13074}
13075
13077 while (V.getOpcode() == ISD::TRUNCATE)
13078 V = V.getOperand(0);
13079 return V;
13080}
13081
13082bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13083 if (V.getOpcode() != ISD::XOR)
13084 return false;
13085 V = peekThroughBitcasts(V.getOperand(1));
13086 unsigned NumBits = V.getScalarValueSizeInBits();
13087 ConstantSDNode *C =
13088 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13089 return C && (C->getAPIntValue().countr_one() >= NumBits);
13090}
13091
13093 bool AllowTruncation) {
13094 EVT VT = N.getValueType();
13095 APInt DemandedElts = VT.isFixedLengthVector()
13097 : APInt(1, 1);
13098 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13099}
13100
13102 bool AllowUndefs,
13103 bool AllowTruncation) {
13105 return CN;
13106
13107 // SplatVectors can truncate their operands. Ignore that case here unless
13108 // AllowTruncation is set.
13109 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13110 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13111 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13112 EVT CVT = CN->getValueType(0);
13113 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13114 if (AllowTruncation || CVT == VecEltVT)
13115 return CN;
13116 }
13117 }
13118
13120 BitVector UndefElements;
13121 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13122
13123 // BuildVectors can truncate their operands. Ignore that case here unless
13124 // AllowTruncation is set.
13125 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13126 if (CN && (UndefElements.none() || AllowUndefs)) {
13127 EVT CVT = CN->getValueType(0);
13128 EVT NSVT = N.getValueType().getScalarType();
13129 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13130 if (AllowTruncation || (CVT == NSVT))
13131 return CN;
13132 }
13133 }
13134
13135 return nullptr;
13136}
13137
13139 EVT VT = N.getValueType();
13140 APInt DemandedElts = VT.isFixedLengthVector()
13142 : APInt(1, 1);
13143 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13144}
13145
13147 const APInt &DemandedElts,
13148 bool AllowUndefs) {
13150 return CN;
13151
13153 BitVector UndefElements;
13154 ConstantFPSDNode *CN =
13155 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13156 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13157 if (CN && (UndefElements.none() || AllowUndefs))
13158 return CN;
13159 }
13160
13161 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13162 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13163 return CN;
13164
13165 return nullptr;
13166}
13167
13168bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13169 // TODO: may want to use peekThroughBitcast() here.
13170 ConstantSDNode *C =
13171 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13172 return C && C->isZero();
13173}
13174
13175bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13176 ConstantSDNode *C =
13177 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13178 return C && C->isOne();
13179}
13180
13181bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13182 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13183 return C && C->isExactlyValue(1.0);
13184}
13185
13186bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13188 unsigned BitWidth = N.getScalarValueSizeInBits();
13189 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13190 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13191}
13192
13193bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13194 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13195 return C && APInt::isSameValue(C->getAPIntValue(),
13196 APInt(C->getAPIntValue().getBitWidth(), 1));
13197}
13198
13199bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13201 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13202 return C && C->isZero();
13203}
13204
13205bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13206 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13207 return C && C->isZero();
13208}
13209
13213
13214MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13215 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13216 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13217 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13218 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13219 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13220 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13221
13222 // We check here that the size of the memory operand fits within the size of
13223 // the MMO. This is because the MMO might indicate only a possible address
13224 // range instead of specifying the affected memory addresses precisely.
13225 assert(
13226 (!MMO->getType().isValid() ||
13227 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13228 "Size mismatch!");
13229}
13230
13231/// Profile - Gather unique data for the node.
13232///
13234 AddNodeIDNode(ID, this);
13235}
13236
13237namespace {
13238
13239 struct EVTArray {
13240 std::vector<EVT> VTs;
13241
13242 EVTArray() {
13243 VTs.reserve(MVT::VALUETYPE_SIZE);
13244 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13245 VTs.push_back(MVT((MVT::SimpleValueType)i));
13246 }
13247 };
13248
13249} // end anonymous namespace
13250
13251/// getValueTypeList - Return a pointer to the specified value type.
13252///
13253const EVT *SDNode::getValueTypeList(MVT VT) {
13254 static EVTArray SimpleVTArray;
13255
13256 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13257 return &SimpleVTArray.VTs[VT.SimpleTy];
13258}
13259
13260/// hasAnyUseOfValue - Return true if there are any use of the indicated
13261/// value. This method ignores uses of other values defined by this operation.
13262bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13263 assert(Value < getNumValues() && "Bad value!");
13264
13265 for (SDUse &U : uses())
13266 if (U.getResNo() == Value)
13267 return true;
13268
13269 return false;
13270}
13271
13272/// isOnlyUserOf - Return true if this node is the only use of N.
13273bool SDNode::isOnlyUserOf(const SDNode *N) const {
13274 bool Seen = false;
13275 for (const SDNode *User : N->users()) {
13276 if (User == this)
13277 Seen = true;
13278 else
13279 return false;
13280 }
13281
13282 return Seen;
13283}
13284
13285/// Return true if the only users of N are contained in Nodes.
13287 bool Seen = false;
13288 for (const SDNode *User : N->users()) {
13289 if (llvm::is_contained(Nodes, User))
13290 Seen = true;
13291 else
13292 return false;
13293 }
13294
13295 return Seen;
13296}
13297
13298/// Return true if the referenced return value is an operand of N.
13299bool SDValue::isOperandOf(const SDNode *N) const {
13300 return is_contained(N->op_values(), *this);
13301}
13302
13303bool SDNode::isOperandOf(const SDNode *N) const {
13304 return any_of(N->op_values(),
13305 [this](SDValue Op) { return this == Op.getNode(); });
13306}
13307
13308/// reachesChainWithoutSideEffects - Return true if this operand (which must
13309/// be a chain) reaches the specified operand without crossing any
13310/// side-effecting instructions on any chain path. In practice, this looks
13311/// through token factors and non-volatile loads. In order to remain efficient,
13312/// this only looks a couple of nodes in, it does not do an exhaustive search.
13313///
13314/// Note that we only need to examine chains when we're searching for
13315/// side-effects; SelectionDAG requires that all side-effects are represented
13316/// by chains, even if another operand would force a specific ordering. This
13317/// constraint is necessary to allow transformations like splitting loads.
13319 unsigned Depth) const {
13320 if (*this == Dest) return true;
13321
13322 // Don't search too deeply, we just want to be able to see through
13323 // TokenFactor's etc.
13324 if (Depth == 0) return false;
13325
13326 // If this is a token factor, all inputs to the TF happen in parallel.
13327 if (getOpcode() == ISD::TokenFactor) {
13328 // First, try a shallow search.
13329 if (is_contained((*this)->ops(), Dest)) {
13330 // We found the chain we want as an operand of this TokenFactor.
13331 // Essentially, we reach the chain without side-effects if we could
13332 // serialize the TokenFactor into a simple chain of operations with
13333 // Dest as the last operation. This is automatically true if the
13334 // chain has one use: there are no other ordering constraints.
13335 // If the chain has more than one use, we give up: some other
13336 // use of Dest might force a side-effect between Dest and the current
13337 // node.
13338 if (Dest.hasOneUse())
13339 return true;
13340 }
13341 // Next, try a deep search: check whether every operand of the TokenFactor
13342 // reaches Dest.
13343 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13344 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13345 });
13346 }
13347
13348 // Loads don't have side effects, look through them.
13349 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13350 if (Ld->isUnordered())
13351 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13352 }
13353 return false;
13354}
13355
13356bool SDNode::hasPredecessor(const SDNode *N) const {
13359 Worklist.push_back(this);
13360 return hasPredecessorHelper(N, Visited, Worklist);
13361}
13362
13364 this->Flags &= Flags;
13365}
13366
13367SDValue
13369 ArrayRef<ISD::NodeType> CandidateBinOps,
13370 bool AllowPartials) {
13371 // The pattern must end in an extract from index 0.
13372 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13373 !isNullConstant(Extract->getOperand(1)))
13374 return SDValue();
13375
13376 // Match against one of the candidate binary ops.
13377 SDValue Op = Extract->getOperand(0);
13378 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13379 return Op.getOpcode() == unsigned(BinOp);
13380 }))
13381 return SDValue();
13382
13383 // Floating-point reductions may require relaxed constraints on the final step
13384 // of the reduction because they may reorder intermediate operations.
13385 unsigned CandidateBinOp = Op.getOpcode();
13386 if (Op.getValueType().isFloatingPoint()) {
13387 SDNodeFlags Flags = Op->getFlags();
13388 switch (CandidateBinOp) {
13389 case ISD::FADD:
13390 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13391 return SDValue();
13392 break;
13393 default:
13394 llvm_unreachable("Unhandled FP opcode for binop reduction");
13395 }
13396 }
13397
13398 // Matching failed - attempt to see if we did enough stages that a partial
13399 // reduction from a subvector is possible.
13400 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13401 if (!AllowPartials || !Op)
13402 return SDValue();
13403 EVT OpVT = Op.getValueType();
13404 EVT OpSVT = OpVT.getScalarType();
13405 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13406 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13407 return SDValue();
13408 BinOp = (ISD::NodeType)CandidateBinOp;
13409 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13410 };
13411
13412 // At each stage, we're looking for something that looks like:
13413 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13414 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13415 // i32 undef, i32 undef, i32 undef, i32 undef>
13416 // %a = binop <8 x i32> %op, %s
13417 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13418 // we expect something like:
13419 // <4,5,6,7,u,u,u,u>
13420 // <2,3,u,u,u,u,u,u>
13421 // <1,u,u,u,u,u,u,u>
13422 // While a partial reduction match would be:
13423 // <2,3,u,u,u,u,u,u>
13424 // <1,u,u,u,u,u,u,u>
13425 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13426 SDValue PrevOp;
13427 for (unsigned i = 0; i < Stages; ++i) {
13428 unsigned MaskEnd = (1 << i);
13429
13430 if (Op.getOpcode() != CandidateBinOp)
13431 return PartialReduction(PrevOp, MaskEnd);
13432
13433 SDValue Op0 = Op.getOperand(0);
13434 SDValue Op1 = Op.getOperand(1);
13435
13437 if (Shuffle) {
13438 Op = Op1;
13439 } else {
13440 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13441 Op = Op0;
13442 }
13443
13444 // The first operand of the shuffle should be the same as the other operand
13445 // of the binop.
13446 if (!Shuffle || Shuffle->getOperand(0) != Op)
13447 return PartialReduction(PrevOp, MaskEnd);
13448
13449 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13450 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13451 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13452 return PartialReduction(PrevOp, MaskEnd);
13453
13454 PrevOp = Op;
13455 }
13456
13457 // Handle subvector reductions, which tend to appear after the shuffle
13458 // reduction stages.
13459 while (Op.getOpcode() == CandidateBinOp) {
13460 unsigned NumElts = Op.getValueType().getVectorNumElements();
13461 SDValue Op0 = Op.getOperand(0);
13462 SDValue Op1 = Op.getOperand(1);
13463 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13465 Op0.getOperand(0) != Op1.getOperand(0))
13466 break;
13467 SDValue Src = Op0.getOperand(0);
13468 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13469 if (NumSrcElts != (2 * NumElts))
13470 break;
13471 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13472 Op1.getConstantOperandAPInt(1) == NumElts) &&
13473 !(Op1.getConstantOperandAPInt(1) == 0 &&
13474 Op0.getConstantOperandAPInt(1) == NumElts))
13475 break;
13476 Op = Src;
13477 }
13478
13479 BinOp = (ISD::NodeType)CandidateBinOp;
13480 return Op;
13481}
13482
13484 EVT VT = N->getValueType(0);
13485 EVT EltVT = VT.getVectorElementType();
13486 unsigned NE = VT.getVectorNumElements();
13487
13488 SDLoc dl(N);
13489
13490 // If ResNE is 0, fully unroll the vector op.
13491 if (ResNE == 0)
13492 ResNE = NE;
13493 else if (NE > ResNE)
13494 NE = ResNE;
13495
13496 if (N->getNumValues() == 2) {
13497 SmallVector<SDValue, 8> Scalars0, Scalars1;
13498 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13499 EVT VT1 = N->getValueType(1);
13500 EVT EltVT1 = VT1.getVectorElementType();
13501
13502 unsigned i;
13503 for (i = 0; i != NE; ++i) {
13504 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13505 SDValue Operand = N->getOperand(j);
13506 EVT OperandVT = Operand.getValueType();
13507
13508 // A vector operand; extract a single element.
13509 EVT OperandEltVT = OperandVT.getVectorElementType();
13510 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13511 }
13512
13513 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13514 Scalars0.push_back(EltOp);
13515 Scalars1.push_back(EltOp.getValue(1));
13516 }
13517
13518 for (; i < ResNE; ++i) {
13519 Scalars0.push_back(getUNDEF(EltVT));
13520 Scalars1.push_back(getUNDEF(EltVT1));
13521 }
13522
13523 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13524 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13525 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13526 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13527 return getMergeValues({Vec0, Vec1}, dl);
13528 }
13529
13530 assert(N->getNumValues() == 1 &&
13531 "Can't unroll a vector with multiple results!");
13532
13534 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13535
13536 unsigned i;
13537 for (i= 0; i != NE; ++i) {
13538 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13539 SDValue Operand = N->getOperand(j);
13540 EVT OperandVT = Operand.getValueType();
13541 if (OperandVT.isVector()) {
13542 // A vector operand; extract a single element.
13543 EVT OperandEltVT = OperandVT.getVectorElementType();
13544 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13545 } else {
13546 // A scalar operand; just use it as is.
13547 Operands[j] = Operand;
13548 }
13549 }
13550
13551 switch (N->getOpcode()) {
13552 default: {
13553 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13554 N->getFlags()));
13555 break;
13556 }
13557 case ISD::VSELECT:
13558 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13559 break;
13560 case ISD::SHL:
13561 case ISD::SRA:
13562 case ISD::SRL:
13563 case ISD::ROTL:
13564 case ISD::ROTR:
13565 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13566 getShiftAmountOperand(Operands[0].getValueType(),
13567 Operands[1])));
13568 break;
13570 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13571 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13572 Operands[0],
13573 getValueType(ExtVT)));
13574 break;
13575 }
13576 case ISD::ADDRSPACECAST: {
13577 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13578 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13579 ASC->getSrcAddressSpace(),
13580 ASC->getDestAddressSpace()));
13581 break;
13582 }
13583 }
13584 }
13585
13586 for (; i < ResNE; ++i)
13587 Scalars.push_back(getUNDEF(EltVT));
13588
13589 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13590 return getBuildVector(VecVT, dl, Scalars);
13591}
13592
13593std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13594 SDNode *N, unsigned ResNE) {
13595 unsigned Opcode = N->getOpcode();
13596 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13597 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13598 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13599 "Expected an overflow opcode");
13600
13601 EVT ResVT = N->getValueType(0);
13602 EVT OvVT = N->getValueType(1);
13603 EVT ResEltVT = ResVT.getVectorElementType();
13604 EVT OvEltVT = OvVT.getVectorElementType();
13605 SDLoc dl(N);
13606
13607 // If ResNE is 0, fully unroll the vector op.
13608 unsigned NE = ResVT.getVectorNumElements();
13609 if (ResNE == 0)
13610 ResNE = NE;
13611 else if (NE > ResNE)
13612 NE = ResNE;
13613
13614 SmallVector<SDValue, 8> LHSScalars;
13615 SmallVector<SDValue, 8> RHSScalars;
13616 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13617 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13618
13619 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13620 SDVTList VTs = getVTList(ResEltVT, SVT);
13621 SmallVector<SDValue, 8> ResScalars;
13622 SmallVector<SDValue, 8> OvScalars;
13623 for (unsigned i = 0; i < NE; ++i) {
13624 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13625 SDValue Ov =
13626 getSelect(dl, OvEltVT, Res.getValue(1),
13627 getBoolConstant(true, dl, OvEltVT, ResVT),
13628 getConstant(0, dl, OvEltVT));
13629
13630 ResScalars.push_back(Res);
13631 OvScalars.push_back(Ov);
13632 }
13633
13634 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13635 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13636
13637 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13638 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13639 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13640 getBuildVector(NewOvVT, dl, OvScalars));
13641}
13642
13645 unsigned Bytes,
13646 int Dist) const {
13647 if (LD->isVolatile() || Base->isVolatile())
13648 return false;
13649 // TODO: probably too restrictive for atomics, revisit
13650 if (!LD->isSimple())
13651 return false;
13652 if (LD->isIndexed() || Base->isIndexed())
13653 return false;
13654 if (LD->getChain() != Base->getChain())
13655 return false;
13656 EVT VT = LD->getMemoryVT();
13657 if (VT.getSizeInBits() / 8 != Bytes)
13658 return false;
13659
13660 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13661 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13662
13663 int64_t Offset = 0;
13664 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13665 return (Dist * (int64_t)Bytes == Offset);
13666 return false;
13667}
13668
13669/// InferPtrAlignment - Infer alignment of a load / store address. Return
13670/// std::nullopt if it cannot be inferred.
13672 // If this is a GlobalAddress + cst, return the alignment.
13673 const GlobalValue *GV = nullptr;
13674 int64_t GVOffset = 0;
13675 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13676 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13677 KnownBits Known(PtrWidth);
13679 unsigned AlignBits = Known.countMinTrailingZeros();
13680 if (AlignBits)
13681 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13682 }
13683
13684 // If this is a direct reference to a stack slot, use information about the
13685 // stack slot's alignment.
13686 int FrameIdx = INT_MIN;
13687 int64_t FrameOffset = 0;
13689 FrameIdx = FI->getIndex();
13690 } else if (isBaseWithConstantOffset(Ptr) &&
13692 // Handle FI+Cst
13693 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13694 FrameOffset = Ptr.getConstantOperandVal(1);
13695 }
13696
13697 if (FrameIdx != INT_MIN) {
13699 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13700 }
13701
13702 return std::nullopt;
13703}
13704
13705/// Split the scalar node with EXTRACT_ELEMENT using the provided
13706/// VTs and return the low/high part.
13707std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13708 const SDLoc &DL,
13709 const EVT &LoVT,
13710 const EVT &HiVT) {
13711 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13712 "Split node must be a scalar type");
13713 SDValue Lo =
13715 SDValue Hi =
13717 return std::make_pair(Lo, Hi);
13718}
13719
13720/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13721/// which is split (or expanded) into two not necessarily identical pieces.
13722std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13723 // Currently all types are split in half.
13724 EVT LoVT, HiVT;
13725 if (!VT.isVector())
13726 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13727 else
13728 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13729
13730 return std::make_pair(LoVT, HiVT);
13731}
13732
13733/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13734/// type, dependent on an enveloping VT that has been split into two identical
13735/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13736std::pair<EVT, EVT>
13738 bool *HiIsEmpty) const {
13739 EVT EltTp = VT.getVectorElementType();
13740 // Examples:
13741 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13742 // custom VL=9 with enveloping VL=8/8 yields 8/1
13743 // custom VL=10 with enveloping VL=8/8 yields 8/2
13744 // etc.
13745 ElementCount VTNumElts = VT.getVectorElementCount();
13746 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13747 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13748 "Mixing fixed width and scalable vectors when enveloping a type");
13749 EVT LoVT, HiVT;
13750 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13751 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13752 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13753 *HiIsEmpty = false;
13754 } else {
13755 // Flag that hi type has zero storage size, but return split envelop type
13756 // (this would be easier if vector types with zero elements were allowed).
13757 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13758 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13759 *HiIsEmpty = true;
13760 }
13761 return std::make_pair(LoVT, HiVT);
13762}
13763
13764/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13765/// low/high part.
13766std::pair<SDValue, SDValue>
13767SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13768 const EVT &HiVT) {
13769 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13770 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13771 "Splitting vector with an invalid mixture of fixed and scalable "
13772 "vector types");
13774 N.getValueType().getVectorMinNumElements() &&
13775 "More vector elements requested than available!");
13776 SDValue Lo, Hi;
13777 Lo = getExtractSubvector(DL, LoVT, N, 0);
13778 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13779 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13780 // IDX with the runtime scaling factor of the result vector type. For
13781 // fixed-width result vectors, that runtime scaling factor is 1.
13784 return std::make_pair(Lo, Hi);
13785}
13786
13787std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13788 const SDLoc &DL) {
13789 // Split the vector length parameter.
13790 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13791 EVT VT = N.getValueType();
13793 "Expecting the mask to be an evenly-sized vector");
13794 SDValue HalfNumElts = getElementCount(
13796 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13797 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13798 return std::make_pair(Lo, Hi);
13799}
13800
13801/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13803 EVT VT = N.getValueType();
13806 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13807}
13808
13811 unsigned Start, unsigned Count,
13812 EVT EltVT) {
13813 EVT VT = Op.getValueType();
13814 if (Count == 0)
13816 if (EltVT == EVT())
13817 EltVT = VT.getVectorElementType();
13818 SDLoc SL(Op);
13819 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13820 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13821 }
13822}
13823
13824// getAddressSpace - Return the address space this GlobalAddress belongs to.
13826 return getGlobal()->getType()->getAddressSpace();
13827}
13828
13831 return Val.MachineCPVal->getType();
13832 return Val.ConstVal->getType();
13833}
13834
13835bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13836 unsigned &SplatBitSize,
13837 bool &HasAnyUndefs,
13838 unsigned MinSplatBits,
13839 bool IsBigEndian) const {
13840 EVT VT = getValueType(0);
13841 assert(VT.isVector() && "Expected a vector type");
13842 unsigned VecWidth = VT.getSizeInBits();
13843 if (MinSplatBits > VecWidth)
13844 return false;
13845
13846 // FIXME: The widths are based on this node's type, but build vectors can
13847 // truncate their operands.
13848 SplatValue = APInt(VecWidth, 0);
13849 SplatUndef = APInt(VecWidth, 0);
13850
13851 // Get the bits. Bits with undefined values (when the corresponding element
13852 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13853 // in SplatValue. If any of the values are not constant, give up and return
13854 // false.
13855 unsigned int NumOps = getNumOperands();
13856 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13857 unsigned EltWidth = VT.getScalarSizeInBits();
13858
13859 for (unsigned j = 0; j < NumOps; ++j) {
13860 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13861 SDValue OpVal = getOperand(i);
13862 unsigned BitPos = j * EltWidth;
13863
13864 if (OpVal.isUndef())
13865 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13866 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13867 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13868 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13869 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13870 else
13871 return false;
13872 }
13873
13874 // The build_vector is all constants or undefs. Find the smallest element
13875 // size that splats the vector.
13876 HasAnyUndefs = (SplatUndef != 0);
13877
13878 // FIXME: This does not work for vectors with elements less than 8 bits.
13879 while (VecWidth > 8) {
13880 // If we can't split in half, stop here.
13881 if (VecWidth & 1)
13882 break;
13883
13884 unsigned HalfSize = VecWidth / 2;
13885 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13886 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13887 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13888 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13889
13890 // If the two halves do not match (ignoring undef bits), stop here.
13891 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13892 MinSplatBits > HalfSize)
13893 break;
13894
13895 SplatValue = HighValue | LowValue;
13896 SplatUndef = HighUndef & LowUndef;
13897
13898 VecWidth = HalfSize;
13899 }
13900
13901 // FIXME: The loop above only tries to split in halves. But if the input
13902 // vector for example is <3 x i16> it wouldn't be able to detect a
13903 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13904 // optimizations. I guess that back in the days when this helper was created
13905 // vectors normally was power-of-2 sized.
13906
13907 SplatBitSize = VecWidth;
13908 return true;
13909}
13910
13912 BitVector *UndefElements) const {
13913 unsigned NumOps = getNumOperands();
13914 if (UndefElements) {
13915 UndefElements->clear();
13916 UndefElements->resize(NumOps);
13917 }
13918 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13919 if (!DemandedElts)
13920 return SDValue();
13921 SDValue Splatted;
13922 for (unsigned i = 0; i != NumOps; ++i) {
13923 if (!DemandedElts[i])
13924 continue;
13925 SDValue Op = getOperand(i);
13926 if (Op.isUndef()) {
13927 if (UndefElements)
13928 (*UndefElements)[i] = true;
13929 } else if (!Splatted) {
13930 Splatted = Op;
13931 } else if (Splatted != Op) {
13932 return SDValue();
13933 }
13934 }
13935
13936 if (!Splatted) {
13937 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13938 assert(getOperand(FirstDemandedIdx).isUndef() &&
13939 "Can only have a splat without a constant for all undefs.");
13940 return getOperand(FirstDemandedIdx);
13941 }
13942
13943 return Splatted;
13944}
13945
13947 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13948 return getSplatValue(DemandedElts, UndefElements);
13949}
13950
13952 SmallVectorImpl<SDValue> &Sequence,
13953 BitVector *UndefElements) const {
13954 unsigned NumOps = getNumOperands();
13955 Sequence.clear();
13956 if (UndefElements) {
13957 UndefElements->clear();
13958 UndefElements->resize(NumOps);
13959 }
13960 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13961 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
13962 return false;
13963
13964 // Set the undefs even if we don't find a sequence (like getSplatValue).
13965 if (UndefElements)
13966 for (unsigned I = 0; I != NumOps; ++I)
13967 if (DemandedElts[I] && getOperand(I).isUndef())
13968 (*UndefElements)[I] = true;
13969
13970 // Iteratively widen the sequence length looking for repetitions.
13971 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
13972 Sequence.append(SeqLen, SDValue());
13973 for (unsigned I = 0; I != NumOps; ++I) {
13974 if (!DemandedElts[I])
13975 continue;
13976 SDValue &SeqOp = Sequence[I % SeqLen];
13978 if (Op.isUndef()) {
13979 if (!SeqOp)
13980 SeqOp = Op;
13981 continue;
13982 }
13983 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
13984 Sequence.clear();
13985 break;
13986 }
13987 SeqOp = Op;
13988 }
13989 if (!Sequence.empty())
13990 return true;
13991 }
13992
13993 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
13994 return false;
13995}
13996
13998 BitVector *UndefElements) const {
13999 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14000 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14001}
14002
14005 BitVector *UndefElements) const {
14007 getSplatValue(DemandedElts, UndefElements));
14008}
14009
14012 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14013}
14014
14017 BitVector *UndefElements) const {
14019 getSplatValue(DemandedElts, UndefElements));
14020}
14021
14026
14027int32_t
14029 uint32_t BitWidth) const {
14030 if (ConstantFPSDNode *CN =
14032 bool IsExact;
14033 APSInt IntVal(BitWidth);
14034 const APFloat &APF = CN->getValueAPF();
14035 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14036 APFloat::opOK ||
14037 !IsExact)
14038 return -1;
14039
14040 return IntVal.exactLogBase2();
14041 }
14042 return -1;
14043}
14044
14046 bool IsLittleEndian, unsigned DstEltSizeInBits,
14047 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14048 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14049 if (!isConstant())
14050 return false;
14051
14052 unsigned NumSrcOps = getNumOperands();
14053 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14054 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14055 "Invalid bitcast scale");
14056
14057 // Extract raw src bits.
14058 SmallVector<APInt> SrcBitElements(NumSrcOps,
14059 APInt::getZero(SrcEltSizeInBits));
14060 BitVector SrcUndeElements(NumSrcOps, false);
14061
14062 for (unsigned I = 0; I != NumSrcOps; ++I) {
14064 if (Op.isUndef()) {
14065 SrcUndeElements.set(I);
14066 continue;
14067 }
14068 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14069 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14070 assert((CInt || CFP) && "Unknown constant");
14071 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14072 : CFP->getValueAPF().bitcastToAPInt();
14073 }
14074
14075 // Recast to dst width.
14076 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14077 SrcBitElements, UndefElements, SrcUndeElements);
14078 return true;
14079}
14080
14081void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14082 unsigned DstEltSizeInBits,
14083 SmallVectorImpl<APInt> &DstBitElements,
14084 ArrayRef<APInt> SrcBitElements,
14085 BitVector &DstUndefElements,
14086 const BitVector &SrcUndefElements) {
14087 unsigned NumSrcOps = SrcBitElements.size();
14088 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14089 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14090 "Invalid bitcast scale");
14091 assert(NumSrcOps == SrcUndefElements.size() &&
14092 "Vector size mismatch");
14093
14094 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14095 DstUndefElements.clear();
14096 DstUndefElements.resize(NumDstOps, false);
14097 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14098
14099 // Concatenate src elements constant bits together into dst element.
14100 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14101 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14102 for (unsigned I = 0; I != NumDstOps; ++I) {
14103 DstUndefElements.set(I);
14104 APInt &DstBits = DstBitElements[I];
14105 for (unsigned J = 0; J != Scale; ++J) {
14106 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14107 if (SrcUndefElements[Idx])
14108 continue;
14109 DstUndefElements.reset(I);
14110 const APInt &SrcBits = SrcBitElements[Idx];
14111 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14112 "Illegal constant bitwidths");
14113 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14114 }
14115 }
14116 return;
14117 }
14118
14119 // Split src element constant bits into dst elements.
14120 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14121 for (unsigned I = 0; I != NumSrcOps; ++I) {
14122 if (SrcUndefElements[I]) {
14123 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14124 continue;
14125 }
14126 const APInt &SrcBits = SrcBitElements[I];
14127 for (unsigned J = 0; J != Scale; ++J) {
14128 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14129 APInt &DstBits = DstBitElements[Idx];
14130 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14131 }
14132 }
14133}
14134
14136 for (const SDValue &Op : op_values()) {
14137 unsigned Opc = Op.getOpcode();
14138 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14139 return false;
14140 }
14141 return true;
14142}
14143
14144std::optional<std::pair<APInt, APInt>>
14146 unsigned NumOps = getNumOperands();
14147 if (NumOps < 2)
14148 return std::nullopt;
14149
14152 return std::nullopt;
14153
14154 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14155 APInt Start = getConstantOperandAPInt(0).trunc(EltSize);
14156 APInt Stride = getConstantOperandAPInt(1).trunc(EltSize) - Start;
14157
14158 if (Stride.isZero())
14159 return std::nullopt;
14160
14161 for (unsigned i = 2; i < NumOps; ++i) {
14163 return std::nullopt;
14164
14165 APInt Val = getConstantOperandAPInt(i).trunc(EltSize);
14166 if (Val != (Start + (Stride * i)))
14167 return std::nullopt;
14168 }
14169
14170 return std::make_pair(Start, Stride);
14171}
14172
14174 // Find the first non-undef value in the shuffle mask.
14175 unsigned i, e;
14176 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14177 /* search */;
14178
14179 // If all elements are undefined, this shuffle can be considered a splat
14180 // (although it should eventually get simplified away completely).
14181 if (i == e)
14182 return true;
14183
14184 // Make sure all remaining elements are either undef or the same as the first
14185 // non-undef value.
14186 for (int Idx = Mask[i]; i != e; ++i)
14187 if (Mask[i] >= 0 && Mask[i] != Idx)
14188 return false;
14189 return true;
14190}
14191
14192// Returns true if it is a constant integer BuildVector or constant integer,
14193// possibly hidden by a bitcast.
14195 SDValue N, bool AllowOpaques) const {
14197
14198 if (auto *C = dyn_cast<ConstantSDNode>(N))
14199 return AllowOpaques || !C->isOpaque();
14200
14202 return true;
14203
14204 // Treat a GlobalAddress supporting constant offset folding as a
14205 // constant integer.
14206 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14207 if (GA->getOpcode() == ISD::GlobalAddress &&
14208 TLI->isOffsetFoldingLegal(GA))
14209 return true;
14210
14211 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14212 isa<ConstantSDNode>(N.getOperand(0)))
14213 return true;
14214 return false;
14215}
14216
14217// Returns true if it is a constant float BuildVector or constant float.
14220 return true;
14221
14223 return true;
14224
14225 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14226 isa<ConstantFPSDNode>(N.getOperand(0)))
14227 return true;
14228
14229 return false;
14230}
14231
14232std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14233 ConstantSDNode *Const =
14234 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14235 if (!Const)
14236 return std::nullopt;
14237
14238 EVT VT = N->getValueType(0);
14239 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14240 switch (TLI->getBooleanContents(N.getValueType())) {
14242 if (CVal.isOne())
14243 return true;
14244 if (CVal.isZero())
14245 return false;
14246 return std::nullopt;
14248 if (CVal.isAllOnes())
14249 return true;
14250 if (CVal.isZero())
14251 return false;
14252 return std::nullopt;
14254 return CVal[0];
14255 }
14256 llvm_unreachable("Unknown BooleanContent enum");
14257}
14258
14259void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14260 assert(!Node->OperandList && "Node already has operands");
14262 "too many operands to fit into SDNode");
14263 SDUse *Ops = OperandRecycler.allocate(
14264 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14265
14266 bool IsDivergent = false;
14267 for (unsigned I = 0; I != Vals.size(); ++I) {
14268 Ops[I].setUser(Node);
14269 Ops[I].setInitial(Vals[I]);
14270 EVT VT = Ops[I].getValueType();
14271
14272 // Skip Chain. It does not carry divergence.
14273 if (VT != MVT::Other &&
14274 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14275 Ops[I].getNode()->isDivergent()) {
14276 IsDivergent = true;
14277 }
14278 }
14279 Node->NumOperands = Vals.size();
14280 Node->OperandList = Ops;
14281 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14282 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14283 Node->SDNodeBits.IsDivergent = IsDivergent;
14284 }
14285 checkForCycles(Node);
14286}
14287
14290 size_t Limit = SDNode::getMaxNumOperands();
14291 while (Vals.size() > Limit) {
14292 unsigned SliceIdx = Vals.size() - Limit;
14293 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14294 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14295 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14296 Vals.emplace_back(NewTF);
14297 }
14298 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14299}
14300
14302 EVT VT, SDNodeFlags Flags) {
14303 switch (Opcode) {
14304 default:
14305 return SDValue();
14306 case ISD::ADD:
14307 case ISD::OR:
14308 case ISD::XOR:
14309 case ISD::UMAX:
14310 return getConstant(0, DL, VT);
14311 case ISD::MUL:
14312 return getConstant(1, DL, VT);
14313 case ISD::AND:
14314 case ISD::UMIN:
14315 return getAllOnesConstant(DL, VT);
14316 case ISD::SMAX:
14318 case ISD::SMIN:
14320 case ISD::FADD:
14321 // If flags allow, prefer positive zero since it's generally cheaper
14322 // to materialize on most targets.
14323 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14324 case ISD::FMUL:
14325 return getConstantFP(1.0, DL, VT);
14326 case ISD::FMINNUM:
14327 case ISD::FMAXNUM: {
14328 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14329 const fltSemantics &Semantics = VT.getFltSemantics();
14330 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14331 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14332 APFloat::getLargest(Semantics);
14333 if (Opcode == ISD::FMAXNUM)
14334 NeutralAF.changeSign();
14335
14336 return getConstantFP(NeutralAF, DL, VT);
14337 }
14338 case ISD::FMINIMUM:
14339 case ISD::FMAXIMUM: {
14340 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14341 const fltSemantics &Semantics = VT.getFltSemantics();
14342 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14343 : APFloat::getLargest(Semantics);
14344 if (Opcode == ISD::FMAXIMUM)
14345 NeutralAF.changeSign();
14346
14347 return getConstantFP(NeutralAF, DL, VT);
14348 }
14349
14350 }
14351}
14352
14353/// Helper used to make a call to a library function that has one argument of
14354/// pointer type.
14355///
14356/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14357/// used to get or set floating-point state. They have one argument of pointer
14358/// type, which points to the memory region containing bits of the
14359/// floating-point state. The value returned by such function is ignored in the
14360/// created call.
14361///
14362/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14363/// \param Ptr Pointer used to save/load state.
14364/// \param InChain Ingoing token chain.
14365/// \returns Outgoing chain token.
14367 SDValue InChain,
14368 const SDLoc &DLoc) {
14369 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14371 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14372 RTLIB::LibcallImpl LibcallImpl =
14373 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14374 if (LibcallImpl == RTLIB::Unsupported)
14375 reportFatalUsageError("emitting call to unsupported libcall");
14376
14377 SDValue Callee =
14378 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14380 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14381 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14382 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14383 return TLI->LowerCallTo(CLI).second;
14384}
14385
14387 assert(From && To && "Invalid SDNode; empty source SDValue?");
14388 auto I = SDEI.find(From);
14389 if (I == SDEI.end())
14390 return;
14391
14392 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14393 // the iterator, hence the need to make a copy to prevent a use-after-free.
14394 NodeExtraInfo NEI = I->second;
14395 if (LLVM_LIKELY(!NEI.PCSections)) {
14396 // No deep copy required for the types of extra info set.
14397 //
14398 // FIXME: Investigate if other types of extra info also need deep copy. This
14399 // depends on the types of nodes they can be attached to: if some extra info
14400 // is only ever attached to nodes where a replacement To node is always the
14401 // node where later use and propagation of the extra info has the intended
14402 // semantics, no deep copy is required.
14403 SDEI[To] = std::move(NEI);
14404 return;
14405 }
14406
14407 const SDNode *EntrySDN = getEntryNode().getNode();
14408
14409 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14410 // through the replacement of From with To. Otherwise, replacements of a node
14411 // (From) with more complex nodes (To and its operands) may result in lost
14412 // extra info where the root node (To) is insignificant in further propagating
14413 // and using extra info when further lowering to MIR.
14414 //
14415 // In the first step pre-populate the visited set with the nodes reachable
14416 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14417 // DAG that is not new and should be left untouched.
14418 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14419 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14420 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14421 if (MaxDepth == 0) {
14422 // Remember this node in case we need to increase MaxDepth and continue
14423 // populating FromReach from this node.
14424 Leafs.emplace_back(N);
14425 return;
14426 }
14427 if (!FromReach.insert(N).second)
14428 return;
14429 for (const SDValue &Op : N->op_values())
14430 Self(Self, Op.getNode(), MaxDepth - 1);
14431 };
14432
14433 // Copy extra info to To and all its transitive operands (that are new).
14435 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14436 if (FromReach.contains(N))
14437 return true;
14438 if (!Visited.insert(N).second)
14439 return true;
14440 if (EntrySDN == N)
14441 return false;
14442 for (const SDValue &Op : N->op_values()) {
14443 if (N == To && Op.getNode() == EntrySDN) {
14444 // Special case: New node's operand is the entry node; just need to
14445 // copy extra info to new node.
14446 break;
14447 }
14448 if (!Self(Self, Op.getNode()))
14449 return false;
14450 }
14451 // Copy only if entry node was not reached.
14452 SDEI[N] = NEI;
14453 return true;
14454 };
14455
14456 // We first try with a lower MaxDepth, assuming that the path to common
14457 // operands between From and To is relatively short. This significantly
14458 // improves performance in the common case. The initial MaxDepth is big
14459 // enough to avoid retry in the common case; the last MaxDepth is large
14460 // enough to avoid having to use the fallback below (and protects from
14461 // potential stack exhaustion from recursion).
14462 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14463 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14464 // StartFrom is the previous (or initial) set of leafs reachable at the
14465 // previous maximum depth.
14467 std::swap(StartFrom, Leafs);
14468 for (const SDNode *N : StartFrom)
14469 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14470 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14471 return;
14472 // This should happen very rarely (reached the entry node).
14473 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14474 assert(!Leafs.empty());
14475 }
14476
14477 // This should not happen - but if it did, that means the subgraph reachable
14478 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14479 // could not visit all reachable common operands. Consequently, we were able
14480 // to reach the entry node.
14481 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14482 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14483 // Best-effort fallback if assertions disabled.
14484 SDEI[To] = std::move(NEI);
14485}
14486
14487#ifndef NDEBUG
14488static void checkForCyclesHelper(const SDNode *N,
14491 const llvm::SelectionDAG *DAG) {
14492 // If this node has already been checked, don't check it again.
14493 if (Checked.count(N))
14494 return;
14495
14496 // If a node has already been visited on this depth-first walk, reject it as
14497 // a cycle.
14498 if (!Visited.insert(N).second) {
14499 errs() << "Detected cycle in SelectionDAG\n";
14500 dbgs() << "Offending node:\n";
14501 N->dumprFull(DAG); dbgs() << "\n";
14502 abort();
14503 }
14504
14505 for (const SDValue &Op : N->op_values())
14506 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14507
14508 Checked.insert(N);
14509 Visited.erase(N);
14510}
14511#endif
14512
14514 const llvm::SelectionDAG *DAG,
14515 bool force) {
14516#ifndef NDEBUG
14517 bool check = force;
14518#ifdef EXPENSIVE_CHECKS
14519 check = true;
14520#endif // EXPENSIVE_CHECKS
14521 if (check) {
14522 assert(N && "Checking nonexistent SDNode");
14525 checkForCyclesHelper(N, visited, checked, DAG);
14526 }
14527#endif // !NDEBUG
14528}
14529
14530void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14531 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14532}
return SDValue()
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool isConstant(const MachineInstr &MI)
constexpr LLT S1
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
#define __asan_unpoison_memory_region(p, size)
Definition Compiler.h:592
#define LLVM_LIKELY(EXPR)
Definition Compiler.h:335
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseSet and SmallDenseSet classes.
This file contains constants used for implementing Dwarf debug support.
This file defines a hash set that can be used to remove duplication of nodes in a graph.
iv users
Definition IVUsers.cpp:48
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static LVOptions Options
Definition LVOptions.cpp:25
static Register getMemsetValue(Register Val, LLT Ty, MachineIRBuilder &MIB)
static bool shouldLowerMemFuncForSize(const MachineFunction &MF)
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:539
static Align getPrefTypeAlign(EVT VT, SelectionDAG &DAG)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file declares the MachineConstantPool class which is an abstract constant pool to keep track of ...
Register const TargetRegisterInfo * TRI
This file provides utility analysis objects describing memory locations.
This file contains the declarations for metadata subclasses.
#define T
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define P(N)
PowerPC Reduce CR logical Operation
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
Contains matchers for matching SelectionDAG nodes and values.
static Type * getValueType(Value *V)
Returns the type of the given value/instruction V.
This file contains some templates that are useful if you are working with the STL at all.
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static SDValue getMemcpyLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo, BatchAAResults *BatchAA)
static SDValue getFixedOrScalableQuantity(SelectionDAG &DAG, const SDLoc &DL, EVT VT, Ty Quantity)
static std::pair< SDValue, SDValue > getRuntimeCallSDValueHelper(SDValue Chain, const SDLoc &dl, TargetLowering::ArgListTy &&Args, const CallInst *CI, RTLIB::Libcall Call, SelectionDAG *DAG, const TargetLowering *TLI)
static SDValue getMemsetStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo)
Lower the call to 'memset' intrinsic function into a series of store operations.
static std::optional< APInt > FoldValueWithUndef(unsigned Opcode, const APInt &C1, bool IsUndef1, const APInt &C2, bool IsUndef2)
static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step, SelectionDAG &DAG)
static void AddNodeIDNode(FoldingSetNodeID &ID, unsigned OpC, SDVTList VTList, ArrayRef< SDValue > OpList)
static SDValue getMemsetStringVal(EVT VT, const SDLoc &dl, SelectionDAG &DAG, const TargetLowering &TLI, const ConstantDataArraySlice &Slice)
getMemsetStringVal - Similar to getMemsetValue.
static cl::opt< bool > EnableMemCpyDAGOpt("enable-memcpy-dag-opt", cl::Hidden, cl::init(true), cl::desc("Gang up loads and stores generated by inlining of memcpy"))
static bool haveNoCommonBitsSetCommutative(SDValue A, SDValue B)
static void AddNodeIDValueTypes(FoldingSetNodeID &ID, SDVTList VTList)
AddNodeIDValueTypes - Value type lists are intern'd so we can represent them solely with their pointe...
static void commuteShuffle(SDValue &N1, SDValue &N2, MutableArrayRef< int > M)
Swaps the values of N1 and N2.
static bool isMemSrcFromConstant(SDValue Src, ConstantDataArraySlice &Slice)
Returns true if memcpy source is constant data.
static SDValue getMemmoveLoadsAndStores(SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src, uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo)
static void AddNodeIDOpcode(FoldingSetNodeID &ID, unsigned OpC)
AddNodeIDOpcode - Add the node opcode to the NodeID data.
static ISD::CondCode getSetCCInverseImpl(ISD::CondCode Op, bool isIntegerLike)
static bool doNotCSE(SDNode *N)
doNotCSE - Return true if CSE should not be performed for this node.
static cl::opt< int > MaxLdStGlue("ldstmemcpy-glue-max", cl::desc("Number limit for gluing ld/st of memcpy."), cl::Hidden, cl::init(0))
static void AddNodeIDOperands(FoldingSetNodeID &ID, ArrayRef< SDValue > Ops)
AddNodeIDOperands - Various routines for adding operands to the NodeID data.
static SDValue foldCONCAT_VECTORS(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
Try to simplify vector concatenation to an input value, undef, or build vector.
static MachinePointerInfo InferPointerInfo(const MachinePointerInfo &Info, SelectionDAG &DAG, SDValue Ptr, int64_t Offset=0)
InferPointerInfo - If the specified ptr/offset is a frame index, infer a MachinePointerInfo record fr...
static bool isInTailCallPositionWrapper(const CallInst *CI, const SelectionDAG *SelDAG, bool AllowReturnsFirstArg)
static void AddNodeIDCustom(FoldingSetNodeID &ID, const SDNode *N)
If this is an SDNode with special info, add this info to the NodeID data.
static bool gluePropagatesDivergence(const SDNode *Node)
Return true if a glue output should propagate divergence information.
static void NewSDValueDbgMsg(SDValue V, StringRef Msg, SelectionDAG *G)
static SDVTList makeVTList(const EVT *VTs, unsigned NumVTs)
makeVTList - Return an instance of the SDVTList struct initialized with the specified members.
static void checkForCyclesHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallPtrSetImpl< const SDNode * > &Checked, const llvm::SelectionDAG *DAG)
static void chainLoadsAndStoresForMemcpy(SelectionDAG &DAG, const SDLoc &dl, SmallVector< SDValue, 32 > &OutChains, unsigned From, unsigned To, SmallVector< SDValue, 16 > &OutLoadChains, SmallVector< SDValue, 16 > &OutStoreChains)
static int isSignedOp(ISD::CondCode Opcode)
For an integer comparison, return 1 if the comparison is a signed operation and 2 if the result is an...
static std::optional< APInt > FoldValue(unsigned Opcode, const APInt &C1, const APInt &C2)
static SDValue FoldBUILD_VECTOR(const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SelectionDAG &DAG)
static void checkAddrSpaceIsValidForLibcall(const TargetLowering *TLI, unsigned AS)
static cl::opt< unsigned > MaxSteps("has-predecessor-max-steps", cl::Hidden, cl::init(8192), cl::desc("DAG combiner limit number of steps when searching DAG " "for predecessor nodes"))
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:114
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This file describes how to lower LLVM code to machine code.
static void removeOperands(MachineInstr &MI, unsigned i)
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static const fltSemantics & IEEEsingle()
Definition APFloat.h:296
cmpResult
IEEE-754R 5.11: Floating Point Comparison Relations.
Definition APFloat.h:334
static constexpr roundingMode rmTowardZero
Definition APFloat.h:348
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEquad()
Definition APFloat.h:298
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmTowardNegative
Definition APFloat.h:347
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static constexpr roundingMode rmTowardPositive
Definition APFloat.h:346
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:360
static APFloat getQNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for QNaN values.
Definition APFloat.h:1183
opStatus divide(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1271
void copySign(const APFloat &RHS)
Definition APFloat.h:1365
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:5975
opStatus subtract(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1253
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
Definition APFloat.h:1495
opStatus add(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1244
bool isFinite() const
Definition APFloat.h:1517
opStatus convertFromAPInt(const APInt &Input, bool IsSigned, roundingMode RM)
Definition APFloat.h:1410
opStatus multiply(const APFloat &RHS, roundingMode RM)
Definition APFloat.h:1262
bool isSignaling() const
Definition APFloat.h:1514
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Definition APFloat.h:1298
bool isZero() const
Definition APFloat.h:1508
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1201
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1395
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1161
opStatus mod(const APFloat &RHS)
Definition APFloat.h:1289
bool isPosZero() const
Definition APFloat.h:1523
bool isNegZero() const
Definition APFloat.h:1524
void changeSign()
Definition APFloat.h:1360
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1172
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1982
LLVM_ABI APInt usub_sat(const APInt &RHS) const
Definition APInt.cpp:2066
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1584
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1415
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1023
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1549
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1400
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1679
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1394
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1044
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1521
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1339
APInt abs() const
Get the absolute value.
Definition APInt.h:1804
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2037
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1677
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1497
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1655
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1405
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1165
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:835
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1648
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1637
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1607
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2097
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2111
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1052
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1152
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1444
unsigned logBase2() const
Definition APInt.h:1770
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2047
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1747
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:996
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1376
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static bool isSameValue(const APInt &I1, const APInt &I2)
Determine if two APInts have the same value, after zero-extending one of them (if needed!...
Definition APInt.h:554
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1426
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1397
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2056
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h:904
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
A "pseudo-class" with methods for operating on BUILD_VECTORs.
LLVM_ABI bool getConstantRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &RawBitElements, BitVector &UndefElements) const
Extract the raw bit data from a build vector of Undef, Constant or ConstantFP node elements.
static LLVM_ABI void recastRawBits(bool IsLittleEndian, unsigned DstEltSizeInBits, SmallVectorImpl< APInt > &DstBitElements, ArrayRef< APInt > SrcBitElements, BitVector &DstUndefElements, const BitVector &SrcUndefElements)
Recast bit data SrcBitElements to DstEltSizeInBits wide elements.
LLVM_ABI bool getRepeatedSequence(const APInt &DemandedElts, SmallVectorImpl< SDValue > &Sequence, BitVector *UndefElements=nullptr) const
Find the shortest repeating sequence of values in the build vector.
LLVM_ABI ConstantFPSDNode * getConstantFPSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant FP or null if this is not a constant FP splat.
LLVM_ABI std::optional< std::pair< APInt, APInt > > isConstantSequence() const
If this BuildVector is constant and represents the numerical series "<a, a+n, a+2n,...
LLVM_ABI SDValue getSplatValue(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted value or a null value if this is not a splat.
LLVM_ABI bool isConstantSplat(APInt &SplatValue, APInt &SplatUndef, unsigned &SplatBitSize, bool &HasAnyUndefs, unsigned MinSplatBits=0, bool isBigEndian=false) const
Check if this is a constant splat, and if so, find the smallest element size that splats the vector.
LLVM_ABI ConstantSDNode * getConstantSplatNode(const APInt &DemandedElts, BitVector *UndefElements=nullptr) const
Returns the demanded splatted constant or null if this is not a constant splat.
LLVM_ABI int32_t getConstantFPSplatPow2ToLog2Int(BitVector *UndefElements, uint32_t BitWidth) const
If this is a constant FP splat and the splatted constant FP is an exact power or 2,...
LLVM_ABI bool isConstant() const
This class represents a function call, abstracting a target machine's calling convention.
bool isTailCall() const
static LLVM_ABI bool isValueValidForType(EVT VT, const APFloat &Val)
const APFloat & getValueAPF() const
bool isExactlyValue(double V) const
We don't rely on operator== working on double values, as it returns true for things that are clearly ...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
const APFloat & getValue() const
Definition Constants.h:326
This is the shared class of boolean and integer constants.
Definition Constants.h:87
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
MachineConstantPoolValue * getMachineCPVal() const
const Constant * getConstVal() const
LLVM_ABI Type * getType() const
This class represents a range of values.
LLVM_ABI ConstantRange multiply(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a multiplication of a value in thi...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
uint64_t getZExtValue() const
const APInt & getAPIntValue() const
This is an important base class in LLVM.
Definition Constant.h:43
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
DWARF expression.
static LLVM_ABI ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)
Returns the ops for a zero- or sign-extension in a DIExpression.
static LLVM_ABI void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
static LLVM_ABI DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)
Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...
static LLVM_ABI const DIExpression * convertToVariadicExpression(const DIExpression *Expr)
If Expr is a non-variadic expression (i.e.
static LLVM_ABI std::optional< DIExpression * > createFragmentExpression(const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits)
Create a DIExpression to describe one part of an aggregate variable that is fragmented across multipl...
Base class for variables.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
LLVM_ABI IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
LLVM_ABI Align getABITypeAlign(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
LLVM_ABI Align getPrefTypeAlign(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
A debug info location.
Definition DebugLoc.h:123
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
const char * getSymbol() const
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:209
Data structure describing the variable locations in a function.
bool hasMinSize() const
Optimize this function for minimum size (-Oz).
Definition Function.h:703
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:352
LLVM_ABI unsigned getAddressSpace() const
const GlobalValue * getGlobal() const
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Tracks which library functions to use for a particular subtarget.
LLVM_ABI 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:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
LLVM_ABI MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl, SDVTList VTs, EVT memvt, MachineMemOperand *MMO)
MachineMemOperand * MMO
Memory reference information.
MachineMemOperand * getMemOperand() const
Return a MachineMemOperand object describing the memory reference performed by operation.
const MachinePointerInfo & getPointerInfo() const
unsigned getRawSubclassData() const
Return the SubclassData value, without HasDebugValue.
EVT getMemoryVT() const
Return the type of the in-memory value.
Representation for a specific memory location.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:235
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition ArrayRef.h:298
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
Class to represent pointers.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
bool isNull() const
Test if the pointer held in the union is null, regardless of which type it is.
Analysis providing profile information.
void Deallocate(SubClass *E)
Deallocate - Release storage for the pointed-to object.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Keeps track of dbg_value information through SDISel.
LLVM_ABI void add(SDDbgValue *V, bool isParameter)
LLVM_ABI void erase(const SDNode *Node)
Invalidate all DbgValues attached to the node and remove it from the Node-to-DbgValues map.
Holds the information from a dbg_label node through SDISel.
Holds the information for a single machine location through SDISel; either an SDNode,...
static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo)
static SDDbgOperand fromFrameIdx(unsigned FrameIdx)
static SDDbgOperand fromVReg(Register VReg)
static SDDbgOperand fromConst(const Value *Const)
@ SDNODE
Value is the result of an expression.
Holds the information from a dbg_value node through SDISel.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
const DebugLoc & getDebugLoc() const
unsigned getIROrder() const
This class provides iterator support for SDUse operands that use a specific SDNode.
Represents one node in the SelectionDAG.
ArrayRef< SDUse > ops() const
const APInt & getAsAPIntVal() const
Helper method returns the APInt value of a ConstantSDNode.
LLVM_ABI void dumprFull(const SelectionDAG *G=nullptr) const
printrFull to dbgs().
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool isDivergent() const
LLVM_ABI bool isOnlyUserOf(const SDNode *N) const
Return true if this node is the only use of N.
iterator_range< value_op_iterator > op_values() const
unsigned getIROrder() const
Return the node ordering.
static constexpr size_t getMaxNumOperands()
Return the maximum number of operands that a SDNode can hold.
iterator_range< use_iterator > uses()
MemSDNodeBitfields MemSDNodeBits
LLVM_ABI void Profile(FoldingSetNodeID &ID) const
Gather unique data for the node.
bool getHasDebugValue() const
SDNodeFlags getFlags() const
void setNodeId(int Id)
Set unique node id.
LLVM_ABI void intersectFlagsWith(const SDNodeFlags Flags)
Clear any flags in this node that aren't also set in Flags.
static bool hasPredecessorHelper(const SDNode *N, SmallPtrSetImpl< const SDNode * > &Visited, SmallVectorImpl< const SDNode * > &Worklist, unsigned int MaxSteps=0, bool TopologicalPrune=false)
Returns true if N is a predecessor of any node in Worklist.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
bool use_empty() const
Return true if there are no uses of this node.
unsigned getNumValues() const
Return the number of values defined/returned by this operator.
unsigned getNumOperands() const
Return the number of values used by this operation.
const SDValue & getOperand(unsigned Num) const
static LLVM_ABI bool areOnlyUsersOf(ArrayRef< const SDNode * > Nodes, const SDNode *N)
Return true if all the users of N are contained in Nodes.
use_iterator use_begin() const
Provide iteration support to walk over all uses of an SDNode.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if this node is an operand of N.
const APInt & getConstantOperandAPInt(unsigned Num) const
Helper method returns the APInt of a ConstantSDNode operand.
std::optional< APInt > bitcastToAPInt() const
LLVM_ABI bool hasPredecessor(const SDNode *N) const
Return true if N is a predecessor of this node.
LLVM_ABI bool hasAnyUseOfValue(unsigned Value) const
Return true if there are any use of the indicated value.
EVT getValueType(unsigned ResNo) const
Return the type of a specified result.
bool isUndef() const
Returns true if the node type is UNDEF or POISON.
op_iterator op_end() const
op_iterator op_begin() const
static use_iterator use_end()
LLVM_ABI void DropOperands()
Release the operands and set this node to have zero operands.
SDNode(unsigned Opc, unsigned Order, DebugLoc dl, SDVTList VTs)
Create an SDNode.
Represents a use of a SDNode.
SDNode * getUser()
This returns the SDNode that contains this Use.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
bool isUndef() const
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node.
LLVM_ABI bool isOperandOf(const SDNode *N) const
Return true if the referenced return value is an operand of N.
SDValue()=default
LLVM_ABI bool reachesChainWithoutSideEffects(SDValue Dest, unsigned Depth=2) const
Return true if this operand (which must be a chain) reaches the specified operand without crossing an...
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
TypeSize getValueSizeInBits() const
Returns the size of the value in bits.
const SDValue & getOperand(unsigned i) const
bool use_empty() const
Return true if there are no nodes using value ResNo of Node.
const APInt & getConstantOperandAPInt(unsigned i) const
uint64_t getScalarValueSizeInBits() const
unsigned getResNo() const
get the index which selects a specific result in the SDNode
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
virtual void verifyTargetNode(const SelectionDAG &DAG, const SDNode *N) const
Checks that the given target-specific node is valid. Aborts if it is not.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI SDValue getElementCount(const SDLoc &DL, EVT VT, ElementCount EC)
LLVM_ABI Align getReducedAlign(EVT VT, bool UseABI)
In most cases this function returns the ABI alignment for a given type, except for illegal vector typ...
LLVM_ABI SDValue getVPZeroExtendInReg(SDValue Op, SDValue Mask, SDValue EVL, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI SDValue getShiftAmountOperand(EVT LHSTy, SDValue Op)
Return the specified value casted to the target's desired shift amount type.
LLVM_ABI SDValue getExtLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getExtLoadVP(ISD::LoadExtType ExtType, const SDLoc &dl, EVT VT, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, MaybeAlign Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsExpanding=false)
SDValue getExtractVectorElt(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Extract element at Idx from Vec.
LLVM_ABI SDValue getSplatSourceVector(SDValue V, int &SplatIndex)
If V is a splatted value, return the source vector and its splat index.
LLVM_ABI SDValue getLabelNode(unsigned Opcode, const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI OverflowKind computeOverflowForUnsignedSub(SDValue N0, SDValue N1) const
Determine if the result of the unsigned sub of 2 nodes can overflow.
LLVM_ABI unsigned ComputeMaxSignificantBits(SDValue Op, unsigned Depth=0) const
Get the upper bound on bit size for this Value Op as a signed integer.
const SDValue & getRoot() const
Return the root tag of the SelectionDAG.
LLVM_ABI std::pair< SDValue, SDValue > getStrlen(SDValue Chain, const SDLoc &dl, SDValue Src, const CallInst *CI)
Lower a strlen operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getMaskedGather(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, ISD::LoadExtType ExtTy)
LLVM_ABI SDValue getAddrSpaceCast(const SDLoc &dl, EVT VT, SDValue Ptr, unsigned SrcAS, unsigned DestAS)
Return an AddrSpaceCastSDNode.
bool isKnownNeverSNaN(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
LLVM_ABI std::optional< bool > isBoolConstant(SDValue N) const
Check if a value \op N is a constant using the target's BooleanContent for its type.
LLVM_ABI SDValue getStackArgumentTokenFactor(SDValue Chain)
Compute a TokenFactor to force all the incoming stack arguments to be loaded from the stack.
const TargetSubtargetInfo & getSubtarget() const
LLVM_ABI SDValue getMergeValues(ArrayRef< SDValue > Ops, const SDLoc &dl)
Create a MERGE_VALUES node from the given operands.
LLVM_ABI SDVTList getVTList(EVT VT)
Return an SDVTList that represents the list of values specified.
LLVM_ABI SDValue getShiftAmountConstant(uint64_t Val, EVT VT, const SDLoc &DL)
LLVM_ABI void updateDivergence(SDNode *N)
LLVM_ABI SDValue getSplatValue(SDValue V, bool LegalTypes=false)
If V is a splat vector, return its scalar source operand by extracting that element from the source v...
LLVM_ABI SDValue FoldSetCC(EVT VT, SDValue N1, SDValue N2, ISD::CondCode Cond, const SDLoc &dl)
Constant fold a setcc to true or false.
LLVM_ABI SDValue getAllOnesConstant(const SDLoc &DL, EVT VT, bool IsTarget=false, bool IsOpaque=false)
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI void ExtractVectorElements(SDValue Op, SmallVectorImpl< SDValue > &Args, unsigned Start=0, unsigned Count=0, EVT EltVT=EVT())
Append the extracted elements from Start to Count out of the vector Op in Args.
LLVM_ABI SDValue getNeutralElement(unsigned Opcode, const SDLoc &DL, EVT VT, SDNodeFlags Flags)
Get the (commutative) neutral element for the given opcode, if it exists.
LLVM_ABI SDValue getAtomicMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Value, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo)
LLVM_ABI SDValue getAtomicLoad(ISD::LoadExtType ExtType, const SDLoc &dl, EVT MemVT, EVT VT, SDValue Chain, SDValue Ptr, MachineMemOperand *MMO)
LLVM_ABI SDNode * getNodeIfExists(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops, const SDNodeFlags Flags, bool AllowCommute=false)
Get the specified node if it's already available, or else return NULL.
LLVM_ABI SDValue getPseudoProbeNode(const SDLoc &Dl, SDValue Chain, uint64_t Guid, uint64_t Index, uint32_t Attr)
Creates a PseudoProbeSDNode with function GUID Guid and the index of the block Index it is probing,...
LLVM_ABI SDValue getFreeze(SDValue V)
Return a freeze using the SDLoc of the value operand.
LLVM_ABI SDNode * SelectNodeTo(SDNode *N, unsigned MachineOpc, EVT VT)
These are used for target selectors to mutate the specified node to have the specified return type,...
LLVM_ABI void init(MachineFunction &NewMF, OptimizationRemarkEmitter &NewORE, Pass *PassPtr, const TargetLibraryInfo *LibraryInfo, const LibcallLoweringInfo *LibcallsInfo, UniformityInfo *UA, ProfileSummaryInfo *PSIin, BlockFrequencyInfo *BFIin, MachineModuleInfo &MMI, FunctionVarLocs const *FnVarLocs)
Prepare this SelectionDAG to process code in the given MachineFunction.
LLVM_ABI SelectionDAG(const TargetMachine &TM, CodeGenOptLevel)
LLVM_ABI SDValue getMemset(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, MachinePointerInfo DstPtrInfo, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getBitcastedSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getConstantPool(const Constant *C, EVT VT, MaybeAlign Align=std::nullopt, int Offs=0, bool isT=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getStridedLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getAtomicCmpSwap(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDVTList VTs, SDValue Chain, SDValue Ptr, SDValue Cmp, SDValue Swp, MachineMemOperand *MMO)
Gets a node for an atomic cmpxchg op.
LLVM_ABI SDValue makeEquivalentMemoryOrdering(SDValue OldChain, SDValue NewMemOpChain)
If an existing load has uses of its chain, create a token factor node with that chain and the new mem...
LLVM_ABI bool isConstantIntBuildVectorOrConstantInt(SDValue N, bool AllowOpaques=true) const
Test whether the given value is a constant int or similar node.
LLVM_ABI void ReplaceAllUsesOfValuesWith(const SDValue *From, const SDValue *To, unsigned Num)
Like ReplaceAllUsesOfValueWith, but for multiple values at once.
LLVM_ABI SDValue getJumpTableDebugInfo(int JTI, SDValue Chain, const SDLoc &DL)
SDValue getSetCC(const SDLoc &DL, EVT VT, SDValue LHS, SDValue RHS, ISD::CondCode Cond, SDValue Chain=SDValue(), bool IsSignaling=false)
Helper function to make it easier to build SetCC's if you just have an ISD::CondCode instead of an SD...
LLVM_ABI SDValue getSymbolFunctionGlobalAddress(SDValue Op, Function **TargetFunction=nullptr)
Return a GlobalAddress of the function from the current module with name matching the given ExternalS...
LLVM_ABI std::optional< unsigned > getValidMaximumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue UnrollVectorOp(SDNode *N, unsigned ResNE=0)
Utility function used by legalize and lowering to "unroll" a vector operation by splitting out the sc...
LLVM_ABI SDValue getVScale(const SDLoc &DL, EVT VT, APInt MulImm)
Return a node that represents the runtime scaling 'MulImm * RuntimeVL'.
LLVM_ABI SDValue getConstantFP(double Val, const SDLoc &DL, EVT VT, bool isTarget=false)
Create a ConstantFPSDNode wrapping a constant value.
OverflowKind
Used to represent the possible overflow behavior of an operation.
static LLVM_ABI unsigned getHasPredecessorMaxSteps()
LLVM_ABI bool haveNoCommonBitsSet(SDValue A, SDValue B) const
Return true if A and B have no common bits set.
SDValue getExtractSubvector(const SDLoc &DL, EVT VT, SDValue Vec, unsigned Idx)
Return the VT typed sub-vector of Vec at Idx.
LLVM_ABI bool cannotBeOrderedNegativeFP(SDValue Op) const
Test whether the given float value is known to be positive.
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
LLVM_ABI bool calculateDivergence(SDNode *N)
LLVM_ABI SDValue getGetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getAssertAlign(const SDLoc &DL, SDValue V, Align A)
Return an AssertAlignSDNode.
LLVM_ABI SDNode * mutateStrictFPToFP(SDNode *Node)
Mutate the specified strict FP node to its non-strict equivalent, unlinking the node from its chain a...
LLVM_ABI SDValue getLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, MachinePointerInfo PtrInfo, MaybeAlign Alignment=MaybeAlign(), MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr)
Loads are not normal binary operators: their result type is not determined by their operands,...
LLVM_ABI bool canIgnoreSignBitOfZero(const SDUse &Use) const
Check if a use of a float value is insensitive to signed zeros.
LLVM_ABI bool SignBitIsZeroFP(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero, for a floating-point value.
LLVM_ABI SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef< SDValue > Ops, EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags Flags=MachineMemOperand::MOLoad|MachineMemOperand::MOStore, LocationSize Size=LocationSize::precise(0), const AAMDNodes &AAInfo=AAMDNodes())
Creates a MemIntrinsicNode that may produce a result and takes a list of operands.
SDValue getInsertSubvector(const SDLoc &DL, SDValue Vec, SDValue SubVec, unsigned Idx)
Insert SubVec at the Idx element of Vec.
LLVM_ABI SDValue getBitcastedZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI SDValue getStepVector(const SDLoc &DL, EVT ResVT, const APInt &StepVal)
Returns a vector of type ResVT whose elements contain the linear sequence <0, Step,...
LLVM_ABI SDValue getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT, SDValue Chain, SDValue Ptr, SDValue Val, MachineMemOperand *MMO)
Gets a node for an atomic op, produces result (if relevant) and chain and takes 2 operands.
LLVM_ABI SDValue getMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI Align getEVTAlign(EVT MemoryVT) const
Compute the default alignment value for the given type.
LLVM_ABI bool shouldOptForSize() const
LLVM_ABI SDValue getNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a bitwise NOT operation as (XOR Val, -1).
LLVM_ABI SDValue getVPZExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be an integer vector, to the vector-type VT,...
const TargetLowering & getTargetLoweringInfo() const
LLVM_ABI bool isEqualTo(SDValue A, SDValue B) const
Test whether two SDValues are known to compare equal.
static constexpr unsigned MaxRecursionDepth
LLVM_ABI SDValue getStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
bool isGuaranteedNotToBePoison(SDValue Op, unsigned Depth=0) const
Return true if this function can prove that Op is never poison.
LLVM_ABI SDValue expandVACopy(SDNode *Node)
Expand the specified ISD::VACOPY node as the Legalize pass would.
LLVM_ABI SDValue getIndexedMaskedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI void dump(bool Sorted=false) const
Dump the textual format of this DAG.
LLVM_ABI APInt computeVectorKnownZeroElements(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
For each demanded element of a vector, see if it is known to be zero.
LLVM_ABI void AddDbgValue(SDDbgValue *DB, bool isParameter)
Add a dbg_value SDNode.
bool NewNodesMustHaveLegalTypes
When true, additional steps are taken to ensure that getConstant() and similar functions return DAG n...
LLVM_ABI std::pair< EVT, EVT > GetSplitDestVTs(const EVT &VT) const
Compute the VTs needed for the low/hi parts of a type which is split (or expanded) into two not neces...
LLVM_ABI void salvageDebugInfo(SDNode &N)
To be invoked on an SDNode that is slated to be erased.
LLVM_ABI SDNode * MorphNodeTo(SDNode *N, unsigned Opc, SDVTList VTs, ArrayRef< SDValue > Ops)
This mutates the specified node to have the specified return type, opcode, and operands.
LLVM_ABI std::pair< SDValue, SDValue > UnrollVectorOverflowOp(SDNode *N, unsigned ResNE=0)
Like UnrollVectorOp(), but for the [US](ADD|SUB|MUL)O family of opcodes.
allnodes_const_iterator allnodes_begin() const
SDValue getUNDEF(EVT VT)
Return an UNDEF node. UNDEF does not have a useful SDLoc.
LLVM_ABI SDValue getGatherVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
SDValue getBuildVector(EVT VT, const SDLoc &DL, ArrayRef< SDValue > Ops)
Return an ISD::BUILD_VECTOR node.
LLVM_ABI SDValue getBitcastedAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by first bitcasting (from potentia...
LLVM_ABI bool isSplatValue(SDValue V, const APInt &DemandedElts, APInt &UndefElts, unsigned Depth=0) const
Test whether V has a splatted value for all the demanded elements.
LLVM_ABI void DeleteNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI SDValue getBitcast(EVT VT, SDValue V)
Return a bitcast using the SDLoc of the value operand, and casting to the provided type.
LLVM_ABI SDDbgValue * getDbgValueList(DIVariable *Var, DIExpression *Expr, ArrayRef< SDDbgOperand > Locs, ArrayRef< SDNode * > Dependencies, bool IsIndirect, const DebugLoc &DL, unsigned O, bool IsVariadic)
Creates a SDDbgValue node from a list of locations.
LLVM_ABI std::pair< SDValue, SDValue > getStrcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, const CallInst *CI)
Lower a strcpy operation into a target library call and return the resulting chain and call result as...
SDValue getSelect(const SDLoc &DL, EVT VT, SDValue Cond, SDValue LHS, SDValue RHS, SDNodeFlags Flags=SDNodeFlags())
Helper function to make it easier to build Select's if you just have operands and don't want to check...
LLVM_ABI SDValue getNegative(SDValue Val, const SDLoc &DL, EVT VT)
Create negative operation as (SUB 0, Val).
LLVM_ABI std::optional< unsigned > getValidShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has a uniform shift amount that is less than the element bit-width of the shi...
LLVM_ABI void setNodeMemRefs(MachineSDNode *N, ArrayRef< MachineMemOperand * > NewMemRefs)
Mutate the specified machine node's memory references to the provided list.
LLVM_ABI SDValue simplifySelect(SDValue Cond, SDValue TVal, SDValue FVal)
Try to simplify a select/vselect into 1 of its operands or a constant.
LLVM_ABI SDValue getZeroExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to zero extend the Op value assuming it was the smaller SrcTy value.
LLVM_ABI bool isConstantFPBuildVectorOrConstantFP(SDValue N) const
Test whether the given value is a constant FP or similar node.
const DataLayout & getDataLayout() const
LLVM_ABI SDValue expandVAArg(SDNode *Node)
Expand the specified ISD::VAARG node as the Legalize pass would.
LLVM_ABI SDValue getTokenFactor(const SDLoc &DL, SmallVectorImpl< SDValue > &Vals)
Creates a new TokenFactor containing Vals.
LLVM_ABI bool doesNodeExist(unsigned Opcode, SDVTList VTList, ArrayRef< SDValue > Ops)
Check if a node exists without modifying its flags.
const SelectionDAGTargetInfo & getSelectionDAGInfo() const
LLVM_ABI bool areNonVolatileConsecutiveLoads(LoadSDNode *LD, LoadSDNode *Base, unsigned Bytes, int Dist) const
Return true if loads are next to each other and can be merged.
LLVM_ABI SDValue getMaskedHistogram(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDDbgLabel * getDbgLabel(DILabel *Label, const DebugLoc &DL, unsigned O)
Creates a SDDbgLabel node.
LLVM_ABI SDValue getStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI OverflowKind computeOverflowForUnsignedMul(SDValue N0, SDValue N1) const
Determine if the result of the unsigned mul of 2 nodes can overflow.
LLVM_ABI void copyExtraInfo(SDNode *From, SDNode *To)
Copy extra info associated with one node to another.
LLVM_ABI SDValue getConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
Create a ConstantSDNode wrapping a constant value.
LLVM_ABI SDValue getMemBasePlusOffset(SDValue Base, TypeSize Offset, const SDLoc &DL, const SDNodeFlags Flags=SDNodeFlags())
Returns sum of the base pointer and offset.
LLVM_ABI SDValue getGlobalAddress(const GlobalValue *GV, const SDLoc &DL, EVT VT, int64_t offset=0, bool isTargetGA=false, unsigned TargetFlags=0)
LLVM_ABI SDValue getVAArg(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue SV, unsigned Align)
VAArg produces a result and token chain, and takes a pointer and a source value as input.
LLVM_ABI SDValue getTruncStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
LLVM_ABI SDValue getLoadFFVP(EVT VT, const SDLoc &DL, SDValue Chain, SDValue Ptr, SDValue Mask, SDValue EVL, MachineMemOperand *MMO)
LLVM_ABI SDValue getTypeSize(const SDLoc &DL, EVT VT, TypeSize TS)
LLVM_ABI SDValue getMDNode(const MDNode *MD)
Return an MDNodeSDNode which holds an MDNode.
LLVM_ABI void clear()
Clear state and free memory necessary to make this SelectionDAG ready to process a new block.
LLVM_ABI std::pair< SDValue, SDValue > getMemcmp(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, const CallInst *CI)
Lower a memcmp operation into a target library call and return the resulting chain and call result as...
LLVM_ABI void ReplaceAllUsesWith(SDValue From, SDValue To)
Modify anything using 'From' to use 'To' instead.
LLVM_ABI SDValue getCommutedVectorShuffle(const ShuffleVectorSDNode &SV)
Returns an ISD::VECTOR_SHUFFLE node semantically equivalent to the shuffle node in input but with swa...
LLVM_ABI std::pair< SDValue, SDValue > SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the vector with EXTRACT_SUBVECTOR using the provided VTs and return the low/high part.
LLVM_ABI SDValue makeStateFunctionCall(unsigned LibFunc, SDValue Ptr, SDValue InChain, const SDLoc &DLoc)
Helper used to make a call to a library function that has one argument of pointer type.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(SDValue Op, bool PoisonOnly=false, unsigned Depth=0) const
Return true if this function can prove that Op is never poison and, if PoisonOnly is false,...
LLVM_ABI SDValue getStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, MachinePointerInfo PtrInfo, Align Alignment, MachineMemOperand::Flags MMOFlags=MachineMemOperand::MONone, const AAMDNodes &AAInfo=AAMDNodes())
Helper function to build ISD::STORE nodes.
LLVM_ABI SDValue getSignedConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isTarget=false, bool isOpaque=false)
LLVM_ABI SDValue getIndexedLoadVP(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getSrcValue(const Value *v)
Construct a node to track a Value* through the backend.
SDValue getSplatVector(EVT VT, const SDLoc &DL, SDValue Op)
LLVM_ABI SDValue getAtomicMemcpy(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI OverflowKind computeOverflowForSignedMul(SDValue N0, SDValue N1) const
Determine if the result of the signed mul of 2 nodes can overflow.
LLVM_ABI MaybeAlign InferPtrAlign(SDValue Ptr) const
Infer alignment of a load / store address.
LLVM_ABI bool MaskedValueIsAllOnes(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if '(Op & Mask) == Mask'.
LLVM_ABI bool SignBitIsZero(SDValue Op, unsigned Depth=0) const
Return true if the sign bit of Op is known to be zero.
LLVM_ABI void RemoveDeadNodes()
This method deletes all unreachable nodes in the SelectionDAG.
LLVM_ABI void RemoveDeadNode(SDNode *N)
Remove the specified node from the system.
LLVM_ABI void AddDbgLabel(SDDbgLabel *DB)
Add a dbg_label SDNode.
bool isConstantValueOfAnyType(SDValue N) const
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
LLVM_ABI SDValue getBasicBlock(MachineBasicBlock *MBB)
LLVM_ABI SDValue getSExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either sign-extending or trunca...
LLVM_ABI SDDbgValue * getVRegDbgValue(DIVariable *Var, DIExpression *Expr, Register VReg, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a VReg SDDbgValue node.
LLVM_ABI bool isKnownToBeAPowerOfTwo(SDValue Val, unsigned Depth=0) const
Test if the given value is known to have exactly one bit set.
LLVM_ABI SDValue getEHLabel(const SDLoc &dl, SDValue Root, MCSymbol *Label)
LLVM_ABI SDValue getIndexedStoreVP(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI bool isKnownNeverZero(SDValue Op, unsigned Depth=0) const
Test whether the given SDValue is known to contain non-zero value(s).
LLVM_ABI SDValue getIndexedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops, SDNodeFlags Flags=SDNodeFlags())
LLVM_ABI std::optional< unsigned > getValidMinimumShiftAmount(SDValue V, const APInt &DemandedElts, unsigned Depth=0) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue getSetFPEnv(SDValue Chain, const SDLoc &dl, SDValue Ptr, EVT MemVT, MachineMemOperand *MMO)
LLVM_ABI SDValue getBoolExtOrTrunc(SDValue Op, const SDLoc &SL, EVT VT, EVT OpVT)
Convert Op, which must be of integer type, to the integer type VT, by using an extension appropriate ...
LLVM_ABI SDValue getMaskedStore(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Base, SDValue Offset, SDValue Mask, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, bool IsTruncating=false, bool IsCompressing=false)
LLVM_ABI SDValue getExternalSymbol(const char *Sym, EVT VT)
const TargetMachine & getTarget() const
LLVM_ABI std::pair< SDValue, SDValue > getStrictFPExtendOrRound(SDValue Op, SDValue Chain, const SDLoc &DL, EVT VT)
Convert Op, which must be a STRICT operation of float type, to the float type VT, by either extending...
LLVM_ABI std::pair< SDValue, SDValue > SplitEVL(SDValue N, EVT VecVT, const SDLoc &DL)
Split the explicit vector length parameter of a VP operation.
LLVM_ABI SDValue getPtrExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either truncating it or perform...
LLVM_ABI SDValue getVPLogicalNOT(const SDLoc &DL, SDValue Val, SDValue Mask, SDValue EVL, EVT VT)
Create a vector-predicated logical NOT operation as (VP_XOR Val, BooleanOne, Mask,...
LLVM_ABI SDValue getMaskFromElementCount(const SDLoc &DL, EVT VT, ElementCount Len)
Return a vector with the first 'Len' lanes set to true and remaining lanes set to false.
LLVM_ABI SDValue getAnyExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either any-extending or truncat...
iterator_range< allnodes_iterator > allnodes()
LLVM_ABI SDValue getBlockAddress(const BlockAddress *BA, EVT VT, int64_t Offset=0, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI SDValue WidenVector(const SDValue &N, const SDLoc &DL)
Widen the vector up to the next power of two using INSERT_SUBVECTOR.
LLVM_ABI bool isKnownNeverZeroFloat(SDValue Op) const
Test whether the given floating point SDValue is known to never be positive or negative zero.
const LibcallLoweringInfo & getLibcalls() const
LLVM_ABI SDValue getLoadVP(ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl, SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, const MDNode *Ranges=nullptr, bool IsExpanding=false)
LLVM_ABI SDValue getIntPtrConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI SDDbgValue * getConstantDbgValue(DIVariable *Var, DIExpression *Expr, const Value *C, const DebugLoc &DL, unsigned O)
Creates a constant SDDbgValue node.
LLVM_ABI SDValue getScatterVP(SDVTList VTs, EVT VT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType)
LLVM_ABI SDValue getValueType(EVT)
LLVM_ABI SDValue getLifetimeNode(bool IsStart, const SDLoc &dl, SDValue Chain, int FrameIndex)
Creates a LifetimeSDNode that starts (IsStart==true) or ends (IsStart==false) the lifetime of the Fra...
ArrayRef< SDDbgValue * > GetDbgValues(const SDNode *SD) const
Get the debug values which reference the given SDNode.
LLVM_ABI SDValue getNode(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDUse > Ops)
Gets or creates the specified node.
LLVM_ABI OverflowKind computeOverflowForSignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the signed addition of 2 nodes can overflow.
LLVM_ABI SDValue getFPExtendOrRound(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of float type, to the float type VT, by either extending or rounding (by tr...
LLVM_ABI unsigned AssignTopologicalOrder()
Topological-sort the AllNodes list and a assign a unique node id for each node in the DAG based on th...
ilist< SDNode >::size_type allnodes_size() const
LLVM_ABI bool isKnownNeverNaN(SDValue Op, const APInt &DemandedElts, bool SNaN=false, unsigned Depth=0) const
Test whether the given SDValue (or all elements of it, if it is a vector) is known to never be NaN in...
LLVM_ABI SDValue FoldConstantBuildVector(BuildVectorSDNode *BV, const SDLoc &DL, EVT DstEltVT)
Fold BUILD_VECTOR of constants/undefs to the destination type BUILD_VECTOR of constants/undefs elemen...
LLVM_ABI SDValue getAtomicMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Type *SizeTy, unsigned ElemSz, bool isTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo)
LLVM_ABI SDValue getIndexedMaskedStore(SDValue OrigStore, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTruncStoreVP(SDValue Chain, const SDLoc &dl, SDValue Val, SDValue Ptr, SDValue Mask, SDValue EVL, MachinePointerInfo PtrInfo, EVT SVT, Align Alignment, MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo, bool IsCompressing=false)
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI unsigned ComputeNumSignBits(SDValue Op, unsigned Depth=0) const
Return the number of times the sign bit of the register is replicated into the other bits.
LLVM_ABI bool MaskedVectorIsZero(SDValue Op, const APInt &DemandedElts, unsigned Depth=0) const
Return true if 'Op' is known to be zero in DemandedElts.
LLVM_ABI SDValue getBoolConstant(bool V, const SDLoc &DL, EVT VT, EVT OpVT)
Create a true or false constant of type VT using the target's BooleanContent for type OpVT.
LLVM_ABI SDDbgValue * getFrameIndexDbgValue(DIVariable *Var, DIExpression *Expr, unsigned FI, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a FrameIndex SDDbgValue node.
LLVM_ABI SDValue getExtStridedLoadVP(ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding=false)
LLVM_ABI SDValue getMemmove(SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size, Align Alignment, bool isVol, const CallInst *CI, std::optional< bool > OverrideTailCall, MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo=AAMDNodes(), BatchAAResults *BatchAA=nullptr)
LLVM_ABI SDValue getJumpTable(int JTI, EVT VT, bool isTarget=false, unsigned TargetFlags=0)
LLVM_ABI bool isBaseWithConstantOffset(SDValue Op) const
Return true if the specified operand is an ISD::ADD with a ConstantSDNode on the right-hand side,...
LLVM_ABI SDValue getVPPtrExtOrTrunc(const SDLoc &DL, EVT VT, SDValue Op, SDValue Mask, SDValue EVL)
Convert a vector-predicated Op, which must be of integer type, to the vector-type integer type VT,...
LLVM_ABI SDValue getVectorIdxConstant(uint64_t Val, const SDLoc &DL, bool isTarget=false)
LLVM_ABI void getTopologicallyOrderedNodes(SmallVectorImpl< const SDNode * > &SortedNodes) const
Get all the nodes in their topological order without modifying any states.
LLVM_ABI void ReplaceAllUsesOfValueWith(SDValue From, SDValue To)
Replace any uses of From with To, leaving uses of other values produced by From.getNode() alone.
MachineFunction & getMachineFunction() const
LLVM_ABI std::pair< SDValue, SDValue > getStrstr(SDValue Chain, const SDLoc &dl, SDValue S0, SDValue S1, const CallInst *CI)
Lower a strstr operation into a target library call and return the resulting chain and call result as...
LLVM_ABI SDValue getPtrExtendInReg(SDValue Op, const SDLoc &DL, EVT VT)
Return the expression required to extend the Op as a pointer value assuming it was the smaller SrcTy ...
LLVM_ABI bool canCreateUndefOrPoison(SDValue Op, const APInt &DemandedElts, bool PoisonOnly=false, bool ConsiderFlags=true, unsigned Depth=0) const
Return true if Op can create undef or poison from non-undef & non-poison operands.
LLVM_ABI OverflowKind computeOverflowForUnsignedAdd(SDValue N0, SDValue N1) const
Determine if the result of the unsigned addition of 2 nodes can overflow.
SDValue getPOISON(EVT VT)
Return a POISON node. POISON does not have a useful SDLoc.
SDValue getSplatBuildVector(EVT VT, const SDLoc &DL, SDValue Op)
Return a splat ISD::BUILD_VECTOR node, consisting of Op splatted to all elements.
LLVM_ABI SDValue getFrameIndex(int FI, EVT VT, bool isTarget=false)
LLVM_ABI SDValue getTruncStridedStoreVP(SDValue Chain, const SDLoc &DL, SDValue Val, SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT SVT, MachineMemOperand *MMO, bool IsCompressing=false)
LLVM_ABI void canonicalizeCommutativeBinop(unsigned Opcode, SDValue &N1, SDValue &N2) const
Swap N1 and N2 if Opcode is a commutative binary opcode and the canonical form expects the opposite o...
LLVM_ABI KnownBits computeKnownBits(SDValue Op, unsigned Depth=0) const
Determine which bits of Op are known to be either zero or one and return them in Known.
LLVM_ABI SDValue getRegisterMask(const uint32_t *RegMask)
LLVM_ABI SDValue getZExtOrTrunc(SDValue Op, const SDLoc &DL, EVT VT)
Convert Op, which must be of integer type, to the integer type VT, by either zero-extending or trunca...
LLVM_ABI SDValue getCondCode(ISD::CondCode Cond)
LLVM_ABI bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
LLVM_ABI bool isKnownToBeAPowerOfTwoFP(SDValue Val, unsigned Depth=0) const
Test if the given fp value is known to be an integer power-of-2, either positive or negative.
LLVM_ABI OverflowKind computeOverflowForSignedSub(SDValue N0, SDValue N1) const
Determine if the result of the signed sub of 2 nodes can overflow.
SDValue getObjectPtrOffset(const SDLoc &SL, SDValue Ptr, TypeSize Offset)
Create an add instruction with appropriate flags when used for addressing some offset of an object.
LLVMContext * getContext() const
LLVM_ABI SDValue simplifyFPBinop(unsigned Opcode, SDValue X, SDValue Y, SDNodeFlags Flags)
Try to simplify a floating-point binary operation into 1 of its operands or a constant.
const SDValue & setRoot(SDValue N)
Set the current root tag of the SelectionDAG.
LLVM_ABI SDValue getDeactivationSymbol(const GlobalValue *GV)
LLVM_ABI SDValue getTargetExternalSymbol(const char *Sym, EVT VT, unsigned TargetFlags=0)
LLVM_ABI SDValue getMCSymbol(MCSymbol *Sym, EVT VT)
LLVM_ABI bool isUndef(unsigned Opcode, ArrayRef< SDValue > Ops)
Return true if the result of this operation is always undefined.
LLVM_ABI SDValue CreateStackTemporary(TypeSize Bytes, Align Alignment)
Create a stack temporary based on the size in bytes and the alignment.
LLVM_ABI SDNode * UpdateNodeOperands(SDNode *N, SDValue Op)
Mutate the specified node in-place to have the specified operands.
LLVM_ABI std::pair< EVT, EVT > GetDependentSplitDestVTs(const EVT &VT, const EVT &EnvVT, bool *HiIsEmpty) const
Compute the VTs needed for the low/hi parts of a type, dependent on an enveloping VT that has been sp...
LLVM_ABI SDValue foldConstantFPMath(unsigned Opcode, const SDLoc &DL, EVT VT, ArrayRef< SDValue > Ops)
Fold floating-point operations when all operands are constants and/or undefined.
LLVM_ABI std::optional< ConstantRange > getValidShiftAmountRange(SDValue V, const APInt &DemandedElts, unsigned Depth) const
If a SHL/SRA/SRL node V has shift amounts that are all less than the element bit-width of the shift n...
LLVM_ABI SDValue FoldSymbolOffset(unsigned Opcode, EVT VT, const GlobalAddressSDNode *GA, const SDNode *N2)
LLVM_ABI SDValue getIndexedLoad(SDValue OrigLoad, const SDLoc &dl, SDValue Base, SDValue Offset, ISD::MemIndexedMode AM)
LLVM_ABI SDValue getTargetInsertSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand, SDValue Subreg)
A convenience function for creating TargetInstrInfo::INSERT_SUBREG nodes.
SDValue getEntryNode() const
Return the token chain corresponding to the entry of the function.
LLVM_ABI SDDbgValue * getDbgValue(DIVariable *Var, DIExpression *Expr, SDNode *N, unsigned R, bool IsIndirect, const DebugLoc &DL, unsigned O)
Creates a SDDbgValue node.
LLVM_ABI SDValue getMaskedLoad(EVT VT, const SDLoc &dl, SDValue Chain, SDValue Base, SDValue Offset, SDValue Mask, SDValue Src0, EVT MemVT, MachineMemOperand *MMO, ISD::MemIndexedMode AM, ISD::LoadExtType, bool IsExpanding=false)
SDValue getSplat(EVT VT, const SDLoc &DL, SDValue Op)
Returns a node representing a splat of one value into all lanes of the provided vector type.
LLVM_ABI std::pair< SDValue, SDValue > SplitScalar(const SDValue &N, const SDLoc &DL, const EVT &LoVT, const EVT &HiVT)
Split the scalar node with EXTRACT_ELEMENT using the provided VTs and return the low/high part.
LLVM_ABI SDValue matchBinOpReduction(SDNode *Extract, ISD::NodeType &BinOp, ArrayRef< ISD::NodeType > CandidateBinOps, bool AllowPartials=false)
Match a binop + shuffle pyramid that represents a horizontal reduction over the elements of a vector ...
LLVM_ABI bool isADDLike(SDValue Op, bool NoWrap=false) const
Return true if the specified operand is an ISD::OR or ISD::XOR node that can be treated as an ISD::AD...
LLVM_ABI SDValue getVectorShuffle(EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, ArrayRef< int > Mask)
Return an ISD::VECTOR_SHUFFLE node.
LLVM_ABI SDValue simplifyShift(SDValue X, SDValue Y)
Try to simplify a shift into 1 of its operands or a constant.
LLVM_ABI void transferDbgValues(SDValue From, SDValue To, unsigned OffsetInBits=0, unsigned SizeInBits=0, bool InvalidateDbg=true)
Transfer debug values from one node to another, while optionally generating fragment expressions for ...
LLVM_ABI SDValue getLogicalNOT(const SDLoc &DL, SDValue Val, EVT VT)
Create a logical NOT operation as (XOR Val, BooleanOne).
LLVM_ABI SDValue getMaskedScatter(SDVTList VTs, EVT MemVT, const SDLoc &dl, ArrayRef< SDValue > Ops, MachineMemOperand *MMO, ISD::MemIndexType IndexType, bool IsTruncating=false)
ilist< SDNode >::iterator allnodes_iterator
This SDNode is used to implement the code generator support for the llvm IR shufflevector instruction...
int getMaskElt(unsigned Idx) const
ArrayRef< int > getMask() const
static void commuteMask(MutableArrayRef< int > Mask)
Change values in a shuffle permute mask assuming the two vector operands have swapped position.
static LLVM_ABI bool isSplatMask(ArrayRef< int > Mask)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class is used to represent ISD::STORE nodes.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
Information about stack frame layout on the target.
virtual TargetStackID::Value getStackIDForScalableVectors() const
Returns the StackID that scalable vectors should be associated with.
Align getStackAlign() const
getStackAlignment - This method returns the number of bytes to which the stack pointer must be aligne...
Completely target-dependent object reference.
unsigned getTargetFlags() const
Provides information about what library functions are available for the current target.
virtual bool shouldConvertConstantLoadToIntImm(const APInt &Imm, Type *Ty) const
Return true if it is beneficial to convert a load of a constant to just the constant itself.
const TargetMachine & getTargetMachine() const
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
unsigned getMaxStoresPerMemcpy(bool OptSize) const
Get maximum # of store operations permitted for llvm.memcpy.
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) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h:632
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:343
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
LLVM_ABI void set(Value *Val)
Definition Value.h:905
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:207
This class is used to represent an VP_GATHER node.
This class is used to represent a VP_LOAD node.
This class is used to represent an VP_SCATTER node.
This class is used to represent a VP_STORE node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_LOAD node.
This class is used to represent an EXPERIMENTAL_VP_STRIDED_STORE node.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:269
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:230
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr bool isKnownEven() const
A return value of true indicates we know at compile time that the number of elements (vscale * Min) i...
Definition TypeSize.h:176
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:252
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:237
A raw_ostream that writes to an std::string.
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt clmulr(const APInt &LHS, const APInt &RHS)
Perform a reversed carry-less multiply.
Definition APInt.cpp:3212
LLVM_ABI APInt mulhu(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on zero-extended operands.
Definition APInt.cpp:3142
LLVM_ABI APInt avgCeilU(const APInt &C1, const APInt &C2)
Compute the ceil of the unsigned average of C1 and C2.
Definition APInt.cpp:3129
LLVM_ABI APInt avgFloorU(const APInt &C1, const APInt &C2)
Compute the floor of the unsigned average of C1 and C2.
Definition APInt.cpp:3119
LLVM_ABI APInt fshr(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift right.
Definition APInt.cpp:3193
LLVM_ABI APInt mulhs(const APInt &C1, const APInt &C2)
Performs (2*N)-bit multiplication on sign-extended operands.
Definition APInt.cpp:3134
LLVM_ABI APInt clmul(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, also known as XOR multiplication, and return low-bits.
Definition APInt.cpp:3202
APInt abds(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be signed.
Definition APInt.h:2277
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3184
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3020
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3217
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2282
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3114
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3124
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:818
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:787
@ TargetConstantPool
Definition ISDOpcodes.h:189
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ PTRADD
PTRADD represents pointer arithmetic semantics, for targets that opt in using shouldPreservePtrArith(...
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition ISDOpcodes.h:538
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:778
@ TargetBlockAddress
Definition ISDOpcodes.h:191
@ DEACTIVATION_SYMBOL
Untyped node storing deactivation symbol reference (DeactivationSymbolSDNode).
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:294
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:522
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:852
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:879
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:746
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:909
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:992
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:773
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:843
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:714
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:664
@ TargetExternalSymbol
Definition ISDOpcodes.h:190
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:786
@ TargetJumpTable
Definition ISDOpcodes.h:188
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:198
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ TRUNCATE_SSAT_U
Definition ISDOpcodes.h:872
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:826
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:690
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:795
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:671
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:791
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:703
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:764
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:649
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:849
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:810
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ TargetFrameIndex
Definition ISDOpcodes.h:187
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by IMM elements and retu...
Definition ISDOpcodes.h:653
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:898
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:887
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:726
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:977
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:804
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:925
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:505
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:738
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ GET_FPENV_MEM
Gets the current floating-point environment.
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:734
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:709
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, IMM) - Shifts CONCAT_VECTORS(VEC1, VEC2) right by IMM elements and re...
Definition ISDOpcodes.h:656
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:680
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:958
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:698
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:920
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:996
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:944
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:855
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ VECREDUCE_SEQ_FMUL
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:832
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ TRUNCATE_SSAT_S
TRUNCATE_[SU]SAT_[SU] - Truncate for saturated operand [SU] located in middle, prefix for SAT means i...
Definition ISDOpcodes.h:870
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:721
@ TRUNCATE_USAT_U
Definition ISDOpcodes.h:874
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:338
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
LLVM_ABI NodeType getOppositeSignednessMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns the corresponding opcode with the opposi...
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h:362
ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred)
getICmpCondCode - Return the ISD condition code corresponding to the given LLVM IR integer condition ...
Definition Analysis.cpp:237
void fill(R &&Range, T &&Value)
Provide wrappers to std::fill which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1757
LLVM_ABI SDValue peekThroughExtractSubvectors(SDValue V)
Return the non-extracted vector source operand of V if it exists.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
Definition Utils.cpp:1613
LLVM_ABI SDValue getBitwiseNotOperand(SDValue V, SDValue Mask, bool AllowUndefs)
If V is a bitwise not, returns the inverted operand.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2544
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:293
bool isIntOrFPConstant(SDValue V)
Return true if V is either a integer or FP constant.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
LLVM_ABI bool isOneOrOneSplatFP(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant floating-point value, or a splatted vector of a constant float...
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1706
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:243
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
Definition Utils.cpp:1595
LLVM_ABI bool isMinSignedConstant(SDValue V)
Returns true if V is a constant min signed integer value.
LLVM_ABI ConstantFPSDNode * isConstOrConstSplatFP(SDValue N, bool AllowUndefs=false)
Returns the SDNode if it is a constant splat BuildVector or constant float.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
APFloat frexp(const APFloat &X, int &Exp, APFloat::roundingMode RM)
Equivalent of C standard library function.
Definition APFloat.h:1618
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1661
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs=false)
Returns true if V is a bitwise not operation.
LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V, const APInt &DemandedElts)
Recursively peek through INSERT_VECTOR_ELT nodes, returning the source vector operand of V,...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
LLVM_ABI void checkForCycles(const SelectionDAG *DAG, bool force=false)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1634
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1692
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI SDValue peekThroughTruncates(SDValue V)
Return the non-truncated source operand of V if it exists.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1751
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI SDValue peekThroughOneUseBitcasts(SDValue V)
Return the non-bitcasted and one-use source operand of V if it exists.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI bool isOneOrOneSplat(SDValue V, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1642
@ Mul
Product of integers.
@ Sub
Subtraction of integers.
LLVM_ABI bool isNullConstantOrUndef(SDValue V)
Returns true if V is a constant integer zero or an UNDEF node.
bool isInTailCallPosition(const CallBase &Call, const TargetMachine &TM, bool ReturnsFirstArg=false)
Test if the given instruction is in a position to be optimized with a tail-call.
Definition Analysis.cpp:539
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI ConstantSDNode * isConstOrConstSplat(SDValue N, bool AllowUndefs=false, bool AllowTruncation=false)
Returns the SDNode if it is a constant splat BuildVector or constant int.
OutputIt copy(R &&Range, OutputIt Out)
Definition STLExtras.h:1883
constexpr unsigned BitWidth
bool funcReturnsFirstArgOfCall(const CallInst &CI)
Returns true if the parent of CI returns CI's first argument after calling CI.
Definition Analysis.cpp:719
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isZeroOrZeroSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1945
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:201
LLVM_ABI bool isNullFPConstant(SDValue V)
Returns true if V is an FP constant with a value of positive zero.
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant (+/-)0.0 floating-point value or a splatted vector thereof (wi...
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1679
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1719
LLVM_ABI bool isOnesOrOnesSplat(SDValue N, bool AllowUndefs=false)
Return true if the value is a constant 1 integer or a splatted vector of a constant 1 integer (with n...
LLVM_ABI bool isNeutralConstant(unsigned Opc, SDNodeFlags Flags, SDValue V, unsigned OperandNo)
Returns true if V is a neutral element of Opc with Flags.
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
constexpr uint64_t NextPowerOf2(uint64_t A)
Returns the next power of two (in 64-bits) that is strictly greater than A.
Definition MathExtras.h:373
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
#define N
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:761
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:781
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:778
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:395
bool isSimple() const
Test if the given EVT is simple (as opposed to being extended).
Definition ValueTypes.h:137
intptr_t getRawBits() const
Definition ValueTypes.h:512
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:74
EVT changeTypeToInteger() const
Return the type converted to an equivalently sized integer or vector with integer element type.
Definition ValueTypes.h:121
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
Definition ValueTypes.h:284
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
Definition ValueTypes.h:300
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
Definition ValueTypes.h:147
ElementCount getVectorElementCount() const
Definition ValueTypes.h:350
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:373
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
Definition ValueTypes.h:359
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:385
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:316
static EVT getIntegerVT(LLVMContext &Context, unsigned BitWidth)
Returns the EVT that represents an integer with the given number of bits.
Definition ValueTypes.h:65
bool isFixedLengthVector() const
Definition ValueTypes.h:181
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:168
EVT getScalarType() const
If this is a vector type, return the element type, otherwise return this.
Definition ValueTypes.h:323
bool bitsGE(EVT VT) const
Return true if this has no less bits than VT.
Definition ValueTypes.h:292
bool bitsEq(EVT VT) const
Return true if this has the same number of bits as VT.
Definition ValueTypes.h:256
LLVM_ABI Type * getTypeForEVT(LLVMContext &Context) const
This method returns an LLVM type corresponding to the specified EVT.
bool isScalableVector() const
Return true if this is a vector type where the runtime length is machine dependent.
Definition ValueTypes.h:174
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:328
bool isExtended() const
Test if the given EVT is extended (as opposed to being simple).
Definition ValueTypes.h:142
LLVM_ABI const fltSemantics & getFltSemantics() const
Returns an APFloat semantics tag appropriate for the value type.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:336
bool bitsLE(EVT VT) const
Return true if this has no more bits than VT.
Definition ValueTypes.h:308
EVT getHalfNumVectorElementsVT(LLVMContext &Context) const
Definition ValueTypes.h:453
bool isInteger() const
Return true if this is an integer or a vector integer type.
Definition ValueTypes.h:152
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:314
LLVM_ABI KnownBits sextInReg(unsigned SrcBitWidth) const
Return known bits for a in-register sign extension of the value we're tracking.
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:268
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
bool isZero() const
Returns true if value is all zero.
Definition KnownBits.h:80
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:127
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:287
static LLVM_ABI std::optional< bool > ne(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_NE result.
void makeNegative()
Make this value negative.
Definition KnownBits.h:122
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:164
KnownBits byteSwap() const
Definition KnownBits.h:532
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:302
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:536
KnownBits concat(const KnownBits &Lo) const
Concatenate the bits from Lo onto the bottom of *this.
Definition KnownBits.h:246
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:175
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:334
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
static LLVM_ABI KnownBits abdu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for abdu(LHS, RHS).
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:238
static LLVM_ABI KnownBits avgFloorU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorU.
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:324
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:183
static LLVM_ABI KnownBits computeForSubBorrow(const KnownBits &LHS, KnownBits RHS, const KnownBits &Borrow)
Compute known bits results from subtracting RHS from LHS with 1-bit Borrow.
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:199
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:148
static LLVM_ABI KnownBits abds(KnownBits LHS, KnownBits RHS)
Compute known bits for abds(LHS, RHS).
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:339
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
LLVM_ABI KnownBits truncSSat(unsigned BitWidth) const
Truncate with signed saturation (signed input -> signed output)
static LLVM_ABI KnownBits computeForAddCarry(const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry)
Compute known bits resulting from adding LHS, RHS and a 1-bit Carry.
Definition KnownBits.cpp:53
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:293
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:232
static LLVM_ABI KnownBits avgCeilU(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilU.
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:170
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const
Truncate with unsigned saturation (unsigned input -> unsigned output)
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const
Truncate with signed saturation to unsigned (signed input -> unsigned output)
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)