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().isScalableVector() || NumElts == 1) &&
3337 "DemandedElts for scalable vectors must be 1 to represent all lanes");
3338 assert((!Op.getValueType().isFixedLengthVector() ||
3339 NumElts == Op.getValueType().getVectorNumElements()) &&
3340 "Unexpected vector size");
3341
3342 if (!DemandedElts)
3343 return Known; // No demanded elts, better to assume we don't know anything.
3344
3345 unsigned Opcode = Op.getOpcode();
3346 switch (Opcode) {
3347 case ISD::MERGE_VALUES:
3348 return computeKnownBits(Op.getOperand(Op.getResNo()), DemandedElts,
3349 Depth + 1);
3350 case ISD::SPLAT_VECTOR: {
3351 SDValue SrcOp = Op.getOperand(0);
3352 assert(SrcOp.getValueSizeInBits() >= BitWidth &&
3353 "Expected SPLAT_VECTOR implicit truncation");
3354 // Implicitly truncate the bits to match the official semantics of
3355 // SPLAT_VECTOR.
3356 Known = computeKnownBits(SrcOp, Depth + 1).trunc(BitWidth);
3357 break;
3358 }
3360 unsigned ScalarSize = Op.getOperand(0).getScalarValueSizeInBits();
3361 assert(ScalarSize * Op.getNumOperands() == BitWidth &&
3362 "Expected SPLAT_VECTOR_PARTS scalars to cover element width");
3363 for (auto [I, SrcOp] : enumerate(Op->ops())) {
3364 Known.insertBits(computeKnownBits(SrcOp, Depth + 1), ScalarSize * I);
3365 }
3366 break;
3367 }
3368 case ISD::STEP_VECTOR: {
3369 const APInt &Step = Op.getConstantOperandAPInt(0);
3370
3371 if (Step.isPowerOf2())
3372 Known.Zero.setLowBits(Step.logBase2());
3373
3375
3376 if (!isUIntN(BitWidth, Op.getValueType().getVectorMinNumElements()))
3377 break;
3378 const APInt MinNumElts =
3379 APInt(BitWidth, Op.getValueType().getVectorMinNumElements());
3380
3381 bool Overflow;
3382 const APInt MaxNumElts = getVScaleRange(&F, BitWidth)
3384 .umul_ov(MinNumElts, Overflow);
3385 if (Overflow)
3386 break;
3387
3388 const APInt MaxValue = (MaxNumElts - 1).umul_ov(Step, Overflow);
3389 if (Overflow)
3390 break;
3391
3392 Known.Zero.setHighBits(MaxValue.countl_zero());
3393 break;
3394 }
3395 case ISD::BUILD_VECTOR:
3396 assert(!Op.getValueType().isScalableVector());
3397 // Collect the known bits that are shared by every demanded vector element.
3398 Known.setAllConflict();
3399 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) {
3400 if (!DemandedElts[i])
3401 continue;
3402
3403 SDValue SrcOp = Op.getOperand(i);
3404 Known2 = computeKnownBits(SrcOp, Depth + 1);
3405
3406 // BUILD_VECTOR can implicitly truncate sources, we must handle this.
3407 if (SrcOp.getValueSizeInBits() != BitWidth) {
3408 assert(SrcOp.getValueSizeInBits() > BitWidth &&
3409 "Expected BUILD_VECTOR implicit truncation");
3410 Known2 = Known2.trunc(BitWidth);
3411 }
3412
3413 // Known bits are the values that are shared by every demanded element.
3414 Known = Known.intersectWith(Known2);
3415
3416 // If we don't know any bits, early out.
3417 if (Known.isUnknown())
3418 break;
3419 }
3420 break;
3421 case ISD::VECTOR_COMPRESS: {
3422 SDValue Vec = Op.getOperand(0);
3423 SDValue PassThru = Op.getOperand(2);
3424 Known = computeKnownBits(PassThru, DemandedElts, Depth + 1);
3425 // If we don't know any bits, early out.
3426 if (Known.isUnknown())
3427 break;
3428 Known2 = computeKnownBits(Vec, Depth + 1);
3429 Known = Known.intersectWith(Known2);
3430 break;
3431 }
3432 case ISD::VECTOR_SHUFFLE: {
3433 assert(!Op.getValueType().isScalableVector());
3434 // Collect the known bits that are shared by every vector element referenced
3435 // by the shuffle.
3436 APInt DemandedLHS, DemandedRHS;
3438 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
3439 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
3440 DemandedLHS, DemandedRHS))
3441 break;
3442
3443 // Known bits are the values that are shared by every demanded element.
3444 Known.setAllConflict();
3445 if (!!DemandedLHS) {
3446 SDValue LHS = Op.getOperand(0);
3447 Known2 = computeKnownBits(LHS, DemandedLHS, Depth + 1);
3448 Known = Known.intersectWith(Known2);
3449 }
3450 // If we don't know any bits, early out.
3451 if (Known.isUnknown())
3452 break;
3453 if (!!DemandedRHS) {
3454 SDValue RHS = Op.getOperand(1);
3455 Known2 = computeKnownBits(RHS, DemandedRHS, Depth + 1);
3456 Known = Known.intersectWith(Known2);
3457 }
3458 break;
3459 }
3460 case ISD::VSCALE: {
3462 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
3463 Known = getVScaleRange(&F, BitWidth).multiply(Multiplier).toKnownBits();
3464 break;
3465 }
3466 case ISD::CONCAT_VECTORS: {
3467 if (Op.getValueType().isScalableVector())
3468 break;
3469 // Split DemandedElts and test each of the demanded subvectors.
3470 Known.setAllConflict();
3471 EVT SubVectorVT = Op.getOperand(0).getValueType();
3472 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
3473 unsigned NumSubVectors = Op.getNumOperands();
3474 for (unsigned i = 0; i != NumSubVectors; ++i) {
3475 APInt DemandedSub =
3476 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
3477 if (!!DemandedSub) {
3478 SDValue Sub = Op.getOperand(i);
3479 Known2 = computeKnownBits(Sub, DemandedSub, Depth + 1);
3480 Known = Known.intersectWith(Known2);
3481 }
3482 // If we don't know any bits, early out.
3483 if (Known.isUnknown())
3484 break;
3485 }
3486 break;
3487 }
3488 case ISD::INSERT_SUBVECTOR: {
3489 if (Op.getValueType().isScalableVector())
3490 break;
3491 // Demand any elements from the subvector and the remainder from the src its
3492 // inserted into.
3493 SDValue Src = Op.getOperand(0);
3494 SDValue Sub = Op.getOperand(1);
3495 uint64_t Idx = Op.getConstantOperandVal(2);
3496 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
3497 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
3498 APInt DemandedSrcElts = DemandedElts;
3499 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
3500
3501 Known.setAllConflict();
3502 if (!!DemandedSubElts) {
3503 Known = computeKnownBits(Sub, DemandedSubElts, Depth + 1);
3504 if (Known.isUnknown())
3505 break; // early-out.
3506 }
3507 if (!!DemandedSrcElts) {
3508 Known2 = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3509 Known = Known.intersectWith(Known2);
3510 }
3511 break;
3512 }
3514 // Offset the demanded elts by the subvector index.
3515 SDValue Src = Op.getOperand(0);
3516
3517 APInt DemandedSrcElts;
3518 if (Src.getValueType().isScalableVector())
3519 DemandedSrcElts = APInt(1, 1); // <=> 'demand all elements'
3520 else {
3521 uint64_t Idx = Op.getConstantOperandVal(1);
3522 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
3523 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
3524 }
3525 Known = computeKnownBits(Src, DemandedSrcElts, Depth + 1);
3526 break;
3527 }
3528 case ISD::SCALAR_TO_VECTOR: {
3529 if (Op.getValueType().isScalableVector())
3530 break;
3531 // We know about scalar_to_vector as much as we know about it source,
3532 // which becomes the first element of otherwise unknown vector.
3533 if (DemandedElts != 1)
3534 break;
3535
3536 SDValue N0 = Op.getOperand(0);
3537 Known = computeKnownBits(N0, Depth + 1);
3538 if (N0.getValueSizeInBits() != BitWidth)
3539 Known = Known.trunc(BitWidth);
3540
3541 break;
3542 }
3543 case ISD::BITCAST: {
3544 if (Op.getValueType().isScalableVector())
3545 break;
3546
3547 SDValue N0 = Op.getOperand(0);
3548 EVT SubVT = N0.getValueType();
3549 unsigned SubBitWidth = SubVT.getScalarSizeInBits();
3550
3551 // Ignore bitcasts from unsupported types.
3552 if (!(SubVT.isInteger() || SubVT.isFloatingPoint()))
3553 break;
3554
3555 // Fast handling of 'identity' bitcasts.
3556 if (BitWidth == SubBitWidth) {
3557 Known = computeKnownBits(N0, DemandedElts, Depth + 1);
3558 break;
3559 }
3560
3561 bool IsLE = getDataLayout().isLittleEndian();
3562
3563 // Bitcast 'small element' vector to 'large element' scalar/vector.
3564 if ((BitWidth % SubBitWidth) == 0) {
3565 assert(N0.getValueType().isVector() && "Expected bitcast from vector");
3566
3567 // Collect known bits for the (larger) output by collecting the known
3568 // bits from each set of sub elements and shift these into place.
3569 // We need to separately call computeKnownBits for each set of
3570 // sub elements as the knownbits for each is likely to be different.
3571 unsigned SubScale = BitWidth / SubBitWidth;
3572 APInt SubDemandedElts(NumElts * SubScale, 0);
3573 for (unsigned i = 0; i != NumElts; ++i)
3574 if (DemandedElts[i])
3575 SubDemandedElts.setBit(i * SubScale);
3576
3577 for (unsigned i = 0; i != SubScale; ++i) {
3578 Known2 = computeKnownBits(N0, SubDemandedElts.shl(i),
3579 Depth + 1);
3580 unsigned Shifts = IsLE ? i : SubScale - 1 - i;
3581 Known.insertBits(Known2, SubBitWidth * Shifts);
3582 }
3583 }
3584
3585 // Bitcast 'large element' scalar/vector to 'small element' vector.
3586 if ((SubBitWidth % BitWidth) == 0) {
3587 assert(Op.getValueType().isVector() && "Expected bitcast to vector");
3588
3589 // Collect known bits for the (smaller) output by collecting the known
3590 // bits from the overlapping larger input elements and extracting the
3591 // sub sections we actually care about.
3592 unsigned SubScale = SubBitWidth / BitWidth;
3593 APInt SubDemandedElts =
3594 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
3595 Known2 = computeKnownBits(N0, SubDemandedElts, Depth + 1);
3596
3597 Known.setAllConflict();
3598 for (unsigned i = 0; i != NumElts; ++i)
3599 if (DemandedElts[i]) {
3600 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
3601 unsigned Offset = (Shifts % SubScale) * BitWidth;
3602 Known = Known.intersectWith(Known2.extractBits(BitWidth, Offset));
3603 // If we don't know any bits, early out.
3604 if (Known.isUnknown())
3605 break;
3606 }
3607 }
3608 break;
3609 }
3610 case ISD::AND:
3611 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3612 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3613
3614 Known &= Known2;
3615 break;
3616 case ISD::OR:
3617 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3618 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3619
3620 Known |= Known2;
3621 break;
3622 case ISD::XOR:
3623 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3624 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3625
3626 Known ^= Known2;
3627 break;
3628 case ISD::MUL: {
3629 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3630 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3631 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3632 // TODO: SelfMultiply can be poison, but not undef.
3633 if (SelfMultiply)
3634 SelfMultiply &= isGuaranteedNotToBeUndefOrPoison(
3635 Op.getOperand(0), DemandedElts, false, Depth + 1);
3636 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3637
3638 // If the multiplication is known not to overflow, the product of a number
3639 // with itself is non-negative. Only do this if we didn't already computed
3640 // the opposite value for the sign bit.
3641 if (Op->getFlags().hasNoSignedWrap() &&
3642 Op.getOperand(0) == Op.getOperand(1) &&
3643 !Known.isNegative())
3644 Known.makeNonNegative();
3645 break;
3646 }
3647 case ISD::MULHU: {
3648 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3649 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3650 Known = KnownBits::mulhu(Known, Known2);
3651 break;
3652 }
3653 case ISD::MULHS: {
3654 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3655 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3656 Known = KnownBits::mulhs(Known, Known2);
3657 break;
3658 }
3659 case ISD::ABDU: {
3660 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3661 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3662 Known = KnownBits::abdu(Known, Known2);
3663 break;
3664 }
3665 case ISD::ABDS: {
3666 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3667 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3668 Known = KnownBits::abds(Known, Known2);
3669 unsigned SignBits1 =
3670 ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
3671 if (SignBits1 == 1)
3672 break;
3673 unsigned SignBits0 =
3674 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
3675 Known.Zero.setHighBits(std::min(SignBits0, SignBits1) - 1);
3676 break;
3677 }
3678 case ISD::UMUL_LOHI: {
3679 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3680 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3681 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3682 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3683 if (Op.getResNo() == 0)
3684 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3685 else
3686 Known = KnownBits::mulhu(Known, Known2);
3687 break;
3688 }
3689 case ISD::SMUL_LOHI: {
3690 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3691 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3692 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3693 bool SelfMultiply = Op.getOperand(0) == Op.getOperand(1);
3694 if (Op.getResNo() == 0)
3695 Known = KnownBits::mul(Known, Known2, SelfMultiply);
3696 else
3697 Known = KnownBits::mulhs(Known, Known2);
3698 break;
3699 }
3700 case ISD::AVGFLOORU: {
3701 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3702 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3703 Known = KnownBits::avgFloorU(Known, Known2);
3704 break;
3705 }
3706 case ISD::AVGCEILU: {
3707 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3708 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3709 Known = KnownBits::avgCeilU(Known, Known2);
3710 break;
3711 }
3712 case ISD::AVGFLOORS: {
3713 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3714 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3715 Known = KnownBits::avgFloorS(Known, Known2);
3716 break;
3717 }
3718 case ISD::AVGCEILS: {
3719 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3720 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3721 Known = KnownBits::avgCeilS(Known, Known2);
3722 break;
3723 }
3724 case ISD::SELECT:
3725 case ISD::VSELECT:
3726 Known = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3727 // If we don't know any bits, early out.
3728 if (Known.isUnknown())
3729 break;
3730 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth+1);
3731
3732 // Only known if known in both the LHS and RHS.
3733 Known = Known.intersectWith(Known2);
3734 break;
3735 case ISD::SELECT_CC:
3736 Known = computeKnownBits(Op.getOperand(3), DemandedElts, Depth+1);
3737 // If we don't know any bits, early out.
3738 if (Known.isUnknown())
3739 break;
3740 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth+1);
3741
3742 // Only known if known in both the LHS and RHS.
3743 Known = Known.intersectWith(Known2);
3744 break;
3745 case ISD::SMULO:
3746 case ISD::UMULO:
3747 if (Op.getResNo() != 1)
3748 break;
3749 // The boolean result conforms to getBooleanContents.
3750 // If we know the result of a setcc has the top bits zero, use this info.
3751 // We know that we have an integer-based boolean since these operations
3752 // are only available for integer.
3753 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
3755 BitWidth > 1)
3756 Known.Zero.setBitsFrom(1);
3757 break;
3758 case ISD::SETCC:
3759 case ISD::SETCCCARRY:
3760 case ISD::STRICT_FSETCC:
3761 case ISD::STRICT_FSETCCS: {
3762 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
3763 // If we know the result of a setcc has the top bits zero, use this info.
3764 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
3766 BitWidth > 1)
3767 Known.Zero.setBitsFrom(1);
3768 break;
3769 }
3770 case ISD::SHL: {
3771 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3772 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3773
3774 bool NUW = Op->getFlags().hasNoUnsignedWrap();
3775 bool NSW = Op->getFlags().hasNoSignedWrap();
3776
3777 bool ShAmtNonZero = Known2.isNonZero();
3778
3779 Known = KnownBits::shl(Known, Known2, NUW, NSW, ShAmtNonZero);
3780
3781 // Minimum shift low bits are known zero.
3782 if (std::optional<unsigned> ShMinAmt =
3783 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3784 Known.Zero.setLowBits(*ShMinAmt);
3785 break;
3786 }
3787 case ISD::SRL:
3788 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3789 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3790 Known = KnownBits::lshr(Known, Known2, /*ShAmtNonZero=*/false,
3791 Op->getFlags().hasExact());
3792
3793 // Minimum shift high bits are known zero.
3794 if (std::optional<unsigned> ShMinAmt =
3795 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
3796 Known.Zero.setHighBits(*ShMinAmt);
3797 break;
3798 case ISD::SRA:
3799 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3800 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3801 Known = KnownBits::ashr(Known, Known2, /*ShAmtNonZero=*/false,
3802 Op->getFlags().hasExact());
3803 break;
3804 case ISD::ROTL:
3805 case ISD::ROTR:
3806 if (ConstantSDNode *C =
3807 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
3808 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3809
3810 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3811
3812 // Canonicalize to ROTR.
3813 if (Opcode == ISD::ROTL && Amt != 0)
3814 Amt = BitWidth - Amt;
3815
3816 Known.Zero = Known.Zero.rotr(Amt);
3817 Known.One = Known.One.rotr(Amt);
3818 }
3819 break;
3820 case ISD::FSHL:
3821 case ISD::FSHR:
3822 if (ConstantSDNode *C = isConstOrConstSplat(Op.getOperand(2), DemandedElts)) {
3823 unsigned Amt = C->getAPIntValue().urem(BitWidth);
3824
3825 // For fshl, 0-shift returns the 1st arg.
3826 // For fshr, 0-shift returns the 2nd arg.
3827 if (Amt == 0) {
3828 Known = computeKnownBits(Op.getOperand(Opcode == ISD::FSHL ? 0 : 1),
3829 DemandedElts, Depth + 1);
3830 break;
3831 }
3832
3833 // fshl: (X << (Z % BW)) | (Y >> (BW - (Z % BW)))
3834 // fshr: (X << (BW - (Z % BW))) | (Y >> (Z % BW))
3835 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3836 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3837 if (Opcode == ISD::FSHL) {
3838 Known <<= Amt;
3839 Known2 >>= BitWidth - Amt;
3840 } else {
3841 Known <<= BitWidth - Amt;
3842 Known2 >>= Amt;
3843 }
3844 Known = Known.unionWith(Known2);
3845 }
3846 break;
3847 case ISD::SHL_PARTS:
3848 case ISD::SRA_PARTS:
3849 case ISD::SRL_PARTS: {
3850 assert((Op.getResNo() == 0 || Op.getResNo() == 1) && "Unknown result");
3851
3852 // Collect lo/hi source values and concatenate.
3853 unsigned LoBits = Op.getOperand(0).getScalarValueSizeInBits();
3854 unsigned HiBits = Op.getOperand(1).getScalarValueSizeInBits();
3855 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3856 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3857 Known = Known2.concat(Known);
3858
3859 // Collect shift amount.
3860 Known2 = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
3861
3862 if (Opcode == ISD::SHL_PARTS)
3863 Known = KnownBits::shl(Known, Known2);
3864 else if (Opcode == ISD::SRA_PARTS)
3865 Known = KnownBits::ashr(Known, Known2);
3866 else // if (Opcode == ISD::SRL_PARTS)
3867 Known = KnownBits::lshr(Known, Known2);
3868
3869 // TODO: Minimum shift low/high bits are known zero.
3870
3871 if (Op.getResNo() == 0)
3872 Known = Known.extractBits(LoBits, 0);
3873 else
3874 Known = Known.extractBits(HiBits, LoBits);
3875 break;
3876 }
3878 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3879 EVT EVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
3880 Known = Known.sextInReg(EVT.getScalarSizeInBits());
3881 break;
3882 }
3883 case ISD::CTTZ:
3884 case ISD::CTTZ_ZERO_UNDEF: {
3885 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3886 // If we have a known 1, its position is our upper bound.
3887 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
3888 unsigned LowBits = llvm::bit_width(PossibleTZ);
3889 Known.Zero.setBitsFrom(LowBits);
3890 break;
3891 }
3892 case ISD::CTLZ:
3893 case ISD::CTLZ_ZERO_UNDEF: {
3894 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3895 // If we have a known 1, its position is our upper bound.
3896 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
3897 unsigned LowBits = llvm::bit_width(PossibleLZ);
3898 Known.Zero.setBitsFrom(LowBits);
3899 break;
3900 }
3901 case ISD::CTLS: {
3902 unsigned MinRedundantSignBits =
3903 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1;
3904 ConstantRange Range(APInt(BitWidth, MinRedundantSignBits),
3906 Known = Range.toKnownBits();
3907 break;
3908 }
3909 case ISD::CTPOP: {
3910 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3911 // If we know some of the bits are zero, they can't be one.
3912 unsigned PossibleOnes = Known2.countMaxPopulation();
3913 Known.Zero.setBitsFrom(llvm::bit_width(PossibleOnes));
3914 break;
3915 }
3916 case ISD::PARITY: {
3917 // Parity returns 0 everywhere but the LSB.
3918 Known.Zero.setBitsFrom(1);
3919 break;
3920 }
3921 case ISD::CLMUL: {
3922 Known = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
3923 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
3924 Known = KnownBits::clmul(Known, Known2);
3925 break;
3926 }
3927 case ISD::MGATHER:
3928 case ISD::MLOAD: {
3929 ISD::LoadExtType ETy =
3930 (Opcode == ISD::MGATHER)
3931 ? cast<MaskedGatherSDNode>(Op)->getExtensionType()
3932 : cast<MaskedLoadSDNode>(Op)->getExtensionType();
3933 if (ETy == ISD::ZEXTLOAD) {
3934 EVT MemVT = cast<MemSDNode>(Op)->getMemoryVT();
3935 KnownBits Known0(MemVT.getScalarSizeInBits());
3936 return Known0.zext(BitWidth);
3937 }
3938 break;
3939 }
3940 case ISD::LOAD: {
3942 const Constant *Cst = TLI->getTargetConstantFromLoad(LD);
3943 if (ISD::isNON_EXTLoad(LD) && Cst) {
3944 // Determine any common known bits from the loaded constant pool value.
3945 Type *CstTy = Cst->getType();
3946 if ((NumElts * BitWidth) == CstTy->getPrimitiveSizeInBits() &&
3947 !Op.getValueType().isScalableVector()) {
3948 // If its a vector splat, then we can (quickly) reuse the scalar path.
3949 // NOTE: We assume all elements match and none are UNDEF.
3950 if (CstTy->isVectorTy()) {
3951 if (const Constant *Splat = Cst->getSplatValue()) {
3952 Cst = Splat;
3953 CstTy = Cst->getType();
3954 }
3955 }
3956 // TODO - do we need to handle different bitwidths?
3957 if (CstTy->isVectorTy() && BitWidth == CstTy->getScalarSizeInBits()) {
3958 // Iterate across all vector elements finding common known bits.
3959 Known.setAllConflict();
3960 for (unsigned i = 0; i != NumElts; ++i) {
3961 if (!DemandedElts[i])
3962 continue;
3963 if (Constant *Elt = Cst->getAggregateElement(i)) {
3964 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
3965 const APInt &Value = CInt->getValue();
3966 Known.One &= Value;
3967 Known.Zero &= ~Value;
3968 continue;
3969 }
3970 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
3971 APInt Value = CFP->getValueAPF().bitcastToAPInt();
3972 Known.One &= Value;
3973 Known.Zero &= ~Value;
3974 continue;
3975 }
3976 }
3977 Known.One.clearAllBits();
3978 Known.Zero.clearAllBits();
3979 break;
3980 }
3981 } else if (BitWidth == CstTy->getPrimitiveSizeInBits()) {
3982 if (auto *CInt = dyn_cast<ConstantInt>(Cst)) {
3983 Known = KnownBits::makeConstant(CInt->getValue());
3984 } else if (auto *CFP = dyn_cast<ConstantFP>(Cst)) {
3985 Known =
3986 KnownBits::makeConstant(CFP->getValueAPF().bitcastToAPInt());
3987 }
3988 }
3989 }
3990 } else if (Op.getResNo() == 0) {
3991 unsigned ScalarMemorySize = LD->getMemoryVT().getScalarSizeInBits();
3992 KnownBits KnownScalarMemory(ScalarMemorySize);
3993 if (const MDNode *MD = LD->getRanges())
3994 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
3995
3996 // Extend the Known bits from memory to the size of the scalar result.
3997 if (ISD::isZEXTLoad(Op.getNode()))
3998 Known = KnownScalarMemory.zext(BitWidth);
3999 else if (ISD::isSEXTLoad(Op.getNode()))
4000 Known = KnownScalarMemory.sext(BitWidth);
4001 else if (ISD::isEXTLoad(Op.getNode()))
4002 Known = KnownScalarMemory.anyext(BitWidth);
4003 else
4004 Known = KnownScalarMemory;
4005 assert(Known.getBitWidth() == BitWidth);
4006 return Known;
4007 }
4008 break;
4009 }
4011 if (Op.getValueType().isScalableVector())
4012 break;
4013 EVT InVT = Op.getOperand(0).getValueType();
4014 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4015 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4016 Known = Known.zext(BitWidth);
4017 break;
4018 }
4019 case ISD::ZERO_EXTEND: {
4020 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4021 Known = Known.zext(BitWidth);
4022 break;
4023 }
4025 if (Op.getValueType().isScalableVector())
4026 break;
4027 EVT InVT = Op.getOperand(0).getValueType();
4028 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4029 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4030 // If the sign bit is known to be zero or one, then sext will extend
4031 // it to the top bits, else it will just zext.
4032 Known = Known.sext(BitWidth);
4033 break;
4034 }
4035 case ISD::SIGN_EXTEND: {
4036 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4037 // If the sign bit is known to be zero or one, then sext will extend
4038 // it to the top bits, else it will just zext.
4039 Known = Known.sext(BitWidth);
4040 break;
4041 }
4043 if (Op.getValueType().isScalableVector())
4044 break;
4045 EVT InVT = Op.getOperand(0).getValueType();
4046 APInt InDemandedElts = DemandedElts.zext(InVT.getVectorNumElements());
4047 Known = computeKnownBits(Op.getOperand(0), InDemandedElts, Depth + 1);
4048 Known = Known.anyext(BitWidth);
4049 break;
4050 }
4051 case ISD::ANY_EXTEND: {
4052 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4053 Known = Known.anyext(BitWidth);
4054 break;
4055 }
4056 case ISD::TRUNCATE: {
4057 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4058 Known = Known.trunc(BitWidth);
4059 break;
4060 }
4061 case ISD::TRUNCATE_SSAT_S: {
4062 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4063 Known = Known.truncSSat(BitWidth);
4064 break;
4065 }
4066 case ISD::TRUNCATE_SSAT_U: {
4067 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4068 Known = Known.truncSSatU(BitWidth);
4069 break;
4070 }
4071 case ISD::TRUNCATE_USAT_U: {
4072 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4073 Known = Known.truncUSat(BitWidth);
4074 break;
4075 }
4076 case ISD::AssertZext: {
4077 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4079 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4080 Known.Zero |= (~InMask);
4081 Known.One &= (~Known.Zero);
4082 break;
4083 }
4084 case ISD::AssertAlign: {
4085 unsigned LogOfAlign = Log2(cast<AssertAlignSDNode>(Op)->getAlign());
4086 assert(LogOfAlign != 0);
4087
4088 // TODO: Should use maximum with source
4089 // If a node is guaranteed to be aligned, set low zero bits accordingly as
4090 // well as clearing one bits.
4091 Known.Zero.setLowBits(LogOfAlign);
4092 Known.One.clearLowBits(LogOfAlign);
4093 break;
4094 }
4095 case ISD::AssertNoFPClass: {
4096 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4097
4098 FPClassTest NoFPClass =
4099 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
4100 const FPClassTest NegativeTestMask = fcNan | fcNegative;
4101 if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
4102 // Cannot be negative.
4103 Known.makeNonNegative();
4104 }
4105
4106 const FPClassTest PositiveTestMask = fcNan | fcPositive;
4107 if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
4108 // Cannot be positive.
4109 Known.makeNegative();
4110 }
4111
4112 break;
4113 }
4114 case ISD::FGETSIGN:
4115 // All bits are zero except the low bit.
4116 Known.Zero.setBitsFrom(1);
4117 break;
4118 case ISD::ADD:
4119 case ISD::SUB: {
4120 SDNodeFlags Flags = Op.getNode()->getFlags();
4121 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4122 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4124 Op.getOpcode() == ISD::ADD, Flags.hasNoSignedWrap(),
4125 Flags.hasNoUnsignedWrap(), Known, Known2);
4126 break;
4127 }
4128 case ISD::USUBO:
4129 case ISD::SSUBO:
4130 case ISD::USUBO_CARRY:
4131 case ISD::SSUBO_CARRY:
4132 if (Op.getResNo() == 1) {
4133 // If we know the result of a setcc has the top bits zero, use this info.
4134 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4136 BitWidth > 1)
4137 Known.Zero.setBitsFrom(1);
4138 break;
4139 }
4140 [[fallthrough]];
4141 case ISD::SUBC: {
4142 assert(Op.getResNo() == 0 &&
4143 "We only compute knownbits for the difference here.");
4144
4145 // With USUBO_CARRY and SSUBO_CARRY a borrow bit may be added in.
4146 KnownBits Borrow(1);
4147 if (Opcode == ISD::USUBO_CARRY || Opcode == ISD::SSUBO_CARRY) {
4148 Borrow = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4149 // Borrow has bit width 1
4150 Borrow = Borrow.trunc(1);
4151 } else {
4152 Borrow.setAllZero();
4153 }
4154
4155 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4156 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4157 Known = KnownBits::computeForSubBorrow(Known, Known2, Borrow);
4158 break;
4159 }
4160 case ISD::UADDO:
4161 case ISD::SADDO:
4162 case ISD::UADDO_CARRY:
4163 case ISD::SADDO_CARRY:
4164 if (Op.getResNo() == 1) {
4165 // If we know the result of a setcc has the top bits zero, use this info.
4166 if (TLI->getBooleanContents(Op.getOperand(0).getValueType()) ==
4168 BitWidth > 1)
4169 Known.Zero.setBitsFrom(1);
4170 break;
4171 }
4172 [[fallthrough]];
4173 case ISD::ADDC:
4174 case ISD::ADDE: {
4175 assert(Op.getResNo() == 0 && "We only compute knownbits for the sum here.");
4176
4177 // With ADDE and UADDO_CARRY, a carry bit may be added in.
4178 KnownBits Carry(1);
4179 if (Opcode == ISD::ADDE)
4180 // Can't track carry from glue, set carry to unknown.
4181 Carry.resetAll();
4182 else if (Opcode == ISD::UADDO_CARRY || Opcode == ISD::SADDO_CARRY) {
4183 Carry = computeKnownBits(Op.getOperand(2), DemandedElts, Depth + 1);
4184 // Carry has bit width 1
4185 Carry = Carry.trunc(1);
4186 } else {
4187 Carry.setAllZero();
4188 }
4189
4190 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4191 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4192 Known = KnownBits::computeForAddCarry(Known, Known2, Carry);
4193 break;
4194 }
4195 case ISD::UDIV: {
4196 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4197 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4198 Known = KnownBits::udiv(Known, Known2, Op->getFlags().hasExact());
4199 break;
4200 }
4201 case ISD::SDIV: {
4202 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4203 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4204 Known = KnownBits::sdiv(Known, Known2, Op->getFlags().hasExact());
4205 break;
4206 }
4207 case ISD::SREM: {
4208 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4209 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4210 Known = KnownBits::srem(Known, Known2);
4211 break;
4212 }
4213 case ISD::UREM: {
4214 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4215 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4216 Known = KnownBits::urem(Known, Known2);
4217 break;
4218 }
4219 case ISD::EXTRACT_ELEMENT: {
4220 Known = computeKnownBits(Op.getOperand(0), Depth+1);
4221 const unsigned Index = Op.getConstantOperandVal(1);
4222 const unsigned EltBitWidth = Op.getValueSizeInBits();
4223
4224 // Remove low part of known bits mask
4225 Known.Zero = Known.Zero.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4226 Known.One = Known.One.getHiBits(Known.getBitWidth() - Index * EltBitWidth);
4227
4228 // Remove high part of known bit mask
4229 Known = Known.trunc(EltBitWidth);
4230 break;
4231 }
4233 SDValue InVec = Op.getOperand(0);
4234 SDValue EltNo = Op.getOperand(1);
4235 EVT VecVT = InVec.getValueType();
4236 // computeKnownBits not yet implemented for scalable vectors.
4237 if (VecVT.isScalableVector())
4238 break;
4239 const unsigned EltBitWidth = VecVT.getScalarSizeInBits();
4240 const unsigned NumSrcElts = VecVT.getVectorNumElements();
4241
4242 // If BitWidth > EltBitWidth the value is anyext:ed. So we do not know
4243 // anything about the extended bits.
4244 if (BitWidth > EltBitWidth)
4245 Known = Known.trunc(EltBitWidth);
4246
4247 // If we know the element index, just demand that vector element, else for
4248 // an unknown element index, ignore DemandedElts and demand them all.
4249 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
4250 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
4251 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
4252 DemandedSrcElts =
4253 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
4254
4255 Known = computeKnownBits(InVec, DemandedSrcElts, Depth + 1);
4256 if (BitWidth > EltBitWidth)
4257 Known = Known.anyext(BitWidth);
4258 break;
4259 }
4261 if (Op.getValueType().isScalableVector())
4262 break;
4263
4264 // If we know the element index, split the demand between the
4265 // source vector and the inserted element, otherwise assume we need
4266 // the original demanded vector elements and the value.
4267 SDValue InVec = Op.getOperand(0);
4268 SDValue InVal = Op.getOperand(1);
4269 SDValue EltNo = Op.getOperand(2);
4270 bool DemandedVal = true;
4271 APInt DemandedVecElts = DemandedElts;
4272 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
4273 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
4274 unsigned EltIdx = CEltNo->getZExtValue();
4275 DemandedVal = !!DemandedElts[EltIdx];
4276 DemandedVecElts.clearBit(EltIdx);
4277 }
4278 Known.setAllConflict();
4279 if (DemandedVal) {
4280 Known2 = computeKnownBits(InVal, Depth + 1);
4281 Known = Known.intersectWith(Known2.zextOrTrunc(BitWidth));
4282 }
4283 if (!!DemandedVecElts) {
4284 Known2 = computeKnownBits(InVec, DemandedVecElts, Depth + 1);
4285 Known = Known.intersectWith(Known2);
4286 }
4287 break;
4288 }
4289 case ISD::BITREVERSE: {
4290 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4291 Known = Known2.reverseBits();
4292 break;
4293 }
4294 case ISD::BSWAP: {
4295 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4296 Known = Known2.byteSwap();
4297 break;
4298 }
4299 case ISD::ABS: {
4300 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4301 Known = Known2.abs();
4302 Known.Zero.setHighBits(
4303 ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1) - 1);
4304 break;
4305 }
4306 case ISD::USUBSAT: {
4307 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4308 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4309 Known = KnownBits::usub_sat(Known, Known2);
4310 break;
4311 }
4312 case ISD::UMIN: {
4313 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4314 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4315 Known = KnownBits::umin(Known, Known2);
4316 break;
4317 }
4318 case ISD::UMAX: {
4319 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4320 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4321 Known = KnownBits::umax(Known, Known2);
4322 break;
4323 }
4324 case ISD::SMIN:
4325 case ISD::SMAX: {
4326 // If we have a clamp pattern, we know that the number of sign bits will be
4327 // the minimum of the clamp min/max range.
4328 bool IsMax = (Opcode == ISD::SMAX);
4329 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4330 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4331 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4332 CstHigh =
4333 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4334 if (CstLow && CstHigh) {
4335 if (!IsMax)
4336 std::swap(CstLow, CstHigh);
4337
4338 const APInt &ValueLow = CstLow->getAPIntValue();
4339 const APInt &ValueHigh = CstHigh->getAPIntValue();
4340 if (ValueLow.sle(ValueHigh)) {
4341 unsigned LowSignBits = ValueLow.getNumSignBits();
4342 unsigned HighSignBits = ValueHigh.getNumSignBits();
4343 unsigned MinSignBits = std::min(LowSignBits, HighSignBits);
4344 if (ValueLow.isNegative() && ValueHigh.isNegative()) {
4345 Known.One.setHighBits(MinSignBits);
4346 break;
4347 }
4348 if (ValueLow.isNonNegative() && ValueHigh.isNonNegative()) {
4349 Known.Zero.setHighBits(MinSignBits);
4350 break;
4351 }
4352 }
4353 }
4354
4355 Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4356 Known2 = computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
4357 if (IsMax)
4358 Known = KnownBits::smax(Known, Known2);
4359 else
4360 Known = KnownBits::smin(Known, Known2);
4361
4362 // For SMAX, if CstLow is non-negative we know the result will be
4363 // non-negative and thus all sign bits are 0.
4364 // TODO: There's an equivalent of this for smin with negative constant for
4365 // known ones.
4366 if (IsMax && CstLow) {
4367 const APInt &ValueLow = CstLow->getAPIntValue();
4368 if (ValueLow.isNonNegative()) {
4369 unsigned SignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4370 Known.Zero.setHighBits(std::min(SignBits, ValueLow.getNumSignBits()));
4371 }
4372 }
4373
4374 break;
4375 }
4376 case ISD::UINT_TO_FP: {
4377 Known.makeNonNegative();
4378 break;
4379 }
4380 case ISD::SINT_TO_FP: {
4381 Known2 = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
4382 if (Known2.isNonNegative())
4383 Known.makeNonNegative();
4384 else if (Known2.isNegative())
4385 Known.makeNegative();
4386 break;
4387 }
4388 case ISD::FP_TO_UINT_SAT: {
4389 // FP_TO_UINT_SAT produces an unsigned value that fits in the saturating VT.
4390 EVT VT = cast<VTSDNode>(Op.getOperand(1))->getVT();
4392 break;
4393 }
4394 case ISD::ATOMIC_LOAD: {
4395 // If we are looking at the loaded value.
4396 if (Op.getResNo() == 0) {
4397 auto *AT = cast<AtomicSDNode>(Op);
4398 unsigned ScalarMemorySize = AT->getMemoryVT().getScalarSizeInBits();
4399 KnownBits KnownScalarMemory(ScalarMemorySize);
4400 if (const MDNode *MD = AT->getRanges())
4401 computeKnownBitsFromRangeMetadata(*MD, KnownScalarMemory);
4402
4403 switch (AT->getExtensionType()) {
4404 case ISD::ZEXTLOAD:
4405 Known = KnownScalarMemory.zext(BitWidth);
4406 break;
4407 case ISD::SEXTLOAD:
4408 Known = KnownScalarMemory.sext(BitWidth);
4409 break;
4410 case ISD::EXTLOAD:
4411 switch (TLI->getExtendForAtomicOps()) {
4412 case ISD::ZERO_EXTEND:
4413 Known = KnownScalarMemory.zext(BitWidth);
4414 break;
4415 case ISD::SIGN_EXTEND:
4416 Known = KnownScalarMemory.sext(BitWidth);
4417 break;
4418 default:
4419 Known = KnownScalarMemory.anyext(BitWidth);
4420 break;
4421 }
4422 break;
4423 case ISD::NON_EXTLOAD:
4424 Known = KnownScalarMemory;
4425 break;
4426 }
4427 assert(Known.getBitWidth() == BitWidth);
4428 }
4429 break;
4430 }
4432 if (Op.getResNo() == 1) {
4433 // The boolean result conforms to getBooleanContents.
4434 // If we know the result of a setcc has the top bits zero, use this info.
4435 // We know that we have an integer-based boolean since these operations
4436 // are only available for integer.
4437 if (TLI->getBooleanContents(Op.getValueType().isVector(), false) ==
4439 BitWidth > 1)
4440 Known.Zero.setBitsFrom(1);
4441 break;
4442 }
4443 [[fallthrough]];
4445 case ISD::ATOMIC_SWAP:
4456 case ISD::ATOMIC_LOAD_UMAX: {
4457 // If we are looking at the loaded value.
4458 if (Op.getResNo() == 0) {
4459 auto *AT = cast<AtomicSDNode>(Op);
4460 unsigned MemBits = AT->getMemoryVT().getScalarSizeInBits();
4461
4462 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
4463 Known.Zero.setBitsFrom(MemBits);
4464 }
4465 break;
4466 }
4467 case ISD::FrameIndex:
4469 TLI->computeKnownBitsForFrameIndex(cast<FrameIndexSDNode>(Op)->getIndex(),
4470 Known, getMachineFunction());
4471 break;
4472
4473 default:
4474 if (Opcode < ISD::BUILTIN_OP_END)
4475 break;
4476 [[fallthrough]];
4480 // TODO: Probably okay to remove after audit; here to reduce change size
4481 // in initial enablement patch for scalable vectors
4482 if (Op.getValueType().isScalableVector())
4483 break;
4484
4485 // Allow the target to implement this method for its nodes.
4486 TLI->computeKnownBitsForTargetNode(Op, Known, DemandedElts, *this, Depth);
4487 break;
4488 }
4489
4490 return Known;
4491}
4492
4493/// Convert ConstantRange OverflowResult into SelectionDAG::OverflowKind.
4506
4509 // X + 0 never overflow
4510 if (isNullConstant(N1))
4511 return OFK_Never;
4512
4513 // If both operands each have at least two sign bits, the addition
4514 // cannot overflow.
4515 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4516 return OFK_Never;
4517
4518 // TODO: Add ConstantRange::signedAddMayOverflow handling.
4519 return OFK_Sometime;
4520}
4521
4524 // X + 0 never overflow
4525 if (isNullConstant(N1))
4526 return OFK_Never;
4527
4528 // mulhi + 1 never overflow
4529 KnownBits N1Known = computeKnownBits(N1);
4530 if (N0.getOpcode() == ISD::UMUL_LOHI && N0.getResNo() == 1 &&
4531 N1Known.getMaxValue().ult(2))
4532 return OFK_Never;
4533
4534 KnownBits N0Known = computeKnownBits(N0);
4535 if (N1.getOpcode() == ISD::UMUL_LOHI && N1.getResNo() == 1 &&
4536 N0Known.getMaxValue().ult(2))
4537 return OFK_Never;
4538
4539 // Fallback to ConstantRange::unsignedAddMayOverflow handling.
4540 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4541 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4542 return mapOverflowResult(N0Range.unsignedAddMayOverflow(N1Range));
4543}
4544
4547 // X - 0 never overflow
4548 if (isNullConstant(N1))
4549 return OFK_Never;
4550
4551 // If both operands each have at least two sign bits, the subtraction
4552 // cannot overflow.
4553 if (ComputeNumSignBits(N0) > 1 && ComputeNumSignBits(N1) > 1)
4554 return OFK_Never;
4555
4556 KnownBits N0Known = computeKnownBits(N0);
4557 KnownBits N1Known = computeKnownBits(N1);
4558 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, true);
4559 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, true);
4560 return mapOverflowResult(N0Range.signedSubMayOverflow(N1Range));
4561}
4562
4565 // X - 0 never overflow
4566 if (isNullConstant(N1))
4567 return OFK_Never;
4568
4569 KnownBits N0Known = computeKnownBits(N0);
4570 KnownBits N1Known = computeKnownBits(N1);
4571 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4572 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4573 return mapOverflowResult(N0Range.unsignedSubMayOverflow(N1Range));
4574}
4575
4578 // X * 0 and X * 1 never overflow.
4579 if (isNullConstant(N1) || isOneConstant(N1))
4580 return OFK_Never;
4581
4582 KnownBits N0Known = computeKnownBits(N0);
4583 KnownBits N1Known = computeKnownBits(N1);
4584 ConstantRange N0Range = ConstantRange::fromKnownBits(N0Known, false);
4585 ConstantRange N1Range = ConstantRange::fromKnownBits(N1Known, false);
4586 return mapOverflowResult(N0Range.unsignedMulMayOverflow(N1Range));
4587}
4588
4591 // X * 0 and X * 1 never overflow.
4592 if (isNullConstant(N1) || isOneConstant(N1))
4593 return OFK_Never;
4594
4595 // Get the size of the result.
4596 unsigned BitWidth = N0.getScalarValueSizeInBits();
4597
4598 // Sum of the sign bits.
4599 unsigned SignBits = ComputeNumSignBits(N0) + ComputeNumSignBits(N1);
4600
4601 // If we have enough sign bits, then there's no overflow.
4602 if (SignBits > BitWidth + 1)
4603 return OFK_Never;
4604
4605 if (SignBits == BitWidth + 1) {
4606 // The overflow occurs when the true multiplication of the
4607 // the operands is the minimum negative number.
4608 KnownBits N0Known = computeKnownBits(N0);
4609 KnownBits N1Known = computeKnownBits(N1);
4610 // If one of the operands is non-negative, then there's no
4611 // overflow.
4612 if (N0Known.isNonNegative() || N1Known.isNonNegative())
4613 return OFK_Never;
4614 }
4615
4616 return OFK_Sometime;
4617}
4618
4620 if (Depth >= MaxRecursionDepth)
4621 return false; // Limit search depth.
4622
4623 EVT OpVT = Val.getValueType();
4624 unsigned BitWidth = OpVT.getScalarSizeInBits();
4625
4626 // Is the constant a known power of 2?
4628 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4629 }))
4630 return true;
4631
4632 // A left-shift of a constant one will have exactly one bit set because
4633 // shifting the bit off the end is undefined.
4634 if (Val.getOpcode() == ISD::SHL) {
4635 auto *C = isConstOrConstSplat(Val.getOperand(0));
4636 if (C && C->getAPIntValue() == 1)
4637 return true;
4638 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4639 isKnownNeverZero(Val, Depth);
4640 }
4641
4642 // Similarly, a logical right-shift of a constant sign-bit will have exactly
4643 // one bit set.
4644 if (Val.getOpcode() == ISD::SRL) {
4645 auto *C = isConstOrConstSplat(Val.getOperand(0));
4646 if (C && C->getAPIntValue().isSignMask())
4647 return true;
4648 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1) &&
4649 isKnownNeverZero(Val, Depth);
4650 }
4651
4652 if (Val.getOpcode() == ISD::ROTL || Val.getOpcode() == ISD::ROTR)
4653 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4654
4655 // Are all operands of a build vector constant powers of two?
4656 if (Val.getOpcode() == ISD::BUILD_VECTOR)
4657 if (llvm::all_of(Val->ops(), [BitWidth](SDValue E) {
4658 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(E))
4659 return C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2();
4660 return false;
4661 }))
4662 return true;
4663
4664 // Is the operand of a splat vector a constant power of two?
4665 if (Val.getOpcode() == ISD::SPLAT_VECTOR)
4667 if (C->getAPIntValue().zextOrTrunc(BitWidth).isPowerOf2())
4668 return true;
4669
4670 // vscale(power-of-two) is a power-of-two for some targets
4671 if (Val.getOpcode() == ISD::VSCALE &&
4672 getTargetLoweringInfo().isVScaleKnownToBeAPowerOfTwo() &&
4674 return true;
4675
4676 if (Val.getOpcode() == ISD::SMIN || Val.getOpcode() == ISD::SMAX ||
4677 Val.getOpcode() == ISD::UMIN || Val.getOpcode() == ISD::UMAX)
4678 return isKnownToBeAPowerOfTwo(Val.getOperand(1), Depth + 1) &&
4680
4681 if (Val.getOpcode() == ISD::SELECT || Val.getOpcode() == ISD::VSELECT)
4682 return isKnownToBeAPowerOfTwo(Val.getOperand(2), Depth + 1) &&
4684
4685 // Looking for `x & -x` pattern:
4686 // If x == 0:
4687 // x & -x -> 0
4688 // If x != 0:
4689 // x & -x -> non-zero pow2
4690 // so if we find the pattern return whether we know `x` is non-zero.
4691 SDValue X;
4692 if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
4693 return isKnownNeverZero(X, Depth);
4694
4695 if (Val.getOpcode() == ISD::ZERO_EXTEND)
4696 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4697
4698 // More could be done here, though the above checks are enough
4699 // to handle some common cases.
4700 return false;
4701}
4702
4704 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Val, true))
4705 return C1->getValueAPF().getExactLog2Abs() >= 0;
4706
4707 if (Val.getOpcode() == ISD::UINT_TO_FP || Val.getOpcode() == ISD::SINT_TO_FP)
4708 return isKnownToBeAPowerOfTwo(Val.getOperand(0), Depth + 1);
4709
4710 return false;
4711}
4712
4714 EVT VT = Op.getValueType();
4715
4716 // Since the number of lanes in a scalable vector is unknown at compile time,
4717 // we track one bit which is implicitly broadcast to all lanes. This means
4718 // that all lanes in a scalable vector are considered demanded.
4719 APInt DemandedElts = VT.isFixedLengthVector()
4721 : APInt(1, 1);
4722 return ComputeNumSignBits(Op, DemandedElts, Depth);
4723}
4724
4725unsigned SelectionDAG::ComputeNumSignBits(SDValue Op, const APInt &DemandedElts,
4726 unsigned Depth) const {
4727 EVT VT = Op.getValueType();
4728 assert((VT.isInteger() || VT.isFloatingPoint()) && "Invalid VT!");
4729 unsigned VTBits = VT.getScalarSizeInBits();
4730 unsigned NumElts = DemandedElts.getBitWidth();
4731 unsigned Tmp, Tmp2;
4732 unsigned FirstAnswer = 1;
4733
4734 assert((!VT.isScalableVector() || NumElts == 1) &&
4735 "DemandedElts for scalable vectors must be 1 to represent all lanes");
4736
4737 if (auto *C = dyn_cast<ConstantSDNode>(Op)) {
4738 const APInt &Val = C->getAPIntValue();
4739 return Val.getNumSignBits();
4740 }
4741
4742 if (Depth >= MaxRecursionDepth)
4743 return 1; // Limit search depth.
4744
4745 if (!DemandedElts)
4746 return 1; // No demanded elts, better to assume we don't know anything.
4747
4748 unsigned Opcode = Op.getOpcode();
4749 switch (Opcode) {
4750 default: break;
4751 case ISD::AssertSext:
4752 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4753 return VTBits-Tmp+1;
4754 case ISD::AssertZext:
4755 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getSizeInBits();
4756 return VTBits-Tmp;
4757 case ISD::FREEZE:
4758 if (isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
4759 /*PoisonOnly=*/false))
4760 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4761 break;
4762 case ISD::MERGE_VALUES:
4763 return ComputeNumSignBits(Op.getOperand(Op.getResNo()), DemandedElts,
4764 Depth + 1);
4765 case ISD::SPLAT_VECTOR: {
4766 // Check if the sign bits of source go down as far as the truncated value.
4767 unsigned NumSrcBits = Op.getOperand(0).getValueSizeInBits();
4768 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
4769 if (NumSrcSignBits > (NumSrcBits - VTBits))
4770 return NumSrcSignBits - (NumSrcBits - VTBits);
4771 break;
4772 }
4773 case ISD::BUILD_VECTOR:
4774 assert(!VT.isScalableVector());
4775 Tmp = VTBits;
4776 for (unsigned i = 0, e = Op.getNumOperands(); (i < e) && (Tmp > 1); ++i) {
4777 if (!DemandedElts[i])
4778 continue;
4779
4780 SDValue SrcOp = Op.getOperand(i);
4781 // BUILD_VECTOR can implicitly truncate sources, we handle this specially
4782 // for constant nodes to ensure we only look at the sign bits.
4784 APInt T = C->getAPIntValue().trunc(VTBits);
4785 Tmp2 = T.getNumSignBits();
4786 } else {
4787 Tmp2 = ComputeNumSignBits(SrcOp, Depth + 1);
4788
4789 if (SrcOp.getValueSizeInBits() != VTBits) {
4790 assert(SrcOp.getValueSizeInBits() > VTBits &&
4791 "Expected BUILD_VECTOR implicit truncation");
4792 unsigned ExtraBits = SrcOp.getValueSizeInBits() - VTBits;
4793 Tmp2 = (Tmp2 > ExtraBits ? Tmp2 - ExtraBits : 1);
4794 }
4795 }
4796 Tmp = std::min(Tmp, Tmp2);
4797 }
4798 return Tmp;
4799
4800 case ISD::VECTOR_COMPRESS: {
4801 SDValue Vec = Op.getOperand(0);
4802 SDValue PassThru = Op.getOperand(2);
4803 Tmp = ComputeNumSignBits(PassThru, DemandedElts, Depth + 1);
4804 if (Tmp == 1)
4805 return 1;
4806 Tmp2 = ComputeNumSignBits(Vec, Depth + 1);
4807 Tmp = std::min(Tmp, Tmp2);
4808 return Tmp;
4809 }
4810
4811 case ISD::VECTOR_SHUFFLE: {
4812 // Collect the minimum number of sign bits that are shared by every vector
4813 // element referenced by the shuffle.
4814 APInt DemandedLHS, DemandedRHS;
4816 assert(NumElts == SVN->getMask().size() && "Unexpected vector size");
4817 if (!getShuffleDemandedElts(NumElts, SVN->getMask(), DemandedElts,
4818 DemandedLHS, DemandedRHS))
4819 return 1;
4820
4821 Tmp = std::numeric_limits<unsigned>::max();
4822 if (!!DemandedLHS)
4823 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1);
4824 if (!!DemandedRHS) {
4825 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1);
4826 Tmp = std::min(Tmp, Tmp2);
4827 }
4828 // If we don't know anything, early out and try computeKnownBits fall-back.
4829 if (Tmp == 1)
4830 break;
4831 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
4832 return Tmp;
4833 }
4834
4835 case ISD::BITCAST: {
4836 if (VT.isScalableVector())
4837 break;
4838 SDValue N0 = Op.getOperand(0);
4839 EVT SrcVT = N0.getValueType();
4840 unsigned SrcBits = SrcVT.getScalarSizeInBits();
4841
4842 // Ignore bitcasts from unsupported types..
4843 if (!(SrcVT.isInteger() || SrcVT.isFloatingPoint()))
4844 break;
4845
4846 // Fast handling of 'identity' bitcasts.
4847 if (VTBits == SrcBits)
4848 return ComputeNumSignBits(N0, DemandedElts, Depth + 1);
4849
4850 bool IsLE = getDataLayout().isLittleEndian();
4851
4852 // Bitcast 'large element' scalar/vector to 'small element' vector.
4853 if ((SrcBits % VTBits) == 0) {
4854 assert(VT.isVector() && "Expected bitcast to vector");
4855
4856 unsigned Scale = SrcBits / VTBits;
4857 APInt SrcDemandedElts =
4858 APIntOps::ScaleBitMask(DemandedElts, NumElts / Scale);
4859
4860 // Fast case - sign splat can be simply split across the small elements.
4861 Tmp = ComputeNumSignBits(N0, SrcDemandedElts, Depth + 1);
4862 if (Tmp == SrcBits)
4863 return VTBits;
4864
4865 // Slow case - determine how far the sign extends into each sub-element.
4866 Tmp2 = VTBits;
4867 for (unsigned i = 0; i != NumElts; ++i)
4868 if (DemandedElts[i]) {
4869 unsigned SubOffset = i % Scale;
4870 SubOffset = (IsLE ? ((Scale - 1) - SubOffset) : SubOffset);
4871 SubOffset = SubOffset * VTBits;
4872 if (Tmp <= SubOffset)
4873 return 1;
4874 Tmp2 = std::min(Tmp2, Tmp - SubOffset);
4875 }
4876 return Tmp2;
4877 }
4878 break;
4879 }
4880
4882 // FP_TO_SINT_SAT produces a signed value that fits in the saturating VT.
4883 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4884 return VTBits - Tmp + 1;
4885 case ISD::SIGN_EXTEND:
4886 Tmp = VTBits - Op.getOperand(0).getScalarValueSizeInBits();
4887 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1) + Tmp;
4889 // Max of the input and what this extends.
4890 Tmp = cast<VTSDNode>(Op.getOperand(1))->getVT().getScalarSizeInBits();
4891 Tmp = VTBits-Tmp+1;
4892 Tmp2 = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4893 return std::max(Tmp, Tmp2);
4895 if (VT.isScalableVector())
4896 break;
4897 SDValue Src = Op.getOperand(0);
4898 EVT SrcVT = Src.getValueType();
4899 APInt DemandedSrcElts = DemandedElts.zext(SrcVT.getVectorNumElements());
4900 Tmp = VTBits - SrcVT.getScalarSizeInBits();
4901 return ComputeNumSignBits(Src, DemandedSrcElts, Depth+1) + Tmp;
4902 }
4903 case ISD::SRA:
4904 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4905 // SRA X, C -> adds C sign bits.
4906 if (std::optional<unsigned> ShAmt =
4907 getValidMinimumShiftAmount(Op, DemandedElts, Depth + 1))
4908 Tmp = std::min(Tmp + *ShAmt, VTBits);
4909 return Tmp;
4910 case ISD::SHL:
4911 if (std::optional<ConstantRange> ShAmtRange =
4912 getValidShiftAmountRange(Op, DemandedElts, Depth + 1)) {
4913 unsigned MaxShAmt = ShAmtRange->getUnsignedMax().getZExtValue();
4914 unsigned MinShAmt = ShAmtRange->getUnsignedMin().getZExtValue();
4915 // Try to look through ZERO/SIGN/ANY_EXTEND. If all extended bits are
4916 // shifted out, then we can compute the number of sign bits for the
4917 // operand being extended. A future improvement could be to pass along the
4918 // "shifted left by" information in the recursive calls to
4919 // ComputeKnownSignBits. Allowing us to handle this more generically.
4920 if (ISD::isExtOpcode(Op.getOperand(0).getOpcode())) {
4921 SDValue Ext = Op.getOperand(0);
4922 EVT ExtVT = Ext.getValueType();
4923 SDValue Extendee = Ext.getOperand(0);
4924 EVT ExtendeeVT = Extendee.getValueType();
4925 unsigned SizeDifference =
4926 ExtVT.getScalarSizeInBits() - ExtendeeVT.getScalarSizeInBits();
4927 if (SizeDifference <= MinShAmt) {
4928 Tmp = SizeDifference +
4929 ComputeNumSignBits(Extendee, DemandedElts, Depth + 1);
4930 if (MaxShAmt < Tmp)
4931 return Tmp - MaxShAmt;
4932 }
4933 }
4934 // shl destroys sign bits, ensure it doesn't shift out all sign bits.
4935 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4936 if (MaxShAmt < Tmp)
4937 return Tmp - MaxShAmt;
4938 }
4939 break;
4940 case ISD::AND:
4941 case ISD::OR:
4942 case ISD::XOR: // NOT is handled here.
4943 // Logical binary ops preserve the number of sign bits at the worst.
4944 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth+1);
4945 if (Tmp != 1) {
4946 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4947 FirstAnswer = std::min(Tmp, Tmp2);
4948 // We computed what we know about the sign bits as our first
4949 // answer. Now proceed to the generic code that uses
4950 // computeKnownBits, and pick whichever answer is better.
4951 }
4952 break;
4953
4954 case ISD::SELECT:
4955 case ISD::VSELECT:
4956 Tmp = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth+1);
4957 if (Tmp == 1) return 1; // Early out.
4958 Tmp2 = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4959 return std::min(Tmp, Tmp2);
4960 case ISD::SELECT_CC:
4961 Tmp = ComputeNumSignBits(Op.getOperand(2), DemandedElts, Depth+1);
4962 if (Tmp == 1) return 1; // Early out.
4963 Tmp2 = ComputeNumSignBits(Op.getOperand(3), DemandedElts, Depth+1);
4964 return std::min(Tmp, Tmp2);
4965
4966 case ISD::SMIN:
4967 case ISD::SMAX: {
4968 // If we have a clamp pattern, we know that the number of sign bits will be
4969 // the minimum of the clamp min/max range.
4970 bool IsMax = (Opcode == ISD::SMAX);
4971 ConstantSDNode *CstLow = nullptr, *CstHigh = nullptr;
4972 if ((CstLow = isConstOrConstSplat(Op.getOperand(1), DemandedElts)))
4973 if (Op.getOperand(0).getOpcode() == (IsMax ? ISD::SMIN : ISD::SMAX))
4974 CstHigh =
4975 isConstOrConstSplat(Op.getOperand(0).getOperand(1), DemandedElts);
4976 if (CstLow && CstHigh) {
4977 if (!IsMax)
4978 std::swap(CstLow, CstHigh);
4979 if (CstLow->getAPIntValue().sle(CstHigh->getAPIntValue())) {
4980 Tmp = CstLow->getAPIntValue().getNumSignBits();
4981 Tmp2 = CstHigh->getAPIntValue().getNumSignBits();
4982 return std::min(Tmp, Tmp2);
4983 }
4984 }
4985
4986 // Fallback - just get the minimum number of sign bits of the operands.
4987 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4988 if (Tmp == 1)
4989 return 1; // Early out.
4990 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4991 return std::min(Tmp, Tmp2);
4992 }
4993 case ISD::UMIN:
4994 case ISD::UMAX:
4995 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
4996 if (Tmp == 1)
4997 return 1; // Early out.
4998 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
4999 return std::min(Tmp, Tmp2);
5000 case ISD::SSUBO_CARRY:
5001 case ISD::USUBO_CARRY:
5002 // sub_carry(x,x,c) -> 0/-1 (sext carry)
5003 if (Op.getResNo() == 0 && Op.getOperand(0) == Op.getOperand(1))
5004 return VTBits;
5005 [[fallthrough]];
5006 case ISD::SADDO:
5007 case ISD::UADDO:
5008 case ISD::SADDO_CARRY:
5009 case ISD::UADDO_CARRY:
5010 case ISD::SSUBO:
5011 case ISD::USUBO:
5012 case ISD::SMULO:
5013 case ISD::UMULO:
5014 if (Op.getResNo() != 1)
5015 break;
5016 // The boolean result conforms to getBooleanContents. Fall through.
5017 // If setcc returns 0/-1, all bits are sign bits.
5018 // We know that we have an integer-based boolean since these operations
5019 // are only available for integer.
5020 if (TLI->getBooleanContents(VT.isVector(), false) ==
5022 return VTBits;
5023 break;
5024 case ISD::SETCC:
5025 case ISD::SETCCCARRY:
5026 case ISD::STRICT_FSETCC:
5027 case ISD::STRICT_FSETCCS: {
5028 unsigned OpNo = Op->isStrictFPOpcode() ? 1 : 0;
5029 // If setcc returns 0/-1, all bits are sign bits.
5030 if (TLI->getBooleanContents(Op.getOperand(OpNo).getValueType()) ==
5032 return VTBits;
5033 break;
5034 }
5035 case ISD::ROTL:
5036 case ISD::ROTR:
5037 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5038
5039 // If we're rotating an 0/-1 value, then it stays an 0/-1 value.
5040 if (Tmp == VTBits)
5041 return VTBits;
5042
5043 if (ConstantSDNode *C =
5044 isConstOrConstSplat(Op.getOperand(1), DemandedElts)) {
5045 unsigned RotAmt = C->getAPIntValue().urem(VTBits);
5046
5047 // Handle rotate right by N like a rotate left by 32-N.
5048 if (Opcode == ISD::ROTR)
5049 RotAmt = (VTBits - RotAmt) % VTBits;
5050
5051 // If we aren't rotating out all of the known-in sign bits, return the
5052 // number that are left. This handles rotl(sext(x), 1) for example.
5053 if (Tmp > (RotAmt + 1)) return (Tmp - RotAmt);
5054 }
5055 break;
5056 case ISD::ADD:
5057 case ISD::ADDC:
5058 // TODO: Move Operand 1 check before Operand 0 check
5059 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5060 if (Tmp == 1) return 1; // Early out.
5061
5062 // Special case decrementing a value (ADD X, -1):
5063 if (ConstantSDNode *CRHS =
5064 isConstOrConstSplat(Op.getOperand(1), DemandedElts))
5065 if (CRHS->isAllOnes()) {
5066 KnownBits Known =
5067 computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
5068
5069 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5070 // sign bits set.
5071 if ((Known.Zero | 1).isAllOnes())
5072 return VTBits;
5073
5074 // If we are subtracting one from a positive number, there is no carry
5075 // out of the result.
5076 if (Known.isNonNegative())
5077 return Tmp;
5078 }
5079
5080 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5081 if (Tmp2 == 1) return 1; // Early out.
5082
5083 // Add can have at most one carry bit. Thus we know that the output
5084 // is, at worst, one more bit than the inputs.
5085 return std::min(Tmp, Tmp2) - 1;
5086 case ISD::SUB:
5087 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5088 if (Tmp2 == 1) return 1; // Early out.
5089
5090 // Handle NEG.
5091 if (ConstantSDNode *CLHS =
5092 isConstOrConstSplat(Op.getOperand(0), DemandedElts))
5093 if (CLHS->isZero()) {
5094 KnownBits Known =
5095 computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1);
5096 // If the input is known to be 0 or 1, the output is 0/-1, which is all
5097 // sign bits set.
5098 if ((Known.Zero | 1).isAllOnes())
5099 return VTBits;
5100
5101 // If the input is known to be positive (the sign bit is known clear),
5102 // the output of the NEG has the same number of sign bits as the input.
5103 if (Known.isNonNegative())
5104 return Tmp2;
5105
5106 // Otherwise, we treat this like a SUB.
5107 }
5108
5109 // Sub can have at most one carry bit. Thus we know that the output
5110 // is, at worst, one more bit than the inputs.
5111 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5112 if (Tmp == 1) return 1; // Early out.
5113 return std::min(Tmp, Tmp2) - 1;
5114 case ISD::MUL: {
5115 // The output of the Mul can be at most twice the valid bits in the inputs.
5116 unsigned SignBitsOp0 = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5117 if (SignBitsOp0 == 1)
5118 break;
5119 unsigned SignBitsOp1 = ComputeNumSignBits(Op.getOperand(1), Depth + 1);
5120 if (SignBitsOp1 == 1)
5121 break;
5122 unsigned OutValidBits =
5123 (VTBits - SignBitsOp0 + 1) + (VTBits - SignBitsOp1 + 1);
5124 return OutValidBits > VTBits ? 1 : VTBits - OutValidBits + 1;
5125 }
5126 case ISD::AVGCEILS:
5127 case ISD::AVGFLOORS:
5128 Tmp = ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5129 if (Tmp == 1)
5130 return 1; // Early out.
5131 Tmp2 = ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1);
5132 return std::min(Tmp, Tmp2);
5133 case ISD::SREM:
5134 // The sign bit is the LHS's sign bit, except when the result of the
5135 // remainder is zero. The magnitude of the result should be less than or
5136 // equal to the magnitude of the LHS. Therefore, the result should have
5137 // at least as many sign bits as the left hand side.
5138 return ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1);
5139 case ISD::TRUNCATE: {
5140 // Check if the sign bits of source go down as far as the truncated value.
5141 unsigned NumSrcBits = Op.getOperand(0).getScalarValueSizeInBits();
5142 unsigned NumSrcSignBits = ComputeNumSignBits(Op.getOperand(0), Depth + 1);
5143 if (NumSrcSignBits > (NumSrcBits - VTBits))
5144 return NumSrcSignBits - (NumSrcBits - VTBits);
5145 break;
5146 }
5147 case ISD::EXTRACT_ELEMENT: {
5148 if (VT.isScalableVector())
5149 break;
5150 const int KnownSign = ComputeNumSignBits(Op.getOperand(0), Depth+1);
5151 const int BitWidth = Op.getValueSizeInBits();
5152 const int Items = Op.getOperand(0).getValueSizeInBits() / BitWidth;
5153
5154 // Get reverse index (starting from 1), Op1 value indexes elements from
5155 // little end. Sign starts at big end.
5156 const int rIndex = Items - 1 - Op.getConstantOperandVal(1);
5157
5158 // If the sign portion ends in our element the subtraction gives correct
5159 // result. Otherwise it gives either negative or > bitwidth result
5160 return std::clamp(KnownSign - rIndex * BitWidth, 1, BitWidth);
5161 }
5163 if (VT.isScalableVector())
5164 break;
5165 // If we know the element index, split the demand between the
5166 // source vector and the inserted element, otherwise assume we need
5167 // the original demanded vector elements and the value.
5168 SDValue InVec = Op.getOperand(0);
5169 SDValue InVal = Op.getOperand(1);
5170 SDValue EltNo = Op.getOperand(2);
5171 bool DemandedVal = true;
5172 APInt DemandedVecElts = DemandedElts;
5173 auto *CEltNo = dyn_cast<ConstantSDNode>(EltNo);
5174 if (CEltNo && CEltNo->getAPIntValue().ult(NumElts)) {
5175 unsigned EltIdx = CEltNo->getZExtValue();
5176 DemandedVal = !!DemandedElts[EltIdx];
5177 DemandedVecElts.clearBit(EltIdx);
5178 }
5179 Tmp = std::numeric_limits<unsigned>::max();
5180 if (DemandedVal) {
5181 // TODO - handle implicit truncation of inserted elements.
5182 if (InVal.getScalarValueSizeInBits() != VTBits)
5183 break;
5184 Tmp2 = ComputeNumSignBits(InVal, Depth + 1);
5185 Tmp = std::min(Tmp, Tmp2);
5186 }
5187 if (!!DemandedVecElts) {
5188 Tmp2 = ComputeNumSignBits(InVec, DemandedVecElts, Depth + 1);
5189 Tmp = std::min(Tmp, Tmp2);
5190 }
5191 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5192 return Tmp;
5193 }
5195 SDValue InVec = Op.getOperand(0);
5196 SDValue EltNo = Op.getOperand(1);
5197 EVT VecVT = InVec.getValueType();
5198 // ComputeNumSignBits not yet implemented for scalable vectors.
5199 if (VecVT.isScalableVector())
5200 break;
5201 const unsigned BitWidth = Op.getValueSizeInBits();
5202 const unsigned EltBitWidth = Op.getOperand(0).getScalarValueSizeInBits();
5203 const unsigned NumSrcElts = VecVT.getVectorNumElements();
5204
5205 // If BitWidth > EltBitWidth the value is anyext:ed, and we do not know
5206 // anything about sign bits. But if the sizes match we can derive knowledge
5207 // about sign bits from the vector operand.
5208 if (BitWidth != EltBitWidth)
5209 break;
5210
5211 // If we know the element index, just demand that vector element, else for
5212 // an unknown element index, ignore DemandedElts and demand them all.
5213 APInt DemandedSrcElts = APInt::getAllOnes(NumSrcElts);
5214 auto *ConstEltNo = dyn_cast<ConstantSDNode>(EltNo);
5215 if (ConstEltNo && ConstEltNo->getAPIntValue().ult(NumSrcElts))
5216 DemandedSrcElts =
5217 APInt::getOneBitSet(NumSrcElts, ConstEltNo->getZExtValue());
5218
5219 return ComputeNumSignBits(InVec, DemandedSrcElts, Depth + 1);
5220 }
5222 // Offset the demanded elts by the subvector index.
5223 SDValue Src = Op.getOperand(0);
5224
5225 APInt DemandedSrcElts;
5226 if (Src.getValueType().isScalableVector())
5227 DemandedSrcElts = APInt(1, 1);
5228 else {
5229 uint64_t Idx = Op.getConstantOperandVal(1);
5230 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5231 DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5232 }
5233 return ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5234 }
5235 case ISD::CONCAT_VECTORS: {
5236 if (VT.isScalableVector())
5237 break;
5238 // Determine the minimum number of sign bits across all demanded
5239 // elts of the input vectors. Early out if the result is already 1.
5240 Tmp = std::numeric_limits<unsigned>::max();
5241 EVT SubVectorVT = Op.getOperand(0).getValueType();
5242 unsigned NumSubVectorElts = SubVectorVT.getVectorNumElements();
5243 unsigned NumSubVectors = Op.getNumOperands();
5244 for (unsigned i = 0; (i < NumSubVectors) && (Tmp > 1); ++i) {
5245 APInt DemandedSub =
5246 DemandedElts.extractBits(NumSubVectorElts, i * NumSubVectorElts);
5247 if (!DemandedSub)
5248 continue;
5249 Tmp2 = ComputeNumSignBits(Op.getOperand(i), DemandedSub, Depth + 1);
5250 Tmp = std::min(Tmp, Tmp2);
5251 }
5252 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5253 return Tmp;
5254 }
5255 case ISD::INSERT_SUBVECTOR: {
5256 if (VT.isScalableVector())
5257 break;
5258 // Demand any elements from the subvector and the remainder from the src its
5259 // inserted into.
5260 SDValue Src = Op.getOperand(0);
5261 SDValue Sub = Op.getOperand(1);
5262 uint64_t Idx = Op.getConstantOperandVal(2);
5263 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5264 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5265 APInt DemandedSrcElts = DemandedElts;
5266 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5267
5268 Tmp = std::numeric_limits<unsigned>::max();
5269 if (!!DemandedSubElts) {
5270 Tmp = ComputeNumSignBits(Sub, DemandedSubElts, Depth + 1);
5271 if (Tmp == 1)
5272 return 1; // early-out
5273 }
5274 if (!!DemandedSrcElts) {
5275 Tmp2 = ComputeNumSignBits(Src, DemandedSrcElts, Depth + 1);
5276 Tmp = std::min(Tmp, Tmp2);
5277 }
5278 assert(Tmp <= VTBits && "Failed to determine minimum sign bits");
5279 return Tmp;
5280 }
5281 case ISD::LOAD: {
5282 // If we are looking at the loaded value of the SDNode.
5283 if (Op.getResNo() != 0)
5284 break;
5285
5287 if (const MDNode *Ranges = LD->getRanges()) {
5288 if (DemandedElts != 1)
5289 break;
5290
5292 if (VTBits > CR.getBitWidth()) {
5293 switch (LD->getExtensionType()) {
5294 case ISD::SEXTLOAD:
5295 CR = CR.signExtend(VTBits);
5296 break;
5297 case ISD::ZEXTLOAD:
5298 CR = CR.zeroExtend(VTBits);
5299 break;
5300 default:
5301 break;
5302 }
5303 }
5304
5305 if (VTBits != CR.getBitWidth())
5306 break;
5307 return std::min(CR.getSignedMin().getNumSignBits(),
5309 }
5310
5311 unsigned ExtType = LD->getExtensionType();
5312 switch (ExtType) {
5313 default:
5314 break;
5315 case ISD::SEXTLOAD: // e.g. i16->i32 = '17' bits known.
5316 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5317 return VTBits - Tmp + 1;
5318 case ISD::ZEXTLOAD: // e.g. i16->i32 = '16' bits known.
5319 Tmp = LD->getMemoryVT().getScalarSizeInBits();
5320 return VTBits - Tmp;
5321 case ISD::NON_EXTLOAD:
5322 if (const Constant *Cst = TLI->getTargetConstantFromLoad(LD)) {
5323 // We only need to handle vectors - computeKnownBits should handle
5324 // scalar cases.
5325 Type *CstTy = Cst->getType();
5326 if (CstTy->isVectorTy() && !VT.isScalableVector() &&
5327 (NumElts * VTBits) == CstTy->getPrimitiveSizeInBits() &&
5328 VTBits == CstTy->getScalarSizeInBits()) {
5329 Tmp = VTBits;
5330 for (unsigned i = 0; i != NumElts; ++i) {
5331 if (!DemandedElts[i])
5332 continue;
5333 if (Constant *Elt = Cst->getAggregateElement(i)) {
5334 if (auto *CInt = dyn_cast<ConstantInt>(Elt)) {
5335 const APInt &Value = CInt->getValue();
5336 Tmp = std::min(Tmp, Value.getNumSignBits());
5337 continue;
5338 }
5339 if (auto *CFP = dyn_cast<ConstantFP>(Elt)) {
5340 APInt Value = CFP->getValueAPF().bitcastToAPInt();
5341 Tmp = std::min(Tmp, Value.getNumSignBits());
5342 continue;
5343 }
5344 }
5345 // Unknown type. Conservatively assume no bits match sign bit.
5346 return 1;
5347 }
5348 return Tmp;
5349 }
5350 }
5351 break;
5352 }
5353
5354 break;
5355 }
5358 case ISD::ATOMIC_SWAP:
5370 case ISD::ATOMIC_LOAD: {
5371 auto *AT = cast<AtomicSDNode>(Op);
5372 // If we are looking at the loaded value.
5373 if (Op.getResNo() == 0) {
5374 Tmp = AT->getMemoryVT().getScalarSizeInBits();
5375 if (Tmp == VTBits)
5376 return 1; // early-out
5377
5378 // For atomic_load, prefer to use the extension type.
5379 if (Op->getOpcode() == ISD::ATOMIC_LOAD) {
5380 switch (AT->getExtensionType()) {
5381 default:
5382 break;
5383 case ISD::SEXTLOAD:
5384 return VTBits - Tmp + 1;
5385 case ISD::ZEXTLOAD:
5386 return VTBits - Tmp;
5387 }
5388 }
5389
5390 if (TLI->getExtendForAtomicOps() == ISD::SIGN_EXTEND)
5391 return VTBits - Tmp + 1;
5392 if (TLI->getExtendForAtomicOps() == ISD::ZERO_EXTEND)
5393 return VTBits - Tmp;
5394 }
5395 break;
5396 }
5397 }
5398
5399 // Allow the target to implement this method for its nodes.
5400 if (Opcode >= ISD::BUILTIN_OP_END ||
5401 Opcode == ISD::INTRINSIC_WO_CHAIN ||
5402 Opcode == ISD::INTRINSIC_W_CHAIN ||
5403 Opcode == ISD::INTRINSIC_VOID) {
5404 // TODO: This can probably be removed once target code is audited. This
5405 // is here purely to reduce patch size and review complexity.
5406 if (!VT.isScalableVector()) {
5407 unsigned NumBits =
5408 TLI->ComputeNumSignBitsForTargetNode(Op, DemandedElts, *this, Depth);
5409 if (NumBits > 1)
5410 FirstAnswer = std::max(FirstAnswer, NumBits);
5411 }
5412 }
5413
5414 // Finally, if we can prove that the top bits of the result are 0's or 1's,
5415 // use this information.
5416 KnownBits Known = computeKnownBits(Op, DemandedElts, Depth);
5417 return std::max(FirstAnswer, Known.countMinSignBits());
5418}
5419
5421 unsigned Depth) const {
5422 unsigned SignBits = ComputeNumSignBits(Op, Depth);
5423 return Op.getScalarValueSizeInBits() - SignBits + 1;
5424}
5425
5427 const APInt &DemandedElts,
5428 unsigned Depth) const {
5429 unsigned SignBits = ComputeNumSignBits(Op, DemandedElts, Depth);
5430 return Op.getScalarValueSizeInBits() - SignBits + 1;
5431}
5432
5434 unsigned Depth) const {
5435 // Early out for FREEZE.
5436 if (Op.getOpcode() == ISD::FREEZE)
5437 return true;
5438
5439 EVT VT = Op.getValueType();
5440 APInt DemandedElts = VT.isFixedLengthVector()
5442 : APInt(1, 1);
5443 return isGuaranteedNotToBeUndefOrPoison(Op, DemandedElts, PoisonOnly, Depth);
5444}
5445
5447 const APInt &DemandedElts,
5448 bool PoisonOnly,
5449 unsigned Depth) const {
5450 unsigned Opcode = Op.getOpcode();
5451
5452 // Early out for FREEZE.
5453 if (Opcode == ISD::FREEZE)
5454 return true;
5455
5456 if (Depth >= MaxRecursionDepth)
5457 return false; // Limit search depth.
5458
5459 if (isIntOrFPConstant(Op))
5460 return true;
5461
5462 switch (Opcode) {
5463 case ISD::CONDCODE:
5464 case ISD::VALUETYPE:
5465 case ISD::FrameIndex:
5467 case ISD::CopyFromReg:
5468 return true;
5469
5470 case ISD::POISON:
5471 return false;
5472
5473 case ISD::UNDEF:
5474 return PoisonOnly;
5475
5476 case ISD::BUILD_VECTOR:
5477 // NOTE: BUILD_VECTOR has implicit truncation of wider scalar elements -
5478 // this shouldn't affect the result.
5479 for (unsigned i = 0, e = Op.getNumOperands(); i < e; ++i) {
5480 if (!DemandedElts[i])
5481 continue;
5483 Depth + 1))
5484 return false;
5485 }
5486 return true;
5487
5489 SDValue Src = Op.getOperand(0);
5490 if (Src.getValueType().isScalableVector())
5491 break;
5492 uint64_t Idx = Op.getConstantOperandVal(1);
5493 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
5494 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
5495 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5496 Depth + 1);
5497 }
5498
5499 case ISD::INSERT_SUBVECTOR: {
5500 if (Op.getValueType().isScalableVector())
5501 break;
5502 SDValue Src = Op.getOperand(0);
5503 SDValue Sub = Op.getOperand(1);
5504 uint64_t Idx = Op.getConstantOperandVal(2);
5505 unsigned NumSubElts = Sub.getValueType().getVectorNumElements();
5506 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
5507 APInt DemandedSrcElts = DemandedElts;
5508 DemandedSrcElts.clearBits(Idx, Idx + NumSubElts);
5509
5510 if (!!DemandedSubElts && !isGuaranteedNotToBeUndefOrPoison(
5511 Sub, DemandedSubElts, PoisonOnly, Depth + 1))
5512 return false;
5513 if (!!DemandedSrcElts && !isGuaranteedNotToBeUndefOrPoison(
5514 Src, DemandedSrcElts, PoisonOnly, Depth + 1))
5515 return false;
5516 return true;
5517 }
5518
5520 SDValue Src = Op.getOperand(0);
5521 auto *IndexC = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5522 EVT SrcVT = Src.getValueType();
5523 if (SrcVT.isFixedLengthVector() && IndexC &&
5524 IndexC->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5525 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5526 IndexC->getZExtValue());
5527 return isGuaranteedNotToBeUndefOrPoison(Src, DemandedSrcElts, PoisonOnly,
5528 Depth + 1);
5529 }
5530 break;
5531 }
5532
5534 SDValue InVec = Op.getOperand(0);
5535 SDValue InVal = Op.getOperand(1);
5536 SDValue EltNo = Op.getOperand(2);
5537 EVT VT = InVec.getValueType();
5538 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
5539 if (IndexC && VT.isFixedLengthVector() &&
5540 IndexC->getAPIntValue().ult(VT.getVectorNumElements())) {
5541 if (DemandedElts[IndexC->getZExtValue()] &&
5543 return false;
5544 APInt InVecDemandedElts = DemandedElts;
5545 InVecDemandedElts.clearBit(IndexC->getZExtValue());
5546 if (!!InVecDemandedElts &&
5548 peekThroughInsertVectorElt(InVec, InVecDemandedElts),
5549 InVecDemandedElts, PoisonOnly, Depth + 1))
5550 return false;
5551 return true;
5552 }
5553 break;
5554 }
5555
5557 // Check upper (known undef) elements.
5558 if (DemandedElts.ugt(1) && !PoisonOnly)
5559 return false;
5560 // Check element zero.
5561 if (DemandedElts[0] && !isGuaranteedNotToBeUndefOrPoison(
5562 Op.getOperand(0), PoisonOnly, Depth + 1))
5563 return false;
5564 return true;
5565
5566 case ISD::SPLAT_VECTOR:
5567 return isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), PoisonOnly,
5568 Depth + 1);
5569
5570 case ISD::VECTOR_SHUFFLE: {
5571 APInt DemandedLHS, DemandedRHS;
5572 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5573 if (!getShuffleDemandedElts(DemandedElts.getBitWidth(), SVN->getMask(),
5574 DemandedElts, DemandedLHS, DemandedRHS,
5575 /*AllowUndefElts=*/false))
5576 return false;
5577 if (!DemandedLHS.isZero() &&
5578 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedLHS,
5579 PoisonOnly, Depth + 1))
5580 return false;
5581 if (!DemandedRHS.isZero() &&
5582 !isGuaranteedNotToBeUndefOrPoison(Op.getOperand(1), DemandedRHS,
5583 PoisonOnly, Depth + 1))
5584 return false;
5585 return true;
5586 }
5587
5588 case ISD::SHL:
5589 case ISD::SRL:
5590 case ISD::SRA:
5591 // Shift amount operand is checked by canCreateUndefOrPoison. So it is
5592 // enough to check operand 0 if Op can't create undef/poison.
5593 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5594 /*ConsiderFlags*/ true, Depth) &&
5595 isGuaranteedNotToBeUndefOrPoison(Op.getOperand(0), DemandedElts,
5596 PoisonOnly, Depth + 1);
5597
5598 case ISD::BSWAP:
5599 case ISD::CTPOP:
5600 case ISD::BITREVERSE:
5601 case ISD::AND:
5602 case ISD::OR:
5603 case ISD::XOR:
5604 case ISD::ADD:
5605 case ISD::SUB:
5606 case ISD::MUL:
5607 case ISD::SADDSAT:
5608 case ISD::UADDSAT:
5609 case ISD::SSUBSAT:
5610 case ISD::USUBSAT:
5611 case ISD::SSHLSAT:
5612 case ISD::USHLSAT:
5613 case ISD::SMIN:
5614 case ISD::SMAX:
5615 case ISD::UMIN:
5616 case ISD::UMAX:
5617 case ISD::ZERO_EXTEND:
5618 case ISD::SIGN_EXTEND:
5619 case ISD::ANY_EXTEND:
5620 case ISD::TRUNCATE:
5621 case ISD::VSELECT: {
5622 // If Op can't create undef/poison and none of its operands are undef/poison
5623 // then Op is never undef/poison. A difference from the more common check
5624 // below, outside the switch, is that we handle elementwise operations for
5625 // which the DemandedElts mask is valid for all operands here.
5626 return !canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly,
5627 /*ConsiderFlags*/ true, Depth) &&
5628 all_of(Op->ops(), [&](SDValue V) {
5629 return isGuaranteedNotToBeUndefOrPoison(V, DemandedElts,
5630 PoisonOnly, Depth + 1);
5631 });
5632 }
5633
5634 // TODO: Search for noundef attributes from library functions.
5635
5636 // TODO: Pointers dereferenced by ISD::LOAD/STORE ops are noundef.
5637
5638 default:
5639 // Allow the target to implement this method for its nodes.
5640 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5641 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5642 return TLI->isGuaranteedNotToBeUndefOrPoisonForTargetNode(
5643 Op, DemandedElts, *this, PoisonOnly, Depth);
5644 break;
5645 }
5646
5647 // If Op can't create undef/poison and none of its operands are undef/poison
5648 // then Op is never undef/poison.
5649 // NOTE: TargetNodes can handle this in themselves in
5650 // isGuaranteedNotToBeUndefOrPoisonForTargetNode or let
5651 // TargetLowering::isGuaranteedNotToBeUndefOrPoisonForTargetNode handle it.
5652 return !canCreateUndefOrPoison(Op, PoisonOnly, /*ConsiderFlags*/ true,
5653 Depth) &&
5654 all_of(Op->ops(), [&](SDValue V) {
5655 return isGuaranteedNotToBeUndefOrPoison(V, PoisonOnly, Depth + 1);
5656 });
5657}
5658
5660 bool ConsiderFlags,
5661 unsigned Depth) const {
5662 EVT VT = Op.getValueType();
5663 APInt DemandedElts = VT.isFixedLengthVector()
5665 : APInt(1, 1);
5666 return canCreateUndefOrPoison(Op, DemandedElts, PoisonOnly, ConsiderFlags,
5667 Depth);
5668}
5669
5671 bool PoisonOnly, bool ConsiderFlags,
5672 unsigned Depth) const {
5673 if (ConsiderFlags && Op->hasPoisonGeneratingFlags())
5674 return true;
5675
5676 unsigned Opcode = Op.getOpcode();
5677 switch (Opcode) {
5678 case ISD::AssertSext:
5679 case ISD::AssertZext:
5680 case ISD::AssertAlign:
5682 // Assertion nodes can create poison if the assertion fails.
5683 return true;
5684
5685 case ISD::FREEZE:
5689 case ISD::SADDSAT:
5690 case ISD::UADDSAT:
5691 case ISD::SSUBSAT:
5692 case ISD::USUBSAT:
5693 case ISD::MULHU:
5694 case ISD::MULHS:
5695 case ISD::AVGFLOORS:
5696 case ISD::AVGFLOORU:
5697 case ISD::AVGCEILS:
5698 case ISD::AVGCEILU:
5699 case ISD::ABDU:
5700 case ISD::ABDS:
5701 case ISD::SMIN:
5702 case ISD::SMAX:
5703 case ISD::SCMP:
5704 case ISD::UMIN:
5705 case ISD::UMAX:
5706 case ISD::UCMP:
5707 case ISD::AND:
5708 case ISD::XOR:
5709 case ISD::ROTL:
5710 case ISD::ROTR:
5711 case ISD::FSHL:
5712 case ISD::FSHR:
5713 case ISD::BSWAP:
5714 case ISD::CTTZ:
5715 case ISD::CTLZ:
5716 case ISD::CTLS:
5717 case ISD::CTPOP:
5718 case ISD::BITREVERSE:
5719 case ISD::PARITY:
5720 case ISD::SIGN_EXTEND:
5721 case ISD::TRUNCATE:
5725 case ISD::BITCAST:
5726 case ISD::BUILD_VECTOR:
5727 case ISD::BUILD_PAIR:
5728 case ISD::SPLAT_VECTOR:
5729 case ISD::FABS:
5730 return false;
5731
5732 case ISD::ABS:
5733 // ISD::ABS defines abs(INT_MIN) -> INT_MIN and never generates poison.
5734 // Different to Intrinsic::abs.
5735 return false;
5736
5737 case ISD::ADDC:
5738 case ISD::SUBC:
5739 case ISD::ADDE:
5740 case ISD::SUBE:
5741 case ISD::SADDO:
5742 case ISD::SSUBO:
5743 case ISD::SMULO:
5744 case ISD::SADDO_CARRY:
5745 case ISD::SSUBO_CARRY:
5746 case ISD::UADDO:
5747 case ISD::USUBO:
5748 case ISD::UMULO:
5749 case ISD::UADDO_CARRY:
5750 case ISD::USUBO_CARRY:
5751 // No poison on result or overflow flags.
5752 return false;
5753
5754 case ISD::SELECT_CC:
5755 case ISD::SETCC: {
5756 // Integer setcc cannot create undef or poison.
5757 if (Op.getOperand(0).getValueType().isInteger())
5758 return false;
5759
5760 // FP compares are more complicated. They can create poison for nan/infinity
5761 // based on options and flags. The options and flags also cause special
5762 // nonan condition codes to be used. Those condition codes may be preserved
5763 // even if the nonan flag is dropped somewhere.
5764 unsigned CCOp = Opcode == ISD::SETCC ? 2 : 4;
5765 ISD::CondCode CCCode = cast<CondCodeSDNode>(Op.getOperand(CCOp))->get();
5766 return (unsigned)CCCode & 0x10U;
5767 }
5768
5769 case ISD::OR:
5770 case ISD::ZERO_EXTEND:
5771 case ISD::SELECT:
5772 case ISD::VSELECT:
5773 case ISD::ADD:
5774 case ISD::SUB:
5775 case ISD::MUL:
5776 case ISD::FNEG:
5777 case ISD::FADD:
5778 case ISD::FSUB:
5779 case ISD::FMUL:
5780 case ISD::FDIV:
5781 case ISD::FREM:
5782 case ISD::FCOPYSIGN:
5783 case ISD::FMA:
5784 case ISD::FMAD:
5785 case ISD::FMULADD:
5786 case ISD::FP_EXTEND:
5792 // No poison except from flags (which is handled above)
5793 return false;
5794
5795 case ISD::SHL:
5796 case ISD::SRL:
5797 case ISD::SRA:
5798 // If the max shift amount isn't in range, then the shift can
5799 // create poison.
5800 return !getValidMaximumShiftAmount(Op, DemandedElts, Depth + 1);
5801
5804 // If the amount is zero then the result will be poison.
5805 // TODO: Add isKnownNeverZero DemandedElts handling.
5806 return !isKnownNeverZero(Op.getOperand(0), Depth + 1);
5807
5809 // Check if we demand any upper (undef) elements.
5810 return !PoisonOnly && DemandedElts.ugt(1);
5811
5814 // Ensure that the element index is in bounds.
5815 EVT VecVT = Op.getOperand(0).getValueType();
5816 SDValue Idx = Op.getOperand(Opcode == ISD::INSERT_VECTOR_ELT ? 2 : 1);
5817 KnownBits KnownIdx = computeKnownBits(Idx, Depth + 1);
5818 return KnownIdx.getMaxValue().uge(VecVT.getVectorMinNumElements());
5819 }
5820
5821 case ISD::VECTOR_SHUFFLE: {
5822 // Check for any demanded shuffle element that is undef.
5823 auto *SVN = cast<ShuffleVectorSDNode>(Op);
5824 for (auto [Idx, Elt] : enumerate(SVN->getMask()))
5825 if (Elt < 0 && DemandedElts[Idx])
5826 return true;
5827 return false;
5828 }
5829
5831 return false;
5832
5833 default:
5834 // Allow the target to implement this method for its nodes.
5835 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
5836 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID)
5837 return TLI->canCreateUndefOrPoisonForTargetNode(
5838 Op, DemandedElts, *this, PoisonOnly, ConsiderFlags, Depth);
5839 break;
5840 }
5841
5842 // Be conservative and return true.
5843 return true;
5844}
5845
5846bool SelectionDAG::isADDLike(SDValue Op, bool NoWrap) const {
5847 unsigned Opcode = Op.getOpcode();
5848 if (Opcode == ISD::OR)
5849 return Op->getFlags().hasDisjoint() ||
5850 haveNoCommonBitsSet(Op.getOperand(0), Op.getOperand(1));
5851 if (Opcode == ISD::XOR)
5852 return !NoWrap && isMinSignedConstant(Op.getOperand(1));
5853 return false;
5854}
5855
5857 return Op.getNumOperands() == 2 && isa<ConstantSDNode>(Op.getOperand(1)) &&
5858 (Op.isAnyAdd() || isADDLike(Op));
5859}
5860
5862 unsigned Depth) const {
5863 EVT VT = Op.getValueType();
5864
5865 // Since the number of lanes in a scalable vector is unknown at compile time,
5866 // we track one bit which is implicitly broadcast to all lanes. This means
5867 // that all lanes in a scalable vector are considered demanded.
5868 APInt DemandedElts = VT.isFixedLengthVector()
5870 : APInt(1, 1);
5871
5872 return isKnownNeverNaN(Op, DemandedElts, SNaN, Depth);
5873}
5874
5876 bool SNaN, unsigned Depth) const {
5877 assert(!DemandedElts.isZero() && "No demanded elements");
5878
5879 // If we're told that NaNs won't happen, assume they won't.
5880 if (getTarget().Options.NoNaNsFPMath || Op->getFlags().hasNoNaNs())
5881 return true;
5882
5883 if (Depth >= MaxRecursionDepth)
5884 return false; // Limit search depth.
5885
5886 // If the value is a constant, we can obviously see if it is a NaN or not.
5888 return !C->getValueAPF().isNaN() ||
5889 (SNaN && !C->getValueAPF().isSignaling());
5890 }
5891
5892 unsigned Opcode = Op.getOpcode();
5893 switch (Opcode) {
5894 case ISD::FADD:
5895 case ISD::FSUB:
5896 case ISD::FMUL:
5897 case ISD::FDIV:
5898 case ISD::FREM:
5899 case ISD::FSIN:
5900 case ISD::FCOS:
5901 case ISD::FTAN:
5902 case ISD::FASIN:
5903 case ISD::FACOS:
5904 case ISD::FATAN:
5905 case ISD::FATAN2:
5906 case ISD::FSINH:
5907 case ISD::FCOSH:
5908 case ISD::FTANH:
5909 case ISD::FMA:
5910 case ISD::FMULADD:
5911 case ISD::FMAD: {
5912 if (SNaN)
5913 return true;
5914 // TODO: Need isKnownNeverInfinity
5915 return false;
5916 }
5917 case ISD::FCANONICALIZE:
5918 case ISD::FEXP:
5919 case ISD::FEXP2:
5920 case ISD::FEXP10:
5921 case ISD::FTRUNC:
5922 case ISD::FFLOOR:
5923 case ISD::FCEIL:
5924 case ISD::FROUND:
5925 case ISD::FROUNDEVEN:
5926 case ISD::LROUND:
5927 case ISD::LLROUND:
5928 case ISD::FRINT:
5929 case ISD::LRINT:
5930 case ISD::LLRINT:
5931 case ISD::FNEARBYINT:
5932 case ISD::FLDEXP: {
5933 if (SNaN)
5934 return true;
5935 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5936 }
5937 case ISD::FABS:
5938 case ISD::FNEG:
5939 case ISD::FCOPYSIGN: {
5940 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5941 }
5942 case ISD::SELECT:
5943 return isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1) &&
5944 isKnownNeverNaN(Op.getOperand(2), DemandedElts, SNaN, Depth + 1);
5945 case ISD::FP_EXTEND:
5946 case ISD::FP_ROUND: {
5947 if (SNaN)
5948 return true;
5949 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
5950 }
5951 case ISD::SINT_TO_FP:
5952 case ISD::UINT_TO_FP:
5953 return true;
5954 case ISD::FSQRT: // Need is known positive
5955 case ISD::FLOG:
5956 case ISD::FLOG2:
5957 case ISD::FLOG10:
5958 case ISD::FPOWI:
5959 case ISD::FPOW: {
5960 if (SNaN)
5961 return true;
5962 // TODO: Refine on operand
5963 return false;
5964 }
5965 case ISD::FMINNUM:
5966 case ISD::FMAXNUM:
5967 case ISD::FMINIMUMNUM:
5968 case ISD::FMAXIMUMNUM: {
5969 // Only one needs to be known not-nan, since it will be returned if the
5970 // other ends up being one.
5971 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) ||
5972 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5973 }
5974 case ISD::FMINNUM_IEEE:
5975 case ISD::FMAXNUM_IEEE: {
5976 if (SNaN)
5977 return true;
5978 // This can return a NaN if either operand is an sNaN, or if both operands
5979 // are NaN.
5980 return (isKnownNeverNaN(Op.getOperand(0), DemandedElts, false, Depth + 1) &&
5981 isKnownNeverSNaN(Op.getOperand(1), DemandedElts, Depth + 1)) ||
5982 (isKnownNeverNaN(Op.getOperand(1), DemandedElts, false, Depth + 1) &&
5983 isKnownNeverSNaN(Op.getOperand(0), DemandedElts, Depth + 1));
5984 }
5985 case ISD::FMINIMUM:
5986 case ISD::FMAXIMUM: {
5987 // TODO: Does this quiet or return the origina NaN as-is?
5988 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1) &&
5989 isKnownNeverNaN(Op.getOperand(1), DemandedElts, SNaN, Depth + 1);
5990 }
5992 SDValue Src = Op.getOperand(0);
5993 auto *Idx = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5994 EVT SrcVT = Src.getValueType();
5995 if (SrcVT.isFixedLengthVector() && Idx &&
5996 Idx->getAPIntValue().ult(SrcVT.getVectorNumElements())) {
5997 APInt DemandedSrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(),
5998 Idx->getZExtValue());
5999 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6000 }
6001 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6002 }
6004 SDValue Src = Op.getOperand(0);
6005 if (Src.getValueType().isFixedLengthVector()) {
6006 unsigned Idx = Op.getConstantOperandVal(1);
6007 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
6008 APInt DemandedSrcElts = DemandedElts.zext(NumSrcElts).shl(Idx);
6009 return isKnownNeverNaN(Src, DemandedSrcElts, SNaN, Depth + 1);
6010 }
6011 return isKnownNeverNaN(Src, SNaN, Depth + 1);
6012 }
6013 case ISD::INSERT_SUBVECTOR: {
6014 SDValue BaseVector = Op.getOperand(0);
6015 SDValue SubVector = Op.getOperand(1);
6016 EVT BaseVectorVT = BaseVector.getValueType();
6017 if (BaseVectorVT.isFixedLengthVector()) {
6018 unsigned Idx = Op.getConstantOperandVal(2);
6019 unsigned NumBaseElts = BaseVectorVT.getVectorNumElements();
6020 unsigned NumSubElts = SubVector.getValueType().getVectorNumElements();
6021
6022 // Clear/Extract the bits at the position where the subvector will be
6023 // inserted.
6024 APInt DemandedMask =
6025 APInt::getBitsSet(NumBaseElts, Idx, Idx + NumSubElts);
6026 APInt DemandedSrcElts = DemandedElts & ~DemandedMask;
6027 APInt DemandedSubElts = DemandedElts.extractBits(NumSubElts, Idx);
6028
6029 bool NeverNaN = true;
6030 if (!DemandedSrcElts.isZero())
6031 NeverNaN &=
6032 isKnownNeverNaN(BaseVector, DemandedSrcElts, SNaN, Depth + 1);
6033 if (NeverNaN && !DemandedSubElts.isZero())
6034 NeverNaN &=
6035 isKnownNeverNaN(SubVector, DemandedSubElts, SNaN, Depth + 1);
6036 return NeverNaN;
6037 }
6038 return isKnownNeverNaN(BaseVector, SNaN, Depth + 1) &&
6039 isKnownNeverNaN(SubVector, SNaN, Depth + 1);
6040 }
6041 case ISD::BUILD_VECTOR: {
6042 unsigned NumElts = Op.getNumOperands();
6043 for (unsigned I = 0; I != NumElts; ++I)
6044 if (DemandedElts[I] &&
6045 !isKnownNeverNaN(Op.getOperand(I), SNaN, Depth + 1))
6046 return false;
6047 return true;
6048 }
6049 case ISD::AssertNoFPClass: {
6050 FPClassTest NoFPClass =
6051 static_cast<FPClassTest>(Op.getConstantOperandVal(1));
6052 if ((NoFPClass & fcNan) == fcNan)
6053 return true;
6054 if (SNaN && (NoFPClass & fcSNan) == fcSNan)
6055 return true;
6056 return isKnownNeverNaN(Op.getOperand(0), DemandedElts, SNaN, Depth + 1);
6057 }
6058 default:
6059 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::INTRINSIC_WO_CHAIN ||
6060 Opcode == ISD::INTRINSIC_W_CHAIN || Opcode == ISD::INTRINSIC_VOID) {
6061 return TLI->isKnownNeverNaNForTargetNode(Op, DemandedElts, *this, SNaN,
6062 Depth);
6063 }
6064
6065 return false;
6066 }
6067}
6068
6070 assert(Op.getValueType().isFloatingPoint() &&
6071 "Floating point type expected");
6072
6073 // If the value is a constant, we can obviously see if it is a zero or not.
6075 Op, [](ConstantFPSDNode *C) { return !C->isZero(); });
6076}
6077
6079 if (Depth >= MaxRecursionDepth)
6080 return false; // Limit search depth.
6081
6082 assert(!Op.getValueType().isFloatingPoint() &&
6083 "Floating point types unsupported - use isKnownNeverZeroFloat");
6084
6085 // If the value is a constant, we can obviously see if it is a zero or not.
6087 [](ConstantSDNode *C) { return !C->isZero(); }))
6088 return true;
6089
6090 // TODO: Recognize more cases here. Most of the cases are also incomplete to
6091 // some degree.
6092 switch (Op.getOpcode()) {
6093 default:
6094 break;
6095
6096 case ISD::OR:
6097 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6098 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6099
6100 case ISD::VSELECT:
6101 case ISD::SELECT:
6102 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6103 isKnownNeverZero(Op.getOperand(2), Depth + 1);
6104
6105 case ISD::SHL: {
6106 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6107 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6108 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6109 // 1 << X is never zero.
6110 if (ValKnown.One[0])
6111 return true;
6112 // If max shift cnt of known ones is non-zero, result is non-zero.
6113 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6114 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6115 !ValKnown.One.shl(MaxCnt).isZero())
6116 return true;
6117 break;
6118 }
6119 case ISD::UADDSAT:
6120 case ISD::UMAX:
6121 return isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6122 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6123
6124 // For smin/smax: If either operand is known negative/positive
6125 // respectively we don't need the other to be known at all.
6126 case ISD::SMAX: {
6127 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6128 if (Op1.isStrictlyPositive())
6129 return true;
6130
6131 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6132 if (Op0.isStrictlyPositive())
6133 return true;
6134
6135 if (Op1.isNonZero() && Op0.isNonZero())
6136 return true;
6137
6138 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6139 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6140 }
6141 case ISD::SMIN: {
6142 KnownBits Op1 = computeKnownBits(Op.getOperand(1), Depth + 1);
6143 if (Op1.isNegative())
6144 return true;
6145
6146 KnownBits Op0 = computeKnownBits(Op.getOperand(0), Depth + 1);
6147 if (Op0.isNegative())
6148 return true;
6149
6150 if (Op1.isNonZero() && Op0.isNonZero())
6151 return true;
6152
6153 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6154 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6155 }
6156 case ISD::UMIN:
6157 return isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6158 isKnownNeverZero(Op.getOperand(0), Depth + 1);
6159
6160 case ISD::ROTL:
6161 case ISD::ROTR:
6162 case ISD::BITREVERSE:
6163 case ISD::BSWAP:
6164 case ISD::CTPOP:
6165 case ISD::ABS:
6166 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6167
6168 case ISD::SRA:
6169 case ISD::SRL: {
6170 if (Op->getFlags().hasExact())
6171 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6172 KnownBits ValKnown = computeKnownBits(Op.getOperand(0), Depth + 1);
6173 if (ValKnown.isNegative())
6174 return true;
6175 // If max shift cnt of known ones is non-zero, result is non-zero.
6176 APInt MaxCnt = computeKnownBits(Op.getOperand(1), Depth + 1).getMaxValue();
6177 if (MaxCnt.ult(ValKnown.getBitWidth()) &&
6178 !ValKnown.One.lshr(MaxCnt).isZero())
6179 return true;
6180 break;
6181 }
6182 case ISD::UDIV:
6183 case ISD::SDIV:
6184 // div exact can only produce a zero if the dividend is zero.
6185 // TODO: For udiv this is also true if Op1 u<= Op0
6186 if (Op->getFlags().hasExact())
6187 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6188 break;
6189
6190 case ISD::ADD:
6191 if (Op->getFlags().hasNoUnsignedWrap())
6192 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) ||
6193 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6194 return true;
6195 // TODO: There are a lot more cases we can prove for add.
6196 break;
6197
6198 case ISD::SUB: {
6199 if (isNullConstant(Op.getOperand(0)))
6200 return isKnownNeverZero(Op.getOperand(1), Depth + 1);
6201
6202 std::optional<bool> ne =
6203 KnownBits::ne(computeKnownBits(Op.getOperand(0), Depth + 1),
6204 computeKnownBits(Op.getOperand(1), Depth + 1));
6205 return ne && *ne;
6206 }
6207
6208 case ISD::MUL:
6209 if (Op->getFlags().hasNoSignedWrap() || Op->getFlags().hasNoUnsignedWrap())
6210 if (isKnownNeverZero(Op.getOperand(1), Depth + 1) &&
6211 isKnownNeverZero(Op.getOperand(0), Depth + 1))
6212 return true;
6213 break;
6214
6215 case ISD::ZERO_EXTEND:
6216 case ISD::SIGN_EXTEND:
6217 return isKnownNeverZero(Op.getOperand(0), Depth + 1);
6218 case ISD::VSCALE: {
6220 const APInt &Multiplier = Op.getConstantOperandAPInt(0);
6221 ConstantRange CR =
6222 getVScaleRange(&F, Op.getScalarValueSizeInBits()).multiply(Multiplier);
6223 if (!CR.contains(APInt(CR.getBitWidth(), 0)))
6224 return true;
6225 break;
6226 }
6227 }
6228
6230}
6231
6233 if (ConstantFPSDNode *C1 = isConstOrConstSplatFP(Op, true))
6234 return !C1->isNegative();
6235
6236 switch (Op.getOpcode()) {
6237 case ISD::FABS:
6238 case ISD::FEXP:
6239 case ISD::FEXP2:
6240 case ISD::FEXP10:
6241 return true;
6242 default:
6243 return false;
6244 }
6245
6246 llvm_unreachable("covered opcode switch");
6247}
6248
6250 assert(Use.getValueType().isFloatingPoint());
6251 const SDNode *User = Use.getUser();
6252 if (User->getFlags().hasNoSignedZeros())
6253 return true;
6254
6255 unsigned OperandNo = Use.getOperandNo();
6256 // Check if this use is insensitive to the sign of zero
6257 switch (User->getOpcode()) {
6258 case ISD::SETCC:
6259 // Comparisons: IEEE-754 specifies +0.0 == -0.0.
6260 case ISD::FABS:
6261 // fabs always produces +0.0.
6262 return true;
6263 case ISD::FCOPYSIGN:
6264 // copysign overwrites the sign bit of the first operand.
6265 return OperandNo == 0;
6266 case ISD::FADD:
6267 case ISD::FSUB: {
6268 // Arithmetic with non-zero constants fixes the uncertainty around the
6269 // sign bit.
6270 SDValue Other = User->getOperand(1 - OperandNo);
6272 }
6273 case ISD::FP_TO_SINT:
6274 case ISD::FP_TO_UINT:
6275 // fp-to-int conversions normalize signed zeros.
6276 return true;
6277 default:
6278 return false;
6279 }
6280}
6281
6283 if (Op->getFlags().hasNoSignedZeros())
6284 return true;
6285 // FIXME: Limit the amount of checked uses to not introduce a compile-time
6286 // regression. Ideally, this should be implemented as a demanded-bits
6287 // optimization that stems from the users.
6288 if (Op->use_size() > 2)
6289 return false;
6290 return all_of(Op->uses(),
6291 [&](const SDUse &Use) { return canIgnoreSignBitOfZero(Use); });
6292}
6293
6295 // Check the obvious case.
6296 if (A == B) return true;
6297
6298 // For negative and positive zero.
6301 if (CA->isZero() && CB->isZero()) return true;
6302
6303 // Otherwise they may not be equal.
6304 return false;
6305}
6306
6307// Only bits set in Mask must be negated, other bits may be arbitrary.
6309 if (isBitwiseNot(V, AllowUndefs))
6310 return V.getOperand(0);
6311
6312 // Handle any_extend (not (truncate X)) pattern, where Mask only sets
6313 // bits in the non-extended part.
6314 ConstantSDNode *MaskC = isConstOrConstSplat(Mask);
6315 if (!MaskC || V.getOpcode() != ISD::ANY_EXTEND)
6316 return SDValue();
6317 SDValue ExtArg = V.getOperand(0);
6318 if (ExtArg.getScalarValueSizeInBits() >=
6319 MaskC->getAPIntValue().getActiveBits() &&
6320 isBitwiseNot(ExtArg, AllowUndefs) &&
6321 ExtArg.getOperand(0).getOpcode() == ISD::TRUNCATE &&
6322 ExtArg.getOperand(0).getOperand(0).getValueType() == V.getValueType())
6323 return ExtArg.getOperand(0).getOperand(0);
6324 return SDValue();
6325}
6326
6328 // Match masked merge pattern (X & ~M) op (Y & M)
6329 // Including degenerate case (X & ~M) op M
6330 auto MatchNoCommonBitsPattern = [&](SDValue Not, SDValue Mask,
6331 SDValue Other) {
6332 if (SDValue NotOperand =
6333 getBitwiseNotOperand(Not, Mask, /* AllowUndefs */ true)) {
6334 if (NotOperand->getOpcode() == ISD::ZERO_EXTEND ||
6335 NotOperand->getOpcode() == ISD::TRUNCATE)
6336 NotOperand = NotOperand->getOperand(0);
6337
6338 if (Other == NotOperand)
6339 return true;
6340 if (Other->getOpcode() == ISD::AND)
6341 return NotOperand == Other->getOperand(0) ||
6342 NotOperand == Other->getOperand(1);
6343 }
6344 return false;
6345 };
6346
6347 if (A->getOpcode() == ISD::ZERO_EXTEND || A->getOpcode() == ISD::TRUNCATE)
6348 A = A->getOperand(0);
6349
6350 if (B->getOpcode() == ISD::ZERO_EXTEND || B->getOpcode() == ISD::TRUNCATE)
6351 B = B->getOperand(0);
6352
6353 if (A->getOpcode() == ISD::AND)
6354 return MatchNoCommonBitsPattern(A->getOperand(0), A->getOperand(1), B) ||
6355 MatchNoCommonBitsPattern(A->getOperand(1), A->getOperand(0), B);
6356 return false;
6357}
6358
6359// FIXME: unify with llvm::haveNoCommonBitsSet.
6361 assert(A.getValueType() == B.getValueType() &&
6362 "Values must have the same type");
6365 return true;
6368}
6369
6370static SDValue FoldSTEP_VECTOR(const SDLoc &DL, EVT VT, SDValue Step,
6371 SelectionDAG &DAG) {
6372 if (cast<ConstantSDNode>(Step)->isZero())
6373 return DAG.getConstant(0, DL, VT);
6374
6375 return SDValue();
6376}
6377
6380 SelectionDAG &DAG) {
6381 int NumOps = Ops.size();
6382 assert(NumOps != 0 && "Can't build an empty vector!");
6383 assert(!VT.isScalableVector() &&
6384 "BUILD_VECTOR cannot be used with scalable types");
6385 assert(VT.getVectorNumElements() == (unsigned)NumOps &&
6386 "Incorrect element count in BUILD_VECTOR!");
6387
6388 // BUILD_VECTOR of UNDEFs is UNDEF.
6389 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6390 return DAG.getUNDEF(VT);
6391
6392 // BUILD_VECTOR of seq extract/insert from the same vector + type is Identity.
6393 SDValue IdentitySrc;
6394 bool IsIdentity = true;
6395 for (int i = 0; i != NumOps; ++i) {
6397 Ops[i].getOperand(0).getValueType() != VT ||
6398 (IdentitySrc && Ops[i].getOperand(0) != IdentitySrc) ||
6399 !isa<ConstantSDNode>(Ops[i].getOperand(1)) ||
6400 Ops[i].getConstantOperandAPInt(1) != i) {
6401 IsIdentity = false;
6402 break;
6403 }
6404 IdentitySrc = Ops[i].getOperand(0);
6405 }
6406 if (IsIdentity)
6407 return IdentitySrc;
6408
6409 return SDValue();
6410}
6411
6412/// Try to simplify vector concatenation to an input value, undef, or build
6413/// vector.
6416 SelectionDAG &DAG) {
6417 assert(!Ops.empty() && "Can't concatenate an empty list of vectors!");
6419 [Ops](SDValue Op) {
6420 return Ops[0].getValueType() == Op.getValueType();
6421 }) &&
6422 "Concatenation of vectors with inconsistent value types!");
6423 assert((Ops[0].getValueType().getVectorElementCount() * Ops.size()) ==
6424 VT.getVectorElementCount() &&
6425 "Incorrect element count in vector concatenation!");
6426
6427 if (Ops.size() == 1)
6428 return Ops[0];
6429
6430 // Concat of UNDEFs is UNDEF.
6431 if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); }))
6432 return DAG.getUNDEF(VT);
6433
6434 // Scan the operands and look for extract operations from a single source
6435 // that correspond to insertion at the same location via this concatenation:
6436 // concat (extract X, 0*subvec_elts), (extract X, 1*subvec_elts), ...
6437 SDValue IdentitySrc;
6438 bool IsIdentity = true;
6439 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
6440 SDValue Op = Ops[i];
6441 unsigned IdentityIndex = i * Op.getValueType().getVectorMinNumElements();
6442 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
6443 Op.getOperand(0).getValueType() != VT ||
6444 (IdentitySrc && Op.getOperand(0) != IdentitySrc) ||
6445 Op.getConstantOperandVal(1) != IdentityIndex) {
6446 IsIdentity = false;
6447 break;
6448 }
6449 assert((!IdentitySrc || IdentitySrc == Op.getOperand(0)) &&
6450 "Unexpected identity source vector for concat of extracts");
6451 IdentitySrc = Op.getOperand(0);
6452 }
6453 if (IsIdentity) {
6454 assert(IdentitySrc && "Failed to set source vector of extracts");
6455 return IdentitySrc;
6456 }
6457
6458 // The code below this point is only designed to work for fixed width
6459 // vectors, so we bail out for now.
6460 if (VT.isScalableVector())
6461 return SDValue();
6462
6463 // A CONCAT_VECTOR of scalar sources, such as UNDEF, BUILD_VECTOR and
6464 // single-element INSERT_VECTOR_ELT operands can be simplified to one big
6465 // BUILD_VECTOR.
6466 // FIXME: Add support for SCALAR_TO_VECTOR as well.
6467 EVT SVT = VT.getScalarType();
6469 for (SDValue Op : Ops) {
6470 EVT OpVT = Op.getValueType();
6471 if (Op.isUndef())
6472 Elts.append(OpVT.getVectorNumElements(), DAG.getUNDEF(SVT));
6473 else if (Op.getOpcode() == ISD::BUILD_VECTOR)
6474 Elts.append(Op->op_begin(), Op->op_end());
6475 else if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT &&
6476 OpVT.getVectorNumElements() == 1 &&
6477 isNullConstant(Op.getOperand(2)))
6478 Elts.push_back(Op.getOperand(1));
6479 else
6480 return SDValue();
6481 }
6482
6483 // BUILD_VECTOR requires all inputs to be of the same type, find the
6484 // maximum type and extend them all.
6485 for (SDValue Op : Elts)
6486 SVT = (SVT.bitsLT(Op.getValueType()) ? Op.getValueType() : SVT);
6487
6488 if (SVT.bitsGT(VT.getScalarType())) {
6489 for (SDValue &Op : Elts) {
6490 if (Op.isUndef())
6491 Op = DAG.getUNDEF(SVT);
6492 else
6493 Op = DAG.getTargetLoweringInfo().isZExtFree(Op.getValueType(), SVT)
6494 ? DAG.getZExtOrTrunc(Op, DL, SVT)
6495 : DAG.getSExtOrTrunc(Op, DL, SVT);
6496 }
6497 }
6498
6499 SDValue V = DAG.getBuildVector(VT, DL, Elts);
6500 NewSDValueDbgMsg(V, "New node fold concat vectors: ", &DAG);
6501 return V;
6502}
6503
6504/// Gets or creates the specified node.
6505SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT) {
6506 SDVTList VTs = getVTList(VT);
6508 AddNodeIDNode(ID, Opcode, VTs, {});
6509 void *IP = nullptr;
6510 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
6511 return SDValue(E, 0);
6512
6513 auto *N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6514 CSEMap.InsertNode(N, IP);
6515
6516 InsertNode(N);
6517 SDValue V = SDValue(N, 0);
6518 NewSDValueDbgMsg(V, "Creating new node: ", this);
6519 return V;
6520}
6521
6522SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6523 SDValue N1) {
6524 SDNodeFlags Flags;
6525 if (Inserter)
6526 Flags = Inserter->getFlags();
6527 return getNode(Opcode, DL, VT, N1, Flags);
6528}
6529
6530SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
6531 SDValue N1, const SDNodeFlags Flags) {
6532 assert(N1.getOpcode() != ISD::DELETED_NODE && "Operand is DELETED_NODE!");
6533
6534 // Constant fold unary operations with a vector integer or float operand.
6535 switch (Opcode) {
6536 default:
6537 // FIXME: Entirely reasonable to perform folding of other unary
6538 // operations here as the need arises.
6539 break;
6540 case ISD::FNEG:
6541 case ISD::FABS:
6542 case ISD::FCEIL:
6543 case ISD::FTRUNC:
6544 case ISD::FFLOOR:
6545 case ISD::FP_EXTEND:
6546 case ISD::FP_TO_SINT:
6547 case ISD::FP_TO_UINT:
6548 case ISD::FP_TO_FP16:
6549 case ISD::FP_TO_BF16:
6550 case ISD::TRUNCATE:
6551 case ISD::ANY_EXTEND:
6552 case ISD::ZERO_EXTEND:
6553 case ISD::SIGN_EXTEND:
6554 case ISD::UINT_TO_FP:
6555 case ISD::SINT_TO_FP:
6556 case ISD::FP16_TO_FP:
6557 case ISD::BF16_TO_FP:
6558 case ISD::BITCAST:
6559 case ISD::ABS:
6560 case ISD::BITREVERSE:
6561 case ISD::BSWAP:
6562 case ISD::CTLZ:
6564 case ISD::CTTZ:
6566 case ISD::CTPOP:
6567 case ISD::CTLS:
6568 case ISD::STEP_VECTOR: {
6569 SDValue Ops = {N1};
6570 if (SDValue Fold = FoldConstantArithmetic(Opcode, DL, VT, Ops))
6571 return Fold;
6572 }
6573 }
6574
6575 unsigned OpOpcode = N1.getNode()->getOpcode();
6576 switch (Opcode) {
6577 case ISD::STEP_VECTOR:
6578 assert(VT.isScalableVector() &&
6579 "STEP_VECTOR can only be used with scalable types");
6580 assert(OpOpcode == ISD::TargetConstant &&
6581 VT.getVectorElementType() == N1.getValueType() &&
6582 "Unexpected step operand");
6583 break;
6584 case ISD::FREEZE:
6585 assert(VT == N1.getValueType() && "Unexpected VT!");
6586 if (isGuaranteedNotToBeUndefOrPoison(N1, /*PoisonOnly=*/false))
6587 return N1;
6588 break;
6589 case ISD::TokenFactor:
6590 case ISD::MERGE_VALUES:
6592 return N1; // Factor, merge or concat of one node? No need.
6593 case ISD::BUILD_VECTOR: {
6594 // Attempt to simplify BUILD_VECTOR.
6595 SDValue Ops[] = {N1};
6596 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
6597 return V;
6598 break;
6599 }
6600 case ISD::FP_ROUND: llvm_unreachable("Invalid method to make FP_ROUND node");
6601 case ISD::FP_EXTEND:
6603 "Invalid FP cast!");
6604 if (N1.getValueType() == VT) return N1; // noop conversion.
6605 assert((!VT.isVector() || VT.getVectorElementCount() ==
6607 "Vector element count mismatch!");
6608 assert(N1.getValueType().bitsLT(VT) && "Invalid fpext node, dst < src!");
6609 if (N1.isUndef())
6610 return getUNDEF(VT);
6611 break;
6612 case ISD::FP_TO_SINT:
6613 case ISD::FP_TO_UINT:
6614 if (N1.isUndef())
6615 return getUNDEF(VT);
6616 break;
6617 case ISD::SINT_TO_FP:
6618 case ISD::UINT_TO_FP:
6619 // [us]itofp(undef) = 0, because the result value is bounded.
6620 if (N1.isUndef())
6621 return getConstantFP(0.0, DL, VT);
6622 break;
6623 case ISD::SIGN_EXTEND:
6624 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6625 "Invalid SIGN_EXTEND!");
6626 assert(VT.isVector() == N1.getValueType().isVector() &&
6627 "SIGN_EXTEND result type type should be vector iff the operand "
6628 "type is vector!");
6629 if (N1.getValueType() == VT) return N1; // noop extension
6630 assert((!VT.isVector() || VT.getVectorElementCount() ==
6632 "Vector element count mismatch!");
6633 assert(N1.getValueType().bitsLT(VT) && "Invalid sext node, dst < src!");
6634 if (OpOpcode == ISD::SIGN_EXTEND || OpOpcode == ISD::ZERO_EXTEND) {
6635 SDNodeFlags Flags;
6636 if (OpOpcode == ISD::ZERO_EXTEND)
6637 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6638 SDValue NewVal = getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6639 transferDbgValues(N1, NewVal);
6640 return NewVal;
6641 }
6642
6643 if (OpOpcode == ISD::POISON)
6644 return getPOISON(VT);
6645
6646 if (N1.isUndef())
6647 // sext(undef) = 0, because the top bits will all be the same.
6648 return getConstant(0, DL, VT);
6649
6650 // Skip unnecessary sext_inreg pattern:
6651 // (sext (trunc x)) -> x iff the upper bits are all signbits.
6652 if (OpOpcode == ISD::TRUNCATE) {
6653 SDValue OpOp = N1.getOperand(0);
6654 if (OpOp.getValueType() == VT) {
6655 unsigned NumSignExtBits =
6657 if (ComputeNumSignBits(OpOp) > NumSignExtBits) {
6658 transferDbgValues(N1, OpOp);
6659 return OpOp;
6660 }
6661 }
6662 }
6663 break;
6664 case ISD::ZERO_EXTEND:
6665 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6666 "Invalid ZERO_EXTEND!");
6667 assert(VT.isVector() == N1.getValueType().isVector() &&
6668 "ZERO_EXTEND result type type should be vector iff the operand "
6669 "type is vector!");
6670 if (N1.getValueType() == VT) return N1; // noop extension
6671 assert((!VT.isVector() || VT.getVectorElementCount() ==
6673 "Vector element count mismatch!");
6674 assert(N1.getValueType().bitsLT(VT) && "Invalid zext node, dst < src!");
6675 if (OpOpcode == ISD::ZERO_EXTEND) { // (zext (zext x)) -> (zext x)
6676 SDNodeFlags Flags;
6677 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6678 SDValue NewVal =
6679 getNode(ISD::ZERO_EXTEND, DL, VT, N1.getOperand(0), Flags);
6680 transferDbgValues(N1, NewVal);
6681 return NewVal;
6682 }
6683
6684 if (OpOpcode == ISD::POISON)
6685 return getPOISON(VT);
6686
6687 if (N1.isUndef())
6688 // zext(undef) = 0, because the top bits will be zero.
6689 return getConstant(0, DL, VT);
6690
6691 // Skip unnecessary zext_inreg pattern:
6692 // (zext (trunc x)) -> x iff the upper bits are known zero.
6693 // TODO: Remove (zext (trunc (and x, c))) exception which some targets
6694 // use to recognise zext_inreg patterns.
6695 if (OpOpcode == ISD::TRUNCATE) {
6696 SDValue OpOp = N1.getOperand(0);
6697 if (OpOp.getValueType() == VT) {
6698 if (OpOp.getOpcode() != ISD::AND) {
6701 if (MaskedValueIsZero(OpOp, HiBits)) {
6702 transferDbgValues(N1, OpOp);
6703 return OpOp;
6704 }
6705 }
6706 }
6707 }
6708 break;
6709 case ISD::ANY_EXTEND:
6710 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6711 "Invalid ANY_EXTEND!");
6712 assert(VT.isVector() == N1.getValueType().isVector() &&
6713 "ANY_EXTEND result type type should be vector iff the operand "
6714 "type is vector!");
6715 if (N1.getValueType() == VT) return N1; // noop extension
6716 assert((!VT.isVector() || VT.getVectorElementCount() ==
6718 "Vector element count mismatch!");
6719 assert(N1.getValueType().bitsLT(VT) && "Invalid anyext node, dst < src!");
6720
6721 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6722 OpOpcode == ISD::ANY_EXTEND) {
6723 SDNodeFlags Flags;
6724 if (OpOpcode == ISD::ZERO_EXTEND)
6725 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6726 // (ext (zext x)) -> (zext x) and (ext (sext x)) -> (sext x)
6727 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6728 }
6729 if (N1.isUndef())
6730 return getUNDEF(VT);
6731
6732 // (ext (trunc x)) -> x
6733 if (OpOpcode == ISD::TRUNCATE) {
6734 SDValue OpOp = N1.getOperand(0);
6735 if (OpOp.getValueType() == VT) {
6736 transferDbgValues(N1, OpOp);
6737 return OpOp;
6738 }
6739 }
6740 break;
6741 case ISD::TRUNCATE:
6742 assert(VT.isInteger() && N1.getValueType().isInteger() &&
6743 "Invalid TRUNCATE!");
6744 assert(VT.isVector() == N1.getValueType().isVector() &&
6745 "TRUNCATE result type type should be vector iff the operand "
6746 "type is vector!");
6747 if (N1.getValueType() == VT) return N1; // noop truncate
6748 assert((!VT.isVector() || VT.getVectorElementCount() ==
6750 "Vector element count mismatch!");
6751 assert(N1.getValueType().bitsGT(VT) && "Invalid truncate node, src < dst!");
6752 if (OpOpcode == ISD::TRUNCATE)
6753 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6754 if (OpOpcode == ISD::ZERO_EXTEND || OpOpcode == ISD::SIGN_EXTEND ||
6755 OpOpcode == ISD::ANY_EXTEND) {
6756 // If the source is smaller than the dest, we still need an extend.
6758 VT.getScalarType())) {
6759 SDNodeFlags Flags;
6760 if (OpOpcode == ISD::ZERO_EXTEND)
6761 Flags.setNonNeg(N1->getFlags().hasNonNeg());
6762 return getNode(OpOpcode, DL, VT, N1.getOperand(0), Flags);
6763 }
6764 if (N1.getOperand(0).getValueType().bitsGT(VT))
6765 return getNode(ISD::TRUNCATE, DL, VT, N1.getOperand(0));
6766 return N1.getOperand(0);
6767 }
6768 if (N1.isUndef())
6769 return getUNDEF(VT);
6770 if (OpOpcode == ISD::VSCALE && !NewNodesMustHaveLegalTypes)
6771 return getVScale(DL, VT,
6773 break;
6777 assert(VT.isVector() && "This DAG node is restricted to vector types.");
6778 assert(N1.getValueType().bitsLE(VT) &&
6779 "The input must be the same size or smaller than the result.");
6782 "The destination vector type must have fewer lanes than the input.");
6783 break;
6784 case ISD::ABS:
6785 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid ABS!");
6786 if (N1.isUndef())
6787 return getConstant(0, DL, VT);
6788 break;
6789 case ISD::BSWAP:
6790 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BSWAP!");
6791 assert((VT.getScalarSizeInBits() % 16 == 0) &&
6792 "BSWAP types must be a multiple of 16 bits!");
6793 if (N1.isUndef())
6794 return getUNDEF(VT);
6795 // bswap(bswap(X)) -> X.
6796 if (OpOpcode == ISD::BSWAP)
6797 return N1.getOperand(0);
6798 break;
6799 case ISD::BITREVERSE:
6800 assert(VT.isInteger() && VT == N1.getValueType() && "Invalid BITREVERSE!");
6801 if (N1.isUndef())
6802 return getUNDEF(VT);
6803 break;
6804 case ISD::BITCAST:
6806 "Cannot BITCAST between types of different sizes!");
6807 if (VT == N1.getValueType()) return N1; // noop conversion.
6808 if (OpOpcode == ISD::BITCAST) // bitconv(bitconv(x)) -> bitconv(x)
6809 return getNode(ISD::BITCAST, DL, VT, N1.getOperand(0));
6810 if (N1.isUndef())
6811 return getUNDEF(VT);
6812 break;
6814 assert(VT.isVector() && !N1.getValueType().isVector() &&
6815 (VT.getVectorElementType() == N1.getValueType() ||
6817 N1.getValueType().isInteger() &&
6819 "Illegal SCALAR_TO_VECTOR node!");
6820 if (N1.isUndef())
6821 return getUNDEF(VT);
6822 // scalar_to_vector(extract_vector_elt V, 0) -> V, top bits are undefined.
6823 if (OpOpcode == ISD::EXTRACT_VECTOR_ELT &&
6825 N1.getConstantOperandVal(1) == 0 &&
6826 N1.getOperand(0).getValueType() == VT)
6827 return N1.getOperand(0);
6828 break;
6829 case ISD::FNEG:
6830 // Negation of an unknown bag of bits is still completely undefined.
6831 if (N1.isUndef())
6832 return getUNDEF(VT);
6833
6834 if (OpOpcode == ISD::FNEG) // --X -> X
6835 return N1.getOperand(0);
6836 break;
6837 case ISD::FABS:
6838 if (OpOpcode == ISD::FNEG) // abs(-X) -> abs(X)
6839 return getNode(ISD::FABS, DL, VT, N1.getOperand(0));
6840 break;
6841 case ISD::VSCALE:
6842 assert(VT == N1.getValueType() && "Unexpected VT!");
6843 break;
6844 case ISD::CTPOP:
6845 if (N1.getValueType().getScalarType() == MVT::i1)
6846 return N1;
6847 break;
6848 case ISD::CTLZ:
6849 case ISD::CTTZ:
6850 if (N1.getValueType().getScalarType() == MVT::i1)
6851 return getNOT(DL, N1, N1.getValueType());
6852 break;
6853 case ISD::CTLS:
6854 if (N1.getValueType().getScalarType() == MVT::i1)
6855 return getConstant(0, DL, VT);
6856 break;
6857 case ISD::VECREDUCE_ADD:
6858 if (N1.getValueType().getScalarType() == MVT::i1)
6859 return getNode(ISD::VECREDUCE_XOR, DL, VT, N1);
6860 break;
6863 if (N1.getValueType().getScalarType() == MVT::i1)
6864 return getNode(ISD::VECREDUCE_OR, DL, VT, N1);
6865 break;
6868 if (N1.getValueType().getScalarType() == MVT::i1)
6869 return getNode(ISD::VECREDUCE_AND, DL, VT, N1);
6870 break;
6871 case ISD::SPLAT_VECTOR:
6872 assert(VT.isVector() && "Wrong return type!");
6873 // FIXME: Hexagon uses i32 scalar for a floating point zero vector so allow
6874 // that for now.
6876 (VT.isFloatingPoint() && N1.getValueType() == MVT::i32) ||
6878 N1.getValueType().isInteger() &&
6880 "Wrong operand type!");
6881 break;
6882 }
6883
6884 SDNode *N;
6885 SDVTList VTs = getVTList(VT);
6886 SDValue Ops[] = {N1};
6887 if (VT != MVT::Glue) { // Don't CSE glue producing nodes
6889 AddNodeIDNode(ID, Opcode, VTs, Ops);
6890 void *IP = nullptr;
6891 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
6892 E->intersectFlagsWith(Flags);
6893 return SDValue(E, 0);
6894 }
6895
6896 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6897 N->setFlags(Flags);
6898 createOperands(N, Ops);
6899 CSEMap.InsertNode(N, IP);
6900 } else {
6901 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
6902 createOperands(N, Ops);
6903 }
6904
6905 InsertNode(N);
6906 SDValue V = SDValue(N, 0);
6907 NewSDValueDbgMsg(V, "Creating new node: ", this);
6908 return V;
6909}
6910
6911static std::optional<APInt> FoldValue(unsigned Opcode, const APInt &C1,
6912 const APInt &C2) {
6913 switch (Opcode) {
6914 case ISD::ADD: return C1 + C2;
6915 case ISD::SUB: return C1 - C2;
6916 case ISD::MUL: return C1 * C2;
6917 case ISD::AND: return C1 & C2;
6918 case ISD::OR: return C1 | C2;
6919 case ISD::XOR: return C1 ^ C2;
6920 case ISD::SHL: return C1 << C2;
6921 case ISD::SRL: return C1.lshr(C2);
6922 case ISD::SRA: return C1.ashr(C2);
6923 case ISD::ROTL: return C1.rotl(C2);
6924 case ISD::ROTR: return C1.rotr(C2);
6925 case ISD::SMIN: return C1.sle(C2) ? C1 : C2;
6926 case ISD::SMAX: return C1.sge(C2) ? C1 : C2;
6927 case ISD::UMIN: return C1.ule(C2) ? C1 : C2;
6928 case ISD::UMAX: return C1.uge(C2) ? C1 : C2;
6929 case ISD::SADDSAT: return C1.sadd_sat(C2);
6930 case ISD::UADDSAT: return C1.uadd_sat(C2);
6931 case ISD::SSUBSAT: return C1.ssub_sat(C2);
6932 case ISD::USUBSAT: return C1.usub_sat(C2);
6933 case ISD::SSHLSAT: return C1.sshl_sat(C2);
6934 case ISD::USHLSAT: return C1.ushl_sat(C2);
6935 case ISD::UDIV:
6936 if (!C2.getBoolValue())
6937 break;
6938 return C1.udiv(C2);
6939 case ISD::UREM:
6940 if (!C2.getBoolValue())
6941 break;
6942 return C1.urem(C2);
6943 case ISD::SDIV:
6944 if (!C2.getBoolValue())
6945 break;
6946 return C1.sdiv(C2);
6947 case ISD::SREM:
6948 if (!C2.getBoolValue())
6949 break;
6950 return C1.srem(C2);
6951 case ISD::AVGFLOORS:
6952 return APIntOps::avgFloorS(C1, C2);
6953 case ISD::AVGFLOORU:
6954 return APIntOps::avgFloorU(C1, C2);
6955 case ISD::AVGCEILS:
6956 return APIntOps::avgCeilS(C1, C2);
6957 case ISD::AVGCEILU:
6958 return APIntOps::avgCeilU(C1, C2);
6959 case ISD::ABDS:
6960 return APIntOps::abds(C1, C2);
6961 case ISD::ABDU:
6962 return APIntOps::abdu(C1, C2);
6963 case ISD::MULHS:
6964 return APIntOps::mulhs(C1, C2);
6965 case ISD::MULHU:
6966 return APIntOps::mulhu(C1, C2);
6967 case ISD::CLMUL:
6968 return APIntOps::clmul(C1, C2);
6969 case ISD::CLMULR:
6970 return APIntOps::clmulr(C1, C2);
6971 case ISD::CLMULH:
6972 return APIntOps::clmulh(C1, C2);
6973 }
6974 return std::nullopt;
6975}
6976// Handle constant folding with UNDEF.
6977// TODO: Handle more cases.
6978static std::optional<APInt> FoldValueWithUndef(unsigned Opcode, const APInt &C1,
6979 bool IsUndef1, const APInt &C2,
6980 bool IsUndef2) {
6981 if (!(IsUndef1 || IsUndef2))
6982 return FoldValue(Opcode, C1, C2);
6983
6984 // Fold and(x, undef) -> 0
6985 // Fold mul(x, undef) -> 0
6986 if (Opcode == ISD::AND || Opcode == ISD::MUL)
6987 return APInt::getZero(C1.getBitWidth());
6988
6989 return std::nullopt;
6990}
6991
6993 const GlobalAddressSDNode *GA,
6994 const SDNode *N2) {
6995 if (GA->getOpcode() != ISD::GlobalAddress)
6996 return SDValue();
6997 if (!TLI->isOffsetFoldingLegal(GA))
6998 return SDValue();
6999 auto *C2 = dyn_cast<ConstantSDNode>(N2);
7000 if (!C2)
7001 return SDValue();
7002 int64_t Offset = C2->getSExtValue();
7003 switch (Opcode) {
7004 case ISD::ADD:
7005 case ISD::PTRADD:
7006 break;
7007 case ISD::SUB: Offset = -uint64_t(Offset); break;
7008 default: return SDValue();
7009 }
7010 return getGlobalAddress(GA->getGlobal(), SDLoc(C2), VT,
7011 GA->getOffset() + uint64_t(Offset));
7012}
7013
7015 switch (Opcode) {
7016 case ISD::SDIV:
7017 case ISD::UDIV:
7018 case ISD::SREM:
7019 case ISD::UREM: {
7020 // If a divisor is zero/undef or any element of a divisor vector is
7021 // zero/undef, the whole op is undef.
7022 assert(Ops.size() == 2 && "Div/rem should have 2 operands");
7023 SDValue Divisor = Ops[1];
7024 if (Divisor.isUndef() || isNullConstant(Divisor))
7025 return true;
7026
7027 return ISD::isBuildVectorOfConstantSDNodes(Divisor.getNode()) &&
7028 llvm::any_of(Divisor->op_values(),
7029 [](SDValue V) { return V.isUndef() ||
7030 isNullConstant(V); });
7031 // TODO: Handle signed overflow.
7032 }
7033 // TODO: Handle oversized shifts.
7034 default:
7035 return false;
7036 }
7037}
7038
7041 SDNodeFlags Flags) {
7042 // If the opcode is a target-specific ISD node, there's nothing we can
7043 // do here and the operand rules may not line up with the below, so
7044 // bail early.
7045 // We can't create a scalar CONCAT_VECTORS so skip it. It will break
7046 // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
7047 // foldCONCAT_VECTORS in getNode before this is called.
7048 if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
7049 return SDValue();
7050
7051 unsigned NumOps = Ops.size();
7052 if (NumOps == 0)
7053 return SDValue();
7054
7055 if (isUndef(Opcode, Ops))
7056 return getUNDEF(VT);
7057
7058 // Handle unary special cases.
7059 if (NumOps == 1) {
7060 SDValue N1 = Ops[0];
7061
7062 // Constant fold unary operations with an integer constant operand. Even
7063 // opaque constant will be folded, because the folding of unary operations
7064 // doesn't create new constants with different values. Nevertheless, the
7065 // opaque flag is preserved during folding to prevent future folding with
7066 // other constants.
7067 if (auto *C = dyn_cast<ConstantSDNode>(N1)) {
7068 const APInt &Val = C->getAPIntValue();
7069 switch (Opcode) {
7070 case ISD::SIGN_EXTEND:
7071 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7072 C->isTargetOpcode(), C->isOpaque());
7073 case ISD::TRUNCATE:
7074 if (C->isOpaque())
7075 break;
7076 [[fallthrough]];
7077 case ISD::ZERO_EXTEND:
7078 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7079 C->isTargetOpcode(), C->isOpaque());
7080 case ISD::ANY_EXTEND:
7081 // Some targets like RISCV prefer to sign extend some types.
7082 if (TLI->isSExtCheaperThanZExt(N1.getValueType(), VT))
7083 return getConstant(Val.sextOrTrunc(VT.getSizeInBits()), DL, VT,
7084 C->isTargetOpcode(), C->isOpaque());
7085 return getConstant(Val.zextOrTrunc(VT.getSizeInBits()), DL, VT,
7086 C->isTargetOpcode(), C->isOpaque());
7087 case ISD::ABS:
7088 return getConstant(Val.abs(), DL, VT, C->isTargetOpcode(),
7089 C->isOpaque());
7090 case ISD::BITREVERSE:
7091 return getConstant(Val.reverseBits(), DL, VT, C->isTargetOpcode(),
7092 C->isOpaque());
7093 case ISD::BSWAP:
7094 return getConstant(Val.byteSwap(), DL, VT, C->isTargetOpcode(),
7095 C->isOpaque());
7096 case ISD::CTPOP:
7097 return getConstant(Val.popcount(), DL, VT, C->isTargetOpcode(),
7098 C->isOpaque());
7099 case ISD::CTLZ:
7101 return getConstant(Val.countl_zero(), DL, VT, C->isTargetOpcode(),
7102 C->isOpaque());
7103 case ISD::CTTZ:
7105 return getConstant(Val.countr_zero(), DL, VT, C->isTargetOpcode(),
7106 C->isOpaque());
7107 case ISD::CTLS:
7108 // CTLS returns the number of extra sign bits so subtract one.
7109 return getConstant(Val.getNumSignBits() - 1, DL, VT,
7110 C->isTargetOpcode(), C->isOpaque());
7111 case ISD::UINT_TO_FP:
7112 case ISD::SINT_TO_FP: {
7114 (void)FPV.convertFromAPInt(Val, Opcode == ISD::SINT_TO_FP,
7116 return getConstantFP(FPV, DL, VT);
7117 }
7118 case ISD::FP16_TO_FP:
7119 case ISD::BF16_TO_FP: {
7120 bool Ignored;
7121 APFloat FPV(Opcode == ISD::FP16_TO_FP ? APFloat::IEEEhalf()
7122 : APFloat::BFloat(),
7123 (Val.getBitWidth() == 16) ? Val : Val.trunc(16));
7124
7125 // This can return overflow, underflow, or inexact; we don't care.
7126 // FIXME need to be more flexible about rounding mode.
7128 &Ignored);
7129 return getConstantFP(FPV, DL, VT);
7130 }
7131 case ISD::STEP_VECTOR:
7132 if (SDValue V = FoldSTEP_VECTOR(DL, VT, N1, *this))
7133 return V;
7134 break;
7135 case ISD::BITCAST:
7136 if (VT == MVT::f16 && C->getValueType(0) == MVT::i16)
7137 return getConstantFP(APFloat(APFloat::IEEEhalf(), Val), DL, VT);
7138 if (VT == MVT::f32 && C->getValueType(0) == MVT::i32)
7139 return getConstantFP(APFloat(APFloat::IEEEsingle(), Val), DL, VT);
7140 if (VT == MVT::f64 && C->getValueType(0) == MVT::i64)
7141 return getConstantFP(APFloat(APFloat::IEEEdouble(), Val), DL, VT);
7142 if (VT == MVT::f128 && C->getValueType(0) == MVT::i128)
7143 return getConstantFP(APFloat(APFloat::IEEEquad(), Val), DL, VT);
7144 break;
7145 }
7146 }
7147
7148 // Constant fold unary operations with a floating point constant operand.
7149 if (auto *C = dyn_cast<ConstantFPSDNode>(N1)) {
7150 APFloat V = C->getValueAPF(); // make copy
7151 switch (Opcode) {
7152 case ISD::FNEG:
7153 V.changeSign();
7154 return getConstantFP(V, DL, VT);
7155 case ISD::FABS:
7156 V.clearSign();
7157 return getConstantFP(V, DL, VT);
7158 case ISD::FCEIL: {
7159 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardPositive);
7161 return getConstantFP(V, DL, VT);
7162 return SDValue();
7163 }
7164 case ISD::FTRUNC: {
7165 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardZero);
7167 return getConstantFP(V, DL, VT);
7168 return SDValue();
7169 }
7170 case ISD::FFLOOR: {
7171 APFloat::opStatus fs = V.roundToIntegral(APFloat::rmTowardNegative);
7173 return getConstantFP(V, DL, VT);
7174 return SDValue();
7175 }
7176 case ISD::FP_EXTEND: {
7177 bool ignored;
7178 // This can return overflow, underflow, or inexact; we don't care.
7179 // FIXME need to be more flexible about rounding mode.
7180 (void)V.convert(VT.getFltSemantics(), APFloat::rmNearestTiesToEven,
7181 &ignored);
7182 return getConstantFP(V, DL, VT);
7183 }
7184 case ISD::FP_TO_SINT:
7185 case ISD::FP_TO_UINT: {
7186 bool ignored;
7187 APSInt IntVal(VT.getSizeInBits(), Opcode == ISD::FP_TO_UINT);
7188 // FIXME need to be more flexible about rounding mode.
7190 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored);
7191 if (s == APFloat::opInvalidOp) // inexact is OK, in fact usual
7192 break;
7193 return getConstant(IntVal, DL, VT);
7194 }
7195 case ISD::FP_TO_FP16:
7196 case ISD::FP_TO_BF16: {
7197 bool Ignored;
7198 // This can return overflow, underflow, or inexact; we don't care.
7199 // FIXME need to be more flexible about rounding mode.
7200 (void)V.convert(Opcode == ISD::FP_TO_FP16 ? APFloat::IEEEhalf()
7201 : APFloat::BFloat(),
7203 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7204 }
7205 case ISD::BITCAST:
7206 if (VT == MVT::i16 && C->getValueType(0) == MVT::f16)
7207 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7208 VT);
7209 if (VT == MVT::i16 && C->getValueType(0) == MVT::bf16)
7210 return getConstant((uint16_t)V.bitcastToAPInt().getZExtValue(), DL,
7211 VT);
7212 if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
7213 return getConstant((uint32_t)V.bitcastToAPInt().getZExtValue(), DL,
7214 VT);
7215 if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
7216 return getConstant(V.bitcastToAPInt().getZExtValue(), DL, VT);
7217 break;
7218 }
7219 }
7220
7221 // Early-out if we failed to constant fold a bitcast.
7222 if (Opcode == ISD::BITCAST)
7223 return SDValue();
7224 }
7225
7226 // Handle binops special cases.
7227 if (NumOps == 2) {
7228 if (SDValue CFP = foldConstantFPMath(Opcode, DL, VT, Ops))
7229 return CFP;
7230
7231 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7232 if (auto *C2 = dyn_cast<ConstantSDNode>(Ops[1])) {
7233 if (C1->isOpaque() || C2->isOpaque())
7234 return SDValue();
7235
7236 std::optional<APInt> FoldAttempt =
7237 FoldValue(Opcode, C1->getAPIntValue(), C2->getAPIntValue());
7238 if (!FoldAttempt)
7239 return SDValue();
7240
7241 SDValue Folded = getConstant(*FoldAttempt, DL, VT);
7242 assert((!Folded || !VT.isVector()) &&
7243 "Can't fold vectors ops with scalar operands");
7244 return Folded;
7245 }
7246 }
7247
7248 // fold (add Sym, c) -> Sym+c
7250 return FoldSymbolOffset(Opcode, VT, GA, Ops[1].getNode());
7251 if (TLI->isCommutativeBinOp(Opcode))
7253 return FoldSymbolOffset(Opcode, VT, GA, Ops[0].getNode());
7254
7255 // fold (sext_in_reg c1) -> c2
7256 if (Opcode == ISD::SIGN_EXTEND_INREG) {
7257 EVT EVT = cast<VTSDNode>(Ops[1])->getVT();
7258
7259 auto SignExtendInReg = [&](APInt Val, llvm::EVT ConstantVT) {
7260 unsigned FromBits = EVT.getScalarSizeInBits();
7261 Val <<= Val.getBitWidth() - FromBits;
7262 Val.ashrInPlace(Val.getBitWidth() - FromBits);
7263 return getConstant(Val, DL, ConstantVT);
7264 };
7265
7266 if (auto *C1 = dyn_cast<ConstantSDNode>(Ops[0])) {
7267 const APInt &Val = C1->getAPIntValue();
7268 return SignExtendInReg(Val, VT);
7269 }
7270
7272 SmallVector<SDValue, 8> ScalarOps;
7273 llvm::EVT OpVT = Ops[0].getOperand(0).getValueType();
7274 for (int I = 0, E = VT.getVectorNumElements(); I != E; ++I) {
7275 SDValue Op = Ops[0].getOperand(I);
7276 if (Op.isUndef()) {
7277 ScalarOps.push_back(getUNDEF(OpVT));
7278 continue;
7279 }
7280 const APInt &Val = cast<ConstantSDNode>(Op)->getAPIntValue();
7281 ScalarOps.push_back(SignExtendInReg(Val, OpVT));
7282 }
7283 return getBuildVector(VT, DL, ScalarOps);
7284 }
7285
7286 if (Ops[0].getOpcode() == ISD::SPLAT_VECTOR &&
7287 isa<ConstantSDNode>(Ops[0].getOperand(0)))
7288 return getNode(ISD::SPLAT_VECTOR, DL, VT,
7289 SignExtendInReg(Ops[0].getConstantOperandAPInt(0),
7290 Ops[0].getOperand(0).getValueType()));
7291 }
7292 }
7293
7294 // Handle fshl/fshr special cases.
7295 if (Opcode == ISD::FSHL || Opcode == ISD::FSHR) {
7296 auto *C1 = dyn_cast<ConstantSDNode>(Ops[0]);
7297 auto *C2 = dyn_cast<ConstantSDNode>(Ops[1]);
7298 auto *C3 = dyn_cast<ConstantSDNode>(Ops[2]);
7299
7300 if (C1 && C2 && C3) {
7301 if (C1->isOpaque() || C2->isOpaque() || C3->isOpaque())
7302 return SDValue();
7303 const APInt &V1 = C1->getAPIntValue(), &V2 = C2->getAPIntValue(),
7304 &V3 = C3->getAPIntValue();
7305
7306 APInt FoldedVal = Opcode == ISD::FSHL ? APIntOps::fshl(V1, V2, V3)
7307 : APIntOps::fshr(V1, V2, V3);
7308 return getConstant(FoldedVal, DL, VT);
7309 }
7310 }
7311
7312 // Handle fma/fmad special cases.
7313 if (Opcode == ISD::FMA || Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7314 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7315 assert(Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7316 Ops[2].getValueType() == VT && "FMA types must match!");
7320 if (C1 && C2 && C3) {
7321 APFloat V1 = C1->getValueAPF();
7322 const APFloat &V2 = C2->getValueAPF();
7323 const APFloat &V3 = C3->getValueAPF();
7324 if (Opcode == ISD::FMAD || Opcode == ISD::FMULADD) {
7327 } else
7329 return getConstantFP(V1, DL, VT);
7330 }
7331 }
7332
7333 // This is for vector folding only from here on.
7334 if (!VT.isVector())
7335 return SDValue();
7336
7337 ElementCount NumElts = VT.getVectorElementCount();
7338
7339 // See if we can fold through any bitcasted integer ops.
7340 if (NumOps == 2 && VT.isFixedLengthVector() && VT.isInteger() &&
7341 Ops[0].getValueType() == VT && Ops[1].getValueType() == VT &&
7342 (Ops[0].getOpcode() == ISD::BITCAST ||
7343 Ops[1].getOpcode() == ISD::BITCAST)) {
7346 auto *BV1 = dyn_cast<BuildVectorSDNode>(N1);
7347 auto *BV2 = dyn_cast<BuildVectorSDNode>(N2);
7348 if (BV1 && BV2 && N1.getValueType().isInteger() &&
7349 N2.getValueType().isInteger()) {
7350 bool IsLE = getDataLayout().isLittleEndian();
7351 unsigned EltBits = VT.getScalarSizeInBits();
7352 SmallVector<APInt> RawBits1, RawBits2;
7353 BitVector UndefElts1, UndefElts2;
7354 if (BV1->getConstantRawBits(IsLE, EltBits, RawBits1, UndefElts1) &&
7355 BV2->getConstantRawBits(IsLE, EltBits, RawBits2, UndefElts2)) {
7356 SmallVector<APInt> RawBits;
7357 for (unsigned I = 0, E = NumElts.getFixedValue(); I != E; ++I) {
7358 std::optional<APInt> Fold = FoldValueWithUndef(
7359 Opcode, RawBits1[I], UndefElts1[I], RawBits2[I], UndefElts2[I]);
7360 if (!Fold)
7361 break;
7362 RawBits.push_back(*Fold);
7363 }
7364 if (RawBits.size() == NumElts.getFixedValue()) {
7365 // We have constant folded, but we might need to cast this again back
7366 // to the original (possibly legalized) type.
7367 EVT BVVT, BVEltVT;
7368 if (N1.getValueType() == VT) {
7369 BVVT = N1.getValueType();
7370 BVEltVT = BV1->getOperand(0).getValueType();
7371 } else {
7372 BVVT = N2.getValueType();
7373 BVEltVT = BV2->getOperand(0).getValueType();
7374 }
7375 unsigned BVEltBits = BVEltVT.getSizeInBits();
7376 SmallVector<APInt> DstBits;
7377 BitVector DstUndefs;
7379 DstBits, RawBits, DstUndefs,
7380 BitVector(RawBits.size(), false));
7381 SmallVector<SDValue> Ops(DstBits.size(), getUNDEF(BVEltVT));
7382 for (unsigned I = 0, E = DstBits.size(); I != E; ++I) {
7383 if (DstUndefs[I])
7384 continue;
7385 Ops[I] = getConstant(DstBits[I].sext(BVEltBits), DL, BVEltVT);
7386 }
7387 return getBitcast(VT, getBuildVector(BVVT, DL, Ops));
7388 }
7389 }
7390 }
7391 }
7392
7393 // Fold (mul step_vector(C0), C1) to (step_vector(C0 * C1)).
7394 // (shl step_vector(C0), C1) -> (step_vector(C0 << C1))
7395 if ((Opcode == ISD::MUL || Opcode == ISD::SHL) &&
7396 Ops[0].getOpcode() == ISD::STEP_VECTOR) {
7397 APInt RHSVal;
7398 if (ISD::isConstantSplatVector(Ops[1].getNode(), RHSVal)) {
7399 APInt NewStep = Opcode == ISD::MUL
7400 ? Ops[0].getConstantOperandAPInt(0) * RHSVal
7401 : Ops[0].getConstantOperandAPInt(0) << RHSVal;
7402 return getStepVector(DL, VT, NewStep);
7403 }
7404 }
7405
7406 auto IsScalarOrSameVectorSize = [NumElts](const SDValue &Op) {
7407 return !Op.getValueType().isVector() ||
7408 Op.getValueType().getVectorElementCount() == NumElts;
7409 };
7410
7411 auto IsBuildVectorSplatVectorOrUndef = [](const SDValue &Op) {
7412 return Op.isUndef() || Op.getOpcode() == ISD::CONDCODE ||
7413 Op.getOpcode() == ISD::BUILD_VECTOR ||
7414 Op.getOpcode() == ISD::SPLAT_VECTOR;
7415 };
7416
7417 // All operands must be vector types with the same number of elements as
7418 // the result type and must be either UNDEF or a build/splat vector
7419 // or UNDEF scalars.
7420 if (!llvm::all_of(Ops, IsBuildVectorSplatVectorOrUndef) ||
7421 !llvm::all_of(Ops, IsScalarOrSameVectorSize))
7422 return SDValue();
7423
7424 // If we are comparing vectors, then the result needs to be a i1 boolean that
7425 // is then extended back to the legal result type depending on how booleans
7426 // are represented.
7427 EVT SVT = (Opcode == ISD::SETCC ? MVT::i1 : VT.getScalarType());
7428 ISD::NodeType ExtendCode =
7429 (Opcode == ISD::SETCC && SVT != VT.getScalarType())
7430 ? TargetLowering::getExtendForContent(TLI->getBooleanContents(VT))
7432
7433 // Find legal integer scalar type for constant promotion and
7434 // ensure that its scalar size is at least as large as source.
7435 EVT LegalSVT = VT.getScalarType();
7436 if (NewNodesMustHaveLegalTypes && LegalSVT.isInteger()) {
7437 LegalSVT = TLI->getTypeToTransformTo(*getContext(), LegalSVT);
7438 if (LegalSVT.bitsLT(VT.getScalarType()))
7439 return SDValue();
7440 }
7441
7442 // For scalable vector types we know we're dealing with SPLAT_VECTORs. We
7443 // only have one operand to check. For fixed-length vector types we may have
7444 // a combination of BUILD_VECTOR and SPLAT_VECTOR.
7445 unsigned NumVectorElts = NumElts.isScalable() ? 1 : NumElts.getFixedValue();
7446
7447 // Constant fold each scalar lane separately.
7448 SmallVector<SDValue, 4> ScalarResults;
7449 for (unsigned I = 0; I != NumVectorElts; I++) {
7450 SmallVector<SDValue, 4> ScalarOps;
7451 for (SDValue Op : Ops) {
7452 EVT InSVT = Op.getValueType().getScalarType();
7453 if (Op.getOpcode() != ISD::BUILD_VECTOR &&
7454 Op.getOpcode() != ISD::SPLAT_VECTOR) {
7455 if (Op.isUndef())
7456 ScalarOps.push_back(getUNDEF(InSVT));
7457 else
7458 ScalarOps.push_back(Op);
7459 continue;
7460 }
7461
7462 SDValue ScalarOp =
7463 Op.getOperand(Op.getOpcode() == ISD::SPLAT_VECTOR ? 0 : I);
7464 EVT ScalarVT = ScalarOp.getValueType();
7465
7466 // Build vector (integer) scalar operands may need implicit
7467 // truncation - do this before constant folding.
7468 if (ScalarVT.isInteger() && ScalarVT.bitsGT(InSVT)) {
7469 // Don't create illegally-typed nodes unless they're constants or undef
7470 // - if we fail to constant fold we can't guarantee the (dead) nodes
7471 // we're creating will be cleaned up before being visited for
7472 // legalization.
7473 if (NewNodesMustHaveLegalTypes && !ScalarOp.isUndef() &&
7474 !isa<ConstantSDNode>(ScalarOp) &&
7475 TLI->getTypeAction(*getContext(), InSVT) !=
7477 return SDValue();
7478 ScalarOp = getNode(ISD::TRUNCATE, DL, InSVT, ScalarOp);
7479 }
7480
7481 ScalarOps.push_back(ScalarOp);
7482 }
7483
7484 // Constant fold the scalar operands.
7485 SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
7486
7487 // Scalar folding only succeeded if the result is a constant or UNDEF.
7488 if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
7489 ScalarResult.getOpcode() != ISD::ConstantFP)
7490 return SDValue();
7491
7492 // Legalize the (integer) scalar constant if necessary. We only do
7493 // this once we know the folding succeeded, since otherwise we would
7494 // get a node with illegal type which has a user.
7495 if (LegalSVT != SVT)
7496 ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
7497
7498 ScalarResults.push_back(ScalarResult);
7499 }
7500
7501 SDValue V = NumElts.isScalable() ? getSplatVector(VT, DL, ScalarResults[0])
7502 : getBuildVector(VT, DL, ScalarResults);
7503 NewSDValueDbgMsg(V, "New node fold constant vector: ", this);
7504 return V;
7505}
7506
7509 // TODO: Add support for unary/ternary fp opcodes.
7510 if (Ops.size() != 2)
7511 return SDValue();
7512
7513 // TODO: We don't do any constant folding for strict FP opcodes here, but we
7514 // should. That will require dealing with a potentially non-default
7515 // rounding mode, checking the "opStatus" return value from the APFloat
7516 // math calculations, and possibly other variations.
7517 SDValue N1 = Ops[0];
7518 SDValue N2 = Ops[1];
7519 ConstantFPSDNode *N1CFP = isConstOrConstSplatFP(N1, /*AllowUndefs*/ false);
7520 ConstantFPSDNode *N2CFP = isConstOrConstSplatFP(N2, /*AllowUndefs*/ false);
7521 if (N1CFP && N2CFP) {
7522 APFloat C1 = N1CFP->getValueAPF(); // make copy
7523 const APFloat &C2 = N2CFP->getValueAPF();
7524 switch (Opcode) {
7525 case ISD::FADD:
7527 return getConstantFP(C1, DL, VT);
7528 case ISD::FSUB:
7530 return getConstantFP(C1, DL, VT);
7531 case ISD::FMUL:
7533 return getConstantFP(C1, DL, VT);
7534 case ISD::FDIV:
7536 return getConstantFP(C1, DL, VT);
7537 case ISD::FREM:
7538 C1.mod(C2);
7539 return getConstantFP(C1, DL, VT);
7540 case ISD::FCOPYSIGN:
7541 C1.copySign(C2);
7542 return getConstantFP(C1, DL, VT);
7543 case ISD::FMINNUM:
7544 if (C1.isSignaling() || C2.isSignaling())
7545 return SDValue();
7546 return getConstantFP(minnum(C1, C2), DL, VT);
7547 case ISD::FMAXNUM:
7548 if (C1.isSignaling() || C2.isSignaling())
7549 return SDValue();
7550 return getConstantFP(maxnum(C1, C2), DL, VT);
7551 case ISD::FMINIMUM:
7552 return getConstantFP(minimum(C1, C2), DL, VT);
7553 case ISD::FMAXIMUM:
7554 return getConstantFP(maximum(C1, C2), DL, VT);
7555 case ISD::FMINIMUMNUM:
7556 return getConstantFP(minimumnum(C1, C2), DL, VT);
7557 case ISD::FMAXIMUMNUM:
7558 return getConstantFP(maximumnum(C1, C2), DL, VT);
7559 default: break;
7560 }
7561 }
7562 if (N1CFP && Opcode == ISD::FP_ROUND) {
7563 APFloat C1 = N1CFP->getValueAPF(); // make copy
7564 bool Unused;
7565 // This can return overflow, underflow, or inexact; we don't care.
7566 // FIXME need to be more flexible about rounding mode.
7568 &Unused);
7569 return getConstantFP(C1, DL, VT);
7570 }
7571
7572 switch (Opcode) {
7573 case ISD::FSUB:
7574 // -0.0 - undef --> undef (consistent with "fneg undef")
7575 if (ConstantFPSDNode *N1C = isConstOrConstSplatFP(N1, /*AllowUndefs*/ true))
7576 if (N1C && N1C->getValueAPF().isNegZero() && N2.isUndef())
7577 return getUNDEF(VT);
7578 [[fallthrough]];
7579
7580 case ISD::FADD:
7581 case ISD::FMUL:
7582 case ISD::FDIV:
7583 case ISD::FREM:
7584 // If both operands are undef, the result is undef. If 1 operand is undef,
7585 // the result is NaN. This should match the behavior of the IR optimizer.
7586 if (N1.isUndef() && N2.isUndef())
7587 return getUNDEF(VT);
7588 if (N1.isUndef() || N2.isUndef())
7590 }
7591 return SDValue();
7592}
7593
7595 const SDLoc &DL, EVT DstEltVT) {
7596 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
7597
7598 // If this is already the right type, we're done.
7599 if (SrcEltVT == DstEltVT)
7600 return SDValue(BV, 0);
7601
7602 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
7603 unsigned DstBitSize = DstEltVT.getSizeInBits();
7604
7605 // If this is a conversion of N elements of one type to N elements of another
7606 // type, convert each element. This handles FP<->INT cases.
7607 if (SrcBitSize == DstBitSize) {
7609 for (SDValue Op : BV->op_values()) {
7610 // If the vector element type is not legal, the BUILD_VECTOR operands
7611 // are promoted and implicitly truncated. Make that explicit here.
7612 if (Op.getValueType() != SrcEltVT)
7613 Op = getNode(ISD::TRUNCATE, DL, SrcEltVT, Op);
7614 Ops.push_back(getBitcast(DstEltVT, Op));
7615 }
7616 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT,
7618 return getBuildVector(VT, DL, Ops);
7619 }
7620
7621 // Otherwise, we're growing or shrinking the elements. To avoid having to
7622 // handle annoying details of growing/shrinking FP values, we convert them to
7623 // int first.
7624 if (SrcEltVT.isFloatingPoint()) {
7625 // Convert the input float vector to a int vector where the elements are the
7626 // same sizes.
7627 EVT IntEltVT = EVT::getIntegerVT(*getContext(), SrcEltVT.getSizeInBits());
7628 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7630 DstEltVT);
7631 return SDValue();
7632 }
7633
7634 // Now we know the input is an integer vector. If the output is a FP type,
7635 // convert to integer first, then to FP of the right size.
7636 if (DstEltVT.isFloatingPoint()) {
7637 EVT IntEltVT = EVT::getIntegerVT(*getContext(), DstEltVT.getSizeInBits());
7638 if (SDValue Tmp = FoldConstantBuildVector(BV, DL, IntEltVT))
7640 DstEltVT);
7641 return SDValue();
7642 }
7643
7644 // Okay, we know the src/dst types are both integers of differing types.
7645 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
7646
7647 // Extract the constant raw bit data.
7648 BitVector UndefElements;
7649 SmallVector<APInt> RawBits;
7650 bool IsLE = getDataLayout().isLittleEndian();
7651 if (!BV->getConstantRawBits(IsLE, DstBitSize, RawBits, UndefElements))
7652 return SDValue();
7653
7655 for (unsigned I = 0, E = RawBits.size(); I != E; ++I) {
7656 if (UndefElements[I])
7657 Ops.push_back(getUNDEF(DstEltVT));
7658 else
7659 Ops.push_back(getConstant(RawBits[I], DL, DstEltVT));
7660 }
7661
7662 EVT VT = EVT::getVectorVT(*getContext(), DstEltVT, Ops.size());
7663 return getBuildVector(VT, DL, Ops);
7664}
7665
7667 assert(Val.getValueType().isInteger() && "Invalid AssertAlign!");
7668
7669 // There's no need to assert on a byte-aligned pointer. All pointers are at
7670 // least byte aligned.
7671 if (A == Align(1))
7672 return Val;
7673
7674 SDVTList VTs = getVTList(Val.getValueType());
7676 AddNodeIDNode(ID, ISD::AssertAlign, VTs, {Val});
7677 ID.AddInteger(A.value());
7678
7679 void *IP = nullptr;
7680 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP))
7681 return SDValue(E, 0);
7682
7683 auto *N =
7684 newSDNode<AssertAlignSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, A);
7685 createOperands(N, {Val});
7686
7687 CSEMap.InsertNode(N, IP);
7688 InsertNode(N);
7689
7690 SDValue V(N, 0);
7691 NewSDValueDbgMsg(V, "Creating new node: ", this);
7692 return V;
7693}
7694
7695SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7696 SDValue N1, SDValue N2) {
7697 SDNodeFlags Flags;
7698 if (Inserter)
7699 Flags = Inserter->getFlags();
7700 return getNode(Opcode, DL, VT, N1, N2, Flags);
7701}
7702
7704 SDValue &N2) const {
7705 if (!TLI->isCommutativeBinOp(Opcode))
7706 return;
7707
7708 // Canonicalize:
7709 // binop(const, nonconst) -> binop(nonconst, const)
7712 bool N1CFP = isConstantFPBuildVectorOrConstantFP(N1);
7713 bool N2CFP = isConstantFPBuildVectorOrConstantFP(N2);
7714 if ((N1C && !N2C) || (N1CFP && !N2CFP))
7715 std::swap(N1, N2);
7716
7717 // Canonicalize:
7718 // binop(splat(x), step_vector) -> binop(step_vector, splat(x))
7719 else if (N1.getOpcode() == ISD::SPLAT_VECTOR &&
7721 std::swap(N1, N2);
7722}
7723
7724SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
7725 SDValue N1, SDValue N2, const SDNodeFlags Flags) {
7727 N2.getOpcode() != ISD::DELETED_NODE &&
7728 "Operand is DELETED_NODE!");
7729
7730 canonicalizeCommutativeBinop(Opcode, N1, N2);
7731
7732 auto *N1C = dyn_cast<ConstantSDNode>(N1);
7733 auto *N2C = dyn_cast<ConstantSDNode>(N2);
7734
7735 // Don't allow undefs in vector splats - we might be returning N2 when folding
7736 // to zero etc.
7737 ConstantSDNode *N2CV =
7738 isConstOrConstSplat(N2, /*AllowUndefs*/ false, /*AllowTruncation*/ true);
7739
7740 switch (Opcode) {
7741 default: break;
7742 case ISD::TokenFactor:
7743 assert(VT == MVT::Other && N1.getValueType() == MVT::Other &&
7744 N2.getValueType() == MVT::Other && "Invalid token factor!");
7745 // Fold trivial token factors.
7746 if (N1.getOpcode() == ISD::EntryToken) return N2;
7747 if (N2.getOpcode() == ISD::EntryToken) return N1;
7748 if (N1 == N2) return N1;
7749 break;
7750 case ISD::BUILD_VECTOR: {
7751 // Attempt to simplify BUILD_VECTOR.
7752 SDValue Ops[] = {N1, N2};
7753 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
7754 return V;
7755 break;
7756 }
7757 case ISD::CONCAT_VECTORS: {
7758 SDValue Ops[] = {N1, N2};
7759 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
7760 return V;
7761 break;
7762 }
7763 case ISD::AND:
7764 assert(VT.isInteger() && "This operator does not apply to FP types!");
7765 assert(N1.getValueType() == N2.getValueType() &&
7766 N1.getValueType() == VT && "Binary operator types must match!");
7767 // (X & 0) -> 0. This commonly occurs when legalizing i64 values, so it's
7768 // worth handling here.
7769 if (N2CV && N2CV->isZero())
7770 return N2;
7771 if (N2CV && N2CV->isAllOnes()) // X & -1 -> X
7772 return N1;
7773 break;
7774 case ISD::OR:
7775 case ISD::XOR:
7776 case ISD::ADD:
7777 case ISD::PTRADD:
7778 case ISD::SUB:
7779 assert(VT.isInteger() && "This operator does not apply to FP types!");
7780 assert(N1.getValueType() == N2.getValueType() &&
7781 N1.getValueType() == VT && "Binary operator types must match!");
7782 // The equal operand types requirement is unnecessarily strong for PTRADD.
7783 // However, the SelectionDAGBuilder does not generate PTRADDs with different
7784 // operand types, and we'd need to re-implement GEP's non-standard wrapping
7785 // logic everywhere where PTRADDs may be folded or combined to properly
7786 // support them. If/when we introduce pointer types to the SDAG, we will
7787 // need to relax this constraint.
7788
7789 // (X ^|+- 0) -> X. This commonly occurs when legalizing i64 values, so
7790 // it's worth handling here.
7791 if (N2CV && N2CV->isZero())
7792 return N1;
7793 if ((Opcode == ISD::ADD || Opcode == ISD::SUB) &&
7794 VT.getScalarType() == MVT::i1)
7795 return getNode(ISD::XOR, DL, VT, N1, N2);
7796 // Fold (add (vscale * C0), (vscale * C1)) to (vscale * (C0 + C1)).
7797 if (Opcode == ISD::ADD && N1.getOpcode() == ISD::VSCALE &&
7798 N2.getOpcode() == ISD::VSCALE) {
7799 const APInt &C1 = N1->getConstantOperandAPInt(0);
7800 const APInt &C2 = N2->getConstantOperandAPInt(0);
7801 return getVScale(DL, VT, C1 + C2);
7802 }
7803 break;
7804 case ISD::MUL:
7805 assert(VT.isInteger() && "This operator does not apply to FP types!");
7806 assert(N1.getValueType() == N2.getValueType() &&
7807 N1.getValueType() == VT && "Binary operator types must match!");
7808 if (VT.getScalarType() == MVT::i1)
7809 return getNode(ISD::AND, DL, VT, N1, N2);
7810 if (N2CV && N2CV->isZero())
7811 return N2;
7812 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7813 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7814 const APInt &N2CImm = N2C->getAPIntValue();
7815 return getVScale(DL, VT, MulImm * N2CImm);
7816 }
7817 break;
7818 case ISD::UDIV:
7819 case ISD::UREM:
7820 case ISD::MULHU:
7821 case ISD::MULHS:
7822 case ISD::SDIV:
7823 case ISD::SREM:
7824 case ISD::SADDSAT:
7825 case ISD::SSUBSAT:
7826 case ISD::UADDSAT:
7827 case ISD::USUBSAT:
7828 assert(VT.isInteger() && "This operator does not apply to FP types!");
7829 assert(N1.getValueType() == N2.getValueType() &&
7830 N1.getValueType() == VT && "Binary operator types must match!");
7831 if (VT.getScalarType() == MVT::i1) {
7832 // fold (add_sat x, y) -> (or x, y) for bool types.
7833 if (Opcode == ISD::SADDSAT || Opcode == ISD::UADDSAT)
7834 return getNode(ISD::OR, DL, VT, N1, N2);
7835 // fold (sub_sat x, y) -> (and x, ~y) for bool types.
7836 if (Opcode == ISD::SSUBSAT || Opcode == ISD::USUBSAT)
7837 return getNode(ISD::AND, DL, VT, N1, getNOT(DL, N2, VT));
7838 }
7839 break;
7840 case ISD::SCMP:
7841 case ISD::UCMP:
7842 assert(N1.getValueType() == N2.getValueType() &&
7843 "Types of operands of UCMP/SCMP must match");
7844 assert(N1.getValueType().isVector() == VT.isVector() &&
7845 "Operands and return type of must both be scalars or vectors");
7846 if (VT.isVector())
7849 "Result and operands must have the same number of elements");
7850 break;
7851 case ISD::AVGFLOORS:
7852 case ISD::AVGFLOORU:
7853 case ISD::AVGCEILS:
7854 case ISD::AVGCEILU:
7855 assert(VT.isInteger() && "This operator does not apply to FP types!");
7856 assert(N1.getValueType() == N2.getValueType() &&
7857 N1.getValueType() == VT && "Binary operator types must match!");
7858 break;
7859 case ISD::ABDS:
7860 case ISD::ABDU:
7861 assert(VT.isInteger() && "This operator does not apply to FP types!");
7862 assert(N1.getValueType() == N2.getValueType() &&
7863 N1.getValueType() == VT && "Binary operator types must match!");
7864 if (VT.getScalarType() == MVT::i1)
7865 return getNode(ISD::XOR, DL, VT, N1, N2);
7866 break;
7867 case ISD::SMIN:
7868 case ISD::UMAX:
7869 assert(VT.isInteger() && "This operator does not apply to FP types!");
7870 assert(N1.getValueType() == N2.getValueType() &&
7871 N1.getValueType() == VT && "Binary operator types must match!");
7872 if (VT.getScalarType() == MVT::i1)
7873 return getNode(ISD::OR, DL, VT, N1, N2);
7874 break;
7875 case ISD::SMAX:
7876 case ISD::UMIN:
7877 assert(VT.isInteger() && "This operator does not apply to FP types!");
7878 assert(N1.getValueType() == N2.getValueType() &&
7879 N1.getValueType() == VT && "Binary operator types must match!");
7880 if (VT.getScalarType() == MVT::i1)
7881 return getNode(ISD::AND, DL, VT, N1, N2);
7882 break;
7883 case ISD::FADD:
7884 case ISD::FSUB:
7885 case ISD::FMUL:
7886 case ISD::FDIV:
7887 case ISD::FREM:
7888 assert(VT.isFloatingPoint() && "This operator only applies to FP types!");
7889 assert(N1.getValueType() == N2.getValueType() &&
7890 N1.getValueType() == VT && "Binary operator types must match!");
7891 if (SDValue V = simplifyFPBinop(Opcode, N1, N2, Flags))
7892 return V;
7893 break;
7894 case ISD::FCOPYSIGN: // N1 and result must match. N1/N2 need not match.
7895 assert(N1.getValueType() == VT &&
7898 "Invalid FCOPYSIGN!");
7899 break;
7900 case ISD::SHL:
7901 if (N2C && (N1.getOpcode() == ISD::VSCALE) && Flags.hasNoSignedWrap()) {
7902 const APInt &MulImm = N1->getConstantOperandAPInt(0);
7903 const APInt &ShiftImm = N2C->getAPIntValue();
7904 return getVScale(DL, VT, MulImm << ShiftImm);
7905 }
7906 [[fallthrough]];
7907 case ISD::SRA:
7908 case ISD::SRL:
7909 if (SDValue V = simplifyShift(N1, N2))
7910 return V;
7911 [[fallthrough]];
7912 case ISD::ROTL:
7913 case ISD::ROTR:
7914 case ISD::SSHLSAT:
7915 case ISD::USHLSAT:
7916 assert(VT == N1.getValueType() &&
7917 "Shift operators return type must be the same as their first arg");
7918 assert(VT.isInteger() && N2.getValueType().isInteger() &&
7919 "Shifts only work on integers");
7920 assert((!VT.isVector() || VT == N2.getValueType()) &&
7921 "Vector shift amounts must be in the same as their first arg");
7922 // Verify that the shift amount VT is big enough to hold valid shift
7923 // amounts. This catches things like trying to shift an i1024 value by an
7924 // i8, which is easy to fall into in generic code that uses
7925 // TLI.getShiftAmount().
7928 "Invalid use of small shift amount with oversized value!");
7929
7930 // Always fold shifts of i1 values so the code generator doesn't need to
7931 // handle them. Since we know the size of the shift has to be less than the
7932 // size of the value, the shift/rotate count is guaranteed to be zero.
7933 if (VT == MVT::i1)
7934 return N1;
7935 if (N2CV && N2CV->isZero())
7936 return N1;
7937 break;
7938 case ISD::FP_ROUND:
7940 VT.bitsLE(N1.getValueType()) && N2C &&
7941 (N2C->getZExtValue() == 0 || N2C->getZExtValue() == 1) &&
7942 N2.getOpcode() == ISD::TargetConstant && "Invalid FP_ROUND!");
7943 if (N1.getValueType() == VT) return N1; // noop conversion.
7944 break;
7945 case ISD::AssertNoFPClass: {
7947 "AssertNoFPClass is used for a non-floating type");
7948 assert(isa<ConstantSDNode>(N2) && "NoFPClass is not Constant");
7949 FPClassTest NoFPClass = static_cast<FPClassTest>(N2->getAsZExtVal());
7950 assert(llvm::to_underlying(NoFPClass) <=
7952 "FPClassTest value too large");
7953 (void)NoFPClass;
7954 break;
7955 }
7956 case ISD::AssertSext:
7957 case ISD::AssertZext: {
7958 EVT EVT = cast<VTSDNode>(N2)->getVT();
7959 assert(VT == N1.getValueType() && "Not an inreg extend!");
7960 assert(VT.isInteger() && EVT.isInteger() &&
7961 "Cannot *_EXTEND_INREG FP types");
7962 assert(!EVT.isVector() &&
7963 "AssertSExt/AssertZExt type should be the vector element type "
7964 "rather than the vector type!");
7965 assert(EVT.bitsLE(VT.getScalarType()) && "Not extending!");
7966 if (VT.getScalarType() == EVT) return N1; // noop assertion.
7967 break;
7968 }
7970 EVT EVT = cast<VTSDNode>(N2)->getVT();
7971 assert(VT == N1.getValueType() && "Not an inreg extend!");
7972 assert(VT.isInteger() && EVT.isInteger() &&
7973 "Cannot *_EXTEND_INREG FP types");
7974 assert(EVT.isVector() == VT.isVector() &&
7975 "SIGN_EXTEND_INREG type should be vector iff the operand "
7976 "type is vector!");
7977 assert((!EVT.isVector() ||
7979 "Vector element counts must match in SIGN_EXTEND_INREG");
7980 assert(EVT.getScalarType().bitsLE(VT.getScalarType()) && "Not extending!");
7981 if (EVT == VT) return N1; // Not actually extending
7982 break;
7983 }
7985 case ISD::FP_TO_UINT_SAT: {
7986 assert(VT.isInteger() && cast<VTSDNode>(N2)->getVT().isInteger() &&
7987 N1.getValueType().isFloatingPoint() && "Invalid FP_TO_*INT_SAT");
7988 assert(N1.getValueType().isVector() == VT.isVector() &&
7989 "FP_TO_*INT_SAT type should be vector iff the operand type is "
7990 "vector!");
7991 assert((!VT.isVector() || VT.getVectorElementCount() ==
7993 "Vector element counts must match in FP_TO_*INT_SAT");
7994 assert(!cast<VTSDNode>(N2)->getVT().isVector() &&
7995 "Type to saturate to must be a scalar.");
7996 assert(cast<VTSDNode>(N2)->getVT().bitsLE(VT.getScalarType()) &&
7997 "Not extending!");
7998 break;
7999 }
8002 "The result of EXTRACT_VECTOR_ELT must be at least as wide as the \
8003 element type of the vector.");
8004
8005 // Extract from an undefined value or using an undefined index is undefined.
8006 if (N1.isUndef() || N2.isUndef())
8007 return getUNDEF(VT);
8008
8009 // EXTRACT_VECTOR_ELT of out-of-bounds element is an UNDEF for fixed length
8010 // vectors. For scalable vectors we will provide appropriate support for
8011 // dealing with arbitrary indices.
8012 if (N2C && N1.getValueType().isFixedLengthVector() &&
8013 N2C->getAPIntValue().uge(N1.getValueType().getVectorNumElements()))
8014 return getUNDEF(VT);
8015
8016 // EXTRACT_VECTOR_ELT of CONCAT_VECTORS is often formed while lowering is
8017 // expanding copies of large vectors from registers. This only works for
8018 // fixed length vectors, since we need to know the exact number of
8019 // elements.
8020 if (N2C && N1.getOpcode() == ISD::CONCAT_VECTORS &&
8022 unsigned Factor = N1.getOperand(0).getValueType().getVectorNumElements();
8023 return getExtractVectorElt(DL, VT,
8024 N1.getOperand(N2C->getZExtValue() / Factor),
8025 N2C->getZExtValue() % Factor);
8026 }
8027
8028 // EXTRACT_VECTOR_ELT of BUILD_VECTOR or SPLAT_VECTOR is often formed while
8029 // lowering is expanding large vector constants.
8030 if (N2C && (N1.getOpcode() == ISD::BUILD_VECTOR ||
8031 N1.getOpcode() == ISD::SPLAT_VECTOR)) {
8034 "BUILD_VECTOR used for scalable vectors");
8035 unsigned Index =
8036 N1.getOpcode() == ISD::BUILD_VECTOR ? N2C->getZExtValue() : 0;
8037 SDValue Elt = N1.getOperand(Index);
8038
8039 if (VT != Elt.getValueType())
8040 // If the vector element type is not legal, the BUILD_VECTOR operands
8041 // are promoted and implicitly truncated, and the result implicitly
8042 // extended. Make that explicit here.
8043 Elt = getAnyExtOrTrunc(Elt, DL, VT);
8044
8045 return Elt;
8046 }
8047
8048 // EXTRACT_VECTOR_ELT of INSERT_VECTOR_ELT is often formed when vector
8049 // operations are lowered to scalars.
8050 if (N1.getOpcode() == ISD::INSERT_VECTOR_ELT) {
8051 // If the indices are the same, return the inserted element else
8052 // if the indices are known different, extract the element from
8053 // the original vector.
8054 SDValue N1Op2 = N1.getOperand(2);
8056
8057 if (N1Op2C && N2C) {
8058 if (N1Op2C->getZExtValue() == N2C->getZExtValue()) {
8059 if (VT == N1.getOperand(1).getValueType())
8060 return N1.getOperand(1);
8061 if (VT.isFloatingPoint()) {
8063 return getFPExtendOrRound(N1.getOperand(1), DL, VT);
8064 }
8065 return getSExtOrTrunc(N1.getOperand(1), DL, VT);
8066 }
8067 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0), N2);
8068 }
8069 }
8070
8071 // EXTRACT_VECTOR_ELT of v1iX EXTRACT_SUBVECTOR could be formed
8072 // when vector types are scalarized and v1iX is legal.
8073 // vextract (v1iX extract_subvector(vNiX, Idx)) -> vextract(vNiX,Idx).
8074 // Here we are completely ignoring the extract element index (N2),
8075 // which is fine for fixed width vectors, since any index other than 0
8076 // is undefined anyway. However, this cannot be ignored for scalable
8077 // vectors - in theory we could support this, but we don't want to do this
8078 // without a profitability check.
8079 if (N1.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8081 N1.getValueType().getVectorNumElements() == 1) {
8082 return getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, N1.getOperand(0),
8083 N1.getOperand(1));
8084 }
8085 break;
8087 assert(N2C && (unsigned)N2C->getZExtValue() < 2 && "Bad EXTRACT_ELEMENT!");
8088 assert(!N1.getValueType().isVector() && !VT.isVector() &&
8089 (N1.getValueType().isInteger() == VT.isInteger()) &&
8090 N1.getValueType() != VT &&
8091 "Wrong types for EXTRACT_ELEMENT!");
8092
8093 // EXTRACT_ELEMENT of BUILD_PAIR is often formed while legalize is expanding
8094 // 64-bit integers into 32-bit parts. Instead of building the extract of
8095 // the BUILD_PAIR, only to have legalize rip it apart, just do it now.
8096 if (N1.getOpcode() == ISD::BUILD_PAIR)
8097 return N1.getOperand(N2C->getZExtValue());
8098
8099 // EXTRACT_ELEMENT of a constant int is also very common.
8100 if (N1C) {
8101 unsigned ElementSize = VT.getSizeInBits();
8102 unsigned Shift = ElementSize * N2C->getZExtValue();
8103 const APInt &Val = N1C->getAPIntValue();
8104 return getConstant(Val.extractBits(ElementSize, Shift), DL, VT);
8105 }
8106 break;
8108 EVT N1VT = N1.getValueType();
8109 assert(VT.isVector() && N1VT.isVector() &&
8110 "Extract subvector VTs must be vectors!");
8112 "Extract subvector VTs must have the same element type!");
8113 assert((VT.isFixedLengthVector() || N1VT.isScalableVector()) &&
8114 "Cannot extract a scalable vector from a fixed length vector!");
8115 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8117 "Extract subvector must be from larger vector to smaller vector!");
8118 assert(N2C && "Extract subvector index must be a constant");
8119 assert((VT.isScalableVector() != N1VT.isScalableVector() ||
8120 (VT.getVectorMinNumElements() + N2C->getZExtValue()) <=
8121 N1VT.getVectorMinNumElements()) &&
8122 "Extract subvector overflow!");
8123 assert(N2C->getAPIntValue().getBitWidth() ==
8124 TLI->getVectorIdxWidth(getDataLayout()) &&
8125 "Constant index for EXTRACT_SUBVECTOR has an invalid size");
8126 assert(N2C->getZExtValue() % VT.getVectorMinNumElements() == 0 &&
8127 "Extract index is not a multiple of the output vector length");
8128
8129 // Trivial extraction.
8130 if (VT == N1VT)
8131 return N1;
8132
8133 // EXTRACT_SUBVECTOR of an UNDEF is an UNDEF.
8134 if (N1.isUndef())
8135 return getUNDEF(VT);
8136
8137 // EXTRACT_SUBVECTOR of CONCAT_VECTOR can be simplified if the pieces of
8138 // the concat have the same type as the extract.
8139 if (N1.getOpcode() == ISD::CONCAT_VECTORS &&
8140 VT == N1.getOperand(0).getValueType()) {
8141 unsigned Factor = VT.getVectorMinNumElements();
8142 return N1.getOperand(N2C->getZExtValue() / Factor);
8143 }
8144
8145 // EXTRACT_SUBVECTOR of INSERT_SUBVECTOR is often created
8146 // during shuffle legalization.
8147 if (N1.getOpcode() == ISD::INSERT_SUBVECTOR && N2 == N1.getOperand(2) &&
8148 VT == N1.getOperand(1).getValueType())
8149 return N1.getOperand(1);
8150 break;
8151 }
8152 }
8153
8154 if (N1.getOpcode() == ISD::POISON || N2.getOpcode() == ISD::POISON) {
8155 switch (Opcode) {
8156 case ISD::XOR:
8157 case ISD::ADD:
8158 case ISD::PTRADD:
8159 case ISD::SUB:
8161 case ISD::UDIV:
8162 case ISD::SDIV:
8163 case ISD::UREM:
8164 case ISD::SREM:
8165 case ISD::MUL:
8166 case ISD::AND:
8167 case ISD::SSUBSAT:
8168 case ISD::USUBSAT:
8169 case ISD::UMIN:
8170 case ISD::OR:
8171 case ISD::SADDSAT:
8172 case ISD::UADDSAT:
8173 case ISD::UMAX:
8174 case ISD::SMAX:
8175 case ISD::SMIN:
8176 // fold op(arg1, poison) -> poison, fold op(poison, arg2) -> poison.
8177 return N2.getOpcode() == ISD::POISON ? N2 : N1;
8178 }
8179 }
8180
8181 // Canonicalize an UNDEF to the RHS, even over a constant.
8182 if (N1.getOpcode() == ISD::UNDEF && N2.getOpcode() != ISD::UNDEF) {
8183 if (TLI->isCommutativeBinOp(Opcode)) {
8184 std::swap(N1, N2);
8185 } else {
8186 switch (Opcode) {
8187 case ISD::PTRADD:
8188 case ISD::SUB:
8189 // fold op(undef, non_undef_arg2) -> undef.
8190 return N1;
8192 case ISD::UDIV:
8193 case ISD::SDIV:
8194 case ISD::UREM:
8195 case ISD::SREM:
8196 case ISD::SSUBSAT:
8197 case ISD::USUBSAT:
8198 // fold op(undef, non_undef_arg2) -> 0.
8199 return getConstant(0, DL, VT);
8200 }
8201 }
8202 }
8203
8204 // Fold a bunch of operators when the RHS is undef.
8205 if (N2.getOpcode() == ISD::UNDEF) {
8206 switch (Opcode) {
8207 case ISD::XOR:
8208 if (N1.getOpcode() == ISD::UNDEF)
8209 // Handle undef ^ undef -> 0 special case. This is a common
8210 // idiom (misuse).
8211 return getConstant(0, DL, VT);
8212 [[fallthrough]];
8213 case ISD::ADD:
8214 case ISD::PTRADD:
8215 case ISD::SUB:
8216 // fold op(arg1, undef) -> undef.
8217 return N2;
8218 case ISD::UDIV:
8219 case ISD::SDIV:
8220 case ISD::UREM:
8221 case ISD::SREM:
8222 // fold op(arg1, undef) -> poison.
8223 return getPOISON(VT);
8224 case ISD::MUL:
8225 case ISD::AND:
8226 case ISD::SSUBSAT:
8227 case ISD::USUBSAT:
8228 case ISD::UMIN:
8229 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> 0.
8230 return N1.getOpcode() == ISD::UNDEF ? N2 : getConstant(0, DL, VT);
8231 case ISD::OR:
8232 case ISD::SADDSAT:
8233 case ISD::UADDSAT:
8234 case ISD::UMAX:
8235 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> -1.
8236 return N1.getOpcode() == ISD::UNDEF ? N2 : getAllOnesConstant(DL, VT);
8237 case ISD::SMAX:
8238 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MAX_INT.
8239 return N1.getOpcode() == ISD::UNDEF
8240 ? N2
8241 : getConstant(
8243 VT);
8244 case ISD::SMIN:
8245 // fold op(undef, undef) -> undef, fold op(arg1, undef) -> MIN_INT.
8246 return N1.getOpcode() == ISD::UNDEF
8247 ? N2
8248 : getConstant(
8250 VT);
8251 }
8252 }
8253
8254 // Perform trivial constant folding.
8255 if (SDValue SV = FoldConstantArithmetic(Opcode, DL, VT, {N1, N2}, Flags))
8256 return SV;
8257
8258 // Memoize this node if possible.
8259 SDNode *N;
8260 SDVTList VTs = getVTList(VT);
8261 SDValue Ops[] = {N1, N2};
8262 if (VT != MVT::Glue) {
8264 AddNodeIDNode(ID, Opcode, VTs, Ops);
8265 void *IP = nullptr;
8266 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8267 E->intersectFlagsWith(Flags);
8268 return SDValue(E, 0);
8269 }
8270
8271 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8272 N->setFlags(Flags);
8273 createOperands(N, Ops);
8274 CSEMap.InsertNode(N, IP);
8275 } else {
8276 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8277 createOperands(N, Ops);
8278 }
8279
8280 InsertNode(N);
8281 SDValue V = SDValue(N, 0);
8282 NewSDValueDbgMsg(V, "Creating new node: ", this);
8283 return V;
8284}
8285
8286SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8287 SDValue N1, SDValue N2, SDValue N3) {
8288 SDNodeFlags Flags;
8289 if (Inserter)
8290 Flags = Inserter->getFlags();
8291 return getNode(Opcode, DL, VT, N1, N2, N3, Flags);
8292}
8293
8294SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8295 SDValue N1, SDValue N2, SDValue N3,
8296 const SDNodeFlags Flags) {
8298 N2.getOpcode() != ISD::DELETED_NODE &&
8299 N3.getOpcode() != ISD::DELETED_NODE &&
8300 "Operand is DELETED_NODE!");
8301 // Perform various simplifications.
8302 switch (Opcode) {
8303 case ISD::BUILD_VECTOR: {
8304 // Attempt to simplify BUILD_VECTOR.
8305 SDValue Ops[] = {N1, N2, N3};
8306 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
8307 return V;
8308 break;
8309 }
8310 case ISD::CONCAT_VECTORS: {
8311 SDValue Ops[] = {N1, N2, N3};
8312 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
8313 return V;
8314 break;
8315 }
8316 case ISD::SETCC: {
8317 assert(VT.isInteger() && "SETCC result type must be an integer!");
8318 assert(N1.getValueType() == N2.getValueType() &&
8319 "SETCC operands must have the same type!");
8320 assert(VT.isVector() == N1.getValueType().isVector() &&
8321 "SETCC type should be vector iff the operand type is vector!");
8322 assert((!VT.isVector() || VT.getVectorElementCount() ==
8324 "SETCC vector element counts must match!");
8325 // Use FoldSetCC to simplify SETCC's.
8326 if (SDValue V = FoldSetCC(VT, N1, N2, cast<CondCodeSDNode>(N3)->get(), DL))
8327 return V;
8328 break;
8329 }
8330 case ISD::SELECT:
8331 case ISD::VSELECT:
8332 if (SDValue V = simplifySelect(N1, N2, N3))
8333 return V;
8334 break;
8336 llvm_unreachable("should use getVectorShuffle constructor!");
8338 if (isNullConstant(N3))
8339 return N1;
8340 break;
8342 if (isNullConstant(N3))
8343 return N2;
8344 break;
8346 assert(VT.isVector() && VT == N1.getValueType() &&
8347 "INSERT_VECTOR_ELT vector type mismatch");
8349 "INSERT_VECTOR_ELT scalar fp/int mismatch");
8350 assert((!VT.isFloatingPoint() ||
8351 VT.getVectorElementType() == N2.getValueType()) &&
8352 "INSERT_VECTOR_ELT fp scalar type mismatch");
8353 assert((!VT.isInteger() ||
8355 "INSERT_VECTOR_ELT int scalar size mismatch");
8356
8357 auto *N3C = dyn_cast<ConstantSDNode>(N3);
8358 // INSERT_VECTOR_ELT into out-of-bounds element is an UNDEF, except
8359 // for scalable vectors where we will generate appropriate code to
8360 // deal with out-of-bounds cases correctly.
8361 if (N3C && VT.isFixedLengthVector() &&
8362 N3C->getZExtValue() >= VT.getVectorNumElements())
8363 return getUNDEF(VT);
8364
8365 // Undefined index can be assumed out-of-bounds, so that's UNDEF too.
8366 if (N3.isUndef())
8367 return getUNDEF(VT);
8368
8369 // If inserting poison, just use the input vector.
8370 if (N2.getOpcode() == ISD::POISON)
8371 return N1;
8372
8373 // Inserting undef into undef/poison is still undef.
8374 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8375 return getUNDEF(VT);
8376
8377 // If the inserted element is an UNDEF, just use the input vector.
8378 // But not if skipping the insert could make the result more poisonous.
8379 if (N2.isUndef()) {
8380 if (N3C && VT.isFixedLengthVector()) {
8381 APInt EltMask =
8382 APInt::getOneBitSet(VT.getVectorNumElements(), N3C->getZExtValue());
8383 if (isGuaranteedNotToBePoison(N1, EltMask))
8384 return N1;
8385 } else if (isGuaranteedNotToBePoison(N1))
8386 return N1;
8387 }
8388 break;
8389 }
8390 case ISD::INSERT_SUBVECTOR: {
8391 // If inserting poison, just use the input vector,
8392 if (N2.getOpcode() == ISD::POISON)
8393 return N1;
8394
8395 // Inserting undef into undef/poison is still undef.
8396 if (N2.getOpcode() == ISD::UNDEF && N1.isUndef())
8397 return getUNDEF(VT);
8398
8399 EVT N2VT = N2.getValueType();
8400 assert(VT == N1.getValueType() &&
8401 "Dest and insert subvector source types must match!");
8402 assert(VT.isVector() && N2VT.isVector() &&
8403 "Insert subvector VTs must be vectors!");
8405 "Insert subvector VTs must have the same element type!");
8406 assert((VT.isScalableVector() || N2VT.isFixedLengthVector()) &&
8407 "Cannot insert a scalable vector into a fixed length vector!");
8408 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8410 "Insert subvector must be from smaller vector to larger vector!");
8412 "Insert subvector index must be constant");
8413 assert((VT.isScalableVector() != N2VT.isScalableVector() ||
8414 (N2VT.getVectorMinNumElements() + N3->getAsZExtVal()) <=
8416 "Insert subvector overflow!");
8418 TLI->getVectorIdxWidth(getDataLayout()) &&
8419 "Constant index for INSERT_SUBVECTOR has an invalid size");
8420
8421 // Trivial insertion.
8422 if (VT == N2VT)
8423 return N2;
8424
8425 // If this is an insert of an extracted vector into an undef/poison vector,
8426 // we can just use the input to the extract. But not if skipping the
8427 // extract+insert could make the result more poisonous.
8428 if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
8429 N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT) {
8430 if (N1.getOpcode() == ISD::POISON)
8431 return N2.getOperand(0);
8432 if (VT.isFixedLengthVector() && N2VT.isFixedLengthVector()) {
8433 unsigned LoBit = N3->getAsZExtVal();
8434 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8435 APInt EltMask =
8436 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8437 if (isGuaranteedNotToBePoison(N2.getOperand(0), ~EltMask))
8438 return N2.getOperand(0);
8439 } else if (isGuaranteedNotToBePoison(N2.getOperand(0)))
8440 return N2.getOperand(0);
8441 }
8442
8443 // If the inserted subvector is UNDEF, just use the input vector.
8444 // But not if skipping the insert could make the result more poisonous.
8445 if (N2.isUndef()) {
8446 if (VT.isFixedLengthVector()) {
8447 unsigned LoBit = N3->getAsZExtVal();
8448 unsigned HiBit = LoBit + N2VT.getVectorNumElements();
8449 APInt EltMask =
8450 APInt::getBitsSet(VT.getVectorNumElements(), LoBit, HiBit);
8451 if (isGuaranteedNotToBePoison(N1, EltMask))
8452 return N1;
8453 } else if (isGuaranteedNotToBePoison(N1))
8454 return N1;
8455 }
8456 break;
8457 }
8458 case ISD::BITCAST:
8459 // Fold bit_convert nodes from a type to themselves.
8460 if (N1.getValueType() == VT)
8461 return N1;
8462 break;
8463 case ISD::VP_TRUNCATE:
8464 case ISD::VP_SIGN_EXTEND:
8465 case ISD::VP_ZERO_EXTEND:
8466 // Don't create noop casts.
8467 if (N1.getValueType() == VT)
8468 return N1;
8469 break;
8470 case ISD::VECTOR_COMPRESS: {
8471 [[maybe_unused]] EVT VecVT = N1.getValueType();
8472 [[maybe_unused]] EVT MaskVT = N2.getValueType();
8473 [[maybe_unused]] EVT PassthruVT = N3.getValueType();
8474 assert(VT == VecVT && "Vector and result type don't match.");
8475 assert(VecVT.isVector() && MaskVT.isVector() && PassthruVT.isVector() &&
8476 "All inputs must be vectors.");
8477 assert(VecVT == PassthruVT && "Vector and passthru types don't match.");
8479 "Vector and mask must have same number of elements.");
8480
8481 if (N1.isUndef() || N2.isUndef())
8482 return N3;
8483
8484 break;
8485 }
8490 [[maybe_unused]] EVT AccVT = N1.getValueType();
8491 [[maybe_unused]] EVT Input1VT = N2.getValueType();
8492 [[maybe_unused]] EVT Input2VT = N3.getValueType();
8493 assert(Input1VT.isVector() && Input1VT == Input2VT &&
8494 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8495 "node to have the same type!");
8496 assert(VT.isVector() && VT == AccVT &&
8497 "Expected the first operand of the PARTIAL_REDUCE_MLA node to have "
8498 "the same type as its result!");
8500 AccVT.getVectorElementCount()) &&
8501 "Expected the element count of the second and third operands of the "
8502 "PARTIAL_REDUCE_MLA node to be a positive integer multiple of the "
8503 "element count of the first operand and the result!");
8505 "Expected the second and third operands of the PARTIAL_REDUCE_MLA "
8506 "node to have an element type which is the same as or smaller than "
8507 "the element type of the first operand and result!");
8508 break;
8509 }
8510 }
8511
8512 // Perform trivial constant folding for arithmetic operators.
8513 switch (Opcode) {
8514 case ISD::FMA:
8515 case ISD::FMAD:
8516 case ISD::SETCC:
8517 case ISD::FSHL:
8518 case ISD::FSHR:
8519 if (SDValue SV =
8520 FoldConstantArithmetic(Opcode, DL, VT, {N1, N2, N3}, Flags))
8521 return SV;
8522 break;
8523 }
8524
8525 // Memoize node if it doesn't produce a glue result.
8526 SDNode *N;
8527 SDVTList VTs = getVTList(VT);
8528 SDValue Ops[] = {N1, N2, N3};
8529 if (VT != MVT::Glue) {
8531 AddNodeIDNode(ID, Opcode, VTs, Ops);
8532 void *IP = nullptr;
8533 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
8534 E->intersectFlagsWith(Flags);
8535 return SDValue(E, 0);
8536 }
8537
8538 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8539 N->setFlags(Flags);
8540 createOperands(N, Ops);
8541 CSEMap.InsertNode(N, IP);
8542 } else {
8543 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
8544 createOperands(N, Ops);
8545 }
8546
8547 InsertNode(N);
8548 SDValue V = SDValue(N, 0);
8549 NewSDValueDbgMsg(V, "Creating new node: ", this);
8550 return V;
8551}
8552
8553SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8554 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8555 const SDNodeFlags Flags) {
8556 SDValue Ops[] = { N1, N2, N3, N4 };
8557 return getNode(Opcode, DL, VT, Ops, Flags);
8558}
8559
8560SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8561 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
8562 SDNodeFlags Flags;
8563 if (Inserter)
8564 Flags = Inserter->getFlags();
8565 return getNode(Opcode, DL, VT, N1, N2, N3, N4, Flags);
8566}
8567
8568SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8569 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8570 SDValue N5, const SDNodeFlags Flags) {
8571 SDValue Ops[] = { N1, N2, N3, N4, N5 };
8572 return getNode(Opcode, DL, VT, Ops, Flags);
8573}
8574
8575SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
8576 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
8577 SDValue N5) {
8578 SDNodeFlags Flags;
8579 if (Inserter)
8580 Flags = Inserter->getFlags();
8581 return getNode(Opcode, DL, VT, N1, N2, N3, N4, N5, Flags);
8582}
8583
8584/// getStackArgumentTokenFactor - Compute a TokenFactor to force all
8585/// the incoming stack arguments to be loaded from the stack.
8587 SmallVector<SDValue, 8> ArgChains;
8588
8589 // Include the original chain at the beginning of the list. When this is
8590 // used by target LowerCall hooks, this helps legalize find the
8591 // CALLSEQ_BEGIN node.
8592 ArgChains.push_back(Chain);
8593
8594 // Add a chain value for each stack argument.
8595 for (SDNode *U : getEntryNode().getNode()->users())
8596 if (LoadSDNode *L = dyn_cast<LoadSDNode>(U))
8597 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(L->getBasePtr()))
8598 if (FI->getIndex() < 0)
8599 ArgChains.push_back(SDValue(L, 1));
8600
8601 // Build a tokenfactor for all the chains.
8602 return getNode(ISD::TokenFactor, SDLoc(Chain), MVT::Other, ArgChains);
8603}
8604
8605/// getMemsetValue - Vectorized representation of the memset value
8606/// operand.
8608 const SDLoc &dl) {
8609 assert(!Value.isUndef());
8610
8611 unsigned NumBits = VT.getScalarSizeInBits();
8613 assert(C->getAPIntValue().getBitWidth() == 8);
8614 APInt Val = APInt::getSplat(NumBits, C->getAPIntValue());
8615 if (VT.isInteger()) {
8616 bool IsOpaque = VT.getSizeInBits() > 64 ||
8617 !DAG.getTargetLoweringInfo().isLegalStoreImmediate(C->getSExtValue());
8618 return DAG.getConstant(Val, dl, VT, false, IsOpaque);
8619 }
8620 return DAG.getConstantFP(APFloat(VT.getFltSemantics(), Val), dl, VT);
8621 }
8622
8623 assert(Value.getValueType() == MVT::i8 && "memset with non-byte fill value?");
8624 EVT IntVT = VT.getScalarType();
8625 if (!IntVT.isInteger())
8626 IntVT = EVT::getIntegerVT(*DAG.getContext(), IntVT.getSizeInBits());
8627
8628 Value = DAG.getNode(ISD::ZERO_EXTEND, dl, IntVT, Value);
8629 if (NumBits > 8) {
8630 // Use a multiplication with 0x010101... to extend the input to the
8631 // required length.
8632 APInt Magic = APInt::getSplat(NumBits, APInt(8, 0x01));
8633 Value = DAG.getNode(ISD::MUL, dl, IntVT, Value,
8634 DAG.getConstant(Magic, dl, IntVT));
8635 }
8636
8637 if (VT != Value.getValueType() && !VT.isInteger())
8638 Value = DAG.getBitcast(VT.getScalarType(), Value);
8639 if (VT != Value.getValueType())
8640 Value = DAG.getSplatBuildVector(VT, dl, Value);
8641
8642 return Value;
8643}
8644
8645/// getMemsetStringVal - Similar to getMemsetValue. Except this is only
8646/// used when a memcpy is turned into a memset when the source is a constant
8647/// string ptr.
8649 const TargetLowering &TLI,
8650 const ConstantDataArraySlice &Slice) {
8651 // Handle vector with all elements zero.
8652 if (Slice.Array == nullptr) {
8653 if (VT.isInteger())
8654 return DAG.getConstant(0, dl, VT);
8655 return DAG.getNode(ISD::BITCAST, dl, VT,
8656 DAG.getConstant(0, dl, VT.changeTypeToInteger()));
8657 }
8658
8659 assert(!VT.isVector() && "Can't handle vector type here!");
8660 unsigned NumVTBits = VT.getSizeInBits();
8661 unsigned NumVTBytes = NumVTBits / 8;
8662 unsigned NumBytes = std::min(NumVTBytes, unsigned(Slice.Length));
8663
8664 APInt Val(NumVTBits, 0);
8665 if (DAG.getDataLayout().isLittleEndian()) {
8666 for (unsigned i = 0; i != NumBytes; ++i)
8667 Val |= (uint64_t)(unsigned char)Slice[i] << i*8;
8668 } else {
8669 for (unsigned i = 0; i != NumBytes; ++i)
8670 Val |= (uint64_t)(unsigned char)Slice[i] << (NumVTBytes-i-1)*8;
8671 }
8672
8673 // If the "cost" of materializing the integer immediate is less than the cost
8674 // of a load, then it is cost effective to turn the load into the immediate.
8675 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
8676 if (TLI.shouldConvertConstantLoadToIntImm(Val, Ty))
8677 return DAG.getConstant(Val, dl, VT);
8678 return SDValue();
8679}
8680
8682 const SDLoc &DL,
8683 const SDNodeFlags Flags) {
8684 SDValue Index = getTypeSize(DL, Base.getValueType(), Offset);
8685 return getMemBasePlusOffset(Base, Index, DL, Flags);
8686}
8687
8689 const SDLoc &DL,
8690 const SDNodeFlags Flags) {
8691 assert(Offset.getValueType().isInteger());
8692 EVT BasePtrVT = Ptr.getValueType();
8693 if (TLI->shouldPreservePtrArith(this->getMachineFunction().getFunction(),
8694 BasePtrVT))
8695 return getNode(ISD::PTRADD, DL, BasePtrVT, Ptr, Offset, Flags);
8696 // InBounds only applies to PTRADD, don't set it if we generate ADD.
8697 SDNodeFlags AddFlags = Flags;
8698 AddFlags.setInBounds(false);
8699 return getNode(ISD::ADD, DL, BasePtrVT, Ptr, Offset, AddFlags);
8700}
8701
8702/// Returns true if memcpy source is constant data.
8704 uint64_t SrcDelta = 0;
8705 GlobalAddressSDNode *G = nullptr;
8706 if (Src.getOpcode() == ISD::GlobalAddress)
8708 else if (Src->isAnyAdd() &&
8709 Src.getOperand(0).getOpcode() == ISD::GlobalAddress &&
8710 Src.getOperand(1).getOpcode() == ISD::Constant) {
8711 G = cast<GlobalAddressSDNode>(Src.getOperand(0));
8712 SrcDelta = Src.getConstantOperandVal(1);
8713 }
8714 if (!G)
8715 return false;
8716
8717 return getConstantDataArrayInfo(G->getGlobal(), Slice, 8,
8718 SrcDelta + G->getOffset());
8719}
8720
8722 SelectionDAG &DAG) {
8723 // On Darwin, -Os means optimize for size without hurting performance, so
8724 // only really optimize for size when -Oz (MinSize) is used.
8726 return MF.getFunction().hasMinSize();
8727 return DAG.shouldOptForSize();
8728}
8729
8731 SmallVector<SDValue, 32> &OutChains, unsigned From,
8732 unsigned To, SmallVector<SDValue, 16> &OutLoadChains,
8733 SmallVector<SDValue, 16> &OutStoreChains) {
8734 assert(OutLoadChains.size() && "Missing loads in memcpy inlining");
8735 assert(OutStoreChains.size() && "Missing stores in memcpy inlining");
8736 SmallVector<SDValue, 16> GluedLoadChains;
8737 for (unsigned i = From; i < To; ++i) {
8738 OutChains.push_back(OutLoadChains[i]);
8739 GluedLoadChains.push_back(OutLoadChains[i]);
8740 }
8741
8742 // Chain for all loads.
8743 SDValue LoadToken = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
8744 GluedLoadChains);
8745
8746 for (unsigned i = From; i < To; ++i) {
8747 StoreSDNode *ST = dyn_cast<StoreSDNode>(OutStoreChains[i]);
8748 SDValue NewStore = DAG.getTruncStore(LoadToken, dl, ST->getValue(),
8749 ST->getBasePtr(), ST->getMemoryVT(),
8750 ST->getMemOperand());
8751 OutChains.push_back(NewStore);
8752 }
8753}
8754
8756 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
8757 uint64_t Size, Align Alignment, bool isVol, bool AlwaysInline,
8758 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo,
8759 const AAMDNodes &AAInfo, BatchAAResults *BatchAA) {
8760 // Turn a memcpy of undef to nop.
8761 // FIXME: We need to honor volatile even is Src is undef.
8762 if (Src.isUndef())
8763 return Chain;
8764
8765 // Expand memcpy to a series of load and store ops if the size operand falls
8766 // below a certain threshold.
8767 // TODO: In the AlwaysInline case, if the size is big then generate a loop
8768 // rather than maybe a humongous number of loads and stores.
8769 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8770 const DataLayout &DL = DAG.getDataLayout();
8771 LLVMContext &C = *DAG.getContext();
8772 std::vector<EVT> MemOps;
8773 bool DstAlignCanChange = false;
8775 MachineFrameInfo &MFI = MF.getFrameInfo();
8776 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8778 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8779 DstAlignCanChange = true;
8780 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8781 if (!SrcAlign || Alignment > *SrcAlign)
8782 SrcAlign = Alignment;
8783 assert(SrcAlign && "SrcAlign must be set");
8785 // If marked as volatile, perform a copy even when marked as constant.
8786 bool CopyFromConstant = !isVol && isMemSrcFromConstant(Src, Slice);
8787 bool isZeroConstant = CopyFromConstant && Slice.Array == nullptr;
8788 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemcpy(OptSize);
8789 const MemOp Op = isZeroConstant
8790 ? MemOp::Set(Size, DstAlignCanChange, Alignment,
8791 /*IsZeroMemset*/ true, isVol)
8792 : MemOp::Copy(Size, DstAlignCanChange, Alignment,
8793 *SrcAlign, isVol, CopyFromConstant);
8794 if (!TLI.findOptimalMemOpLowering(
8795 C, MemOps, Limit, Op, DstPtrInfo.getAddrSpace(),
8796 SrcPtrInfo.getAddrSpace(), MF.getFunction().getAttributes(), nullptr))
8797 return SDValue();
8798
8799 if (DstAlignCanChange) {
8800 Type *Ty = MemOps[0].getTypeForEVT(C);
8801 Align NewAlign = DL.getABITypeAlign(Ty);
8802
8803 // Don't promote to an alignment that would require dynamic stack
8804 // realignment which may conflict with optimizations such as tail call
8805 // optimization.
8807 if (!TRI->hasStackRealignment(MF))
8808 if (MaybeAlign StackAlign = DL.getStackAlignment())
8809 NewAlign = std::min(NewAlign, *StackAlign);
8810
8811 if (NewAlign > Alignment) {
8812 // Give the stack frame object a larger alignment if needed.
8813 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
8814 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
8815 Alignment = NewAlign;
8816 }
8817 }
8818
8819 // Prepare AAInfo for loads/stores after lowering this memcpy.
8820 AAMDNodes NewAAInfo = AAInfo;
8821 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
8822
8823 const Value *SrcVal = dyn_cast_if_present<const Value *>(SrcPtrInfo.V);
8824 bool isConstant =
8825 BatchAA && SrcVal &&
8826 BatchAA->pointsToConstantMemory(MemoryLocation(SrcVal, Size, AAInfo));
8827
8828 MachineMemOperand::Flags MMOFlags =
8830 SmallVector<SDValue, 16> OutLoadChains;
8831 SmallVector<SDValue, 16> OutStoreChains;
8832 SmallVector<SDValue, 32> OutChains;
8833 unsigned NumMemOps = MemOps.size();
8834 uint64_t SrcOff = 0, DstOff = 0;
8835 for (unsigned i = 0; i != NumMemOps; ++i) {
8836 EVT VT = MemOps[i];
8837 unsigned VTSize = VT.getSizeInBits() / 8;
8838 SDValue Value, Store;
8839
8840 if (VTSize > Size) {
8841 // Issuing an unaligned load / store pair that overlaps with the previous
8842 // pair. Adjust the offset accordingly.
8843 assert(i == NumMemOps-1 && i != 0);
8844 SrcOff -= VTSize - Size;
8845 DstOff -= VTSize - Size;
8846 }
8847
8848 if (CopyFromConstant &&
8849 (isZeroConstant || (VT.isInteger() && !VT.isVector()))) {
8850 // It's unlikely a store of a vector immediate can be done in a single
8851 // instruction. It would require a load from a constantpool first.
8852 // We only handle zero vectors here.
8853 // FIXME: Handle other cases where store of vector immediate is done in
8854 // a single instruction.
8855 ConstantDataArraySlice SubSlice;
8856 if (SrcOff < Slice.Length) {
8857 SubSlice = Slice;
8858 SubSlice.move(SrcOff);
8859 } else {
8860 // This is an out-of-bounds access and hence UB. Pretend we read zero.
8861 SubSlice.Array = nullptr;
8862 SubSlice.Offset = 0;
8863 SubSlice.Length = VTSize;
8864 }
8865 Value = getMemsetStringVal(VT, dl, DAG, TLI, SubSlice);
8866 if (Value.getNode()) {
8867 Store = DAG.getStore(
8868 Chain, dl, Value,
8869 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8870 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
8871 OutChains.push_back(Store);
8872 }
8873 }
8874
8875 if (!Store.getNode()) {
8876 // The type might not be legal for the target. This should only happen
8877 // if the type is smaller than a legal type, as on PPC, so the right
8878 // thing to do is generate a LoadExt/StoreTrunc pair. These simplify
8879 // to Load/Store if NVT==VT.
8880 // FIXME does the case above also need this?
8881 EVT NVT = TLI.getTypeToTransformTo(C, VT);
8882 assert(NVT.bitsGE(VT));
8883
8884 bool isDereferenceable =
8885 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
8886 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
8887 if (isDereferenceable)
8889 if (isConstant)
8890 SrcMMOFlags |= MachineMemOperand::MOInvariant;
8891
8892 Value = DAG.getExtLoad(
8893 ISD::EXTLOAD, dl, NVT, Chain,
8894 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
8895 SrcPtrInfo.getWithOffset(SrcOff), VT,
8896 commonAlignment(*SrcAlign, SrcOff), SrcMMOFlags, NewAAInfo);
8897 OutLoadChains.push_back(Value.getValue(1));
8898
8899 Store = DAG.getTruncStore(
8900 Chain, dl, Value,
8901 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
8902 DstPtrInfo.getWithOffset(DstOff), VT, Alignment, MMOFlags, NewAAInfo);
8903 OutStoreChains.push_back(Store);
8904 }
8905 SrcOff += VTSize;
8906 DstOff += VTSize;
8907 Size -= VTSize;
8908 }
8909
8910 unsigned GluedLdStLimit = MaxLdStGlue == 0 ?
8912 unsigned NumLdStInMemcpy = OutStoreChains.size();
8913
8914 if (NumLdStInMemcpy) {
8915 // It may be that memcpy might be converted to memset if it's memcpy
8916 // of constants. In such a case, we won't have loads and stores, but
8917 // just stores. In the absence of loads, there is nothing to gang up.
8918 if ((GluedLdStLimit <= 1) || !EnableMemCpyDAGOpt) {
8919 // If target does not care, just leave as it.
8920 for (unsigned i = 0; i < NumLdStInMemcpy; ++i) {
8921 OutChains.push_back(OutLoadChains[i]);
8922 OutChains.push_back(OutStoreChains[i]);
8923 }
8924 } else {
8925 // Ld/St less than/equal limit set by target.
8926 if (NumLdStInMemcpy <= GluedLdStLimit) {
8927 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, 0,
8928 NumLdStInMemcpy, OutLoadChains,
8929 OutStoreChains);
8930 } else {
8931 unsigned NumberLdChain = NumLdStInMemcpy / GluedLdStLimit;
8932 unsigned RemainingLdStInMemcpy = NumLdStInMemcpy % GluedLdStLimit;
8933 unsigned GlueIter = 0;
8934
8935 // Residual ld/st.
8936 if (RemainingLdStInMemcpy) {
8938 DAG, dl, OutChains, NumLdStInMemcpy - RemainingLdStInMemcpy,
8939 NumLdStInMemcpy, OutLoadChains, OutStoreChains);
8940 }
8941
8942 for (unsigned cnt = 0; cnt < NumberLdChain; ++cnt) {
8943 unsigned IndexFrom = NumLdStInMemcpy - RemainingLdStInMemcpy -
8944 GlueIter - GluedLdStLimit;
8945 unsigned IndexTo = NumLdStInMemcpy - RemainingLdStInMemcpy - GlueIter;
8946 chainLoadsAndStoresForMemcpy(DAG, dl, OutChains, IndexFrom, IndexTo,
8947 OutLoadChains, OutStoreChains);
8948 GlueIter += GluedLdStLimit;
8949 }
8950 }
8951 }
8952 }
8953 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
8954}
8955
8957 SDValue Chain, SDValue Dst, SDValue Src,
8958 uint64_t Size, Align Alignment,
8959 bool isVol, bool AlwaysInline,
8960 MachinePointerInfo DstPtrInfo,
8961 MachinePointerInfo SrcPtrInfo,
8962 const AAMDNodes &AAInfo) {
8963 // Turn a memmove of undef to nop.
8964 // FIXME: We need to honor volatile even is Src is undef.
8965 if (Src.isUndef())
8966 return Chain;
8967
8968 // Expand memmove to a series of load and store ops if the size operand falls
8969 // below a certain threshold.
8970 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
8971 const DataLayout &DL = DAG.getDataLayout();
8972 LLVMContext &C = *DAG.getContext();
8973 std::vector<EVT> MemOps;
8974 bool DstAlignCanChange = false;
8976 MachineFrameInfo &MFI = MF.getFrameInfo();
8977 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
8979 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
8980 DstAlignCanChange = true;
8981 MaybeAlign SrcAlign = DAG.InferPtrAlign(Src);
8982 if (!SrcAlign || Alignment > *SrcAlign)
8983 SrcAlign = Alignment;
8984 assert(SrcAlign && "SrcAlign must be set");
8985 unsigned Limit = AlwaysInline ? ~0U : TLI.getMaxStoresPerMemmove(OptSize);
8986 if (!TLI.findOptimalMemOpLowering(
8987 C, MemOps, Limit,
8988 MemOp::Copy(Size, DstAlignCanChange, Alignment, *SrcAlign,
8989 /*IsVolatile*/ true),
8990 DstPtrInfo.getAddrSpace(), SrcPtrInfo.getAddrSpace(),
8991 MF.getFunction().getAttributes(), nullptr))
8992 return SDValue();
8993
8994 if (DstAlignCanChange) {
8995 Type *Ty = MemOps[0].getTypeForEVT(C);
8996 Align NewAlign = DL.getABITypeAlign(Ty);
8997
8998 // Don't promote to an alignment that would require dynamic stack
8999 // realignment which may conflict with optimizations such as tail call
9000 // optimization.
9002 if (!TRI->hasStackRealignment(MF))
9003 if (MaybeAlign StackAlign = DL.getStackAlignment())
9004 NewAlign = std::min(NewAlign, *StackAlign);
9005
9006 if (NewAlign > Alignment) {
9007 // Give the stack frame object a larger alignment if needed.
9008 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9009 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9010 Alignment = NewAlign;
9011 }
9012 }
9013
9014 // Prepare AAInfo for loads/stores after lowering this memmove.
9015 AAMDNodes NewAAInfo = AAInfo;
9016 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9017
9018 MachineMemOperand::Flags MMOFlags =
9020 uint64_t SrcOff = 0, DstOff = 0;
9021 SmallVector<SDValue, 8> LoadValues;
9022 SmallVector<SDValue, 8> LoadChains;
9023 SmallVector<SDValue, 8> OutChains;
9024 unsigned NumMemOps = MemOps.size();
9025 for (unsigned i = 0; i < NumMemOps; i++) {
9026 EVT VT = MemOps[i];
9027 unsigned VTSize = VT.getSizeInBits() / 8;
9028 SDValue Value;
9029
9030 bool isDereferenceable =
9031 SrcPtrInfo.getWithOffset(SrcOff).isDereferenceable(VTSize, C, DL);
9032 MachineMemOperand::Flags SrcMMOFlags = MMOFlags;
9033 if (isDereferenceable)
9035
9036 Value = DAG.getLoad(
9037 VT, dl, Chain,
9038 DAG.getObjectPtrOffset(dl, Src, TypeSize::getFixed(SrcOff)),
9039 SrcPtrInfo.getWithOffset(SrcOff), *SrcAlign, SrcMMOFlags, NewAAInfo);
9040 LoadValues.push_back(Value);
9041 LoadChains.push_back(Value.getValue(1));
9042 SrcOff += VTSize;
9043 }
9044 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, LoadChains);
9045 OutChains.clear();
9046 for (unsigned i = 0; i < NumMemOps; i++) {
9047 EVT VT = MemOps[i];
9048 unsigned VTSize = VT.getSizeInBits() / 8;
9049 SDValue Store;
9050
9051 Store = DAG.getStore(
9052 Chain, dl, LoadValues[i],
9053 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9054 DstPtrInfo.getWithOffset(DstOff), Alignment, MMOFlags, NewAAInfo);
9055 OutChains.push_back(Store);
9056 DstOff += VTSize;
9057 }
9058
9059 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9060}
9061
9062/// Lower the call to 'memset' intrinsic function into a series of store
9063/// operations.
9064///
9065/// \param DAG Selection DAG where lowered code is placed.
9066/// \param dl Link to corresponding IR location.
9067/// \param Chain Control flow dependency.
9068/// \param Dst Pointer to destination memory location.
9069/// \param Src Value of byte to write into the memory.
9070/// \param Size Number of bytes to write.
9071/// \param Alignment Alignment of the destination in bytes.
9072/// \param isVol True if destination is volatile.
9073/// \param AlwaysInline Makes sure no function call is generated.
9074/// \param DstPtrInfo IR information on the memory pointer.
9075/// \returns New head in the control flow, if lowering was successful, empty
9076/// SDValue otherwise.
9077///
9078/// The function tries to replace 'llvm.memset' intrinsic with several store
9079/// operations and value calculation code. This is usually profitable for small
9080/// memory size or when the semantic requires inlining.
9082 SDValue Chain, SDValue Dst, SDValue Src,
9083 uint64_t Size, Align Alignment, bool isVol,
9084 bool AlwaysInline, MachinePointerInfo DstPtrInfo,
9085 const AAMDNodes &AAInfo) {
9086 // Turn a memset of undef to nop.
9087 // FIXME: We need to honor volatile even is Src is undef.
9088 if (Src.isUndef())
9089 return Chain;
9090
9091 // Expand memset to a series of load/store ops if the size operand
9092 // falls below a certain threshold.
9093 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
9094 std::vector<EVT> MemOps;
9095 bool DstAlignCanChange = false;
9096 LLVMContext &C = *DAG.getContext();
9098 MachineFrameInfo &MFI = MF.getFrameInfo();
9099 bool OptSize = shouldLowerMemFuncForSize(MF, DAG);
9101 if (FI && !MFI.isFixedObjectIndex(FI->getIndex()))
9102 DstAlignCanChange = true;
9103 bool IsZeroVal = isNullConstant(Src);
9104 unsigned Limit = AlwaysInline ? ~0 : TLI.getMaxStoresPerMemset(OptSize);
9105
9106 EVT LargestVT;
9107 if (!TLI.findOptimalMemOpLowering(
9108 C, MemOps, Limit,
9109 MemOp::Set(Size, DstAlignCanChange, Alignment, IsZeroVal, isVol),
9110 DstPtrInfo.getAddrSpace(), ~0u, MF.getFunction().getAttributes(),
9111 &LargestVT))
9112 return SDValue();
9113
9114 if (DstAlignCanChange) {
9115 Type *Ty = MemOps[0].getTypeForEVT(*DAG.getContext());
9116 const DataLayout &DL = DAG.getDataLayout();
9117 Align NewAlign = DL.getABITypeAlign(Ty);
9118
9119 // Don't promote to an alignment that would require dynamic stack
9120 // realignment which may conflict with optimizations such as tail call
9121 // optimization.
9123 if (!TRI->hasStackRealignment(MF))
9124 if (MaybeAlign StackAlign = DL.getStackAlignment())
9125 NewAlign = std::min(NewAlign, *StackAlign);
9126
9127 if (NewAlign > Alignment) {
9128 // Give the stack frame object a larger alignment if needed.
9129 if (MFI.getObjectAlign(FI->getIndex()) < NewAlign)
9130 MFI.setObjectAlignment(FI->getIndex(), NewAlign);
9131 Alignment = NewAlign;
9132 }
9133 }
9134
9135 SmallVector<SDValue, 8> OutChains;
9136 uint64_t DstOff = 0;
9137 unsigned NumMemOps = MemOps.size();
9138
9139 // Find the largest store and generate the bit pattern for it.
9140 // If target didn't set LargestVT, compute it from MemOps.
9141 if (!LargestVT.isSimple()) {
9142 LargestVT = MemOps[0];
9143 for (unsigned i = 1; i < NumMemOps; i++)
9144 if (MemOps[i].bitsGT(LargestVT))
9145 LargestVT = MemOps[i];
9146 }
9147 SDValue MemSetValue = getMemsetValue(Src, LargestVT, DAG, dl);
9148
9149 // Prepare AAInfo for loads/stores after lowering this memset.
9150 AAMDNodes NewAAInfo = AAInfo;
9151 NewAAInfo.TBAA = NewAAInfo.TBAAStruct = nullptr;
9152
9153 for (unsigned i = 0; i < NumMemOps; i++) {
9154 EVT VT = MemOps[i];
9155 unsigned VTSize = VT.getSizeInBits() / 8;
9156 // The target should specify store types that exactly cover the memset size
9157 // (with the last store potentially being oversized for overlapping stores).
9158 assert(Size > 0 && "Target specified more stores than needed in "
9159 "findOptimalMemOpLowering");
9160 if (VTSize > Size) {
9161 // Issuing an unaligned load / store pair that overlaps with the previous
9162 // pair. Adjust the offset accordingly.
9163 assert(i == NumMemOps-1 && i != 0);
9164 DstOff -= VTSize - Size;
9165 }
9166
9167 // If this store is smaller than the largest store see whether we can get
9168 // the smaller value for free with a truncate or extract vector element and
9169 // then store.
9170 SDValue Value = MemSetValue;
9171 if (VT.bitsLT(LargestVT)) {
9172 unsigned Index;
9173 unsigned NElts = LargestVT.getSizeInBits() / VT.getSizeInBits();
9174 EVT SVT = EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), NElts);
9175 if (!LargestVT.isVector() && !VT.isVector() &&
9176 TLI.isTruncateFree(LargestVT, VT))
9177 Value = DAG.getNode(ISD::TRUNCATE, dl, VT, MemSetValue);
9178 else if (LargestVT.isVector() && !VT.isVector() &&
9180 LargestVT.getTypeForEVT(*DAG.getContext()),
9181 VT.getSizeInBits(), Index) &&
9182 TLI.isTypeLegal(SVT) &&
9183 LargestVT.getSizeInBits() == SVT.getSizeInBits()) {
9184 // Target which can combine store(extractelement VectorTy, Idx) can get
9185 // the smaller value for free.
9186 SDValue TailValue = DAG.getNode(ISD::BITCAST, dl, SVT, MemSetValue);
9187 Value = DAG.getExtractVectorElt(dl, VT, TailValue, Index);
9188 } else
9189 Value = getMemsetValue(Src, VT, DAG, dl);
9190 }
9191 assert(Value.getValueType() == VT && "Value with wrong type.");
9192 SDValue Store = DAG.getStore(
9193 Chain, dl, Value,
9194 DAG.getObjectPtrOffset(dl, Dst, TypeSize::getFixed(DstOff)),
9195 DstPtrInfo.getWithOffset(DstOff), Alignment,
9197 NewAAInfo);
9198 OutChains.push_back(Store);
9199 DstOff += VT.getSizeInBits() / 8;
9200 // For oversized overlapping stores, only subtract the remaining bytes.
9201 // For normal stores, subtract the full store size.
9202 if (VTSize > Size) {
9203 Size = 0;
9204 } else {
9205 Size -= VTSize;
9206 }
9207 }
9208
9209 // After processing all stores, Size should be exactly 0. Any remaining bytes
9210 // indicate a bug in the target's findOptimalMemOpLowering implementation.
9211 assert(Size == 0 && "Target's findOptimalMemOpLowering did not specify "
9212 "stores that exactly cover the memset size");
9213
9214 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
9215}
9216
9218 unsigned AS) {
9219 // Lowering memcpy / memset / memmove intrinsics to calls is only valid if all
9220 // pointer operands can be losslessly bitcasted to pointers of address space 0
9221 if (AS != 0 && !TLI->getTargetMachine().isNoopAddrSpaceCast(AS, 0)) {
9222 report_fatal_error("cannot lower memory intrinsic in address space " +
9223 Twine(AS));
9224 }
9225}
9226
9228 const SelectionDAG *SelDAG,
9229 bool AllowReturnsFirstArg) {
9230 if (!CI || !CI->isTailCall())
9231 return false;
9232 // TODO: Fix "returns-first-arg" determination so it doesn't depend on which
9233 // helper symbol we lower to.
9234 return isInTailCallPosition(*CI, SelDAG->getTarget(),
9235 AllowReturnsFirstArg &&
9237}
9238
9239static std::pair<SDValue, SDValue>
9242 const CallInst *CI, RTLIB::Libcall Call,
9243 SelectionDAG *DAG, const TargetLowering *TLI) {
9244 RTLIB::LibcallImpl LCImpl = DAG->getLibcalls().getLibcallImpl(Call);
9245
9246 if (LCImpl == RTLIB::Unsupported)
9247 return {};
9248
9250 bool IsTailCall =
9251 isInTailCallPositionWrapper(CI, DAG, /*AllowReturnsFirstArg=*/true);
9252 SDValue Callee =
9253 DAG->getExternalSymbol(LCImpl, TLI->getPointerTy(DAG->getDataLayout()));
9254
9255 CLI.setDebugLoc(dl)
9256 .setChain(Chain)
9258 CI->getType(), Callee, std::move(Args))
9259 .setTailCall(IsTailCall);
9260
9261 return TLI->LowerCallTo(CLI);
9262}
9263
9264std::pair<SDValue, SDValue> SelectionDAG::getStrstr(SDValue Chain,
9265 const SDLoc &dl, SDValue S1,
9266 SDValue S2,
9267 const CallInst *CI) {
9269 TargetLowering::ArgListTy Args = {{S1, PT}, {S2, PT}};
9270 return getRuntimeCallSDValueHelper(Chain, dl, std::move(Args), CI,
9271 RTLIB::STRSTR, this, TLI);
9272}
9273
9274std::pair<SDValue, SDValue>
9276 SDValue Mem1, SDValue Size, const CallInst *CI) {
9277 RTLIB::LibcallImpl MemcmpImpl = Libcalls->getLibcallImpl(RTLIB::MEMCMP);
9278 if (MemcmpImpl == RTLIB::Unsupported)
9279 return {};
9280
9283 {Mem0, PT},
9284 {Mem1, PT},
9286
9288 bool IsTailCall =
9289 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9290
9291 CLI.setDebugLoc(dl)
9292 .setChain(Chain)
9293 .setLibCallee(
9294 Libcalls->getLibcallImplCallingConv(MemcmpImpl),
9296 getExternalSymbol(MemcmpImpl, TLI->getPointerTy(getDataLayout())),
9297 std::move(Args))
9298 .setTailCall(IsTailCall);
9299
9300 return TLI->LowerCallTo(CLI);
9301}
9302
9303std::pair<SDValue, SDValue> SelectionDAG::getStrcpy(SDValue Chain,
9304 const SDLoc &dl,
9305 SDValue Dst, SDValue Src,
9306 const CallInst *CI) {
9307 RTLIB::LibcallImpl LCImpl = Libcalls->getLibcallImpl(RTLIB::STRCPY);
9308 if (LCImpl == RTLIB::Unsupported)
9309 return {};
9310
9312 TargetLowering::ArgListTy Args = {{Dst, PT}, {Src, PT}};
9313
9315 bool IsTailCall =
9316 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg=*/true);
9317
9318 CLI.setDebugLoc(dl)
9319 .setChain(Chain)
9320 .setLibCallee(
9321 Libcalls->getLibcallImplCallingConv(LCImpl), CI->getType(),
9322 getExternalSymbol(LCImpl, TLI->getPointerTy(getDataLayout())),
9323 std::move(Args))
9324 .setTailCall(IsTailCall);
9325
9326 return TLI->LowerCallTo(CLI);
9327}
9328
9329std::pair<SDValue, SDValue> SelectionDAG::getStrlen(SDValue Chain,
9330 const SDLoc &dl,
9331 SDValue Src,
9332 const CallInst *CI) {
9333 RTLIB::LibcallImpl StrlenImpl = Libcalls->getLibcallImpl(RTLIB::STRLEN);
9334 if (StrlenImpl == RTLIB::Unsupported)
9335 return {};
9336
9337 // Emit a library call.
9340
9342 bool IsTailCall =
9343 isInTailCallPositionWrapper(CI, this, /*AllowReturnsFirstArg*/ true);
9344
9345 CLI.setDebugLoc(dl)
9346 .setChain(Chain)
9347 .setLibCallee(Libcalls->getLibcallImplCallingConv(StrlenImpl),
9348 CI->getType(),
9350 StrlenImpl, TLI->getProgramPointerTy(getDataLayout())),
9351 std::move(Args))
9352 .setTailCall(IsTailCall);
9353
9354 return TLI->LowerCallTo(CLI);
9355}
9356
9358 SDValue Chain, const SDLoc &dl, SDValue Dst, SDValue Src, SDValue Size,
9359 Align Alignment, bool isVol, bool AlwaysInline, const CallInst *CI,
9360 std::optional<bool> OverrideTailCall, MachinePointerInfo DstPtrInfo,
9361 MachinePointerInfo SrcPtrInfo, const AAMDNodes &AAInfo,
9362 BatchAAResults *BatchAA) {
9363 // Check to see if we should lower the memcpy to loads and stores first.
9364 // For cases within the target-specified limits, this is the best choice.
9366 if (ConstantSize) {
9367 // Memcpy with size zero? Just return the original chain.
9368 if (ConstantSize->isZero())
9369 return Chain;
9370
9372 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9373 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9374 if (Result.getNode())
9375 return Result;
9376 }
9377
9378 // Then check to see if we should lower the memcpy with target-specific
9379 // code. If the target chooses to do this, this is the next best.
9380 if (TSI) {
9381 SDValue Result = TSI->EmitTargetCodeForMemcpy(
9382 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline,
9383 DstPtrInfo, SrcPtrInfo);
9384 if (Result.getNode())
9385 return Result;
9386 }
9387
9388 // If we really need inline code and the target declined to provide it,
9389 // use a (potentially long) sequence of loads and stores.
9390 if (AlwaysInline) {
9391 assert(ConstantSize && "AlwaysInline requires a constant size!");
9393 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9394 isVol, true, DstPtrInfo, SrcPtrInfo, AAInfo, BatchAA);
9395 }
9396
9399
9400 // FIXME: If the memcpy is volatile (isVol), lowering it to a plain libc
9401 // memcpy is not guaranteed to be safe. libc memcpys aren't required to
9402 // respect volatile, so they may do things like read or write memory
9403 // beyond the given memory regions. But fixing this isn't easy, and most
9404 // people don't care.
9405
9406 // Emit a library call.
9409 Args.emplace_back(Dst, PtrTy);
9410 Args.emplace_back(Src, PtrTy);
9411 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9412 // FIXME: pass in SDLoc
9414 bool IsTailCall = false;
9415 RTLIB::LibcallImpl MemCpyImpl = TLI->getMemcpyImpl();
9416
9417 if (OverrideTailCall.has_value()) {
9418 IsTailCall = *OverrideTailCall;
9419 } else {
9420 bool LowersToMemcpy = MemCpyImpl == RTLIB::impl_memcpy;
9421 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemcpy);
9422 }
9423
9424 CLI.setDebugLoc(dl)
9425 .setChain(Chain)
9426 .setLibCallee(
9427 Libcalls->getLibcallImplCallingConv(MemCpyImpl),
9428 Dst.getValueType().getTypeForEVT(*getContext()),
9429 getExternalSymbol(MemCpyImpl, TLI->getPointerTy(getDataLayout())),
9430 std::move(Args))
9432 .setTailCall(IsTailCall);
9433
9434 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9435 return CallResult.second;
9436}
9437
9439 SDValue Dst, SDValue Src, SDValue Size,
9440 Type *SizeTy, unsigned ElemSz,
9441 bool isTailCall,
9442 MachinePointerInfo DstPtrInfo,
9443 MachinePointerInfo SrcPtrInfo) {
9444 // Emit a library call.
9447 Args.emplace_back(Dst, ArgTy);
9448 Args.emplace_back(Src, ArgTy);
9449 Args.emplace_back(Size, SizeTy);
9450
9451 RTLIB::Libcall LibraryCall =
9453 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9454 if (LibcallImpl == RTLIB::Unsupported)
9455 report_fatal_error("Unsupported element size");
9456
9458 CLI.setDebugLoc(dl)
9459 .setChain(Chain)
9460 .setLibCallee(
9461 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9463 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9464 std::move(Args))
9466 .setTailCall(isTailCall);
9467
9468 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9469 return CallResult.second;
9470}
9471
9473 SDValue Src, SDValue Size, Align Alignment,
9474 bool isVol, const CallInst *CI,
9475 std::optional<bool> OverrideTailCall,
9476 MachinePointerInfo DstPtrInfo,
9477 MachinePointerInfo SrcPtrInfo,
9478 const AAMDNodes &AAInfo,
9479 BatchAAResults *BatchAA) {
9480 // Check to see if we should lower the memmove to loads and stores first.
9481 // For cases within the target-specified limits, this is the best choice.
9483 if (ConstantSize) {
9484 // Memmove with size zero? Just return the original chain.
9485 if (ConstantSize->isZero())
9486 return Chain;
9487
9489 *this, dl, Chain, Dst, Src, ConstantSize->getZExtValue(), Alignment,
9490 isVol, false, DstPtrInfo, SrcPtrInfo, AAInfo);
9491 if (Result.getNode())
9492 return Result;
9493 }
9494
9495 // Then check to see if we should lower the memmove with target-specific
9496 // code. If the target chooses to do this, this is the next best.
9497 if (TSI) {
9498 SDValue Result =
9499 TSI->EmitTargetCodeForMemmove(*this, dl, Chain, Dst, Src, Size,
9500 Alignment, isVol, DstPtrInfo, SrcPtrInfo);
9501 if (Result.getNode())
9502 return Result;
9503 }
9504
9507
9508 // FIXME: If the memmove is volatile, lowering it to plain libc memmove may
9509 // not be safe. See memcpy above for more details.
9510
9511 // Emit a library call.
9514 Args.emplace_back(Dst, PtrTy);
9515 Args.emplace_back(Src, PtrTy);
9516 Args.emplace_back(Size, getDataLayout().getIntPtrType(*getContext()));
9517 // FIXME: pass in SDLoc
9519
9520 RTLIB::LibcallImpl MemmoveImpl = Libcalls->getLibcallImpl(RTLIB::MEMMOVE);
9521
9522 bool IsTailCall = false;
9523 if (OverrideTailCall.has_value()) {
9524 IsTailCall = *OverrideTailCall;
9525 } else {
9526 bool LowersToMemmove = MemmoveImpl == RTLIB::impl_memmove;
9527 IsTailCall = isInTailCallPositionWrapper(CI, this, LowersToMemmove);
9528 }
9529
9530 CLI.setDebugLoc(dl)
9531 .setChain(Chain)
9532 .setLibCallee(
9533 Libcalls->getLibcallImplCallingConv(MemmoveImpl),
9534 Dst.getValueType().getTypeForEVT(*getContext()),
9535 getExternalSymbol(MemmoveImpl, TLI->getPointerTy(getDataLayout())),
9536 std::move(Args))
9538 .setTailCall(IsTailCall);
9539
9540 std::pair<SDValue,SDValue> CallResult = TLI->LowerCallTo(CLI);
9541 return CallResult.second;
9542}
9543
9545 SDValue Dst, SDValue Src, SDValue Size,
9546 Type *SizeTy, unsigned ElemSz,
9547 bool isTailCall,
9548 MachinePointerInfo DstPtrInfo,
9549 MachinePointerInfo SrcPtrInfo) {
9550 // Emit a library call.
9552 Type *IntPtrTy = getDataLayout().getIntPtrType(*getContext());
9553 Args.emplace_back(Dst, IntPtrTy);
9554 Args.emplace_back(Src, IntPtrTy);
9555 Args.emplace_back(Size, SizeTy);
9556
9557 RTLIB::Libcall LibraryCall =
9559 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9560 if (LibcallImpl == RTLIB::Unsupported)
9561 report_fatal_error("Unsupported element size");
9562
9564 CLI.setDebugLoc(dl)
9565 .setChain(Chain)
9566 .setLibCallee(
9567 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9569 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9570 std::move(Args))
9572 .setTailCall(isTailCall);
9573
9574 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9575 return CallResult.second;
9576}
9577
9579 SDValue Src, SDValue Size, Align Alignment,
9580 bool isVol, bool AlwaysInline,
9581 const CallInst *CI,
9582 MachinePointerInfo DstPtrInfo,
9583 const AAMDNodes &AAInfo) {
9584 // Check to see if we should lower the memset to stores first.
9585 // For cases within the target-specified limits, this is the best choice.
9587 if (ConstantSize) {
9588 // Memset with size zero? Just return the original chain.
9589 if (ConstantSize->isZero())
9590 return Chain;
9591
9592 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9593 ConstantSize->getZExtValue(), Alignment,
9594 isVol, false, DstPtrInfo, AAInfo);
9595
9596 if (Result.getNode())
9597 return Result;
9598 }
9599
9600 // Then check to see if we should lower the memset with target-specific
9601 // code. If the target chooses to do this, this is the next best.
9602 if (TSI) {
9603 SDValue Result = TSI->EmitTargetCodeForMemset(
9604 *this, dl, Chain, Dst, Src, Size, Alignment, isVol, AlwaysInline, DstPtrInfo);
9605 if (Result.getNode())
9606 return Result;
9607 }
9608
9609 // If we really need inline code and the target declined to provide it,
9610 // use a (potentially long) sequence of loads and stores.
9611 if (AlwaysInline) {
9612 assert(ConstantSize && "AlwaysInline requires a constant size!");
9613 SDValue Result = getMemsetStores(*this, dl, Chain, Dst, Src,
9614 ConstantSize->getZExtValue(), Alignment,
9615 isVol, true, DstPtrInfo, AAInfo);
9616 assert(Result &&
9617 "getMemsetStores must return a valid sequence when AlwaysInline");
9618 return Result;
9619 }
9620
9622
9623 // Emit a library call.
9624 auto &Ctx = *getContext();
9625 const auto& DL = getDataLayout();
9626
9628 // FIXME: pass in SDLoc
9629 CLI.setDebugLoc(dl).setChain(Chain);
9630
9631 RTLIB::LibcallImpl BzeroImpl = Libcalls->getLibcallImpl(RTLIB::BZERO);
9632 bool UseBZero = BzeroImpl != RTLIB::Unsupported && isNullConstant(Src);
9633
9634 // If zeroing out and bzero is present, use it.
9635 if (UseBZero) {
9637 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9638 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9639 CLI.setLibCallee(
9640 Libcalls->getLibcallImplCallingConv(BzeroImpl), Type::getVoidTy(Ctx),
9641 getExternalSymbol(BzeroImpl, TLI->getPointerTy(DL)), std::move(Args));
9642 } else {
9643 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9644
9646 Args.emplace_back(Dst, PointerType::getUnqual(Ctx));
9647 Args.emplace_back(Src, Src.getValueType().getTypeForEVT(Ctx));
9648 Args.emplace_back(Size, DL.getIntPtrType(Ctx));
9649 CLI.setLibCallee(Libcalls->getLibcallImplCallingConv(MemsetImpl),
9650 Dst.getValueType().getTypeForEVT(Ctx),
9651 getExternalSymbol(MemsetImpl, TLI->getPointerTy(DL)),
9652 std::move(Args));
9653 }
9654
9655 RTLIB::LibcallImpl MemsetImpl = Libcalls->getLibcallImpl(RTLIB::MEMSET);
9656 bool LowersToMemset = MemsetImpl == RTLIB::impl_memset;
9657
9658 // If we're going to use bzero, make sure not to tail call unless the
9659 // subsequent return doesn't need a value, as bzero doesn't return the first
9660 // arg unlike memset.
9661 bool ReturnsFirstArg = CI && funcReturnsFirstArgOfCall(*CI) && !UseBZero;
9662 bool IsTailCall =
9663 CI && CI->isTailCall() &&
9664 isInTailCallPosition(*CI, getTarget(), ReturnsFirstArg && LowersToMemset);
9665 CLI.setDiscardResult().setTailCall(IsTailCall);
9666
9667 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9668 return CallResult.second;
9669}
9670
9673 Type *SizeTy, unsigned ElemSz,
9674 bool isTailCall,
9675 MachinePointerInfo DstPtrInfo) {
9676 // Emit a library call.
9678 Args.emplace_back(Dst, getDataLayout().getIntPtrType(*getContext()));
9679 Args.emplace_back(Value, Type::getInt8Ty(*getContext()));
9680 Args.emplace_back(Size, SizeTy);
9681
9682 RTLIB::Libcall LibraryCall =
9684 RTLIB::LibcallImpl LibcallImpl = Libcalls->getLibcallImpl(LibraryCall);
9685 if (LibcallImpl == RTLIB::Unsupported)
9686 report_fatal_error("Unsupported element size");
9687
9689 CLI.setDebugLoc(dl)
9690 .setChain(Chain)
9691 .setLibCallee(
9692 Libcalls->getLibcallImplCallingConv(LibcallImpl),
9694 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout())),
9695 std::move(Args))
9697 .setTailCall(isTailCall);
9698
9699 std::pair<SDValue, SDValue> CallResult = TLI->LowerCallTo(CLI);
9700 return CallResult.second;
9701}
9702
9703SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9705 MachineMemOperand *MMO,
9706 ISD::LoadExtType ExtType) {
9708 AddNodeIDNode(ID, Opcode, VTList, Ops);
9709 ID.AddInteger(MemVT.getRawBits());
9710 ID.AddInteger(getSyntheticNodeSubclassData<AtomicSDNode>(
9711 dl.getIROrder(), Opcode, VTList, MemVT, MMO, ExtType));
9712 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9713 ID.AddInteger(MMO->getFlags());
9714 void* IP = nullptr;
9715 if (auto *E = cast_or_null<AtomicSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
9716 E->refineAlignment(MMO);
9717 E->refineRanges(MMO);
9718 return SDValue(E, 0);
9719 }
9720
9721 auto *N = newSDNode<AtomicSDNode>(dl.getIROrder(), dl.getDebugLoc(), Opcode,
9722 VTList, MemVT, MMO, ExtType);
9723 createOperands(N, Ops);
9724
9725 CSEMap.InsertNode(N, IP);
9726 InsertNode(N);
9727 SDValue V(N, 0);
9728 NewSDValueDbgMsg(V, "Creating new node: ", this);
9729 return V;
9730}
9731
9733 EVT MemVT, SDVTList VTs, SDValue Chain,
9734 SDValue Ptr, SDValue Cmp, SDValue Swp,
9735 MachineMemOperand *MMO) {
9736 assert(Opcode == ISD::ATOMIC_CMP_SWAP ||
9738 assert(Cmp.getValueType() == Swp.getValueType() && "Invalid Atomic Op Types");
9739
9740 SDValue Ops[] = {Chain, Ptr, Cmp, Swp};
9741 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9742}
9743
9744SDValue SelectionDAG::getAtomic(unsigned Opcode, const SDLoc &dl, EVT MemVT,
9745 SDValue Chain, SDValue Ptr, SDValue Val,
9746 MachineMemOperand *MMO) {
9747 assert((Opcode == ISD::ATOMIC_LOAD_ADD || Opcode == ISD::ATOMIC_LOAD_SUB ||
9748 Opcode == ISD::ATOMIC_LOAD_AND || Opcode == ISD::ATOMIC_LOAD_CLR ||
9749 Opcode == ISD::ATOMIC_LOAD_OR || Opcode == ISD::ATOMIC_LOAD_XOR ||
9750 Opcode == ISD::ATOMIC_LOAD_NAND || Opcode == ISD::ATOMIC_LOAD_MIN ||
9751 Opcode == ISD::ATOMIC_LOAD_MAX || Opcode == ISD::ATOMIC_LOAD_UMIN ||
9752 Opcode == ISD::ATOMIC_LOAD_UMAX || Opcode == ISD::ATOMIC_LOAD_FADD ||
9753 Opcode == ISD::ATOMIC_LOAD_FSUB || Opcode == ISD::ATOMIC_LOAD_FMAX ||
9754 Opcode == ISD::ATOMIC_LOAD_FMIN ||
9755 Opcode == ISD::ATOMIC_LOAD_FMINIMUM ||
9756 Opcode == ISD::ATOMIC_LOAD_FMAXIMUM ||
9757 Opcode == ISD::ATOMIC_LOAD_UINC_WRAP ||
9758 Opcode == ISD::ATOMIC_LOAD_UDEC_WRAP ||
9759 Opcode == ISD::ATOMIC_LOAD_USUB_COND ||
9760 Opcode == ISD::ATOMIC_LOAD_USUB_SAT || Opcode == ISD::ATOMIC_SWAP ||
9761 Opcode == ISD::ATOMIC_STORE) &&
9762 "Invalid Atomic Op");
9763
9764 EVT VT = Val.getValueType();
9765
9766 SDVTList VTs = Opcode == ISD::ATOMIC_STORE ? getVTList(MVT::Other) :
9767 getVTList(VT, MVT::Other);
9768 SDValue Ops[] = {Chain, Ptr, Val};
9769 return getAtomic(Opcode, dl, MemVT, VTs, Ops, MMO);
9770}
9771
9773 EVT MemVT, EVT VT, SDValue Chain,
9774 SDValue Ptr, MachineMemOperand *MMO) {
9775 SDVTList VTs = getVTList(VT, MVT::Other);
9776 SDValue Ops[] = {Chain, Ptr};
9777 return getAtomic(ISD::ATOMIC_LOAD, dl, MemVT, VTs, Ops, MMO, ExtType);
9778}
9779
9780/// getMergeValues - Create a MERGE_VALUES node from the given operands.
9782 if (Ops.size() == 1)
9783 return Ops[0];
9784
9786 VTs.reserve(Ops.size());
9787 for (const SDValue &Op : Ops)
9788 VTs.push_back(Op.getValueType());
9789 return getNode(ISD::MERGE_VALUES, dl, getVTList(VTs), Ops);
9790}
9791
9793 unsigned Opcode, const SDLoc &dl, SDVTList VTList, ArrayRef<SDValue> Ops,
9794 EVT MemVT, MachinePointerInfo PtrInfo, Align Alignment,
9796 const AAMDNodes &AAInfo) {
9797 if (Size.hasValue() && !Size.getValue())
9799
9801 MachineMemOperand *MMO =
9802 MF.getMachineMemOperand(PtrInfo, Flags, Size, Alignment, AAInfo);
9803
9804 return getMemIntrinsicNode(Opcode, dl, VTList, Ops, MemVT, MMO);
9805}
9806
9808 SDVTList VTList,
9809 ArrayRef<SDValue> Ops, EVT MemVT,
9810 MachineMemOperand *MMO) {
9811 assert(
9812 (Opcode == ISD::INTRINSIC_VOID || Opcode == ISD::INTRINSIC_W_CHAIN ||
9813 Opcode == ISD::PREFETCH ||
9814 (Opcode <= (unsigned)std::numeric_limits<int>::max() &&
9815 Opcode >= ISD::BUILTIN_OP_END && TSI->isTargetMemoryOpcode(Opcode))) &&
9816 "Opcode is not a memory-accessing opcode!");
9817
9818 // Memoize the node unless it returns a glue result.
9820 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
9822 AddNodeIDNode(ID, Opcode, VTList, Ops);
9823 ID.AddInteger(getSyntheticNodeSubclassData<MemIntrinsicSDNode>(
9824 Opcode, dl.getIROrder(), VTList, MemVT, MMO));
9825 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
9826 ID.AddInteger(MMO->getFlags());
9827 ID.AddInteger(MemVT.getRawBits());
9828 void *IP = nullptr;
9829 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
9830 cast<MemIntrinsicSDNode>(E)->refineAlignment(MMO);
9831 return SDValue(E, 0);
9832 }
9833
9834 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9835 VTList, MemVT, MMO);
9836 createOperands(N, Ops);
9837
9838 CSEMap.InsertNode(N, IP);
9839 } else {
9840 N = newSDNode<MemIntrinsicSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(),
9841 VTList, MemVT, MMO);
9842 createOperands(N, Ops);
9843 }
9844 InsertNode(N);
9845 SDValue V(N, 0);
9846 NewSDValueDbgMsg(V, "Creating new node: ", this);
9847 return V;
9848}
9849
9851 SDValue Chain, int FrameIndex) {
9852 const unsigned Opcode = IsStart ? ISD::LIFETIME_START : ISD::LIFETIME_END;
9853 const auto VTs = getVTList(MVT::Other);
9854 SDValue Ops[2] = {
9855 Chain,
9856 getFrameIndex(FrameIndex,
9857 getTargetLoweringInfo().getFrameIndexTy(getDataLayout()),
9858 true)};
9859
9861 AddNodeIDNode(ID, Opcode, VTs, Ops);
9862 ID.AddInteger(FrameIndex);
9863 void *IP = nullptr;
9864 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
9865 return SDValue(E, 0);
9866
9867 LifetimeSDNode *N =
9868 newSDNode<LifetimeSDNode>(Opcode, dl.getIROrder(), dl.getDebugLoc(), VTs);
9869 createOperands(N, Ops);
9870 CSEMap.InsertNode(N, IP);
9871 InsertNode(N);
9872 SDValue V(N, 0);
9873 NewSDValueDbgMsg(V, "Creating new node: ", this);
9874 return V;
9875}
9876
9878 uint64_t Guid, uint64_t Index,
9879 uint32_t Attr) {
9880 const unsigned Opcode = ISD::PSEUDO_PROBE;
9881 const auto VTs = getVTList(MVT::Other);
9882 SDValue Ops[] = {Chain};
9884 AddNodeIDNode(ID, Opcode, VTs, Ops);
9885 ID.AddInteger(Guid);
9886 ID.AddInteger(Index);
9887 void *IP = nullptr;
9888 if (SDNode *E = FindNodeOrInsertPos(ID, Dl, IP))
9889 return SDValue(E, 0);
9890
9891 auto *N = newSDNode<PseudoProbeSDNode>(
9892 Opcode, Dl.getIROrder(), Dl.getDebugLoc(), VTs, Guid, Index, Attr);
9893 createOperands(N, Ops);
9894 CSEMap.InsertNode(N, IP);
9895 InsertNode(N);
9896 SDValue V(N, 0);
9897 NewSDValueDbgMsg(V, "Creating new node: ", this);
9898 return V;
9899}
9900
9901/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9902/// MachinePointerInfo record from it. This is particularly useful because the
9903/// code generator has many cases where it doesn't bother passing in a
9904/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9906 SelectionDAG &DAG, SDValue Ptr,
9907 int64_t Offset = 0) {
9908 // If this is FI+Offset, we can model it.
9909 if (const FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr))
9911 FI->getIndex(), Offset);
9912
9913 // If this is (FI+Offset1)+Offset2, we can model it.
9914 if (Ptr.getOpcode() != ISD::ADD ||
9917 return Info;
9918
9919 int FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
9921 DAG.getMachineFunction(), FI,
9922 Offset + cast<ConstantSDNode>(Ptr.getOperand(1))->getSExtValue());
9923}
9924
9925/// InferPointerInfo - If the specified ptr/offset is a frame index, infer a
9926/// MachinePointerInfo record from it. This is particularly useful because the
9927/// code generator has many cases where it doesn't bother passing in a
9928/// MachinePointerInfo to getLoad or getStore when it has "FI+Cst".
9930 SelectionDAG &DAG, SDValue Ptr,
9931 SDValue OffsetOp) {
9932 // If the 'Offset' value isn't a constant, we can't handle this.
9934 return InferPointerInfo(Info, DAG, Ptr, OffsetNode->getSExtValue());
9935 if (OffsetOp.isUndef())
9936 return InferPointerInfo(Info, DAG, Ptr);
9937 return Info;
9938}
9939
9941 EVT VT, const SDLoc &dl, SDValue Chain,
9942 SDValue Ptr, SDValue Offset,
9943 MachinePointerInfo PtrInfo, EVT MemVT,
9944 Align Alignment,
9945 MachineMemOperand::Flags MMOFlags,
9946 const AAMDNodes &AAInfo, const MDNode *Ranges) {
9947 assert(Chain.getValueType() == MVT::Other &&
9948 "Invalid chain type");
9949
9950 MMOFlags |= MachineMemOperand::MOLoad;
9951 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
9952 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
9953 // clients.
9954 if (PtrInfo.V.isNull())
9955 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
9956
9957 TypeSize Size = MemVT.getStoreSize();
9959 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
9960 Alignment, AAInfo, Ranges);
9961 return getLoad(AM, ExtType, VT, dl, Chain, Ptr, Offset, MemVT, MMO);
9962}
9963
9965 EVT VT, const SDLoc &dl, SDValue Chain,
9966 SDValue Ptr, SDValue Offset, EVT MemVT,
9967 MachineMemOperand *MMO) {
9968 if (VT == MemVT) {
9969 ExtType = ISD::NON_EXTLOAD;
9970 } else if (ExtType == ISD::NON_EXTLOAD) {
9971 assert(VT == MemVT && "Non-extending load from different memory type!");
9972 } else {
9973 // Extending load.
9974 assert(MemVT.getScalarType().bitsLT(VT.getScalarType()) &&
9975 "Should only be an extending load, not truncating!");
9976 assert(VT.isInteger() == MemVT.isInteger() &&
9977 "Cannot convert from FP to Int or Int -> FP!");
9978 assert(VT.isVector() == MemVT.isVector() &&
9979 "Cannot use an ext load to convert to or from a vector!");
9980 assert((!VT.isVector() ||
9982 "Cannot use an ext load to change the number of vector elements!");
9983 }
9984
9985 assert((!MMO->getRanges() ||
9987 ->getBitWidth() == MemVT.getScalarSizeInBits() &&
9988 MemVT.isInteger())) &&
9989 "Range metadata and load type must match!");
9990
9991 bool Indexed = AM != ISD::UNINDEXED;
9992 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
9993
9994 SDVTList VTs = Indexed ?
9995 getVTList(VT, Ptr.getValueType(), MVT::Other) : getVTList(VT, MVT::Other);
9996 SDValue Ops[] = { Chain, Ptr, Offset };
9998 AddNodeIDNode(ID, ISD::LOAD, VTs, Ops);
9999 ID.AddInteger(MemVT.getRawBits());
10000 ID.AddInteger(getSyntheticNodeSubclassData<LoadSDNode>(
10001 dl.getIROrder(), VTs, AM, ExtType, MemVT, MMO));
10002 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10003 ID.AddInteger(MMO->getFlags());
10004 void *IP = nullptr;
10005 if (auto *E = cast_or_null<LoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10006 E->refineAlignment(MMO);
10007 E->refineRanges(MMO);
10008 return SDValue(E, 0);
10009 }
10010 auto *N = newSDNode<LoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10011 ExtType, MemVT, MMO);
10012 createOperands(N, Ops);
10013
10014 CSEMap.InsertNode(N, IP);
10015 InsertNode(N);
10016 SDValue V(N, 0);
10017 NewSDValueDbgMsg(V, "Creating new node: ", this);
10018 return V;
10019}
10020
10022 SDValue Ptr, MachinePointerInfo PtrInfo,
10023 MaybeAlign Alignment,
10024 MachineMemOperand::Flags MMOFlags,
10025 const AAMDNodes &AAInfo, const MDNode *Ranges) {
10027 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10028 PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges);
10029}
10030
10032 SDValue Ptr, MachineMemOperand *MMO) {
10034 return getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10035 VT, MMO);
10036}
10037
10039 EVT VT, SDValue Chain, SDValue Ptr,
10040 MachinePointerInfo PtrInfo, EVT MemVT,
10041 MaybeAlign Alignment,
10042 MachineMemOperand::Flags MMOFlags,
10043 const AAMDNodes &AAInfo) {
10045 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, PtrInfo,
10046 MemVT, Alignment, MMOFlags, AAInfo);
10047}
10048
10050 EVT VT, SDValue Chain, SDValue Ptr, EVT MemVT,
10051 MachineMemOperand *MMO) {
10053 return getLoad(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef,
10054 MemVT, MMO);
10055}
10056
10060 LoadSDNode *LD = cast<LoadSDNode>(OrigLoad);
10061 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10062 // Don't propagate the invariant or dereferenceable flags.
10063 auto MMOFlags =
10064 LD->getMemOperand()->getFlags() &
10066 return getLoad(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10067 LD->getChain(), Base, Offset, LD->getPointerInfo(),
10068 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo());
10069}
10070
10072 SDValue Ptr, MachinePointerInfo PtrInfo,
10073 Align Alignment,
10074 MachineMemOperand::Flags MMOFlags,
10075 const AAMDNodes &AAInfo) {
10076 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10077
10078 MMOFlags |= MachineMemOperand::MOStore;
10079 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10080
10081 if (PtrInfo.V.isNull())
10082 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10083
10086 MachineMemOperand *MMO =
10087 MF.getMachineMemOperand(PtrInfo, MMOFlags, Size, Alignment, AAInfo);
10088 return getStore(Chain, dl, Val, Ptr, MMO);
10089}
10090
10092 SDValue Ptr, MachineMemOperand *MMO) {
10094 return getStore(Chain, dl, Val, Ptr, Undef, Val.getValueType(), MMO,
10096}
10097
10099 SDValue Ptr, SDValue Offset, EVT SVT,
10101 bool IsTruncating) {
10102 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10103 EVT VT = Val.getValueType();
10104 if (VT == SVT) {
10105 IsTruncating = false;
10106 } else if (!IsTruncating) {
10107 assert(VT == SVT && "No-truncating store from different memory type!");
10108 } else {
10110 "Should only be a truncating store, not extending!");
10111 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10112 assert(VT.isVector() == SVT.isVector() &&
10113 "Cannot use trunc store to convert to or from a vector!");
10114 assert((!VT.isVector() ||
10116 "Cannot use trunc store to change the number of vector elements!");
10117 }
10118
10119 bool Indexed = AM != ISD::UNINDEXED;
10120 assert((Indexed || Offset.isUndef()) && "Unindexed store with an offset!");
10121 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10122 : getVTList(MVT::Other);
10123 SDValue Ops[] = {Chain, Val, Ptr, Offset};
10126 ID.AddInteger(SVT.getRawBits());
10127 ID.AddInteger(getSyntheticNodeSubclassData<StoreSDNode>(
10128 dl.getIROrder(), VTs, AM, IsTruncating, SVT, MMO));
10129 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10130 ID.AddInteger(MMO->getFlags());
10131 void *IP = nullptr;
10132 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10133 cast<StoreSDNode>(E)->refineAlignment(MMO);
10134 return SDValue(E, 0);
10135 }
10136 auto *N = newSDNode<StoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10137 IsTruncating, SVT, MMO);
10138 createOperands(N, Ops);
10139
10140 CSEMap.InsertNode(N, IP);
10141 InsertNode(N);
10142 SDValue V(N, 0);
10143 NewSDValueDbgMsg(V, "Creating new node: ", this);
10144 return V;
10145}
10146
10148 SDValue Ptr, MachinePointerInfo PtrInfo,
10149 EVT SVT, Align Alignment,
10150 MachineMemOperand::Flags MMOFlags,
10151 const AAMDNodes &AAInfo) {
10152 assert(Chain.getValueType() == MVT::Other &&
10153 "Invalid chain type");
10154
10155 MMOFlags |= MachineMemOperand::MOStore;
10156 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10157
10158 if (PtrInfo.V.isNull())
10159 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10160
10162 MachineMemOperand *MMO = MF.getMachineMemOperand(
10163 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10164 return getTruncStore(Chain, dl, Val, Ptr, SVT, MMO);
10165}
10166
10168 SDValue Ptr, EVT SVT,
10169 MachineMemOperand *MMO) {
10171 return getStore(Chain, dl, Val, Ptr, Undef, SVT, MMO, ISD::UNINDEXED, true);
10172}
10173
10177 StoreSDNode *ST = cast<StoreSDNode>(OrigStore);
10178 assert(ST->getOffset().isUndef() && "Store is already a indexed store!");
10179 return getStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10180 ST->getMemoryVT(), ST->getMemOperand(), AM,
10181 ST->isTruncatingStore());
10182}
10183
10185 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &dl,
10186 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Mask, SDValue EVL,
10187 MachinePointerInfo PtrInfo, EVT MemVT, Align Alignment,
10188 MachineMemOperand::Flags MMOFlags, const AAMDNodes &AAInfo,
10189 const MDNode *Ranges, bool IsExpanding) {
10190 MMOFlags |= MachineMemOperand::MOLoad;
10191 assert((MMOFlags & MachineMemOperand::MOStore) == 0);
10192 // If we don't have a PtrInfo, infer the trivial frame index case to simplify
10193 // clients.
10194 if (PtrInfo.V.isNull())
10195 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr, Offset);
10196
10197 TypeSize Size = MemVT.getStoreSize();
10199 MachineMemOperand *MMO = MF.getMachineMemOperand(PtrInfo, MMOFlags, Size,
10200 Alignment, AAInfo, Ranges);
10201 return getLoadVP(AM, ExtType, VT, dl, Chain, Ptr, Offset, Mask, EVL, MemVT,
10202 MMO, IsExpanding);
10203}
10204
10206 ISD::LoadExtType ExtType, EVT VT,
10207 const SDLoc &dl, SDValue Chain, SDValue Ptr,
10208 SDValue Offset, SDValue Mask, SDValue EVL,
10209 EVT MemVT, MachineMemOperand *MMO,
10210 bool IsExpanding) {
10211 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10212 assert(Mask.getValueType().getVectorElementCount() ==
10213 VT.getVectorElementCount() &&
10214 "Vector width mismatch between mask and data");
10215
10216 bool Indexed = AM != ISD::UNINDEXED;
10217 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10218
10219 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10220 : getVTList(VT, MVT::Other);
10221 SDValue Ops[] = {Chain, Ptr, Offset, Mask, EVL};
10223 AddNodeIDNode(ID, ISD::VP_LOAD, VTs, Ops);
10224 ID.AddInteger(MemVT.getRawBits());
10225 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadSDNode>(
10226 dl.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10227 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10228 ID.AddInteger(MMO->getFlags());
10229 void *IP = nullptr;
10230 if (auto *E = cast_or_null<VPLoadSDNode>(FindNodeOrInsertPos(ID, dl, IP))) {
10231 E->refineAlignment(MMO);
10232 E->refineRanges(MMO);
10233 return SDValue(E, 0);
10234 }
10235 auto *N = newSDNode<VPLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10236 ExtType, IsExpanding, MemVT, MMO);
10237 createOperands(N, Ops);
10238
10239 CSEMap.InsertNode(N, IP);
10240 InsertNode(N);
10241 SDValue V(N, 0);
10242 NewSDValueDbgMsg(V, "Creating new node: ", this);
10243 return V;
10244}
10245
10247 SDValue Ptr, SDValue Mask, SDValue EVL,
10248 MachinePointerInfo PtrInfo,
10249 MaybeAlign Alignment,
10250 MachineMemOperand::Flags MMOFlags,
10251 const AAMDNodes &AAInfo, const MDNode *Ranges,
10252 bool IsExpanding) {
10254 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10255 Mask, EVL, PtrInfo, VT, Alignment, MMOFlags, AAInfo, Ranges,
10256 IsExpanding);
10257}
10258
10260 SDValue Ptr, SDValue Mask, SDValue EVL,
10261 MachineMemOperand *MMO, bool IsExpanding) {
10263 return getLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, dl, Chain, Ptr, Undef,
10264 Mask, EVL, VT, MMO, IsExpanding);
10265}
10266
10268 EVT VT, SDValue Chain, SDValue Ptr,
10269 SDValue Mask, SDValue EVL,
10270 MachinePointerInfo PtrInfo, EVT MemVT,
10271 MaybeAlign Alignment,
10272 MachineMemOperand::Flags MMOFlags,
10273 const AAMDNodes &AAInfo, bool IsExpanding) {
10275 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10276 EVL, PtrInfo, MemVT, Alignment, MMOFlags, AAInfo, nullptr,
10277 IsExpanding);
10278}
10279
10281 EVT VT, SDValue Chain, SDValue Ptr,
10282 SDValue Mask, SDValue EVL, EVT MemVT,
10283 MachineMemOperand *MMO, bool IsExpanding) {
10285 return getLoadVP(ISD::UNINDEXED, ExtType, VT, dl, Chain, Ptr, Undef, Mask,
10286 EVL, MemVT, MMO, IsExpanding);
10287}
10288
10292 auto *LD = cast<VPLoadSDNode>(OrigLoad);
10293 assert(LD->getOffset().isUndef() && "Load is already a indexed load!");
10294 // Don't propagate the invariant or dereferenceable flags.
10295 auto MMOFlags =
10296 LD->getMemOperand()->getFlags() &
10298 return getLoadVP(AM, LD->getExtensionType(), OrigLoad.getValueType(), dl,
10299 LD->getChain(), Base, Offset, LD->getMask(),
10300 LD->getVectorLength(), LD->getPointerInfo(),
10301 LD->getMemoryVT(), LD->getAlign(), MMOFlags, LD->getAAInfo(),
10302 nullptr, LD->isExpandingLoad());
10303}
10304
10306 SDValue Ptr, SDValue Offset, SDValue Mask,
10307 SDValue EVL, EVT MemVT, MachineMemOperand *MMO,
10308 ISD::MemIndexedMode AM, bool IsTruncating,
10309 bool IsCompressing) {
10310 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10311 assert(Mask.getValueType().getVectorElementCount() ==
10313 "Vector width mismatch between mask and data");
10314
10315 bool Indexed = AM != ISD::UNINDEXED;
10316 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10317 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10318 : getVTList(MVT::Other);
10319 SDValue Ops[] = {Chain, Val, Ptr, Offset, Mask, EVL};
10321 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10322 ID.AddInteger(MemVT.getRawBits());
10323 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10324 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10325 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10326 ID.AddInteger(MMO->getFlags());
10327 void *IP = nullptr;
10328 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10329 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10330 return SDValue(E, 0);
10331 }
10332 auto *N = newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10333 IsTruncating, IsCompressing, MemVT, MMO);
10334 createOperands(N, Ops);
10335
10336 CSEMap.InsertNode(N, IP);
10337 InsertNode(N);
10338 SDValue V(N, 0);
10339 NewSDValueDbgMsg(V, "Creating new node: ", this);
10340 return V;
10341}
10342
10344 SDValue Val, SDValue Ptr, SDValue Mask,
10345 SDValue EVL, MachinePointerInfo PtrInfo,
10346 EVT SVT, Align Alignment,
10347 MachineMemOperand::Flags MMOFlags,
10348 const AAMDNodes &AAInfo,
10349 bool IsCompressing) {
10350 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10351
10352 MMOFlags |= MachineMemOperand::MOStore;
10353 assert((MMOFlags & MachineMemOperand::MOLoad) == 0);
10354
10355 if (PtrInfo.V.isNull())
10356 PtrInfo = InferPointerInfo(PtrInfo, *this, Ptr);
10357
10359 MachineMemOperand *MMO = MF.getMachineMemOperand(
10360 PtrInfo, MMOFlags, SVT.getStoreSize(), Alignment, AAInfo);
10361 return getTruncStoreVP(Chain, dl, Val, Ptr, Mask, EVL, SVT, MMO,
10362 IsCompressing);
10363}
10364
10366 SDValue Val, SDValue Ptr, SDValue Mask,
10367 SDValue EVL, EVT SVT,
10368 MachineMemOperand *MMO,
10369 bool IsCompressing) {
10370 EVT VT = Val.getValueType();
10371
10372 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10373 if (VT == SVT)
10374 return getStoreVP(Chain, dl, Val, Ptr, getUNDEF(Ptr.getValueType()), Mask,
10375 EVL, VT, MMO, ISD::UNINDEXED,
10376 /*IsTruncating*/ false, IsCompressing);
10377
10379 "Should only be a truncating store, not extending!");
10380 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10381 assert(VT.isVector() == SVT.isVector() &&
10382 "Cannot use trunc store to convert to or from a vector!");
10383 assert((!VT.isVector() ||
10385 "Cannot use trunc store to change the number of vector elements!");
10386
10387 SDVTList VTs = getVTList(MVT::Other);
10389 SDValue Ops[] = {Chain, Val, Ptr, Undef, Mask, EVL};
10391 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10392 ID.AddInteger(SVT.getRawBits());
10393 ID.AddInteger(getSyntheticNodeSubclassData<VPStoreSDNode>(
10394 dl.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10395 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10396 ID.AddInteger(MMO->getFlags());
10397 void *IP = nullptr;
10398 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10399 cast<VPStoreSDNode>(E)->refineAlignment(MMO);
10400 return SDValue(E, 0);
10401 }
10402 auto *N =
10403 newSDNode<VPStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10404 ISD::UNINDEXED, true, IsCompressing, SVT, MMO);
10405 createOperands(N, Ops);
10406
10407 CSEMap.InsertNode(N, IP);
10408 InsertNode(N);
10409 SDValue V(N, 0);
10410 NewSDValueDbgMsg(V, "Creating new node: ", this);
10411 return V;
10412}
10413
10417 auto *ST = cast<VPStoreSDNode>(OrigStore);
10418 assert(ST->getOffset().isUndef() && "Store is already an indexed store!");
10419 SDVTList VTs = getVTList(Base.getValueType(), MVT::Other);
10420 SDValue Ops[] = {ST->getChain(), ST->getValue(), Base,
10421 Offset, ST->getMask(), ST->getVectorLength()};
10423 AddNodeIDNode(ID, ISD::VP_STORE, VTs, Ops);
10424 ID.AddInteger(ST->getMemoryVT().getRawBits());
10425 ID.AddInteger(ST->getRawSubclassData());
10426 ID.AddInteger(ST->getPointerInfo().getAddrSpace());
10427 ID.AddInteger(ST->getMemOperand()->getFlags());
10428 void *IP = nullptr;
10429 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10430 return SDValue(E, 0);
10431
10432 auto *N = newSDNode<VPStoreSDNode>(
10433 dl.getIROrder(), dl.getDebugLoc(), VTs, AM, ST->isTruncatingStore(),
10434 ST->isCompressingStore(), ST->getMemoryVT(), ST->getMemOperand());
10435 createOperands(N, Ops);
10436
10437 CSEMap.InsertNode(N, IP);
10438 InsertNode(N);
10439 SDValue V(N, 0);
10440 NewSDValueDbgMsg(V, "Creating new node: ", this);
10441 return V;
10442}
10443
10445 ISD::MemIndexedMode AM, ISD::LoadExtType ExtType, EVT VT, const SDLoc &DL,
10446 SDValue Chain, SDValue Ptr, SDValue Offset, SDValue Stride, SDValue Mask,
10447 SDValue EVL, EVT MemVT, MachineMemOperand *MMO, bool IsExpanding) {
10448 bool Indexed = AM != ISD::UNINDEXED;
10449 assert((Indexed || Offset.isUndef()) && "Unindexed load with an offset!");
10450
10451 SDValue Ops[] = {Chain, Ptr, Offset, Stride, Mask, EVL};
10452 SDVTList VTs = Indexed ? getVTList(VT, Ptr.getValueType(), MVT::Other)
10453 : getVTList(VT, MVT::Other);
10455 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_LOAD, VTs, Ops);
10456 ID.AddInteger(VT.getRawBits());
10457 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedLoadSDNode>(
10458 DL.getIROrder(), VTs, AM, ExtType, IsExpanding, MemVT, MMO));
10459 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10460
10461 void *IP = nullptr;
10462 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10463 cast<VPStridedLoadSDNode>(E)->refineAlignment(MMO);
10464 return SDValue(E, 0);
10465 }
10466
10467 auto *N =
10468 newSDNode<VPStridedLoadSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs, AM,
10469 ExtType, IsExpanding, MemVT, MMO);
10470 createOperands(N, Ops);
10471 CSEMap.InsertNode(N, IP);
10472 InsertNode(N);
10473 SDValue V(N, 0);
10474 NewSDValueDbgMsg(V, "Creating new node: ", this);
10475 return V;
10476}
10477
10479 SDValue Ptr, SDValue Stride,
10480 SDValue Mask, SDValue EVL,
10481 MachineMemOperand *MMO,
10482 bool IsExpanding) {
10484 return getStridedLoadVP(ISD::UNINDEXED, ISD::NON_EXTLOAD, VT, DL, Chain, Ptr,
10485 Undef, Stride, Mask, EVL, VT, MMO, IsExpanding);
10486}
10487
10489 ISD::LoadExtType ExtType, const SDLoc &DL, EVT VT, SDValue Chain,
10490 SDValue Ptr, SDValue Stride, SDValue Mask, SDValue EVL, EVT MemVT,
10491 MachineMemOperand *MMO, bool IsExpanding) {
10493 return getStridedLoadVP(ISD::UNINDEXED, ExtType, VT, DL, Chain, Ptr, Undef,
10494 Stride, Mask, EVL, MemVT, MMO, IsExpanding);
10495}
10496
10498 SDValue Val, SDValue Ptr,
10499 SDValue Offset, SDValue Stride,
10500 SDValue Mask, SDValue EVL, EVT MemVT,
10501 MachineMemOperand *MMO,
10503 bool IsTruncating, bool IsCompressing) {
10504 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10505 bool Indexed = AM != ISD::UNINDEXED;
10506 assert((Indexed || Offset.isUndef()) && "Unindexed vp_store with an offset!");
10507 SDVTList VTs = Indexed ? getVTList(Ptr.getValueType(), MVT::Other)
10508 : getVTList(MVT::Other);
10509 SDValue Ops[] = {Chain, Val, Ptr, Offset, Stride, Mask, EVL};
10511 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10512 ID.AddInteger(MemVT.getRawBits());
10513 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10514 DL.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10515 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10516 void *IP = nullptr;
10517 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10518 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10519 return SDValue(E, 0);
10520 }
10521 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10522 VTs, AM, IsTruncating,
10523 IsCompressing, MemVT, MMO);
10524 createOperands(N, Ops);
10525
10526 CSEMap.InsertNode(N, IP);
10527 InsertNode(N);
10528 SDValue V(N, 0);
10529 NewSDValueDbgMsg(V, "Creating new node: ", this);
10530 return V;
10531}
10532
10534 SDValue Val, SDValue Ptr,
10535 SDValue Stride, SDValue Mask,
10536 SDValue EVL, EVT SVT,
10537 MachineMemOperand *MMO,
10538 bool IsCompressing) {
10539 EVT VT = Val.getValueType();
10540
10541 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10542 if (VT == SVT)
10543 return getStridedStoreVP(Chain, DL, Val, Ptr, getUNDEF(Ptr.getValueType()),
10544 Stride, Mask, EVL, VT, MMO, ISD::UNINDEXED,
10545 /*IsTruncating*/ false, IsCompressing);
10546
10548 "Should only be a truncating store, not extending!");
10549 assert(VT.isInteger() == SVT.isInteger() && "Can't do FP-INT conversion!");
10550 assert(VT.isVector() == SVT.isVector() &&
10551 "Cannot use trunc store to convert to or from a vector!");
10552 assert((!VT.isVector() ||
10554 "Cannot use trunc store to change the number of vector elements!");
10555
10556 SDVTList VTs = getVTList(MVT::Other);
10558 SDValue Ops[] = {Chain, Val, Ptr, Undef, Stride, Mask, EVL};
10560 AddNodeIDNode(ID, ISD::EXPERIMENTAL_VP_STRIDED_STORE, VTs, Ops);
10561 ID.AddInteger(SVT.getRawBits());
10562 ID.AddInteger(getSyntheticNodeSubclassData<VPStridedStoreSDNode>(
10563 DL.getIROrder(), VTs, ISD::UNINDEXED, true, IsCompressing, SVT, MMO));
10564 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10565 void *IP = nullptr;
10566 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10567 cast<VPStridedStoreSDNode>(E)->refineAlignment(MMO);
10568 return SDValue(E, 0);
10569 }
10570 auto *N = newSDNode<VPStridedStoreSDNode>(DL.getIROrder(), DL.getDebugLoc(),
10571 VTs, ISD::UNINDEXED, true,
10572 IsCompressing, SVT, MMO);
10573 createOperands(N, Ops);
10574
10575 CSEMap.InsertNode(N, IP);
10576 InsertNode(N);
10577 SDValue V(N, 0);
10578 NewSDValueDbgMsg(V, "Creating new node: ", this);
10579 return V;
10580}
10581
10584 ISD::MemIndexType IndexType) {
10585 assert(Ops.size() == 6 && "Incompatible number of operands");
10586
10588 AddNodeIDNode(ID, ISD::VP_GATHER, VTs, Ops);
10589 ID.AddInteger(VT.getRawBits());
10590 ID.AddInteger(getSyntheticNodeSubclassData<VPGatherSDNode>(
10591 dl.getIROrder(), VTs, VT, MMO, IndexType));
10592 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10593 ID.AddInteger(MMO->getFlags());
10594 void *IP = nullptr;
10595 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10596 cast<VPGatherSDNode>(E)->refineAlignment(MMO);
10597 return SDValue(E, 0);
10598 }
10599
10600 auto *N = newSDNode<VPGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10601 VT, MMO, IndexType);
10602 createOperands(N, Ops);
10603
10604 assert(N->getMask().getValueType().getVectorElementCount() ==
10605 N->getValueType(0).getVectorElementCount() &&
10606 "Vector width mismatch between mask and data");
10607 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10608 N->getValueType(0).getVectorElementCount().isScalable() &&
10609 "Scalable flags of index and data do not match");
10611 N->getIndex().getValueType().getVectorElementCount(),
10612 N->getValueType(0).getVectorElementCount()) &&
10613 "Vector width mismatch between index and data");
10614 assert(isa<ConstantSDNode>(N->getScale()) &&
10615 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10616 "Scale should be a constant power of 2");
10617
10618 CSEMap.InsertNode(N, IP);
10619 InsertNode(N);
10620 SDValue V(N, 0);
10621 NewSDValueDbgMsg(V, "Creating new node: ", this);
10622 return V;
10623}
10624
10627 MachineMemOperand *MMO,
10628 ISD::MemIndexType IndexType) {
10629 assert(Ops.size() == 7 && "Incompatible number of operands");
10630
10632 AddNodeIDNode(ID, ISD::VP_SCATTER, VTs, Ops);
10633 ID.AddInteger(VT.getRawBits());
10634 ID.AddInteger(getSyntheticNodeSubclassData<VPScatterSDNode>(
10635 dl.getIROrder(), VTs, VT, MMO, IndexType));
10636 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10637 ID.AddInteger(MMO->getFlags());
10638 void *IP = nullptr;
10639 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10640 cast<VPScatterSDNode>(E)->refineAlignment(MMO);
10641 return SDValue(E, 0);
10642 }
10643 auto *N = newSDNode<VPScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10644 VT, MMO, IndexType);
10645 createOperands(N, Ops);
10646
10647 assert(N->getMask().getValueType().getVectorElementCount() ==
10648 N->getValue().getValueType().getVectorElementCount() &&
10649 "Vector width mismatch between mask and data");
10650 assert(
10651 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10652 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10653 "Scalable flags of index and data do not match");
10655 N->getIndex().getValueType().getVectorElementCount(),
10656 N->getValue().getValueType().getVectorElementCount()) &&
10657 "Vector width mismatch between index and data");
10658 assert(isa<ConstantSDNode>(N->getScale()) &&
10659 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10660 "Scale should be a constant power of 2");
10661
10662 CSEMap.InsertNode(N, IP);
10663 InsertNode(N);
10664 SDValue V(N, 0);
10665 NewSDValueDbgMsg(V, "Creating new node: ", this);
10666 return V;
10667}
10668
10671 SDValue PassThru, EVT MemVT,
10672 MachineMemOperand *MMO,
10674 ISD::LoadExtType ExtTy, bool isExpanding) {
10675 bool Indexed = AM != ISD::UNINDEXED;
10676 assert((Indexed || Offset.isUndef()) &&
10677 "Unindexed masked load with an offset!");
10678 SDVTList VTs = Indexed ? getVTList(VT, Base.getValueType(), MVT::Other)
10679 : getVTList(VT, MVT::Other);
10680 SDValue Ops[] = {Chain, Base, Offset, Mask, PassThru};
10683 ID.AddInteger(MemVT.getRawBits());
10684 ID.AddInteger(getSyntheticNodeSubclassData<MaskedLoadSDNode>(
10685 dl.getIROrder(), VTs, AM, ExtTy, isExpanding, MemVT, MMO));
10686 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10687 ID.AddInteger(MMO->getFlags());
10688 void *IP = nullptr;
10689 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10690 cast<MaskedLoadSDNode>(E)->refineAlignment(MMO);
10691 return SDValue(E, 0);
10692 }
10693 auto *N = newSDNode<MaskedLoadSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs,
10694 AM, ExtTy, isExpanding, MemVT, MMO);
10695 createOperands(N, Ops);
10696
10697 CSEMap.InsertNode(N, IP);
10698 InsertNode(N);
10699 SDValue V(N, 0);
10700 NewSDValueDbgMsg(V, "Creating new node: ", this);
10701 return V;
10702}
10703
10708 assert(LD->getOffset().isUndef() && "Masked load is already a indexed load!");
10709 return getMaskedLoad(OrigLoad.getValueType(), dl, LD->getChain(), Base,
10710 Offset, LD->getMask(), LD->getPassThru(),
10711 LD->getMemoryVT(), LD->getMemOperand(), AM,
10712 LD->getExtensionType(), LD->isExpandingLoad());
10713}
10714
10717 SDValue Mask, EVT MemVT,
10718 MachineMemOperand *MMO,
10719 ISD::MemIndexedMode AM, bool IsTruncating,
10720 bool IsCompressing) {
10721 assert(Chain.getValueType() == MVT::Other &&
10722 "Invalid chain type");
10723 bool Indexed = AM != ISD::UNINDEXED;
10724 assert((Indexed || Offset.isUndef()) &&
10725 "Unindexed masked store with an offset!");
10726 SDVTList VTs = Indexed ? getVTList(Base.getValueType(), MVT::Other)
10727 : getVTList(MVT::Other);
10728 SDValue Ops[] = {Chain, Val, Base, Offset, Mask};
10731 ID.AddInteger(MemVT.getRawBits());
10732 ID.AddInteger(getSyntheticNodeSubclassData<MaskedStoreSDNode>(
10733 dl.getIROrder(), VTs, AM, IsTruncating, IsCompressing, MemVT, MMO));
10734 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10735 ID.AddInteger(MMO->getFlags());
10736 void *IP = nullptr;
10737 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10738 cast<MaskedStoreSDNode>(E)->refineAlignment(MMO);
10739 return SDValue(E, 0);
10740 }
10741 auto *N =
10742 newSDNode<MaskedStoreSDNode>(dl.getIROrder(), dl.getDebugLoc(), VTs, AM,
10743 IsTruncating, IsCompressing, MemVT, MMO);
10744 createOperands(N, Ops);
10745
10746 CSEMap.InsertNode(N, IP);
10747 InsertNode(N);
10748 SDValue V(N, 0);
10749 NewSDValueDbgMsg(V, "Creating new node: ", this);
10750 return V;
10751}
10752
10757 assert(ST->getOffset().isUndef() &&
10758 "Masked store is already a indexed store!");
10759 return getMaskedStore(ST->getChain(), dl, ST->getValue(), Base, Offset,
10760 ST->getMask(), ST->getMemoryVT(), ST->getMemOperand(),
10761 AM, ST->isTruncatingStore(), ST->isCompressingStore());
10762}
10763
10766 MachineMemOperand *MMO,
10767 ISD::MemIndexType IndexType,
10768 ISD::LoadExtType ExtTy) {
10769 assert(Ops.size() == 6 && "Incompatible number of operands");
10770
10773 ID.AddInteger(MemVT.getRawBits());
10774 ID.AddInteger(getSyntheticNodeSubclassData<MaskedGatherSDNode>(
10775 dl.getIROrder(), VTs, MemVT, MMO, IndexType, ExtTy));
10776 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10777 ID.AddInteger(MMO->getFlags());
10778 void *IP = nullptr;
10779 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10780 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10781 return SDValue(E, 0);
10782 }
10783
10784 auto *N = newSDNode<MaskedGatherSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10785 VTs, MemVT, MMO, IndexType, ExtTy);
10786 createOperands(N, Ops);
10787
10788 assert(N->getPassThru().getValueType() == N->getValueType(0) &&
10789 "Incompatible type of the PassThru value in MaskedGatherSDNode");
10790 assert(N->getMask().getValueType().getVectorElementCount() ==
10791 N->getValueType(0).getVectorElementCount() &&
10792 "Vector width mismatch between mask and data");
10793 assert(N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10794 N->getValueType(0).getVectorElementCount().isScalable() &&
10795 "Scalable flags of index and data do not match");
10797 N->getIndex().getValueType().getVectorElementCount(),
10798 N->getValueType(0).getVectorElementCount()) &&
10799 "Vector width mismatch between index and data");
10800 assert(isa<ConstantSDNode>(N->getScale()) &&
10801 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10802 "Scale should be a constant power of 2");
10803
10804 CSEMap.InsertNode(N, IP);
10805 InsertNode(N);
10806 SDValue V(N, 0);
10807 NewSDValueDbgMsg(V, "Creating new node: ", this);
10808 return V;
10809}
10810
10813 MachineMemOperand *MMO,
10814 ISD::MemIndexType IndexType,
10815 bool IsTrunc) {
10816 assert(Ops.size() == 6 && "Incompatible number of operands");
10817
10820 ID.AddInteger(MemVT.getRawBits());
10821 ID.AddInteger(getSyntheticNodeSubclassData<MaskedScatterSDNode>(
10822 dl.getIROrder(), VTs, MemVT, MMO, IndexType, IsTrunc));
10823 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10824 ID.AddInteger(MMO->getFlags());
10825 void *IP = nullptr;
10826 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10827 cast<MaskedScatterSDNode>(E)->refineAlignment(MMO);
10828 return SDValue(E, 0);
10829 }
10830
10831 auto *N = newSDNode<MaskedScatterSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10832 VTs, MemVT, MMO, IndexType, IsTrunc);
10833 createOperands(N, Ops);
10834
10835 assert(N->getMask().getValueType().getVectorElementCount() ==
10836 N->getValue().getValueType().getVectorElementCount() &&
10837 "Vector width mismatch between mask and data");
10838 assert(
10839 N->getIndex().getValueType().getVectorElementCount().isScalable() ==
10840 N->getValue().getValueType().getVectorElementCount().isScalable() &&
10841 "Scalable flags of index and data do not match");
10843 N->getIndex().getValueType().getVectorElementCount(),
10844 N->getValue().getValueType().getVectorElementCount()) &&
10845 "Vector width mismatch between index and data");
10846 assert(isa<ConstantSDNode>(N->getScale()) &&
10847 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10848 "Scale should be a constant power of 2");
10849
10850 CSEMap.InsertNode(N, IP);
10851 InsertNode(N);
10852 SDValue V(N, 0);
10853 NewSDValueDbgMsg(V, "Creating new node: ", this);
10854 return V;
10855}
10856
10858 const SDLoc &dl, ArrayRef<SDValue> Ops,
10859 MachineMemOperand *MMO,
10860 ISD::MemIndexType IndexType) {
10861 assert(Ops.size() == 7 && "Incompatible number of operands");
10862
10865 ID.AddInteger(MemVT.getRawBits());
10866 ID.AddInteger(getSyntheticNodeSubclassData<MaskedHistogramSDNode>(
10867 dl.getIROrder(), VTs, MemVT, MMO, IndexType));
10868 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10869 ID.AddInteger(MMO->getFlags());
10870 void *IP = nullptr;
10871 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP)) {
10872 cast<MaskedGatherSDNode>(E)->refineAlignment(MMO);
10873 return SDValue(E, 0);
10874 }
10875
10876 auto *N = newSDNode<MaskedHistogramSDNode>(dl.getIROrder(), dl.getDebugLoc(),
10877 VTs, MemVT, MMO, IndexType);
10878 createOperands(N, Ops);
10879
10880 assert(N->getMask().getValueType().getVectorElementCount() ==
10881 N->getIndex().getValueType().getVectorElementCount() &&
10882 "Vector width mismatch between mask and data");
10883 assert(isa<ConstantSDNode>(N->getScale()) &&
10884 N->getScale()->getAsAPIntVal().isPowerOf2() &&
10885 "Scale should be a constant power of 2");
10886 assert(N->getInc().getValueType().isInteger() && "Non integer update value");
10887
10888 CSEMap.InsertNode(N, IP);
10889 InsertNode(N);
10890 SDValue V(N, 0);
10891 NewSDValueDbgMsg(V, "Creating new node: ", this);
10892 return V;
10893}
10894
10896 SDValue Ptr, SDValue Mask, SDValue EVL,
10897 MachineMemOperand *MMO) {
10898 SDVTList VTs = getVTList(VT, EVL.getValueType(), MVT::Other);
10899 SDValue Ops[] = {Chain, Ptr, Mask, EVL};
10901 AddNodeIDNode(ID, ISD::VP_LOAD_FF, VTs, Ops);
10902 ID.AddInteger(VT.getRawBits());
10903 ID.AddInteger(getSyntheticNodeSubclassData<VPLoadFFSDNode>(DL.getIROrder(),
10904 VTs, VT, MMO));
10905 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10906 ID.AddInteger(MMO->getFlags());
10907 void *IP = nullptr;
10908 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
10909 cast<VPLoadFFSDNode>(E)->refineAlignment(MMO);
10910 return SDValue(E, 0);
10911 }
10912 auto *N = newSDNode<VPLoadFFSDNode>(DL.getIROrder(), DL.getDebugLoc(), VTs,
10913 VT, MMO);
10914 createOperands(N, Ops);
10915
10916 CSEMap.InsertNode(N, IP);
10917 InsertNode(N);
10918 SDValue V(N, 0);
10919 NewSDValueDbgMsg(V, "Creating new node: ", this);
10920 return V;
10921}
10922
10924 EVT MemVT, MachineMemOperand *MMO) {
10925 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10926 SDVTList VTs = getVTList(MVT::Other);
10927 SDValue Ops[] = {Chain, Ptr};
10930 ID.AddInteger(MemVT.getRawBits());
10931 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10932 ISD::GET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10933 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10934 ID.AddInteger(MMO->getFlags());
10935 void *IP = nullptr;
10936 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10937 return SDValue(E, 0);
10938
10939 auto *N = newSDNode<FPStateAccessSDNode>(ISD::GET_FPENV_MEM, dl.getIROrder(),
10940 dl.getDebugLoc(), VTs, MemVT, MMO);
10941 createOperands(N, Ops);
10942
10943 CSEMap.InsertNode(N, IP);
10944 InsertNode(N);
10945 SDValue V(N, 0);
10946 NewSDValueDbgMsg(V, "Creating new node: ", this);
10947 return V;
10948}
10949
10951 EVT MemVT, MachineMemOperand *MMO) {
10952 assert(Chain.getValueType() == MVT::Other && "Invalid chain type");
10953 SDVTList VTs = getVTList(MVT::Other);
10954 SDValue Ops[] = {Chain, Ptr};
10957 ID.AddInteger(MemVT.getRawBits());
10958 ID.AddInteger(getSyntheticNodeSubclassData<FPStateAccessSDNode>(
10959 ISD::SET_FPENV_MEM, dl.getIROrder(), VTs, MemVT, MMO));
10960 ID.AddInteger(MMO->getPointerInfo().getAddrSpace());
10961 ID.AddInteger(MMO->getFlags());
10962 void *IP = nullptr;
10963 if (SDNode *E = FindNodeOrInsertPos(ID, dl, IP))
10964 return SDValue(E, 0);
10965
10966 auto *N = newSDNode<FPStateAccessSDNode>(ISD::SET_FPENV_MEM, dl.getIROrder(),
10967 dl.getDebugLoc(), VTs, MemVT, MMO);
10968 createOperands(N, Ops);
10969
10970 CSEMap.InsertNode(N, IP);
10971 InsertNode(N);
10972 SDValue V(N, 0);
10973 NewSDValueDbgMsg(V, "Creating new node: ", this);
10974 return V;
10975}
10976
10978 // select undef, T, F --> T (if T is a constant), otherwise F
10979 // select, ?, undef, F --> F
10980 // select, ?, T, undef --> T
10981 if (Cond.isUndef())
10982 return isConstantValueOfAnyType(T) ? T : F;
10983 if (T.isUndef())
10985 if (F.isUndef())
10987
10988 // select true, T, F --> T
10989 // select false, T, F --> F
10990 if (auto C = isBoolConstant(Cond))
10991 return *C ? T : F;
10992
10993 // select ?, T, T --> T
10994 if (T == F)
10995 return T;
10996
10997 return SDValue();
10998}
10999
11001 // shift undef, Y --> 0 (can always assume that the undef value is 0)
11002 if (X.isUndef())
11003 return getConstant(0, SDLoc(X.getNode()), X.getValueType());
11004 // shift X, undef --> undef (because it may shift by the bitwidth)
11005 if (Y.isUndef())
11006 return getUNDEF(X.getValueType());
11007
11008 // shift 0, Y --> 0
11009 // shift X, 0 --> X
11011 return X;
11012
11013 // shift X, C >= bitwidth(X) --> undef
11014 // All vector elements must be too big (or undef) to avoid partial undefs.
11015 auto isShiftTooBig = [X](ConstantSDNode *Val) {
11016 return !Val || Val->getAPIntValue().uge(X.getScalarValueSizeInBits());
11017 };
11018 if (ISD::matchUnaryPredicate(Y, isShiftTooBig, true))
11019 return getUNDEF(X.getValueType());
11020
11021 // shift i1/vXi1 X, Y --> X (any non-zero shift amount is undefined).
11022 if (X.getValueType().getScalarType() == MVT::i1)
11023 return X;
11024
11025 return SDValue();
11026}
11027
11029 SDNodeFlags Flags) {
11030 // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
11031 // (an undef operand can be chosen to be Nan/Inf), then the result of this
11032 // operation is poison. That result can be relaxed to undef.
11033 ConstantFPSDNode *XC = isConstOrConstSplatFP(X, /* AllowUndefs */ true);
11034 ConstantFPSDNode *YC = isConstOrConstSplatFP(Y, /* AllowUndefs */ true);
11035 bool HasNan = (XC && XC->getValueAPF().isNaN()) ||
11036 (YC && YC->getValueAPF().isNaN());
11037 bool HasInf = (XC && XC->getValueAPF().isInfinity()) ||
11038 (YC && YC->getValueAPF().isInfinity());
11039
11040 if (Flags.hasNoNaNs() && (HasNan || X.isUndef() || Y.isUndef()))
11041 return getUNDEF(X.getValueType());
11042
11043 if (Flags.hasNoInfs() && (HasInf || X.isUndef() || Y.isUndef()))
11044 return getUNDEF(X.getValueType());
11045
11046 if (!YC)
11047 return SDValue();
11048
11049 // X + -0.0 --> X
11050 if (Opcode == ISD::FADD)
11051 if (YC->getValueAPF().isNegZero())
11052 return X;
11053
11054 // X - +0.0 --> X
11055 if (Opcode == ISD::FSUB)
11056 if (YC->getValueAPF().isPosZero())
11057 return X;
11058
11059 // X * 1.0 --> X
11060 // X / 1.0 --> X
11061 if (Opcode == ISD::FMUL || Opcode == ISD::FDIV)
11062 if (YC->getValueAPF().isExactlyValue(1.0))
11063 return X;
11064
11065 // X * 0.0 --> 0.0
11066 if (Opcode == ISD::FMUL && Flags.hasNoNaNs() && Flags.hasNoSignedZeros())
11067 if (YC->getValueAPF().isZero())
11068 return getConstantFP(0.0, SDLoc(Y), Y.getValueType());
11069
11070 return SDValue();
11071}
11072
11074 SDValue Ptr, SDValue SV, unsigned Align) {
11075 SDValue Ops[] = { Chain, Ptr, SV, getTargetConstant(Align, dl, MVT::i32) };
11076 return getNode(ISD::VAARG, dl, getVTList(VT, MVT::Other), Ops);
11077}
11078
11079SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11081 switch (Ops.size()) {
11082 case 0: return getNode(Opcode, DL, VT);
11083 case 1: return getNode(Opcode, DL, VT, Ops[0].get());
11084 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1]);
11085 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2]);
11086 default: break;
11087 }
11088
11089 // Copy from an SDUse array into an SDValue array for use with
11090 // the regular getNode logic.
11092 return getNode(Opcode, DL, VT, NewOps);
11093}
11094
11095SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11097 SDNodeFlags Flags;
11098 if (Inserter)
11099 Flags = Inserter->getFlags();
11100 return getNode(Opcode, DL, VT, Ops, Flags);
11101}
11102
11103SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
11104 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11105 unsigned NumOps = Ops.size();
11106 switch (NumOps) {
11107 case 0: return getNode(Opcode, DL, VT);
11108 case 1: return getNode(Opcode, DL, VT, Ops[0], Flags);
11109 case 2: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Flags);
11110 case 3: return getNode(Opcode, DL, VT, Ops[0], Ops[1], Ops[2], Flags);
11111 default: break;
11112 }
11113
11114#ifndef NDEBUG
11115 for (const auto &Op : Ops)
11116 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11117 "Operand is DELETED_NODE!");
11118#endif
11119
11120 switch (Opcode) {
11121 default: break;
11122 case ISD::BUILD_VECTOR:
11123 // Attempt to simplify BUILD_VECTOR.
11124 if (SDValue V = FoldBUILD_VECTOR(DL, VT, Ops, *this))
11125 return V;
11126 break;
11128 if (SDValue V = foldCONCAT_VECTORS(DL, VT, Ops, *this))
11129 return V;
11130 break;
11131 case ISD::SELECT_CC:
11132 assert(NumOps == 5 && "SELECT_CC takes 5 operands!");
11133 assert(Ops[0].getValueType() == Ops[1].getValueType() &&
11134 "LHS and RHS of condition must have same type!");
11135 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11136 "True and False arms of SelectCC must have same type!");
11137 assert(Ops[2].getValueType() == VT &&
11138 "select_cc node must be of same type as true and false value!");
11139 assert((!Ops[0].getValueType().isVector() ||
11140 Ops[0].getValueType().getVectorElementCount() ==
11141 VT.getVectorElementCount()) &&
11142 "Expected select_cc with vector result to have the same sized "
11143 "comparison type!");
11144 break;
11145 case ISD::BR_CC:
11146 assert(NumOps == 5 && "BR_CC takes 5 operands!");
11147 assert(Ops[2].getValueType() == Ops[3].getValueType() &&
11148 "LHS/RHS of comparison should match types!");
11149 break;
11150 case ISD::VP_ADD:
11151 case ISD::VP_SUB:
11152 // If it is VP_ADD/VP_SUB mask operation then turn it to VP_XOR
11153 if (VT.getScalarType() == MVT::i1)
11154 Opcode = ISD::VP_XOR;
11155 break;
11156 case ISD::VP_MUL:
11157 // If it is VP_MUL mask operation then turn it to VP_AND
11158 if (VT.getScalarType() == MVT::i1)
11159 Opcode = ISD::VP_AND;
11160 break;
11161 case ISD::VP_REDUCE_MUL:
11162 // If it is VP_REDUCE_MUL mask operation then turn it to VP_REDUCE_AND
11163 if (VT == MVT::i1)
11164 Opcode = ISD::VP_REDUCE_AND;
11165 break;
11166 case ISD::VP_REDUCE_ADD:
11167 // If it is VP_REDUCE_ADD mask operation then turn it to VP_REDUCE_XOR
11168 if (VT == MVT::i1)
11169 Opcode = ISD::VP_REDUCE_XOR;
11170 break;
11171 case ISD::VP_REDUCE_SMAX:
11172 case ISD::VP_REDUCE_UMIN:
11173 // If it is VP_REDUCE_SMAX/VP_REDUCE_UMIN mask operation then turn it to
11174 // VP_REDUCE_AND.
11175 if (VT == MVT::i1)
11176 Opcode = ISD::VP_REDUCE_AND;
11177 break;
11178 case ISD::VP_REDUCE_SMIN:
11179 case ISD::VP_REDUCE_UMAX:
11180 // If it is VP_REDUCE_SMIN/VP_REDUCE_UMAX mask operation then turn it to
11181 // VP_REDUCE_OR.
11182 if (VT == MVT::i1)
11183 Opcode = ISD::VP_REDUCE_OR;
11184 break;
11185 }
11186
11187 // Memoize nodes.
11188 SDNode *N;
11189 SDVTList VTs = getVTList(VT);
11190
11191 if (VT != MVT::Glue) {
11193 AddNodeIDNode(ID, Opcode, VTs, Ops);
11194 void *IP = nullptr;
11195
11196 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11197 E->intersectFlagsWith(Flags);
11198 return SDValue(E, 0);
11199 }
11200
11201 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11202 createOperands(N, Ops);
11203
11204 CSEMap.InsertNode(N, IP);
11205 } else {
11206 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11207 createOperands(N, Ops);
11208 }
11209
11210 N->setFlags(Flags);
11211 InsertNode(N);
11212 SDValue V(N, 0);
11213 NewSDValueDbgMsg(V, "Creating new node: ", this);
11214 return V;
11215}
11216
11217SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11218 ArrayRef<EVT> ResultTys, ArrayRef<SDValue> Ops) {
11219 SDNodeFlags Flags;
11220 if (Inserter)
11221 Flags = Inserter->getFlags();
11222 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11223}
11224
11225SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11227 const SDNodeFlags Flags) {
11228 return getNode(Opcode, DL, getVTList(ResultTys), Ops, Flags);
11229}
11230
11231SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11233 SDNodeFlags Flags;
11234 if (Inserter)
11235 Flags = Inserter->getFlags();
11236 return getNode(Opcode, DL, VTList, Ops, Flags);
11237}
11238
11239SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11240 ArrayRef<SDValue> Ops, const SDNodeFlags Flags) {
11241 if (VTList.NumVTs == 1)
11242 return getNode(Opcode, DL, VTList.VTs[0], Ops, Flags);
11243
11244#ifndef NDEBUG
11245 for (const auto &Op : Ops)
11246 assert(Op.getOpcode() != ISD::DELETED_NODE &&
11247 "Operand is DELETED_NODE!");
11248#endif
11249
11250 switch (Opcode) {
11251 case ISD::SADDO:
11252 case ISD::UADDO:
11253 case ISD::SSUBO:
11254 case ISD::USUBO: {
11255 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11256 "Invalid add/sub overflow op!");
11257 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11258 Ops[0].getValueType() == Ops[1].getValueType() &&
11259 Ops[0].getValueType() == VTList.VTs[0] &&
11260 "Binary operator types must match!");
11261 SDValue N1 = Ops[0], N2 = Ops[1];
11262 canonicalizeCommutativeBinop(Opcode, N1, N2);
11263
11264 // (X +- 0) -> X with zero-overflow.
11265 ConstantSDNode *N2CV = isConstOrConstSplat(N2, /*AllowUndefs*/ false,
11266 /*AllowTruncation*/ true);
11267 if (N2CV && N2CV->isZero()) {
11268 SDValue ZeroOverFlow = getConstant(0, DL, VTList.VTs[1]);
11269 return getNode(ISD::MERGE_VALUES, DL, VTList, {N1, ZeroOverFlow}, Flags);
11270 }
11271
11272 if (VTList.VTs[0].getScalarType() == MVT::i1 &&
11273 VTList.VTs[1].getScalarType() == MVT::i1) {
11274 SDValue F1 = getFreeze(N1);
11275 SDValue F2 = getFreeze(N2);
11276 // {vXi1,vXi1} (u/s)addo(vXi1 x, vXi1y) -> {xor(x,y),and(x,y)}
11277 if (Opcode == ISD::UADDO || Opcode == ISD::SADDO)
11278 return getNode(ISD::MERGE_VALUES, DL, VTList,
11279 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11280 getNode(ISD::AND, DL, VTList.VTs[1], F1, F2)},
11281 Flags);
11282 // {vXi1,vXi1} (u/s)subo(vXi1 x, vXi1y) -> {xor(x,y),and(~x,y)}
11283 if (Opcode == ISD::USUBO || Opcode == ISD::SSUBO) {
11284 SDValue NotF1 = getNOT(DL, F1, VTList.VTs[0]);
11285 return getNode(ISD::MERGE_VALUES, DL, VTList,
11286 {getNode(ISD::XOR, DL, VTList.VTs[0], F1, F2),
11287 getNode(ISD::AND, DL, VTList.VTs[1], NotF1, F2)},
11288 Flags);
11289 }
11290 }
11291 break;
11292 }
11293 case ISD::SADDO_CARRY:
11294 case ISD::UADDO_CARRY:
11295 case ISD::SSUBO_CARRY:
11296 case ISD::USUBO_CARRY:
11297 assert(VTList.NumVTs == 2 && Ops.size() == 3 &&
11298 "Invalid add/sub overflow op!");
11299 assert(VTList.VTs[0].isInteger() && VTList.VTs[1].isInteger() &&
11300 Ops[0].getValueType() == Ops[1].getValueType() &&
11301 Ops[0].getValueType() == VTList.VTs[0] &&
11302 Ops[2].getValueType() == VTList.VTs[1] &&
11303 "Binary operator types must match!");
11304 break;
11305 case ISD::SMUL_LOHI:
11306 case ISD::UMUL_LOHI: {
11307 assert(VTList.NumVTs == 2 && Ops.size() == 2 && "Invalid mul lo/hi op!");
11308 assert(VTList.VTs[0].isInteger() && VTList.VTs[0] == VTList.VTs[1] &&
11309 VTList.VTs[0] == Ops[0].getValueType() &&
11310 VTList.VTs[0] == Ops[1].getValueType() &&
11311 "Binary operator types must match!");
11312 // Constant fold.
11315 if (LHS && RHS) {
11316 unsigned Width = VTList.VTs[0].getScalarSizeInBits();
11317 unsigned OutWidth = Width * 2;
11318 APInt Val = LHS->getAPIntValue();
11319 APInt Mul = RHS->getAPIntValue();
11320 if (Opcode == ISD::SMUL_LOHI) {
11321 Val = Val.sext(OutWidth);
11322 Mul = Mul.sext(OutWidth);
11323 } else {
11324 Val = Val.zext(OutWidth);
11325 Mul = Mul.zext(OutWidth);
11326 }
11327 Val *= Mul;
11328
11329 SDValue Hi =
11330 getConstant(Val.extractBits(Width, Width), DL, VTList.VTs[0]);
11331 SDValue Lo = getConstant(Val.trunc(Width), DL, VTList.VTs[0]);
11332 return getNode(ISD::MERGE_VALUES, DL, VTList, {Lo, Hi}, Flags);
11333 }
11334 break;
11335 }
11336 case ISD::FFREXP: {
11337 assert(VTList.NumVTs == 2 && Ops.size() == 1 && "Invalid ffrexp op!");
11338 assert(VTList.VTs[0].isFloatingPoint() && VTList.VTs[1].isInteger() &&
11339 VTList.VTs[0] == Ops[0].getValueType() && "frexp type mismatch");
11340
11342 int FrexpExp;
11343 APFloat FrexpMant =
11344 frexp(C->getValueAPF(), FrexpExp, APFloat::rmNearestTiesToEven);
11345 SDValue Result0 = getConstantFP(FrexpMant, DL, VTList.VTs[0]);
11346 SDValue Result1 = getSignedConstant(FrexpMant.isFinite() ? FrexpExp : 0,
11347 DL, VTList.VTs[1]);
11348 return getNode(ISD::MERGE_VALUES, DL, VTList, {Result0, Result1}, Flags);
11349 }
11350
11351 break;
11352 }
11354 assert(VTList.NumVTs == 2 && Ops.size() == 2 &&
11355 "Invalid STRICT_FP_EXTEND!");
11356 assert(VTList.VTs[0].isFloatingPoint() &&
11357 Ops[1].getValueType().isFloatingPoint() && "Invalid FP cast!");
11358 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11359 "STRICT_FP_EXTEND result type should be vector iff the operand "
11360 "type is vector!");
11361 assert((!VTList.VTs[0].isVector() ||
11362 VTList.VTs[0].getVectorElementCount() ==
11363 Ops[1].getValueType().getVectorElementCount()) &&
11364 "Vector element count mismatch!");
11365 assert(Ops[1].getValueType().bitsLT(VTList.VTs[0]) &&
11366 "Invalid fpext node, dst <= src!");
11367 break;
11369 assert(VTList.NumVTs == 2 && Ops.size() == 3 && "Invalid STRICT_FP_ROUND!");
11370 assert(VTList.VTs[0].isVector() == Ops[1].getValueType().isVector() &&
11371 "STRICT_FP_ROUND result type should be vector iff the operand "
11372 "type is vector!");
11373 assert((!VTList.VTs[0].isVector() ||
11374 VTList.VTs[0].getVectorElementCount() ==
11375 Ops[1].getValueType().getVectorElementCount()) &&
11376 "Vector element count mismatch!");
11377 assert(VTList.VTs[0].isFloatingPoint() &&
11378 Ops[1].getValueType().isFloatingPoint() &&
11379 VTList.VTs[0].bitsLT(Ops[1].getValueType()) &&
11380 Ops[2].getOpcode() == ISD::TargetConstant &&
11381 (Ops[2]->getAsZExtVal() == 0 || Ops[2]->getAsZExtVal() == 1) &&
11382 "Invalid STRICT_FP_ROUND!");
11383 break;
11384 }
11385
11386 // Memoize the node unless it returns a glue result.
11387 SDNode *N;
11388 if (VTList.VTs[VTList.NumVTs-1] != MVT::Glue) {
11390 AddNodeIDNode(ID, Opcode, VTList, Ops);
11391 void *IP = nullptr;
11392 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11393 E->intersectFlagsWith(Flags);
11394 return SDValue(E, 0);
11395 }
11396
11397 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11398 createOperands(N, Ops);
11399 CSEMap.InsertNode(N, IP);
11400 } else {
11401 N = newSDNode<SDNode>(Opcode, DL.getIROrder(), DL.getDebugLoc(), VTList);
11402 createOperands(N, Ops);
11403 }
11404
11405 N->setFlags(Flags);
11406 InsertNode(N);
11407 SDValue V(N, 0);
11408 NewSDValueDbgMsg(V, "Creating new node: ", this);
11409 return V;
11410}
11411
11412SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL,
11413 SDVTList VTList) {
11414 return getNode(Opcode, DL, VTList, ArrayRef<SDValue>());
11415}
11416
11417SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11418 SDValue N1) {
11419 SDValue Ops[] = { N1 };
11420 return getNode(Opcode, DL, VTList, Ops);
11421}
11422
11423SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11424 SDValue N1, SDValue N2) {
11425 SDValue Ops[] = { N1, N2 };
11426 return getNode(Opcode, DL, VTList, Ops);
11427}
11428
11429SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11430 SDValue N1, SDValue N2, SDValue N3) {
11431 SDValue Ops[] = { N1, N2, N3 };
11432 return getNode(Opcode, DL, VTList, Ops);
11433}
11434
11435SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11436 SDValue N1, SDValue N2, SDValue N3, SDValue N4) {
11437 SDValue Ops[] = { N1, N2, N3, N4 };
11438 return getNode(Opcode, DL, VTList, Ops);
11439}
11440
11441SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList,
11442 SDValue N1, SDValue N2, SDValue N3, SDValue N4,
11443 SDValue N5) {
11444 SDValue Ops[] = { N1, N2, N3, N4, N5 };
11445 return getNode(Opcode, DL, VTList, Ops);
11446}
11447
11449 if (!VT.isExtended())
11450 return makeVTList(SDNode::getValueTypeList(VT.getSimpleVT()), 1);
11451
11452 return makeVTList(&(*EVTs.insert(VT).first), 1);
11453}
11454
11457 ID.AddInteger(2U);
11458 ID.AddInteger(VT1.getRawBits());
11459 ID.AddInteger(VT2.getRawBits());
11460
11461 void *IP = nullptr;
11462 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11463 if (!Result) {
11464 EVT *Array = Allocator.Allocate<EVT>(2);
11465 Array[0] = VT1;
11466 Array[1] = VT2;
11467 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 2);
11468 VTListMap.InsertNode(Result, IP);
11469 }
11470 return Result->getSDVTList();
11471}
11472
11475 ID.AddInteger(3U);
11476 ID.AddInteger(VT1.getRawBits());
11477 ID.AddInteger(VT2.getRawBits());
11478 ID.AddInteger(VT3.getRawBits());
11479
11480 void *IP = nullptr;
11481 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11482 if (!Result) {
11483 EVT *Array = Allocator.Allocate<EVT>(3);
11484 Array[0] = VT1;
11485 Array[1] = VT2;
11486 Array[2] = VT3;
11487 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 3);
11488 VTListMap.InsertNode(Result, IP);
11489 }
11490 return Result->getSDVTList();
11491}
11492
11495 ID.AddInteger(4U);
11496 ID.AddInteger(VT1.getRawBits());
11497 ID.AddInteger(VT2.getRawBits());
11498 ID.AddInteger(VT3.getRawBits());
11499 ID.AddInteger(VT4.getRawBits());
11500
11501 void *IP = nullptr;
11502 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11503 if (!Result) {
11504 EVT *Array = Allocator.Allocate<EVT>(4);
11505 Array[0] = VT1;
11506 Array[1] = VT2;
11507 Array[2] = VT3;
11508 Array[3] = VT4;
11509 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, 4);
11510 VTListMap.InsertNode(Result, IP);
11511 }
11512 return Result->getSDVTList();
11513}
11514
11516 unsigned NumVTs = VTs.size();
11518 ID.AddInteger(NumVTs);
11519 for (unsigned index = 0; index < NumVTs; index++) {
11520 ID.AddInteger(VTs[index].getRawBits());
11521 }
11522
11523 void *IP = nullptr;
11524 SDVTListNode *Result = VTListMap.FindNodeOrInsertPos(ID, IP);
11525 if (!Result) {
11526 EVT *Array = Allocator.Allocate<EVT>(NumVTs);
11527 llvm::copy(VTs, Array);
11528 Result = new (Allocator) SDVTListNode(ID.Intern(Allocator), Array, NumVTs);
11529 VTListMap.InsertNode(Result, IP);
11530 }
11531 return Result->getSDVTList();
11532}
11533
11534
11535/// UpdateNodeOperands - *Mutate* the specified node in-place to have the
11536/// specified operands. If the resultant node already exists in the DAG,
11537/// this does not modify the specified node, instead it returns the node that
11538/// already exists. If the resultant node does not exist in the DAG, the
11539/// input node is returned. As a degenerate case, if you specify the same
11540/// input operands as the node already has, the input node is returned.
11542 assert(N->getNumOperands() == 1 && "Update with wrong number of operands");
11543
11544 // Check to see if there is no change.
11545 if (Op == N->getOperand(0)) return N;
11546
11547 // See if the modified node already exists.
11548 void *InsertPos = nullptr;
11549 if (SDNode *Existing = FindModifiedNodeSlot(N, Op, InsertPos))
11550 return Existing;
11551
11552 // Nope it doesn't. Remove the node from its current place in the maps.
11553 if (InsertPos)
11554 if (!RemoveNodeFromCSEMaps(N))
11555 InsertPos = nullptr;
11556
11557 // Now we update the operands.
11558 N->OperandList[0].set(Op);
11559
11561 // If this gets put into a CSE map, add it.
11562 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11563 return N;
11564}
11565
11567 assert(N->getNumOperands() == 2 && "Update with wrong number of operands");
11568
11569 // Check to see if there is no change.
11570 if (Op1 == N->getOperand(0) && Op2 == N->getOperand(1))
11571 return N; // No operands changed, just return the input node.
11572
11573 // See if the modified node already exists.
11574 void *InsertPos = nullptr;
11575 if (SDNode *Existing = FindModifiedNodeSlot(N, Op1, Op2, InsertPos))
11576 return Existing;
11577
11578 // Nope it doesn't. Remove the node from its current place in the maps.
11579 if (InsertPos)
11580 if (!RemoveNodeFromCSEMaps(N))
11581 InsertPos = nullptr;
11582
11583 // Now we update the operands.
11584 if (N->OperandList[0] != Op1)
11585 N->OperandList[0].set(Op1);
11586 if (N->OperandList[1] != Op2)
11587 N->OperandList[1].set(Op2);
11588
11590 // If this gets put into a CSE map, add it.
11591 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11592 return N;
11593}
11594
11597 SDValue Ops[] = { Op1, Op2, Op3 };
11598 return UpdateNodeOperands(N, Ops);
11599}
11600
11603 SDValue Op3, SDValue Op4) {
11604 SDValue Ops[] = { Op1, Op2, Op3, Op4 };
11605 return UpdateNodeOperands(N, Ops);
11606}
11607
11610 SDValue Op3, SDValue Op4, SDValue Op5) {
11611 SDValue Ops[] = { Op1, Op2, Op3, Op4, Op5 };
11612 return UpdateNodeOperands(N, Ops);
11613}
11614
11617 unsigned NumOps = Ops.size();
11618 assert(N->getNumOperands() == NumOps &&
11619 "Update with wrong number of operands");
11620
11621 // If no operands changed just return the input node.
11622 if (std::equal(Ops.begin(), Ops.end(), N->op_begin()))
11623 return N;
11624
11625 // See if the modified node already exists.
11626 void *InsertPos = nullptr;
11627 if (SDNode *Existing = FindModifiedNodeSlot(N, Ops, InsertPos))
11628 return Existing;
11629
11630 // Nope it doesn't. Remove the node from its current place in the maps.
11631 if (InsertPos)
11632 if (!RemoveNodeFromCSEMaps(N))
11633 InsertPos = nullptr;
11634
11635 // Now we update the operands.
11636 for (unsigned i = 0; i != NumOps; ++i)
11637 if (N->OperandList[i] != Ops[i])
11638 N->OperandList[i].set(Ops[i]);
11639
11641 // If this gets put into a CSE map, add it.
11642 if (InsertPos) CSEMap.InsertNode(N, InsertPos);
11643 return N;
11644}
11645
11646/// DropOperands - Release the operands and set this node to have
11647/// zero operands.
11649 // Unlike the code in MorphNodeTo that does this, we don't need to
11650 // watch for dead nodes here.
11651 for (op_iterator I = op_begin(), E = op_end(); I != E; ) {
11652 SDUse &Use = *I++;
11653 Use.set(SDValue());
11654 }
11655}
11656
11658 ArrayRef<MachineMemOperand *> NewMemRefs) {
11659 if (NewMemRefs.empty()) {
11660 N->clearMemRefs();
11661 return;
11662 }
11663
11664 // Check if we can avoid allocating by storing a single reference directly.
11665 if (NewMemRefs.size() == 1) {
11666 N->MemRefs = NewMemRefs[0];
11667 N->NumMemRefs = 1;
11668 return;
11669 }
11670
11671 MachineMemOperand **MemRefsBuffer =
11672 Allocator.template Allocate<MachineMemOperand *>(NewMemRefs.size());
11673 llvm::copy(NewMemRefs, MemRefsBuffer);
11674 N->MemRefs = MemRefsBuffer;
11675 N->NumMemRefs = static_cast<int>(NewMemRefs.size());
11676}
11677
11678/// SelectNodeTo - These are wrappers around MorphNodeTo that accept a
11679/// machine opcode.
11680///
11682 EVT VT) {
11683 SDVTList VTs = getVTList(VT);
11684 return SelectNodeTo(N, MachineOpc, VTs, {});
11685}
11686
11688 EVT VT, SDValue Op1) {
11689 SDVTList VTs = getVTList(VT);
11690 SDValue Ops[] = { Op1 };
11691 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11692}
11693
11695 EVT VT, SDValue Op1,
11696 SDValue Op2) {
11697 SDVTList VTs = getVTList(VT);
11698 SDValue Ops[] = { Op1, Op2 };
11699 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11700}
11701
11703 EVT VT, SDValue Op1,
11704 SDValue Op2, SDValue Op3) {
11705 SDVTList VTs = getVTList(VT);
11706 SDValue Ops[] = { Op1, Op2, Op3 };
11707 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11708}
11709
11712 SDVTList VTs = getVTList(VT);
11713 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11714}
11715
11717 EVT VT1, EVT VT2, ArrayRef<SDValue> Ops) {
11718 SDVTList VTs = getVTList(VT1, VT2);
11719 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11720}
11721
11723 EVT VT1, EVT VT2) {
11724 SDVTList VTs = getVTList(VT1, VT2);
11725 return SelectNodeTo(N, MachineOpc, VTs, {});
11726}
11727
11729 EVT VT1, EVT VT2, EVT VT3,
11731 SDVTList VTs = getVTList(VT1, VT2, VT3);
11732 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11733}
11734
11736 EVT VT1, EVT VT2,
11737 SDValue Op1, SDValue Op2) {
11738 SDVTList VTs = getVTList(VT1, VT2);
11739 SDValue Ops[] = { Op1, Op2 };
11740 return SelectNodeTo(N, MachineOpc, VTs, Ops);
11741}
11742
11745 SDNode *New = MorphNodeTo(N, ~MachineOpc, VTs, Ops);
11746 // Reset the NodeID to -1.
11747 New->setNodeId(-1);
11748 if (New != N) {
11749 ReplaceAllUsesWith(N, New);
11751 }
11752 return New;
11753}
11754
11755/// UpdateSDLocOnMergeSDNode - If the opt level is -O0 then it throws away
11756/// the line number information on the merged node since it is not possible to
11757/// preserve the information that operation is associated with multiple lines.
11758/// This will make the debugger working better at -O0, were there is a higher
11759/// probability having other instructions associated with that line.
11760///
11761/// For IROrder, we keep the smaller of the two
11762SDNode *SelectionDAG::UpdateSDLocOnMergeSDNode(SDNode *N, const SDLoc &OLoc) {
11763 DebugLoc NLoc = N->getDebugLoc();
11764 if (NLoc && OptLevel == CodeGenOptLevel::None && OLoc.getDebugLoc() != NLoc) {
11765 N->setDebugLoc(DebugLoc());
11766 }
11767 unsigned Order = std::min(N->getIROrder(), OLoc.getIROrder());
11768 N->setIROrder(Order);
11769 return N;
11770}
11771
11772/// MorphNodeTo - This *mutates* the specified node to have the specified
11773/// return type, opcode, and operands.
11774///
11775/// Note that MorphNodeTo returns the resultant node. If there is already a
11776/// node of the specified opcode and operands, it returns that node instead of
11777/// the current one. Note that the SDLoc need not be the same.
11778///
11779/// Using MorphNodeTo is faster than creating a new node and swapping it in
11780/// with ReplaceAllUsesWith both because it often avoids allocating a new
11781/// node, and because it doesn't require CSE recalculation for any of
11782/// the node's users.
11783///
11784/// However, note that MorphNodeTo recursively deletes dead nodes from the DAG.
11785/// As a consequence it isn't appropriate to use from within the DAG combiner or
11786/// the legalizer which maintain worklists that would need to be updated when
11787/// deleting things.
11790 // If an identical node already exists, use it.
11791 void *IP = nullptr;
11792 if (VTs.VTs[VTs.NumVTs-1] != MVT::Glue) {
11794 AddNodeIDNode(ID, Opc, VTs, Ops);
11795 if (SDNode *ON = FindNodeOrInsertPos(ID, SDLoc(N), IP))
11796 return UpdateSDLocOnMergeSDNode(ON, SDLoc(N));
11797 }
11798
11799 if (!RemoveNodeFromCSEMaps(N))
11800 IP = nullptr;
11801
11802 // Start the morphing.
11803 N->NodeType = Opc;
11804 N->ValueList = VTs.VTs;
11805 N->NumValues = VTs.NumVTs;
11806
11807 // Clear the operands list, updating used nodes to remove this from their
11808 // use list. Keep track of any operands that become dead as a result.
11809 SmallPtrSet<SDNode*, 16> DeadNodeSet;
11810 for (SDNode::op_iterator I = N->op_begin(), E = N->op_end(); I != E; ) {
11811 SDUse &Use = *I++;
11812 SDNode *Used = Use.getNode();
11813 Use.set(SDValue());
11814 if (Used->use_empty())
11815 DeadNodeSet.insert(Used);
11816 }
11817
11818 // For MachineNode, initialize the memory references information.
11820 MN->clearMemRefs();
11821
11822 // Swap for an appropriately sized array from the recycler.
11823 removeOperands(N);
11824 createOperands(N, Ops);
11825
11826 // Delete any nodes that are still dead after adding the uses for the
11827 // new operands.
11828 if (!DeadNodeSet.empty()) {
11829 SmallVector<SDNode *, 16> DeadNodes;
11830 for (SDNode *N : DeadNodeSet)
11831 if (N->use_empty())
11832 DeadNodes.push_back(N);
11833 RemoveDeadNodes(DeadNodes);
11834 }
11835
11836 if (IP)
11837 CSEMap.InsertNode(N, IP); // Memoize the new node.
11838 return N;
11839}
11840
11842 unsigned OrigOpc = Node->getOpcode();
11843 unsigned NewOpc;
11844 switch (OrigOpc) {
11845 default:
11846 llvm_unreachable("mutateStrictFPToFP called with unexpected opcode!");
11847#define DAG_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11848 case ISD::STRICT_##DAGN: NewOpc = ISD::DAGN; break;
11849#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
11850 case ISD::STRICT_##DAGN: NewOpc = ISD::SETCC; break;
11851#include "llvm/IR/ConstrainedOps.def"
11852 }
11853
11854 assert(Node->getNumValues() == 2 && "Unexpected number of results!");
11855
11856 // We're taking this node out of the chain, so we need to re-link things.
11857 SDValue InputChain = Node->getOperand(0);
11858 SDValue OutputChain = SDValue(Node, 1);
11859 ReplaceAllUsesOfValueWith(OutputChain, InputChain);
11860
11862 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
11863 Ops.push_back(Node->getOperand(i));
11864
11865 SDVTList VTs = getVTList(Node->getValueType(0));
11866 SDNode *Res = MorphNodeTo(Node, NewOpc, VTs, Ops);
11867
11868 // MorphNodeTo can operate in two ways: if an existing node with the
11869 // specified operands exists, it can just return it. Otherwise, it
11870 // updates the node in place to have the requested operands.
11871 if (Res == Node) {
11872 // If we updated the node in place, reset the node ID. To the isel,
11873 // this should be just like a newly allocated machine node.
11874 Res->setNodeId(-1);
11875 } else {
11878 }
11879
11880 return Res;
11881}
11882
11883/// getMachineNode - These are used for target selectors to create a new node
11884/// with specified return type(s), MachineInstr opcode, and operands.
11885///
11886/// Note that getMachineNode returns the resultant node. If there is already a
11887/// node of the specified opcode and operands, it returns that node instead of
11888/// the current one.
11890 EVT VT) {
11891 SDVTList VTs = getVTList(VT);
11892 return getMachineNode(Opcode, dl, VTs, {});
11893}
11894
11896 EVT VT, SDValue Op1) {
11897 SDVTList VTs = getVTList(VT);
11898 SDValue Ops[] = { Op1 };
11899 return getMachineNode(Opcode, dl, VTs, Ops);
11900}
11901
11903 EVT VT, SDValue Op1, SDValue Op2) {
11904 SDVTList VTs = getVTList(VT);
11905 SDValue Ops[] = { Op1, Op2 };
11906 return getMachineNode(Opcode, dl, VTs, Ops);
11907}
11908
11910 EVT VT, SDValue Op1, SDValue Op2,
11911 SDValue Op3) {
11912 SDVTList VTs = getVTList(VT);
11913 SDValue Ops[] = { Op1, Op2, Op3 };
11914 return getMachineNode(Opcode, dl, VTs, Ops);
11915}
11916
11919 SDVTList VTs = getVTList(VT);
11920 return getMachineNode(Opcode, dl, VTs, Ops);
11921}
11922
11924 EVT VT1, EVT VT2, SDValue Op1,
11925 SDValue Op2) {
11926 SDVTList VTs = getVTList(VT1, VT2);
11927 SDValue Ops[] = { Op1, Op2 };
11928 return getMachineNode(Opcode, dl, VTs, Ops);
11929}
11930
11932 EVT VT1, EVT VT2, SDValue Op1,
11933 SDValue Op2, SDValue Op3) {
11934 SDVTList VTs = getVTList(VT1, VT2);
11935 SDValue Ops[] = { Op1, Op2, Op3 };
11936 return getMachineNode(Opcode, dl, VTs, Ops);
11937}
11938
11940 EVT VT1, EVT VT2,
11942 SDVTList VTs = getVTList(VT1, VT2);
11943 return getMachineNode(Opcode, dl, VTs, Ops);
11944}
11945
11947 EVT VT1, EVT VT2, EVT VT3,
11948 SDValue Op1, SDValue Op2) {
11949 SDVTList VTs = getVTList(VT1, VT2, VT3);
11950 SDValue Ops[] = { Op1, Op2 };
11951 return getMachineNode(Opcode, dl, VTs, Ops);
11952}
11953
11955 EVT VT1, EVT VT2, EVT VT3,
11956 SDValue Op1, SDValue Op2,
11957 SDValue Op3) {
11958 SDVTList VTs = getVTList(VT1, VT2, VT3);
11959 SDValue Ops[] = { Op1, Op2, Op3 };
11960 return getMachineNode(Opcode, dl, VTs, Ops);
11961}
11962
11964 EVT VT1, EVT VT2, EVT VT3,
11966 SDVTList VTs = getVTList(VT1, VT2, VT3);
11967 return getMachineNode(Opcode, dl, VTs, Ops);
11968}
11969
11971 ArrayRef<EVT> ResultTys,
11973 SDVTList VTs = getVTList(ResultTys);
11974 return getMachineNode(Opcode, dl, VTs, Ops);
11975}
11976
11978 SDVTList VTs,
11980 bool DoCSE = VTs.VTs[VTs.NumVTs-1] != MVT::Glue;
11982 void *IP = nullptr;
11983
11984 if (DoCSE) {
11986 AddNodeIDNode(ID, ~Opcode, VTs, Ops);
11987 IP = nullptr;
11988 if (SDNode *E = FindNodeOrInsertPos(ID, DL, IP)) {
11989 return cast<MachineSDNode>(UpdateSDLocOnMergeSDNode(E, DL));
11990 }
11991 }
11992
11993 // Allocate a new MachineSDNode.
11994 N = newSDNode<MachineSDNode>(~Opcode, DL.getIROrder(), DL.getDebugLoc(), VTs);
11995 createOperands(N, Ops);
11996
11997 if (DoCSE)
11998 CSEMap.InsertNode(N, IP);
11999
12000 InsertNode(N);
12001 NewSDValueDbgMsg(SDValue(N, 0), "Creating new machine node: ", this);
12002 return N;
12003}
12004
12005/// getTargetExtractSubreg - A convenience function for creating
12006/// TargetOpcode::EXTRACT_SUBREG nodes.
12008 SDValue Operand) {
12009 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12010 SDNode *Subreg = getMachineNode(TargetOpcode::EXTRACT_SUBREG, DL,
12011 VT, Operand, SRIdxVal);
12012 return SDValue(Subreg, 0);
12013}
12014
12015/// getTargetInsertSubreg - A convenience function for creating
12016/// TargetOpcode::INSERT_SUBREG nodes.
12018 SDValue Operand, SDValue Subreg) {
12019 SDValue SRIdxVal = getTargetConstant(SRIdx, DL, MVT::i32);
12020 SDNode *Result = getMachineNode(TargetOpcode::INSERT_SUBREG, DL,
12021 VT, Operand, Subreg, SRIdxVal);
12022 return SDValue(Result, 0);
12023}
12024
12025/// getNodeIfExists - Get the specified node if it's already available, or
12026/// else return NULL.
12029 bool AllowCommute) {
12030 SDNodeFlags Flags;
12031 if (Inserter)
12032 Flags = Inserter->getFlags();
12033 return getNodeIfExists(Opcode, VTList, Ops, Flags, AllowCommute);
12034}
12035
12038 const SDNodeFlags Flags,
12039 bool AllowCommute) {
12040 if (VTList.VTs[VTList.NumVTs - 1] == MVT::Glue)
12041 return nullptr;
12042
12043 auto Lookup = [&](ArrayRef<SDValue> LookupOps) -> SDNode * {
12045 AddNodeIDNode(ID, Opcode, VTList, LookupOps);
12046 void *IP = nullptr;
12047 if (SDNode *E = FindNodeOrInsertPos(ID, IP)) {
12048 E->intersectFlagsWith(Flags);
12049 return E;
12050 }
12051 return nullptr;
12052 };
12053
12054 if (SDNode *Existing = Lookup(Ops))
12055 return Existing;
12056
12057 if (AllowCommute && TLI->isCommutativeBinOp(Opcode))
12058 return Lookup({Ops[1], Ops[0]});
12059
12060 return nullptr;
12061}
12062
12063/// doesNodeExist - Check if a node exists without modifying its flags.
12064bool SelectionDAG::doesNodeExist(unsigned Opcode, SDVTList VTList,
12066 if (VTList.VTs[VTList.NumVTs - 1] != MVT::Glue) {
12068 AddNodeIDNode(ID, Opcode, VTList, Ops);
12069 void *IP = nullptr;
12070 if (FindNodeOrInsertPos(ID, SDLoc(), IP))
12071 return true;
12072 }
12073 return false;
12074}
12075
12076/// getDbgValue - Creates a SDDbgValue node.
12077///
12078/// SDNode
12080 SDNode *N, unsigned R, 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::fromNode(N, R),
12086 {}, IsIndirect, DL, O,
12087 /*IsVariadic=*/false);
12088}
12089
12090/// Constant
12092 DIExpression *Expr,
12093 const Value *C,
12094 const DebugLoc &DL, unsigned O) {
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, SDDbgOperand::fromConst(C), {},
12099 /*IsIndirect=*/false, DL, O,
12100 /*IsVariadic=*/false);
12101}
12102
12103/// FrameIndex
12105 DIExpression *Expr, unsigned FI,
12106 bool IsIndirect,
12107 const DebugLoc &DL,
12108 unsigned O) {
12109 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12110 "Expected inlined-at fields to agree");
12111 return getFrameIndexDbgValue(Var, Expr, FI, {}, IsIndirect, DL, O);
12112}
12113
12114/// FrameIndex with dependencies
12116 DIExpression *Expr, unsigned FI,
12117 ArrayRef<SDNode *> Dependencies,
12118 bool IsIndirect,
12119 const DebugLoc &DL,
12120 unsigned O) {
12121 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12122 "Expected inlined-at fields to agree");
12123 return new (DbgInfo->getAlloc())
12124 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromFrameIdx(FI),
12125 Dependencies, IsIndirect, DL, O,
12126 /*IsVariadic=*/false);
12127}
12128
12129/// VReg
12131 Register VReg, bool IsIndirect,
12132 const DebugLoc &DL, unsigned O) {
12133 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12134 "Expected inlined-at fields to agree");
12135 return new (DbgInfo->getAlloc())
12136 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, SDDbgOperand::fromVReg(VReg),
12137 {}, IsIndirect, DL, O,
12138 /*IsVariadic=*/false);
12139}
12140
12143 ArrayRef<SDNode *> Dependencies,
12144 bool IsIndirect, const DebugLoc &DL,
12145 unsigned O, bool IsVariadic) {
12146 assert(cast<DILocalVariable>(Var)->isValidLocationForIntrinsic(DL) &&
12147 "Expected inlined-at fields to agree");
12148 return new (DbgInfo->getAlloc())
12149 SDDbgValue(DbgInfo->getAlloc(), Var, Expr, Locs, Dependencies, IsIndirect,
12150 DL, O, IsVariadic);
12151}
12152
12154 unsigned OffsetInBits, unsigned SizeInBits,
12155 bool InvalidateDbg) {
12156 SDNode *FromNode = From.getNode();
12157 SDNode *ToNode = To.getNode();
12158 assert(FromNode && ToNode && "Can't modify dbg values");
12159
12160 // PR35338
12161 // TODO: assert(From != To && "Redundant dbg value transfer");
12162 // TODO: assert(FromNode != ToNode && "Intranode dbg value transfer");
12163 if (From == To || FromNode == ToNode)
12164 return;
12165
12166 if (!FromNode->getHasDebugValue())
12167 return;
12168
12169 SDDbgOperand FromLocOp =
12170 SDDbgOperand::fromNode(From.getNode(), From.getResNo());
12172
12174 for (SDDbgValue *Dbg : GetDbgValues(FromNode)) {
12175 if (Dbg->isInvalidated())
12176 continue;
12177
12178 // TODO: assert(!Dbg->isInvalidated() && "Transfer of invalid dbg value");
12179
12180 // Create a new location ops vector that is equal to the old vector, but
12181 // with each instance of FromLocOp replaced with ToLocOp.
12182 bool Changed = false;
12183 auto NewLocOps = Dbg->copyLocationOps();
12184 std::replace_if(
12185 NewLocOps.begin(), NewLocOps.end(),
12186 [&Changed, FromLocOp](const SDDbgOperand &Op) {
12187 bool Match = Op == FromLocOp;
12188 Changed |= Match;
12189 return Match;
12190 },
12191 ToLocOp);
12192 // Ignore this SDDbgValue if we didn't find a matching location.
12193 if (!Changed)
12194 continue;
12195
12196 DIVariable *Var = Dbg->getVariable();
12197 auto *Expr = Dbg->getExpression();
12198 // If a fragment is requested, update the expression.
12199 if (SizeInBits) {
12200 // When splitting a larger (e.g., sign-extended) value whose
12201 // lower bits are described with an SDDbgValue, do not attempt
12202 // to transfer the SDDbgValue to the upper bits.
12203 if (auto FI = Expr->getFragmentInfo())
12204 if (OffsetInBits + SizeInBits > FI->SizeInBits)
12205 continue;
12206 auto Fragment = DIExpression::createFragmentExpression(Expr, OffsetInBits,
12207 SizeInBits);
12208 if (!Fragment)
12209 continue;
12210 Expr = *Fragment;
12211 }
12212
12213 auto AdditionalDependencies = Dbg->getAdditionalDependencies();
12214 // Clone the SDDbgValue and move it to To.
12215 SDDbgValue *Clone = getDbgValueList(
12216 Var, Expr, NewLocOps, AdditionalDependencies, Dbg->isIndirect(),
12217 Dbg->getDebugLoc(), std::max(ToNode->getIROrder(), Dbg->getOrder()),
12218 Dbg->isVariadic());
12219 ClonedDVs.push_back(Clone);
12220
12221 if (InvalidateDbg) {
12222 // Invalidate value and indicate the SDDbgValue should not be emitted.
12223 Dbg->setIsInvalidated();
12224 Dbg->setIsEmitted();
12225 }
12226 }
12227
12228 for (SDDbgValue *Dbg : ClonedDVs) {
12229 assert(is_contained(Dbg->getSDNodes(), ToNode) &&
12230 "Transferred DbgValues should depend on the new SDNode");
12231 AddDbgValue(Dbg, false);
12232 }
12233}
12234
12236 if (!N.getHasDebugValue())
12237 return;
12238
12239 auto GetLocationOperand = [](SDNode *Node, unsigned ResNo) {
12240 if (auto *FISDN = dyn_cast<FrameIndexSDNode>(Node))
12241 return SDDbgOperand::fromFrameIdx(FISDN->getIndex());
12242 return SDDbgOperand::fromNode(Node, ResNo);
12243 };
12244
12246 for (auto *DV : GetDbgValues(&N)) {
12247 if (DV->isInvalidated())
12248 continue;
12249 switch (N.getOpcode()) {
12250 default:
12251 break;
12252 case ISD::ADD: {
12253 SDValue N0 = N.getOperand(0);
12254 SDValue N1 = N.getOperand(1);
12255 if (!isa<ConstantSDNode>(N0)) {
12256 bool RHSConstant = isa<ConstantSDNode>(N1);
12258 if (RHSConstant)
12259 Offset = N.getConstantOperandVal(1);
12260 // We are not allowed to turn indirect debug values variadic, so
12261 // don't salvage those.
12262 if (!RHSConstant && DV->isIndirect())
12263 continue;
12264
12265 // Rewrite an ADD constant node into a DIExpression. Since we are
12266 // performing arithmetic to compute the variable's *value* in the
12267 // DIExpression, we need to mark the expression with a
12268 // DW_OP_stack_value.
12269 auto *DIExpr = DV->getExpression();
12270 auto NewLocOps = DV->copyLocationOps();
12271 bool Changed = false;
12272 size_t OrigLocOpsSize = NewLocOps.size();
12273 for (size_t i = 0; i < OrigLocOpsSize; ++i) {
12274 // We're not given a ResNo to compare against because the whole
12275 // node is going away. We know that any ISD::ADD only has one
12276 // result, so we can assume any node match is using the result.
12277 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12278 NewLocOps[i].getSDNode() != &N)
12279 continue;
12280 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12281 if (RHSConstant) {
12284 DIExpr = DIExpression::appendOpsToArg(DIExpr, ExprOps, i, true);
12285 } else {
12286 // Convert to a variadic expression (if not already).
12287 // convertToVariadicExpression() returns a const pointer, so we use
12288 // a temporary const variable here.
12289 const auto *TmpDIExpr =
12293 ExprOps.push_back(NewLocOps.size());
12294 ExprOps.push_back(dwarf::DW_OP_plus);
12295 SDDbgOperand RHS =
12297 NewLocOps.push_back(RHS);
12298 DIExpr = DIExpression::appendOpsToArg(TmpDIExpr, ExprOps, i, true);
12299 }
12300 Changed = true;
12301 }
12302 (void)Changed;
12303 assert(Changed && "Salvage target doesn't use N");
12304
12305 bool IsVariadic =
12306 DV->isVariadic() || OrigLocOpsSize != NewLocOps.size();
12307
12308 auto AdditionalDependencies = DV->getAdditionalDependencies();
12309 SDDbgValue *Clone = getDbgValueList(
12310 DV->getVariable(), DIExpr, NewLocOps, AdditionalDependencies,
12311 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder(), IsVariadic);
12312 ClonedDVs.push_back(Clone);
12313 DV->setIsInvalidated();
12314 DV->setIsEmitted();
12315 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting";
12316 N0.getNode()->dumprFull(this);
12317 dbgs() << " into " << *DIExpr << '\n');
12318 }
12319 break;
12320 }
12321 case ISD::TRUNCATE: {
12322 SDValue N0 = N.getOperand(0);
12323 TypeSize FromSize = N0.getValueSizeInBits();
12324 TypeSize ToSize = N.getValueSizeInBits(0);
12325
12326 DIExpression *DbgExpression = DV->getExpression();
12327 auto ExtOps = DIExpression::getExtOps(FromSize, ToSize, false);
12328 auto NewLocOps = DV->copyLocationOps();
12329 bool Changed = false;
12330 for (size_t i = 0; i < NewLocOps.size(); ++i) {
12331 if (NewLocOps[i].getKind() != SDDbgOperand::SDNODE ||
12332 NewLocOps[i].getSDNode() != &N)
12333 continue;
12334
12335 NewLocOps[i] = GetLocationOperand(N0.getNode(), N0.getResNo());
12336 DbgExpression = DIExpression::appendOpsToArg(DbgExpression, ExtOps, i);
12337 Changed = true;
12338 }
12339 assert(Changed && "Salvage target doesn't use N");
12340 (void)Changed;
12341
12342 SDDbgValue *Clone =
12343 getDbgValueList(DV->getVariable(), DbgExpression, NewLocOps,
12344 DV->getAdditionalDependencies(), DV->isIndirect(),
12345 DV->getDebugLoc(), DV->getOrder(), DV->isVariadic());
12346
12347 ClonedDVs.push_back(Clone);
12348 DV->setIsInvalidated();
12349 DV->setIsEmitted();
12350 LLVM_DEBUG(dbgs() << "SALVAGE: Rewriting"; N0.getNode()->dumprFull(this);
12351 dbgs() << " into " << *DbgExpression << '\n');
12352 break;
12353 }
12354 }
12355 }
12356
12357 for (SDDbgValue *Dbg : ClonedDVs) {
12358 assert((!Dbg->getSDNodes().empty() ||
12359 llvm::any_of(Dbg->getLocationOps(),
12360 [&](const SDDbgOperand &Op) {
12361 return Op.getKind() == SDDbgOperand::FRAMEIX;
12362 })) &&
12363 "Salvaged DbgValue should depend on a new SDNode");
12364 AddDbgValue(Dbg, false);
12365 }
12366}
12367
12368/// Creates a SDDbgLabel node.
12370 const DebugLoc &DL, unsigned O) {
12371 assert(cast<DILabel>(Label)->isValidLocationForIntrinsic(DL) &&
12372 "Expected inlined-at fields to agree");
12373 return new (DbgInfo->getAlloc()) SDDbgLabel(Label, DL, O);
12374}
12375
12376namespace {
12377
12378/// RAUWUpdateListener - Helper for ReplaceAllUsesWith - When the node
12379/// pointed to by a use iterator is deleted, increment the use iterator
12380/// so that it doesn't dangle.
12381///
12382class RAUWUpdateListener : public SelectionDAG::DAGUpdateListener {
12385
12386 void NodeDeleted(SDNode *N, SDNode *E) override {
12387 // Increment the iterator as needed.
12388 while (UI != UE && N == UI->getUser())
12389 ++UI;
12390 }
12391
12392public:
12393 RAUWUpdateListener(SelectionDAG &d,
12396 : SelectionDAG::DAGUpdateListener(d), UI(ui), UE(ue) {}
12397};
12398
12399} // end anonymous namespace
12400
12401/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12402/// This can cause recursive merging of nodes in the DAG.
12403///
12404/// This version assumes From has a single result value.
12405///
12407 SDNode *From = FromN.getNode();
12408 assert(From->getNumValues() == 1 && FromN.getResNo() == 0 &&
12409 "Cannot replace with this method!");
12410 assert(From != To.getNode() && "Cannot replace uses of with self");
12411
12412 // Preserve Debug Values
12413 transferDbgValues(FromN, To);
12414 // Preserve extra info.
12415 copyExtraInfo(From, To.getNode());
12416
12417 // Iterate over all the existing uses of From. New uses will be added
12418 // to the beginning of the use list, which we avoid visiting.
12419 // This specifically avoids visiting uses of From that arise while the
12420 // replacement is happening, because any such uses would be the result
12421 // of CSE: If an existing node looks like From after one of its operands
12422 // is replaced by To, we don't want to replace of all its users with To
12423 // too. See PR3018 for more info.
12424 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12425 RAUWUpdateListener Listener(*this, UI, UE);
12426 while (UI != UE) {
12427 SDNode *User = UI->getUser();
12428
12429 // This node is about to morph, remove its old self from the CSE maps.
12430 RemoveNodeFromCSEMaps(User);
12431
12432 // A user can appear in a use list multiple times, and when this
12433 // happens the uses are usually next to each other in the list.
12434 // To help reduce the number of CSE recomputations, process all
12435 // the uses of this user that we can find this way.
12436 do {
12437 SDUse &Use = *UI;
12438 ++UI;
12439 Use.set(To);
12440 if (To->isDivergent() != From->isDivergent())
12442 } while (UI != UE && UI->getUser() == User);
12443 // Now that we have modified User, add it back to the CSE maps. If it
12444 // already exists there, recursively merge the results together.
12445 AddModifiedNodeToCSEMaps(User);
12446 }
12447
12448 // If we just RAUW'd the root, take note.
12449 if (FromN == getRoot())
12450 setRoot(To);
12451}
12452
12453/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12454/// This can cause recursive merging of nodes in the DAG.
12455///
12456/// This version assumes that for each value of From, there is a
12457/// corresponding value in To in the same position with the same type.
12458///
12460#ifndef NDEBUG
12461 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12462 assert((!From->hasAnyUseOfValue(i) ||
12463 From->getValueType(i) == To->getValueType(i)) &&
12464 "Cannot use this version of ReplaceAllUsesWith!");
12465#endif
12466
12467 // Handle the trivial case.
12468 if (From == To)
12469 return;
12470
12471 // Preserve Debug Info. Only do this if there's a use.
12472 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
12473 if (From->hasAnyUseOfValue(i)) {
12474 assert((i < To->getNumValues()) && "Invalid To location");
12475 transferDbgValues(SDValue(From, i), SDValue(To, i));
12476 }
12477 // Preserve extra info.
12478 copyExtraInfo(From, To);
12479
12480 // Iterate over just the existing users of From. See the comments in
12481 // the ReplaceAllUsesWith above.
12482 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12483 RAUWUpdateListener Listener(*this, UI, UE);
12484 while (UI != UE) {
12485 SDNode *User = UI->getUser();
12486
12487 // This node is about to morph, remove its old self from the CSE maps.
12488 RemoveNodeFromCSEMaps(User);
12489
12490 // A user can appear in a use list multiple times, and when this
12491 // happens the uses are usually next to each other in the list.
12492 // To help reduce the number of CSE recomputations, process all
12493 // the uses of this user that we can find this way.
12494 do {
12495 SDUse &Use = *UI;
12496 ++UI;
12497 Use.setNode(To);
12498 if (To->isDivergent() != From->isDivergent())
12500 } while (UI != UE && UI->getUser() == User);
12501
12502 // Now that we have modified User, add it back to the CSE maps. If it
12503 // already exists there, recursively merge the results together.
12504 AddModifiedNodeToCSEMaps(User);
12505 }
12506
12507 // If we just RAUW'd the root, take note.
12508 if (From == getRoot().getNode())
12509 setRoot(SDValue(To, getRoot().getResNo()));
12510}
12511
12512/// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
12513/// This can cause recursive merging of nodes in the DAG.
12514///
12515/// This version can replace From with any result values. To must match the
12516/// number and types of values returned by From.
12518 if (From->getNumValues() == 1) // Handle the simple case efficiently.
12519 return ReplaceAllUsesWith(SDValue(From, 0), To[0]);
12520
12521 for (unsigned i = 0, e = From->getNumValues(); i != e; ++i) {
12522 // Preserve Debug Info.
12523 transferDbgValues(SDValue(From, i), To[i]);
12524 // Preserve extra info.
12525 copyExtraInfo(From, To[i].getNode());
12526 }
12527
12528 // Iterate over just the existing users of From. See the comments in
12529 // the ReplaceAllUsesWith above.
12530 SDNode::use_iterator UI = From->use_begin(), UE = From->use_end();
12531 RAUWUpdateListener Listener(*this, UI, UE);
12532 while (UI != UE) {
12533 SDNode *User = UI->getUser();
12534
12535 // This node is about to morph, remove its old self from the CSE maps.
12536 RemoveNodeFromCSEMaps(User);
12537
12538 // A user can appear in a use list multiple times, and when this happens the
12539 // uses are usually next to each other in the list. To help reduce the
12540 // number of CSE and divergence recomputations, process all the uses of this
12541 // user that we can find this way.
12542 bool To_IsDivergent = false;
12543 do {
12544 SDUse &Use = *UI;
12545 const SDValue &ToOp = To[Use.getResNo()];
12546 ++UI;
12547 Use.set(ToOp);
12548 if (ToOp.getValueType() != MVT::Other)
12549 To_IsDivergent |= ToOp->isDivergent();
12550 } while (UI != UE && UI->getUser() == User);
12551
12552 if (To_IsDivergent != From->isDivergent())
12554
12555 // Now that we have modified User, add it back to the CSE maps. If it
12556 // already exists there, recursively merge the results together.
12557 AddModifiedNodeToCSEMaps(User);
12558 }
12559
12560 // If we just RAUW'd the root, take note.
12561 if (From == getRoot().getNode())
12562 setRoot(SDValue(To[getRoot().getResNo()]));
12563}
12564
12565/// ReplaceAllUsesOfValueWith - Replace any uses of From with To, leaving
12566/// uses of other values produced by From.getNode() alone. The Deleted
12567/// vector is handled the same way as for ReplaceAllUsesWith.
12569 // Handle the really simple, really trivial case efficiently.
12570 if (From == To) return;
12571
12572 // Handle the simple, trivial, case efficiently.
12573 if (From.getNode()->getNumValues() == 1) {
12574 ReplaceAllUsesWith(From, To);
12575 return;
12576 }
12577
12578 // Preserve Debug Info.
12579 transferDbgValues(From, To);
12580 copyExtraInfo(From.getNode(), To.getNode());
12581
12582 // Iterate over just the existing users of From. See the comments in
12583 // the ReplaceAllUsesWith above.
12584 SDNode::use_iterator UI = From.getNode()->use_begin(),
12585 UE = From.getNode()->use_end();
12586 RAUWUpdateListener Listener(*this, UI, UE);
12587 while (UI != UE) {
12588 SDNode *User = UI->getUser();
12589 bool UserRemovedFromCSEMaps = false;
12590
12591 // A user can appear in a use list multiple times, and when this
12592 // happens the uses are usually next to each other in the list.
12593 // To help reduce the number of CSE recomputations, process all
12594 // the uses of this user that we can find this way.
12595 do {
12596 SDUse &Use = *UI;
12597
12598 // Skip uses of different values from the same node.
12599 if (Use.getResNo() != From.getResNo()) {
12600 ++UI;
12601 continue;
12602 }
12603
12604 // If this node hasn't been modified yet, it's still in the CSE maps,
12605 // so remove its old self from the CSE maps.
12606 if (!UserRemovedFromCSEMaps) {
12607 RemoveNodeFromCSEMaps(User);
12608 UserRemovedFromCSEMaps = true;
12609 }
12610
12611 ++UI;
12612 Use.set(To);
12613 if (To->isDivergent() != From->isDivergent())
12615 } while (UI != UE && UI->getUser() == User);
12616 // We are iterating over all uses of the From node, so if a use
12617 // doesn't use the specific value, no changes are made.
12618 if (!UserRemovedFromCSEMaps)
12619 continue;
12620
12621 // Now that we have modified User, add it back to the CSE maps. If it
12622 // already exists there, recursively merge the results together.
12623 AddModifiedNodeToCSEMaps(User);
12624 }
12625
12626 // If we just RAUW'd the root, take note.
12627 if (From == getRoot())
12628 setRoot(To);
12629}
12630
12631namespace {
12632
12633/// UseMemo - This class is used by SelectionDAG::ReplaceAllUsesOfValuesWith
12634/// to record information about a use.
12635struct UseMemo {
12636 SDNode *User;
12637 unsigned Index;
12638 SDUse *Use;
12639};
12640
12641/// operator< - Sort Memos by User.
12642bool operator<(const UseMemo &L, const UseMemo &R) {
12643 return (intptr_t)L.User < (intptr_t)R.User;
12644}
12645
12646/// RAUOVWUpdateListener - Helper for ReplaceAllUsesOfValuesWith - When the node
12647/// pointed to by a UseMemo is deleted, set the User to nullptr to indicate that
12648/// the node already has been taken care of recursively.
12649class RAUOVWUpdateListener : public SelectionDAG::DAGUpdateListener {
12650 SmallVectorImpl<UseMemo> &Uses;
12651
12652 void NodeDeleted(SDNode *N, SDNode *E) override {
12653 for (UseMemo &Memo : Uses)
12654 if (Memo.User == N)
12655 Memo.User = nullptr;
12656 }
12657
12658public:
12659 RAUOVWUpdateListener(SelectionDAG &d, SmallVectorImpl<UseMemo> &uses)
12660 : SelectionDAG::DAGUpdateListener(d), Uses(uses) {}
12661};
12662
12663} // end anonymous namespace
12664
12665/// Return true if a glue output should propagate divergence information.
12667 switch (Node->getOpcode()) {
12668 case ISD::CopyFromReg:
12669 case ISD::CopyToReg:
12670 return false;
12671 default:
12672 return true;
12673 }
12674
12675 llvm_unreachable("covered opcode switch");
12676}
12677
12679 if (TLI->isSDNodeAlwaysUniform(N)) {
12680 assert(!TLI->isSDNodeSourceOfDivergence(N, FLI, UA) &&
12681 "Conflicting divergence information!");
12682 return false;
12683 }
12684 if (TLI->isSDNodeSourceOfDivergence(N, FLI, UA))
12685 return true;
12686 for (const auto &Op : N->ops()) {
12687 EVT VT = Op.getValueType();
12688
12689 // Skip Chain. It does not carry divergence.
12690 if (VT != MVT::Other && Op.getNode()->isDivergent() &&
12691 (VT != MVT::Glue || gluePropagatesDivergence(Op.getNode())))
12692 return true;
12693 }
12694 return false;
12695}
12696
12698 SmallVector<SDNode *, 16> Worklist(1, N);
12699 do {
12700 N = Worklist.pop_back_val();
12701 bool IsDivergent = calculateDivergence(N);
12702 if (N->SDNodeBits.IsDivergent != IsDivergent) {
12703 N->SDNodeBits.IsDivergent = IsDivergent;
12704 llvm::append_range(Worklist, N->users());
12705 }
12706 } while (!Worklist.empty());
12707}
12708
12709void SelectionDAG::CreateTopologicalOrder(std::vector<SDNode *> &Order) {
12711 Order.reserve(AllNodes.size());
12712 for (auto &N : allnodes()) {
12713 unsigned NOps = N.getNumOperands();
12714 Degree[&N] = NOps;
12715 if (0 == NOps)
12716 Order.push_back(&N);
12717 }
12718 for (size_t I = 0; I != Order.size(); ++I) {
12719 SDNode *N = Order[I];
12720 for (auto *U : N->users()) {
12721 unsigned &UnsortedOps = Degree[U];
12722 if (0 == --UnsortedOps)
12723 Order.push_back(U);
12724 }
12725 }
12726}
12727
12728#if !defined(NDEBUG) && LLVM_ENABLE_ABI_BREAKING_CHECKS
12729void SelectionDAG::VerifyDAGDivergence() {
12730 std::vector<SDNode *> TopoOrder;
12731 CreateTopologicalOrder(TopoOrder);
12732 for (auto *N : TopoOrder) {
12733 assert(calculateDivergence(N) == N->isDivergent() &&
12734 "Divergence bit inconsistency detected");
12735 }
12736}
12737#endif
12738
12739/// ReplaceAllUsesOfValuesWith - Replace any uses of From with To, leaving
12740/// uses of other values produced by From.getNode() alone. The same value
12741/// may appear in both the From and To list. The Deleted vector is
12742/// handled the same way as for ReplaceAllUsesWith.
12744 const SDValue *To,
12745 unsigned Num){
12746 // Handle the simple, trivial case efficiently.
12747 if (Num == 1)
12748 return ReplaceAllUsesOfValueWith(*From, *To);
12749
12750 transferDbgValues(*From, *To);
12751 copyExtraInfo(From->getNode(), To->getNode());
12752
12753 // Read up all the uses and make records of them. This helps
12754 // processing new uses that are introduced during the
12755 // replacement process.
12757 for (unsigned i = 0; i != Num; ++i) {
12758 unsigned FromResNo = From[i].getResNo();
12759 SDNode *FromNode = From[i].getNode();
12760 for (SDUse &Use : FromNode->uses()) {
12761 if (Use.getResNo() == FromResNo) {
12762 UseMemo Memo = {Use.getUser(), i, &Use};
12763 Uses.push_back(Memo);
12764 }
12765 }
12766 }
12767
12768 // Sort the uses, so that all the uses from a given User are together.
12770 RAUOVWUpdateListener Listener(*this, Uses);
12771
12772 for (unsigned UseIndex = 0, UseIndexEnd = Uses.size();
12773 UseIndex != UseIndexEnd; ) {
12774 // We know that this user uses some value of From. If it is the right
12775 // value, update it.
12776 SDNode *User = Uses[UseIndex].User;
12777 // If the node has been deleted by recursive CSE updates when updating
12778 // another node, then just skip this entry.
12779 if (User == nullptr) {
12780 ++UseIndex;
12781 continue;
12782 }
12783
12784 // This node is about to morph, remove its old self from the CSE maps.
12785 RemoveNodeFromCSEMaps(User);
12786
12787 // The Uses array is sorted, so all the uses for a given User
12788 // are next to each other in the list.
12789 // To help reduce the number of CSE recomputations, process all
12790 // the uses of this user that we can find this way.
12791 do {
12792 unsigned i = Uses[UseIndex].Index;
12793 SDUse &Use = *Uses[UseIndex].Use;
12794 ++UseIndex;
12795
12796 Use.set(To[i]);
12797 } while (UseIndex != UseIndexEnd && Uses[UseIndex].User == User);
12798
12799 // Now that we have modified User, add it back to the CSE maps. If it
12800 // already exists there, recursively merge the results together.
12801 AddModifiedNodeToCSEMaps(User);
12802 }
12803}
12804
12805/// AssignTopologicalOrder - Assign a unique node id for each node in the DAG
12806/// based on their topological order. It returns the maximum id and a vector
12807/// of the SDNodes* in assigned order by reference.
12809 unsigned DAGSize = 0;
12810
12811 // SortedPos tracks the progress of the algorithm. Nodes before it are
12812 // sorted, nodes after it are unsorted. When the algorithm completes
12813 // it is at the end of the list.
12814 allnodes_iterator SortedPos = allnodes_begin();
12815
12816 // Visit all the nodes. Move nodes with no operands to the front of
12817 // the list immediately. Annotate nodes that do have operands with their
12818 // operand count. Before we do this, the Node Id fields of the nodes
12819 // may contain arbitrary values. After, the Node Id fields for nodes
12820 // before SortedPos will contain the topological sort index, and the
12821 // Node Id fields for nodes At SortedPos and after will contain the
12822 // count of outstanding operands.
12824 checkForCycles(&N, this);
12825 unsigned Degree = N.getNumOperands();
12826 if (Degree == 0) {
12827 // A node with no uses, add it to the result array immediately.
12828 N.setNodeId(DAGSize++);
12829 allnodes_iterator Q(&N);
12830 if (Q != SortedPos)
12831 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(Q));
12832 assert(SortedPos != AllNodes.end() && "Overran node list");
12833 ++SortedPos;
12834 } else {
12835 // Temporarily use the Node Id as scratch space for the degree count.
12836 N.setNodeId(Degree);
12837 }
12838 }
12839
12840 // Visit all the nodes. As we iterate, move nodes into sorted order,
12841 // such that by the time the end is reached all nodes will be sorted.
12842 for (SDNode &Node : allnodes()) {
12843 SDNode *N = &Node;
12844 checkForCycles(N, this);
12845 // N is in sorted position, so all its uses have one less operand
12846 // that needs to be sorted.
12847 for (SDNode *P : N->users()) {
12848 unsigned Degree = P->getNodeId();
12849 assert(Degree != 0 && "Invalid node degree");
12850 --Degree;
12851 if (Degree == 0) {
12852 // All of P's operands are sorted, so P may sorted now.
12853 P->setNodeId(DAGSize++);
12854 if (P->getIterator() != SortedPos)
12855 SortedPos = AllNodes.insert(SortedPos, AllNodes.remove(P));
12856 assert(SortedPos != AllNodes.end() && "Overran node list");
12857 ++SortedPos;
12858 } else {
12859 // Update P's outstanding operand count.
12860 P->setNodeId(Degree);
12861 }
12862 }
12863 if (Node.getIterator() == SortedPos) {
12864#ifndef NDEBUG
12866 SDNode *S = &*++I;
12867 dbgs() << "Overran sorted position:\n";
12868 S->dumprFull(this); dbgs() << "\n";
12869 dbgs() << "Checking if this is due to cycles\n";
12870 checkForCycles(this, true);
12871#endif
12872 llvm_unreachable(nullptr);
12873 }
12874 }
12875
12876 assert(SortedPos == AllNodes.end() &&
12877 "Topological sort incomplete!");
12878 assert(AllNodes.front().getOpcode() == ISD::EntryToken &&
12879 "First node in topological sort is not the entry token!");
12880 assert(AllNodes.front().getNodeId() == 0 &&
12881 "First node in topological sort has non-zero id!");
12882 assert(AllNodes.front().getNumOperands() == 0 &&
12883 "First node in topological sort has operands!");
12884 assert(AllNodes.back().getNodeId() == (int)DAGSize-1 &&
12885 "Last node in topologic sort has unexpected id!");
12886 assert(AllNodes.back().use_empty() &&
12887 "Last node in topologic sort has users!");
12888 assert(DAGSize == allnodes_size() && "Node count mismatch!");
12889 return DAGSize;
12890}
12891
12893 SmallVectorImpl<const SDNode *> &SortedNodes) const {
12894 SortedNodes.clear();
12895 // Node -> remaining number of outstanding operands.
12896 DenseMap<const SDNode *, unsigned> RemainingOperands;
12897
12898 // Put nodes without any operands into SortedNodes first.
12899 for (const SDNode &N : allnodes()) {
12900 checkForCycles(&N, this);
12901 unsigned NumOperands = N.getNumOperands();
12902 if (NumOperands == 0)
12903 SortedNodes.push_back(&N);
12904 else
12905 // Record their total number of outstanding operands.
12906 RemainingOperands[&N] = NumOperands;
12907 }
12908
12909 // A node is pushed into SortedNodes when all of its operands (predecessors in
12910 // the graph) are also in SortedNodes.
12911 for (unsigned i = 0U; i < SortedNodes.size(); ++i) {
12912 const SDNode *N = SortedNodes[i];
12913 for (const SDNode *U : N->users()) {
12914 // HandleSDNode is never part of a DAG and therefore has no entry in
12915 // RemainingOperands.
12916 if (U->getOpcode() == ISD::HANDLENODE)
12917 continue;
12918 unsigned &NumRemOperands = RemainingOperands[U];
12919 assert(NumRemOperands && "Invalid number of remaining operands");
12920 --NumRemOperands;
12921 if (!NumRemOperands)
12922 SortedNodes.push_back(U);
12923 }
12924 }
12925
12926 assert(SortedNodes.size() == AllNodes.size() && "Node count mismatch");
12927 assert(SortedNodes.front()->getOpcode() == ISD::EntryToken &&
12928 "First node in topological sort is not the entry token");
12929 assert(SortedNodes.front()->getNumOperands() == 0 &&
12930 "First node in topological sort has operands");
12931}
12932
12933/// AddDbgValue - Add a dbg_value SDNode. If SD is non-null that means the
12934/// value is produced by SD.
12935void SelectionDAG::AddDbgValue(SDDbgValue *DB, bool isParameter) {
12936 for (SDNode *SD : DB->getSDNodes()) {
12937 if (!SD)
12938 continue;
12939 assert(DbgInfo->getSDDbgValues(SD).empty() || SD->getHasDebugValue());
12940 SD->setHasDebugValue(true);
12941 }
12942 DbgInfo->add(DB, isParameter);
12943}
12944
12945void SelectionDAG::AddDbgLabel(SDDbgLabel *DB) { DbgInfo->add(DB); }
12946
12948 SDValue NewMemOpChain) {
12949 assert(isa<MemSDNode>(NewMemOpChain) && "Expected a memop node");
12950 assert(NewMemOpChain.getValueType() == MVT::Other && "Expected a token VT");
12951 // The new memory operation must have the same position as the old load in
12952 // terms of memory dependency. Create a TokenFactor for the old load and new
12953 // memory operation and update uses of the old load's output chain to use that
12954 // TokenFactor.
12955 if (OldChain == NewMemOpChain || OldChain.use_empty())
12956 return NewMemOpChain;
12957
12958 SDValue TokenFactor = getNode(ISD::TokenFactor, SDLoc(OldChain), MVT::Other,
12959 OldChain, NewMemOpChain);
12960 ReplaceAllUsesOfValueWith(OldChain, TokenFactor);
12961 UpdateNodeOperands(TokenFactor.getNode(), OldChain, NewMemOpChain);
12962 return TokenFactor;
12963}
12964
12966 SDValue NewMemOp) {
12967 assert(isa<MemSDNode>(NewMemOp.getNode()) && "Expected a memop node");
12968 SDValue OldChain = SDValue(OldLoad, 1);
12969 SDValue NewMemOpChain = NewMemOp.getValue(1);
12970 return makeEquivalentMemoryOrdering(OldChain, NewMemOpChain);
12971}
12972
12974 Function **OutFunction) {
12975 assert(isa<ExternalSymbolSDNode>(Op) && "Node should be an ExternalSymbol");
12976
12977 auto *Symbol = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12978 auto *Module = MF->getFunction().getParent();
12979 auto *Function = Module->getFunction(Symbol);
12980
12981 if (OutFunction != nullptr)
12982 *OutFunction = Function;
12983
12984 if (Function != nullptr) {
12985 auto PtrTy = TLI->getPointerTy(getDataLayout(), Function->getAddressSpace());
12986 return getGlobalAddress(Function, SDLoc(Op), PtrTy);
12987 }
12988
12989 std::string ErrorStr;
12990 raw_string_ostream ErrorFormatter(ErrorStr);
12991 ErrorFormatter << "Undefined external symbol ";
12992 ErrorFormatter << '"' << Symbol << '"';
12993 report_fatal_error(Twine(ErrorStr));
12994}
12995
12996//===----------------------------------------------------------------------===//
12997// SDNode Class
12998//===----------------------------------------------------------------------===//
12999
13002 return Const != nullptr && Const->isZero();
13003}
13004
13006 return V.isUndef() || isNullConstant(V);
13007}
13008
13011 return Const != nullptr && Const->isZero() && !Const->isNegative();
13012}
13013
13016 return Const != nullptr && Const->isAllOnes();
13017}
13018
13021 return Const != nullptr && Const->isOne();
13022}
13023
13026 return Const != nullptr && Const->isMinSignedValue();
13027}
13028
13029bool llvm::isNeutralConstant(unsigned Opcode, SDNodeFlags Flags, SDValue V,
13030 unsigned OperandNo) {
13031 // NOTE: The cases should match with IR's ConstantExpr::getBinOpIdentity().
13032 // TODO: Target-specific opcodes could be added.
13033 if (auto *ConstV = isConstOrConstSplat(V, /*AllowUndefs*/ false,
13034 /*AllowTruncation*/ true)) {
13035 APInt Const = ConstV->getAPIntValue().trunc(V.getScalarValueSizeInBits());
13036 switch (Opcode) {
13037 case ISD::ADD:
13038 case ISD::OR:
13039 case ISD::XOR:
13040 case ISD::UMAX:
13041 return Const.isZero();
13042 case ISD::MUL:
13043 return Const.isOne();
13044 case ISD::AND:
13045 case ISD::UMIN:
13046 return Const.isAllOnes();
13047 case ISD::SMAX:
13048 return Const.isMinSignedValue();
13049 case ISD::SMIN:
13050 return Const.isMaxSignedValue();
13051 case ISD::SUB:
13052 case ISD::SHL:
13053 case ISD::SRA:
13054 case ISD::SRL:
13055 return OperandNo == 1 && Const.isZero();
13056 case ISD::UDIV:
13057 case ISD::SDIV:
13058 return OperandNo == 1 && Const.isOne();
13059 }
13060 } else if (auto *ConstFP = isConstOrConstSplatFP(V)) {
13061 switch (Opcode) {
13062 case ISD::FADD:
13063 return ConstFP->isZero() &&
13064 (Flags.hasNoSignedZeros() || ConstFP->isNegative());
13065 case ISD::FSUB:
13066 return OperandNo == 1 && ConstFP->isZero() &&
13067 (Flags.hasNoSignedZeros() || !ConstFP->isNegative());
13068 case ISD::FMUL:
13069 return ConstFP->isExactlyValue(1.0);
13070 case ISD::FDIV:
13071 return OperandNo == 1 && ConstFP->isExactlyValue(1.0);
13072 case ISD::FMINNUM:
13073 case ISD::FMAXNUM: {
13074 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
13075 EVT VT = V.getValueType();
13076 const fltSemantics &Semantics = VT.getFltSemantics();
13077 APFloat NeutralAF = !Flags.hasNoNaNs()
13078 ? APFloat::getQNaN(Semantics)
13079 : !Flags.hasNoInfs()
13080 ? APFloat::getInf(Semantics)
13081 : APFloat::getLargest(Semantics);
13082 if (Opcode == ISD::FMAXNUM)
13083 NeutralAF.changeSign();
13084
13085 return ConstFP->isExactlyValue(NeutralAF);
13086 }
13087 }
13088 }
13089 return false;
13090}
13091
13093 while (V.getOpcode() == ISD::BITCAST)
13094 V = V.getOperand(0);
13095 return V;
13096}
13097
13099 while (V.getOpcode() == ISD::BITCAST && V.getOperand(0).hasOneUse())
13100 V = V.getOperand(0);
13101 return V;
13102}
13103
13105 while (V.getOpcode() == ISD::EXTRACT_SUBVECTOR)
13106 V = V.getOperand(0);
13107 return V;
13108}
13109
13111 while (V.getOpcode() == ISD::INSERT_VECTOR_ELT) {
13112 SDValue InVec = V.getOperand(0);
13113 SDValue EltNo = V.getOperand(2);
13114 EVT VT = InVec.getValueType();
13115 auto *IndexC = dyn_cast<ConstantSDNode>(EltNo);
13116 if (IndexC && VT.isFixedLengthVector() &&
13117 IndexC->getAPIntValue().ult(VT.getVectorNumElements()) &&
13118 !DemandedElts[IndexC->getZExtValue()]) {
13119 V = InVec;
13120 continue;
13121 }
13122 break;
13123 }
13124 return V;
13125}
13126
13128 while (V.getOpcode() == ISD::TRUNCATE)
13129 V = V.getOperand(0);
13130 return V;
13131}
13132
13133bool llvm::isBitwiseNot(SDValue V, bool AllowUndefs) {
13134 if (V.getOpcode() != ISD::XOR)
13135 return false;
13136 V = peekThroughBitcasts(V.getOperand(1));
13137 unsigned NumBits = V.getScalarValueSizeInBits();
13138 ConstantSDNode *C =
13139 isConstOrConstSplat(V, AllowUndefs, /*AllowTruncation*/ true);
13140 return C && (C->getAPIntValue().countr_one() >= NumBits);
13141}
13142
13144 bool AllowTruncation) {
13145 EVT VT = N.getValueType();
13146 APInt DemandedElts = VT.isFixedLengthVector()
13148 : APInt(1, 1);
13149 return isConstOrConstSplat(N, DemandedElts, AllowUndefs, AllowTruncation);
13150}
13151
13153 bool AllowUndefs,
13154 bool AllowTruncation) {
13156 return CN;
13157
13158 // SplatVectors can truncate their operands. Ignore that case here unless
13159 // AllowTruncation is set.
13160 if (N->getOpcode() == ISD::SPLAT_VECTOR) {
13161 EVT VecEltVT = N->getValueType(0).getVectorElementType();
13162 if (auto *CN = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
13163 EVT CVT = CN->getValueType(0);
13164 assert(CVT.bitsGE(VecEltVT) && "Illegal splat_vector element extension");
13165 if (AllowTruncation || CVT == VecEltVT)
13166 return CN;
13167 }
13168 }
13169
13171 BitVector UndefElements;
13172 ConstantSDNode *CN = BV->getConstantSplatNode(DemandedElts, &UndefElements);
13173
13174 // BuildVectors can truncate their operands. Ignore that case here unless
13175 // AllowTruncation is set.
13176 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13177 if (CN && (UndefElements.none() || AllowUndefs)) {
13178 EVT CVT = CN->getValueType(0);
13179 EVT NSVT = N.getValueType().getScalarType();
13180 assert(CVT.bitsGE(NSVT) && "Illegal build vector element extension");
13181 if (AllowTruncation || (CVT == NSVT))
13182 return CN;
13183 }
13184 }
13185
13186 return nullptr;
13187}
13188
13190 EVT VT = N.getValueType();
13191 APInt DemandedElts = VT.isFixedLengthVector()
13193 : APInt(1, 1);
13194 return isConstOrConstSplatFP(N, DemandedElts, AllowUndefs);
13195}
13196
13198 const APInt &DemandedElts,
13199 bool AllowUndefs) {
13201 return CN;
13202
13204 BitVector UndefElements;
13205 ConstantFPSDNode *CN =
13206 BV->getConstantFPSplatNode(DemandedElts, &UndefElements);
13207 // TODO: Look into whether we should allow UndefElements in non-DemandedElts
13208 if (CN && (UndefElements.none() || AllowUndefs))
13209 return CN;
13210 }
13211
13212 if (N.getOpcode() == ISD::SPLAT_VECTOR)
13213 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
13214 return CN;
13215
13216 return nullptr;
13217}
13218
13219bool llvm::isNullOrNullSplat(SDValue N, bool AllowUndefs) {
13220 // TODO: may want to use peekThroughBitcast() here.
13221 ConstantSDNode *C =
13222 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation=*/true);
13223 return C && C->isZero();
13224}
13225
13226bool llvm::isOneOrOneSplat(SDValue N, bool AllowUndefs) {
13227 ConstantSDNode *C =
13228 isConstOrConstSplat(N, AllowUndefs, /*AllowTruncation*/ true);
13229 return C && C->isOne();
13230}
13231
13232bool llvm::isOneOrOneSplatFP(SDValue N, bool AllowUndefs) {
13233 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13234 return C && C->isExactlyValue(1.0);
13235}
13236
13237bool llvm::isAllOnesOrAllOnesSplat(SDValue N, bool AllowUndefs) {
13239 unsigned BitWidth = N.getScalarValueSizeInBits();
13240 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13241 return C && C->isAllOnes() && C->getValueSizeInBits(0) == BitWidth;
13242}
13243
13244bool llvm::isOnesOrOnesSplat(SDValue N, bool AllowUndefs) {
13245 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs);
13246 return C && APInt::isSameValue(C->getAPIntValue(),
13247 APInt(C->getAPIntValue().getBitWidth(), 1));
13248}
13249
13250bool llvm::isZeroOrZeroSplat(SDValue N, bool AllowUndefs) {
13252 ConstantSDNode *C = isConstOrConstSplat(N, AllowUndefs, true);
13253 return C && C->isZero();
13254}
13255
13256bool llvm::isZeroOrZeroSplatFP(SDValue N, bool AllowUndefs) {
13257 ConstantFPSDNode *C = isConstOrConstSplatFP(N, AllowUndefs);
13258 return C && C->isZero();
13259}
13260
13264
13265MemSDNode::MemSDNode(unsigned Opc, unsigned Order, const DebugLoc &dl,
13266 SDVTList VTs, EVT memvt, MachineMemOperand *mmo)
13267 : SDNode(Opc, Order, dl, VTs), MemoryVT(memvt), MMO(mmo) {
13268 MemSDNodeBits.IsVolatile = MMO->isVolatile();
13269 MemSDNodeBits.IsNonTemporal = MMO->isNonTemporal();
13270 MemSDNodeBits.IsDereferenceable = MMO->isDereferenceable();
13271 MemSDNodeBits.IsInvariant = MMO->isInvariant();
13272
13273 // We check here that the size of the memory operand fits within the size of
13274 // the MMO. This is because the MMO might indicate only a possible address
13275 // range instead of specifying the affected memory addresses precisely.
13276 assert(
13277 (!MMO->getType().isValid() ||
13278 TypeSize::isKnownLE(memvt.getStoreSize(), MMO->getSize().getValue())) &&
13279 "Size mismatch!");
13280}
13281
13282/// Profile - Gather unique data for the node.
13283///
13285 AddNodeIDNode(ID, this);
13286}
13287
13288namespace {
13289
13290 struct EVTArray {
13291 std::vector<EVT> VTs;
13292
13293 EVTArray() {
13294 VTs.reserve(MVT::VALUETYPE_SIZE);
13295 for (unsigned i = 0; i < MVT::VALUETYPE_SIZE; ++i)
13296 VTs.push_back(MVT((MVT::SimpleValueType)i));
13297 }
13298 };
13299
13300} // end anonymous namespace
13301
13302/// getValueTypeList - Return a pointer to the specified value type.
13303///
13304const EVT *SDNode::getValueTypeList(MVT VT) {
13305 static EVTArray SimpleVTArray;
13306
13307 assert(VT < MVT::VALUETYPE_SIZE && "Value type out of range!");
13308 return &SimpleVTArray.VTs[VT.SimpleTy];
13309}
13310
13311/// hasAnyUseOfValue - Return true if there are any use of the indicated
13312/// value. This method ignores uses of other values defined by this operation.
13313bool SDNode::hasAnyUseOfValue(unsigned Value) const {
13314 assert(Value < getNumValues() && "Bad value!");
13315
13316 for (SDUse &U : uses())
13317 if (U.getResNo() == Value)
13318 return true;
13319
13320 return false;
13321}
13322
13323/// isOnlyUserOf - Return true if this node is the only use of N.
13324bool SDNode::isOnlyUserOf(const SDNode *N) const {
13325 bool Seen = false;
13326 for (const SDNode *User : N->users()) {
13327 if (User == this)
13328 Seen = true;
13329 else
13330 return false;
13331 }
13332
13333 return Seen;
13334}
13335
13336/// Return true if the only users of N are contained in Nodes.
13338 bool Seen = false;
13339 for (const SDNode *User : N->users()) {
13340 if (llvm::is_contained(Nodes, User))
13341 Seen = true;
13342 else
13343 return false;
13344 }
13345
13346 return Seen;
13347}
13348
13349/// Return true if the referenced return value is an operand of N.
13350bool SDValue::isOperandOf(const SDNode *N) const {
13351 return is_contained(N->op_values(), *this);
13352}
13353
13354bool SDNode::isOperandOf(const SDNode *N) const {
13355 return any_of(N->op_values(),
13356 [this](SDValue Op) { return this == Op.getNode(); });
13357}
13358
13359/// reachesChainWithoutSideEffects - Return true if this operand (which must
13360/// be a chain) reaches the specified operand without crossing any
13361/// side-effecting instructions on any chain path. In practice, this looks
13362/// through token factors and non-volatile loads. In order to remain efficient,
13363/// this only looks a couple of nodes in, it does not do an exhaustive search.
13364///
13365/// Note that we only need to examine chains when we're searching for
13366/// side-effects; SelectionDAG requires that all side-effects are represented
13367/// by chains, even if another operand would force a specific ordering. This
13368/// constraint is necessary to allow transformations like splitting loads.
13370 unsigned Depth) const {
13371 if (*this == Dest) return true;
13372
13373 // Don't search too deeply, we just want to be able to see through
13374 // TokenFactor's etc.
13375 if (Depth == 0) return false;
13376
13377 // If this is a token factor, all inputs to the TF happen in parallel.
13378 if (getOpcode() == ISD::TokenFactor) {
13379 // First, try a shallow search.
13380 if (is_contained((*this)->ops(), Dest)) {
13381 // We found the chain we want as an operand of this TokenFactor.
13382 // Essentially, we reach the chain without side-effects if we could
13383 // serialize the TokenFactor into a simple chain of operations with
13384 // Dest as the last operation. This is automatically true if the
13385 // chain has one use: there are no other ordering constraints.
13386 // If the chain has more than one use, we give up: some other
13387 // use of Dest might force a side-effect between Dest and the current
13388 // node.
13389 if (Dest.hasOneUse())
13390 return true;
13391 }
13392 // Next, try a deep search: check whether every operand of the TokenFactor
13393 // reaches Dest.
13394 return llvm::all_of((*this)->ops(), [=](SDValue Op) {
13395 return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
13396 });
13397 }
13398
13399 // Loads don't have side effects, look through them.
13400 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(*this)) {
13401 if (Ld->isUnordered())
13402 return Ld->getChain().reachesChainWithoutSideEffects(Dest, Depth-1);
13403 }
13404 return false;
13405}
13406
13407bool SDNode::hasPredecessor(const SDNode *N) const {
13410 Worklist.push_back(this);
13411 return hasPredecessorHelper(N, Visited, Worklist);
13412}
13413
13415 this->Flags &= Flags;
13416}
13417
13418SDValue
13420 ArrayRef<ISD::NodeType> CandidateBinOps,
13421 bool AllowPartials) {
13422 // The pattern must end in an extract from index 0.
13423 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
13424 !isNullConstant(Extract->getOperand(1)))
13425 return SDValue();
13426
13427 // Match against one of the candidate binary ops.
13428 SDValue Op = Extract->getOperand(0);
13429 if (llvm::none_of(CandidateBinOps, [Op](ISD::NodeType BinOp) {
13430 return Op.getOpcode() == unsigned(BinOp);
13431 }))
13432 return SDValue();
13433
13434 // Floating-point reductions may require relaxed constraints on the final step
13435 // of the reduction because they may reorder intermediate operations.
13436 unsigned CandidateBinOp = Op.getOpcode();
13437 if (Op.getValueType().isFloatingPoint()) {
13438 SDNodeFlags Flags = Op->getFlags();
13439 switch (CandidateBinOp) {
13440 case ISD::FADD:
13441 if (!Flags.hasNoSignedZeros() || !Flags.hasAllowReassociation())
13442 return SDValue();
13443 break;
13444 default:
13445 llvm_unreachable("Unhandled FP opcode for binop reduction");
13446 }
13447 }
13448
13449 // Matching failed - attempt to see if we did enough stages that a partial
13450 // reduction from a subvector is possible.
13451 auto PartialReduction = [&](SDValue Op, unsigned NumSubElts) {
13452 if (!AllowPartials || !Op)
13453 return SDValue();
13454 EVT OpVT = Op.getValueType();
13455 EVT OpSVT = OpVT.getScalarType();
13456 EVT SubVT = EVT::getVectorVT(*getContext(), OpSVT, NumSubElts);
13457 if (!TLI->isExtractSubvectorCheap(SubVT, OpVT, 0))
13458 return SDValue();
13459 BinOp = (ISD::NodeType)CandidateBinOp;
13460 return getExtractSubvector(SDLoc(Op), SubVT, Op, 0);
13461 };
13462
13463 // At each stage, we're looking for something that looks like:
13464 // %s = shufflevector <8 x i32> %op, <8 x i32> undef,
13465 // <8 x i32> <i32 2, i32 3, i32 undef, i32 undef,
13466 // i32 undef, i32 undef, i32 undef, i32 undef>
13467 // %a = binop <8 x i32> %op, %s
13468 // Where the mask changes according to the stage. E.g. for a 3-stage pyramid,
13469 // we expect something like:
13470 // <4,5,6,7,u,u,u,u>
13471 // <2,3,u,u,u,u,u,u>
13472 // <1,u,u,u,u,u,u,u>
13473 // While a partial reduction match would be:
13474 // <2,3,u,u,u,u,u,u>
13475 // <1,u,u,u,u,u,u,u>
13476 unsigned Stages = Log2_32(Op.getValueType().getVectorNumElements());
13477 SDValue PrevOp;
13478 for (unsigned i = 0; i < Stages; ++i) {
13479 unsigned MaskEnd = (1 << i);
13480
13481 if (Op.getOpcode() != CandidateBinOp)
13482 return PartialReduction(PrevOp, MaskEnd);
13483
13484 SDValue Op0 = Op.getOperand(0);
13485 SDValue Op1 = Op.getOperand(1);
13486
13488 if (Shuffle) {
13489 Op = Op1;
13490 } else {
13491 Shuffle = dyn_cast<ShuffleVectorSDNode>(Op1);
13492 Op = Op0;
13493 }
13494
13495 // The first operand of the shuffle should be the same as the other operand
13496 // of the binop.
13497 if (!Shuffle || Shuffle->getOperand(0) != Op)
13498 return PartialReduction(PrevOp, MaskEnd);
13499
13500 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
13501 for (int Index = 0; Index < (int)MaskEnd; ++Index)
13502 if (Shuffle->getMaskElt(Index) != (int)(MaskEnd + Index))
13503 return PartialReduction(PrevOp, MaskEnd);
13504
13505 PrevOp = Op;
13506 }
13507
13508 // Handle subvector reductions, which tend to appear after the shuffle
13509 // reduction stages.
13510 while (Op.getOpcode() == CandidateBinOp) {
13511 unsigned NumElts = Op.getValueType().getVectorNumElements();
13512 SDValue Op0 = Op.getOperand(0);
13513 SDValue Op1 = Op.getOperand(1);
13514 if (Op0.getOpcode() != ISD::EXTRACT_SUBVECTOR ||
13516 Op0.getOperand(0) != Op1.getOperand(0))
13517 break;
13518 SDValue Src = Op0.getOperand(0);
13519 unsigned NumSrcElts = Src.getValueType().getVectorNumElements();
13520 if (NumSrcElts != (2 * NumElts))
13521 break;
13522 if (!(Op0.getConstantOperandAPInt(1) == 0 &&
13523 Op1.getConstantOperandAPInt(1) == NumElts) &&
13524 !(Op1.getConstantOperandAPInt(1) == 0 &&
13525 Op0.getConstantOperandAPInt(1) == NumElts))
13526 break;
13527 Op = Src;
13528 }
13529
13530 BinOp = (ISD::NodeType)CandidateBinOp;
13531 return Op;
13532}
13533
13535 EVT VT = N->getValueType(0);
13536 EVT EltVT = VT.getVectorElementType();
13537 unsigned NE = VT.getVectorNumElements();
13538
13539 SDLoc dl(N);
13540
13541 // If ResNE is 0, fully unroll the vector op.
13542 if (ResNE == 0)
13543 ResNE = NE;
13544 else if (NE > ResNE)
13545 NE = ResNE;
13546
13547 if (N->getNumValues() == 2) {
13548 SmallVector<SDValue, 8> Scalars0, Scalars1;
13549 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13550 EVT VT1 = N->getValueType(1);
13551 EVT EltVT1 = VT1.getVectorElementType();
13552
13553 unsigned i;
13554 for (i = 0; i != NE; ++i) {
13555 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13556 SDValue Operand = N->getOperand(j);
13557 EVT OperandVT = Operand.getValueType();
13558
13559 // A vector operand; extract a single element.
13560 EVT OperandEltVT = OperandVT.getVectorElementType();
13561 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13562 }
13563
13564 SDValue EltOp = getNode(N->getOpcode(), dl, {EltVT, EltVT1}, Operands);
13565 Scalars0.push_back(EltOp);
13566 Scalars1.push_back(EltOp.getValue(1));
13567 }
13568
13569 for (; i < ResNE; ++i) {
13570 Scalars0.push_back(getUNDEF(EltVT));
13571 Scalars1.push_back(getUNDEF(EltVT1));
13572 }
13573
13574 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13575 EVT VecVT1 = EVT::getVectorVT(*getContext(), EltVT1, ResNE);
13576 SDValue Vec0 = getBuildVector(VecVT, dl, Scalars0);
13577 SDValue Vec1 = getBuildVector(VecVT1, dl, Scalars1);
13578 return getMergeValues({Vec0, Vec1}, dl);
13579 }
13580
13581 assert(N->getNumValues() == 1 &&
13582 "Can't unroll a vector with multiple results!");
13583
13585 SmallVector<SDValue, 4> Operands(N->getNumOperands());
13586
13587 unsigned i;
13588 for (i= 0; i != NE; ++i) {
13589 for (unsigned j = 0, e = N->getNumOperands(); j != e; ++j) {
13590 SDValue Operand = N->getOperand(j);
13591 EVT OperandVT = Operand.getValueType();
13592 if (OperandVT.isVector()) {
13593 // A vector operand; extract a single element.
13594 EVT OperandEltVT = OperandVT.getVectorElementType();
13595 Operands[j] = getExtractVectorElt(dl, OperandEltVT, Operand, i);
13596 } else {
13597 // A scalar operand; just use it as is.
13598 Operands[j] = Operand;
13599 }
13600 }
13601
13602 switch (N->getOpcode()) {
13603 default: {
13604 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands,
13605 N->getFlags()));
13606 break;
13607 }
13608 case ISD::VSELECT:
13609 Scalars.push_back(getNode(ISD::SELECT, dl, EltVT, Operands));
13610 break;
13611 case ISD::SHL:
13612 case ISD::SRA:
13613 case ISD::SRL:
13614 case ISD::ROTL:
13615 case ISD::ROTR:
13616 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT, Operands[0],
13617 getShiftAmountOperand(Operands[0].getValueType(),
13618 Operands[1])));
13619 break;
13621 EVT ExtVT = cast<VTSDNode>(Operands[1])->getVT().getVectorElementType();
13622 Scalars.push_back(getNode(N->getOpcode(), dl, EltVT,
13623 Operands[0],
13624 getValueType(ExtVT)));
13625 break;
13626 }
13627 case ISD::ADDRSPACECAST: {
13628 const auto *ASC = cast<AddrSpaceCastSDNode>(N);
13629 Scalars.push_back(getAddrSpaceCast(dl, EltVT, Operands[0],
13630 ASC->getSrcAddressSpace(),
13631 ASC->getDestAddressSpace()));
13632 break;
13633 }
13634 }
13635 }
13636
13637 for (; i < ResNE; ++i)
13638 Scalars.push_back(getUNDEF(EltVT));
13639
13640 EVT VecVT = EVT::getVectorVT(*getContext(), EltVT, ResNE);
13641 return getBuildVector(VecVT, dl, Scalars);
13642}
13643
13644std::pair<SDValue, SDValue> SelectionDAG::UnrollVectorOverflowOp(
13645 SDNode *N, unsigned ResNE) {
13646 unsigned Opcode = N->getOpcode();
13647 assert((Opcode == ISD::UADDO || Opcode == ISD::SADDO ||
13648 Opcode == ISD::USUBO || Opcode == ISD::SSUBO ||
13649 Opcode == ISD::UMULO || Opcode == ISD::SMULO) &&
13650 "Expected an overflow opcode");
13651
13652 EVT ResVT = N->getValueType(0);
13653 EVT OvVT = N->getValueType(1);
13654 EVT ResEltVT = ResVT.getVectorElementType();
13655 EVT OvEltVT = OvVT.getVectorElementType();
13656 SDLoc dl(N);
13657
13658 // If ResNE is 0, fully unroll the vector op.
13659 unsigned NE = ResVT.getVectorNumElements();
13660 if (ResNE == 0)
13661 ResNE = NE;
13662 else if (NE > ResNE)
13663 NE = ResNE;
13664
13665 SmallVector<SDValue, 8> LHSScalars;
13666 SmallVector<SDValue, 8> RHSScalars;
13667 ExtractVectorElements(N->getOperand(0), LHSScalars, 0, NE);
13668 ExtractVectorElements(N->getOperand(1), RHSScalars, 0, NE);
13669
13670 EVT SVT = TLI->getSetCCResultType(getDataLayout(), *getContext(), ResEltVT);
13671 SDVTList VTs = getVTList(ResEltVT, SVT);
13672 SmallVector<SDValue, 8> ResScalars;
13673 SmallVector<SDValue, 8> OvScalars;
13674 for (unsigned i = 0; i < NE; ++i) {
13675 SDValue Res = getNode(Opcode, dl, VTs, LHSScalars[i], RHSScalars[i]);
13676 SDValue Ov =
13677 getSelect(dl, OvEltVT, Res.getValue(1),
13678 getBoolConstant(true, dl, OvEltVT, ResVT),
13679 getConstant(0, dl, OvEltVT));
13680
13681 ResScalars.push_back(Res);
13682 OvScalars.push_back(Ov);
13683 }
13684
13685 ResScalars.append(ResNE - NE, getUNDEF(ResEltVT));
13686 OvScalars.append(ResNE - NE, getUNDEF(OvEltVT));
13687
13688 EVT NewResVT = EVT::getVectorVT(*getContext(), ResEltVT, ResNE);
13689 EVT NewOvVT = EVT::getVectorVT(*getContext(), OvEltVT, ResNE);
13690 return std::make_pair(getBuildVector(NewResVT, dl, ResScalars),
13691 getBuildVector(NewOvVT, dl, OvScalars));
13692}
13693
13696 unsigned Bytes,
13697 int Dist) const {
13698 if (LD->isVolatile() || Base->isVolatile())
13699 return false;
13700 // TODO: probably too restrictive for atomics, revisit
13701 if (!LD->isSimple())
13702 return false;
13703 if (LD->isIndexed() || Base->isIndexed())
13704 return false;
13705 if (LD->getChain() != Base->getChain())
13706 return false;
13707 EVT VT = LD->getMemoryVT();
13708 if (VT.getSizeInBits() / 8 != Bytes)
13709 return false;
13710
13711 auto BaseLocDecomp = BaseIndexOffset::match(Base, *this);
13712 auto LocDecomp = BaseIndexOffset::match(LD, *this);
13713
13714 int64_t Offset = 0;
13715 if (BaseLocDecomp.equalBaseIndex(LocDecomp, *this, Offset))
13716 return (Dist * (int64_t)Bytes == Offset);
13717 return false;
13718}
13719
13720/// InferPtrAlignment - Infer alignment of a load / store address. Return
13721/// std::nullopt if it cannot be inferred.
13723 // If this is a GlobalAddress + cst, return the alignment.
13724 const GlobalValue *GV = nullptr;
13725 int64_t GVOffset = 0;
13726 if (TLI->isGAPlusOffset(Ptr.getNode(), GV, GVOffset)) {
13727 unsigned PtrWidth = getDataLayout().getPointerTypeSizeInBits(GV->getType());
13728 KnownBits Known(PtrWidth);
13730 unsigned AlignBits = Known.countMinTrailingZeros();
13731 if (AlignBits)
13732 return commonAlignment(Align(1ull << std::min(31U, AlignBits)), GVOffset);
13733 }
13734
13735 // If this is a direct reference to a stack slot, use information about the
13736 // stack slot's alignment.
13737 int FrameIdx = INT_MIN;
13738 int64_t FrameOffset = 0;
13740 FrameIdx = FI->getIndex();
13741 } else if (isBaseWithConstantOffset(Ptr) &&
13743 // Handle FI+Cst
13744 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
13745 FrameOffset = Ptr.getConstantOperandVal(1);
13746 }
13747
13748 if (FrameIdx != INT_MIN) {
13750 return commonAlignment(MFI.getObjectAlign(FrameIdx), FrameOffset);
13751 }
13752
13753 return std::nullopt;
13754}
13755
13756/// Split the scalar node with EXTRACT_ELEMENT using the provided
13757/// VTs and return the low/high part.
13758std::pair<SDValue, SDValue> SelectionDAG::SplitScalar(const SDValue &N,
13759 const SDLoc &DL,
13760 const EVT &LoVT,
13761 const EVT &HiVT) {
13762 assert(!LoVT.isVector() && !HiVT.isVector() && !N.getValueType().isVector() &&
13763 "Split node must be a scalar type");
13764 SDValue Lo =
13766 SDValue Hi =
13768 return std::make_pair(Lo, Hi);
13769}
13770
13771/// GetSplitDestVTs - Compute the VTs needed for the low/hi parts of a type
13772/// which is split (or expanded) into two not necessarily identical pieces.
13773std::pair<EVT, EVT> SelectionDAG::GetSplitDestVTs(const EVT &VT) const {
13774 // Currently all types are split in half.
13775 EVT LoVT, HiVT;
13776 if (!VT.isVector())
13777 LoVT = HiVT = TLI->getTypeToTransformTo(*getContext(), VT);
13778 else
13779 LoVT = HiVT = VT.getHalfNumVectorElementsVT(*getContext());
13780
13781 return std::make_pair(LoVT, HiVT);
13782}
13783
13784/// GetDependentSplitDestVTs - Compute the VTs needed for the low/hi parts of a
13785/// type, dependent on an enveloping VT that has been split into two identical
13786/// pieces. Sets the HiIsEmpty flag when hi type has zero storage size.
13787std::pair<EVT, EVT>
13789 bool *HiIsEmpty) const {
13790 EVT EltTp = VT.getVectorElementType();
13791 // Examples:
13792 // custom VL=8 with enveloping VL=8/8 yields 8/0 (hi empty)
13793 // custom VL=9 with enveloping VL=8/8 yields 8/1
13794 // custom VL=10 with enveloping VL=8/8 yields 8/2
13795 // etc.
13796 ElementCount VTNumElts = VT.getVectorElementCount();
13797 ElementCount EnvNumElts = EnvVT.getVectorElementCount();
13798 assert(VTNumElts.isScalable() == EnvNumElts.isScalable() &&
13799 "Mixing fixed width and scalable vectors when enveloping a type");
13800 EVT LoVT, HiVT;
13801 if (VTNumElts.getKnownMinValue() > EnvNumElts.getKnownMinValue()) {
13802 LoVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13803 HiVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts - EnvNumElts);
13804 *HiIsEmpty = false;
13805 } else {
13806 // Flag that hi type has zero storage size, but return split envelop type
13807 // (this would be easier if vector types with zero elements were allowed).
13808 LoVT = EVT::getVectorVT(*getContext(), EltTp, VTNumElts);
13809 HiVT = EVT::getVectorVT(*getContext(), EltTp, EnvNumElts);
13810 *HiIsEmpty = true;
13811 }
13812 return std::make_pair(LoVT, HiVT);
13813}
13814
13815/// SplitVector - Split the vector with EXTRACT_SUBVECTOR and return the
13816/// low/high part.
13817std::pair<SDValue, SDValue>
13818SelectionDAG::SplitVector(const SDValue &N, const SDLoc &DL, const EVT &LoVT,
13819 const EVT &HiVT) {
13820 assert(LoVT.isScalableVector() == HiVT.isScalableVector() &&
13821 LoVT.isScalableVector() == N.getValueType().isScalableVector() &&
13822 "Splitting vector with an invalid mixture of fixed and scalable "
13823 "vector types");
13825 N.getValueType().getVectorMinNumElements() &&
13826 "More vector elements requested than available!");
13827 SDValue Lo, Hi;
13828 Lo = getExtractSubvector(DL, LoVT, N, 0);
13829 // For scalable vectors it is safe to use LoVT.getVectorMinNumElements()
13830 // (rather than having to use ElementCount), because EXTRACT_SUBVECTOR scales
13831 // IDX with the runtime scaling factor of the result vector type. For
13832 // fixed-width result vectors, that runtime scaling factor is 1.
13835 return std::make_pair(Lo, Hi);
13836}
13837
13838std::pair<SDValue, SDValue> SelectionDAG::SplitEVL(SDValue N, EVT VecVT,
13839 const SDLoc &DL) {
13840 // Split the vector length parameter.
13841 // %evl -> umin(%evl, %halfnumelts) and usubsat(%evl - %halfnumelts).
13842 EVT VT = N.getValueType();
13844 "Expecting the mask to be an evenly-sized vector");
13845 SDValue HalfNumElts = getElementCount(
13847 SDValue Lo = getNode(ISD::UMIN, DL, VT, N, HalfNumElts);
13848 SDValue Hi = getNode(ISD::USUBSAT, DL, VT, N, HalfNumElts);
13849 return std::make_pair(Lo, Hi);
13850}
13851
13852/// Widen the vector up to the next power of two using INSERT_SUBVECTOR.
13854 EVT VT = N.getValueType();
13857 return getInsertSubvector(DL, getUNDEF(WideVT), N, 0);
13858}
13859
13862 unsigned Start, unsigned Count,
13863 EVT EltVT) {
13864 EVT VT = Op.getValueType();
13865 if (Count == 0)
13867 if (EltVT == EVT())
13868 EltVT = VT.getVectorElementType();
13869 SDLoc SL(Op);
13870 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
13871 Args.push_back(getExtractVectorElt(SL, EltVT, Op, i));
13872 }
13873}
13874
13875// getAddressSpace - Return the address space this GlobalAddress belongs to.
13877 return getGlobal()->getType()->getAddressSpace();
13878}
13879
13882 return Val.MachineCPVal->getType();
13883 return Val.ConstVal->getType();
13884}
13885
13886bool BuildVectorSDNode::isConstantSplat(APInt &SplatValue, APInt &SplatUndef,
13887 unsigned &SplatBitSize,
13888 bool &HasAnyUndefs,
13889 unsigned MinSplatBits,
13890 bool IsBigEndian) const {
13891 EVT VT = getValueType(0);
13892 assert(VT.isVector() && "Expected a vector type");
13893 unsigned VecWidth = VT.getSizeInBits();
13894 if (MinSplatBits > VecWidth)
13895 return false;
13896
13897 // FIXME: The widths are based on this node's type, but build vectors can
13898 // truncate their operands.
13899 SplatValue = APInt(VecWidth, 0);
13900 SplatUndef = APInt(VecWidth, 0);
13901
13902 // Get the bits. Bits with undefined values (when the corresponding element
13903 // of the vector is an ISD::UNDEF value) are set in SplatUndef and cleared
13904 // in SplatValue. If any of the values are not constant, give up and return
13905 // false.
13906 unsigned int NumOps = getNumOperands();
13907 assert(NumOps > 0 && "isConstantSplat has 0-size build vector");
13908 unsigned EltWidth = VT.getScalarSizeInBits();
13909
13910 for (unsigned j = 0; j < NumOps; ++j) {
13911 unsigned i = IsBigEndian ? NumOps - 1 - j : j;
13912 SDValue OpVal = getOperand(i);
13913 unsigned BitPos = j * EltWidth;
13914
13915 if (OpVal.isUndef())
13916 SplatUndef.setBits(BitPos, BitPos + EltWidth);
13917 else if (auto *CN = dyn_cast<ConstantSDNode>(OpVal))
13918 SplatValue.insertBits(CN->getAPIntValue().zextOrTrunc(EltWidth), BitPos);
13919 else if (auto *CN = dyn_cast<ConstantFPSDNode>(OpVal))
13920 SplatValue.insertBits(CN->getValueAPF().bitcastToAPInt(), BitPos);
13921 else
13922 return false;
13923 }
13924
13925 // The build_vector is all constants or undefs. Find the smallest element
13926 // size that splats the vector.
13927 HasAnyUndefs = (SplatUndef != 0);
13928
13929 // FIXME: This does not work for vectors with elements less than 8 bits.
13930 while (VecWidth > 8) {
13931 // If we can't split in half, stop here.
13932 if (VecWidth & 1)
13933 break;
13934
13935 unsigned HalfSize = VecWidth / 2;
13936 APInt HighValue = SplatValue.extractBits(HalfSize, HalfSize);
13937 APInt LowValue = SplatValue.extractBits(HalfSize, 0);
13938 APInt HighUndef = SplatUndef.extractBits(HalfSize, HalfSize);
13939 APInt LowUndef = SplatUndef.extractBits(HalfSize, 0);
13940
13941 // If the two halves do not match (ignoring undef bits), stop here.
13942 if ((HighValue & ~LowUndef) != (LowValue & ~HighUndef) ||
13943 MinSplatBits > HalfSize)
13944 break;
13945
13946 SplatValue = HighValue | LowValue;
13947 SplatUndef = HighUndef & LowUndef;
13948
13949 VecWidth = HalfSize;
13950 }
13951
13952 // FIXME: The loop above only tries to split in halves. But if the input
13953 // vector for example is <3 x i16> it wouldn't be able to detect a
13954 // SplatBitSize of 16. No idea if that is a design flaw currently limiting
13955 // optimizations. I guess that back in the days when this helper was created
13956 // vectors normally was power-of-2 sized.
13957
13958 SplatBitSize = VecWidth;
13959 return true;
13960}
13961
13963 BitVector *UndefElements) const {
13964 unsigned NumOps = getNumOperands();
13965 if (UndefElements) {
13966 UndefElements->clear();
13967 UndefElements->resize(NumOps);
13968 }
13969 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
13970 if (!DemandedElts)
13971 return SDValue();
13972 SDValue Splatted;
13973 for (unsigned i = 0; i != NumOps; ++i) {
13974 if (!DemandedElts[i])
13975 continue;
13976 SDValue Op = getOperand(i);
13977 if (Op.isUndef()) {
13978 if (UndefElements)
13979 (*UndefElements)[i] = true;
13980 } else if (!Splatted) {
13981 Splatted = Op;
13982 } else if (Splatted != Op) {
13983 return SDValue();
13984 }
13985 }
13986
13987 if (!Splatted) {
13988 unsigned FirstDemandedIdx = DemandedElts.countr_zero();
13989 assert(getOperand(FirstDemandedIdx).isUndef() &&
13990 "Can only have a splat without a constant for all undefs.");
13991 return getOperand(FirstDemandedIdx);
13992 }
13993
13994 return Splatted;
13995}
13996
13998 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
13999 return getSplatValue(DemandedElts, UndefElements);
14000}
14001
14003 SmallVectorImpl<SDValue> &Sequence,
14004 BitVector *UndefElements) const {
14005 unsigned NumOps = getNumOperands();
14006 Sequence.clear();
14007 if (UndefElements) {
14008 UndefElements->clear();
14009 UndefElements->resize(NumOps);
14010 }
14011 assert(NumOps == DemandedElts.getBitWidth() && "Unexpected vector size");
14012 if (!DemandedElts || NumOps < 2 || !isPowerOf2_32(NumOps))
14013 return false;
14014
14015 // Set the undefs even if we don't find a sequence (like getSplatValue).
14016 if (UndefElements)
14017 for (unsigned I = 0; I != NumOps; ++I)
14018 if (DemandedElts[I] && getOperand(I).isUndef())
14019 (*UndefElements)[I] = true;
14020
14021 // Iteratively widen the sequence length looking for repetitions.
14022 for (unsigned SeqLen = 1; SeqLen < NumOps; SeqLen *= 2) {
14023 Sequence.append(SeqLen, SDValue());
14024 for (unsigned I = 0; I != NumOps; ++I) {
14025 if (!DemandedElts[I])
14026 continue;
14027 SDValue &SeqOp = Sequence[I % SeqLen];
14029 if (Op.isUndef()) {
14030 if (!SeqOp)
14031 SeqOp = Op;
14032 continue;
14033 }
14034 if (SeqOp && !SeqOp.isUndef() && SeqOp != Op) {
14035 Sequence.clear();
14036 break;
14037 }
14038 SeqOp = Op;
14039 }
14040 if (!Sequence.empty())
14041 return true;
14042 }
14043
14044 assert(Sequence.empty() && "Failed to empty non-repeating sequence pattern");
14045 return false;
14046}
14047
14049 BitVector *UndefElements) const {
14050 APInt DemandedElts = APInt::getAllOnes(getNumOperands());
14051 return getRepeatedSequence(DemandedElts, Sequence, UndefElements);
14052}
14053
14056 BitVector *UndefElements) const {
14058 getSplatValue(DemandedElts, UndefElements));
14059}
14060
14063 return dyn_cast_or_null<ConstantSDNode>(getSplatValue(UndefElements));
14064}
14065
14068 BitVector *UndefElements) const {
14070 getSplatValue(DemandedElts, UndefElements));
14071}
14072
14077
14078int32_t
14080 uint32_t BitWidth) const {
14081 if (ConstantFPSDNode *CN =
14083 bool IsExact;
14084 APSInt IntVal(BitWidth);
14085 const APFloat &APF = CN->getValueAPF();
14086 if (APF.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact) !=
14087 APFloat::opOK ||
14088 !IsExact)
14089 return -1;
14090
14091 return IntVal.exactLogBase2();
14092 }
14093 return -1;
14094}
14095
14097 bool IsLittleEndian, unsigned DstEltSizeInBits,
14098 SmallVectorImpl<APInt> &RawBitElements, BitVector &UndefElements) const {
14099 // Early-out if this contains anything but Undef/Constant/ConstantFP.
14100 if (!isConstant())
14101 return false;
14102
14103 unsigned NumSrcOps = getNumOperands();
14104 unsigned SrcEltSizeInBits = getValueType(0).getScalarSizeInBits();
14105 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14106 "Invalid bitcast scale");
14107
14108 // Extract raw src bits.
14109 SmallVector<APInt> SrcBitElements(NumSrcOps,
14110 APInt::getZero(SrcEltSizeInBits));
14111 BitVector SrcUndeElements(NumSrcOps, false);
14112
14113 for (unsigned I = 0; I != NumSrcOps; ++I) {
14115 if (Op.isUndef()) {
14116 SrcUndeElements.set(I);
14117 continue;
14118 }
14119 auto *CInt = dyn_cast<ConstantSDNode>(Op);
14120 auto *CFP = dyn_cast<ConstantFPSDNode>(Op);
14121 assert((CInt || CFP) && "Unknown constant");
14122 SrcBitElements[I] = CInt ? CInt->getAPIntValue().trunc(SrcEltSizeInBits)
14123 : CFP->getValueAPF().bitcastToAPInt();
14124 }
14125
14126 // Recast to dst width.
14127 recastRawBits(IsLittleEndian, DstEltSizeInBits, RawBitElements,
14128 SrcBitElements, UndefElements, SrcUndeElements);
14129 return true;
14130}
14131
14132void BuildVectorSDNode::recastRawBits(bool IsLittleEndian,
14133 unsigned DstEltSizeInBits,
14134 SmallVectorImpl<APInt> &DstBitElements,
14135 ArrayRef<APInt> SrcBitElements,
14136 BitVector &DstUndefElements,
14137 const BitVector &SrcUndefElements) {
14138 unsigned NumSrcOps = SrcBitElements.size();
14139 unsigned SrcEltSizeInBits = SrcBitElements[0].getBitWidth();
14140 assert(((NumSrcOps * SrcEltSizeInBits) % DstEltSizeInBits) == 0 &&
14141 "Invalid bitcast scale");
14142 assert(NumSrcOps == SrcUndefElements.size() &&
14143 "Vector size mismatch");
14144
14145 unsigned NumDstOps = (NumSrcOps * SrcEltSizeInBits) / DstEltSizeInBits;
14146 DstUndefElements.clear();
14147 DstUndefElements.resize(NumDstOps, false);
14148 DstBitElements.assign(NumDstOps, APInt::getZero(DstEltSizeInBits));
14149
14150 // Concatenate src elements constant bits together into dst element.
14151 if (SrcEltSizeInBits <= DstEltSizeInBits) {
14152 unsigned Scale = DstEltSizeInBits / SrcEltSizeInBits;
14153 for (unsigned I = 0; I != NumDstOps; ++I) {
14154 DstUndefElements.set(I);
14155 APInt &DstBits = DstBitElements[I];
14156 for (unsigned J = 0; J != Scale; ++J) {
14157 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14158 if (SrcUndefElements[Idx])
14159 continue;
14160 DstUndefElements.reset(I);
14161 const APInt &SrcBits = SrcBitElements[Idx];
14162 assert(SrcBits.getBitWidth() == SrcEltSizeInBits &&
14163 "Illegal constant bitwidths");
14164 DstBits.insertBits(SrcBits, J * SrcEltSizeInBits);
14165 }
14166 }
14167 return;
14168 }
14169
14170 // Split src element constant bits into dst elements.
14171 unsigned Scale = SrcEltSizeInBits / DstEltSizeInBits;
14172 for (unsigned I = 0; I != NumSrcOps; ++I) {
14173 if (SrcUndefElements[I]) {
14174 DstUndefElements.set(I * Scale, (I + 1) * Scale);
14175 continue;
14176 }
14177 const APInt &SrcBits = SrcBitElements[I];
14178 for (unsigned J = 0; J != Scale; ++J) {
14179 unsigned Idx = (I * Scale) + (IsLittleEndian ? J : (Scale - J - 1));
14180 APInt &DstBits = DstBitElements[Idx];
14181 DstBits = SrcBits.extractBits(DstEltSizeInBits, J * DstEltSizeInBits);
14182 }
14183 }
14184}
14185
14187 for (const SDValue &Op : op_values()) {
14188 unsigned Opc = Op.getOpcode();
14189 if (!Op.isUndef() && Opc != ISD::Constant && Opc != ISD::ConstantFP)
14190 return false;
14191 }
14192 return true;
14193}
14194
14195std::optional<std::pair<APInt, APInt>>
14197 unsigned NumOps = getNumOperands();
14198 if (NumOps < 2)
14199 return std::nullopt;
14200
14201 unsigned EltSize = getValueType(0).getScalarSizeInBits();
14202 APInt Start, Stride;
14203 int FirstIdx = -1, SecondIdx = -1;
14204
14205 // Find the first two non-undef constant elements to determine Start and
14206 // Stride, then verify all remaining elements match the sequence.
14207 for (unsigned I = 0; I < NumOps; ++I) {
14209 if (Op->isUndef())
14210 continue;
14211 if (!isa<ConstantSDNode>(Op))
14212 return std::nullopt;
14213
14214 APInt Val = getConstantOperandAPInt(I).trunc(EltSize);
14215 if (FirstIdx < 0) {
14216 FirstIdx = I;
14217 Start = Val;
14218 } else if (SecondIdx < 0) {
14219 SecondIdx = I;
14220 // Compute stride using modular arithmetic. Simple division would handle
14221 // common strides (1, 2, -1, etc.), but modular inverse maximizes matches.
14222 // Example: <0, poison, poison, 0xFF> has stride 0x55 since 3*0x55 = 0xFF
14223 // Note that modular arithmetic is agnostic to signed/unsigned.
14224 unsigned IdxDiff = I - FirstIdx;
14225 APInt ValDiff = Val - Start;
14226
14227 // Step 1: Factor out common powers of 2 from IdxDiff and ValDiff.
14228 unsigned CommonPow2Bits = llvm::countr_zero(IdxDiff);
14229 if (ValDiff.countr_zero() < CommonPow2Bits)
14230 return std::nullopt; // ValDiff not divisible by 2^CommonPow2Bits
14231 IdxDiff >>= CommonPow2Bits;
14232 ValDiff.lshrInPlace(CommonPow2Bits);
14233
14234 // Step 2: IdxDiff is now odd, so its inverse mod 2^EltSize exists.
14235 // TODO: There are 2^CommonPow2Bits valid strides; currently we only try
14236 // one, but we could try all candidates to handle more cases.
14237 Stride = ValDiff * APInt(EltSize, IdxDiff).multiplicativeInverse();
14238 if (Stride.isZero())
14239 return std::nullopt;
14240
14241 // Step 3: Adjust Start based on the first defined element's index.
14242 Start -= Stride * FirstIdx;
14243 } else {
14244 // Verify this element matches the sequence.
14245 if (Val != Start + Stride * I)
14246 return std::nullopt;
14247 }
14248 }
14249
14250 // Need at least two defined elements.
14251 if (SecondIdx < 0)
14252 return std::nullopt;
14253
14254 return std::make_pair(Start, Stride);
14255}
14256
14258 // Find the first non-undef value in the shuffle mask.
14259 unsigned i, e;
14260 for (i = 0, e = Mask.size(); i != e && Mask[i] < 0; ++i)
14261 /* search */;
14262
14263 // If all elements are undefined, this shuffle can be considered a splat
14264 // (although it should eventually get simplified away completely).
14265 if (i == e)
14266 return true;
14267
14268 // Make sure all remaining elements are either undef or the same as the first
14269 // non-undef value.
14270 for (int Idx = Mask[i]; i != e; ++i)
14271 if (Mask[i] >= 0 && Mask[i] != Idx)
14272 return false;
14273 return true;
14274}
14275
14276// Returns true if it is a constant integer BuildVector or constant integer,
14277// possibly hidden by a bitcast.
14279 SDValue N, bool AllowOpaques) const {
14281
14282 if (auto *C = dyn_cast<ConstantSDNode>(N))
14283 return AllowOpaques || !C->isOpaque();
14284
14286 return true;
14287
14288 // Treat a GlobalAddress supporting constant offset folding as a
14289 // constant integer.
14290 if (auto *GA = dyn_cast<GlobalAddressSDNode>(N))
14291 if (GA->getOpcode() == ISD::GlobalAddress &&
14292 TLI->isOffsetFoldingLegal(GA))
14293 return true;
14294
14295 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14296 isa<ConstantSDNode>(N.getOperand(0)))
14297 return true;
14298 return false;
14299}
14300
14301// Returns true if it is a constant float BuildVector or constant float.
14304 return true;
14305
14307 return true;
14308
14309 if ((N.getOpcode() == ISD::SPLAT_VECTOR) &&
14310 isa<ConstantFPSDNode>(N.getOperand(0)))
14311 return true;
14312
14313 return false;
14314}
14315
14316std::optional<bool> SelectionDAG::isBoolConstant(SDValue N) const {
14317 ConstantSDNode *Const =
14318 isConstOrConstSplat(N, false, /*AllowTruncation=*/true);
14319 if (!Const)
14320 return std::nullopt;
14321
14322 EVT VT = N->getValueType(0);
14323 const APInt CVal = Const->getAPIntValue().trunc(VT.getScalarSizeInBits());
14324 switch (TLI->getBooleanContents(N.getValueType())) {
14326 if (CVal.isOne())
14327 return true;
14328 if (CVal.isZero())
14329 return false;
14330 return std::nullopt;
14332 if (CVal.isAllOnes())
14333 return true;
14334 if (CVal.isZero())
14335 return false;
14336 return std::nullopt;
14338 return CVal[0];
14339 }
14340 llvm_unreachable("Unknown BooleanContent enum");
14341}
14342
14343void SelectionDAG::createOperands(SDNode *Node, ArrayRef<SDValue> Vals) {
14344 assert(!Node->OperandList && "Node already has operands");
14346 "too many operands to fit into SDNode");
14347 SDUse *Ops = OperandRecycler.allocate(
14348 ArrayRecycler<SDUse>::Capacity::get(Vals.size()), OperandAllocator);
14349
14350 bool IsDivergent = false;
14351 for (unsigned I = 0; I != Vals.size(); ++I) {
14352 Ops[I].setUser(Node);
14353 Ops[I].setInitial(Vals[I]);
14354 EVT VT = Ops[I].getValueType();
14355
14356 // Skip Chain. It does not carry divergence.
14357 if (VT != MVT::Other &&
14358 (VT != MVT::Glue || gluePropagatesDivergence(Ops[I].getNode())) &&
14359 Ops[I].getNode()->isDivergent()) {
14360 IsDivergent = true;
14361 }
14362 }
14363 Node->NumOperands = Vals.size();
14364 Node->OperandList = Ops;
14365 if (!TLI->isSDNodeAlwaysUniform(Node)) {
14366 IsDivergent |= TLI->isSDNodeSourceOfDivergence(Node, FLI, UA);
14367 Node->SDNodeBits.IsDivergent = IsDivergent;
14368 }
14369 checkForCycles(Node);
14370}
14371
14374 size_t Limit = SDNode::getMaxNumOperands();
14375 while (Vals.size() > Limit) {
14376 unsigned SliceIdx = Vals.size() - Limit;
14377 auto ExtractedTFs = ArrayRef<SDValue>(Vals).slice(SliceIdx, Limit);
14378 SDValue NewTF = getNode(ISD::TokenFactor, DL, MVT::Other, ExtractedTFs);
14379 Vals.erase(Vals.begin() + SliceIdx, Vals.end());
14380 Vals.emplace_back(NewTF);
14381 }
14382 return getNode(ISD::TokenFactor, DL, MVT::Other, Vals);
14383}
14384
14386 EVT VT, SDNodeFlags Flags) {
14387 switch (Opcode) {
14388 default:
14389 return SDValue();
14390 case ISD::ADD:
14391 case ISD::OR:
14392 case ISD::XOR:
14393 case ISD::UMAX:
14394 return getConstant(0, DL, VT);
14395 case ISD::MUL:
14396 return getConstant(1, DL, VT);
14397 case ISD::AND:
14398 case ISD::UMIN:
14399 return getAllOnesConstant(DL, VT);
14400 case ISD::SMAX:
14402 case ISD::SMIN:
14404 case ISD::FADD:
14405 // If flags allow, prefer positive zero since it's generally cheaper
14406 // to materialize on most targets.
14407 return getConstantFP(Flags.hasNoSignedZeros() ? 0.0 : -0.0, DL, VT);
14408 case ISD::FMUL:
14409 return getConstantFP(1.0, DL, VT);
14410 case ISD::FMINNUM:
14411 case ISD::FMAXNUM: {
14412 // Neutral element for fminnum is NaN, Inf or FLT_MAX, depending on FMF.
14413 const fltSemantics &Semantics = VT.getFltSemantics();
14414 APFloat NeutralAF = !Flags.hasNoNaNs() ? APFloat::getQNaN(Semantics) :
14415 !Flags.hasNoInfs() ? APFloat::getInf(Semantics) :
14416 APFloat::getLargest(Semantics);
14417 if (Opcode == ISD::FMAXNUM)
14418 NeutralAF.changeSign();
14419
14420 return getConstantFP(NeutralAF, DL, VT);
14421 }
14422 case ISD::FMINIMUM:
14423 case ISD::FMAXIMUM: {
14424 // Neutral element for fminimum is Inf or FLT_MAX, depending on FMF.
14425 const fltSemantics &Semantics = VT.getFltSemantics();
14426 APFloat NeutralAF = !Flags.hasNoInfs() ? APFloat::getInf(Semantics)
14427 : APFloat::getLargest(Semantics);
14428 if (Opcode == ISD::FMAXIMUM)
14429 NeutralAF.changeSign();
14430
14431 return getConstantFP(NeutralAF, DL, VT);
14432 }
14433
14434 }
14435}
14436
14437/// Helper used to make a call to a library function that has one argument of
14438/// pointer type.
14439///
14440/// Such functions include 'fegetmode', 'fesetenv' and some others, which are
14441/// used to get or set floating-point state. They have one argument of pointer
14442/// type, which points to the memory region containing bits of the
14443/// floating-point state. The value returned by such function is ignored in the
14444/// created call.
14445///
14446/// \param LibFunc Reference to library function (value of RTLIB::Libcall).
14447/// \param Ptr Pointer used to save/load state.
14448/// \param InChain Ingoing token chain.
14449/// \returns Outgoing chain token.
14451 SDValue InChain,
14452 const SDLoc &DLoc) {
14453 assert(InChain.getValueType() == MVT::Other && "Expected token chain");
14455 Args.emplace_back(Ptr, Ptr.getValueType().getTypeForEVT(*getContext()));
14456 RTLIB::LibcallImpl LibcallImpl =
14457 Libcalls->getLibcallImpl(static_cast<RTLIB::Libcall>(LibFunc));
14458 if (LibcallImpl == RTLIB::Unsupported)
14459 reportFatalUsageError("emitting call to unsupported libcall");
14460
14461 SDValue Callee =
14462 getExternalSymbol(LibcallImpl, TLI->getPointerTy(getDataLayout()));
14464 CLI.setDebugLoc(DLoc).setChain(InChain).setLibCallee(
14465 Libcalls->getLibcallImplCallingConv(LibcallImpl),
14466 Type::getVoidTy(*getContext()), Callee, std::move(Args));
14467 return TLI->LowerCallTo(CLI).second;
14468}
14469
14471 assert(From && To && "Invalid SDNode; empty source SDValue?");
14472 auto I = SDEI.find(From);
14473 if (I == SDEI.end())
14474 return;
14475
14476 // Use of operator[] on the DenseMap may cause an insertion, which invalidates
14477 // the iterator, hence the need to make a copy to prevent a use-after-free.
14478 NodeExtraInfo NEI = I->second;
14479 if (LLVM_LIKELY(!NEI.PCSections)) {
14480 // No deep copy required for the types of extra info set.
14481 //
14482 // FIXME: Investigate if other types of extra info also need deep copy. This
14483 // depends on the types of nodes they can be attached to: if some extra info
14484 // is only ever attached to nodes where a replacement To node is always the
14485 // node where later use and propagation of the extra info has the intended
14486 // semantics, no deep copy is required.
14487 SDEI[To] = std::move(NEI);
14488 return;
14489 }
14490
14491 const SDNode *EntrySDN = getEntryNode().getNode();
14492
14493 // We need to copy NodeExtraInfo to all _new_ nodes that are being introduced
14494 // through the replacement of From with To. Otherwise, replacements of a node
14495 // (From) with more complex nodes (To and its operands) may result in lost
14496 // extra info where the root node (To) is insignificant in further propagating
14497 // and using extra info when further lowering to MIR.
14498 //
14499 // In the first step pre-populate the visited set with the nodes reachable
14500 // from the old From node. This avoids copying NodeExtraInfo to parts of the
14501 // DAG that is not new and should be left untouched.
14502 SmallVector<const SDNode *> Leafs{From}; // Leafs reachable with VisitFrom.
14503 DenseSet<const SDNode *> FromReach; // The set of nodes reachable from From.
14504 auto VisitFrom = [&](auto &&Self, const SDNode *N, int MaxDepth) {
14505 if (MaxDepth == 0) {
14506 // Remember this node in case we need to increase MaxDepth and continue
14507 // populating FromReach from this node.
14508 Leafs.emplace_back(N);
14509 return;
14510 }
14511 if (!FromReach.insert(N).second)
14512 return;
14513 for (const SDValue &Op : N->op_values())
14514 Self(Self, Op.getNode(), MaxDepth - 1);
14515 };
14516
14517 // Copy extra info to To and all its transitive operands (that are new).
14519 auto DeepCopyTo = [&](auto &&Self, const SDNode *N) {
14520 if (FromReach.contains(N))
14521 return true;
14522 if (!Visited.insert(N).second)
14523 return true;
14524 if (EntrySDN == N)
14525 return false;
14526 for (const SDValue &Op : N->op_values()) {
14527 if (N == To && Op.getNode() == EntrySDN) {
14528 // Special case: New node's operand is the entry node; just need to
14529 // copy extra info to new node.
14530 break;
14531 }
14532 if (!Self(Self, Op.getNode()))
14533 return false;
14534 }
14535 // Copy only if entry node was not reached.
14536 SDEI[N] = std::move(NEI);
14537 return true;
14538 };
14539
14540 // We first try with a lower MaxDepth, assuming that the path to common
14541 // operands between From and To is relatively short. This significantly
14542 // improves performance in the common case. The initial MaxDepth is big
14543 // enough to avoid retry in the common case; the last MaxDepth is large
14544 // enough to avoid having to use the fallback below (and protects from
14545 // potential stack exhaustion from recursion).
14546 for (int PrevDepth = 0, MaxDepth = 16; MaxDepth <= 1024;
14547 PrevDepth = MaxDepth, MaxDepth *= 2, Visited.clear()) {
14548 // StartFrom is the previous (or initial) set of leafs reachable at the
14549 // previous maximum depth.
14551 std::swap(StartFrom, Leafs);
14552 for (const SDNode *N : StartFrom)
14553 VisitFrom(VisitFrom, N, MaxDepth - PrevDepth);
14554 if (LLVM_LIKELY(DeepCopyTo(DeepCopyTo, To)))
14555 return;
14556 // This should happen very rarely (reached the entry node).
14557 LLVM_DEBUG(dbgs() << __func__ << ": MaxDepth=" << MaxDepth << " too low\n");
14558 assert(!Leafs.empty());
14559 }
14560
14561 // This should not happen - but if it did, that means the subgraph reachable
14562 // from From has depth greater or equal to maximum MaxDepth, and VisitFrom()
14563 // could not visit all reachable common operands. Consequently, we were able
14564 // to reach the entry node.
14565 errs() << "warning: incomplete propagation of SelectionDAG::NodeExtraInfo\n";
14566 assert(false && "From subgraph too complex - increase max. MaxDepth?");
14567 // Best-effort fallback if assertions disabled.
14568 SDEI[To] = std::move(NEI);
14569}
14570
14571#ifndef NDEBUG
14572static void checkForCyclesHelper(const SDNode *N,
14575 const llvm::SelectionDAG *DAG) {
14576 // If this node has already been checked, don't check it again.
14577 if (Checked.count(N))
14578 return;
14579
14580 // If a node has already been visited on this depth-first walk, reject it as
14581 // a cycle.
14582 if (!Visited.insert(N).second) {
14583 errs() << "Detected cycle in SelectionDAG\n";
14584 dbgs() << "Offending node:\n";
14585 N->dumprFull(DAG); dbgs() << "\n";
14586 abort();
14587 }
14588
14589 for (const SDValue &Op : N->op_values())
14590 checkForCyclesHelper(Op.getNode(), Visited, Checked, DAG);
14591
14592 Checked.insert(N);
14593 Visited.erase(N);
14594}
14595#endif
14596
14598 const llvm::SelectionDAG *DAG,
14599 bool force) {
14600#ifndef NDEBUG
14601 bool check = force;
14602#ifdef EXPENSIVE_CHECKS
14603 check = true;
14604#endif // EXPENSIVE_CHECKS
14605 if (check) {
14606 assert(N && "Checking nonexistent SDNode");
14609 checkForCyclesHelper(N, visited, checked, DAG);
14610 }
14611#endif // !NDEBUG
14612}
14613
14614void llvm::checkForCycles(const llvm::SelectionDAG *DAG, bool force) {
14615 checkForCycles(DAG->getRoot().getNode(), DAG, force);
14616}
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")
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:1421
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1023
static APInt getSignMask(unsigned BitWidth)
Get the SignMask for a specific bit width.
Definition APInt.h:230
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1555
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1406
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1685
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1400
LLVM_ABI APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition APInt.cpp:639
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1044
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1527
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:936
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1345
APInt abs() const
Get the absolute value.
Definition APInt.h:1810
LLVM_ABI APInt sadd_sat(const APInt &RHS) const
Definition APInt.cpp:2037
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1189
static APInt getBitsSet(unsigned numBits, unsigned loBit, unsigned hiBit)
Get a value with a block of bits set.
Definition APInt.h:259
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1677
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1503
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1118
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1655
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1411
LLVM_ABI APInt rotr(unsigned rotateAmt) const
Rotate right by rotateAmt.
Definition APInt.cpp:1165
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
Definition APInt.h:841
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1173
unsigned countr_zero() const
Count the number of trailing zero bits.
Definition APInt.h:1654
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1643
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1613
static LLVM_ABI APInt getSplat(unsigned NewLen, const APInt &V)
Return a value containing V broadcasted over NewLen bits.
Definition APInt.cpp:651
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sshl_sat(const APInt &RHS) const
Definition APInt.cpp:2097
LLVM_ABI APInt ushl_sat(const APInt &RHS) const
Definition APInt.cpp:2111
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1052
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
Definition APInt.h:555
LLVM_ABI APInt rotl(unsigned rotateAmt) const
Rotate left by rotateAmt.
Definition APInt.cpp:1152
LLVM_ABI void insertBits(const APInt &SubBits, unsigned bitPosition)
Insert the bits from a smaller APInt starting at bitPosition.
Definition APInt.cpp:397
void clearLowBits(unsigned loBits)
Set bottom loBits bits to 0.
Definition APInt.h:1450
unsigned logBase2() const
Definition APInt.h:1776
LLVM_ABI APInt uadd_sat(const APInt &RHS) const
Definition APInt.cpp:2047
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:834
LLVM_ABI APInt multiplicativeInverse() const
Definition APInt.cpp:1285
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1747
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1157
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:996
void setBits(unsigned loBit, unsigned hiBit)
Set the bits from loBit (inclusive) to hiBit (exclusive) to 1.
Definition APInt.h:1382
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:880
LLVM_ABI APInt byteSwap() const
Definition APInt.cpp:746
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1264
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:441
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
void clearBits(unsigned LoBit, unsigned HiBit)
Clear the bits from LoBit (inclusive) to HiBit (exclusive) to 0.
Definition APInt.h:1432
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1403
LLVM_ABI APInt extractBits(unsigned numBits, unsigned bitPosition) const
Return an APInt with the extracted bits [bitPosition,bitPosition+numBits).
Definition APInt.cpp:482
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
bool isOne() const
Determine if this is a value of 1.
Definition APInt.h:390
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition APInt.h:865
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:858
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1228
LLVM_ABI APInt ssub_sat(const APInt &RHS) const
Definition APInt.cpp:2056
An arbitrary precision integer that knows its signedness.
Definition APSInt.h:24
unsigned getSrcAddressSpace() const
unsigned getDestAddressSpace() const
static Capacity get(size_t N)
Get the capacity of an array that can hold at least N elements.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
This is an SDNode representing atomic operations.
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, offset addresses.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
BitVector & reset()
Definition BitVector.h:411
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
Definition BitVector.h:360
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
BitVector & set()
Definition BitVector.h:370
bool none() const
none - Returns true if none of the bits are set.
Definition BitVector.h:207
size_type size() const
size - Returns the number of bits in this bitvector.
Definition BitVector.h:178
const BlockAddress * getBlockAddress() const
The address of a basic block.
Definition Constants.h: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 an arithmetic sequence "<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:709
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:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
Machine Value Type.
SimpleValueType SimpleTy
static MVT getIntegerVT(unsigned BitWidth)
Abstract base class for all machine specific constantpool value subclasses.
virtual void addSelectionDAGCSEId(FoldingSetNodeID &ID)=0
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
LLVM_ABI int CreateStackObject(uint64_t Size, Align Alignment, bool isSpillSlot, const AllocaInst *Alloca=nullptr, uint8_t ID=0)
Create a new statically sized stack object, returning a nonnegative identifier to represent it.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
Function & getFunction()
Return the LLVM function that this machine code represents.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
A description of a memory reference used in the backend.
const MDNode * getRanges() const
Return the range tag for the memory reference.
Flags
Flags values. These may be or'd together.
@ MOVolatile
The memory access is volatile.
@ MODereferenceable
The memory access is dereferenceable (i.e., doesn't trap).
@ MOLoad
The memory access reads data.
@ MOInvariant
The memory access always returns the same value (or traps).
@ MOStore
The memory access writes data.
const MachinePointerInfo & getPointerInfo() const
Flags getFlags() const
Return the raw flags of the source value,.
This class contains meta information specific to a module.
An SDNode that represents everything that will be needed to construct a MachineInstr.
This class is used to represent an MGATHER node.
This class is used to represent an MLOAD node.
This class is used to represent an MSCATTER node.
This class is used to represent an MSTORE node.
This SDNode is used for target intrinsics that touch memory and need an associated MachineMemOperand.
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, EVT *LargestVT=nullptr) const
Determines the optimal series of memory ops to replace the memset / memcpy.
std::pair< SDValue, SDValue > LowerCallTo(CallLoweringInfo &CLI) const
This function lowers an abstract call to a function into an actual call.
Primary interface to the complete machine description for the target machine.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
const Triple & getTargetTriple() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const SelectionDAGTargetInfo * getSelectionDAGInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
virtual const TargetLowering * getTargetLowering() const
bool isOSDarwin() const
Is this a "Darwin" OS (macOS, iOS, tvOS, watchOS, DriverKit, XROS, or bridgeOS).
Definition Triple.h: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:2283
LLVM_ABI APInt fshl(const APInt &Hi, const APInt &Lo, const APInt &Shift)
Perform a funnel shift left.
Definition APInt.cpp:3184
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3020
LLVM_ABI APInt clmulh(const APInt &LHS, const APInt &RHS)
Perform a carry-less multiply, and return high-bits.
Definition APInt.cpp:3217
APInt abdu(const APInt &A, const APInt &B)
Determine the absolute difference of two APInts considered to be unsigned.
Definition APInt.h:2288
LLVM_ABI APInt avgFloorS(const APInt &C1, const APInt &C2)
Compute the floor of the signed average of C1 and C2.
Definition APInt.cpp:3114
LLVM_ABI APInt avgCeilS(const APInt &C1, const APInt &C2)
Compute the ceil of the signed average of C1 and C2.
Definition APInt.cpp:3124
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, bool isIntegerLike)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
ISD namespace - This namespace contains an enum which represents all of the SelectionDAG node types a...
Definition ISDOpcodes.h:24
LLVM_ABI CondCode getSetCCAndOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical AND between different comparisons of identical values: ((X op1 Y) & (X...
LLVM_ABI bool isConstantSplatVectorAllOnes(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are ~0 ...
bool isNON_EXTLoad(const SDNode *N)
Returns true if the specified node is a non-extending load.
NodeType
ISD::NodeType enum - This enum defines the target-independent operators for a SelectionDAG.
Definition ISDOpcodes.h:41
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:819
@ MERGE_VALUES
MERGE_VALUES - This node takes multiple discrete operands and returns them all as its individual resu...
Definition ISDOpcodes.h:261
@ CTLZ_ZERO_UNDEF
Definition ISDOpcodes.h:788
@ TargetConstantPool
Definition ISDOpcodes.h:189
@ MDNODE_SDNODE
MDNODE_SDNODE - This is a node that holdes an MDNode*, which is used to reference metadata in the IR.
@ STRICT_FSETCC
STRICT_FSETCC/STRICT_FSETCCS - Constrained versions of SETCC, used for floating-point operands only.
Definition ISDOpcodes.h:511
@ PTRADD
PTRADD represents pointer arithmetic semantics, for targets that opt in using shouldPreservePtrArith(...
@ DELETED_NODE
DELETED_NODE - This is an illegal value that is used to catch errors.
Definition ISDOpcodes.h:45
@ POISON
POISON - A poison node.
Definition ISDOpcodes.h:236
@ PARTIAL_REDUCE_SMLA
PARTIAL_REDUCE_[U|S]MLA(Accumulator, Input1, Input2) The partial reduction nodes sign or zero extend ...
@ VECREDUCE_SEQ_FADD
Generic reduction nodes.
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ FGETSIGN
INT = FGETSIGN(FP) - Return the sign bit of the specified floating point value as an integer 0/1 valu...
Definition ISDOpcodes.h:538
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:275
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:600
@ JUMP_TABLE_DEBUG_INFO
JUMP_TABLE_DEBUG_INFO - Jumptable debug info.
@ BSWAP
Byte Swap and Counting operators.
Definition ISDOpcodes.h:779
@ TargetBlockAddress
Definition ISDOpcodes.h:191
@ DEACTIVATION_SYMBOL
Untyped node storing deactivation symbol reference (DeactivationSymbolSDNode).
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADDC
Carry-setting nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:294
@ FMAD
FMAD - Perform a * b + c, while getting the same result as the separately rounded operations.
Definition ISDOpcodes.h:522
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:264
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:853
@ ATOMIC_LOAD_USUB_COND
@ FMA
FMA - Perform a * b + c with no intermediate rounding step.
Definition ISDOpcodes.h:518
@ FATAN2
FATAN2 - atan2, inspired by libm.
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:220
@ GlobalAddress
Definition ISDOpcodes.h:88
@ ATOMIC_CMP_SWAP_WITH_SUCCESS
Val, Success, OUTCHAIN = ATOMIC_CMP_SWAP_WITH_SUCCESS(INCHAIN, ptr, cmp, swap) N.b.
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:880
@ CONCAT_VECTORS
CONCAT_VECTORS(VECTOR0, VECTOR1, ...) - Given a number of values of vector type with the same length ...
Definition ISDOpcodes.h:584
@ VECREDUCE_FMAX
FMIN/FMAX nodes can have flags, for NaN/NoNaN variants.
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:417
@ VECREDUCE_FMAXIMUM
FMINIMUM/FMAXIMUM nodes propatate NaNs and signed zeroes using the llvm.minimum and llvm....
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
Definition ISDOpcodes.h:747
@ SIGN_EXTEND_VECTOR_INREG
SIGN_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register sign-extension of the low ...
Definition ISDOpcodes.h:910
@ FP16_TO_FP
FP16_TO_FP, FP_TO_FP16 - These operators are used to perform promotions and truncation for half-preci...
@ FMULADD
FMULADD - Performs a * b + c, with, or without, intermediate rounding.
Definition ISDOpcodes.h:528
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
Definition ISDOpcodes.h:993
@ BUILD_PAIR
BUILD_PAIR - This is the opposite of EXTRACT_ELEMENT in some ways.
Definition ISDOpcodes.h:254
@ CLMUL
Carry-less multiplication operations.
Definition ISDOpcodes.h:774
@ FLDEXP
FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
@ BUILTIN_OP_END
BUILTIN_OP_END - This must be the last enum value in this list.
@ GlobalTLSAddress
Definition ISDOpcodes.h:89
@ SRCVALUE
SRCVALUE - This is a node type that holds a Value* that is used to make reference to a value in the L...
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
@ ATOMIC_LOAD_USUB_SAT
@ PARTIAL_REDUCE_UMLA
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:844
@ AVGCEILS
AVGCEILS/AVGCEILU - Rounding averaging add - Add two integers using an integer of type i[N+2],...
Definition ISDOpcodes.h:715
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:665
@ TargetExternalSymbol
Definition ISDOpcodes.h:190
@ VECREDUCE_FADD
These reductions have relaxed evaluation order semantics, and have a single vector operand.
@ CTTZ_ZERO_UNDEF
Bit counting operators with an undefined result for zero inputs.
Definition ISDOpcodes.h:787
@ TargetJumpTable
Definition ISDOpcodes.h:188
@ TargetIndex
TargetIndex - Like a constant pool entry, but with completely target-dependent semantics.
Definition ISDOpcodes.h:198
@ PARTIAL_REDUCE_FMLA
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ TRUNCATE_SSAT_U
Definition ISDOpcodes.h:873
@ SETCCCARRY
Like SetCC, ops #0 and #1 are the LHS and RHS operands to compare, but op #2 is a boolean indicating ...
Definition ISDOpcodes.h:827
@ FNEG
Perform various unary floating-point operations inspired by libm.
@ BR_CC
BR_CC - Conditional branch.
@ SSUBO
Same for subtraction.
Definition ISDOpcodes.h:352
@ STEP_VECTOR
STEP_VECTOR(IMM) - Returns a scalable vector whose lanes are comprised of a linear sequence of unsign...
Definition ISDOpcodes.h:691
@ FCANONICALIZE
Returns platform specific canonical encoding of a floating point number.
Definition ISDOpcodes.h:541
@ SSUBSAT
RESULT = [US]SUBSAT(LHS, RHS) - Perform saturation subtraction on 2 integers with the same bit width ...
Definition ISDOpcodes.h:374
@ SELECT
Select(COND, TRUEVAL, FALSEVAL).
Definition ISDOpcodes.h:796
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ UNDEF
UNDEF - An undefined node.
Definition ISDOpcodes.h:233
@ EXTRACT_ELEMENT
EXTRACT_ELEMENT - This is used to get the lower or upper (determined by a Constant,...
Definition ISDOpcodes.h:247
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:672
@ AssertAlign
AssertAlign - These nodes record if a register contains a value that has a known alignment and the tr...
Definition ISDOpcodes.h:69
@ GET_ACTIVE_LANE_MASK
GET_ACTIVE_LANE_MASK - this corrosponds to the llvm.get.active.lane.mask intrinsic.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:230
@ SADDO
RESULT, BOOL = [SU]ADDO(LHS, RHS) - Overflow-aware nodes for addition.
Definition ISDOpcodes.h:348
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:185
@ ARITH_FENCE
ARITH_FENCE - This corresponds to a arithmetic fence intrinsic.
@ CTLS
Count leading redundant sign bits.
Definition ISDOpcodes.h:792
@ VECREDUCE_ADD
Integer reductions may have a result type larger than the vector element type.
@ MULHU
MULHU/MULHS - Multiply high - Multiply two integers of type iN, producing an unsigned/signed value of...
Definition ISDOpcodes.h:704
@ ATOMIC_LOAD_FMAXIMUM
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:765
@ AssertNoFPClass
AssertNoFPClass - These nodes record if a register contains a float value that is known to be not som...
Definition ISDOpcodes.h:78
@ VECTOR_SHUFFLE
VECTOR_SHUFFLE(VEC1, VEC2) - Returns a vector, of the same type as VEC1/VEC2.
Definition ISDOpcodes.h:649
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:614
@ FMINNUM_IEEE
FMINNUM_IEEE/FMAXNUM_IEEE - Perform floating-point minimumNumber or maximumNumber on two values,...
@ EntryToken
EntryToken - This is the marker used to indicate the start of a region.
Definition ISDOpcodes.h:48
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:576
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:224
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:850
@ TargetConstantFP
Definition ISDOpcodes.h:180
@ SELECT_CC
Select with condition operator - This selects between a true value and a false value (ops #2 and #3) ...
Definition ISDOpcodes.h:811
@ VSCALE
VSCALE(IMM) - Returns the runtime scaling factor used to calculate the number of elements within a sc...
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ FMINNUM
FMINNUM/FMAXNUM - Perform floating-point minimum maximum on two values, following IEEE-754 definition...
@ SSHLSAT
RESULT = [US]SHLSAT(LHS, RHS) - Perform saturation left shift.
Definition ISDOpcodes.h:386
@ SMULO
Same for multiplication.
Definition ISDOpcodes.h:356
@ ATOMIC_LOAD_FMINIMUM
@ TargetFrameIndex
Definition ISDOpcodes.h:187
@ VECTOR_SPLICE_LEFT
VECTOR_SPLICE_LEFT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1, VEC2) left by OFFSET elements an...
Definition ISDOpcodes.h:653
@ ANY_EXTEND_VECTOR_INREG
ANY_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register any-extension of the low la...
Definition ISDOpcodes.h:899
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:888
@ SMIN
[US]{MIN/MAX} - Binary minimum or maximum of signed or unsigned integers.
Definition ISDOpcodes.h:727
@ LIFETIME_START
This corresponds to the llvm.lifetime.
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:978
@ VSELECT
Select with a vector condition (op #0) and two vector operands (ops #1 and #2), returning a vector re...
Definition ISDOpcodes.h:805
@ UADDO_CARRY
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:328
@ MGATHER
Masked gather and scatter - load and store operations for a vector of random addresses with additiona...
@ HANDLENODE
HANDLENODE node - Used as a handle for various purposes.
@ BF16_TO_FP
BF16_TO_FP, FP_TO_BF16 - These operators are used to perform promotions and truncation for bfloat16.
@ ATOMIC_LOAD_UDEC_WRAP
@ STRICT_FP_ROUND
X = STRICT_FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision ...
Definition ISDOpcodes.h:500
@ FMINIMUM
FMINIMUM/FMAXIMUM - NaN-propagating minimum/maximum that also treat -0.0 as less than 0....
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:926
@ TargetConstant
TargetConstant* - Like Constant*, but the DAG does not do any folding, simplification,...
Definition ISDOpcodes.h:179
@ STRICT_FP_EXTEND
X = STRICT_FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
Definition ISDOpcodes.h:505
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:739
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:205
@ GET_FPENV_MEM
Gets the current floating-point environment.
@ PSEUDO_PROBE
Pseudo probe for AutoFDO, as a place holder in a basic block to improve the sample counts quality.
@ SCMP
[US]CMP - 3-way comparison of signed or unsigned integers.
Definition ISDOpcodes.h:735
@ AVGFLOORS
AVGFLOORS/AVGFLOORU - Averaging add - Add two integers using an integer of type i[N+1],...
Definition ISDOpcodes.h:710
@ VECTOR_SPLICE_RIGHT
VECTOR_SPLICE_RIGHT(VEC1, VEC2, OFFSET) - Shifts CONCAT_VECTORS(VEC1,VEC2) right by OFFSET elements a...
Definition ISDOpcodes.h:657
@ ADDE
Carry-using nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:304
@ SPLAT_VECTOR_PARTS
SPLAT_VECTOR_PARTS(SCALAR1, SCALAR2, ...) - Returns a vector with the scalar values joined together a...
Definition ISDOpcodes.h:681
@ FREEZE
FREEZE - FREEZE(VAL) returns an arbitrary value if VAL is UNDEF (or is evaluated to UNDEF),...
Definition ISDOpcodes.h:241
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:565
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:53
@ ATOMIC_SWAP
Val, OUTCHAIN = ATOMIC_SWAP(INCHAIN, ptr, amt) Val, OUTCHAIN = ATOMIC_LOAD_[OpName](INCHAIN,...
@ ExternalSymbol
Definition ISDOpcodes.h:93
@ FFREXP
FFREXP - frexp, extract fractional and exponent component of a floating-point value.
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:959
@ VECTOR_COMPRESS
VECTOR_COMPRESS(Vec, Mask, Passthru) consecutively place vector elements based on mask e....
Definition ISDOpcodes.h:699
@ ZERO_EXTEND_VECTOR_INREG
ZERO_EXTEND_VECTOR_INREG(Vector) - This operator represents an in-register zero-extension of the low ...
Definition ISDOpcodes.h:921
@ ADDRSPACECAST
ADDRSPACECAST - This operator converts between pointers of different address spaces.
Definition ISDOpcodes.h:997
@ EXPERIMENTAL_VECTOR_HISTOGRAM
Experimental vector histogram intrinsic Operands: Input Chain, Inc, Mask, Base, Index,...
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:945
@ VECREDUCE_FMINIMUM
@ TRUNCATE
TRUNCATE - Completely drop the high bits.
Definition ISDOpcodes.h:856
@ VAARG
VAARG - VAARG has four operands: an input chain, a pointer, a SRCVALUE, and the alignment.
@ VECREDUCE_SEQ_FMUL
@ SHL_PARTS
SHL_PARTS/SRA_PARTS/SRL_PARTS - These operators are used for expanded integer shift operations.
Definition ISDOpcodes.h:833
@ AssertSext
AssertSext, AssertZext - These nodes record if a register contains a value that has already been zero...
Definition ISDOpcodes.h:62
@ ATOMIC_LOAD_UINC_WRAP
@ FCOPYSIGN
FCOPYSIGN(X, Y) - Return the value of X with the sign of Y.
Definition ISDOpcodes.h:534
@ PARTIAL_REDUCE_SUMLA
@ SADDSAT
RESULT = [US]ADDSAT(LHS, RHS) - Perform saturation addition on 2 integers with the same bit width (W)...
Definition ISDOpcodes.h:365
@ SET_FPENV_MEM
Sets the current floating point environment.
@ FMINIMUMNUM
FMINIMUMNUM/FMAXIMUMNUM - minimumnum/maximumnum that is same with FMINNUM_IEEE and FMAXNUM_IEEE besid...
@ TRUNCATE_SSAT_S
TRUNCATE_[SU]SAT_[SU] - Truncate for saturated operand [SU] located in middle, prefix for SAT means i...
Definition ISDOpcodes.h:871
@ ABDS
ABDS/ABDU - Absolute difference - Return the absolute difference between two numbers interpreted as s...
Definition ISDOpcodes.h:722
@ TRUNCATE_USAT_U
Definition ISDOpcodes.h:875
@ SADDO_CARRY
Carry-using overflow-aware nodes for multiple precision addition and subtraction.
Definition ISDOpcodes.h:338
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:213
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:186
@ BUILD_VECTOR
BUILD_VECTOR(ELT0, ELT1, ELT2, ELT3,...) - Return a fixed-width vector with the specified,...
Definition ISDOpcodes.h:556
LLVM_ABI NodeType getOppositeSignednessMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns the corresponding opcode with the opposi...
LLVM_ABI bool isBuildVectorOfConstantSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantSDNode or undef.
LLVM_ABI NodeType getExtForLoadExtType(bool IsFP, LoadExtType)
bool isZEXTLoad(const SDNode *N)
Returns true if the specified node is a ZEXTLOAD.
bool matchUnaryFpPredicate(SDValue Op, std::function< bool(ConstantFPSDNode *)> Match, bool AllowUndefs=false)
Hook for matching ConstantFPSDNode predicate.
bool isExtOpcode(unsigned Opcode)
LLVM_ABI bool isConstantSplatVectorAllZeros(const SDNode *N, bool BuildVectorOnly=false)
Return true if the specified node is a BUILD_VECTOR or SPLAT_VECTOR where all of the elements are 0 o...
LLVM_ABI bool isVectorShrinkable(const SDNode *N, unsigned NewEltSize, bool Signed)
Returns true if the specified node is a vector where all elements can be truncated to the specified e...
LLVM_ABI bool isVPBinaryOp(unsigned Opcode)
Whether this is a vector-predicated binary operation opcode.
LLVM_ABI CondCode getSetCCInverse(CondCode Operation, EVT Type)
Return the operation corresponding to !(X op Y), where 'op' is a valid SetCC operation.
LLVM_ABI std::optional< unsigned > getBaseOpcodeForVP(unsigned Opcode, bool hasFPExcept)
Translate this VP Opcode to its corresponding non-VP Opcode.
bool isTrueWhenEqual(CondCode Cond)
Return true if the specified condition returns true if the two operands to the condition are equal.
LLVM_ABI std::optional< unsigned > getVPMaskIdx(unsigned Opcode)
The operand position of the vector mask.
unsigned getUnorderedFlavor(CondCode Cond)
This function returns 0 if the condition is always false if an operand is a NaN, 1 if the condition i...
LLVM_ABI std::optional< unsigned > getVPExplicitVectorLengthIdx(unsigned Opcode)
The operand position of the explicit vector length parameter.
bool isEXTLoad(const SDNode *N)
Returns true if the specified node is a EXTLOAD.
LLVM_ABI bool allOperandsUndef(const SDNode *N)
Return true if the node has at least one operand and all operands of the specified node are ISD::UNDE...
LLVM_ABI bool isFreezeUndef(const SDNode *N)
Return true if the specified node is FREEZE(UNDEF).
LLVM_ABI CondCode getSetCCSwappedOperands(CondCode Operation)
Return the operation corresponding to (Y op X) when given the operation for (X op Y).
LLVM_ABI std::optional< unsigned > getVPForBaseOpcode(unsigned Opcode)
Translate this non-VP Opcode to its corresponding VP Opcode.
MemIndexType
MemIndexType enum - This enum defines how to interpret MGATHER/SCATTER's index parameter when calcula...
LLVM_ABI bool isBuildVectorAllZeros(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are 0 or undef.
bool matchUnaryPredicateImpl(SDValue Op, std::function< bool(ConstNodeType *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant BUI...
LLVM_ABI bool isConstantSplatVector(const SDNode *N, APInt &SplatValue)
Node predicates.
LLVM_ABI NodeType getInverseMinMaxOpcode(unsigned MinMaxOpc)
Given a MinMaxOpc of ISD::(U|S)MIN or ISD::(U|S)MAX, returns ISD::(U|S)MAX and ISD::(U|S)MIN,...
LLVM_ABI bool matchBinaryPredicate(SDValue LHS, SDValue RHS, std::function< bool(ConstantSDNode *, ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTypeMismatch=false)
Attempt to match a binary predicate against a pair of scalar/splat constants or every element of a pa...
LLVM_ABI bool isVPReduction(unsigned Opcode)
Whether this is a vector-predicated reduction opcode.
bool matchUnaryPredicate(SDValue Op, std::function< bool(ConstantSDNode *)> Match, bool AllowUndefs=false, bool AllowTruncation=false)
Hook for matching ConstantSDNode predicate.
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
LLVM_ABI bool isBuildVectorOfConstantFPSDNodes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR node of all ConstantFPSDNode or undef.
bool isSEXTLoad(const SDNode *N)
Returns true if the specified node is a SEXTLOAD.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
LLVM_ABI bool isBuildVectorAllOnes(const SDNode *N)
Return true if the specified node is a BUILD_VECTOR where all of the elements are ~0 or undef.
LLVM_ABI NodeType getVecReduceBaseOpcode(unsigned VecReduceOpcode)
Get underlying scalar opcode for VECREDUCE opcode.
LoadExtType
LoadExtType enum - This enum defines the three variants of LOADEXT (load with extension).
LLVM_ABI bool isVPOpcode(unsigned Opcode)
Whether this is a vector-predicated Opcode.
LLVM_ABI CondCode getSetCCOrOperation(CondCode Op1, CondCode Op2, EVT Type)
Return the result of a logical OR between different comparisons of identical values: ((X op1 Y) | (X ...
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
LLVM_ABI Libcall getMEMCPY_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMCPY_ELEMENT_UNORDERED_ATOMIC - Return MEMCPY_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMSET_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMSET_ELEMENT_UNORDERED_ATOMIC - Return MEMSET_ELEMENT_UNORDERED_ATOMIC_* value for the given ele...
LLVM_ABI Libcall getMEMMOVE_ELEMENT_UNORDERED_ATOMIC(uint64_t ElementSize)
getMEMMOVE_ELEMENT_UNORDERED_ATOMIC - Return MEMMOVE_ELEMENT_UNORDERED_ATOMIC_* value for the given e...
bool sd_match(SDNode *N, const SelectionDAG *DAG, Pattern &&P)
initializer< Ty > init(const Ty &Val)
@ DW_OP_LLVM_arg
Only used in LLVM metadata.
Definition Dwarf.h:149
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:668
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
GenericUniformityInfo< SSAContext > UniformityInfo
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:344
@ Offset
Definition DWP.cpp:532
bool operator<(int64_t V1, const APSInt &V2)
Definition APSInt.h: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.
@ Undef
Value of the register doesn't matter.
LLVM_ABI SDValue peekThroughBitcasts(SDValue V)
Return the non-bitcasted source operand of V if it exists.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h: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
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h: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:763
MDNode * TBAAStruct
The tag for type-based alias analysis (tbaa struct).
Definition Metadata.h:783
MDNode * TBAA
The tag for type-based alias analysis.
Definition Metadata.h:780
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:77
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
void move(uint64_t Delta)
Moves the Offset and adjusts Length accordingly.
const ConstantDataArray * Array
ConstantDataArray pointer.
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h: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:535
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:539
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:61
bool isStrictlyPositive() const
Returns true if this value is known to be positive.
Definition KnownBits.h:114
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static LLVM_ABI KnownBits avgFloorS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgFloorS.
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h: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:54
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
static LLVM_ABI KnownBits clmul(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for clmul(LHS, RHS).
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
LLVM_ABI KnownBits truncUSat(unsigned BitWidth) const
Truncate with unsigned saturation (unsigned input -> unsigned output)
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
LLVM_ABI KnownBits truncSSatU(unsigned BitWidth) const
Truncate with signed saturation to unsigned (signed input -> unsigned output)
static LLVM_ABI KnownBits avgCeilS(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from APIntOps::avgCeilS.
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI bool isDereferenceable(unsigned Size, LLVMContext &C, const DataLayout &DL) const
Return true if memory region [V, V+Offset+Size) is known to be dereferenceable.
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
PointerUnion< const Value *, const PseudoSourceValue * > V
This is the IR pointer value for the access, or it is null if unknown.
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
static MemOp Set(uint64_t Size, bool DstAlignCanChange, Align DstAlign, bool IsZeroMemset, bool IsVolatile)
static MemOp Copy(uint64_t Size, bool DstAlignCanChange, Align DstAlign, Align SrcAlign, bool IsVolatile, bool MemcpyStrSrc=false)
static StringRef getLibcallImplName(RTLIB::LibcallImpl CallImpl)
Get the libcall routine name for the specified libcall implementation.
These are IR-level optimization flags that may be propagated to SDNodes.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.
unsigned int NumVTs
Clients of various APIs that cause global effects on the DAG can optionally implement this interface.
virtual void NodeDeleted(SDNode *N, SDNode *E)
The node N that was deleted and, if E is not null, an equivalent node E that replaced it.
virtual void NodeInserted(SDNode *N)
The node N that was inserted.
virtual void NodeUpdated(SDNode *N)
The node N that was updated.
This structure contains all information that is necessary for lowering calls.
CallLoweringInfo & setLibCallee(CallingConv::ID CC, Type *ResultType, SDValue Target, ArgListTy &&ArgsList)
CallLoweringInfo & setDiscardResult(bool Value=true)
CallLoweringInfo & setDebugLoc(const SDLoc &dl)
CallLoweringInfo & setTailCall(bool Value=true)
CallLoweringInfo & setChain(SDValue InChain)